function fieldchk(reqTxt,reqTxtCnt) {
	var element;
	var chk = "";
	var first;
	for (i=0; i <reqTxtCnt; i++){
		element = document.getElementById(reqTxt[i]);
		if (element.value == "" ) {
			if (chk == "") {
				chk = "found";
				first = element; }
			element.style.backgroundColor = 'red'; 
			}
		else {
			element.style.backgroundColor = 'white'; 
			}
	}
	if (chk != "") {
		alert("REQUIRED FIELDS in RED are missing data.");
		first.focus();
		return (false);
	}
}

function Total(theForm)
{
    var accr = theForm.txtAccruedLeave.value * 1;
    var discr = theForm.txtDiscretionary.value * 1;
    var without = theForm.txtWithoutPayLeave.value * 1;
    
    document.getElementById('lblTotal').innerHTML =  accr + discr + without;
}

function ValidateTravel(theForm)
{
    if (theForm.name == "Travel")
    {
        var reqTxtCnt = 11;
		var reqTxt = new Array(reqTxtCnt);
		reqTxt[0]="txtFirstName";
		reqTxt[1]="txtLastName";
		reqTxt[2]="txtYourEmail";
		reqTxt[3]="txtManagerName";
		reqTxt[4]="txtManagerEmail";
		reqTxt[5]="txtFromDate";
		reqTxt[6]="txtToDate";
		reqTxt[7]="txtAccruedLeave";
		reqTxt[8]="txtDiscretionary";
		reqTxt[9]="txtWithoutPayLeave";
		reqTxt[10]="txtReason";
		
		if (fieldchk(reqTxt,reqTxtCnt) == false)
		{	
		    return false; 
		}
		
		if (theForm.ddlCountry.selectedIndex == 0)
		{
			alert("Please select the Country.");
			theForm.ddlCountry.style.backgroundColor = 'red'; 
			theForm.ddlCountry.focus();
			return false;
		}
		return true;
    }
}
function Validator(theForm)
{
    
	if (theForm.name == "frmTravelAuth") {
		/*var reqTxtCnt = 17;
		var reqTxt = new Array(reqTxtCnt);
		reqTxt[0]="FirstName";
		reqTxt[1]="LastName";
		reqTxt[2]="Address1";
		reqTxt[3]="City";
		reqTxt[4]="State";
		reqTxt[5]="Zip";
		reqTxt[6]="Email";
		reqTxt[7]="MgrName";
		reqTxt[8]="MgrEmail";
		reqTxt[9]="TrvlDays";
		reqTxt[10]="DepDate";
		reqTxt[11]="RetDate";
		reqTxt[12]="TrvlReason";
		reqTxt[13]="DestCity";
		reqTxt[14]="TodayDate";
		reqTxt[15]="eSignature";
		reqTxt[16]="SSNum";*/
		var control = document.getElementById("advMisc");
		if (control.value != "" ) 
		{
		    if (theForm.DestCountry2.selectedIndex == 0)
		    {
		        var reqTxtCnt = 18;
		        var reqTxt = new Array(reqTxtCnt);
		        reqTxt[0]="FirstName";
		        reqTxt[1]="LastName";
		        reqTxt[2]="Address1";
		        reqTxt[3]="City";
		        reqTxt[4]="Email";
		        reqTxt[5]="MgrName";
		        reqTxt[6]="MgrEmail";
		        reqTxt[7]="TrvlDays";
		        reqTxt[8]="DepDate";
		        reqTxt[9]="RetDate";
		        reqTxt[10]="TrvlReason";
		        reqTxt[11]="DestCity";
		        reqTxt[12]="Meeting1";
		        reqTxt[13]="Hotel1";
		        reqTxt[14]="Manager1";
		        reqTxt[15]="advMiscDesc";
		        reqTxt[16]="TodayDate";
		        reqTxt[17]="eSignature";
		    }
		    else
		    {
		        var reqTxtCnt = 22;
		        var reqTxt = new Array(reqTxtCnt);
		        reqTxt[0]="FirstName";
		        reqTxt[1]="LastName";
		        reqTxt[2]="Address1";
		        reqTxt[3]="City";
		        reqTxt[4]="Email";
		        reqTxt[5]="MgrName";
		        reqTxt[6]="MgrEmail";
		        reqTxt[7]="TrvlDays";
		        reqTxt[8]="DepDate";
		        reqTxt[9]="RetDate";
		        reqTxt[10]="TrvlReason";
		        reqTxt[11]="DestCity";
		        reqTxt[12]="Meeting1";
		        reqTxt[13]="Hotel1";
		        reqTxt[14]="Manager1";
		        reqTxt[15]="DestCity2";
		        reqTxt[16]="Meeting2";
		        reqTxt[17]="Hotel2";
		        reqTxt[18]="Manager2";
		        reqTxt[19]="advMiscDesc";
		        reqTxt[20]="TodayDate";
		        reqTxt[21]="eSignature";
		    }
		}
		else
		{
		    if (theForm.DestCountry2.selectedIndex == 0)
		    {
	            var reqTxtCnt = 17;
	            var reqTxt = new Array(reqTxtCnt);
	            reqTxt[0]="FirstName";
	            reqTxt[1]="LastName";
	            reqTxt[2]="Address1";
	            reqTxt[3]="City";
	            reqTxt[4]="Email";
	            reqTxt[5]="MgrName";
	            reqTxt[6]="MgrEmail";
	            reqTxt[7]="TrvlDays";
	            reqTxt[8]="DepDate";
	            reqTxt[9]="RetDate";
	            reqTxt[10]="TrvlReason";
	            reqTxt[11]="DestCity";
	            reqTxt[12]="Meeting1";
	            reqTxt[13]="Hotel1";
	            reqTxt[14]="Manager1";
	            reqTxt[15]="TodayDate";
	            reqTxt[16]="eSignature";
	        }
	        else
	        {
	            var reqTxtCnt = 21;
	            var reqTxt = new Array(reqTxtCnt);
	            reqTxt[0]="FirstName";
	            reqTxt[1]="LastName";
	            reqTxt[2]="Address1";
	            reqTxt[3]="City";
	            reqTxt[4]="Email";
	            reqTxt[5]="MgrName";
	            reqTxt[6]="MgrEmail";
	            reqTxt[7]="TrvlDays";
	            reqTxt[8]="DepDate";
	            reqTxt[9]="RetDate";
	            reqTxt[10]="TrvlReason";
	            reqTxt[11]="DestCity";
	            reqTxt[12]="Meeting1";
	            reqTxt[13]="Hotel1";
	            reqTxt[14]="Manager1";
	            reqTxt[15]="DestCity2";
	            reqTxt[16]="Meeting2";
	            reqTxt[17]="Hotel2";
	            reqTxt[18]="Manager2";
	            reqTxt[19]="TodayDate";
	            reqTxt[20]="eSignature";
	        }
		}
		
		if (fieldchk(reqTxt,reqTxtCnt) == false) {	return (false); }
		
		if (theForm.DestCountry.selectedIndex == 0)
		  {
			alert("Please select the Destination Country.");
			theForm.DestCountry.style.backgroundColor = 'red'; 
			theForm.DestCountry.focus();
			return (false);
		  }
	}
	else {
		var reqTxtCnt = 10;
		var reqTxt = new Array(reqTxtCnt);
		reqTxt[0]="FirstName";	//Social Security Number
		reqTxt[1]="LastName";
		reqTxt[2]="Address1";	//Last Name
		reqTxt[3]="City";
		reqTxt[4]="Phone";
		reqTxt[5]="ProDiscipline";
		reqTxt[6]="Specialty";
		reqTxt[7]="NumOfYears";
		reqTxt[8]="DateAvailable";
		reqTxt[9]="eSignature";
		if (fieldchk(reqTxt,reqTxtCnt) == false) {	return (false); }
		
		if (theForm.Position.selectedIndex == 0)
		  {
			alert("Please select an option for the Position applying for.");
			theForm.Position.style.backgroundColor = 'red'; 
			theForm.Position.focus();
			return (false);
		  }
		
		if (theForm.Referredby.selectedIndex == 0)
		  {
			alert("Please select an option for: How you heard about CTS GLOBAL?");
			theForm.Referredby.style.backgroundColor = 'red'; 
			theForm.Referredby.focus();
			return (false);
		  }
	}
	
	return (true);
}
function ValidateForm(theForm)
{
    if (theForm.name == "frmSafeTimeSheet") {
        var reqTxtCnt = 5;
		var reqTxt = new Array(reqTxtCnt);
		reqTxt[0]="txtEmployeeName";
		reqTxt[1]="txtEmpEmail";
		reqTxt[2]="txtSupervisorName";
		reqTxt[3]="txtSupEmail";
		reqTxt[4]="txtDate";

        if (fieldchk(reqTxt,reqTxtCnt) == false) {
            return (false);
        }
    }
    return (true);
}

