// Page functions

function newImage(arg) {	if (document.images) {	rslt = new Image();		rslt.src = arg;		return rslt;	} }
function mouseHomeSpecial(suffix) {  //	alert("home: " + suffix);
	if (suffix=="") { MM_swapImage('home','','/images/navigation3/home.gif',1);			//			alert("TIMEOUT: "+parent.frameNav.HomeTimer.id); 
	}	else {	MM_swapImage('home','','/images/navigation3/home_'+suffix+'.gif',1);		}
}

// Form Functions

function disableOthers() {
	if (MM_findObj("regularotheramount")) MM_findObj("regularotheramount").disabled = true;
	if (MM_findObj("regularotheramount")) MM_findObj("regularotheramount").className = "inputwhite";
}

function ccActive(thepulldown) {
	active = thepulldown.value=="paypal"?'disabled':'';
	gebi('cc_num').disabled = active;
	gebi('month').disabled = active;
	gebi('year').disabled = active;
	gebi('cc_cvv2').disabled = active;
	gebi('address_billing').disabled = active;
	gebi('city_billing').disabled = active;
	gebi('zipcode_billing').disabled = active;
	setSubmitText();
	//Clicking Submit will transfer you to PayPal for your contribution of $XXX.After you approve the use of Paypal, you will be returned to finish your contribution.

}

function setSubmitText() {
	active = !gebi('cc_type_paypal').checked;
	amount = gebi("amount").value;
//	submitNote = gebi("submitnoteHTML");
//	submitNote.innerHTML = active?"&nbsp;We'll email you a receipt. ":"";
	submitText = gebi("amountHTML");
	submitText.innerHTML = amount==0?
		"Please specify an <a href='#amt'>amount</a>.":
		!active?
			"<b>Clicking Submit will transfer you to PayPal for your contribution of $"+amount+".<br>After you approve the use of Paypal, you will be returned to finish your contribution.</b>":
			("<b>$" + amount + 
			(MM_findObj("payment_type").value=="ec"?
				(" will be posted against your checking account.</B><BR>Remember to void the physical check you just used.</b>"):
				(" will be posted against your account.</b>")
			)
		);

}
		

function setAmount (amount,type) {
	MM_findObj("type").value = type;
	recurring  = "N";
	MM_findObj("cnp_recurring").value = recurring;
	if (!amount) {amount = "0" }
	foo = type+amount;  // WHY ISN'T ANY OF THIS WORKING???
	if (amount.indexOf(".")<0) { amount += ".00";}
	if (MM_findObj("amount")) MM_findObj("amount").value = amount;
	if (gebi(foo)) {	gebi(foo).checked = true; 	}
	oncheck = MM_findObj("divchecknumamount");
	setSubmitText();
}


function checkAmount(epform) {
  epform.ep_submit.disabled = true;

  var error = 0;
  var error_message = "Some things on your donation seem to be missing or invalid:\n\n";
	var amount = epform.amount.value;
	var type = epform.type.value; 
	var recurring = (epform.cnp_recurring.value=="Y");
	
	if (amount == "" || amount==0) { error_message = error_message + "* Your donation amount is missing.\n"; error = 1; } 
	else { 
		if (!(amountReg.exec(amount))) { error_message = error_message + "* Your donation amount is invalid.  Enter a valid amount e.g. 49.99.\n";	error = 1;	} 
//		else if (amount<35 && type == "regular") { error_message += "* To become a member, you must donate $35 a year.\n"; error = 1; }
//		else if (amount<5 && type == "sustaining") { error_message += "* To become a Sustaining Member, you must donate at least $5 a month.\n"; error = 1; }
//		else if (amount<100 && type == "founder") { error_message += "* To become a member of the Founder's Circle, you must donate at least $100 a month.\n"; error = 1; }
	}
	if (error == 1) { 
		error_message += "\nTo modify your donation, click OK.";
		alert(error_message);
		epform.ep_submit.disabled = false;
		if (email=='james@caclean.org' || email=='trent@caclean.org' || email=='james@saxon.com') return true
		else return false;
	} else { return true; }
}

function setMethod(showtype) {
	hidetype = ((showtype=="cc")?"ec":"cc");
//	MM_swapImage(showtype+"button","","/images/"+showtype+"_show.gif",hidetype+"button","","/images/"+hidetype+"_hide.gif");
//	MM_findObj('payment_type').value = showtype;
	setAmount(MM_findObj('amount').value,MM_findObj('type').value);
//	showhideMethod(showtype,'show');
//	showhideMethod(hidetype,'hide');
}

