//place the following line in the header of the page that you want to include this file in
//<script src="/jscript/validation.js" type="text/javascript"></script>

function checkIntField(theForm,fieldName,fieldDescription,isRequired) {
  //used to check that a field is numeric (will rip out $ and ,)
  //Input :
  //  frm (object) (the form object)
  //  fieldName (string) (name of the input field)
  //  fieldDescription (string) (description of field to be used in alert
  //  isRequired (bool) (if false then i will return true if the field is empty)
  //Output :
  //  returns true if value is numeric (or if field is blank and isRequired is false)
  //  returns false if value is not numeric (or if field is blank and isRequired is true)
    
  fieldValue = eval("theForm." + fieldName + ".value")
  rExp = '\$'
  fieldValue = fieldValue.replace(rExp,'')
  rExp = /,/gi
  fieldValue = fieldValue.replace(rExp,'')
  eval("theForm." + fieldName + ".value = fieldValue")
  if (fieldValue == ''  && isRequired) {
    alert('Please fill out the ' + fieldDescription + ' field.')
    eval("theForm." + fieldName + ".focus()")
    return false
  } else if (fieldValue == '') {
    return true;
  }
  if (isNaN(fieldValue)) {
    alert('The ' + fieldDescription + ' must be a numeric value.')
    eval("theForm." + fieldName + ".focus()")
    return false;
  }
  return true;
}

function checkRadio(theForm,fieldName,numberOfRadioButtons,errorMessage) {
  //used to validate a set of radio buttons (make sure one is selected
  //Input:
  //  frm (object) (the form object)
  //  fieldName (string) (name of the radio buttons)
  //  numberOfRadioButtons (int) (the number of possible radio buttons: usually 2)
  //  errorMessage (string) (message to be displayed if a radio is not selected
  //Output:
  //  Returns true if a radio is selected
  //  Returns false if not and alerts user and places focus on the field in question
  optionChecked = false
  for (x=0;x<numberOfRadioButtons;x++) {
    if (eval('theForm.' + fieldName + '[' + x + '].checked')) optionChecked = true
  }
  if (!optionChecked) {
    alert(errorMessage)
    eval('theForm.' + fieldName + '[0].focus()')
    return false;
  }
  return true;
}

function checkPhoneNumber(frm,areaCodeFieldName,prefixFieldName,suffixFieldName,extFieldName,optionalField,fieldDescription) {
  //used to validate a phone number 
  //Input:
  //  frm (object) (the form object)
  //  areaCodeFieldName (string) (name of the area code field)
  //  prefixFieldName (string) (name of the prefix field)
  //  suffixFieldName (string) (name of the suffix field)
  //Output:
  //  Returns true if the phone number is valid
  //  Returns false if not and places focus on the field in question
  rExp = / /gi
  //if the field is optional, then return true if all the field are empty
  fieldValue = eval('frm.' + areaCodeFieldName + '.value + frm.' + prefixFieldName + '.value + frm.' + suffixFieldName + '.value + frm.' + extFieldName + '.value');
  fieldValue = fieldValue.replace(rExp,'');
  if (optionalField == true && fieldValue == '') return true;
  //check area code
  areaCodeValue = eval("frm." + areaCodeFieldName + ".value")
  areaCodeValue = areaCodeValue.replace(rExp,'')
  eval("frm." + areaCodeFieldName + ".value = areaCodeValue")
  if (fieldDescription == null) fieldDescription = ''
  if (fieldDescription != '') fieldDescription += ' '
  if (isNaN(areaCodeValue) || areaCodeValue.length != 3){
    alert('Your ' + fieldDescription + 'phone area code does not appear to valid.')
    eval("frm." + areaCodeFieldName + ".focus()")
    return false;
  }
  //check prefix
  prefixValue = eval("frm." + prefixFieldName + ".value")
  prefixValue = prefixValue.replace(rExp,'')
  eval("frm." + prefixFieldName + ".value = prefixValue")
  if (isNaN(prefixValue) || prefixValue.length != 3){
    alert('Your ' + fieldDescription + 'phone prefix does not appear to valid.')
    eval("frm." + prefixFieldName + ".focus()")
    return false;
  }
  //check suffix
  suffixValue = eval("frm." + suffixFieldName + ".value")
  suffixValue = suffixValue.replace(rExp,'')
  eval("frm." + suffixFieldName + ".value = suffixValue")
  if (isNaN(suffixValue) || suffixValue.length != 4){
    alert('Your ' + fieldDescription + 'phone suffix does not appear to valid.')
    eval("frm." + suffixFieldName + ".focus()")
    return false;
  }
  //check extnsension
  extValue = eval("frm." + extFieldName + ".value")
  extValue = extValue.replace(rExp,'')
  eval("frm." + extFieldName + ".value = extValue")
  if (isNaN(extValue)){
    alert('Your ' + fieldDescription + 'phone extension does not appear to valid.')
    eval("frm." + extFieldName + ".focus()")
    return false;
  }
  return true;
}

