/*
* Name: validateField.js
* Description: common functions for validating forms
* 
*  Date       Author             	Description
* ----------  ---------------- 		----------------------
* 03-02-2001  J.Morrison	       		created
* 09-06-2006  T.Smelser				Added format,validate 7 versions of phone
* 
* List to do
* - consolidate with existing CFF JavaScript functions
*/

var blnValueFailed = false;  // Variable used for handleFailedFlag. This variable is set to true if a field is invalid.

/***************************************************
DESCRIPTION: common function to set focus to a field.
    
PARAMETERS:
   objField - the field object to set focus to.
   valPassed - global
      
RETURNS:
   True if valPassed is true, otherwise false.
*************************************************/
function handleFailedFlag (objField) { 

	if (blnValueFailed == true)
  	{
  		// Flag has been set (an invalid field was detected)
		// Set focus and clear flag.
		if(objField.disabled == false){
	  		objField.focus();
  			objField.select();
		}
		blnValueFailed = false;
		return false;
   	}  
   	else{
		// Flag has not been set
		return true;
	}
}

/******************************************************************
DESCRIPTION: common function to check for special characters.
    
PARAMETERS:
   objField - the field object to set focus to.
      
RETURNS:
   None.
******************************************************************/
function checkSpecialChar(objField)
{
	// Set list of special characters.
  	var objRegExp = /\!|\(|\)|\_|\+|=|\[|\]|\{|\}|;|:|\\|'|"|\.|,|<|>|\/|-|\@|\~|\`|\#|\%|\&|\*|\x24|\x5E|\x3F|\x7C|\^/g
  	var strValue = objField.value
 
  	//check to see if special characters exist
	
  	if(objRegExp.test(strValue)){
   		alert("You are not allowed to enter special charaters!"); //doesn't match pattern, bad date
	}
}

function checkRequiredField(objField, strMessage)
/************************************************
DESCRIPTION: common function for required field validation
			 including 1) checking whether the value is filled in
			 2) place the cursor focus on the first missing required field
			 3) display a JavaScript message box
    
PARAMETERS:
   objField - the field object 
   strMessage - text to display in the JavaScript message
      
RETURNS:
   True if validation check passed, otherwise false.
*************************************************/
{
	var strFieldValue = String(objField.value);
	
	if (!checkEmptyValue(strFieldValue)){
		// Field is not blank.
		return true;
	}
	
	// Field is blank, validation failed.		
	objField.focus();
	alert(strMessage);
	// Set flag used to return focus to field.
	blnValueFailed = true;
	return false;
}

function checkEmptyValue(strValue)
/************************************************
DESCRIPTION: Pass in a text field object to verify
			if it is a blank field.  
    
PARAMETERS:
   strValue - value to check whether blank or not
   
RETURNS:
   True if blank(or just spaces), otherwise false.
*************************************************/
{
	var i;
	
	for (i = 0; i < strValue.length; i++)
  	{
    	if (strValue.charAt(i) != " "){
			// Field is not blank.
			return false;
		}
	}
	return true;
}


/*****************************************************************************
					BEGIN DATE FUNCTIONS
*******************************************************************************/

