//-------------------------------------------------------------------------------------------------------------------------------------------------------------
//Validation.js
//written for Kenexa 
// September 2001
// Howard Bregman
//this script will check all input fields for entries that require them and also check each keystroke to make sure users cannot enter a html tags.
// also this script validates an email address, making sure it is formatted properly, ie. name@domain.com(also will allow if user is from 
// foreign country code as well after the .com ie. name@domain.com.fr
// Date fields and Zip code field, Phone number fields are valiated for format and error message is also help box.
//--------------------------------------------------------------------------------------------------------------------------------------------------------------

//----below is for form validation, making sure required fields are filled in------------------------------
<!-- Hide Script

function isBlank(s)
{
	for(var i = 0; i < s.length; i++){
       var c = s.charAt(i);
       if ((c != '') && (c !='\n') && (c != '\t')) return false;
     }
     return true;
}

//perform form validation for required fields
function Verify(f)
{
   
   var msg;
   var empty_fields = "";
   var errors = "";
  
    for(var i = 0; i < f.length; i++){   
       var e = f.elements[i];
	   if (e.name.substring(0,3)=="Req" && e.id.substring(0,3) != "opt"){ 
	      if(((e.type == "text") || (e.type == "textarea"))  || (e.selectedIndex == 0))
            if((e.value == null) || (e.value == "") || isBlank(e.value)){
			     empty_fields +="\n         " + e.name.substring(3,30);
	     		 continue;
              
         }
        
       }
    }
  
//display any messages to user for correction
if(!empty_fields && !errors ) return true;
msg = "________________________________________________\n\n"
msg +="The Form was not submitted because of the following error(s).\n";
msg +="Please correct these error(s) and re-submit.\n";
msg +="________________________________________________\n\n"

if(empty_fields){
   msg += "-The following required field(s) are empty:"
           +empty_fields + "\n\n";
   if(errors) msg += "\n\n";
    
   }
msg += errors;

alert(msg);
return false;
}

