//
// JavaScript functions for CardDuper.
// Michael Benson - 3/16/04
//


var ns4;
var ie4;
var ns6;

// Test for which browser
if (document.layers) ns4 = true;
if (document.all) ie4 = true;
if ((document.getElementById) && (!document.all)) ns6 = true;


function SubmitFormSetVar(formname, pagename, varname, varvalue)
{
	theform = eval("document." + formname);
	thevar = eval("document." + formname + "." + varname);
	thevar.value = varvalue;
	if (pagename != "") {
		theform.action = pagename;
	}
	theform.submit();
	return true;
}

function NewPopupWindow(url, name, options)
{
	var mynewwindow = window.open(url, name, options);
	//mynewwindow.document.close();
	mynewwindow.focus();
}

function ShowDiv(which)
{
	if (ie4) {
		document.all[which].style.visibility = "visible";
	}
	if (ns6) {
		document.getElementById(which).style.visibility = "visible";
	}
	if (ns4) {
		document.layers[which].visibility = "show";
	}
}

function HideDiv(which)
{
	if (ie4) {
		document.all[which].style.visibility = "hidden";
	}
	if (ns6) {
		document.getElementById(which).style.visibility = "hidden";
	}
	if (ns4) {
		document.layers[which].visibility = "hide";
	}
}

function ShowHideCountry(divName, countrySelectName)
{
	var selectbox = eval("document." + countrySelectName);
	country = selectbox.options[selectbox.selectedIndex].value;
	if (country == "other") {
		ShowDiv(divName);
	} else {
		HideDiv(divName);
	}
}

function ShowHideStates(divUS, divCanada, divOther, countrySelectName)
{
	var selectbox = eval("document." + countrySelectName);
	country = selectbox.options[selectbox.selectedIndex].value;
	if (country == "USA") {
		ShowDiv(divUS);
		HideDiv(divCanada);
		HideDiv(divOther);
	} else if (country == "Canada") {
		HideDiv(divUS);
		ShowDiv(divCanada);
		HideDiv(divOther);
	} else {
		HideDiv(divUS);
		HideDiv(divCanada);
		ShowDiv(divOther);
	}
}

var stateNames = [
	'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'American Samoa', 'California',
	'Colorado', 'Connecticut', 'Delaware', 'Washington D.C.', 'Florida', 'F.S. of Micronesia',
	'Georgia', 'Guam', 'Hawaii', 'Idaho', 'Illinois', 'Indiana',
	'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Marshall Islands',
	'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'N. Mariana Islands',
	'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
	'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
	'Ohio', 'Oklahoma', 'Oregon', 'Palau', 'Pennsylvania', 'Puerto Rico',
	'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas',
	'Utah', 'Vermont', 'Virginia', 'Virgin Islands', 'Washington', 'West Virginia',
	'Wisconsin', 'Wyoming'
];

var provinceNames = [
	'Alberta', 'British Columbia', 'Manitoba', 'New Brunswick', 'Newfoundland',
	'Nova Scotia', 'Northwest Territories', 'Ontario', 'Prince Edward Island', 'Quebec', 
	'Saskatchewan', 'Yukon Territory', 'Nunavut'
];

var stateCodes = [
	'AL', 'AK', 'AZ', 'AR', 'AS', 'CA',
	'CO', 'CT', 'DE', 'DC', 'FL', 'FM',
	'GA', 'GU', 'HI', 'ID', 'IL', 'IN',
	'IA', 'KS', 'KY', 'LA', 'ME', 'MH',
	'MD', 'MA', 'MI', 'MN', 'MS', 'MP',
	'MO', 'MT', 'NE', 'NV', 'NH',
	'NJ', 'NM', 'NY', 'NC', 'ND',
	'OH', 'OK', 'OR', 'PW', 'PA', 'PR',
	'RI', 'SC', 'SD', 'TN', 'TX',
	'UT', 'VT', 'VA', 'VI', 'WA', 'WV',
	'WI', 'WY'
];

var provinceCodes = [
	'AB', 'BC', 'MB', 'NB', 'NF',
	'NS', 'NT', 'ON', 'PE', 'QC',
	'SK', 'YT', 'NU'
];

function ReFillStates(countrysel, statesel)
{
	var countrybox = eval("document." + countrysel);
	var statebox = eval("document." + statesel);
	country = countrybox.options[countrybox.selectedIndex].value;
	
	statebox.options.length = 0;
	statebox.options[0] = new Option("(whole country)", "all");
	if (country == "USA") {
		for (i = 0;  i < stateNames.length;  i++) {
			statebox.options[i+1] = new Option(stateNames[i] + " (" + stateCodes[i] + ")", stateCodes[i]);
		}
	} else if (country == "Canada") {
		for (i = 0;  i < provinceNames.length;  i++) {
			statebox.options[i+1] = new Option(provinceNames[i] + " (" + provinceCodes[i] + ")", provinceCodes[i]);
		}
	}
}

function CheckRangeRadio(name)
{
	var radioBtn = eval("document." + name);
	radioBtn[0].checked = false;
	radioBtn[1].checked = true;
}

function AddContent()
{
	document.contents.action = "add_content.php";
	document.contents.submit();
	return true;
}

function UpdateContent(id)
{
	if (document.content_form.name.value == "") {
		alert("Please supply a name.");
		return false;
	}

	document.content_form.action = "update_content.php?id=" + id;
	document.content_form.submit();
	return true;
}