function autoMask(field, event, sMask) {
/************************************************/
// <input type=text name=ssn onkeypress="return autoMask(this,event, '###-##-####');">
        //var sMask = "**?##?####";
        var KeyTyped = String.fromCharCode(getKeyCode(event));
        var targ = getTarget(event);
        keyCount = targ.value.length;

	if(keyCount == sMask.length)
	{
		return false;
	}
      if ((sMask.charAt(keyCount+1) != '#') && (sMask.charAt(keyCount+1) != 'A' ) )
      {
         field.value = field.value + KeyTyped + sMask.charAt(keyCount+1);
         return false;
      }

        if (sMask.charAt(keyCount) == '*')
                return true;

        if (sMask.charAt(keyCount) == KeyTyped)
        {
                return true;
        }

        if ((sMask.charAt(keyCount) == '#') && isNumeric(KeyTyped))
           return true;

        if ((sMask.charAt(keyCount) == 'A') && isAlpha(KeyTyped))
         return true;

      if ((sMask.charAt(keyCount+1) == '?') )
      {
         field.value = field.value + KeyTyped + sMask.charAt(keyCount+1);
         return true;
      }
      if (KeyTyped.charCodeAt(0) < 32) return true;
    return false;
}
 function getTarget(e) {
  // IE5
   if (e.srcElement) {
        return e.srcElement;
   }
    if (e.target) {
        return e.target;
   }
 }

  function getKeyCode(e) {
 //IE5
 if (e.srcElement) {
        return e.keyCode
 }
  // NC5
  if (e.target) {
   return e.which
  }
 }

 function isNumeric(c)
{
        var sNumbers = "01234567890";
        if (sNumbers.indexOf(c) == -1)
                return false;
        else return true;

}

