/*****************************************************************
******************************************************************
**
**  NAME:     fed_common.js
**  AUTHOR:   Jon Persson
**  CREATED:  Aug 6, 2002
**  PURPOSE:  Java Script file provides common java script
**	      		functions.
**
**  INFO   CHANGE    CHANGED
**  NMBR   DATE      BY (EMP) DESCRIPTION
**  -----  --------  -------  ---------------------------------
**  XXXXX  08/06/02  JEP     Created file
**
******************************************************************
*****************************************************************/

//****************************************************************
//			     CONSTANTS
//****************************************************************

// keycode values
var UC_ENTER = 13;
var UC_CENTURY_CUTOFF_YEAR = 49;
var DISABLE_ALL = "false";
var REQUIRED_FIELD_ERROR_MESSAGE_ON_PAGE = false;
var MILLISECONDS_PER_DAY = 1000 * 60 * 60 * 24;
var CURRENT_DAY = Math.floor(new Date().valueOf() / MILLISECONDS_PER_DAY);
var AGENT = navigator.userAgent.toLowerCase();
var IS_INTERNET_EXPLORER = ((AGENT.indexOf("msie") != -1) 
					&& (AGENT.indexOf("opera") == -1));
var IS_NETSCAPE = AGENT.indexOf("netscape") != -1;
var IS_FIREFOX = AGENT.indexOf("firefox") != -1;
var IS_MOZILLA = ((AGENT.indexOf("mozilla") != -1) 
					&& (AGENT.indexOf("firefox") == -1) 
					&& (AGENT.indexOf("opera") == -1)
					&& (AGENT.indexOf("netscape") == -1));
var IS_OPERA = AGENT.indexOf("opera") != -1;
var CLASS_ATTRIBUTE_NAME = IS_INTERNET_EXPLORER ? "className" : "class";
var REQUIRED_ATTRIBUTE_NAME = "required";
var DEFAULT_DATE_VALIDATION_ERROR_MESSAGE = "Invalid Date Entered";

//****************************************************************
//			     FUNCTIONS
//****************************************************************
//
//****************************************************************
//								**
//  FUNCTION:	pageInitialize					**
//  PARAMS:	none						**
//  RETURNS:	void						**
//  PURPOSE:	Allows the application to code this method 	**
//		in order to initialize the page (doing 		**
//		something that would typically be done in the	**
//		BODY onload event).  If they choose not to 	**
//		overwrite this method, nothing will happen.	**
//								**
//****************************************************************
function pageInitialize() {

}

//****************************************************************
//
//  FUNCTION:	isAlpha	
//  PARAMS:		value
//  RETURNS:	boolean				
//  PURPOSE:	This function will check to see if the field	
//				value contains an alpha value. 
//
//****************************************************************
function isAlpha(value) {
 
  var alphaInd = true; 
  valid="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

  for (var i=0; i< value.length; i++) {
     if (valid.indexOf(value.charAt(i)) < 0) {
		alphaInd = false;
        break;
     }
  }
  return alphaInd;

}


//****************************************************************
//
//  FUNCTION:	formatCurrency
//  PARAMS:	field - html input field to format 
//
//  RETURNS:	void
//  PURPOSE:	This function is to be used to validate a number
//		        entered and to format the number. 
//		        example ( #,###,###.## )
//
//		Normally called as an "onBlur" event handler.
//
//****************************************************************
function formatCurrency(field) {

  var number = field.value;
  number = number.replace(/,/g, "");	// remove all commas  ','
  number = number.replace(/ /g, "");	// remove all spaces  ' '

  // check if we have a number
  if (number.length < 1) {
    return;
  }

  // validate number
  if (!isNumeric(number)) {
    alert("Invalid Number Entered");
    setInvalidFieldFocus(field);
    field.select();
    return;
  }

  // format the number
  var sign = (number == (number = Math.abs(number)));

  number = Math.floor(number*100+0.50000000001);
  var cents = number%100;
  number = Math.floor(number/100).toString();

  if(cents<10) {
    cents = "0" + cents;
  }

  for (var i = 0; i < Math.floor((number.length-(1+i))/3); i++) {
    number = number.substring(0,number.length-(4*i+3))+','+
    number.substring(number.length-(4*i+3));
  }

  number = (((sign)?'':'-') + number);

  field.value = number;

}

//****************************************************************
//
//  FUNCTION:	setFocus
//  PARAMS:	tabNumber - parameter to indicate the tabIndex to look 
//						for when setting the focus.  Nullable
//  RETURNS:	void
//  PURPOSE:	Sets the focus to the first document element
//		whose tabIndex value is set to the passed
//		parameter.  No focus is set by this function if null 
//		parameter.
//
//		Normally called as an "onLoad" event handler.
//
//****************************************************************
function setFocus(tabNumber) {

	if (tabNumber != null) {
 		var elementList;
 	
 		// Loop through all forms on the page
 		for (var i=0; i < document.forms.length; i++) {
 			elementList = document.forms[i].elements;
 		
 			// Loop through all elements in the current form
 			for (var j=0; j < elementList.length; j++) {
 				if (elementList[j].tabIndex == tabNumber) {
 					elementList[j].focus();
 					break;
 				}
 			}
 		}
 		
 		// Loop through all links on the page		
 		for (var j=0; j < document.links.length; j++) {
		
 			if (document.links[j].tabIndex == tabNumber) {
 				document.links[j].focus();
 				break;
 			}
 		}
 		
  	}
}

//****************************************************************
//
//  FUNCTION:	setFocusByDocumentElement
//  PARAMS:		documentElement - parameter to indicate
//				the document element to receive focus.  Nullable
//  RETURNS:	void
//  PURPOSE:	Sets the focus to the document element provided.
//				No focus is set by this function if document element 
//				is null.
//
//****************************************************************
function setFocusByDocumentElement(documentElement) {
  if (documentElement != null) {
  	documentElement.focus();
  }
}

//****************************************************************
//
//  FUNCTION:	validateDate
//  PARAMS:		field - input date field
//				forceFourDigitYear - defines whether or not four
//						digit year is the required
//  RETURNS:	none
//  PURPOSE:	This function will check to see if the field
//				value contains a correct date.  If so it will
//				format the date (mm/dd/yyyy).  If the date is
//				invalid a error message will appear and the
//				field will receive focus.
//
//****************************************************************
function validateDate(field, forceFourDigitYear) {

  var date = field.value;
  var isValidYearLength = true;
  var dateErrorAlertMessage = DEFAULT_DATE_VALIDATION_ERROR_MESSAGE;
  
  if (date.length >= 1 && forceFourDigitYear) {
  	isValidYearLength = isValidDateWithFourDigitYear(date);
  }
  if (isValidYearLength && isValidDate(date, forceFourDigitYear)) {
    field.value = formatDate(date);
  } else {
  
  	// A different error message is required for invalid year length
  	if (!isValidYearLength) {
  		dateErrorAlertMessage = "Invalid Date - format is MM/DD/YYYY";
  	}
	alert(dateErrorAlertMessage);
    setInvalidFieldFocus(field);
    field.select();
  }
}

//****************************************************************
//
//  FUNCTION:	setInvalidFieldFocus
//  PARAMS:		field - input field
//  RETURNS:	none
//  PURPOSE:	Sets field focus when an entry field is found to
//				be invalid. A global field is necessary to execute 
//				the focus event outside the realm of this function 
//				using a timer.  The timer event is needed for 
//				compatibility with Mozilla browsers.  Currently 
//				there is a bug in Mozilla browsers where the 
//				keypress that causes an onBlur is not executed
// 				until all JavaScript related to the onBlur event is
// 				completed.  Thus focus must be set after this 
//				function has run its course.  NOTE: This Mozilla
//				bug sometimes makes it difficult to debug JavaScript
//				using alert message boxes.
//
//****************************************************************
var globalField;
function setInvalidFieldFocus(field) {
	globalField = field;
    setTimeout("globalField.focus()", 50);
}

//****************************************************************
//
//  FUNCTION:	isValidDate
//  PARAMS:		value - date string to validate
//				forceFourDigitYear - defines whether or not four
//						digit year is the required
//  RETURNS:	boolean - true if valid date
//  PURPOSE:	This function will check to see if the passed
//				in value is a valid gregorian date.
//
//****************************************************************
function isValidDate(value, forceFourDigitYear) {
  var returnValue = true;
  
  // check if we have a date
  if (value.length >= 1) {
  	if (forceFourDigitYear) {
  		returnValue = isValidDateWithFourDigitYear(value);
  	}
  	if (returnValue == true) {
   		returnValue = FedDate.isValid(value);
   	}
  }
  
  return returnValue;
}

