
//<script runat=server language="Javascript">

var sFieldName;
var err_gsErrorMsg;
var err_gbHasError;
var oForm;
var sTest;
var sLink;
var hWnd;  // handle used for opening the new window
var imgError_on = new Image(10,10);
var imgError_off = new Image(10,10);
imgError_on.src = "/images/ErrorIndicator.gif";
imgError_off.src = "/images/ErrorIndicator_off.gif";
errMessage= "There are errors in the form.  Please look for the red markers.";

function checkField(fieldName, checkType) {
	var bHasError = false;
	oForm = document.forms["main"];
	sFieldName = fieldName;
	err_gsErrorMsg = "";
	err_gbHasError = false;

	if (checkType == "") { }
	else if (checkType == "date") {
		sTest = "!isDateOrBlank(oForm." + sFieldName + ".value)";
		err_gsErrorMsg = eval(sTest) ? "Date must be a valid date. (MM/DD/YY) or (MM/DD/YYYY)" : "";
	}
	else if (checkType == "number") {
		sTest = "!isAllDigits(oForm." + sFieldName + ".value)";
		err_gsErrorMsg = eval(sTest) ? "Must be all numbers." : "";
	}
	else if (checkType == "decimal") {
		sTest = "!isDecimal(oForm." + sFieldName + ".value)";
		err_gsErrorMsg = eval(sTest) ? "Must be a valid decimal number." : "";
	}
	else if (checkType == "bath") {
		sTest = "!isDecimalLimit(oForm." + sFieldName + ".value)";
		err_gsErrorMsg = eval(sTest) ? "Must be a valid decimal number for bath." : "";
	}		
	else if (checkType == "case_number") {
		//sTest = "!isCaseNumber(oForm." + sFieldName + ".value)";
		//err_gsErrorMsg = eval(sTest) ? "Must be valid case number. (###-######)" : "";
	}
	else if (checkType == "fha_case_number") {
		sTest = "!isFHACaseNumber(oForm." + sFieldName + ".value)";
		err_gsErrorMsg = eval(sTest) ? "Must be valid case number. (###-######)" : "";
	}
	else if (checkType == "bank_case_number") {
		sTest = "!isBankCaseNumber(oForm." + sFieldName + ".value)";
		err_gsErrorMsg = eval(sTest) ? "Must be valid case number. (####-######)" : "";
	}	
	else if (checkType == "zip_code") {
		sTest = "!isZipCode(oForm." + sFieldName + ".value)";
		err_gsErrorMsg = eval(sTest) ? "Must be valid 5 digit zip code." : "";
	}
	else if (checkType == "ssn") {
		sTest = "!isSSN(oForm." + sFieldName + ".value)";
		err_gsErrorMsg = eval(sTest) ? "Must be valid social security number. (###-##-####)" : "";
	}
	else if (checkType == "money") {
		sTest = "!isMoney(oForm." + sFieldName + ".value)";
		err_gsErrorMsg = eval(sTest) ? "Must be a valid dollar amount." : "";
	}
		else if (checkType == "minThousand") {
		sTest = "!isMinThousand(oForm." + sFieldName + ".value)";
		err_gsErrorMsg = eval(sTest) ? "Must be a valid dollar amount above $999" : "";
	}
	else if (checkType == "ein_or_ssn") {
		sTest = "!isSSNorEIN(oForm." + sFieldName + ".value)";
		err_gsErrorMsg = eval(sTest) ? "Must be valid SSN/EIN. (###-##-#### or ##-#######)" : "";
	} 
	else if (checkType == "name") {
		sTest = "!isValidName(oForm." + sFieldName + ".value)";
		err_gsErrorMsg = eval(sTest) ? "Please enter a valid name starting with a character from A to Z." : "";
	}
	else if (checkType == "dining_room") {
		sTest = "!isDiningRoom(oForm." + sFieldName + ".value)";
		err_gsErrorMsg = eval(sTest) ? "Must be valid dining room. (#) or (AREA) " : "";
	}	
	else if (checkType == "email") {	    
		sTest = "!checkEMailAddress(oForm." + sFieldName + ".value)";		
		err_gsErrorMsg = eval(sTest) ? "Must be a valid email address." : "";
	}
	if(document.main.js_changed != undefined) 
		document.main.js_changed.value = "true";
	js_errSet(document.images, "err" + sFieldName);
	//setErrorMarkerFields(document.images);

	bHasError = err_gbHasError;

	return(!bHasError);
}

function isFutureMonthYear(fieldNameM, fieldNameY, errorField){
	var bHasError = false;
	oForm = document.forms["main"];
	err_gsErrorMsg = "";
	err_gbHasError = false;

	sTest = "!isAllDigits(oForm." + fieldNameM + ".value)";
	err_gsErrorMsg = eval(sTest) ? "Must be a valid month. " : "";
	if (err_gsErrorMsg == "") {
		sTest = "!isAllDigits(oForm." + fieldNameY + ".value)";
		err_gsErrorMsg = eval(sTest) ? "Must be a valid year. " : "";
	}
	if (err_gsErrorMsg == "") {
		if (eval("!(oForm." + fieldNameM + ".value.length == 0 && oForm."+fieldNameY+".value.length == 0)")){
			var sValue = eval("oForm." + fieldNameM + ".value") + "/01/" + eval("oForm." + fieldNameY + ".value");
			var dateArray = new Array();
			var bIsValid = isDate(sValue, null, dateArray);
			if(bIsValid){
				var today = new Date().getTime();
				var iYear = dateArray[0];
				var iMonth = dateArray[1]; 
				var iDay = dateArray[2];
				var dateToCheck = new Date();
				dateToCheck.setYear(iYear);
				dateToCheck.setMonth(iMonth);
				dateToCheck.setDate(iDay);
				var checkDate = dateToCheck.getTime();
				if(	checkDate >= today){
					bIsValid = true;
				} else {
					bIsValid = false;
				}		
			}
			err_gsErrorMsg = (!bIsValid) ? "Must be valid future month and year" : "";
		}
	} 
//	document.main.js_changed.value = "true";

//	js_errSet(document.images, "err" + errorField);
//		setErrorMarkerFields(document.images);
//	bHasError = err_gbHasError;
//	return(!bHasError);		


if(document.main.js_changed != undefined) 
		document.main.js_changed.value = "true";
	js_errSet(document.images, "err" + errorField);
	js_errSet(document.images, "err" + fieldNameY);
	//setErrorMarkerFields(document.images);

	bHasError = err_gbHasError;

	return(!bHasError);
}