function isAlpha(c)
{
        var lCode = c.charCodeAt(0);
        if (lCode >= 65 && lCode <= 122 )
          {
                return true;
         }
        else
        return false;
}

function isPunct(c)
{
        var lCode = c.charCodeAt(0);
        if (lCode >= 32 && lCode <= 47 )
          {
                return true;
         }
        else
        return false;

}

function KeyPress() { if (window.event.keyCode == 13) window.event.keyCode =0; } 
/************************************************/

function popAlert(strValue) {//v1.0
/************************************************/
 alert(strValue);
}

function textCounter( field, countfield, maxlimit ) {
/************************************************/
  if ( field.value.length > maxlimit )
  {
    field.value = field.value.substring( 0, maxlimit );
    alert( 'Description can only be 250 characters in length.' );
    return false;
  }
  else
  {
    countfield.value = maxlimit - field.value.length;
  }
}
function validateCurrency( intValue)  {
/************************************************/
     if (strValue == ""){
    return true;
  }
  
  var objRegExp = /(^\$\d{1,3}(,\d{3})*\.\d{2}$)|(^\(\$\d{1,3}(,\d{3})*\.\d{2}\)$)/;

  return objRegExp.test( strValue );
}
/*******************************************/

function validateTime ( strValue ) {
          if (strValue == ""){
    return true;
  }
  
  var objRegExp = /^([1-9]|1[0-2]):[0-5]\d(:[0-5]\d(\.\d{1,3})?)?$/;

  return objRegExp.test( strValue );

}