function checkDate(objDateField) {
/************************************************
DESCRIPTION: Pass in a date object to verify if it is a 
			valid date.  Separate error messages will be
			displayed depending on the error code.
    
PARAMETERS:
   objDateField - Date Object to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
*************************************************/
     var aryMonth = new Array(12);
	
	 aryMonth[0] = "January";
	 aryMonth[1] = "February";
	 aryMonth[2] = "March";
	 aryMonth[3] = "April";
	 aryMonth[4] = "May";
	 aryMonth[5] = "June";
	 aryMonth[6] = "July";
	 aryMonth[7] = "August";
	 aryMonth[8] = "September";
	 aryMonth[9] = "October";
	 aryMonth[10] = "November";
	 aryMonth[11] = "December";
	 
	 if (objDateField.value == "" || objDateField.value == null)
	 	return true;
	 
	 var intResultValue = validateUSDate(objDateField);
	 var strMessage;
	 if (intResultValue != 0) {
		// Validation failed.
		// Determine alert message based on return code.
		switch(intResultValue){
			case -99:
				strMessage = "Year must be greater than 1600"
				break
			case -1:
				strMessage = "Invalid Date Format. Please enter a date in this format: mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy."
				break
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				strMessage = aryMonth[intResultValue-1] + " can only have 31 days"
				break
			case 2:
				strMessage = "February has invalid number of days"
				break
			case 4:
			case 6:
			case 9:
			case 11:
				strMessage = aryMonth[intResultValue-1] + " can only have 30 days"
				break
			case 13:
				strMessage = "Invalid Month"
				break
		}
		
		alert(strMessage);
		// Set flag used to return focus to field.
		blnValueFailed = true;
		objDateField.focus();
		return false;
	}
	else {
		return true;
   }
}

function validateUSDate( objDateField ) {
/************************************************
DESCRIPTION: Validates that a string contains only 
    valid dates with 2 digit month, 2 digit day, 
    4 digit year. Date separator can be ., -, or /.
    Uses combination of regular expressions and 
    string parsing to validate date.
    Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy
    
PARAMETERS:
   objDateField - Date Object to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
   
REMARKS:
   Avoids some of the limitations of the Date.parse()
   method such as the date separator character.
*************************************************/
  var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
  var strValue = objDateField.value
 
  //check to see if in correct format
	
  if(!objRegExp.test(strValue))
    return -1; //doesn't match pattern, bad date
  else{
    var aryDate = strValue.split(RegExp.$1); //split date into month, day, year
	var intDay = parseInt(aryDate[1],10); 
	var intYear = parseInt(aryDate[2],10);
    var intMonth = parseInt(aryDate[0],10);
	
	// check for year to make sure it is after 1600.
	if (intYear <= 1600) {
		return -99;
	}
	
	//check for valid month
	if(intMonth > 12 || intMonth < 1) {
		return 13;
	}
	
    //create a lookup for months not equal to Feb.
    var aryLookup = { '1' : 31,'3' : 31, '4' : 30,'5' : 31,'6' : 30,'7' : 31,
                        '8' : 31,'9' : 30,'10' : 31,'11' : 30,'12' : 31}
  
    //check if month value and day value agree
    if(aryLookup[intMonth] != null) {
    	if(intDay <= aryLookup[intMonth] && intDay != 0) {
        	objDateField.value = intMonth + "/" + intDay + "/" + intYear 
			return 0; //found in lookup table, good date
		}
	  	else{
			return intMonth;
		}
    }
	
		
    //check for February
	var blnLeapYear = (intYear % 4 == 0 && (intYear % 100 != 0 || intYear % 400 == 0));
    if( ((blnLeapYear && intDay <= 29) || (!blnLeapYear && intDay <=28)) && intDay !=0) {
	  //OLDstrDateField.value = intMonth + "/" + intDay + "/" + intYear
      objDateField.value = intMonth + "/" + intDay + "/" + intYear 
	
	  return 0; //Feb. had valid number of days
	  }
	else
		return intMonth;
  }
  // return false; //any other values, bad date
}

function doDateCheck(objFrom, objTo) {
	if (Date.parse(objFrom.value) <= Date.parse(objTo.value)) {
		alert("The dates are valid.");
	}
	else {
	if (objFrom.value == "" || objTo.value == "") 
		alert("Both dates must be entered.");
	else 
		alert("To date must occur after the from date.");
   }
}