function checkPhoneExtension(fieldName,  errorField) {

	var bHasError = false;
	oForm = document.forms["main"];
	err_gsErrorMsg = "";
	err_gbHasError = false;		
	
		sTest = "!isAllDigits(oForm." + fieldName + ".value)";	
		err_gsErrorMsg = eval(sTest) ? "Must be valid phone number extension with only Numeric values" : "";	
			
	document.main.js_changed.value = "true";

	js_errSet(document.images, "err" + errorField);	

	bHasError = err_gbHasError;
		
	return(!bHasError);
}

function checkMoneyField(fieldName1, fieldName2, errorField){
	var bHasError = false;
	oForm = document.forms["main"];
	err_gsErrorMsg = "";
	err_gbHasError = false;
	
	sTest = "!isMoney(oForm." + fieldName1 + ".value)";
	err_gsErrorMsg = eval(sTest) ? "Must be a valid dollar amount." : "";
	
	if (err_gsErrorMsg == "") {
		sTest = "!isMoney(oForm." + fieldName2 + ".value)";
		err_gsErrorMsg = eval(sTest) ? "Must be a valid dollar amount." : "";
	}
	
	document.main.js_changed.value = "true";

	js_errSet(document.images, "err" + errorField);

	bHasError = err_gbHasError;
	return(!bHasError);
}

function isValidName( sValue ) {

	fieldMask = eval("/^[a-z]{1}[a-z., \'\-]*$|^$/i");

	// Test the field value against the mask
	return (fieldMask.test(sValue)) 
}
function checkPhone(fieldName1, fieldName2, fieldName3, errorField) {
	var bHasError = false;
	oForm = document.forms["main"];
	err_gsErrorMsg = "";
	err_gbHasError = false;

	sTest = "!isAllDigits(oForm." + fieldName1 + ".value)";
	err_gsErrorMsg = eval(sTest) ? "Must be valid phone number. (###-###-####)" : "";

	if (err_gsErrorMsg == "") {
		sTest = "!isAllDigits(oForm." + fieldName2 + ".value)";
		err_gsErrorMsg = eval(sTest) ? "Must be valid phone number. (###-###-####)" : "";
	}

	if (err_gsErrorMsg == "") {
		sTest = "!isAllDigits(oForm." + fieldName3 + ".value)";
		err_gsErrorMsg = eval(sTest) ? "Must be valid phone number. (###-###-####)" : "";
	}

	if (err_gsErrorMsg == "") {
		if (eval("!(oForm." + fieldName1 + ".value.length == 0 && oForm."+fieldName2+".value.length == 0 && oForm."+fieldName3+".value.length == 0)")){
			sTest = "!(oForm." + fieldName1 + ".value.length == 3 && oForm."+fieldName2+".value.length == 3 && oForm."+fieldName3+".value.length == 4)";
			err_gsErrorMsg = eval(sTest) ? "Must be valid phone number. (###-###-####)" : "";
		}
	}

	document.main.js_changed.value = "true";

	js_errSet(document.images, "err" + errorField);
	//setErrorMarkerFields(document.images);

	bHasError = err_gbHasError;

	return(!bHasError);
}

// The following function takes a decimal value and checks if the value has more than the 
// the specified number of digits after the decimal.
function checkValidDecimal(sFieldName,digits,errorMsg){
	var bHasError = false;
	oForm = document.forms["main"];
	err_gsErrorMsg = "";
	err_gbHasError = false;
	
  sTest = "!isDecimal(oForm." + sFieldName + ".value)";
	err_gsErrorMsg = eval(sTest) ? "Must be a valid decimal number." : "";

  if(err_gsErrorMsg == ""){	
    var dValue = eval("oForm." + sFieldName + ".value");
    if (dValue == null) {
			dValue = '';
		}
		dValue = dValue.toString();
		dValue = formatTrim(dValue);
		iDecimal = dValue.indexOf('.');
		if(iDecimal!=-1){
			if(dValue.substring(iDecimal,dValue.length).length>digits+1){
				err_gsErrorMsg = errorMsg;
			}
		}
	}

	document.main.js_changed.value = "true";

	js_errSet(document.images, "err" + sFieldName);
	//setErrorMarkerFields(document.images);

	bHasError = err_gbHasError;

	return(!bHasError);
}