// Evaluate Form Function
function checkForm(epform) {
  epform.ep_submit.disabled = true;
  var error = 0;
  var error_message = "There are some things on your donation that seem to be missing or invalid.\nHere are the issues:\n\n";
  var payment_type = epform.payment_type.value;
  var payment_value = null;

/*  if (epform.payment_type.length) {
    for (var i = 0; i < epform.payment_type.length; i++)
      if (epform.payment_type[i].checked)
        payment_value = epform.payment_type[i].value;
  } else if (epform.payment_type.checked) {
    payment_value = epform.payment_type.value;
  } else if (epform.payment_type.value) {
    payment_value = epform.payment_type.value;
  } 
*/

//foo = ""; for (i=0;i<epform.length;i++) { foo += epform[i].name +":"+ epform[i].value + "\n"; } alert(foo);

	payment_value = payment_type;
	var first_name = epform.first_name.value;
	var last_name = epform.last_name.value;
	var address1 = epform.address1.value;
//	var address2 = epform.address2.value;
	var city = epform.city.value;
	var state = epform.state.value;
	var zip = epform.zipcode.value;
	var email = epform.useremail.value;
	var phone = epform.phone.value;
	var phonecell = epform.phonecell.value;
	var occupation = epform.occupation.value;
	var employer = epform.employer.value;
	
	var billing_address = epform.address_billing.value;
	var billing_city = epform.city_billing.value;
	var billing_zip = epform.zipcode_billing.value;
	//  var cnp_security = epform.cnp_security.value;

	var cc_type = epform.cc_type.value;
	var cc_number = epform.cc_num.value;
	var cc_cvv2 = epform.cc_cvv2.value;
//	var ec_rt = epform.ec_rt.value;
//	var ec_account = epform.ec_account.value;
//	var ec_account_type = epform.ec_account_type.value;
			//  var ec_id_type = epform.ec_id_type.value;
			//  var ec_id_number = epform.ec_id_number.value;
			//  var ec_id_state = epform.ec_id_state.value;
			//  var ec_bank_name = epform.ec_bank_name.value;
//	var ec_serial_number = epform.ec_serial_number.value;
	var amount = epform.amount.value;
	var recurring = (epform.cnp_recurring.value=="Y");
	var type = epform.type.value; 
	
//	alert("RECURRING IS: "+recurring+" because cnp_recurring is "+epform.cnp_recurring.value);
//	alert(type+amount);
//	alert ("Recurring is "+ recurring + " and discount is " + discount);

  billing_yes = billing_address != "" || billing_city != "" || billing_zip != "";
	if (amount == "" || amount==0) { error_message = error_message + "* Your donation amount is missing.\n"; error = 1; } 
	else { 
		if (!(amountReg.exec(amount))) { error_message = error_message + "* Your donation amount is invalid.  Enter a valid amount e.g. 49.99.\n";	error = 1;	} 
//		else if (amount<35 && type == "regular") { error_message += "* To become a member, you must donate $35 a year.\n"; error = 1; }
//		else if (amount<5 && type == "sustaining") { error_message += "* To become a Sustaining Member, you must donate at least $5 a month.\n"; error = 1; }
//		else if (amount<100 && type == "founder") { error_message += "* To become a member of the Founder's Circle, you must donate at least $100 a month.\n"; error = 1; }
	}
	if (first_name == "" || last_name == "") { error_message = error_message + "* First name and last name are both required.\n"; error = 1;}
	if (address1 == "" || city  == "" || state  == "" || zip == "") {	error_message = error_message + "* Address is not complete.\n";	error = 1; }
	if (!validZip(zip)) { error_message = error_message + "* Your zipcode must be five digits.\n";	error = 1; }
	if (phone == "") {	error_message = error_message + "* Your phone number is required.\n";	error = 1; }
	if (employer == "") {	error_message = error_message + "* Your employer name (or 'none') is required.\n";	error = 1; }
	if (occupation == "") {	error_message = error_message + "* Your occupation is required.\n";	error = 1; }
//	if (phone == "") {	error_message = error_message + "* Phone number is missing.\n"; error = 1; } 
//	else { 
//		if (!(phoneReg.exec(phone))) { error_message = error_message + "* Phone number is invalid.\n"; error = 1;  }
//	}
	if (email == "") {	error_message = error_message + "* Email address is missing.\n"; error = 1; } 
	else {
		if (!(!emailReg1.exec(email) && emailReg2.test(email))) {	error_message = error_message + "* Email address is not valid.\n"; error = 1;  }
	}	
	if (billing_yes) {
		if (billing_address == "" || billing_city == "" || billing_zip == "") { error_message = error_message + "* Billing address is incomplete.\n"; error = 1; }
	if (!validZip(zip)) { error_message = error_message + "* Your billing zipcode must be five digits.\n";	error = 1; }
	} else { 
		billing_address = address1;
		billing_city = city;
		billing_zip = zip;
	}

	if (payment_value == "cc") {
		var cc = '';
		var cclen = 0;
		var ccid = 0;
		var myDate = new Date();
		var thisMonth = myDate.getMonth();
		var thisYear = myDate.getFullYear();
		var year = epform.year.options[epform.year.selectedIndex].value;
		var month = epform.month.options[epform.month.selectedIndex].value;
		var cclen;
		var ccid;
		month = month.substr(0,2);
		str = 'non';
		for (i=0;i<4;i++) {
			if (epform.cc_type[i].checked) str = epform.cc_type[i].value;
//			alert(epform.cc_type[i].checked);
		}
		//if (epform.cc_type.value == undefined) { str = "non"; } 
		//else { var str = epform.cc_type.value; }
		str=str.slice(0,3);
		str=str.toLowerCase();
		if (str=='non') { error_message += "* Please select a credit card type.\n"; error = 1; }
		if (str=='pay') {}
		else if (epform.cc_num.value) {
			if (epform.cc_num.length==0) { error_message += "* Credit card number is missing.\n"; error = 1; }
			else {
				var cc = epform.cc_num.value;
				cc=cc.replace(/[^0-9]/g, "")
				cclen=cc.length;
				ccid=cc.charAt(0); 
				switch (str) {
					case "vis":
						if (ccid != 4) { error_message += "* Credit card number " + cc + " is not a valid Visa number.\n";  error = 1;  }
						if (cclen != 16) { error_message += "* You have " + cclen + " digits in your credit card.  16 digits are required.\n";  error = 1;  }
						var cnp_len = 3;
						break;
					case "mas":
						if (ccid != 5) {	error_message += "* Credit card number " + cc + " is not a valid MasterCard number.\n"; error = 1;  }
						if (cclen != 16) { error_message += "* You have " + cclen + " digits in your credit card.  16 digits are required.\n"; error = 1;  }
						var cnp_len = 3;
						break;
					case "dis":
						if (ccid != 6) { error_message += "* Credit card number " + cc + " is not a valid Discover number.\n";	  error = 1; }
						if (cclen != 16) { error_message += "* You have " + cclen + " digits in your credit card.  16 digits are required.\n";  error = 1; }
						var cnp_len = 4;
						break;
					case "ame":
						if (ccid != 3) { error_message += "* Credit card number " + cc + " is not a valid American Express number.\n";  error = 1;  }
						if (cclen != 15) { error_message += "* You have " + cclen + " digits in your credit card.  15 digits are required.\n"; 	error = 1;  }
						var cnp_len = 4;
						break;
					default:
						//error_message += "* Please select a credit card type.\n"; error = 1;
						break; 
				}
			}
		}
		else {	error_message += "* Credit card number is missing.\n"; error = 1;  }
		if (year == thisYear && month < thisMonth && str!='pay') { error_message += "* Credit card expiration date is invalid.\n";  error = 1; }
//	  if (!epform.no_cnp_security.checked) {
		if (!cc_cvv2) { 	error_message += "* Card security code is missing.\n";	error = 1;	}
	   else {
			codelength = cc_cvv2.length;
			if ((str=='vis' || str=='mas') && codelength != 3) { 
				error_message += "* Card security code must have three digits.\n";
				error = 1;
			} else 	if (str=='ame' && codelength !=4) { 
				error_message += "* Card security code must have four	 digits.\n";
				error = 1;
			}
	  }
	} 
	if (payment_value == "ec") {
	//         if (ec_bank_name == "" || ec_serial_number == "" || ec_id_number == "" || ec_id_state == "" || ec_rt == "" || ec_account == "" || ec_account_type == "" || ec_id_type == "") {
		if ( phone == "" ) {	error_message = error_message + "* Your phone number is required when using an online check.\n"; error = 1;  }
		if ( ec_rt == "" ) {	error_message = error_message + "* The check routing number is missing.\n"; error = 1;  }
		if ( ec_account == "" ) {	error_message = error_message + "* The checking account number is missing.\n";	error = 1;  }
		if ( ec_serial_number == "" ) { error_message = error_message + "* The check number is missing.\n"; error = 1; }
	}
	if (error == 1) { 
		error_message += "\nClick OK to modify your donation form, then click Submit Donation again.";
		alert(error_message);
		epform.ep_submit.disabled = false;
		return false;
	} else { 		
		epform.ep_submit.value = "Please wait...";
		return true; 
	}
}