function validateBetweenDates(objDateLow, objDateHigh) {
	if (objDateLow.value != "" && objDateHigh.value != "") {	
	
		if (checkDate(objDateLow)) {// no action
		}
		else {return false}

		if (checkDate(objDateHigh)) {// no action
		}
		else {return false}
	
		// Make sure that high date is beyond the low date
		if (getDateDiff(objDateLow, objDateHigh) < 0) {
			alert("The end date must be greater than or equal to the start date in a date search")
			objDateLow.focus()
			// Set flag used to return focus to field.
			blnValueFailed = true;
			return false
		}
	}
	return true;
}
function getDateDiff(objDtmFirst, objDtmSecond) {
	intTempfirst = Date.parse(objDtmFirst.value)
	intTempsecond = Date.parse(objDtmSecond.value)
	intNumDaysDiff = intTempsecond - intTempfirst
	intNumDaysDiff = Math.round(intNumDaysDiff / (1000 * 60 * 60 * 24));
	return intNumDaysDiff;
}

/*****************************************************************************
					END DATE FUNCTIONS
*******************************************************************************/


/********************************************************
* Description: A wrapper function to validate numeric value
*
* PARAMETERS:
* objValue - String to be tested for validity
**********************************************************/
function checkNumber( objValue ) {
if (objValue.value == ""){
	// field is blank.  do not validate.
	return true;
}
var intResultValue = validateNumeric( objValue)
if (intResultValue == 0) {
	alert("Invalid value. Value must be a number.")
	objValue.focus();
	// Set flag used to return focus to field.
	blnValueFailed = true;
	return false;
}
else
	return true;
}

function  validateNumeric( objValue ) {
/******************************************************************************
DESCRIPTION: Validates that a string object contains only valid numbers.

PARAMETERS:
   objValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
******************************************************************************/
 var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/; 
  
  //check for numeric characters
  return objRegExp.test(objValue.value);
}

/***************************************************************
Description: A wrapper function to validate if number is integer
*
* PARAMETERS:
* objValue - String to be tested for validity
*****************************************************************/
function checkInteger( objValue, strMessage ) {
if (objValue.value == ""){
	// field is blank.  do not validate.
	return true;
}
else{
	var intResultValue = validateInteger( objValue)
	if (intResultValue == 0) {
		alert(strMessage);
		objValue.focus();
		// Set flag used to return focus to field.
		blnValueFailed = true;
		return false;
	}
	else
		return true;
	}
}

function validateInteger( objValue ) {
/************************************************
DESCRIPTION: Validates that a string object contains only 
    valid integer number.
    
PARAMETERS:
   objValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
******************************************************************************/
  var objRegExp  = /(^-?\d\d*$)/;
 
  //check for integer characters
  return objRegExp.test(objValue.value);
}
/***************************************************************
Description: A function to validate if number is a positive integer
*			 or zero
*
* PARAMETERS:
* objValue - String to be tested for validity
*****************************************************************/
function checkPositiveInteger( objValue ) {
	if (!checkInteger(objValue)) {
		return false;
	}
	else {
		var intValue = objValue.value * 1;
		if (intValue < 0) {
			alert("Value cannot be less than zero");
			objValue.focus();
			// Set flag used to return focus to field.
			blnValueFailed = true;
			return false;
		}
		else {
			return true;
		}
	}
}
			
	
/*****************************************************************************
					BEGIN CURRENCY FUNCTIONS
*******************************************************************************/

function checkCurrency(objInputField){
/***************************************************************
* Description: A wrapper function to check currency and format it
*
* PARAMETERS:
* objInputField - Form field object to be tested and formatted.
*****************************************************************/
	if(objInputField.value == ""){
		// Field is empty - nothing to validate, so field passes check
		return true;
	}
	else{
		// remove formatting.
		removeSpace(objInputField);
		removeCommas(objInputField);
		removeCurrency(objInputField);
		
		if(!validateNumeric(objInputField)){
			alert("Invalid currency value!");
			objInputField.focus();
			// Set flag used to return focus to field.
			blnValueFailed = true;
			return false;
		}
		else{
			// Valid number, so format field.
			return formatNumericAndCurrency(objInputField,false,true,true,2,"$");
		}
	}
}


