//// HIM Forms
// Reset Selected Value
function clearValue(field) {
	if (field.defaultValue == field.value)
	field.value = "";
} 

// Donate Functions
function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=287,height=192');");
}

function popUp2(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=293,height=93');");
}

function checkOther() {
	var refer_val = document.getElementById("dnt_refer").value;
	if(refer_val == "other"){
		document.getElementById("other_text").style.display="block" ;
		} else {
		document.getElementById("other_text").style.display="none" ;
		}
}

function checkPartner() {
	var dnt_partner = get_dnt_partner_value(document.donate_form);
	if(dnt_partner == "yes"){
		document.getElementById("dnt_onetime_total").value = '';
		document.getElementById("dnt_onetime_total").disabled = true;
		document.getElementById("dnt_monthly_total").disabled = false;
		} else {
		document.getElementById("dnt_monthly_total").value = '';
		document.getElementById("dnt_monthly_total").disabled = true;
		document.getElementById("dnt_onetime_total").disabled = false;
		}
}

function checkType(){
	var type_val = document.getElementById("dnt_payment_type").value;
	if(type_val == "CC"){
		document.getElementById("cc").style.display="block";
		document.getElementById("echeck").style.display="none";
		} else if(type_val == "ECHECK"){
		document.getElementById("cc").style.display="none";
		document.getElementById("echeck").style.display="block";
		} else {
		document.getElementById("cc").style.display="none";
		document.getElementById("echeck").style.display="none";
		}
}

function get_dnt_partner_value(the_form){
//for (var i=0; i < the_form.dnt_partner.length; i++){
//   if (the_form.dnt_partner[i].checked)
//      {
//      var rad_val = the_form.dnt_partner[i].value;
//	  return rad_val;
//      }
//   }
	var ret =  document.getElementById('dnt_partner');
	return ret.value;
}

function validForm(){
	var the_form = document.donate_form;
	// Start Basic Error Checking
	var dnt_partner = get_dnt_partner_value(the_form);
	 if(the_form.dnt_refer.value == "") {
		alert('Please Select How you heard about us');
		the_form.dnt_refer.focus();
		return false;
		} else {
			if(the_form.dnt_refer.value == "other"){
				if(the_form.dnt_other.value == "") {
				alert('Please specify how you heard about us');
				the_form.dnt_other.focus();
				return false;
				}
			}
		}
	// Start Advanced Error Checking
	if(the_form.dnt_fname.value == "") {
		alert('Please Enter a First Name');
		the_form.dnt_fname.focus();
		return false;
		} else if (the_form.dnt_lname.value == "") {
		alert('Please Enter a Last Name');
		the_form.dnt_lname.focus();
		return false;
		} else if (the_form.dnt_email.value == "" || !checkMail(the_form.dnt_email.value)) {
		alert('Please Enter a Valid Email');
		the_form.dnt_email.focus();
		return false;
		} else if (the_form.dnt_add_1.value == "") {
		alert('Please Enter an Address');
		the_form.dnt_add_1.focus();
		return false;
		} else if (the_form.dnt_city.value == "") {
		alert('Please Enter a City');
		the_form.dnt_city.focus();
		return false;
		} else if (the_form.dnt_state.value == "") {
		alert('Please Enter a State');
		the_form.dnt_state.focus();
		return false;
		} else if (the_form.dnt_postal.value == "") {
		alert('Please Enter a Postal Code');
		the_form.dnt_postal.focus();
		return false;
		} else if (the_form.dnt_country.value == "") {
		alert('Please Enter a County');
		the_form.dnt_country.focus();
		return false;
		} else if(dnt_partner == "yes"){
			if(the_form.dnt_monthly_total.value == "") {
			alert('Please Select a Montly Amount');
			the_form.dnt_monthly_total.focus();
			return false;
			}
		} else if(dnt_partner == "no"){
			if(the_form.dnt_onetime_total.value == "") {
			alert('Please Select a One Time Amount');
			the_form.dnt_onetime_total.focus();
			return false;
			}	
		}		
		if(the_form.dnt_payment_type.value == "") {
		alert('Please Select a Payment Type');
		the_form.dnt_payment_type.focus();
		return false;
		} else {
			// Check what type of payment type
			var payment_type = the_form.dnt_payment_type.value;
			if(payment_type == "CC") {
			//////////////////
			// CC CHECK
			validCC(the_form, dnt_partner);
			//////////////////
			} else if(payment_type == "ECHECK") {
			//////////////////
			// ECHECK CHECK
			validECHECK(the_form, dnt_partner);
			//////////////////	
			}
		}
	}
	
function confirmDonation(the_form, partner){
	var dnt_partner = (partner == "yes") ? "Monthly" : "One Time";
	var dnt_total = (partner == "yes") ? the_form.dnt_monthly_total.value : the_form.dnt_onetime_total.value;
	var donate = confirm("Are you sure you want to make a\n"+dnt_partner+" Donation of $"+dnt_total+" (USD)\nto Harvest International Ministry?\n\nOnce submitted, do not push back button.\nDoing so may cause a duplicate transaction.");
	if(donate){
		// Submit Form
		the_form.submit();
		return true;
		} else {
		// Cancel
		return false;
		}
}
	
