//#######################################################################
// Orginal code Created by: www.jdstiles.com 
// (The JavaScript Source!! - http://javascript.internet.com )
//  Code updated to work with the masonic dues and donation payment form.
//
//   Form Total Functions - updates the total field of this form.
// 
//#######################################################################
function init(){
	 var initTotal = 0;
	 document.getElementById("total").value = initTotal.toFixed(2);
}

function startCalc(){
  interval = setInterval("calc()",1);
}

function calc(){
  var one = document.getElementById("DuesAmount").value;
  var two = document.getElementById("OhioSRF").value;
  var three = document.getElementById("OhioMH").value;
  var total =  (one * 1) + (two * 1) + (three * 1);
  document.getElementById("total").value = total.toFixed(2);
  //document.duesPay.AMOUNT.value = document.getElementById("total").value;
}

function stopCalc(){
  clearInterval(interval);
}

//#######################################################################
//  		Form Processing Functions
//
//  		submitForm():  			runs checks on data, if data passes calls payFlowSubmit()
//
//  		valiData(): 		  			validates that member number is numeric and nine digits, 
//                         	      			checks to make sure dues (non-zero also) and donation 
//                         	 	  			fields are numeric
//
//  		updateAMT(): 			updates the hidden AMOUNT field before sending data
//						  	  			on to Verisign
//
//  		payFlowSubmit(): 		POSTS data to PayFlow
//
//#######################################################################

function submitForm() {
	var isValid =  valiData(); 
	if (isValid == true) {
		updateAMT();
		setMemNum();
		setUDF();
		payFlowSubmit();
	} 
}

function valiData() {
	var boolMN;
	var re9digit=/^\d{9}$/ ;
	// use the regex to make sure that the member number value is numeric and of the correct length.
	if (document.getElementById("MemberNumber").value.search(re9digit) != 0){
		alert("Your member number must be nine digits.");
		boolMN = false;
	} else if (IsNumeric(document.getElementById("DuesAmount").value) != true || IsNumeric(document.getElementById("OhioSRF").value) != true || IsNumeric(document.getElementById("OhioMH").value) != true){
	     alert("You must enter a dues amount and you must use a numeric value for any dontation field.");
		boolMN = false;
	} else {
		boolMN = true;
	}
  return boolMN;
}

function updateAMT(){
	document.getElementById("AMT").value = document.getElementById("total").value;
}

function setUDF(){
	document.getElementById("USER2").value = document.getElementById("DuesAmount").value;
	document.getElementById("USER3").value = document.getElementById("OhioSRF").value;
	document.getElementById("USER4").value = document.getElementById("OhioMH").value;	
}

function setMemNum(){
    document.getElementById("USER1").value = document.getElementById("MemberNumber").value;
}

function payFlowSubmit(){
	document.method="POST";
	document.duesPay.action = "https://payflowlink.paypal.com";
}

function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;
   if (strString.length == 0) return false;
   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }

//################################################
//  NOTES:
//  Abstract the above code so that it can be used elsewhere.  
//  Right now everything ties into a form ID
//  these really should go to variables instead.  
//  The ID's can be passed into the function when 
//  the function is called from the HTML page.
//################################################