function getCurrencyValue(objInputField){
/***************************************************************
* Description: A function to get the numeric value of a currency
*				field (for use in calculations)
*
* PARAMETERS:
* objInputField - Form field object to be tested and formatted.
*
* RETURNS: integer value of the passed in field.
*****************************************************************/
		// Create object so that original field is not affected
		var objNumericUnit = new Object();
		var intValue;
		objNumericUnit.value = objInputField.value;

		// Remove commas and currency
		intValue=removeSpace(objNumericUnit);
		intValue=removeCommas(objNumericUnit);
		intValue=removeCurrency(objNumericUnit);
	
		// Force js to see value as a number.  If empty, calculate as zero.
		intValue = parseFloat((intValue == "")? 0 : intValue);
		
	return intValue;		
}

function formatNumericAndCurrency(objInputField, blnInteger, blnIncludeCommas, blnOverrideDecimals, numDigitsAfterDecimal, strCurrencySymbol) {
/***************************************************************
* Description: A function to validate currency and integers.
*				This function also first strips out any currenty
*				symbols and commas.  It then validates that the
*				remaining characters are numeric.  Finally, it
*				it formats the field based on the input arguments
* 				and returns true if number is valid.
*
* PARAMETERS:
* objValue - Form field object to be tested and formatted.
*****************************************************************/
if (blnInteger) {
	if (numDigitsAfterDecimal) {
		alert("Invalid Parameters Passed")
		// Set flag used to return focus to field.
		blnValueFailed = true;
		return false
	}
}
			
if (objInputField.value != "") {

	removeCommas(objInputField);
	removeCurrency(objInputField);

	if (blnInteger) {
		if (validateInteger(objInputField)) {
		// field passed validation okay
		}
		else {
			alert("Number is not an Integer")
			return false
		}
	}
	else {
		if (validateNumeric(objInputField)) {
		// field passed validation okay
		}
		else {
			alert("Number is not valid")
			return false
		}
	}

	// check for negative value and strip off for future addition.
	if (objInputField.value.substring(0, 1) == '-') {
		// strip off negative sign and set flag
		blnNegativeValue = true
		strTmpValue = objInputField.value
		objInputField.value = strTmpValue.substring(1, strTmpValue.length)
	}
	else {
		blnNegativeValue = false
	}

	// Remove leading zero
 	if (objInputField.value.substring(0,1) == "0"){
		objInputField.value = objInputField.value.substring(1,objInputField.value.length)
	}

	// Check for decimal place existence.
	idxFirstDecimal = objInputField.value.indexOf('.');
	idxLastDecimal  = objInputField.value.lastIndexOf('.');

	if (idxFirstDecimal >= 0) { // At least one decimal point found.
		if (idxLastDecimal != idxFirstDecimal) { // too many decimals error
			alert("Only one decimal point should be entered")
			return false
		}

		aryNum = objInputField.value.split('.')
		numLeftOfDecimal = aryNum[0]
		numRightOfDecimal = aryNum[1]
		if (numLeftOfDecimal == "") {
			numLeftOfDecimal = "0"
		}

	}
	else { // no decimals are present in input number. 
	
		//numLeftOfDecimal = objNumericUnit.InputValue
		numLeftOfDecimal = objInputField.value
		numRightOfDecimal = ""
	}	

	if (blnOverrideDecimals) {
		// Pad right of decimal with trailing zeroes as appropriate
		for (var intDigit = numRightOfDecimal.length; intDigit < numDigitsAfterDecimal; intDigit++) {
			numRightOfDecimal += "0"
		}

		if (numRightOfDecimal.length > 0) { // chop off digits if they entered more than allowed.
			numRightOfDecimal = numRightOfDecimal.substring(0, numDigitsAfterDecimal);
		}
	}	

	if (blnIncludeCommas) {
		switch(numLeftOfDecimal.length) {
			case 0:
				break;
			case 1:
				break;
			case 2:
				break;
			case 3:
				break;
			case 4:
				tmpLeftOfDecimal =	numLeftOfDecimal.substring(0, 1) + 
									"," + 
									numLeftOfDecimal.substring(1,4)
				numLeftOfDecimal =	tmpLeftOfDecimal
				break;
			case 5:
				tmpLeftOfDecimal =	numLeftOfDecimal.substring(0, 2) + 
									"," + 
									numLeftOfDecimal.substring(2,5)
				numLeftOfDecimal =	tmpLeftOfDecimal
				break;
			case 6:
				tmpLeftOfDecimal =	numLeftOfDecimal.substring(0, 3) + 
									"," + 
									numLeftOfDecimal.substring(3,6)
				numLeftOfDecimal =	tmpLeftOfDecimal
				break;
			case 7:
				tmpLeftOfDecimal =	numLeftOfDecimal.substring(0, 1) + 
									"," + 
									numLeftOfDecimal.substring(1,4) + 
									"," + 
									numLeftOfDecimal.substring(4,7)
				numLeftOfDecimal =	tmpLeftOfDecimal
				break;
			case 8:
				tmpLeftOfDecimal =	numLeftOfDecimal.substring(0, 2) + 
									"," + 
									numLeftOfDecimal.substring(2,5) + 
									"," + 
									numLeftOfDecimal.substring(5,8)
				numLeftOfDecimal =	tmpLeftOfDecimal
				break;
			case 9:
				tmpLeftOfDecimal =	numLeftOfDecimal.substring(0, 3) + 
									"," + 
									numLeftOfDecimal.substring(3,6) + 
									"," + 
									numLeftOfDecimal.substring(6,9)
				numLeftOfDecimal =	tmpLeftOfDecimal
				break;
			case 10:
				tmpLeftOfDecimal =	numLeftOfDecimal.substring(0, 1) + 
									"," + 
									numLeftOfDecimal.substring(1,4) + 
									"," + 
									numLeftOfDecimal.substring(4,7) +
									"," +
									numLeftOfDecimal.substring(7,10)
				numLeftOfDecimal =	tmpLeftOfDecimal
				break;
			case 11:
				tmpLeftOfDecimal =	numLeftOfDecimal.substring(0, 2) + 
									"," + 
									numLeftOfDecimal.substring(2,5) + 
									"," + 
									numLeftOfDecimal.substring(5,8) +
									"," +
									numLeftOfDecimal.substring(8,11)
				numLeftOfDecimal =	tmpLeftOfDecimal
				break;
			case 12:
				tmpLeftOfDecimal =	numLeftOfDecimal.substring(0, 3) + 
									"," + 
									numLeftOfDecimal.substring(3,6) + 
									"," + 
									numLeftOfDecimal.substring(6,9) +
									"," +
									numLeftOfDecimal.substring(9,12)
				numLeftOfDecimal =	tmpLeftOfDecimal
				break;
			default:
				alert("A number of length greater than 12 left of the decimal was passed to comma insert routine.  Can only handle up to 12 places")
				return false
				break;
		}
	}

	if (strCurrencySymbol != "") { // create output field with currency symbol on front.
		numLeftOfDecimal = strCurrencySymbol + numLeftOfDecimal
	}

	if (blnNegativeValue) {
		numLeftOfDecimal = "-" + numLeftOfDecimal
	}	
	
	if (numRightOfDecimal.length != 0) {
		objInputField.value = numLeftOfDecimal + "." + numRightOfDecimal
	}
	else { // don't include the decimal point.
		objInputField.value = numLeftOfDecimal
	}
}	
	return true;	

}