//****************************************************************
//
//  FUNCTION:	isValidDateWithFourDigitYear
//  PARAMS:		theDate - date string to validate
//  RETURNS:	boolean - true if valid date with four digit year
//  PURPOSE:	This function will check to see if the passed
//				in value is a valid gregorian date.  If the
//				input date does not have a valid four digit year,
//				the global date validation error message is
//				updated.
//
//****************************************************************
function isValidDateWithFourDigitYear(theDate) {
	var dateToValidate = trimString(theDate);
	var isValidDate = false;

	if (dateToValidate.length >= 8) {
		var year = dateToValidate.substr(dateToValidate.length - 4);
		isValidDate = isNumeric(year);
	}
	
	return isValidDate;
}

//****************************************************************
//
//  FUNCTION:	validateFutureDate
//  PARAMS:		field - input date field
//				forceFourDigitYear - defines whether or not four
//						digit year is the required
//  RETURNS:	none
//  PURPOSE:	This function will check to see if the field
//				value contains a correct date.  If so it will
//				format the date (mm/dd/yyyy).  It will then
//				validate that the date occurs in the future
//				(excluding the current day). If the date is
//				invalid an error message will appear and the
//				field will receive focus.
//
//****************************************************************
function validateFutureDate(field, forceFourDigitYear) {

  validateDate(field, forceFourDigitYear);

  var date = field.value;

  if (isValidDate(date, forceFourDigitYear) && !isValidFutureDate(date)) {
    alert(DEFAULT_DATE_VALIDATION_ERROR_MESSAGE);
    setInvalidFieldFocus(field);
    field.select();
  }
}

//****************************************************************
//
//  FUNCTION:	validateFutureDateInclusive
//  PARAMS:		field - input date field
//				forceFourDigitYear - defines whether or not four
//						digit year is the required
//  RETURNS:	none
//  PURPOSE:	This function will check to see if the field
//				value contains a correct date.  If so it will
//				format the date (mm/dd/yyyy).  It will then
//				validate that the date occurs in the future
//				(including the current day). If the date is
//				invalid an error message will appear and the
//				field will receive focus.
//
//****************************************************************
function validateFutureDateInclusive(field, forceFourDigitYear) {

  validateDate(field, forceFourDigitYear);
  
  var date = field.value;
  if (isValidDate(date, forceFourDigitYear) 
  		&& !isValidFutureDate(date) 
  		&& !isValidCurrentDate(date)) {
  		
    alert(DEFAULT_DATE_VALIDATION_ERROR_MESSAGE);
    setInvalidFieldFocus(field);
    field.select();
  }
}

//****************************************************************
//
//  FUNCTION:	validatePastDate
//  PARAMS:		field - input date field
//				forceFourDigitYear - defines whether or not four
//						digit year is the required
//  RETURNS:	none
//  PURPOSE:	This function will check to see if the field
//				value contains a correct date.  If so it will
//				format the date (mm/dd/yyyy).  It will then
//				validate that the date occurs in the past
//				(excluding the current day). If the date is
//				invalid an error message will appear and the
//				field will receive focus.
//
//****************************************************************
function validatePastDate(field, forceFourDigitYear) {

  validateDate(field, forceFourDigitYear);
  
  var date = field.value;
  if (isValidDate(date, forceFourDigitYear) && !isValidPastDate(date)) {
    alert(DEFAULT_DATE_VALIDATION_ERROR_MESSAGE);
    setInvalidFieldFocus(field);
    field.select();
  }
}

//****************************************************************
//
//  FUNCTION:	validatePastDateInclusive
//  PARAMS:		field - input date field
//				forceFourDigitYear - defines whether or not four
//						digit year is the required
//  RETURNS:	none
//  PURPOSE:	This function will check to see if the field
//				value contains a correct date.  If so it will
//				format the date (mm/dd/yyyy).  It will then
//				validate that the date occurs in the past
//				(including the current day). If the date is
//				invalid an error message will appear and the
//				field will receive focus.
//
//****************************************************************
function validatePastDateInclusive(field, forceFourDigitYear) {

  validateDate(field, forceFourDigitYear);
  
  var date = field.value;
  if (isValidDate(date, forceFourDigitYear) 
  		&& !isValidPastDate(date) 
  		&& !isValidCurrentDate(date)) {
  								
    alert(DEFAULT_DATE_VALIDATION_ERROR_MESSAGE);
    setInvalidFieldFocus(field);
    field.select();
  }
}

//****************************************************************
//
//  FUNCTION:	isValidFutureDate
//  PARAMS:		value - date string to validate
//  RETURNS:	boolean - true if valid date
//  PURPOSE:	This function will check to see if the passed
//				in value is a valid future date (not including the
//				current day).
//
//****************************************************************
function isValidFutureDate(value) {

  var dayToValidate = Math.floor(new Date(value).valueOf() / MILLISECONDS_PER_DAY);
  var returnValue = false;
  
  // If date value is an empty string, "date" is valid
  if (dayToValidate > CURRENT_DAY || value.length < 1) {
    returnValue = true;
  }

  return returnValue;
}

//****************************************************************
//
//  FUNCTION:	isValidPastDate
//  PARAMS:		value - date string to validate
//  RETURNS:	boolean - true if valid date
//  PURPOSE:	This function will check to see if the passed
//				in value is a valid past date (not including the
//				current day).
//
//****************************************************************
function isValidPastDate(value) {

  var dayToValidate = Math.floor(new Date(value).valueOf() / MILLISECONDS_PER_DAY);
  var returnValue = false;
  
  // If date value is an empty string, "date" is valid
  if (dayToValidate < CURRENT_DAY || value.length < 1) {
    returnValue = true;
  }

  return returnValue;
}

//****************************************************************
//
//  FUNCTION:	isValidCurrentDate
//  PARAMS:		value - date string to validate
//  RETURNS:	boolean - true if valid date
//  PURPOSE:	This function will check to see if the passed
//				in value is the current date.
//
//****************************************************************
function isValidCurrentDate(value) {

  var dayToValidate = Math.floor(new Date(value).valueOf() / MILLISECONDS_PER_DAY);
  var returnValue = false;
  
  // If date value is an empty string, "date" is valid
  if (dayToValidate == CURRENT_DAY || value.length < 1) {
    returnValue = true;
  }

  return returnValue;
}

//****************************************************************
//
//  FUNCTION:	validateDecimal
//  PARAMS:	field - input decimal field, 
//          decimalPlacesRequired - desired number of decimal places
//                                - zero if doesn't matter
//          allowNegInd - allow negative amounts also
//  RETURNS:	Boolean - Did the field pass validation?
//  PURPOSE:	This function will check to see if the field
//				value contains a decimal value.  If the field
//				is not decimal a error message will appear and
//				the field will receive focus.
//
//****************************************************************
function validateDecimal(field, decimalPlacesRequired, allowNegInd) {

  	var theDecimalNumber = trimString(field.value);
  	var passedValidation = true;

  	if (!isNumeric(theDecimalNumber)) {
    	passedValidation = false;
    	alert("Invalid Number Entered");
    	setInvalidFieldFocus(field);
    	field.select();
  	} 
  
  	if (passedValidation) {
  	  if (!allowNegInd && theDecimalNumber < 0) {
	    passedValidation = false;
    	alert("Invalid Number Entered - Negative amounts not allowed");
    	setInvalidFieldFocus(field);
    	field.select();
  	  	}
  	}
  	  
  	if (passedValidation) {
      var decimalPosition = theDecimalNumber.indexOf(".");
  	  if (decimalPosition < 0 && decimalPlacesRequired > 0) {
          passedValidation = false;
	      alert("Invalid Number Entered - Decimal Required");
	      setInvalidFieldFocus(field);
          field.select();
      } else {
		  	if (decimalPlacesRequired > 0) {
		  		var lastCharacter = theDecimalNumber.length - 1;
		  		var numberDecimalPlaces = lastCharacter - decimalPosition;
		  		if (!(numberDecimalPlaces == decimalPlacesRequired)) {      
          	  		passedValidation = false;
	          		alert("Invalid Number Entered - Incorrect number of decimal places entered.");
	          		setInvalidFieldFocus(field);
              		field.select();
              	}
		  	}      
      	}
  	}    

	if (passedValidation) {
	  	//if starting with a dot, add a zero in front of it
  		if (theDecimalNumber.charAt(0) == '.')
	  	{
  			field.value = "0" + theDecimalNumber;
	  	}
	  	return true;
	} else {
		return false;
	}
}