function checkSSN(fieldName1, fieldName2, fieldName3, errorField) {
	var bHasError = false;
	oForm = document.forms["main"];
	err_gsErrorMsg = "";
	err_gbHasError = false;

	sTest = "!isAllDigits(oForm." + fieldName1 + ".value)";
	err_gsErrorMsg = eval(sTest) ? "Must be valid social security number. (###-##-####)" : "";

	if (err_gsErrorMsg == "") {
		sTest = "!isAllDigits(oForm." + fieldName2 + ".value)";
		err_gsErrorMsg = eval(sTest) ? "Must be valid social security number. (###-##-####)" : "";
	}

	if (err_gsErrorMsg == "") {
		sTest = "!isAllDigits(oForm." + fieldName3 + ".value)";
		err_gsErrorMsg = eval(sTest) ? "Must be valid social security number. (###-##-####)" : "";
	}

	if (err_gsErrorMsg == "") {
		if (eval("!(oForm." + fieldName1 + ".value.length == 0 && oForm."+fieldName2+".value.length == 0 && oForm."+fieldName3+".value.length == 0)")){
			sTest = "!(oForm." + fieldName1 + ".value.length == 3 && oForm."+fieldName2+".value.length == 2 && oForm."+fieldName3+".value.length == 4)";
			err_gsErrorMsg = eval(sTest) ? "Must be valid social security number. (###-##-####)" : "";
		}
	}

	document.main.js_changed.value = "true";

	js_errSet(document.images, "err" + errorField);
	//setErrorMarkerFields(document.images);

	bHasError = err_gbHasError;

	return(!bHasError);
}

function checkLenderCaseNumber(fieldName1, fieldName2, errorField) {
	var bHasError = false;
	oForm = document.forms["main"];
	err_gsErrorMsg = "";
	err_gbHasError = false;

	sTest = "!isAllDigits(oForm." + fieldName1 + ".value)";
	err_gsErrorMsg = eval(sTest) ? "Must be valid case number. (###-#######)" : "";

	if (err_gsErrorMsg == "") {
		sTest = "!isAllDigits(oForm." + fieldName2 + ".value)";
		err_gsErrorMsg = eval(sTest) ? "Must be valid case number. (###-#######)" : "";
	}

	if (err_gsErrorMsg == "") {
		if (eval("!(oForm." + fieldName1 + ".value.length == 0 && oForm."+fieldName2+".value.length == 0)")){
			sTest = "!(oForm." + fieldName1 + ".value.length == 3 && oForm."+fieldName2+".value.length > 5)";
			err_gsErrorMsg = eval(sTest) ? "Must be valid case number. (###-#######)" : "";
		}
	}

	document.main.js_changed.value = "true";

	js_errSet(document.images, "err" + errorField);
	//setErrorMarkerFields(document.images);

	bHasError = err_gbHasError;

	return(!bHasError);
}

function checkTaxId(fieldName1, fieldName2, errorField) {
	var bHasError = false;
	oForm = document.forms["main"];
	err_gsErrorMsg = "";
	err_gbHasError = false;

	sTest = "!isAllDigits(oForm." + fieldName1 + ".value)";
	err_gsErrorMsg = eval(sTest) ? "Must be valid tax id number. (##-#######)" : "";

	if (err_gsErrorMsg == "") {
		sTest = "!isAllDigits(oForm." + fieldName2 + ".value)";
		err_gsErrorMsg = eval(sTest) ? "Must be valid tax id number. (##-#######)" : "";
	}

	if (err_gsErrorMsg == "") {
		if (eval("!(oForm." + fieldName1 + ".value.length == 0 && oForm."+fieldName2+".value.length == 0)")){
			sTest = "!(oForm." + fieldName1 + ".value.length == 2 && oForm."+fieldName2+".value.length == 7)";
			err_gsErrorMsg = eval(sTest) ? "Must be valid tax id number. (##-#######)" : "";
		}
	}

	document.main.js_changed.value = "true";

	js_errSet(document.images, "err" + errorField);
	//setErrorMarkerFields(document.images);

	bHasError = err_gbHasError;

	return(!bHasError);
}



///////////////////////////////////////////////////////////////////

// VALIDATION SUPPORTING FUNCTIONS

//////////////////////////////////////////////////////////////////

function isMandatoryEmpty(fieldName,type,size) {
	//alert(fieldName);
	var frm = document.forms["main"];
	var contrl = frm.elements[fieldName];
	if(type=='select'){
			sValue = eval("contrl.options[contrl.selectedIndex].value");
	} else if(type=='radio'){
		var isChkd = true;
		for(var i=0; i<size; i++){
			isChkd = eval("document.main."+fieldName+"["+i+"].checked");
			if(isChkd){break;}
		}
		if(!isChkd){
			return true;
		}else{
			sValue='ischecked';
		}
	} else {
		sValue = eval("document.main." + fieldName + ".value");
	}
	if (sValue == null || sValue.length == 0) {	return true; }
	return false;
}

function isErrorPresentInForm() {

	var elems = document.forms["main"].elements;
	var errorPresent = false;
	
	for ( var i = 0; i < elems.length; i = (i + 1) ) {
		errorPresent = isErrorPresent( elems [i].name );
		if ( errorPresent == true ) {
			return ( errorPresent );
		}
	} 
	
	return errorPresent;
}

function isErrorPresent(fieldName) {
	if (eval("document.images.err" + fieldName) ==null || eval("typeof(document.images.err" + fieldName+")") =="undefined") {return false;}

	sValue = eval("document.images.err" + fieldName + ".alt");

	if ( typeof(sValue) == "undefined" || typeof(sValue.length) == "undefined" ||  sValue.length == null) {	return false; }
	else if (sValue.length == 0 ) {return false; }

	return true;
}

function isDateOrBlank(sValue) {
var bIsValid;
	if (typeof(sValue) != "string") { return false;}
	if (sValue.length == 0) {
		bIsValid = true;
	} else {
		bIsValid = isDate(sValue)
	}
	return(bIsValid);
}