/*****************************************************************************
					END CURRENCY FUNCTIONS
*******************************************************************************/


/*****************************************************************************
					BEGIN REMOVE FUNCTIONS
*******************************************************************************/

function removeCurrency(objValue) {
/************************************************
DESCRIPTION: Removes currency formatting from 
  source string.
  
PARAMETERS: 
  objValue - 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(objValue.value)){
    strMinus = '-';
  }
  
  objRegExp = /\-|[,]/g;
  objValue.value = objValue.value.replace(objRegExp,'');
  if(objValue.value.indexOf('$') >= 0){
    objValue.value = objValue.value.substring(1, objValue.value.length);
  }
  
  objValue.value = strMinus + objValue.value
  return objValue.value;
}

function removeCommas( objValue ) {
/************************************************
DESCRIPTION: Removes commas from source string.

PARAMETERS: 
  objValue - 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
  var strString = objValue.value.replace(objRegExp,'');
  
  objValue.value = objValue.value.replace(objRegExp,'');
  return objValue.value;
}

function removeDash( objValue ) {
/************************************************
DESCRIPTION: Removes dashes from source string.

PARAMETERS: 
  objValue - Source string from which dash will 
    be removed;

RETURNS: Source string with dashes removed.
*************************************************/
  var objRegExp = /\-/g; //search for dash globally
 
  //replace all matches with empty strings
  var strString = objValue.value.replace(objRegExp,'');

  objValue.value = objValue.value.replace(objRegExp,'');
}