function checkZipCode(frm,fieldName) {
  //used to validate a zip code
  //Input:
  //  frm (object) (the form object)
  //  fieldName (string) (name of the zip code field)
  //Output:
  //  Returns true if the zip code is valid
  //  Returns false if not and places focus on the field in question
  eval("zipCode = frm." + fieldName + ".value")
  rExp = / /gi
  zipCode = zipCode.replace(rExp,'')
  eval("frm." + fieldName + ".value = zipCode")
  if (isNaN(zipCode) || zipCode.length != 5) {
    alert('You must enter a 5 digit numeric zip code')
    eval("frm." + fieldName + ".focus()")
    return false;
  }
  return true;
}

function g_fnCheckEmail(poForm, psFieldName){
	
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// New Email validation
	// Returns true if the email address is valid else Returns false
    // and places focus on the field
    // Email is not allow any invalid Email characters
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	var lsEmail = eval(poForm + '.' + psFieldName + '.value')
	var rExp	= /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z]*[-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/gi;
	
	if (lsEmail == '') {	// not empty
		eval(poForm + '.' + psFieldName + '.focus()')		//focus on the field 
		return false;
	 }
 
	if (!rExp.test(lsEmail)){		//not allow any invalid Email characters	
		eval(poForm + '.' + psFieldName + '.focus()')			//focus on the field 
		return false;
	 }

return true;

}



function checkEmail(frm,fieldName) {
  //used to validate an email address.
  //Input:
  //  frm (object) (the form object)
  //  fieldName (string) (name of the email field)
  //Output:
  //  Returns true if the email address is valid
  //  Returns false if not and places focus on the field in question
  eval('email=frm.' + fieldName + '.value')
  rExp = / /gi
  email = email.replace(rExp,'')
  rExp = /\.\./gi
  email = email.replace(rExp,'.')
  rExp = /,/gi
  email = email.replace(rExp,'.')
  eval('frm.' + fieldName + '.value = email')
  if (email == '') {
    alert('You must enter an e-mail address.')
    eval('frm.' + fieldName + '.focus()')
    return false;
  }
  //alert(email)
  atLoc = email.indexOf('@')
  dotLoc = email.indexOf('.')
  forwardSlashLoc = email.indexOf('/')
  backSlashLoc = email.indexOf('\\')
  secondAtLoc = email.indexOf('@',atLoc + 1)
  wwwLoc = email.indexOf('www')
  semiColonLoc = email.indexOf(';')
  quoteLoc = email.indexOf("'")
  doubleQuoteLoc = email.indexOf('"')
  emailLen = email.length - 1
  //alert('atLoc:' + atLoc + ' dotLoc:' + dotLoc + ' emailLen:' + emailLen + ' email.charAt(emailLen):' + email.charAt(emailLen))
  if ( atLoc < 1 || dotLoc < 0 ||  emailLen < 5 || email.charAt(emailLen) == '.' || (atLoc + 1) == dotLoc ){
    alert('It appears that you did not enter a valid email address.')
    eval('frm.' + fieldName + '.focus()')
    return false;
  }
  if (secondAtLoc != -1) {
    alert('This is not a valid email address. There are two @ symbols in your entry. Please enter only one email address.')
    eval('frm.' + fieldName + '.focus()')
    return false;
  }
  if (forwardSlashLoc != -1 || backSlashLoc != -1) {
    alert('This is not a valid email address. Please do not use any slashes (eg: /) in your email address.')
    eval('frm.' + fieldName + '.focus()')
    return false;
  }
  if (wwwLoc != -1 && confirm('Most email addresses do not have a www in them. Would you like to change your entry?') ) {
    eval('frm.' + fieldName + '.focus()')
    return false;
  }
  if (semiColonLoc != -1) {
    alert('This is not a valid email address. Please do not use any semi-colons in your email address.')
    eval('frm.' + fieldName + '.focus()')
    return false;
  }
  if (quoteLoc != -1 || doubleQuoteLoc != -1) {
    alert('This is not a valid email address. Please do not use any quotes or double quotes in your email address.')
    eval('frm.' + fieldName + '.focus()')
    return false;
  }
  return true;
}

