// validation
var isProcessing=new Boolean(false);

function showDiv(divName){
	try {
		if (document.getElementById) 
			document.getElementById(divName).style.display = "";
		else if (document.all) 
			document.all[divName].style.display = "";
		else if (document.layers && document.layers[divName]) 
			document.layers[divName].style.display = "";
	}
	catch(err) {
		alert("showDiv: " + divName + "\n" + err.description);
	}
}
function hideDiv(divName){
	try {
		if (document.getElementById) 
			document.getElementById(divName).style.display = "none";
		else if (document.all) 
			document.all[divName].style.display = "none";
		else if (document.layers && document.layers[divName]) 
			document.layers[divName].style.display = "none";
	}
	catch(err) {
		alert("hideDiv: " + divName + "\n" + err.description);
	}
}
function localizeForm(){
	var countrySelector = MM_findObj('Country');
	var selectedCountry = countrySelector.options[countrySelector.selectedIndex].value;
	if (selectedCountry == 'US'){
		//show divs for United States
		showDiv('localize_address_US');
		showDiv('localize_city');
		showDiv('localize_US');
		//hide other divs
		hideDiv('localize_address_INT');
		hideDiv('localize_province');
		hideDiv('localize_postal_phone_INT');
	}
	else if (selectedCountry == 'CA'){
		//show divs for Canada
		showDiv('localize_address_US');
		showDiv('localize_city');
		showDiv('localize_province');
		showDiv('localize_postal_phone_INT');
		//hide other divs
		hideDiv('localize_address_INT');
		hideDiv('localize_US');
	}
	else{
		//show divs for international addresses
		showDiv('localize_address_INT');
		showDiv('localize_postal_phone_INT');
		//hide other divs
		hideDiv('localize_address_US');
		hideDiv('localize_city');
		hideDiv('localize_US');
		hideDiv('localize_province');
	}
}
// thanks to Lawrence O'Sullivan for the following functions
function ltrim(str){
	return str.replace(/^\s+/, '');
}
function rtrim(str) {
	return str.replace(/\s+$/, '');
}
function alltrim(str) {
	return str.replace(/^\s+|\s+$/g, '');
}
// thanks to Lawrence O'Sullivan for the preceding functions
function trim(inputString) {
   if (typeof inputString != "string") {return inputString;}
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") {retValue = retValue.substring(1, retValue.length);ch = retValue.substring(0, 1);}
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") {retValue = retValue.substring(0, retValue.length-1);ch = retValue.substring(retValue.length-1, retValue.length);}
   while (retValue.indexOf("  ") != -1) {retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length);}
   return retValue;
}
// validate form
function validate(frm){
	var bValid=new Boolean(true);
	//var submitButton = MM_findObj('Submit');
	//var formState = submitButton.childNodes[0].nodeValue;
/*	alert ("after formState");
	if (formState=="processing...") {
		return false;
	}
	else {
		submitButton.childNodes[0].nodeValue="processing...";
	}*/
	if (isProcessing==true) {
		alert ("The form has been submitted.");
		return false;
	}
	isProcessing=true;
	// cc expiration
	if (frm.expYR.selectedIndex < 1) {
		bValid = false;
		frm.expYR.focus();
		alert ("Please indicate the credit card expiration YEAR.");
	}
    if (frm.expMO.selectedIndex < 1) {
		bValid = false;
		frm.expMO.focus();
		alert ("Please indicate the credit card expiration MONTH.");
	}
	//compare selected date to hard-coded date because client-side processing is dependent on the time on the client computer which can be wrong.
	if ((frm.expYR.options[frm.expYR.selectedIndex].value == strYear) && (frm.expMO.selectedIndex < intMonth)) {
		bValid = false;
		frm.expMO.focus();
		alert ("The credit card has expired. Please check the CREDIT CARD EXPIRATION DATE.");
	}
	if (frm.cvv.value.length > 4 || frm.cvv.value.length < 3 || isNaN(frm.cvv.value)) {
	bValid = false;
	frm.cvv.focus();
	alert ("Please fill in the Card Verification Value.");
	}		
    if (frm.CreditCardNumber.value.length > 17 || frm.CreditCardNumber.value.length < 12 || isNaN(frm.CreditCardNumber.value)) {
		bValid = false;
		frm.CreditCardNumber.focus();
		alert ("Please fill in the CREDIT CARD NUMBER.");
	}
    if (frm.PaymentType.selectedIndex < 1) {
		bValid = false;
		frm.PaymentType.focus();
		alert ("Please select the CREDIT CARD TYPE.");
	}
	frm.Email2.value = trim(frm.Email2.value);
	frm.Email.value = trim(frm.Email.value);
	var emailfilter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	var e2result=emailfilter.test(frm.Email2.value);
	var e1result=emailfilter.test(frm.Email.value);
/*	if (e2result==false){
		bValid = false;
		frm.Email2.focus();
		alert ("Please provide a valid email address in the Confirm Email field.");
	}
	if (e1result==false){
		bValid = false;
		frm.Email.focus();
		alert ("Please provide a valid email address in the Email field.");
	}*/
	if ((e2result==false)||(e1result==false)||(frm.Email2.value!=frm.Email.value)){
		bValid = false;
		frm.Email.focus();
		alert ("Please provide the same valid email address in both the Email and Confirm Email fields.");
	}
	var CanProvs = 'AB,BC,MB,NB,NL,NT,NS,NU,ON,PE,QC,SK,YT';
	frm.Country.value = alltrim(frm.Country.value);
	if (frm.Country.value == "US"){
		frm.Phone1.value = alltrim(frm.Phone1.value);
		frm.Phone2.value = alltrim(frm.Phone2.value);
		frm.Phone3.value = alltrim(frm.Phone3.value);
		if (frm.Phone1.value.length != 3 || isNaN(frm.Phone1.value) || frm.Phone2.value.length != 3 || isNaN(frm.Phone2.value) || frm.Phone3.value.length != 4 || isNaN(frm.Phone3.value)) {
			bValid = false;
			frm.Phone1.focus();
			alert ("Please fill in the PHONE NUMBER fields.");
		}
		frm.Zip1.value = alltrim(frm.Zip1.value);
		frm.Zip2.value = alltrim(frm.Zip2.value);
		if (frm.Zip1.value.length != 5 || isNaN(frm.Zip1.value)) {
			bValid = false;
			frm.Zip1.focus();
			alert ("Please fill in the ZIP CODE field.");
		}
		var selectedState = frm.State.options[frm.State.selectedIndex].value;
		if ((selectedState == "---") || (selectedState == "")) {
			bValid = false;
			frm.State.focus();
			alert ("Please select a US State or Territory from the State list.");
		}
		frm.City.value = alltrim(frm.City.value);
		if (frm.City.value == "" || frm.City.value.length < 1) {
			bValid = false;
			frm.City.focus();
			alert ("Please fill in the CITY field.");
		}
		frm.Address1.value = alltrim(frm.Address1.value);
		if (frm.Address1.value == "" || frm.Address1.value.length < 1) {
			bValid = false;
			frm.Address1.focus();
			alert ("Please fill in the ADDRESS field.");
		}
	}
	else if (frm.Country.value == "CA"){
		frm.Phone.value = alltrim(frm.Phone.value);
		if (frm.Phone.value == "") {
			bValid = false;
			frm.Phone.focus();
			alert ("Please fill in the PHONE NUMBER field.");
		}
		frm.PostalCode.value = alltrim(frm.PostalCode.value);
		if (frm.PostalCode.value == "" ) {
			bValid = false;
			frm.PostalCode.focus();
			alert ("Please fill in the POSTAL CODE.");
		}
		var selectedProvince = frm.Province.options[frm.Province.selectedIndex].value;
		if ((selectedProvince == "---") || (selectedProvince == "")) {
			bValid = false;
			frm.Province.focus();
			alert ("Please select a Province from the Province list.");
		}
		frm.City.value = alltrim(frm.City.value);
		if (frm.City.value == "" || frm.City.value.length < 1) {
			bValid = false;
			frm.City.focus();
			alert ("Please fill in the CITY field.");
		}
		frm.Address1.value = alltrim(frm.Address1.value);
		if (frm.Address1.value == "" || frm.Address1.value.length < 1) {
			bValid = false;
			frm.Address1.focus();
			alert ("Please fill in the ADDRESS field.");
		}
	}
	else {
		frm.Phone.value = alltrim(frm.Phone.value);
		if (frm.Phone.value == "") {
			bValid = false;
			frm.Phone.focus();
			alert ("Please fill in the PHONE NUMBER field.");
		}
		frm.PostalCode.value = alltrim(frm.PostalCode.value);
		if (frm.PostalCode.value == "" ) {
			bValid = false;
			frm.PostalCode.focus();
			alert ("Please fill in the POSTAL CODE.");
		}
		frm.Address.value = alltrim(frm.Address.value);
		if (frm.Address.value == "" || frm.Address.value.length < 1) {
			bValid = false;
			frm.Address.focus();
			alert ("Please fill in the INTERNATIONAL ADDRESS field.");
		}
	}
	frm.LastName.value = alltrim(frm.LastName.value);
	if (frm.LastName.value == "" || frm.LastName.value.length < 1) {
		bValid = false;
		frm.LastName.focus();
		alert ("Please fill in the LAST NAME field.");
	}
	frm.FirstName.value = alltrim(frm.FirstName.value);
	if (frm.FirstName.value == "" || frm.FirstName.value.length < 1) {
		bValid = false;
		frm.FirstName.focus();
		alert ("Please fill in the FIRST NAME field.");
	}
	var donLevelValue = getCheckedValue(frm.donLevel);
/*	for (var i=0; i < frm.donLevel.length; i++) {
		if (frm.donLevel[i].checked) {
			donLevelValue = frm.donLevel[i].value;
		}
	}*/
	if (donLevelValue == "") {
		bValid = false;
		frm.donLevel.focus();
		alert ("Please select a donation option.");
	}
	else if (donLevelValue == "open") {
		frm.don_open.value = alltrim(frm.don_open.value);
		if (!isNaN(frm.don_open.value)){
			frm.don_open.value = Math.round(frm.don_open.value)
		}
		if ((frm.don_open.value.length > 0) && (isNaN(frm.don_open.value) || (frm.don_open.value < 5))) {
			bValid = false;
			frm.don_open.focus();
			alert ("Because of processing fees, if you are making a gift that is less than $5, we ask that you mail your donation to the address at the bottom of this form or call 435-644-2001 and select Option 5. Thank you for your generosity.");
		}
	}
	else if (donLevelValue == "open_m") {
		frm.don_open_m.value = alltrim(frm.don_open_m.value);
		if (!isNaN(frm.don_open_m.value)){
			frm.don_open_m.value = Math.round(frm.don_open_m.value)
		}
		if ((frm.don_open_m.value.length > 0) && (isNaN(frm.don_open_m.value) || (frm.don_open_m.value < 5))) {
			bValid = false;
			frm.don_open_m.focus();
			alert ("Because of processing fees, if you are making a gift that is less than $5, we ask that you mail your donation to the address at the bottom of this form or call 435-644-2001 and select Option 5. Thank you for your generosity.");
		}
	}
/*	if (bValid==true) {
		submitButton.childNodes[0].nodeValue="processing...";
	}
	else {
		submitButton.childNodes[0].nodeValue="Submit";
	}*/
	isProcessing=false;
	return bValid;
}
// End Validate Form Function
//return the value of the radio button that is checked
//return an empty string if none are checked, or
//there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

//set the radio button with the given value as being checked
//do nothing if there are no radio buttons
//if the given value does not exist, all the radio buttons
//are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}