function ModifyContent(num_contents)
{
	// Check that exactly one content is selected:
	numchecked = 0;
	id = "";
	for (i = 0;  i < num_contents;  i++) {
		obj = eval("document.contents.content_" + i);
		if (obj && obj.checked) {
			if (numchecked == 0) {
				obj2 = eval("document.contents.content_id_" + i);
				id = obj2.value;
			}
			numchecked++;
		}
	}
	if (numchecked == 0) {
		alert("Please select a content to modify (or just click the name).");
		return false;
	}
	if (numchecked > 1 && !confirm("You have selected more than one content.  Do you want to modify the first one selected?")) {
		return false;
	}

	document.contents.action = "modify_content.php?id=" + id;
	document.contents.submit();
	
	return true;
}

function DeleteContents(num_contents)
{
	// Check if at least one content is selected:
	atleast1 = false;
	for (i = 0;  i < num_contents;  i++) {
		obj = eval("document.contents.content_" + i);
		if (obj && obj.checked) {
			atleast1 = true;
			break;
		}
	}
	if (!atleast1) {
		alert("Please select at least one content to delete.");
		return false;
	}
	if (!confirm("Are you sure you want to delete the selected contents?")) {
		return false;
	}

	document.contents.action = "delete_contents.php";
	document.contents.submit();
	return true;
}

function SelectLithographs()
{
	numliths = document.lithoform.numliths.value;
	numsel = 0;
	lithsel = "";
	
	for (i = 1;  i <= 2;  i++) {
		parentlith = eval("self.opener.document.donationform.litho" + i);
		parentlith.value = "";
	}
	
	for (i = 0;  i < numliths;  i++) {
		obj = eval("document.lithoform.lith" + i);
		if (obj && obj.checked) {
			numsel++;
			parentlith = eval("self.opener.document.donationform.litho" + numsel);
			parentlith.value = obj.value;
			if (lithsel == "") {
				lithsel = "Lithographs selected: ";
			} else {
				lithsel = lithsel + ", ";
			}
			lithsel = lithsel + obj.value;
		}
		if (numsel >= 2) break;
	}
	
	SetLithoDiv(lithsel);
	window.close();
}

function SetLithoDiv(whichcontent)
{
	if (document.all || document.getElementById) {
		cross_el = self.opener.document.getElementById ?
			self.opener.document.getElementById("lithodiv"):
			self.opener.document.all["lithodiv"];
		cross_el.innerHTML = whichcontent;
	} else if (document.layers) {
		self.opener.document["d1"].document["d2"].document.write(whichcontent);
		self.opener.document["d1"].document["d2"].document.close();
	}
}

function UpdateKiwanisTotal()
{
	numitems = document.kiwanis.boxes.value;
	total = parseFloat(numitems) * 57.0;
	if (document.kiwanis.checkextra.checked) {
		total += 40;
	}
	document.kiwanis.total.value = total;
}

function UpdateKiwanisTotal2()
{
	monthly = document.kiwanis.monthly.value;
	if (monthly == "") {
		monthly = 0;
	}
	onetime = document.kiwanis.onetime.value;
	if (onetime == "") {
		onetime = 0;
	}
	total = parseFloat(monthly) + parseFloat(onetime);
	document.kiwanis.total.value = total;
}

function ClearKiwanisOneTime()
{
	document.kiwanis.onetime.value = "";
	return true;
}

function PlaceKiwanisOrder()
{
	if (document.kiwanis.chapter.options[document.kiwanis.chapter.selectedIndex].value == '0') {
		alert("Please select a chapter (or register a new one)");
		return false;
	}
	
	document.kiwanis.action = "verify_order.php";
	document.kiwanis.submit();
	return true;
}

function UpdateDonation(item)
{
	qty = eval("document.orderform.qty_" + item);
	sug = eval("document.orderform.sug_" + item);
	don = eval("document.orderform.don_" + item);
	don.value = parseFloat(qty.value * sug.value);
	UpdateDonationTotal();
}

function UpdateDonationTotal()
{
	numitems = document.orderform.numitems.value;
	total = parseFloat(0);
	for (i = 0;  i < numitems;  i++) {
		don = eval("document.orderform.don_" + i);
		val = don.value;
		if (val != "") {
			total += parseFloat(val);
		}
	}
	donextra = document.orderform.don_extra.value;
	if (donextra != "") {
		total += parseFloat(donextra);
	}
	document.orderform.don_total.value = parseFloat(total);
}

function UpdateInvoice()
{
	email = document.donationform.email.value;
	document.donationform.invoice.value = email;
}

function UpdateCustom()
{
	amount = document.donationform.amount.value;
	document.donationform.custom.value = amount;
}

function CalcDonationAmount()
{
	numbrochures = document.donationform.numbrochures.value;
	perbrochure = document.donationform.perbrochure.value;
	document.donationform.amount.value = numbrochures * perbrochure;
	document.donationform.item_name.value = "The Jesse Cause: " + numbrochures + " brochures at $" + perbrochure + " each";
}

function SetDonationAmount(amount)
{
	document.donationform.amount.value = amount;
	if (amount <= 50) {
		document.donationform.gift.value = "";
	} else if (amount <= 100) {
		document.donationform.gift.value = "Mother's Day Lithograph";
	} else if (amount <= 200) {
		document.donationform.gift.value = "1 Lithograph";
	} else if (amount <= 500) {
		document.donationform.gift.value = "2 Lithographs";
	} else if (amount <= 1000) {
		document.donationform.gift.value = "CD, 2 Lithographs";
	} else if (amount > 1000) {
		document.donationform.gift.value = "CD, Churchill Lithograph, 2 Lithographs";
	}
}

function DeleteStory(id)
{
	if (!confirm("Are you sure you want to delete this story? (" + id +")")) {
		return false;
	}
	
	document.review.action = "delete_story.php?id=" + id;
	document.review.submit();
	
	return true;
}