//--below is for the Valid Characters validation-------------------------------------------------------
//4/10/07 JAG:
//discovered that this was allowing plus signs and question marks,
//so have altered it slightly, and now it seems to work correctly.
var re = /^[\"\.]?(\w\s*[\s\.\-\'\"\s])*[\w\s\"]*$/
//var re = /^[\"\.]?(\w+\s*[\s\.\-?\'\"\s])*[\w+\s\"?]*$/
var re1 = /^\w+([\.\-]*\w+)*@\w+([\.-]?\w+)*(\.\w{2,4}\s?)+$/
var re2 = /^(\d+[\/\\]?)*$/
var re3 = /^(\d+[\-]?\d+[\-]?\d+[\-]?\d{4})*$/
var re4 = /^\w{5,6}([-]?\w{4})*$/
var re5 = /^[a-zA-Z]{2,3}$/
var re6 =  /^[\w+\s;\.\- \' \";:,\?]*$/
var re7 = /^[\w+\s;\.\-@]*$/
var re8 = /^(\d+)$/
var re9 =  /^[\w+\s;\.\- \' \";:,\?#]*$/

function CheckChars(input)
{
  
	if(re.test(input.value))
	{
	  return true;
	}
	  alert("\n\n-This field cannot contain any of the following characters:\n\n\n~ ` ! @ # $ % ^ & * ( ) = + \\ | / ] [{ }; :? > < ,\n\n");
	  input.value = "";
	  return false;
}	    


function ValidEmail(email)
{

   if(re1.test(email.value))
   {
      return true;
    }
	  alert("-The email address you entered is invalid.\n Please re-enter your email address.\n\nIf you need email address advice, please consult with your test administrator.\n\nSign up for a free email address at either hotmail.com or yahoo.com.");
	  email.value = "";
	 // email.focus()
	  return false;
	
}


function ValidDate(dateInput)
{
    
   if(re2.test(dateInput.value))
   {
	  return true;
    }

	alert("-You entered an Invalid Character\n Please format Date fields: MM/DD/YYYY");
	dateInput.value = "";
	//dateInput.focus()
	return false;
}

function validPhone(phoneInput)
{
    if(re3.test(phoneInput.value))
   {
	  return true;
   }

function validOptPhone(phoneInput)
{
	if (phoneInput == "")
	{
		return true;
	}
	return validPhone(phoneInput);
}
	  alert("-The Phone Number you entered is invalid.\n\nIf you are inside the United States or Canada enter your area code and 7 digit phone number in the format: 123-456-7890.\n\nAll other countries please enter your phone number in the following format: Country Code - City/Area Code - Phone Number.\n\n If you do not have a phone number please enter 000-000-0000\n");
	  phoneInput.value = "";
	//  phoneInput.focus()
	  return false;
}

function validZip(zipInput)
{
   if(re4.test(zipInput.value))
   {
     return true;
   }

	  alert("-You entered an Invalid Postal Code.\nPlease format your Postal Code: xxxxx-xxxx.\nInside the United States the + 4 is optional.");
	  zipInput.value = "";
	  return false;
}

function validState(stateInput)
{
   if(re5.test(stateInput.value))
   {
      return true;
   }

	  alert("-You can only enter up to a maximum of 3 letters for your State or Province");
	  stateInput.value = "";
	  return false;
}

function validTextArea(textInput)
{
   var NewStr = "";
   var str = textInput.value;
   
   for (var i=0;i<str.length+1;i++)
   {
       //loop through string character by character and perform a test on each character
        /*if (str.substring(i,1).search(re6) != -1)
        {
		    //if character is ok, re-build our new string to place back into text area in case an illegal character is found, don't delete what was already ok.
		    NewStr = str.substring(0,i)
			continue;
		} */
		if (str.substring(i,1).search(re6) == -1)
		{
		  //found an illegal character, send back a message and place string upto illegal character back into text area
		  NewStr = str.substring(0,i-1)
		  alert("\n\n-This field cannot contain any of the following characters:\n\n\n~ ` !  @ # $ % ^ & * ( )  = + \\ | / ] [{ } > < \n\n");
		  textInput.value = NewStr;
		  break;
		}
   }			
     
}

function validTextAreaLen(textInput, len)
{
   var NewStr = "";
   var str = textInput.value;

	if (str.length > len)
	{
		NewStr = str.substring(0,len)
		alert("\n\n-This field cannot contain more than " + len + " characters.\n\n");
		textInput.value = NewStr;
		str = NewStr;
	}
   
   for (var i=0;i<str.length+1;i++)
   {
       //loop through string character by character and perform a test on each character
        /*if (str.substring(i,1).search(re6) != -1)
        {
		    //if character is ok, re-build our new string to place back into text area in case an illegal character is found, don't delete what was already ok.
		    NewStr = str.substring(0,i)
			continue;
		} */
		if (str.substring(i,1).search(re6) == -1)
		{
		  //found an illegal character, send back a message and place string upto illegal character back into text area
		  NewStr = str.substring(0,i-1)
		  alert("\n\n-This field cannot contain any of the following characters:\n\n\n~ ` !  @ # $ % ^ & * ( )  = + \\ | / ] [{ } > < \n\n");
		  textInput.value = NewStr;
		  break;
		}
   }			
     
}

function Email(email, CurrentEmail)
{
    if(re7.test(email.value)) 
	{
		 return true
     }
	    alert("\n\n-Your email address(es) cannot contain any of the following characters:\n\n\n~ ` !  \' \" # $ % ^ & * ( )  = + \\ | / ] [{ } : ? > < ,\n\n");
         email.value = CurrentEmail;
		 return false;

}
function ValidNumber(numInput)
{
   if(re8.test(numInput.value))
   {
	  return true;
    }
	  alert("\n\n-Only numbers are allowed in this field");
      numInput.value = "";
	  return false;
}
function CheckCharTestTitle(input)
{
  
	if(re9.test(input.value))
	{
	  return true;
	}
	  alert("\n\n-This field cannot contain any of the following characters:\n\n\n~ ` ! @ $ % ^ & * ( ) = + \\ | / ] [{ }; :? > < ,\n\n");
	  input.value = "";
	  return false;
}	    
function check_date(field){
var checkstr = "0123456789";
var DateField = field;
var Datevalue = "";
var DateTemp = "";
var seperator = "/";
var day;
var month;
var year;
var leap = 0;
var err = 0;
var i;
   err = 0;
   /* Delete all chars except 0..9 */
   for (i = 0; i < field.length; i++) {
	  if (checkstr.indexOf(field.substr(i,1)) >= 0) {
	     DateTemp = DateTemp + field.substr(i,1);
	  }
   }
   DateValue = DateTemp;
   /* Always change date to 8 digits - string*/
   /* if year is entered as 2-digit / always assume 20xx */
   if (DateValue.length == 6) {
      DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
   if (DateValue.length != 8) {
      err = 19;}
   /* year is wrong if year = 0000 */
   year = DateValue.substr(4,4);
   if (year == 0) {
      err = 20;
   }
   /* Validation of month*/
   month = DateValue.substr(0,2);
   if ((month < 1) || (month > 12)) {
      err = 21;
   }
   /* Validation of day*/
   day = DateValue.substr(2,2);
   if (day < 1) {
     err = 22;
   }
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
      err = 26;
   }
   /* if 00 is entered, no error, deleting the entry */
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 1; day = ""; month = ""; year = ""; seperator = "";
   }
   /* if no error, send back true */
   if (err == 0) {
      return true;
   /* Error-message if err != 0 */
   } else {
	    return false;
   }
}
function chkdate(objValue) {
//To use code below for European date just switch the 2 variables below.
var strDatestyle = "US"; //United States date style
//var strDatestyle = "EU";  //European date style
var strDate;
var strDateArray;
var strDay;
var strMonth;
var strYear;
var intday;
var intMonth;
var intYear;
var mooFound = false;
var datefield = objValue;
var strSeparatorArray = new Array("-"," ","/",".","\\", "+");
var intElementNr;
var err = 0;
var strMonthArray = new Array(12);
strMonthArray[0] = "01";
strMonthArray[1] = "02";
strMonthArray[2] = "03";
strMonthArray[3] = "04";
strMonthArray[4] = "05";
strMonthArray[5] = "06";
strMonthArray[6] = "07";
strMonthArray[7] = "08";
strMonthArray[8] = "09";
strMonthArray[9] = "10";
strMonthArray[10] = "11";
strMonthArray[11] = "12";
strDate = datefield.value;
if (strDate.length < 1) {
return true;
}
for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
   if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
      strDateArray = strDate.split(strSeparatorArray[intElementNr]);
       if (strDateArray.length != 3) {
        err = 1;
        return false;
   }
   else {
       strDay = strDateArray[0];
	   strMonth = strDateArray[1];
       strYear = strDateArray[2];
   }
   mooFound = true;
   }
}
if (mooFound == false) {
    if (strDate.length> 4) {
        strDay = strDate.substr(0, 2);
        strMonth = strDate.substr(2, 2);
        strYear = strDate.substr(4);
     }
}
if (strYear.length == 2) {
    strYear = '20' + strYear;
}
// US style
if (strDatestyle == "US") {
   strTemp = strDay;
   strDay = strMonth;
   strMonth = strTemp;
}
intday = parseInt(strDay, 10);
if (isNaN(intday)) {
  err = 2;
  return false;
}
intMonth = parseInt(strMonth, 10);
if (isNaN(intMonth)) {
  for (i = 0;i<12;i++) {
     if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
       intMonth = i+1;
       strMonth = strMonthArray[i];
       i = 12;
     }
   }
  if (isNaN(intMonth)) {
   err = 3;
   return false;
  }
}
intYear = parseInt(strYear, 10);
if (isNaN(intYear)) {
   err = 4;
   return false;
}
if (intMonth>12 || intMonth<1) {
  err = 5;
  return false;
}
if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
  err = 6;
  return false;
}
if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
  err = 7;
  return false;
}
if (intMonth == 2) {
   if (intday < 1) {
   err = 8;
   return false;
}
if (LeapYear(intYear) == true) {
   if (intday > 29) {
     err = 9;
     return false;
   }
}
else {
  if (intday > 28) {
    err = 10;
    return false;
  }
 }
}

