
function goback() {
   history.go(-1);
   return true;
}

function updateGift (newValue) {
   document.Donations.giftamt.value = newValue;
   return false;
}

function updateRecommended (val) {
   if (val == document.Donations.rec_val_0.value) {
      document.Donations.recommended[0].checked = true;
   } else if (val == document.Donations.rec_val_1.value) {
      document.Donations.recommended[1].checked = true;
   } else if (val == document.Donations.rec_val_2.value) {
      document.Donations.recommended[2].checked = true;
   } else {
      document.Donations.recommended[0].checked = false;
      document.Donations.recommended[1].checked = false;
      document.Donations.recommended[2].checked = false;
   }
}

var opt1 = new Option('Visa', 'VS');
var opt2 = new Option('MasterCard', 'MC');
var opt3 = new Option('American Express', 'AX');
var opt4 = new Option('Discover', 'DS');
var opt5 = new Option('Diners Club', 'DN');

function updateCsc (val) {
	var reqstar = "<font class=reqstar>*</font>";
	var cclist = document.Donations.cctype;
	// alert("value: " + val);
	if (val != "US" && val != "") {
		if (cclist.options.length == 6) {
			cclist.options[5] = null;
			cclist.options[4] = null;
			cclist.options[3] = null;
		}
		var cscSpot = document.getElementById("cscReqSpot");
		cscSpot.innerHTML = reqstar;
		var numMask = document.getElementById("usnummask1");
		var numMask2 = document.getElementById("usnummask2");
		numMask.style.display = "none";
		numMask2.style.display = "none";
	} else {
		if (cclist.options.length == 3) {
			cclist.options[3] = opt3;
			cclist.options[4] = opt4;
			cclist.options[5] = opt5;
		}
		var cscSpot = document.getElementById("cscReqSpot");
		cscSpot.innerHTML = '';
		var numMask = document.getElementById("usnummask1");
		var numMask2 = document.getElementById("usnummask2");
		numMask.style.display = "inline";
		numMask2.style.display = "inline";
	}
}

function isEmpty (str) {
   if (!str || str == "" || str == null) {
      return true;
   } else if (str.search(/^\s*$/) != -1) {
      // make sure they're not just putting in whitespace :P
      return true;
   } else {
      return false;
   }
}

function killButton(str) {
   if (!true) {
      return false;
   } else {
      document.Donations.submit();
      document.Donations.submode[0].disabled = true;
      document.Donations.submode[1].disabled = true;
   }
}