function isDate(sValue,FieldName, dateArray){
	var sTemp;
	var bIsValid;
	var sFieldName = FieldName;
	var iCenturyBreak;
	oForm = document.forms["main"];

	if (typeof(sValue) != "string") { return false;}
	iCenturyBreak = 20;

	//Convert dashes and periods to slashes so that delimiters are standardized.
	while (sValue.indexOf("-") != -1) {
				sValue = sValue.replace("-", "/");
	}
	while (sValue.indexOf(".") != -1) {
				sValue = sValue.replace(".", "/");
	}

	//Check to see if date is 6 or 8 digits.  If so, assume mmddyy or mmddyyyy and convert to mm/dd/yy[yy]
	if (sValue.length == 8 || sValue.length == 6) {
				if (isAllDigits(sValue)) {
							var sTemp = sValue.substring(0,2);
							sTemp = sTemp.concat("/");
							sTemp = sTemp.concat(sValue.substring(2,4));
							sTemp = sTemp.concat("/");
							sTemp = sTemp.concat(sValue.substring(4));
							sValue = sTemp;
				}
	}
	//Date must be a minimum of 6 characters (#/#/##) and a maximum of 10 characters (##/##/####).
	//Start with this validation.
	var bIsValid = (sValue.length >= 6 && sValue.length <= 10);
	//Check for exactly two delimiters.  Ensure that the first is not the first character
	//and the second is not before the forth character
	if (bIsValid) {
				var iFirstDelim = sValue.indexOf("/");
				var iSecondDelim = sValue.indexOf("/", iFirstDelim + 1);
				var iThirdDelim = sValue.indexOf("/", iSecondDelim + 1);
				bIsValid = (iFirstDelim > 0 && iSecondDelim > 2 && iThirdDelim == -1);
	}
	//if exactly two delimiters are found, get the Month, Day, and Year from the string.
	//Check to make sure that the Month, Day, and Year are Numbers.  Ensure that Month is
	//in 1-12 and day is in 1-31
	if (bIsValid) {
				var iMonth = parseInt(sValue.substring(0,iFirstDelim),10);
				var iDay = parseInt(sValue.substring(iFirstDelim+1,iSecondDelim),10);
				var iYear = parseInt(sValue.substring(iSecondDelim+1,sValue.length),10);
				
				if ( !isAllDigits(sValue.substring(0,iFirstDelim),10) 
					|| !isAllDigits(sValue.substring(iFirstDelim+1,iSecondDelim),10)
					|| !isAllDigits(sValue.substring(iSecondDelim+1,sValue.length),10) ) {
					bIsValid = false;
				}
				
				//bIsValid = (!isNaN(iMonth) || !isNaN(iDay) || !isNaN(iYear));
				if (bIsValid) {
							bIsValid = (iMonth>=1 && iMonth<=12 && iDay>=1 && iDay<=31);
				}
	}
	//If year is between 0 and 99 (inclusive), convert year to 4 digits.  Check that the
	//year falls into the specified allowable range of 1700 - 2300, inclusive
	if (bIsValid) {
				if (iYear >= 0 && iYear <= 99) {
							if (iYear < iCenturyBreak) {
										iYear = iYear + 2000;
							} else {
										iYear = iYear + 1900;
							}
				}
				bIsValid = (iYear >= 1700 && iYear <= 2300);
	}

	if (bIsValid) {
				var iNumDays;
				if (iMonth==1 || iMonth==3 || iMonth==5 || iMonth==7 || iMonth==8 || iMonth==10 || iMonth==12) {
							iNumDays = 31;
				} else if (iMonth==4 || iMonth==6 || iMonth==9 || iMonth==11) {
							iNumDays = 30;
				} else {    // iMonth=2, check for leap year.
							if (iYear % 4 != 0) {
										iNumDays = 28;
							} else if (iYear % 400 == 0) {
										iNumDays = 29;
							} else if (iYear % 100 == 0) {
										iNumDays = 28;
							} else {
										iNumDays = 29;
							}
				}
				bIsValid = (iDay <= iNumDays);
	}
	if (bIsValid && sFieldName != null) { 
	oForm[sFieldName].value = (iMonth + "/" + iDay + "/" + iYear);
	}
	if (dateArray != undefined ) {
			dateArray[0] = iYear
			dateArray[1] = iMonth
			dateArray[2] = iDay
		}
	return(bIsValid);
}

function isFHACaseNumber(sValue) {
var sSub;
var bReturn;
	if (sValue.length == 0) { bReturn = true;}	
	else if (sValue.length == 9 && isAllDigits(sValue)) {
		bReturn = true;
	}	
	else if (sValue.length == 10) {
		bReturn = isAllDigits(sValue.substring(0,3));
		bReturn = bReturn && (sValue.charAt(3)=="-");
		bReturn = bReturn && isAllDigits(sValue.substring(4,10));
	}
	else { bReturn = false; }	
    
	return(bReturn);
}


function isBankCaseNumber(sValue) {
	var sSub;
	var bReturn;
	if (sValue.length == 0) { bReturn = true;}	
	else if (sValue.length == 11) {
		bReturn = isAllDigits(sValue.substring(0,4));
		bReturn = bReturn && (sValue.charAt(4)=="-");
		bReturn = bReturn && isAllDigits(sValue.substring(5,11));
	}
	else { bReturn = false; }	
    
	return(bReturn);
}

function isAllDigits(sValue) {
	var sAllowedChars = "0123456789"
	var bIsValid = true;
	var iIndex;
	for (i = 0;  i < sValue.length;  i++) {
		searchChar = sValue.charAt(i);
		iIndex = sAllowedChars.indexOf(searchChar);
		bIsValid = bIsValid && (iIndex != -1);
	}
	return(bIsValid)
}