//****************************************************************
//
//  FUNCTION:	validateNumber
//  PARAMS:	field - input number field
//  RETURNS:	none
//  PURPOSE:	This function will check to see if the field
//				value contains a numeric value.  If the field
//				is not numeric a error message will appear and
//				the field will receive focus.
//
//****************************************************************
function validateNumber(field) {

  var number = field.value;
  if (!isNumeric(number)) {
    alert("Invalid Number Entered");
    setInvalidFieldFocus(field);
    field.select();
  }

}

//****************************************************************
//  FUNCTION:	validateAlphaNumericNumber						**
//  PARAMS:	field - input field 							**
//  RETURNS:	none											**
//  PURPOSE:	This function will check to see if the field	**
//		value contains an alphanumeric value. If it isn't,		**
//		an error message will appear and the field will receive	**
//		focus.			 										**
//****************************************************************
function validateAlphaNumeric(field) {
  
  valid="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    for (var i=0; i< field.value.length; i++) {
        if (valid.indexOf(field.value.charAt(i)) < 0) {
            alert("Invalid Characters Entered");
    		setInvalidFieldFocus(field);
            field.select();
            
            break;
        }
    }
}
//****************************************************************
//								**
//  FUNCTION:	isNumeric					**
//  PARAMS:	value - number string to validate		**
//  RETURNS:	boolean - true if numeric			**
//  PURPOSE:	This function will check to see if the passed	**
//		in value is a number.				**
//								**
//****************************************************************
function isNumeric(value) {
    return !(isNaN(value));
}

//****************************************************************
//								**
//  FUNCTION:	validateP&CAccountNumber			**
//  PARAMS:	field - input account field 			**
//  RETURNS:	none						**
//  PURPOSE:	This function will check to see if the field	**
//		value contains a valid property and casualty	**
//		account number.  If the account number is valid **
//		the function will properly format the account	**
//		number.  If not an error message will appear.	**
//								**
//****************************************************************
function validatePCAccountNumber(field) {

  var number = field.value;
  if (isValidPCAccountNumber(number)) {
    field.value = formatPCAccountNumber(number);
  } else {
    alert("Invalid Account Number Entered");
    setInvalidFieldFocus(field);
    field.select();
  }

}

//****************************************************************
//								**
//  FUNCTION:	isValidP&CAccountNumber				**
//  PARAMS:	value - account number to validate		**
//  RETURNS:	boolean - true if valid account number		**
//  PURPOSE:	This function will check to see if the passed	**
//		in value is a valid property and casualty	**
//		account number.					**
//								**
//****************************************************************
function isValidPCAccountNumber(value) {

  // check if we have a number
  if (value.length < 1) {
    return true;
  }

  // remove any previous formatting
  if (value.length==9 && value.charAt(3)=='-' && value.charAt(7)=='-') {
    value = value.replace("-",""); 
    value = value.replace("-",""); 
  }

  // must be numeric and length of seven
  return (isNumeric(value) && value.length==7);

}

//****************************************************************
//								**
//  FUNCTION:	validatePhoneNumber				**
//  PARAMS:	field - input phone field 			**
//  RETURNS:	none						**
//  PURPOSE:	This function will check to see if the field	**
//		value contains a valid phone number.  If the 	**
//		field contains a valid phone number it will	**
//		properly format the number.  If the phone nbr	**
//		is invalid an error message will appear.	**
//								**
//****************************************************************
function validatePhoneNumber(field) {

  var number = field.value;
  if (isValidNorthAmericanPhoneNumber(number)) {
    field.value = formatNorthAmericanPhoneNumber(number);
  } else {
    alert("Invalid Phone Number Entered");
    setInvalidFieldFocus(field);
    field.select();
  }

}

//****************************************************************
//
//  FUNCTION:	isValidNorthAmericanPhoneNumber
//  PARAMS:		value - phone number to validate
//  RETURNS:	boolean - true if valid phone number
//  PURPOSE:	This function will check to see if the passed
//				in value is a valid phone number.
//
//****************************************************************
function isValidNorthAmericanPhoneNumber(value) {

  // check if we have a date
  if (value.length < 1) {
    return true;
  }

  // remove any formatting characters
  value = value.replace("(", "");	// remove left paran '('
  value = value.replace(")", "");	// remove right paran ')'
  value = value.replace(/-/g, "");	// remove all dashes  '-'
  value = value.replace(/ /g, "");	// remove all spaces  ' '
  value = value.replace(/\./g, "");	// remove all periods  '.'

  // must be numeric and length of 10
  return (isNumeric(value) && value.length==10);

}

//****************************************************************
//								**
//  FUNCTION:	validateTaxId					**
//  PARAMS:	field - input tax id field 			**
//  RETURNS:	none						**
//  PURPOSE:	This function will check to see if the field	**
//		value contains a valid tax id.  If the field	**
//		contains a valid tax id it will propertly 	**
//		format the number.  If the tax id number	**
//		is invalid an error message will appear.	**
//								**
//****************************************************************
function validateTaxId(field) {

  var taxId = field.value;
  if (isValidUSTaxId(taxId)) {
    field.value = formatUSTaxId(taxId);
  } else {
    alert("Invalid Tax Id Entered");
    setInvalidFieldFocus(field);
    field.select();
  }

}

//****************************************************************
//								**
//  FUNCTION:	isValidUSTaxId					**
//  PARAMS:	value - tax id to validate			**
//  RETURNS:	boolean - true if valid US tax id		**
//  PURPOSE:	This function will check to see if the passed	**
//		in value is a valid united states tax id.	**
//		exp: XX-XXXXXXX					**
//		     XXXXXXXXX					**
//								**
//****************************************************************
function isValidUSTaxId(value) {

  // check if we have a date
  if (value.length < 1) {
    return true;
  }

  // check if it is formated (remove formatting)
  if (value.length==10 && value.charAt(2)=='-') {
    value = value.replace("-",""); 
  }

  return (value.length==9 && isNumeric(value));

}

//****************************************************************
//								**
//  FUNCTION:	validateSocialSecurityNumber			**
//  PARAMS:	field - input ssn field 			**
//  RETURNS:	none						**
//  PURPOSE:	This function will check to see if the field	**
//		value contains a social security number.  If 	**
//		the field contains a valid ssn it will 		**
//		propertly format the number.  If the ssn 	**
//		number is invalid an error message will appear.	**
//								**
//****************************************************************
function validateSocialSecurityNumber(field) {

  var number = field.value;
  if (isValidUSSocialSecurityNumber(number)) {
    field.value = formatUSSocialSecurityNumber(number);
  } else {
    alert("Invalid Social Security Number Entered");
    setInvalidFieldFocus(field);
    field.select();
  }

}

//****************************************************************
//								**
//  FUNCTION:	isUSSocialSecurityNumber			**
//  PARAMS:	value - ssn to validate				**
//  RETURNS:	boolean - true if valid US ssn			**
//  PURPOSE:	This function will check to see if the passed	**
//		in value is a valid united states social 	**
//		security number.				**
//		exp: XXX-XX-XXXX				**
//		     XXXXXXXXX					**
//								**
//****************************************************************
function isValidUSSocialSecurityNumber(value) {

  // check if we have a date
  if (value.length < 1) {
    return true;
  }

  // check if it is formated (remove formatting)
  if (value.length==11 && value.charAt(3)=='-' && value.charAt(6)=='-') {
    value = value.replace("-",""); 
    value = value.replace("-",""); 
  }

  return (value.length==9 && isNumeric(value));

}

//****************************************************************
//
//  FUNCTION:	validateZipCode
//  PARAMS:		field - input zip code field
//  RETURNS:	none
//  PURPOSE:	This function will check to see if the field
//				value contains a zip code.  If the field contains 
//				a valid zip code it will properly format the 
//				number.  If the zip code is invalid an error 
//				message will appear.
//
//****************************************************************
function validateZipCode(field) {

  var number = field.value;
  if (isValidZipCode(number)) {
    field.value = formatZipCode(number);
  } else {
    alert("Invalid Zip Code Entered");
    setInvalidFieldFocus(field);
    field.select();
  }

}