function validCC(the_form, partner){
	if(the_form.dnt_CC_cname.value == ""){
		alert('Please Enter the Cardholders Name');
		the_form.dnt_CC_cname.focus();
		return false;
		} else if(the_form.dnt_CC_card_type.value == ""){
		alert('Please Enter a Card Type');
		the_form.dnt_CC_card_type.focus();
		return false;
		} else if(the_form.dnt_CC_card_num.value == ""){
		alert('Please Enter a Card Number');
		the_form.dnt_CC_card_num.focus();
		return false;
		} else {
		type = the_form.dnt_CC_card_type.value;
		ccnum = the_form.dnt_CC_card_num.value
		if(!isValidCreditCard(type, ccnum)) {
			alert('Please Enter a valid '+getCardLabel(type)+' Card Number');
			the_form.dnt_CC_card_num.focus();
			return false;
			} else if(the_form.dnt_CC_card_exp_month.value == ""){
			alert('Please Enter a Expiration Month');
			the_form.dnt_CC_card_exp_month.focus();
			return false;
			} else if(the_form.dnt_CC_card_exp_year.value == ""){
			alert('Please Enter a Expiration Year');
			the_form.dnt_CC_card_exp_year.focus();
			return false;
			} else if(the_form.dnt_CC_card_code.value == ""){
			alert('Please Enter a Card Code');
			the_form.dnt_CC_card_code.focus();
			return false;
			} else {
			// Confirm Donation
			confirmDonation(the_form, partner);
			} 
		}
	}

function validECHECK(the_form, partner){
	if(the_form.dnt_ECHECK_account_type.value == ""){
		alert('Please Enter an Account Type');
		the_form.dnt_ECHECK_account_type.focus();
		return false;
		} else if(!checkABA(the_form.dnt_ECHECK_routing_num.value)){
		alert('Please Enter a valid ABA Routing Number');
		the_form.dnt_ECHECK_routing_num.focus();
		return false;
		} else if(the_form.dnt_ECHECK_account_num.value == ""){
		alert('Please Enter an Account Number');
		the_form.dnt_ECHECK_account_num.focus();
		return false;
		} else if(the_form.dnt_ECHECK_bank_name.value == ""){
		alert('Please Enter a Bank Name');
		the_form.dnt_ECHECK_bank_name.focus();
		return false;
		} else {
		// Confirm Donation
		confirmDonation(the_form, partner);
		}
	}

function checkMail(email)
{
	var x = email;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) {
		return true;
		} else {
		return false;
		}
}

function isValidCreditCard(type, ccnum) {
   if (type == "VI") {
      // Visa: length 16, prefix 4, dashes optional.
      var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "MC") {
      // Mastercard: length 16, prefix 51-55, dashes optional.
      var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "DC") {
      // Discover: length 16, prefix 6011, dashes optional.
      var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "AM") {
      // American Express: length 15, prefix 34 or 37.
      var re = /^3[4,7]\d{13}$/;
   } else if (type == "DN") {
      // Diners: length 14, prefix 30, 36, or 38.
      var re = /^3[0,6,8]\d{12}$/;
   }
   if (!re.test(ccnum)) return false;
   // Remove all dashes for the checksum checks to eliminate negative numbers
   ccnum = ccnum.split("-").join("");
   // Checksum ("Mod 10")
   // Add even digits in even length strings or odd digits in odd length strings.
   var checksum = 0;
   for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) {
      checksum += parseInt(ccnum.charAt(i-1));
   }
   // Analyze odd digits in even length strings or even digits in odd length strings.
   for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+=2) {
      var digit = parseInt(ccnum.charAt(i-1)) * 2;
      if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }
   }
   if ((checksum % 10) == 0) return true; else return false;
}

function checkABA(s) {
 if(s == ""){
 	return false;
 }
  var i, n, t;

  // First, remove any non-numeric characters.

  t = "";
  for (i = 0; i < s.length; i++) {
    c = parseInt(s.charAt(i), 10);
    if (c >= 0 && c <= 9)
      t = t + c;
  }

  // Check the length, it should be nine digits.

  if (t.length != 9)
    return false;

  // Now run through each digit and calculate the total.

  n = 0;
  for (i = 0; i < t.length; i += 3) {
    n += parseInt(t.charAt(i),     10) * 3
      +  parseInt(t.charAt(i + 1), 10) * 7
      +  parseInt(t.charAt(i + 2), 10);
  }

  // If the resulting sum is an even multiple of ten (but not zero),
  // the aba routing number is good.

  if (n != 0 && n % 10 == 0)
    return true;
  else
    return false;
}

function getCardLabel(type) {
	if (type == "VI") {
		return "Visa";
		} else if (type == "MC") {
		return "MasterCard";
		}  else if (type == "AM") {
		return "American Express";
		}
	}
function noenter() {
  return !(window.event && window.event.keyCode == 13); 
  }