function removeParenthesis( objValue ) {
/************************************************
DESCRIPTION: Removes Parenthesis from source string.

PARAMETERS: 
  objValue - Source string from which Parenthesis will 
    be removed;

RETURNS: Source string with Parenthesis removed.
*************************************************/
  var objRegExp = /\(|\)/g; //search for Parenthesis globally
 
  //replace all matches with empty strings
  var strString = objValue.value.replace(objRegExp,'');

  objValue.value = objValue.value.replace(objRegExp,'');
}

function removePercent(objValue) {
/************************************************
DESCRIPTION: Removes Percent sign from source string.

PARAMETERS: 
  objValue - Source string from which Percent will 
    be removed;

RETURNS: Source string with Percent removed.
*************************************************/
  var objRegExp = /\%/g; //search for Percent globally
 
  //replace all matches with empty strings
  var strString = objValue.value.replace(objRegExp,'');

  objValue.value = objValue.value.replace(objRegExp,'');
}

function removeSpace(objValue) {
/************************************************
DESCRIPTION: Removes spaces from source string.

PARAMETERS: 
  objValue - Source string from which Space will 
    be removed;

RETURNS: Source string with Space removed.
*************************************************/
  var objRegExp = /\s/g; //search for Space globally
 
  //replace all matches with empty strings
  var strString = objValue.value.replace(objRegExp,'');

  objValue.value = objValue.value.replace(objRegExp,'');
}
/*****************************************************************************
					END REMOVE FUNCTIONS
*******************************************************************************/


/**************************************************
DESCRIPTION: A wrapper to check and format SSN

PARAMERTERS:
	 objValue - Source string to be checked

**************************************************/
function checkSSN (objValue) {

	if (objValue.value == "" || objValue.value == null)
		return true;
		
	if (validateSSN (objValue)) {
		formatSSN (objValue);
		return true;
	}
	else {
		alert("Social Security Number is not Valid");
		// Set flag used to return focus to field.
		blnValueFailed = true;
		return false
	}
}
function validateSSN( objValue ) {
/************************************************
DESCRIPTION: Validates that a string contains a 
  valid social security number. 
  
 PARAMETERS:
   objValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
*************************************************/
var objRegExp = /^\d{3}((\s|\-)?)\d{2}\1\d{4}$/
 
  //check for valid SSN
  return objRegExp.test(objValue.value);

}