function isZipCode(sValue) {
var sSub;
var bReturn;
	bReturn = true;
	if (sValue.length == 0) { }
	else if (sValue.length == 5) {
		bReturn = (isAllDigits(sValue));
	} else {
		bReturn = (sValue.length == 10);
		if (bReturn) {
			sSub = sValue.substring(0,5)
			bReturn = isAllDigits(sSub);
		}
		if (bReturn) {
			bReturn = (sValue.charAt(5)=="-");
		}
		if (bReturn) {
			sSub = sValue.substring(6)
			bReturn = isAllDigits(sSub);
		}
	}
	return(bReturn);
}

function isSSN(sValue) {
	var sSub;
	var bReturn;
	bReturn = true;
	if (sValue.length == 9) {
		bReturn = (isAllDigits(sValue));
	} else {
		bReturn = (sValue.length == 11);
		if (bReturn) {
			sSub = sValue.substring(0,3)
			bReturn = isAllDigits(sSub);
		}
		if (bReturn) {
			bReturn = (sValue.charAt(3)=="-");
		}
		if (bReturn) {
			sSub = sValue.substring(4,6)
			bReturn = isAllDigits(sSub);
		}
		if (bReturn) {
			bReturn = (sValue.charAt(6)=="-");
		}
		if (bReturn) {
			sSub = sValue.substring(7)
			bReturn = isAllDigits(sSub);
		}
	}
	return(bReturn);
}

function isDiningRoom(sValue) {
	var sSub;
	var bReturn;
	bReturn = true;
	if (sValue.length > 0) {
		if (isAllDigits(sValue)){
			return bReturn;
		} else {
			bReturn = (sValue.length == 4);
			if(bReturn){
				bReturn = (sValue.toUpperCase() == "AREA");
			}
		}
	} 
	return(bReturn);
}

function isEmail(sValue) {
	if (sValue.length > 0 && sValue.indexOf('.') > 0 && sValue.indexOf('@') > 0){
		return true;
	} 
	return(false);
}

function isSSNorEIN(sValue) {
	var sSub;
	var bReturn;
	bReturn = true;
  if (sValue == "" || sValue == null) {
    
  }
	else if (sValue.length == 10) {
		if (bReturn) {
			sSub = sValue.substring(0,2)
			bReturn = isAllDigits(sSub);
		}
		if (bReturn) {
			bReturn = (sValue.charAt(2)=="-");
		}
		if (bReturn) {
			sSub = sValue.substring(3,7)
			bReturn = isAllDigits(sSub);
		}
	} else { //check SSN
		bReturn = (sValue.length == 11);
		if (bReturn) {
			sSub = sValue.substring(0,3)
			bReturn = isAllDigits(sSub);
		}
		if (bReturn) {
			bReturn = (sValue.charAt(3)=="-");
		}
		if (bReturn) {
			sSub = sValue.substring(4,6)
			bReturn = isAllDigits(sSub);
		}
		if (bReturn) {
			bReturn = (sValue.charAt(6)=="-");
		}
		if (bReturn) {
			sSub = sValue.substring(7)
			bReturn = isAllDigits(sSub);
		}
	}
	return(bReturn);
}

function isDecimal(sValue) {
	var bReturn = false;
	if (sValue == null) {
		sValue = '';
	}
	sValue = sValue.toString();
	sValue = formatTrim(sValue);

	//Check for exactly one decimal point
	iDecimal = sValue.indexOf('.');
	bReturn = (iDecimal == sValue.lastIndexOf('.'));

	if (bReturn) {
		if (iDecimal == 0) {
			sValue = '0' + sValue;
		} else if (iDecimal == -1) {
			sValue = sValue + '.00';
		} else if (iDecimal == (sValue.length-1)) {
			sValue = sValue + '00';
		} else if (iDecimal == (sValue.length-2)) {
			sValue = sValue + '0';
		}
		iDecimal = sValue.indexOf('.');
	}

	//If first character is plus or minus, remove it.
	if (bReturn) {
		if ((sValue.charAt(0) == '+') || (sValue.charAt(0) == '-')) {
			sValue = sValue.substring(1);
			iDecimal = sValue.indexOf('.');
		}
	}

	//check that all characters to the right of the decimal are numeric
	if (bReturn) {
		bReturn = isAllDigits(sValue.substring(iDecimal+1));
	}

	//Remove all commas to the left of the decimal and check to see if the remaining
	//characters are digits.
	if (bReturn) {
		sLeft = sValue.substring(0, iDecimal)
		sLeft = sLeft.replace(',','');
		bReturn = isAllDigits(sLeft);
	}
	return bReturn;
}

function isDecimalLimit(sValue) {
	var bReturn = false;
	if (sValue == null) {
		sValue = '';
	}
	sValue = sValue.toString();
	sValue = formatTrim(sValue);

	//Check for exactly one decimal point
	iDecimal = sValue.indexOf('.');
	
	bReturn = (iDecimal == sValue.lastIndexOf('.'));
	
	if (bReturn) {
		if (iDecimal == 0) {
			sValue = '0' + sValue;
		} else if (iDecimal == -1) {
			sValue = sValue + '.0';
		} else if (iDecimal == (sValue.length-1)) {
			sValue = sValue + '0';
		} 
		iDecimal = sValue.indexOf('.');
	}

	//If first character is plus or minus, remove it.
	if (bReturn) {
		if ((sValue.charAt(0) == '+') || (sValue.charAt(0) == '-')) {
			sValue = sValue.substring(1);
			iDecimal = sValue.indexOf('.');
		}
	}

	//check that all characters to the right of the decimal are numeric
	if (bReturn) {
		bReturn = isAllDigits(sValue.substring(iDecimal+1));
	}

	//Remove all commas to the left of the decimal and check to see if the remaining
	//characters are digits.
	if (bReturn) {
		sLeft = sValue.substring(0, iDecimal)
		sLeft = sLeft.replace(',','');
		bReturn = isAllDigits(sLeft);
	}
	
	// check for only one digit after the decimal
	
	if (bReturn) {
		bReturn = ( sValue.substring(iDecimal+1).length<=2 );
	}
	return bReturn;
}

