// Validation Routines

// Checks to see if a field is blank.
// Example: 	if (isBlank(obj.N_APPL_FRST_NAME)) 
function isBlank(fieldName) {
	re=/\w+/i;
	return ( (fieldName.value=="") || ( ! re.test(fieldName.value) ) );
}

// Checks to see if a particular select option is selected or if any option is selected.
// Example: 	if ( isSelected(obj.C_APPL_EMPLM_STAT,-1) ) 
// Note:			If no option in a select obj is selected -1 is returned.
//                  In the above case, if no option in C_APPL_EMPLM_STAT was selected the
//					expression evaluates to true.
function isSelected(field,whichSelection) {
	return (field.selectedIndex == whichSelection);
}

// Check to see if a checkbox is checked.
// Example: 	if ( isChecked(obj.F_INV_HIST_STK) )
function isChecked(fieldName) {
	return (fieldName.checked);
}


// Checks to see if any radio button has been selected or if a particular radio button has been selected.
// Example 1: Has a radio button been selected
// 	if (! isRadioChecked(obj.C_APPL_CITZN_STAT,-1) )
//			return reportError("indicate your Citizenship Status",obj.C_APPL_CITZN_STAT,0);
// Example 2: Has the third radio button been selected
// 	if ( isRadioChecked(obj.C_APPL_CITZN_STAT,2) &&  isBlank(obj.T_APPL_CITZN_CNTRY) ) {
//			return reportError("enter your Country of Citizenship",obj.T_APPL_CITZN_CNTRY,1);
function isRadioChecked(field,whichOne) {
	var i;
	if (whichOne >= 0) {
		returnValue = field[whichOne].checked;
	} else {
		returnValue = false;
		for(i=0; i < field.length; i++)
			returnValue = (returnValue || field[i].checked);
	}

	return returnValue;
}

// Check for a valid keypress in the current field.
// Example:  <input type="text" name="SSN" size="11" onKeyPress="return isValidChar(event,'ssn');">
function isValidChar(e,iType) {		
	var charCode = (browserType() == "netscape") ? e.which : e.keyCode;
	switch (iType) {
		case "ssn":		// Social Security: accepts 0-9 and - 
			if ( ((charCode > 47) && (charCode < 58)) || (charCode == 45) || (charCode==8))
				return true;
			else
				return false;
		case "phone":	// Phone: accepts 0-9 [48-57], ( [40], ) [41], - [45], space [32], backspace [8], and x [120]
			if ( ((charCode > 47) && (charCode < 58)) || 
				 (charCode==45) || 
				 (charCode==40 ) || 
				 (charCode==41) || 
				 (charCode==120) || 
				 (charCode==8) || 
				 (charCode==32) )
				return true;
			else
				return false;			
		case "dob":		// Date of Birth: accepts 0-9 [48-57], / [47], and backspace [8]
			if ( ((charCode > 47) && (charCode < 58)) || 
				 (charCode==47) || 
				 (charCode==8) )
				return true;
			else
				return false;
		case "num":		// Number: accepts 0-9 [48-57] and backspace [8]
			if ( ((charCode > 47) && (charCode < 58)) || 
				 (charCode==8) ) 
				return true;
			else
				return false;									
	}
				
}

// Checks for a valid email address.
// Example: 	if ( ! isValidEmail(obj.N_APPL_EMAIL_ADDR.value) )
//					return reportError("enter a valid E-mail Address\n(e.g. - jdoe@acompany.com)",obj.N_APPL_EMAIL_ADDR,1);
function isValidEmail(fld) {
	strEmail = new String(fld.value);
    if (strEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
        return true;
    else
        return false;
}

// Check for a valid U.S. business phone.  Similar to isValidPhoneHome, except that it allows for extensions.
// isValidPhoneBus(obj.I_APPL_BUS_PH_NUM)
function isValidPhoneBus(fld) {
	var string = fld.value;
    if (string.search(/^(\(?\d{3}\)?(\-?| ))?\d{3}(\-?| )\d{4}( x\d{1,5})?$/) != -1)
        return true;
    else
        return false;
}

// Check for a valid U.S. home phone.  Similar to isValidPhoneBus, except that extensions are not allowed.
// isValidPhoneHome(obj.I_APPL_HME_PH_NUM)
function isValidPhoneHome(fld) {
	var string = fld.value;
    if (string.search(/^(\(?\d{3}\)?(\-?| ))?\d{3}(\-?| )\d{4}$/) != -1)
        return true;
    else
        return false;
}

// Check for a simple phone number in the format ###-###-####.
// isValidPhoneSimple(frm.phone1)
function isValidPhoneSimple(fld) {
	var string = fld.value;
    if (string.search(/^\d{3}\-?\d{3}\-\d{4}$/) != -1)
        return true;
    else
        return false;
}

// Check for a valid U.S. zipcode.
// isValidZip(obj.N_APPL_EMPLR_ZIP)
function isValidZip(fld) {
	var string = fld.value;
    if (string.search(/^\d{5}(-\d{4})?$/) != -1)
        return true;
    else
        return false;
}

// Adjusts a year for Y2K if necessary.  Called by the isValidDate function.
function y2k(number) { 
	return (number < 1000) ? number + 1900 : number; 
}

// Checks if date passed is in valid mm/dd/yyyy format.
function isValidDate(myDate) {
	sep = '/'
	reason = 'valid';

	if (myDate.length == 10) {
        if (myDate.substring(2,3) == sep && myDate.substring(5,6) == sep) {
            var month  = myDate.substring(0,2);
            var date = myDate.substring(3,5);
            var year  = myDate.substring(6,10);

            var test = new Date(year,month-1,date);

			if ( (year.substring(0,2) != '19') && (year.substring(0,2) != '20') ) {
				reason = 'The date is not valid';
				return reason;
			}

            if (year == y2k(test.getYear()) && (month-1 == test.getMonth()) && (date == test.getDate())) {
                reason = 'valid';
                return reason;
            }
            else {
                reason = 'The format is valid, but the date is not';
                return reason;
            }
        }
        else {
            reason = 'You must use a / to separate date parts';
            return reason;
        }
    }
    else {
       	reason = 'Your date is the wrong length';
		return reason;
    }
}

// check for a valid social security number
// Example:	isValidSSN(obj.SSN.value)
// Note:	Dashes are optional
function isValidSSN(string) {
    if (string.search(/^[0-9][0-9][0-9]\-?[0-9][0-9]\-?[0-9][0-9][0-9][0-9]$/) != -1)
         return true;
     else
         return false;
}

// Used to report an error to the user.
// errTxt = The error message to be supplied to the user.
// errFld = The field where the error occurred. Used to return the user to the errant field.
// giveFocus = If set to true the focus will be set to the errant field.
// Example:	
//	if (! isValidPhoneBus(obj.I_APPL_BUS_PH_NUM,1) )
//		return reportError("enter a valid Business Phone number\n(e.g. - (123)456-7890 x12345 )",obj.I_APPL_BUS_PH_NUM,1);
// if this not a valid business phones number than diplay the message "Please enter a valid Business Phone...." and move to field.
function reportError(errTxt,errFld,giveFocus) {
	alert("Please "+errTxt+".");
	if (giveFocus) {
		errFld.focus();
	}
	return false;
}