function formatSSN ( objValue ) {
/***********************************************
 DESCRIPTION: Format any valid SSN into the following
 	format: ddd-dd-dddd
	
 PARAMETERS:
 	objValue - String to be formatted
	
 RETURNS:
 	the formatted string.
*************************************************/

var objRegExp = /(\s|\-)/g; //search for commas globally
var objRegExp2 = /(\d{3})(\d{2})(\d{4})/;

objValue.value = objValue.value.replace(objRegExp,'').replace(objRegExp2,'$1-$2-$3');
return objValue.value;


}
/**************************************************
DESCRIPTION: A wrapper to check and format Phone Number
*
PARAMERTERS:
	 objValue - Source string to be checked
*
**************************************************/
function checkPhoneNumber (objValue, strMessage) {

	if (objValue.value == "" || objValue.value == null)
		return true;
		
	if (validatePhoneNumber (objValue)) {
		formatPhoneNumber (objValue);
		return true;
	}
	else {
		alert(strMessage);
		objValue.focus();
		// Set flag used to return focus to field.
		blnValueFailed = true;
		return false
	}
}
function checkPhoneNumber7 (objValue, strMessage) {

	if (objValue.value == "" || objValue.value == null)
		return true;
		
	if (validatePhoneNumber7 (objValue)) {
		formatPhoneNumber7 (objValue);
		return true;
	}
	else {
		alert(strMessage);
		objValue.focus();
		// Set flag used to return focus to field.
		blnValueFailed = true;
		return false
	}
}
function validatePhoneNumber( objValue ) {
/************************************************
DESCRIPTION: Validates that a string contains a 
  valid Phone number. 
  
 PARAMETERS:
   objValue - String to be tested for validity
   format allowed: nnn-nnn-nnnn
   or			   nnnnnnnnnn
   or			   nnn-nnnnnnn
   or			   nnnnnn-nnnn
   or			  (nnn)nnn-nnnn
   or			  (nnn) nnn-nnnn
   or			  (nnn)nnnnnnn
   or			  (nnn) nnnnnnn
      
RETURNS:
   True if valid, otherwise false.
*************************************************/
var objRegExp = /((^\d{3}(\-)?)|(^(\()\d{3}(\))))\s?\d{3}(\-)?\d{4}$/

  //check for valid phone number
  return objRegExp.test(objValue.value);

}
function validatePhoneNumber7( objValue ) {
/************************************************
DESCRIPTION: Validates that a string contains a 
  valid 7 digit Phone number. 
  
 PARAMETERS:
   objValue - String to be tested for validity
   format allowed: nnn-nnnn
   or			   nnnnnnn
      
RETURNS:
   True if valid, otherwise false.
*************************************************/
var objRegExp  = /(^\d{7}$)|(^\d{3}-\d{4}$)/;


  //check for valid phone number
  return objRegExp.test(objValue.value);

}
function formatPhoneNumber ( objValue ) {
/***********************************************
 DESCRIPTION: Format any valid SSN into the following
 	format: (nnn) nnn-nnnn
	
 PARAMETERS:
 	objValue - String to be formatted
	
 RETURNS:
 	the formatted string.
*************************************************/

var objRegExp = /(\-|\(|\)|\s)/g; 
var objRegExp2 = /(\d{3})(\d{3})(\d{4})/;

objValue.value = objValue.value.replace(objRegExp,'').replace(objRegExp2,'($1) $2-$3');
return objValue.value;


}
function formatPhoneNumber7 ( objValue ) {
/***********************************************
 DESCRIPTION: Format any valid 7 digit Phone
 	format: nnn-nnnn
	
 PARAMETERS:
 	objValue - String to be formatted
	
 RETURNS:
 	the formatted string.
*************************************************/

var objRegExp = /(\-|\(|\)|\s)/g; 
var objRegExp2 = /(\d{3})(\d{4})/;
objValue.value = objValue.value.replace(objRegExp,'').replace(objRegExp2,'$1-$2');
return objValue.value;


}	
/**************************************************
DESCRIPTION: A wrapper to check and format Zip Code
*
PARAMERTERS:
	 objValue - Source string to be checked
*
**************************************************/
function checkZipCode (objValue) {
	
	if (objValue.value == "" || objValue.value == null)
		return true;
		
	if (validateZipCode(objValue)) {
		return true;
	}
	else {
		alert("Zip Code is not Valid");
		objValue.focus();
		// Set flag used to return focus to field.
		blnValueFailed = true;
		return false
	}
}
function validateZipCode( objValue ) {
/************************************************
DESCRIPTION: Validates that a string contains a 
  valid Zip Code. 
  
 PARAMETERS:
   objValue - String to be tested for validity
   format allowed: 12345
   				   12345-6789
   
RETURNS:
   True if valid, otherwise false.
*************************************************/

var objRegExp = /(^\d{5}$)|(^\d{5}-\d{4}$)/

  //check for valid Zip Code
  return objRegExp.test(objValue.value);

}