//****************************************************************
//
//  FUNCTION:	isValidZipCode
//  PARAMS:		value - zip code to validate
//  RETURNS:	boolean - true if valid zip code
//  PURPOSE:	This function will check to see if the passed
//				in value is a valid zip code.
//		exp: XXXXX
//		     XXXXXXXXX
//			 XXXXX-XXXX
//			 XXXXX XXXX
//
//****************************************************************
function isValidZipCode(value) {

  var returnValue = true;
  
  if (value.length >= 1) {
    // Remove all dashes and spaces
    value = value.replace(/-/g,"");
    value = value.replace(/ /g,"");
    
    returnValue = ((value.length==9 || value.length==5) && isNumeric(value));
  }

  return returnValue;
}

//****************************************************************
//
//  FUNCTION:	validateClaimNumber
//  PARAMS:		field - input claim number field
//  RETURNS:	none
//  PURPOSE:	This function will check to see if the field
//				value contains a claim number.  If the field contains 
//				a valid claim number it will properly format the 
//				number.  If the claim number is invalid an error 
//				message will appear.
//
//****************************************************************
function validateClaimNumber(field) {

  var number = field.value;
  
  if (number.length < 1) {
    return "";
  }
  // check if we have a valid claim number
  if (!isValidClaimNumber(number)) {
    alert("Invalid Claim Number Entered");
    setInvalidFieldFocus(field);
    field.select();
    return;
  }

  // remove any previous formatting
  number = number.replace(/-/g,"");
  number = number.replace(/ /g,"");
  number = number.replace(/\./g,"");
  number = number.toUpperCase();

  field.value = formatNumberString(number, "XX-X-XXXXXX");

}

//****************************************************************
//
//  FUNCTION:	isValidClaimNumber
//  PARAMS:		value - claim number to validate
//  RETURNS:	boolean - true if valid claim number
//  PURPOSE:	This function will check to see if the passed
//				in value is a valid claim number.
//		exp: XXXXXXXXX
//			 XX-X-XXXXXX
//			 XX.X.XXXXXX
//
//****************************************************************
function isValidClaimNumber(value) {

  // check if we have a number
  if (value.length < 1) {
    return true;
  }

  // Remove any formatting - dashes, spaces, and dot
  value = value.replace(/-/g,"");
  value = value.replace(/ /g,"");
  value = value.replace(/\./g,"");
  value = value.toUpperCase();

  if (value.length != 9) {
    return false;
  }
  
  // must be length of nine
  var officeNumber = value.substring(0,2);
  var claimType = value.substring(2,3);
  var claimNumber = value.substring(3,9);

  return (isNumeric(officeNumber) && isAlpha(claimType) && isNumeric(claimNumber));
}

//****************************************************************
//								**
//  FUNCTION:	formatDate					**
//  PARAMS:	dateString - date value to format		**
//		pattern    - date format (optional)		**
//  RETURNS:	string - formatted date value			**
//  PURPOSE:	This function will format the passed in date	**
//		with the proper format.  If no format is given	**
//		the function will default to mm/dd/yyyy.  If 	**
//		passed in date is invalid, the function will	**
//		return null.					**
//								**
//****************************************************************
function formatDate(dateString, pattern, centuryCutoffYear) {

  if (dateString.length < 1) {
    return "";
  }

  // default if pattern not supplied
  if (arguments.length < 3) {
    centuryCutoffYear = UC_CENTURY_CUTOFF_YEAR;
  }

  if (arguments.length < 2) {
    pattern = "mm/dd/yyyy";
  }

  var fedDate = new FedDate(dateString, centuryCutoffYear);
  return fedDate.formatDate(pattern);

}

//****************************************************************
//								**
//  FUNCTION:	unformatDate					**
//  PARAMS:	dateString - date value to unformat		**
//  RETURNS:	string - formatted date value			**
//  PURPOSE:	This function will unformat the passed in date	**
//		with the proper format.  (mmddyyyy)  If the 	**
//		passed in date is invalid, the function will	**
//		return null.					**
//								**
//****************************************************************
function unformatDate(dateString) {
  return formatDate(dateString, "mmddyyyy");
}

//****************************************************************
//								**
//  FUNCTION:	formatP&CAccountNumber				**
//  PARAMS:	value - account number to format		**
//  RETURNS:	string - formatted account number		**
//  PURPOSE:	This function will format the passed in account **
//		number value. (XXX-XXX-X).  If the value is an  **
//		invalid account number, the function will 	**
//		return null.					**
//								**
//****************************************************************
function formatPCAccountNumber(value) {

  if (value.length < 1) {
    return "";
  }

  // check if we have a valid account number
  if (!isValidPCAccountNumber(value)) {
    return value;
  }

  // remove any previuos formatting
  value = value.replace(/-/g, "");	// remove all dashes  '-'

  return formatNumberString(value, "XXX-XXX-X");

}

//****************************************************************
//								**
//  FUNCTION:	formatNorhtAmericanPhoneNumber			**
//  PARAMS:	value - phone number string to format		**
//  RETURNS:	string - formatted phone number			**
//  PURPOSE:	This function will format the passed in phone	**
//		number value. (XXX-XXX-XXXX).  If the value is	**
//		an invalid phone number, the function will 	**
//		return null.					**
//								**
//****************************************************************
function formatNorthAmericanPhoneNumber(value) {

  if (value.length < 1) {
    return "";
  }
 
 // check if we have a valid phone number
  if (!isValidNorthAmericanPhoneNumber(value)) {
    return value;
  }

  // remove any previous formatting
  value = value.replace("(", "");	// remove right paran '('
  value = value.replace(")", "");	// remove left paran  ')'
  value = value.replace(/-/g, "");	// remove all dashes  '-'
  value = value.replace(/ /g, "");	// remove all spaces  ' '
  value = value.replace(/\./g, "");	// remove all periods  '.'

  // return formatted string
  return formatNumberString(value, "XXX-XXX-XXXX");

}

//****************************************************************
//								**
//  FUNCTION:	formatUSTaxId					**
//  PARAMS:	value - tax id to format			**
//  RETURNS:	string - formatted us tax id			**
//  PURPOSE:	This function will format the passed in US	**
//		tax id. (XX-XXXXXXX).  If the passed in string	**
//		is not a valid tax id, the function will return	**
//		null.						**
//								**
//****************************************************************
function formatUSTaxId(value) {

  if (value.length < 1) {
    return "";
  }

  // check if valid tax id
  if (!isValidUSTaxId(value)) {
    return value;
  }

  // remove any previous formatting
  value = value.replace(/-/g, "");	// remove all dashes '-'

  // return formatted tax id
  return formatNumberString(value, "XX-XXXXXXX");

}

//****************************************************************
//								**
//  FUNCTION:	formatUSSocialSecurityNumber			**
//  PARAMS:	value - ssn to format				**
//  RETURNS:	string - formatted SSN				**
//  PURPOSE:	This function will format the passed in social	**
//		security number. (XXX-XX-XXXX).  If the passed	**
//		in string is not a valid ssn, the function will	**
//		return null.					**
//								**
//****************************************************************
function formatUSSocialSecurityNumber(value) {

  if (value.length < 1) {
    return "";
  }

  // check if valid ssn
  if (!isValidUSSocialSecurityNumber(value)) {
    return value;
  }

  // remove any previous formatting
  value = value.replace(/-/g, "");	// remove all dashes '-'

  // return formatted ssn
  return formatNumberString(value, "XXX-XX-XXXX");

}

//****************************************************************
//
//  FUNCTION:	formatZipCode
//  PARAMS:		value - zip code to format
//  RETURNS:	string - formatted zip code
//  PURPOSE:	This function will format the passed in zip code
//				If the passed in string is not a valid zip code, 
//				the function will return null.
//
//****************************************************************
function formatZipCode(value) {

  var returnValue = "";
  if (value.length >= 1) {

    if (isValidZipCode(value)) {
      // remove any previous formatting
      value = value.replace(/-/g,"");
      value = value.replace(/ /g,"");

      // add a dash to a nine digit zip code
  	  if (value.length==9) {
        returnValue = formatNumberString(value, "XXXXX-XXXX");
      } else {
		returnValue = value;
	  }
    } else {
	  returnValue = value;
    }
  }

  return returnValue;
}

//****************************************************************
//								**
//  FUNCTION:	setFormIdAndSubmit				**
//  PARAMS:	form   - form to submit				**
//		formId - formId to submit with function		**
//  RETURNS:	none						**
//  PURPOSE:	This function will set the formId to the given	**
//		form and then submit the form.			**
//								**
//****************************************************************
function setFormIdAndSubmit(form, formId) {

  // set the formid
  form.formId.value = formId;

  // submit the form
  submitForm(form);

}