function checkCCNumber(frm,fieldName) {
  //used to validate a credit card number
  //Input:
  //  frm (object) (the form object)
  //  fieldName (string) (name of the credit card number field)
  //Output:
  //  Returns true if the cc number is valid
  //  Returns false if not and places focus on the field in question
  ccNumber = eval("frm." + fieldName + ".value")
  rExp = / /gi
  ccNumber = ccNumber.replace(rExp,'')
  rExp = /-/gi
  ccNumber = ccNumber.replace(rExp,'')
  if (isNaN(ccNumber) || ccNumber.length > 16 || ccNumber.length < 13) {
    alert('Your credit card number does not appear to be valid.')
    eval("frm." + fieldName + ".focus()");
    return false;
  }
  if (ccNumber.indexOf(".") >= 0) {
    alert('Your credit card number contains invalid characters. Please do not use punctuation like periods and dashes.')
    eval("frm." + fieldName + ".focus()");
    return false;
  }
  return true;
}

function checkABARoutingNumber(frm,fieldName) {
	//used to validate Routing Number for ACH Transaction
	//Input:
	//  frm (object) (the form object)
	//  fieldName (string) (name of the Routing number field)
	//Output:
	//  Returns true if the Routing Number is valid
	//  Returns false if not and places focus on the field in question
	
	strFieldValue = eval("frm." + fieldName + ".value")
	
	// First, remove any non-numeric characters.
	strABARoutingNumber = "";
	for (i = 0; i < strFieldValue.length; i++) {
		c = parseInt(strFieldValue.charAt(i), 10);
		if (c >= 0 && c <= 9)
			strABARoutingNumber = strABARoutingNumber + c;
	}
	
	// Check the length, it should be nine digits.
	if (strABARoutingNumber.length != 9)
	{
		alert('Your Routing number does not appear to be valid.')
		eval("frm." + fieldName + ".focus()");
		return false;
	}

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

	n = 0;
	for (i = 0; i < strABARoutingNumber.length; i += 3) {
   n += parseInt(strABARoutingNumber.charAt(i),     10) * 3
     +  parseInt(strABARoutingNumber.charAt(i + 1), 10) * 7
     +  parseInt(strABARoutingNumber.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
	{
	 alert('Your Routing number does not appear to be valid.')
    eval("frm." + fieldName + ".focus()");
    return false;
    }
	

}

function standardValidation(frm,fieldNames,fieldDesc) {
  //used to validate that fields are not blank
  //Input:
  //  frm (the form object)
  //  fieldNames (a csv string of the html names of the fields that are too be tested)
  //  fieldDesc (a description of the above fieldNames, MUST contain a description for each field passed)
  //Output:
  //  Returns true if all fields are valid
  //  Returns false if not and places focus on the field in question
  
  fields = fieldNames.split(",")
  desc = fieldDesc.split(",")
  for (x=0;x<fields.length;x++) {
    eval('fieldValue = frm.' + fields[x] + '.value')
//    alert(fields[x] + ':' + fieldValue)
    rExp = / /gi
    fieldValue = fieldValue.replace(rExp,'')
    if (fieldValue.length == 0) {
      alert('The ' + desc[x] + ' field is required.');
      eval('frm.' + fields[x] + '.focus();')
      return false;
    }
  }
  return true
}


// currently used by BHD to validate password.
function checkPassword(frm, fieldName) {
  //used to validate a password
  //Input:
  //  frm (object) (the form object)
  //  fieldName (string) (name of the credit card number field)
  //Output:
  //  Return true if password contains letters, numbers and underscore and
  //  the length is between 4 and 10.
  //  Returns false otherwise

  eval('pswd = frm.' + fieldName + '.value')

  if (pswd.length < 4 || pswd.length > 10) {
    alert('Your password should be between 4 and 10 characters long.')
    eval("frm." + fieldName + ".focus()");
    return false;
  }

  tomatch = /^[a-zA-Z0-9_]+$/;  //regular expression shortcut would be /^\w+$/
  if (!tomatch.test(pswd)) {		
    alert("Password can only contain letters, numbers, and underscores");
    eval("frm." + fieldName + ".focus()");
    return false;
  } 

  return true;
}