function validateState (strValue ) {
/************************************************/
   if (strValue == ""){
    return true;
  }

var objRegExp = /^(AK|AL|AR|AZ|CA|CO|CT|DC|DE|FL|GA|HI|IA|ID|IL|IN|KS|KY|LA|MA|MD|ME|MI|MN|MO|MS|MT|NB|NC|ND|NH|NJ|NM|NV|NY|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VA|VT|WA|WI|WV|WY)$/i; 

  return objRegExp.test(strValue);
}


function validateSSN( strValue ) {
/************************************************/
   if (strValue == ""){
    return true;
  }

var objRegExp  = /^\d{3}\-\d{2}\-\d{4}$/;
 
  //check for valid SSN
  return objRegExp.test(strValue);

}


function validateEmail( strValue) {
/***********************************************/
   if (strValue == ""){
    return true;
  }
var objRegExp  = /^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,3}(\.[a-z]{2}){0,2})$/i;
  //check for valid email
  return objRegExp.test(strValue);
}

function validateUSPhone( strValue ) {
/************************************************/
   if (strValue == ""){
    return true;
  } 
 var objRegExp  =  /^\d\d\d-\d\d\d-\d\d\d\d$/;
 
  //check for valid us phone with or without space between 
  //area code
  return objRegExp.test(strValue); 
}

function  validateNumeric( strValue ) {
/*****************************************/
   if (strValue == ""){
    return true;
  } 
 var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/; 
 
  //check for numeric characters 
  return objRegExp.test(strValue);
}

function validateInteger( strValue ) {
/****************************************/
   if (strValue == ""){
    return true;
  }
  var objRegExp  = /(^-?\d\d*$)/;

 
  //check for integer characters
  return objRegExp.test(strValue);
}

function validateNotEmpty( strValue ) {
/************************************/
   var strTemp = strValue;
   strTemp = trimAll(strTemp);
   if(strTemp.length > 0){
     return true;
   }  
   return false;
}

function validateUSZip( strValue ) {

/**************************************/

   if (strValue == ""){
    return true;
  }
var objRegExp  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
 
  //check for valid US Zipcode
  return objRegExp.test(strValue);
}

function validateUSDate( strValue ) {
/*****************************************/
  if (strValue == ""){
    return true;
  }

  var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
 
  //check to see if in correct format
  if(!objRegExp.test(strValue))
    return false; //doesn't match pattern, bad date
  else{
    var arrayDate = strValue.split(RegExp.$1); //split date into month, day, year
	var intDay = parseInt(arrayDate[1],10); 
	var intYear = parseInt(arrayDate[2],10);
    var intMonth = parseInt(arrayDate[0],10);
	
	//check for valid month
	if(intMonth > 12 || intMonth < 1) {
		return false;
	}
	
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
  
    //check if month value and day value agree
    if(arrayLookup[arrayDate[0]] != null) {
      if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
        return true; //found in lookup table, good date
    }
		
    //check for February
	var booLeapYear = (intYear % 4 == 0 && (intYear % 100 != 0 || intYear % 400 == 0));
    if( ((booLeapYear && intDay <= 29) || (!booLeapYear && intDay <=28)) && intDay !=0)
      return true; //Feb. had valid number of days
  }
  return false; //any other values, bad date
}

function validateValue( strValue, strMatchPattern ) {
/*****************************************/
var objRegExp = new RegExp( strMatchPattern);

 //check if string matches pattern
 return objRegExp.test(strValue);
}


function rightTrim( strValue ) {



/***TRIM AND FORMAT DATA FUNCTIONS BELOW THIS LINE***/


/************************************************

DESCRIPTION: Trims trailing whitespace chars.
    
PARAMETERS:
   strValue - String to be trimmed.  
      
RETURNS:
   Source string with right whitespaces removed.
*************************************************/
var objRegExp = /^([\w\W]*)(\b\s*)$/;
 
      if(objRegExp.test(strValue)) {
       //remove trailing a whitespace characters
       strValue = strValue.replace(objRegExp, '$1');
    }
  return strValue;
}

