function validZip(zip)
{
   if (zip.match(/^[0-9]{5}$/)) {
     return true;
   }

   zip=zip.toUpperCase();
   if (zip.match(/^[A-Z][0-9][A-Z][0-9][A-Z][0-9]$/)) {
     return true;
   }

   if (zip.match(/^[A-Z][0-9][A-Z].[0-9][A-Z][0-9]$/)) {
     return true;
   }

   return false;
}

function ValidatePostalCode(){
	var zip=document.frmApplication.bankzip
	
	if ((zip.value==null)||(zip.value=="")){
		zip.focus()
		return false
	}
	if (validZip(zip.value)==false){
		zip.focus()
		return false
	}
	return true
 }

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}

function ValidateEmailAddress(){
	var emailID=document.frmApplication.email
	
	if ((emailID.value==null)||(emailID.value=="")){
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.focus()
		return false
	}
	return true
 }



// Declaring valid date character, minimum year and maximum year
var dtCh= "-";
var minYear=1900;
var maxYear=2100;

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
        // YYYY-MM-DD
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strYear=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strDay=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false
	}
return true
}

function ValidateDate(){
	var dt=document.frmApplication.bday
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
    return true
 }



// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}


function checkInternationalPhone(strPhone){
   s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function ValidateTelephone(){
	var Phone=document.frmApplication.teleno
	
	if ((Phone.value==null)||(Phone.value=="")){
		Phone.focus()
		return false
	}
	if (checkInternationalPhone(Phone.value)==false){
		Phone.focus()
		return false
	}
	return true
 }

function its_empty(string_value) {

  // Check for the empty string and null
  if (string_value == "" || string_value == null) {
      return true
  }
  return false
}

function validate(current_form) {
  
  var missing_fields = new Array()
  var total_missing = 0
  

  // Loop through all the form elements
  for (counter = 0; counter < current_form.length; counter++) {
  
    // Is this a visible text field that's mandatory?
    if ((current_form[counter].type == "text" ||
      current_form[counter].type == "textarea" ||
      current_form[counter].type == "password") &&
      current_form[counter].mandatory) {

      // Is it empty?
      if (its_empty(current_form[counter].value)) {

       
        // If so, add the field to the array of missing fields
        missing_fields[total_missing] = current_form[counter]
        total_missing++
      }
    }
  }

  // Were there any fields missing?
  if (total_missing > 0) {
  
    // Start the message
    var missing_message = "Sorry, you must fill in the following " +
               (total_missing == 1 ? " field:" : " fields:") +
               "\n______________________________\n\n"
    
    // Loop through the missing fields
    for (counter = 0; counter < missing_fields.length; counter++) {
      missing_message += missing_fields[counter].name + "\n"
    }
  
    // Finish up and display the message
    missing_message += "\n______________________________\n\n" +
            "Please fill in these fields and then resubmit the form."
    alert(missing_message)
    
    // For emphasis, put the focus on the first missing field
    missing_fields[0].focus()
    
    return false

  }
  else {
  
    // Verify Certain Data elements are valid

    var total_invalid = 0

    var invaliddata_message = "Sorry, the following fields have invalid values " +
               "\n______________________________\n\n"
    if ( !ValidateEmailAddress() ) {
       invaliddata_message += "Your Email Address has an invalid format\n"
       total_invalid++
    }
    if ( !ValidatePostalCode() ) {
       invaliddata_message += "Your Zip/Postal code in not in correct format (US NNNNN / Canada ANA NAN)\n"
       total_invalid++
    }

    var obox=document.frmApplication.bankstate
    if ((obox.value==null)||(obox.value=="")){
       obox.focus()
       invaliddata_message += "Need to specify your State or Provence\n"
       total_invalid++
    }

    var obox=document.frmApplication.country
    if ((obox.value==null)||(obox.value=="")){
       obox.focus()
       invaliddata_message += "Need to specify your Country of Residence\n"
       total_invalid++
    }

    var cbox=document.frmApplication.acceptterms
    if (!cbox.checked){
       cbox.focus()
       invaliddata_message += "We are unable to process this authorization, unless you have read and accept the terms and conditions\n"
       total_invalid++
    }

    var c1=document.frmApplication.ondemand

    if ( !c1.checked ){
       c1.focus()
       invaliddata_message += "We are unable to process this authorization, unless you authorize On Demand PAPs\n"
       total_invalid++
    }

    c1=document.frmApplication.selectweekly
    var v1=document.frmApplication.wmaxamount
    if ( (c1.checked) && its_empty(v1.value) ) {
       v1.focus()
       invaliddata_message += "Weekly Reoccuring requires a maximum amount to be set.\n"
       total_invalid++
    }

    var c2=document.frmApplication.selectreoccuring
    if ((c1.checked) && (c2.checked)){
       c2.focus()
       invaliddata_message += "You cannot select both weekly and monthly reoccurring\n"
       total_invalid++
    }
   
    invaliddata_message += "\n______________________________\n\n" +
            "Please correct these fields and then resubmit the form."

    if ( total_invalid > 0 ) {
       alert(invaliddata_message)
       return false
    } 
    return false
   
  }
}



