// 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("sustainingotheramount")) MM_findObj("sustainingotheramount").disabled = true;
	if (MM_findObj("sustainingotheramount")) MM_findObj("sustainingotheramount").className = "inputwhite";
	if (MM_findObj("regularotheramount")) MM_findObj("regularotheramount").disabled = true;
	if (MM_findObj("regularotheramount")) MM_findObj("regularotheramount").className = "inputwhite";
	if (MM_findObj("founderotheramount")) MM_findObj("founderotheramount").disabled = true;
	if (MM_findObj("founderotheramount")) MM_findObj("founderotheramount").className = "inputwhite";
}

function setAmount (amount,type) {
	MM_findObj("type").value = type;
	recurring  = (type=="sustaining" || type == "founder" || type == "lifetime1500installment" || type == "lifetimejoint2500installment" ? "Y" : "N");
	MM_findObj("cnp_recurring").value = recurring;
//	alert("CNP RECURRING IS: "+ MM_findObj("cnp_recurring").value);
//	blah = amount?amount.split(".")[0]:"";
//	alert(amount + " " + type + " recurring is: "+recurring + " gebi is " + (""+type+ blah));
//	obj = gebi(""+type+blah);
//	if (obj) obj.checked = true;
	if (!amount) {amount = "0" }
	if (amount.indexOf(".")<0) { amount += ".00";}
//	MM_findObj("annualchoice").disabled = (recurring=="Y");
//	MM_findObj("monthlychoice").disabled = (recurring=="N");
//	MM_findObj("monthlygift1").disabled = (recurring=="N");
//	MM_findObj("monthlygift2").disabled = (recurring=="N");
	if (MM_findObj("amount")) MM_findObj("amount").value = amount;
	oncheck = MM_findObj("divchecknumamount");
	if (oncheck) oncheck.innerHTML = amount;
	atbottom = MM_findObj("amountHTML");
	monthly = recurring=="Y"?" each month":"";
	if (atbottom) atbottom.innerHTML = amount==0?
		"Please specify an <a href='#amt'>amount</a>.":
		("<B>$" + amount + 
			(MM_findObj("payment_type").value=="ec"?
				(" will be withdrawn using from your checking account"+monthly+".</B><BR>Remember to void the physical check you just used."):
				(" will be charged on your card"+monthly+".</B>")
			)
		);
}
// Check Form	on submit
function checkAmount(epform) {
//	alert("CHECK");
  epform.submit_button.disabled = true;
	epform.submit_button.value = "Please Wait...";

	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.submit_button.disabled = false;
		epform.submit_button.value = "Submit Donation";

		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.submit_button.disabled = true;
 	epform.submit_button.value = "Please Wait...";
	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 cnp_security = epform.cnp_security.value;
	
	var cc_number = epform.cc_num.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; 
	var gift1 = epform.monthlygift1.value; 
	var gift2 = epform.monthlygift2.value; 
	
//	alert("RECURRING IS: "+recurring+" because cnp_recurring is "+epform.cnp_recurring.value);
//	alert(type+amount);
//	alert ("Recurring is "+ recurring + " and discount is " + discount);

	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 (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 (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);
		var str = epform.cc_type.options[epform.cc_type.selectedIndex].value;
		str=str.slice(0,3);
		str=str.toLowerCase();
		if (str=='non') { error_message += "* Please select a credit card type.\n"; error = 1; }
		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) { error_message += "* Credit card expiration date is invalid.\n";  error = 1; }
/*
	  if (!epform.no_cnp_security.checked) {
	    if (!epform.cnp_security.value) {
	      error_message += "* Card Security Code Is Missing.\n";
	      error = 1;
	    }
	    else {
	      var cnp_val = epform.cnp_security.value;
	      if (false) { // cnp_val.length != cnp_len) { //jbs
	        error_message += "* Card Security Code Is Invalid.\n";
			error = 1;
	        epform.cnp_security.focus();
	        epform.cnp_security.select();
	      }
	    }
	  }
*/
	} 
	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 (recurring && !top.giftalerted) {
		if ( gift1 == "blank" ) {	
			error_message = error_message + "* Please select a gift to receive as thanks for your monthly membership.\n"; 
			error = 1;  
			top.giftalerted = true;}
		if ( gift2 == "blank" && amount>50 ) {	
			error_message = error_message + "* Please select a gift to receive as thanks for your +$50 monthly membership.\n"; 
			error = 1;  
			top.giftalerted = true;}
	}
	if (error == 1) { 
		error_message += "\nClick OK to modify your donation form, then click Submit Donation again.";
		alert(error_message);
		if (false) { }  //email=='james@caclean.org' || email=='trent@caclean.org' || email=='james@saxon.com') return true
		else {
			epform.submit_button.disabled = false;
			epform.submit_button.value = "Submit Donation";
			return false;
		}
	} else { return true; }
}