function leftTrim( strValue ) {
/************************************************
DESCRIPTION: Trims leading whitespace chars.
    
PARAMETERS:
   strValue - String to be trimmed
   
RETURNS:
   Source string with left whitespaces removed.
*************************************************/
var objRegExp = /^(\s*)(\b[\w\W]*)$/;
 
      if(objRegExp.test(strValue)) {
       //remove leading a whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

function trimAll( strValue ) {
/************************************************
DESCRIPTION: Removes leading and trailing spaces.

PARAMETERS: Source string from which spaces will
  be removed;

RETURNS: Source string with whitespaces removed.
*************************************************/ 
 var objRegExp = /^(\s*)$/;

    //check for all spaces
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }
    
   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

function removeCurrency( strValue ) {
/************************************************
DESCRIPTION: Removes currency formatting from 
  source string.
  
PARAMETERS: 
  strValue - Source string from which currency formatting
     will be removed;

RETURNS: Source string with commas removed.
*************************************************/
  var objRegExp = /\(/;
  var strMinus = '';
 
  //check if negative
  if(objRegExp.test(strValue)){
    strMinus = '-';
  }
  
  objRegExp = /\)|\(|[,]/g;
  strValue = strValue.replace(objRegExp,'');
  if(strValue.indexOf('$') >= 0){
    strValue = strValue.substring(1, strValue.length);
  }
  return strMinus + strValue;
}

function addCurrency( strValue ) {
/************************************************
DESCRIPTION: Formats a number as currency.

PARAMETERS: 
  strValue - Source string to be formatted

REMARKS: Assumes number passed is a valid 
  numeric value in the rounded to 2 decimal 
  places.  If not, returns original value.
*************************************************/
  var objRegExp = /-?[0-9]+\.[0-9]{2}$/;
   
    if( objRegExp.test(strValue)) {
      objRegExp.compile('^-');
      strValue = addCommas(strValue);
      if (objRegExp.test(strValue)){
        strValue = '($' + strValue.replace(objRegExp,'') + ')';
      }
      else {
        strValue = '$' + strValue;
      }
      return  strValue;
    }
    else
      return strValue;
}

function removeCommas( strValue ) {
/************************************************
DESCRIPTION: Removes commas from source string.

PARAMETERS: 
  strValue - Source string from which commas will 
    be removed;

RETURNS: Source string with commas removed.
*************************************************/
  var objRegExp = /,/g; //search for commas globally
 
  //replace all matches with empty strings
  return strValue.replace(objRegExp,'');
}

function addCommas( strValue ) {
/************************************************
DESCRIPTION: Inserts commas into numeric string.

PARAMETERS: 
  strValue - source string containing commas.
  
RETURNS: String modified with comma grouping if
  source was all numeric, otherwise source is 
  returned.
  
REMARKS: Used with integers or numbers with
  2 or less decimal places.
*************************************************/
  var objRegExp  = new RegExp('(-?[0-9]+)([0-9]{3})'); 

    //check for match to search criteria
    while(objRegExp.test(strValue)) {
       //replace original string with first group match, 
       //a comma, then second group match
       strValue = strValue.replace(objRegExp, '$1,$2');
    }
  return strValue;
}

function removeCharacters( strValue, strMatchPattern ) {
/************************************************
DESCRIPTION: Removes characters from a source string
  based upon matches of the supplied pattern.

PARAMETERS: 
  strValue - source string containing number.
  
RETURNS: String modified with characters
  matching search pattern removed
  
USAGE:  strNoSpaces = removeCharacters( ' sfdf  dfd', 
                                '\s*')
*************************************************/
 var objRegExp =  new RegExp( strMatchPattern, 'gi' );
 
 //replace passed pattern matches with blanks
  return strValue.replace(objRegExp,'');
}

 function validateMY( strValue ) {
/**************************************/

   if (strValue == ""){
    return true;
  }
var objRegExp  =/^((0[1-9])|(1[0-2]))(\/|\-|\.)(\d{4})$/;
 
  //MM/YYYY
  return objRegExp.test(strValue);
}