function isMoney(sValue) {
	var bReturn = false;
	if (sValue == null) {
		sValue = '';
	}
	sValue = sValue.toString();
	sValue = formatTrim(sValue);

	//Check for exactly one decimal point
	iDecimal = sValue.indexOf('.');
	bReturn = (iDecimal == sValue.lastIndexOf('.'));

	if (bReturn) {
		if (iDecimal == 0) {
			sValue = '0' + sValue;
		} else if (iDecimal == -1) {
			sValue = sValue + '.00';
		} else if (iDecimal == (sValue.length-1)) {
			sValue = sValue + '00';
		} else if (iDecimal == (sValue.length-2)) {
			sValue = sValue + '0';
		}
		iDecimal = sValue.indexOf('.');
	}

	//If first character is plus or minus, remove it.
	if (bReturn) {
		if ((sValue.charAt(0) == '+') || (sValue.charAt(0) == '-')) {
			sValue = sValue.substring(1);
			iDecimal = sValue.indexOf('.');
		}
	}

	//check that all characters to the right of the decimal are numeric
	if (bReturn) {
		bReturn = isAllDigits(sValue.substring(iDecimal+1));
	}

	//Remove all commas to the left of the decimal and check to see if the remaining
	//characters are digits.
	if (bReturn) {
		sLeft = sValue.substring(0, iDecimal)
		sLeft = sLeft.replace(',','');
		bReturn = isAllDigits(sLeft);
	}
	return bReturn;
}

function isMinThousand(sValue) {
   var bReturn = false;
   
   bReturn = isMoney(sValue);

if (bReturn && sValue > 999 )
    bReturn = true;
else
    bReturn=false;
       
    return (bReturn) 
}

function isMinOne(sValue) {
	var bReturn = false;
	sValue = ReplaceAll(sValue,',', '');
	sValue = sValue.replace('.', '');
	bReturn = isAllDigits(sValue);
	if (bReturn && sValue > 0) {
		bReturn = true;
	}
	else {
		bReturn = false;
	}
	return (bReturn)
}

function ReplaceAll(source, stringToFind, stringToReplace) {
	var temp = source;
	var index = temp.indexOf(stringToFind);
	while (index != -1) {
		temp = temp.replace(stringToFind, stringToReplace);
		index = temp.indexOf(stringToFind);
	}
	return temp;
}

function isValidState(sValue) {
	var sAllowedChars = "AL;AK;AZ;AR;CA;CO;CT;DC;DE;FL;GA;HI;ID;IL;IN;IA;KS;KY;LA;ME;MD;MA;MI;MN;MS;MO;MT;NE;NV;NH;NJ;NM;NY;NC;ND;OH;OK;OR;PA;PR;RI;SC;SD;TN;TX;UT;VT;VA;VI;WA;WV;WI;WY"
	var bIsValid = true;
	var iIndex;
	iIndex = sAllowedChars.search(sValue);
	bIsValid = bIsValid && (sValue.length==2) && (iIndex != -1);
	return(bIsValid)
}
function isCasePrefix(sValue) {
	var bCIsValid = true;
	var iCIndex;
	bCIsValid = bCIsValid && (sValue.length==3) && isAllDigits(sValue);
	return(bCIsValid)
}
function isCityState(sValue) {
	var bCIsValid = true;
	var iCIndex;
	iCIndex = sValue.search(',');
	bCIsValid = bCIsValid && (iCIndex != -1);
	if (bCIsValid) {
		bCIsValid = bCIsValid && isValidState(sValue.slice(iCIndex+1));
	}
	return(bCIsValid)
}
///////////////////////////////////////////////////////////////////
// COMMON FUNCTIONS FOR VALIDATION
//////////////////////////////////////////////////////////////////
function alertErr(sFieldName) {
	var sValue = document.images[sFieldName].alt;
	if ((typeof(sValue) != "undefined") && (sValue != "undefined") && (sValue != "")) {
		alert(sValue);
	}
}

function js_errSet(oErrors, sName, bClearErrorAfterSet) {
	if (oErrors[sName] != null) {
			oErrors[sName].alt = err_gsErrorMsg;
	}
	if (err_gsErrorMsg != "") {
		err_gbHasError = true;
		if (oErrors[sName] != null) {oErrors[sName].src = imgError_on.src;}
	}
	else {
		if (oErrors[sName] != null) {oErrors[sName].src = imgError_off.src;}
	}
	if ((typeof(bClearErrorAfterSet) != "boolean")  || (bClearErrorAfterSet == true)) {
		err_gsErrorMsg = "";
	}
	
}

//No longer used
function setErrorMarkerFields(oImages) {
	var iIndex;
	var sName;
	var sError;
	for (iIndex=0;iIndex<oImages.length;iIndex++) {
		sName = oImages[iIndex].name;
		if (sName.substring(0,3)=="err") {
			sError = oImages[iIndex].alt;
			if ((typeof(sError) == "undefined") || (sError == "")) {
				oImages[iIndex].src = imgError_off.src;
			} else {
				oImages[iIndex].src = imgError_on.src;

			}
		}
	}
}