//****************************************************************
//
//  FUNCTION:	askForConfirmation(message)
//  PARAMS:		message - confirmation message to present to user
//  RETURNS:	boolean describing confirmation selection
//  PURPOSE:	Asks user to confirm what he/she is doing.  If null
//				value is passed in the message parameter, a default
//				message is created.
//
//****************************************************************
function askForConfirmation(message) {

  // provide default message if not given
  if (message == null) {
    message = "Are you sure?";
  }

  return confirm(message);	
}

//****************************************************************
//
//  FUNCTION:	confirmLink
//  PARAMS:	url - url to submit
//			message - confirmation message to present to user
//  RETURNS:	none
//  PURPOSE:	This function will look for user confirmation
//				for the link, then continue on to the link.
//
//****************************************************************
function confirmLink(url, message) {

  if (askForConfirmation(message)) {
	location.href =url;
  } 
}

//****************************************************************
//
//  FUNCTION:	confirmOpenNewWindow
//  PARAMS:		url - url to submit
//				message - confirmation message to present to user
//  RETURNS:	none
//  PURPOSE:	This function will look for user confirmation
//				then open a new window to the specified url.
//
//****************************************************************
function confirmOpenNewWindow(url, windowName, windowOptions, message) {

  if (askForConfirmation(message)) {
	openNewWindow(url,windowName,windowOptions); 
  } 
}

//****************************************************************
//
//  FUNCTION:	openNewWindow
//  PARAMS:		url - url to submit
//				windowName - the name of the new window
//				windowOptions - the option string for the new window
//  RETURNS:	none
//  PURPOSE:	This function will open a new window to the specified url.
//
//****************************************************************
function openNewWindow(url, windowName, windowOptions) {

	windowName=window.open(url,windowName,windowOptions); 
	windowName.focus();

}

//****************************************************************
//
//  FUNCTION:	openNewWindowWithDefaults
//  PARAMS:		url - url to submit
//				windowName - the name of the new window
//  RETURNS:	none
//  PURPOSE:	This function will open a new window to the specified url.
//              User browser defaults will be used.
//
//****************************************************************
function openNewWindowWithDefaults(url, windowName) {

	windowName=window.open(url,windowName); 
	windowName.focus();

}


//****************************************************************
//																**
//  FUNCTION:	openNewWindowWithToolBars						**
//  PARAMS:		url - url to submit								**
//				windowName - name of the new window				**
//  RETURNS:	none											**
//  PURPOSE:	This function will open a new window to the		**
//				specified url.  The new window will contain		**
//				the tool bar and menu bar.						**
//																**
//****************************************************************
function openNewWindowWithToolBars(url, windowName) {
	openNewWindow(url, windowName, 'toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes');
}