if (strDatestyle == "US") {
	if (err == 0)
	{
		 datefield.value = strMonthArray[intMonth-1] + "/" +  intday + "/" + strYear;
		 return true;
	} else {
		 return false;
	}
}
else {
    if (err == 0)
    {
		 datefield.value = intday + "/" + strMonthArray[intMonth-1] + "/" + strYear;
		 return true;
    } else {
		 return false;
	}
}
return true;
}
function LeapYear(intYear) {
  if (intYear % 100 == 0) {
     if (intYear % 400 == 0) { return true; }
     }
   else {
      if ((intYear % 4) == 0) { return true; }
   }
   return false;
}
function JSTrim(sString) {
   //Function will trim leading and trailing white space.
	while (sString.substring(0,1) == ' ') {
	    sString = sString.substring(1, sString.length);
	 }
	while (sString.substring(sString.length-1, sString.length) == ' ') {
	   sString = sString.substring(0,sString.length-1);
	}
	return sString;
}	
//function SetFocus(e, f) {
 //  var isNN = (window.Event)? true : false;
 //  if (isNN) 
 //        var whichKey = Keyevent.which;
 //     else {
 //        var whichKey = window.event.keyCode;
 //  }
 //  var sLength = eval("document.form1." + e + ".value;")
  // if((sLength.length == 2) && (whichKey != 9)) {
//		eval("document.form1." + f + ".focus();");
//		eval("document.form1." + f + ".select();");
//	}
 // }

 function NewCheckChars (inputelementCheck, strWhitelist, strBlacklist) {
	 if (! strBlacklist) {
		/*
		4/23/10 JAG:
		updating this to include all characters rejected by our asp character validation function
		for consistency.
		*/
		 strBlacklist = ";,!$%*+`-./:<>()#&\"'=?@[]\\^_{}|~\n";
	 }
	 if (! strWhitelist) {
		 strWhitelist = "";
	 }

	 var strExcludeList = "";
	 var strDisplayList = "";

	 for (var intCounter = 0 ;intCounter < strBlacklist.length ; intCounter++)	//loop throuhgh blacklist
	 {
		 if (strWhitelist.indexOf(strBlacklist.charAt(intCounter)) < 0)
		 {
			 strExcludeList += strBlacklist.charAt(intCounter);		//building exclusion list
			 strDisplayList += strBlacklist.charAt(intCounter) + " ";		//and display list
		 }
	 }
	 for (var intCounter = 0 ;intCounter < strExcludeList.length ; intCounter++)	//loop throuhgh blacklist
	 {
		 if (inputelementCheck.value.indexOf(strExcludeList.charAt(intCounter)) >= 0)	//filtering input string
		 {
			alert("\n\n-This field cannot contain the characters:\n\n\t" + strDisplayList + "\n\n");
			inputelementCheck.value = "";
			inputelementCheck.focus();
			return false;
		 }
	 }
	 return true;
 }