function submitFromQS(sAction) {
 	if ((document.main.js_changed.value == 'true') && (confirm ("   Save Form Changes?\n\n    'OK' to save changes.\n'Cancel' to discard changes.")) ) {
		if (document.main.p_quick_search_value != undefined) {
			document.main.p_quick_search_value.value = "";
		}
 		formAction("SAVE");
 	} else {
		document.main.submit();
	}
}

function formAction(sAction,mandAlertMessage) {
	// The following variable is overriden if Form submit does not happen in specific instances
	// By default, it's set to true
	//alert("Inside FormAction: " + sAction);
	var submitForm = true;
	if (document.main.p_quick_search_value != undefined) {
			document.main.p_quick_search_value.value = "";
	}
	
	if (sAction =='SAVE') {
		//remove the quicksearch if submitting the form.
		if (document.main.p_quick_case_number != undefined) {
			document.main.p_quick_case_number.value = "";
		}
		
		var mandatory = false;
		
		if ( window.checkMandatoryFieldsEmpty ) {
			mandatory = checkMandatoryFieldsEmpty();
		}
				
		if ( mandatory == true) {
			submitForm = false;
			if(mandAlertMessage == undefined) {
				alert ("All mandatory fields must be completed."); // LEAVE
			} else {
				alert(mandAlertMessage); // LEAVE
			}
		} else if( mandatory == 'custommsg' ) {
			submitForm = false;
		} else {
	  	
	  	var fieldErrors = false;
	  	
	  	if ( window.checkFieldErrors ) {
				fieldErrors = checkFieldErrors();
			}
	  	
			if (fieldErrors == true ) {
				submitForm = false;
     		alert (errMessage); // Leave
    	} else if (fieldErrors == "FORMERROR") { 
				submitForm = false;
    	} else {
                
				document.main.p_form_action.value = 'SAVE';
      	if(  document.main.p_action != null && document.main.p_action != undefined
           && document.main.p_action.value != undefined
           && document.main.p_action.value.length>0  
           && document.main.p_action.value != 'NEXT'
           && document.main.p_action.value != 'PREV' ) {
           
        	  document.main.p_action.value = 'SAVE';
       	}  
        
				document.main.submit();
			}
		}
	}
	else if (sAction =='NEW') {
		//remove the quicksearch if submitting the form.
		if (document.main.p_quick_case_number != undefined) {
			document.main.p_quick_case_number.value = "";
		}
		
		if (document.main.js_changed.value == 'true') {
			if (confirm ("Save Changes First?")) {
				submitForm = false;
				formAction('SAVE');
			}
			else {
				document.main.p_form_action.value = 'NEW';
				document.main.submit();
			}
		}
		else {
			document.main.p_form_action.value = 'NEW';
			document.main.submit();
		}
	}
	else if (sAction == 'CANCEL'){
		//remove the quicksearch if submitting the form.
		if (document.main.p_quick_case_number != undefined) {
			document.main.p_quick_case_number.value = "";
		}
		document.main.p_form_action.value = 'CANCEL';
          if(  document.main.p_action != null && document.main.p_action != undefined
                     && document.main.p_action.value != undefined
                     && document.main.p_action.value.length>0  
                     && document.main.p_action.value != 'NEXT'
                     && document.main.p_action.value != 'PREV' ) {
                    document.main.p_action.value = 'CANCEL';
        }
        document.main.submit();
     }
	else if (sAction == 'CLEAR'){
		document.main.p_form_action.value = 'CLEAR';
		document.main.submit();
	}
	else if (sAction =='OK') {
		if (checkMandatoryFieldsEmpty() == true) {
			submitForm = false;
	     	alert ("All mandatory fields must be completed.");
	  	}
		else {
			document.main.p_form_action.value = 'OK';
			document.main.submit();
		}
	}
	else if (sAction == "APPROVE") {
		if (checkMandatoryFieldsEmpty() == true) {
			submitForm = false;
	     	alert ("All mandatory fields must be completed.");
	  	}
		else {
				if (document.main.js_changed.value == 'true') {
					if (confirm ("Save Changes First?")) {
						submitForm = false;
						formAction('SAVE');
					}
				}	
				else {
					document.main.p_form_action.value = 'APPROVE';
                      if(  document.main.p_action != null && document.main.p_action != undefined
                     && document.main.p_action.value != undefined
                     && document.main.p_action.value.length>0  
                     && document.main.p_action.value != 'NEXT'
                     && document.main.p_action.value != 'PREV' ) {
                    document.main.p_action.value = 'APPROVE';
                    }   
					document.main.submit();
				}			
		}
	}
	else {
		if ( document.main.js_changed != undefined && document.main.js_changed.value == 'true') {
			if (confirm ("Save Changes First?")) {
				submitForm = false;
				formAction('SAVE');
			}
			else {
				if ( document.main.p_form_action != undefined ) {
						document.main.p_form_action.value = sAction;
 				 }
				document.main.submit();
			}
		}
		else {
			 if ( document.main.p_form_action != undefined ) {
					document.main.p_form_action.value = sAction;
 			  }
			document.main.submit();
		}
	}
	// The following block of code implements common features associated with
	// submit of a form
	if (submitForm) {
		//remove the quicksearch if submitting the form.
		if (document.main.p_quick_search_value != undefined) {
			document.main.p_quick_search_value.value = "";
		}
		
		// While submitting the form, disable all submit buttons in the form so that
		// multiple submits don't happen udring form processing
		for( i = 0 ;i < document.forms[0].elements.length; i++ ) { //
			if(document.forms[0].elements[i].name=="p_submit_button"){ // 
				sSubmitButtonsLen = eval("document.main.p_submit_button.length");
				sSubmitButtons = eval("document.main.p_submit_button");
				// Take care of 'p_submit_button' button
				if ((typeof(sSubmitButtonsLen) == "undefined") || (sSubmitButtonsLen == "undefined")) {
					if (!((typeof(sSubmitButtons) == "undefined") || (sSubmitButtons == "undefined"))) {
						document.main.p_submit_button.disabled=true;
					}
				} else {
					for(subButCtr=0;subButCtr<sSubmitButtonsLen; subButCtr++)	{
						sSubmitButtons[subButCtr].disabled=true;
					}
				}
			} else if(document.forms[0].elements[i].name=="p_submit"){ // 
				sPSubmitButtonLen = eval("document.main.p_submit.length"); // Some forms use 'p_submit' as button name
				sPSubmitButton = eval("document.main.p_submit"); // Some forms use 'p_submit' as button name
				// Take care of 'p_submit' button
				if ((typeof(sPSubmitButtonLen) == "undefined") || (sPSubmitButtonLen == "undefined")) {
					if (!((typeof(sPSubmitButton) == "undefined") || (sPSubmitButton == "undefined"))) {
						document.main.p_submit.disabled=true;
					}
				} else {
					for(subButCtr=0;subButCtr<sPSubmitButtonLen; subButCtr++)	{
						sPSubmitButton[subButCtr].disabled=true;
					}
				}
			}
		}
	
	}
}