//****************************************************************
//																**
//  FUNCTION:	openNewWindowNoToolBar							**
//  PARAMS:		url - url to submit								**
//				windowName - name of the new window				**
//  RETURNS:	none											**
//  PURPOSE:	This function will open a new window to the		**
//				specified url.  The new window will NOT			**
//				contain a tool bar or menu bar.					**
//																**
//****************************************************************
function openNewWindowNoToolBar(url, windowName) {
	openNewWindow(url, windowName, 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes');
}

//****************************************************************
//																**
//  FUNCTION:	openWebApplication								**
//  PARAMS:		url - url to submit								**
//  RETURNS:	none											**
//  PURPOSE:	This function will use an ActiveXObject to		**
//				open a new Internet Explorer window to the		**
//				specified url.									**
//																**
//****************************************************************
function openWebApplication(url) {
	
	var objExplorer= new ActiveXObject("InternetExplorer.Application");

	if (objExplorer != null) {
		objExplorer.Visible = true;
		objExplorer.Navigate(url);
	}
}

//****************************************************************
//																**
//  FUNCTION:	openExcelApplication							**
//  PARAMS:		filePath - file path to submit					**
//  RETURNS:	none											**
//  PURPOSE:	This function will use an ActiveXObject to		**
//				open Windows Explorer to the specified			**
//				file path.										**
//																**
//****************************************************************
function openExcelApplication(filePath) {

  var myApp = new ActiveXObject("Excel.Application");

  if (myApp != null) {
    myApp.Visible = true;
    myApp.Workbooks.Open(filePath);
  }
}

//****************************************************************
//																**
//  FUNCTION:	openWindowsExplorer								**
//  PARAMS:		filePath - file path to submit					**
//  RETURNS:	none											**
//  PURPOSE:	This function will use an ActiveXObject to		**
//				open Windows Explorer to the specified			**
//				file path.										**
//																**
//****************************************************************
function openWindowsExplorer(filePath) {
	
	var objExplorer = new ActiveXObject("WScript.Shell");
	
	if (objExplorer != null) {
		// syntax for Run command: Run(strCommand [,intWindowStyle] [,bWaitOnReturn])
		// intWindowStyle = 5: Activates the window and displays it in its current size and position.
		objExplorer.Run("explorer " + filePath, 5);
	}
}

//****************************************************************
//
//  FUNCTION:	setActionAndSubmit
//  PARAMS:	form - form to submit
//			action - action to perform
//  RETURNS:	none
//  PURPOSE:	This function will set the action to the given
//				form and then submit the form.
//
//****************************************************************
function setActionAndSubmit(form, action) {

	// set the action
	form.action = action;
	
	// submit the form
	submitForm(form);

}

//****************************************************************
//
//  FUNCTION:	confirmSetActionAndSubmit
//  PARAMS:	form - form to submit
//			action - action to perform
//			message - confirmation message to present to user
//  RETURNS:	none
//  PURPOSE:	This function will look for user confirmation
//				for the submit, then continue on to the
//				setActionAndSubmit function.
//
//****************************************************************
function confirmSetActionAndSubmit(form, action, message) {
  if (askForConfirmation(message)) {
	setActionAndSubmit(form, action);
  }
}

//*************** Start New 03/04/2003 RAB ***********************

//****************************************************************
//
//  FUNCTION:	editAndSubmit
//  PARAMS:		form - form to submit
//				editType - strength of editing
//  RETURNS:	none
//  PURPOSE:	This function will set the action to the given
//		form and then submit the form.
//
//****************************************************************
function editAndSubmit(form, editType) {

  if (!editRequiredFields(form, editType)) {
    return;
  }

  // submit the form
  submitForm(form);

}

//****************************************************************
//
//  FUNCTION:	confirmEditAndSubmit
//  PARAMS:		form - form to submit
//				message - confirmation message to present to user
//				editType - strength of editing
//  RETURNS:	none
//  PURPOSE:	This function will look for user confirmation
//				for the submit, then continue on to the
//				editAndSubmit function.
//
//****************************************************************
function confirmEditAndSubmit(form, message, editType) {
  if (askForConfirmation(message)) {
	editAndSubmit(form, editType);
  }
}

//****************************************************************
//
//  FUNCTION:	editSetActionAndSubmit
//  PARAMS:		form - form to submit
//				action - action to perform
//				editType - strength of editing
//  RETURNS:	none
//  PURPOSE:	This function will set the action to the given
//		form and then submit the form.
//
//****************************************************************
function editSetActionAndSubmit(form, action, editType) {

  if (!editRequiredFields(form, editType)) {
    return;
  }

  // call to set action and submit
  setActionAndSubmit(form, action);
}

//****************************************************************
//
//  FUNCTION:	confirmEditSetActionAndSubmit
//  PARAMS:		form - form to submit
//				action - action to perform
//				message - confirmation message to present to user
//				editType - strength of editing
//  RETURNS:	none
//  PURPOSE:	This function will look for user confirmation
//				for the submit, then continue on to the
//				editSetActionAndSubmit function.
//
//****************************************************************
function confirmEditSetActionAndSubmit(form, action, message, editType) {
  if (askForConfirmation(message)) {
	editSetActionAndSubmit(form, action, editType);
  }
}

//****************************************************************
//
//  FUNCTION:	editSetTableKeyValueAndSubmit
//  PARAMS:		form - form to submit
//				tableKeyValue - vlaue to set key
//				editType - strength of editing
//  RETURNS:	none
//  PURPOSE:	This function will set the action to the given
//				form and then submit the form.
//
//****************************************************************
function editSetTableKeyValueAndSubmit(form, tableKeyValue, editType) {

  if (!editRequiredFields(form, editType)) {
    return;
  }

  if (!setSelectedTableKey(form, tableKeyValue)) {
    return
  }

  // submit the form
  submitForm(form);

}

//****************************************************************
//
//  FUNCTION:	confirmEditSetTableKeyValueAndSubmit
//  PARAMS:		form - form to submit
//				tableKeyValue - vlaue to set key
//				editType - strength of editing
//				message - confirmation message to present to user
//  RETURNS:	none
//  PURPOSE:	This function will look for user confirmation
//				for the submit, then continue on to the
//				editSetTableKeyValueAndSubmit function.
//
//****************************************************************
function confirmEditSetTableKeyValueAndSubmit(
	form, 
	tableKeyValue, 
	editType, 
	message) {
	
  if (askForConfirmation(message)) {
	editSetTableKeyValueAndSubmit(form, tableKeyValue, editType);
  }
}

//****************************************************************
//
//  FUNCTION:	editSetTableKeyValueSetActionAndSubmit
//  PARAMS:		form - form to submit
//				action - action to perform
//				tableKeyValue - value to set key
//				editType - strength of editing
//  RETURNS:	none
//  PURPOSE:	This function will set the action to the given
//				form and then submit the form.
//
//****************************************************************
function editSetTableKeyValueSetActionAndSubmit(form, action, tableKeyValue, editType) {

  if (!editRequiredFields(form, editType)) {
    return;
  }

  if (!setSelectedTableKey(form, tableKeyValue)) {
    return
  }

  // call to set action and submit
  setActionAndSubmit(form, action)

}

//****************************************************************
//
//  FUNCTION:	confirmEditSetTableKeyValueSetActionAndSubmit
//  PARAMS:		form - form to submit
//				action - action to perform
//				tableKeyValue - value to set key
//				editType - strength of editing
//				message - confirmation message to present to user
//  RETURNS:	none
//  PURPOSE:	This function will set the action to the given
//				form and then submit the form.
//
//****************************************************************
function confirmEditSetTableKeyValueSetActionAndSubmit(
	form, 
	action, 
	tableKeyValue, 
	editType, 
	message) {
	
  if (askForConfirmation(message)) {
	editSetTableKeyValueSetActionAndSubmit(
		form, 
		action,
		tableKeyValue, 
		editType);
  }
}

//****************************************************************
//
//  FUNCTION:	editRequiredFields
//  PARAMS:		form - form to edit
//				editType - strength of editing
//  RETURNS:	none
//  PURPOSE:	This function will check eachfield for required
//				and issue a message if there is not a value.
//
//****************************************************************
function editRequiredFields(form, editType) {
	var returnValue = true;

	if (editType != "none") {
	
		var requiredFields = "";
		var firstFailedElementNumber = -1;

		var a = form.elements;
		var separatorValue = "\n";
		var requiredFieldClassName = "requiredField";
  
		if (REQUIRED_FIELD_ERROR_MESSAGE_ON_PAGE) {
			separatorValue = ", ";
		}

		// Check if a field is required
		for (var i=0; i < a.length; i++) {
			if ((a[i].getAttribute(REQUIRED_ATTRIBUTE_NAME)) 
				&& (a[i].value.length == 0)) {
	           
			  	if (firstFailedElementNumber == -1) {
	    	        firstFailedElementNumber = i;
	      		}
	      		
				requiredFields = requiredFields 
								+ a[i].getAttribute(REQUIRED_ATTRIBUTE_NAME) 
								+ separatorValue;
				var classString = a[i].getAttribute(CLASS_ATTRIBUTE_NAME);
				if (classString == null) {
					classString = "";
				}
				a[i].setAttribute(CLASS_ATTRIBUTE_NAME, classString + " " + requiredFieldClassName);
				setDivClassNameFromElement(a[i]);
	   		} else if (a[i].getAttribute(REQUIRED_ATTRIBUTE_NAME)) {
				// If a required field has been filled in but other required fields
				// are still necessary, revert the className so the field is no
				// longer highlighted as the requiredField css specifies
				var classString = a[i].getAttribute(CLASS_ATTRIBUTE_NAME);
				if (classString != null) {	
					var requiredFieldClassNameIndex = 
							classString.indexOf(
								requiredFieldClassName);
					if (requiredFieldClassNameIndex > -1) {
					
						// the required field class name should always be the last class name
						var updatedClassString = classString.substring(
													0, 
													requiredFieldClassNameIndex);
		   				a[i].setAttribute(CLASS_ATTRIBUTE_NAME, updatedClassString);
		   			}
					setDivClassNameFromElement(a[i]);
				}
		  	} //end else if  	
		} //end for

		// If any field required and not filled in, issue message
		if (requiredFields.length > 0) {
		  	if (!REQUIRED_FIELD_ERROR_MESSAGE_ON_PAGE) {
	    		if (editType == "hard") {
					alert("The following fields are required:\n\n" + requiredFields );
					a[firstFailedElementNumber].focus();
					returnValue = false;
			    } else {  // soft edit
	      
	      			if (!confirm("The following fields are required:\n\n" 
	      							+ requiredFields 
	      							+ "\n\nSelect 'OK' to continue submission.\n"
	      							+ "Select 'Cancel' to return to data entry.")) {
	      							
				        a[firstFailedElementNumber].focus();
				        returnValue = false;
					}
			    } //end else
			} else {  //error message on page
				var messageField = document.getElementById("requiredFieldErrorMessageField");
				var lastSeparatorIndex = requiredFields.lastIndexOf(separatorValue);
				var message = "Please provide the following fields:\n\n" 
								+ requiredFields.substr(0, lastSeparatorIndex);
				messageField.firstChild.nodeValue = message;
				a[firstFailedElementNumber].focus();
				returnValue = false;
			}  //end else
  		}  // end logic if missing required fields were found
 	}  //end no edit
  return returnValue;
}
//****************************************************************
//
//  FUNCTION:	setDivClassNameFromElement
//  PARAMS:		element - the element the DIV tag surrounds
//  RETURNS:	none
//  PURPOSE:	If DIV surrounds a select field that is required,
//				the class for the select field's surrounding DIV
//				tag is updated as necessary.
//
//****************************************************************
function setDivClassNameFromElement(element) {
  	if (element.getAttribute(REQUIRED_ATTRIBUTE_NAME) 
  		&& (element.type.indexOf("select") > -1)) {
  		
		var selectDiv = getDivFromDocument(element.getAttribute("name"));

		if (trimString(element.getAttribute(CLASS_ATTRIBUTE_NAME)) == "") {
			selectDiv.setAttribute(CLASS_ATTRIBUTE_NAME, "selectField");
		} else {
			selectDiv.setAttribute(CLASS_ATTRIBUTE_NAME, 
									"selectField " + 
									element.getAttribute(CLASS_ATTRIBUTE_NAME));
		}
  	}
}

//****************************************************************
//
//  FUNCTION:	trimString
//  PARAMS:		stringToTrim - string to be trimmed
//  RETURNS:	String
//  PURPOSE:	Removes any ' ' characters from the beginning and
//				end of a string.
//
//****************************************************************
function trimString(stringToTrim) {
	var trimmedString = stringToTrim;
	
  	while (trimmedString.charAt(0) == ' ') {
    	trimmedString = trimmedString.substring(1);
    }
  	while (trimmedString.charAt(
  			trimmedString.length - 1) == ' ') {
  			
    	trimmedString = trimmedString.substring(
    						0, 
    						trimmedString.length - 1);
    }
  	return trimmedString;
}

//****************************************************************
//
//  FUNCTION:	getDivFromDocument
//  PARAMS:		name - name value for DIV element to retrieve
//  RETURNS:	the associated DIV element, or null
//  PURPOSE:	This function cycles through all DIV elements in
//				the document looking for a specifically named
//				element.  When found, the DIV element is returned.
//
//****************************************************************
function getDivFromDocument(name) {
	var divList = document.getElementsByTagName('div');
	var divsToCheck = divList.length;
	var divFound = false;
	var returnDiv = null;

	while (divsToCheck-- && !divFound) {
		if (divList[divsToCheck].getAttribute("name") == name) {
			returnDiv = divList[divsToCheck];
			divFound = true;
		}
	}
	return returnDiv;
}

//****************************************************************
//								**
//  FUNCTION:	setSelectedTableKey				**
//  PARAMS:	form     - form to submit			**
//		tableKeyValue - vlaue to set key		**
//  RETURNS:	none						**
//  PURPOSE:	This function will set selectedTableKey 	**
//		hidden input field in a form.			**
//								**
//****************************************************************
function setSelectedTableKey(form, tableKeyValue) {

  if (!form.selectedTableKey) {
    alert("selectedTableKey hidden input field not defined.");
    return false;
  }

  form.selectedTableKey.value = tableKeyValue;
  return true;
}

//**************** End New 03/04/2003 RAB ************************

//**************** Start New 11/11/2003 MGB **********************
//
//  FUNCTION:	wasEnterPressed
//  PARAMS:		theEvent - event enter is being checked on
//  RETURNS:	boolean - true if the keystroke was enter,
//                        false if it wasn't.
//  PURPOSE:	To verify that the key pressed was enter.
//
//****************************************************************
function wasEnterPressed(theEvent) {
	return (theEvent.keyCode == UC_ENTER);
}

//****************************************************************
//
//  FUNCTION:	ignoreEnterKey
//  PARAMS:		field - field this function is being called from
//  PURPOSE:	To override the default behavior of the Enter 
//              key when there is only one input field on a form.
//
//****************************************************************
function ignoreEnterKey(field) {
    var keyCode = window.event.keyCode;
    //alert("in ignoreEnterKey; keyCode = " + window.event.keyCode);
    if (keyCode == 13) {
       window.event.returnValue = false;
    }
}
//****************************************************************
//
//  FUNCTION:	editAndSubmitOnEnter
//  PARAMS:		form   - form to submit	on Enter
//         	 	editType - type of edit
//  			theEvent - event passed in the method since some browsers
//					do not support a global window.event
//  RETURNS:	none
//  PURPOSE:	This function will set the action to the given
//				form and then submit the form on Enter.
//
//****************************************************************
function editAndSubmitOnEnter(form, editType, theEvent) {

	// Incoming event (theEvent) may be null depending on browser
	if (!theEvent) {
		theEvent = window.event;
	}
	
	if (wasEnterPressed(theEvent)) {
     	editAndSubmit(form, editType);
     	theEvent.cancelBubble =true; 
     	theEvent.returnValue=false; 
  	}
}
//*************************************************************************
//
//  FUNCTION:	editSetActionAndSubmitOnEnter
//  PARAMS:		form     - form to submit
//		   		action   - the request that is submitted
//                         the form.
//          	editType - type of edit
//          	button   - button to put focus on
//  			theEvent - event passed in the method since some browsers
//					do not support a global window.event
//  RETURNS:	none
//  PURPOSE:	This function will detect if the enter key
//              has been pressed and submit the specified
//              form with the given request using the
//              editSetActionAndSubmit function with edit type specified.
//
//*************************************************************************
function editSetActionAndSubmitOnEnter(form, action, editType, button, theEvent) {
	// Incoming event (theEvent) may be null depending on browser
	if (!theEvent) {
		theEvent = window.event;
	}

  	if (wasEnterPressed(theEvent)) {
     	if (button != null) {
     		editSetActionAndSubmit(form, action, editType);
     		theEvent.cancelBubble =true; 
     		theEvent.returnValue=false;
     	} 
  	}
}

//*************************************************************************
//
//  FUNCTION:	setActionAndSubmitOnEnter
//  PARAMS:		form     - form to submit
//		    	action   - the request that is submitted
//                         the form.
//		    	button   - the button to put focus on
//  			theEvent - event passed in the method since some browsers
//					do not support a global window.event
//  RETURNS:	none
//  PURPOSE:	This function will detect if the enter key
//              has been pressed and submit the specified
//              form with the given request using the
//              setActionAndSubmit function.
//
//*************************************************************************
function setActionAndSubmitOnEnter(form, action, button, theEvent) {

	// Incoming event (theEvent) may be null depending on browser
	if (!theEvent) {
		theEvent = window.event;
	}

  	if (wasEnterPressed(theEvent)) {
       
     	if (button != null) {
     	
     		setActionAndSubmit(form, action);
     
     		theEvent.cancelBubble=true; 
     		theEvent.returnValue=false;
   	 	}  		
  	}
}
//**************** End New 11/11/2003 MGB ************************
//
//**************** Begin New 03/18/2004 JKJ **********************
//****************************************************************
//																**
//  FUNCTION:	pause (used ONLY for testing!!!)				**
//  PARAMS:	milliseconds										**
//  RETURNS:	none											**
//  PURPOSE:	This function will pause execution				**
//																**
//  The main problem with this function is that it is not 		**
//	sleeping the underlying Javascript interpreter thread.  	**
//	Instead, it is worthlessly burning up a lot of CPU cycles.  **
//																**
//****************************************************************
function pause(numberMillis) {
        var now = new Date();
        var exitTime = now.getTime() + numberMillis;
        while (true) {
            now = new Date();
            if (now.getTime() > exitTime)
                return;
        }
}
//****************************************************************
//																**
//  FUNCTION:	submitForm										**
//  PARAMS:		theForm   - form to submit						**
//  RETURNS:	none											**
//  PURPOSE:	This function will check to see if the buttons	**
//				and links should be disabled, then it submits	**
//				the form.										**
//																**
//****************************************************************
function submitForm(theForm) {

	if (DISABLE_ALL) {
		disableActions(theForm);
	}
	
	theForm.submit();
}

//****************************************************************
//																**
//  FUNCTION:	clickButtonOnEnter								**
//  PARAMS:		theForm   - form to submit						**
//				theEvent - event passed in the method since some *
//					browsers do not support a global window.event*
//  RETURNS:	none											**
//  PURPOSE:	This function will check to see if the buttons	**
//				and links should be disabled, then it submits	**
//				the form.										**
//																**
//****************************************************************
function clickButtonOnEnter(theButton, theEvent) {

			//alert("in clickButtonOnEnter; keyCode = " + theEvent.keyCode);
			// clicks the Button if the Enter Key was pressed
			if (theEvent.keyCode == 13) {
				theButton.click();
				theEvent.cancelBubble = true; 
				theEvent.returnValue = false;
			}
		}

//****************************************************************
//																**
//  FUNCTION:	disableActions									**
//  PARAMS:														**
//  RETURNS:	none											**
//  PURPOSE:	This function will disable all buttons and 		**
//				links.  This function typically is called 		**
//				before the form is submitted and when a link	**
//				is clicked.										**
//																**
//****************************************************************
function disableActions() {

	//loop through all the forms on the document
	for (i=0;i < document.forms.length;i++) {
		var theform = document.forms[i];

		//loop through all the elements on the form
		for (j = 0; j < theform.length; j++) {
			var tempobj = theform.elements[j];

			//if the element is a button, disable it
			if (tempobj.type != null) {
				if (tempobj.type.toLowerCase() == "button"  || tempobj.type.toLowerCase() == "submit" || tempobj.type.toLowerCase() == "reset") {
					tempobj.disabled = true;
				}
			}
		}
	}

	//loop through all the links on the document
	for (k=0;k < document.links.length;k++) {
		var templink = document.links[k];
		//disable the link
		templink.onclick = new Function("return false;");
	}

}
//****************************************************************
//																**
//  FUNCTION:	linkDisableActions								**
//  PARAMS:		thisLink - the link to execute					**
//  RETURNS:	none											**
//  PURPOSE:	This function will disable all buttons and		**
//				links on the form before executing the link.	**
//
//	This function doesn't do anything special right now but		**
//	may be useful if we find out later that there is something	**
//	special that needs to be done for a link.
//																**
//****************************************************************

function linkDisableActions(thisLink) {

	disableActions();

}

//**************** End New 03/18/2004 JKJ ************************




//****************************************************************
//
//  FUNCTION:	textAreaCount
//  PARAMS:		characterCount - The name of the element representing
//					the remaining character count.  Nullable.
//				theEvent - event passed in the method since some browser
//					do not support a global window.event
//  RETURNS:	void
//  PURPOSE:	This method is called from a text area's onkeyup event.
//				If the text area length is larger than its max length,
//				the text area is shortened to its max length.  If the
//				characterCount element exists, it is set to the
//				remaining number of characters available until the max
//				length of the text area is reached.  The remaining
//				number of characters displayed will never be less than
//				zero.
//
//****************************************************************
function textAreaCount(characterCount, theEvent) {
	var textAreaObject;
	
	if (IS_INTERNET_EXPLORER) {
		textAreaObject = theEvent.srcElement;
	} else {
		textAreaObject = theEvent.target;
	}

	var textAreaMaxLength = textAreaObject.getAttribute("maxlength");
	
	if (textAreaObject.value.length > textAreaMaxLength) {
		textAreaObject.value = 
			textAreaObject.value.substr(0, textAreaMaxLength);
	}

	if (characterCount) {
		var characterCountElement = document.getElementById(characterCount);
		if (textAreaObject.value.length > textAreaMaxLength) {
			characterCountElement.childNodes[0].nodeValue = "0";
		} else {
			characterCountElement.childNodes[0].nodeValue 
				= textAreaMaxLength 
				- textAreaObject.value.length;
		}
	}
}

//****************************************************************
//								**
//  FUNCTION:	formatNumberString				**
//  PARAMS:	number - number string to format		**
//		format - format layout				**
//  RETURNS:	string - formatted number			**
//  PURPOSE:	This function will format the passed in value 	**
//		based off the format given.  The function will	**
//		replace the 'X' in the format string, with the	**
//		next available number character.		**
//								**
//****************************************************************
function formatNumberString(number, format) {

  if (number.length < 1 || format < 1) {
    return number;
  }

  for (var i=0; i<format.length; i++) {

    var pos = format.indexOf("X");
    if (pos != -1) {
      format = format.replace("X", number.substr(i, 1));
    } else {
      format = format + number.substr(i);
    }

  }

  // remove any extra formatting chars
  var pos = format.indexOf("X");
  if (pos != -1) {
    format = format.substr(0, pos-1);
  }

  return format;

}

//****************************************************************
//		  	FED DATE JS OBJECT
//****************************************************************
//  The FedDate java script object implements a basic date
//  routine object.  This object will validate a date, will
//  break up a date into monthy, day, year, and format a date
//  string.
//
//  METHODS:
//  FedDate(dateString)      		constructor
//  FedDate(dateString, centuryCutoff)	constructor
//  FedDate(year, month, day)   	constructor
//  getDay()
//  getMonth()
//  getYear()
//  formatDate(format)
//  toString()
//
//  STATIC METHODS:
//  isValid(dateString)
//  isLeapYear(year)
//****************************************************************

function FedDate(dateString) {
  this.initialize(dateString, UC_CENTURY_CUTOFF_YEAR);
}

function FedDate(year, month, day) {

  if (arguments.length == 1) {
    this.initialize(year, UC_CENTURY_CUTOFF_YEAR);
    return;
  }

  if (arguments.length == 2) {
    this.initialize(year, month);
    return;
  }

  this.dayString = day;
  this.monthString = month;
  this.yearString = year;
  this.validDate = true;
}

FedDate.prototype.initialize = function(dateString, centuryCutoffYear) {

  this.strDate = dateString;
  this.centuryCutoffYear = centuryCutoffYear;

  // set if we have a valid date, if so set date values
  if (FedDate.isValid(dateString, this.centuryCutoffYear)) {
    this.validDate = true;
    var date = FedDate.stripDate(this.strDate, this.centuryCutoffYear);
    this.dayString = date.getDay();
    this.monthString = date.getMonth();
    this.yearString = date.getYear();
  } else {
    this.validDate = false;
    this.dayString = null;
    this.monthString = null;
    this.yearString = null;
  }

}

FedDate.prototype.toString = function() {

  if (this.validDate) {
    return this.monthString + "/" + this.dayString + "/" + this.yearString;
  } else {
    return null;
  }

}

FedDate.prototype.setCenturyCutoffYear = function(year) {
  this.centuryCutoffYear = year;
}

FedDate.prototype.getDay = function() {
  return this.dayString;
}

FedDate.prototype.getMonth = function() {
  return this.monthString;
}

FedDate.prototype.getYear = function() {
  return this.yearString;
}

FedDate.prototype.formatDate = function(format) {

  if (!this.validDate) {
    return null;
  }

  // replace month
  if (format.search(/mm/i) != -1) {
    format = format.replace(/mm/i, this.getFormattedMonth(false));
  } else if (format.search(/m/i) != -1) {
    format = format.replace(/m/i, this.getFormattedMonth(true));
  } else {
    return null;
  }

  // replace day
  if (format.search(/dd/i) != -1) {
    format = format.replace(/dd/i, this.getFormattedDay(false));
  } else if (format.search(/d/i) != -1) {
    format = format.replace(/d/i, this.getFormattedDay(true));
  } else {
    return null;
  }

  // replace year
  if (format.search(/yyyy/i) != -1) {
    format = format.replace(/yyyy/i, this.yearString);
  } else if (format.search(/yy/i) != -1) {
    format = format.replace(/yy/i, this.yearString.substr(2));
  } else {
    return null;
  }

  return format;

}

FedDate.prototype.getFormattedMonth = function(singleDigitMonth) {

  if (singleDigitMonth) {

    // check if we need to remove leading zero 
    if (this.monthString.length==2 && this.monthString.charAt(0)=='0') {
      return this.monthString.charAt(1);
    } else {
      return this.monthString;
    }

  } else {

    // check if we need to add leading zero
    if (this.monthString.length==1) {
      return "0" + this.monthString;
    } else {
      return this.monthString;
    }

  }

}

FedDate.prototype.getFormattedDay = function(singleDigitDay) {

  if (singleDigitDay) {

    // check if we need to remove leading zero 
    if (this.dayString.length==2 && this.dayString.charAt(0)=='0') {
      return this.dayString.charAt(1);
    } else {
      return this.dayString;
    }

  } else {

    // check if we need to add leading zero
    if (this.dayString.length==1) {
      return "0" + this.dayString;
    } else {
      return this.dayString;
    }

  }

}

FedDate.isValid = function(dateString, centuryCutoffYear) {

  // length must be greater than 5
  if (dateString.length < 6) {
    return false;
  }

  // default century cutoff year if not passed
  if (arguments.length < 2) {
    centuryCutoffYear = UC_CENTURY_CUTOFF_YEAR;
  }

  // strip date apart
  var date = FedDate.stripDate(dateString, centuryCutoffYear);
  if (date == null) {
    return false;
  }

  // validate the year
  var year = date.getYear();
  if ( year.length != 4 ||	// must be 4 chars
       isNaN(year)      ||      // must be numeric
       year < 1875 	|| 	// between 1875 & 2100
       year > 2100 ) {	
	return false;
  }

  // validate the month
  var month = date.getMonth();
  if ( isNaN(month) 	||	// must be numeric
       month < 1	||	// between 1 & 12
       month > 12 ) {
	return false;
  }

  // validate the day
  var day = date.getDay();
  if ( isNaN(day)	||	// must be numeric
       day < 1		||	// must be less then 31
       day > 31 ) {		
	return false;
  }

  // 30 day months
  if (month==4 || month==6 || month==9 || month==11) {
    if (day > 30) {
      return false;
    }
  }

  // check february
  if (month==2) {
    if (FedDate.isLeapYear(year)) {
      if (day > 29)  return false;
    } else {
      if (day > 28)  return false;
    }
  }

  return true;

}

FedDate.stripDate = function(dateValue, centuryCutoffYear) {

  var strSeparatorArray = new Array("/", "-", " ", ",", ".");

  // we have formatted date
  for (var i=0; i<strSeparatorArray.length; i++) {

    var strDateArray = dateValue.split(strSeparatorArray[i]);

    if (strDateArray.length == 3) {
      return FedDate.setDateFields(strDateArray, centuryCutoffYear);
    }

  }	// end for loop

  // we have non formatted date
  if (dateValue.length > 5) {
    var strDateArray = new Array(3);
    strDateArray[0] = dateValue.substr(0,2);
    strDateArray[1] = dateValue.substr(2,2);
    strDateArray[2] = dateValue.substr(4);
    return FedDate.setDateFields(strDateArray, centuryCutoffYear);
  }

  return null;

}

FedDate.setDateFields = function(strDateArray, centuryCutoffYear) {

  var strMonth = strDateArray[0];
  var strDay = strDateArray[1];
  var strYear = strDateArray[2];

  if (strYear.length == 2) {
    if (strYear > centuryCutoffYear) {
      strYear = "19" + strYear;
    } else {
      strYear = "20" + strYear;
    }
  }

  var date = new FedDate(strYear, strMonth, strDay);
  return date;

}

FedDate.isLeapYear = function(intYear) {

  if ((intYear % 100) == 0) {
    if ((intYear % 400) == 0)	return true;
  } else {
    if ((intYear % 4) == 0) 	return true;
  }

  return false;

}