function ValidateData () {
   // document.Donations.submitbtn.disabled = true;
   // Gift Amount entered?
   if (document.Donations.recommended[0].checked && isEmpty(document.Donations.giftamt.value)) {
      document.Donations.giftamt.value = document.Donations.recommended[0].value;
   }
   if (document.Donations.recommended[1].checked && isEmpty(document.Donations.giftamt.value)) {
      document.Donations.giftamt.value = document.Donations.recommended[1].value;
   }
   if (document.Donations.recommended[2].checked && isEmpty(document.Donations.giftamt.value)) {
      document.Donations.giftamt.value = document.Donations.recommended[2].value;
   }
   if (isEmpty(document.Donations.giftamt.value) || document.Donations.giftamt.value == "0") {
      // No, display error message
      alert("Please enter a gift amount.");
      document.Donations.giftamt.focus();
      return false;
   } else if (document.Donations.giftamt.value.search(/[^0-9]/) != -1) {
      // Yes, Gift Amount all numbers?
         // No, display error message
         alert("Please enter a valid number.");
         document.Donations.giftamt.focus();
         document.Donations.giftamt.select();
         return false;
   }
   // First Name/Last Name entered?
   if (isEmpty(document.Donations.firstname.value)) {
         // No, display error message
         alert("Please enter your first name.");
         document.Donations.firstname.focus();
         return false;
   }
   if (isEmpty(document.Donations.lastname.value)) {
      alert("Please enter your last name.");
      document.Donations.lastname.focus();
      return false;
   }
   // Street Address Entered?
   if (isEmpty(document.Donations.address1.value)) {
      // No, display error message
      alert("Please enter your address.");
      document.Donations.address1.focus();
      return false;
   }
   // City Entered?
   if (isEmpty(document.Donations.city.value)) {
      // No, display error message
      alert("Please enter your city.");
      document.Donations.city.focus();
      return false;
   }
   // State entered?
   if (isEmpty(document.Donations.state.value)) {
      alert("Please enter your state.");
      document.Donations.state.focus();
      return false;
   }
   // Zip Entered?
   if (isEmpty(document.Donations.zip.value)) {
      // No, display error message
      alert("Please enter your zip code.");
      document.Donations.zip.focus();
      return false;
   } else if (document.Donations.country.value == "US" && document.Donations.zip.value.search(/^[0-9]{5}(\-[0-9]{4})?$/) == -1) {
      alert("Invalid Zip Code");
      document.Donations.zip.focus();
      document.Donations.zip.select();
      return false;
   }
   // e-mail Entered?
   var emailaddr = document.Donations.emailaddress.value;
   if (isEmpty(document.Donations.emailaddress.value)) {
      // No, display error message
      alert("Please enter your email address.");
      document.Donations.emailaddress.focus();
      return false;
   } else if (emailaddr.search(/^\w+[\w\.\,\-]*@([A-Za-z][\w\-]*\w\.)+\w+$/) == -1) {
      // this regex may be a little too restrictive; I'm not sure what
      // various punctuation chars are routinely allowed in the <local-part>
      // without being escaped; I suspect it doesn't quite conform to RFC 821. :P
      alert("Invalid Email Address");
      document.Donations.emailaddress.focus();
      document.Donations.emailaddress.select();
      return false;
   }
	// if we get a home phone number, make sure it's a good one
   // if (isEmpty(document.Donations.hphone.value)) {
      // alert("Please enter a home phone number");
      // document.Donations.hphone.focus();
      // return false;
		// } else if... (search)
   if (!isEmpty(document.Donations.hphone.value)) {
   	if (document.Donations.country.value == "US" && document.Donations.hphone.value.search(/^[0-9]{3}\-[0-9]{3}\-[0-9]{4}(x[0-9]+)?$/) == -1) {
      	alert("Please enter a valid phone number");
      	document.Donations.hphone.focus();
      	return false;
		}
   }
   // if we get a work phone number, make sure it's a good one
   if (!isEmpty(document.Donations.wphone.value)) {
      if (document.Donations.country.value == "US" && document.Donations.wphone.value.search(/^[0-9]{3}\-[0-9]{3}\-[0-9]{4}(x[0-9]+)?$/) == -1) {
         alert("Please enter a valid phone number");
         document.Donations.wphone.focus();
         return false;
      }
   }
   // Credit Card Name entered?
   if (isEmpty(document.Donations.ccname.value)) {
      // No, display error message
      alert("Please enter your name as it appears on your credit card.");
      document.Donations.ccname.focus ();
      return false;
   }
   // Credit Card # entered?
   if (isEmpty(document.Donations.ccnum.value))
   {
      // No, display error message
      alert("Please enter your credit card number.");
      document.Donations.ccnum.focus ();
      return false;
   } else {
      var a = document.Donations.ccnum.value;
      // should stick some more robust cc validation in here
      if (a.search(/^\**[0-9]+/) == -1) {
         alert("Invalid credit card number.");
         return false;
      }
   }
	// CSC entered?
	if (document.Donations.country.value != "US" && isEmpty(document.Donations.csc.value)) {
		// No, display error message
		alert("Please enter your Card Security Code.");
		document.Donations.csc.focus();
		return false;
	} else if (!isEmpty(document.Donations.csc.value) && document.Donations.csc.value.search(/^[0-9]{3,5}$/) == -1) {
		alert("Please enter a valid Card Security Code.");
		document.Donations.csc.focus();
		return false;
	}
	// CC type selected?
	if (document.Donations.cctype[0].type == "radio") {
		// we've got radio buttons
		var gottype=false;
		for (var i = 0; i < document.Donations.cctype.length; i++) {
			if (document.Donations.cctype[i].checked) {
				gottype=true;
			}
		}
		if (!gottype) {
			alert("Please select a credit card type.");
			return false;
		}
	} else if (isEmpty(document.Donations.cctype.value)) {
		// No, display error message
		alert("Please select a credit card type.");
		return false;
	} else if (document.Donations.country.value != "US" && document.Donations.cctype.value != "VS" && document.Donations.cctype.value != "MC") {
		alert("Please accept our apologies.\nWe only accept Visa and Mastercard for non-US donations.");
		return false;
	}
   // Expiration date?
	if (document.Donations.ccexp_mon) {
		// we've got the broken-out form
		if (isEmpty(document.Donations.ccexp_mon.value)) { 
			alert("Please enter your credit card expiration month.");
			document.Donations.ccexp_mon.focus();
			return false;
		} else if (isEmpty(document.Donations.ccexp_yr.value)) {
			alert("Please enter your credit card expiration year.");
			document.Donations.ccexp_yr.focus();
			return false;
		} else {
			var tmpdate=document.Donations.ccexp_mon + "/" + document.Donations.ccexp_yr;
			var ccmon=document.Donations.ccexp_mon.value;
			var ccyr=document.Donations.ccexp_yr.value;
			if (ccmon.search(/^(0[1-9]|1[0-2])/) == -1 || ccyr.search(/[0-9]{2}/) == -1) {
         	alert("Invalid Credit Card Expiration Date!");
         	document.Donations.ccexp_mon.focus();
         	return false;
      	} else {
         	checkDate = new Date();
         	tryYr = (Number(ccyr) > 69 ? "19"+ccyr : "20"+ccyr);
         	tryDate = new Date(tryYr,ccmon,01);
         	if (tryDate <= checkDate) {
            	alert("Invalid Credit Card Expiration Date!");
            	document.Donations.ccexp_mon.focus();
            	return false;
         	}
      	}
		}
	} else if (document.Donations.ccexpdate) {
   	var expdate = document.Donations.ccexpdate.value;
   	if (isEmpty(document.Donations.ccexpdate.value)) {
      	// No, display error message
      	alert("No Credit Card Expiration Date entered!");
      	document.Donations.ccexpdate.focus();
      	return false;
   	} else if (expdate.search(/^(0[1-9]|1[0-2])[\/\-][0-9]{2}/) == -1) {
      	alert("Invalid Credit Card Expiration Date!");
      	document.Donations.ccexpdate.focus();
      	return false;
   	} else {
      	checkDate = new Date();
      	tryYr = (Number(expdate.substring(3)) > 69 ? "19"+expdate.substring(3) : "20"+expdate.substring(3));
      	tryDate = new Date(tryYr,expdate.substring(0,2),01);
      	if (tryDate <= checkDate) {
         	alert("Invalid Credit Card Expiration Date!");
         	document.Donations.ccexpdate.focus();
         	return false;
      	}
		}
   }
   document.Donations.submitbtn.disabled = true;
   document.Donations.submit();
}

function openReceipt(siteid) {
	var popwin=window.open('index.cfm?siteid=' + siteid + '&displayMode=receipt', 'popupwin', 'location=no,directories=no,status=yes,menubar=no,toolbar=no,scrollbars=yes,width=550');
   popwin.document.close();
   popwin.focus();
}

function showPopup(url) {
	var popwin=window.open(url, 'popupwin', 'location=no,directories=no,status=yes,menubar=no,toolbar=no,resizable=yes,width=510,height=280');
   popwin.document.close();
   popwin.focus();
   return false;
}