function linkAction(sAction) {
	if ( (typeof(document.main) != "undefined") && (typeof(document.main.js_changed) != "undefined") && (document.main.js_changed.value == 'true') && (confirm ("   Save Form Changes?\n\n    'OK' to save changes.\n'Cancel' to discard changes.")) ) {
		formAction('SAVE');
	}
	else {
		homeTelosWindowLocation(sAction);
	}
}

function formatTrim(sValue) {
	sValue = formatLTrim(sValue);
	sValue = formatRTrim(sValue);
	return (sValue);
}

function formatLTrim(sValue) {
	while ((sValue.length > 0) && ((sValue.charAt(0) == " " ) || (sValue.charCodeAt(0) == 9  ))) {
		sValue = sValue.substring(1);
	}
	return(sValue)
}

function formatRTrim(sValue) {
	var iEndCharIndex;
	if (sValue.length > 0) {
		iEndCharIndex = sValue.length - 1;
	}
	while ((sValue.length > 0) && ((sValue.charAt(iEndCharIndex) == " " ) || (sValue.charCodeAt(iEndCharIndex) == 9  ))) {
		sValue = sValue.substring(0,iEndCharIndex);
		iEndCharIndex = sValue.length - 1;
	}
	return(sValue)
}
function PromptExcelMaxMsg(msg) {
	alert(msg);
}

function checkEMailAddress(emailAddress)
{
	var emailAdd;	
	//var check  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; 
	var check = eval("/^$|^[a-z0-9_-]+([\.]{1}[a-z0-9_-]+)*[@]{1}[a-z0-9_-]+[\.]{1}[a-z]{2,4}$|^[a-z0-9_-]+([\.]{1}[a-z0-9_-]+)*[@]{1}[a-z0-9_-]+[\.]{1}[a-z0-9_-]+[\.]{1}[a-z]{2,4}$|^[a-z0-9_-]+([\.]{1}[a-z0-9_-]+)*[@]{1}[a-z0-9_-]+[\.]{1}[a-z0-9_-]+[\.]{1}[a-z0-9_-]+[\.]{1}[a-z]{2,4}$/i");	
	if(emailAddress!=''){
		if(emailAddress.indexOf(",")!=-1){
			emailAdd = emailAddress.split(",");
		}else if(emailAddress.indexOf(";")!=-1){
			emailAdd = emailAddress.split(";");
		}else if(emailAddress.indexOf("/")!=-1){
			emailAdd = emailAddress.split("/");
		}else{
			emailAdd = new Array(emailAddress);
		}	
			
		for(i=0;i<emailAdd.length;i++){	
		    if(emailAdd[i]!=''){		    
			    if (!check.test(emailAdd[i])){
						return false;
					}				
				}else{
				     return false;
				}				
		}
		return true;
	}else{
		return true;
	}
}

function checkLenderCaseNumberNonHud(fieldName1, fieldName2, errorField) {
	var bHasError = false;
	oForm = document.forms["main"];
	err_gsErrorMsg = "";
	err_gbHasError = false;

	sTest = "!isAllDigits(oForm." + fieldName1 + ".value)";
	err_gsErrorMsg = eval(sTest) ? "Must be valid case number. (###-#######)" : "";

	if (err_gsErrorMsg == "") {
		sTest = "!isAllDigits(oForm." + fieldName2 + ".value)";
		err_gsErrorMsg = eval(sTest) ? "Must be valid case number. (###-#######)" : "";
	}

	if (err_gsErrorMsg == "") {
		if (eval("!(oForm." + fieldName1 + ".value.length == 0 && oForm."+fieldName2+".value.length == 0)")){
			sTest = "!(oForm." + fieldName1 + ".value.length == 4 && oForm."+fieldName2+".value.length > 5)";
			err_gsErrorMsg = eval(sTest) ? "Must be valid case number. (###-#######)" : "";
		}
	}

	document.main.js_changed.value = "true";

	js_errSet(document.images, "err" + errorField);
	//setErrorMarkerFields(document.images);

	bHasError = err_gbHasError;

	return(!bHasError);
}

//</script>

