//--------------------------------------------------------------------------------------
//Functions added by Ratish on Aug 20 2003 for browser detection

function CheckBrowser(b,v) {
  /*
  ** Check if the current browser is compatible
  **  b  browser name
  **  v  version number (if 0 don't check version)
  ** returns true if browser equals and version equals or greater
  */
  browserOk = false;
  versionOk = false;

  browserOk = (navigator.appName.indexOf(b) != -1);
  if (v == 0) versionOk = true;
  else  versionOk = (v <= parseInt(navigator.appVersion));
  return browserOk && versionOk;
 }

var BROWSER_AGENT = "";
if(CheckBrowser("Microsoft",0)==true){
	BROWSER_AGENT = "ie";
}else{
	BROWSER_AGENT = "nt";
}

//---------------------------------------------------------------------------------------
 
function VAL_InitElement(oTargetElement, bRequired, sFormatType, sBusLogicValidator, sCustomLogic, sCustomCleanUp) {

	oTargetElement.required = bRequired;
	if (sFormatType != null) oTargetElement.formatType = sFormatType;
	if (sBusLogicValidator != null) oTargetElement.busLogicValidator = sBusLogicValidator;
	if (sCustomLogic != null) oTargetElement.customLogic = sCustomLogic;
	if (sCustomCleanUp != null) oTargetElement.customLogicCleanUp = sCustomCleanUp;
	oTargetElement.isFormValidated = true;
}//end function

function VAL_ErrorDisplay(sFullName, oFormLabel, oErrorLabel, sErrorLabelCSS) {
	this.fullName = sFullName;
	if (oFormLabel != null) this.formLabel = oFormLabel;
	if (oErrorLabel != null) this.errorLabel = oErrorLabel;
	if (sErrorLabelCSS != null) this.errorLabelCSS = sErrorLabelCSS;
}//end function

function VAL_AttachErrorDisplay(oTargetElement, oErrorDisplay) {
	oTargetElement.errorDisplay = oErrorDisplay;
	VAL_SetFieldRequired(oTargetElement, oTargetElement.required);
}//end if

function VAL_Validate(oElement) {

	// Set reference to target element
	var oTargetElement = VAL_GetTargetElement(oElement);

	// Initialize success flag
	var bSuccess = false;
		
	// Reset form and error labels
	VAL_ResetLabels(oTargetElement);
	
	// Determine if element is required
	bSuccess = VAL_CheckRequired(oTargetElement);

	if (!bSuccess) return false;
	
	// Determine if value contains only white space.
	// If it does, set value to an empty string.	
	if (VAL_StripWhiteSpace(oTargetElement.value) != "") {
		
		// Validate format and set element to preferred format
		bSuccess = VAL_ValidateFormat(oTargetElement);
		if (!bSuccess) return false;
		
		// Validate business logic
		bSuccess = VAL_CheckBusinessLogic(oTargetElement);
		if (!bSuccess) return false;
		
		// Process custom logic
		VAL_PerformCustomLogic(oTargetElement);
		
	}//end if
	else {
		// Set value to empty string
		oTargetElement.value = ""
	}//end else
	
	// Return success
	return true;	
}//end function

function VAL_ValidateFormat(oTargetElement) {

	var iStatusCode;
	var bSuccess = true;
	var SUCCESS_CODE = 1;
	
	if (typeof(oTargetElement.formatType) != "undefined") {
		switch (oTargetElement.formatType.toUpperCase()) {
			case "SSN" :
				iStatusCode = VAL_CheckSSN(oTargetElement);
				break;
			case "PHONE" :
				iStatusCode = VAL_CheckPhone(oTargetElement);
				break;
			case "FIRSTCAP" :
				iStatusCode = VAL_FormatFirstLetterCapital(oTargetElement);
				break;
			case "NAME" :
				iStatusCode = VAL_CheckName(oTargetElement);
				break;
			case "MIDINITIAL" :
				iStatusCode = VAL_CheckMiddleInitial(oTargetElement);
				break;
			case "DATE" :
				iStatusCode = VAL_CheckDate(oTargetElement);
				break;
			case "ZIP" :
				iStatusCode = VAL_CheckZIP(oTargetElement);
				break;
			case "CREDIT" :
				iStatusCode = VAL_CheckCreditCard(oTargetElement);
				break;
			case "EMAIL" :
	//alert('emailvalidatiom');
				iStatusCode = VAL_CheckEmail(oTargetElement);
				break;
			case "PASSWORD" :
				iStatusCode = VAL_CheckPassword(oTargetElement);
				break;
			case "GENERIC" :
				iStatusCode = VAL_CheckRequired(oTargetElement);
				break;
			
		}//end switch
		
		if (iStatusCode != SUCCESS_CODE) {
			bSuccess = false;
			VAL_CleanUpCustomLogic(oTargetElement);
			VAL_ShowFormatError(oTargetElement, iStatusCode);
		}//end if
	}//end if
	
	return bSuccess;
}//end function

function VAL_GetTargetElement(oElement) {
	if (typeof(oElement) == "undefined")
 			return window.event.srcElement;
	else
		return oElement;
}//end function
	
function VAL_ValidateForm(oForm) {
	
	var oElements = oForm.elements;
	var sBuffer = "";
	var bElementSuccess = true;
	var bFormSuccess = true;
	
	for (var i = 0; i < oElements.length; i++) {
		//if (typeof(oElements[i].isFormValidated) != "undefined") {
		if (oElements[i].isFormValidated) {
			bElementSuccess = VAL_Validate(oElements[i]);
			if (!bElementSuccess) bFormSuccess = false;		
		}//end if
	}//end for

	return bFormSuccess;
}//end function
	
function VAL_CheckRequired(oTargetElement) {
	if (oTargetElement.required) {
		var sValue = VAL_StripWhiteSpace(oTargetElement.value);
		if (sValue == "") {
			VAL_ShowRequiredError(oTargetElement);
			VAL_CleanUpCustomLogic(oTargetElement);
			return false;
		}//end if
	}//end if
	
	return true;
}//end function

function VAL_CheckPassword(oTargetElement){
	var SUCCESS = 1;
	var INVALID_FORMAT = -1;
	var sValue = VAL_TrimWhiteSpace(oTargetElement.value);
	if (sValue.length <6){
		oTargetElement.focus();
		return INVALID_FORMAT;
							}
	var firstChar = "";
	var lastChar = "";
	var sAllow = "abcdefghijklmnopqrstuvwxyz'.-1234567890 +_&*~><\/?#!$%^()=|"
	var sBuffer = "";
	var bFound;
	
	for (var i = 0; i < sValue.length; i++) {
		bFound = false;
		
		if (i==0){
			 firstChar = sValue.charAt(i);
				if (IsNumeric(firstChar)==true) {
					oTargetElement.focus();
					return INVALID_FORMAT;
												}
				}
		if (i==sValue.length-1){
			lastChar = sValue.charAt(i);
				if (IsNumeric(lastChar)==true) {
					oTargetElement.focus();
					return INVALID_FORMAT;
												}
				}
		
		for (var j = 0; j < sAllow.length; j++) {
			if (sValue.charAt(i) == sAllow.charAt(j) || sValue.charAt(i) == sAllow.charAt(j).toUpperCase()) {
				bFound = true;
				break;
			}//end if
		}//end for
		
		if (bFound) sBuffer += sValue.charAt(i);
	}//end for

		
	if (VAL_StripWhiteSpace(sBuffer) == "")
		return INVALID_FORMAT;
	else {
		oTargetElement.value = VAL_TrimWhiteSpace(sBuffer);
		return SUCCESS;
	}//end else

}//end function

function IsNumeric(sChar){
	var bFnd = false;
	var sNumeric = "0123456789";
		for (var j = 0; j < sNumeric.length; j++) {
			if (sChar == sNumeric.charAt(j)) {
				bFnd = true;
				break;
			}//end if
		}//end for

	return bFnd;
}//	end function

function VAL_CheckGeneric(oTargetElement){
	var SUCCESS = 1;
	var INVALID_FORMAT = -1;
	var sValue = VAL_TrimWhiteSpace(oTargetElement.value);
	if (sValue.length <1){
		oTargetElement.focus();
		return INVALID_FORMAT;
							}
	else {
		oTargetElement.value = VAL_TrimWhiteSpace(sBuffer);
		return SUCCESS;
	}//end else
}//end function


function VAL_CheckPhone(oTargetElement) {
	var SUCCESS = 1;
	var INVALID_FORMAT = -1;
	var sBuffer = VAL_StripNonNumeric(oTargetElement.value);
		
	if (sBuffer.length != 10)
		return INVALID_FORMAT;
	else {
		VAL_FormatPhone(sBuffer, oTargetElement);
		return SUCCESS;
	}//end else
}//end function

function VAL_CheckName(oTargetElement) {
	var SUCCESS = 1;
	var INVALID_FORMAT = -1;
	var sValue = VAL_TrimWhiteSpace(oTargetElement.value);
	var sAllow = "abcdefghijklmnopqrstuvwxyz'.- "
	var sBuffer = "";
	var bFound;
	
	for (var i = 0; i < sValue.length; i++) {
		bFound = false;

		for (var j = 0; j < sAllow.length; j++) {
			if (sValue.charAt(i) == sAllow.charAt(j) || sValue.charAt(i) == sAllow.charAt(j).toUpperCase()) {
				bFound = true;
				break;
			}//end if
		}//end for
		
		if (bFound) sBuffer += sValue.charAt(i);
	}//end for
		
	if (VAL_StripWhiteSpace(sBuffer) == "")
		return INVALID_FORMAT;
	else {
		oTargetElement.value = VAL_TrimWhiteSpace(sBuffer);
		VAL_FormatFirstLetterCapital(oTargetElement);
		return SUCCESS;
	}//end else
}//end function

function VAL_CheckMiddleInitial(oTargetElement) {
	var SUCCESS = 1;
	var INVALID_FORMAT = -1;
	var sValue = VAL_StripWhiteSpace(oTargetElement.value);
	
	if (sValue.length > 0) {
		var sAllow = "abcdefghijklmnopqrstuvwxyz"
		var bFound;
	
		for (var i = 0; i < sValue.length; i++) {
			bFound = false;

			for (var j = 0; j < sAllow.length; j++) {
				if (sValue.charAt(i) == sAllow.charAt(j) || sValue.charAt(i) == sAllow.charAt(j).toUpperCase()) {
					bFound = true;
					break;
				}//end if
			}//end for
		
			if (bFound) {
				oTargetElement.value = sValue.charAt(i);
				VAL_FormatFirstLetterCapital(oTargetElement);
				return SUCCESS;
			}//end if
		}//end for
		
		return INVALID_FORMAT;
	}//end if
}//end function

function VAL_CheckSSN(oTargetElement) {
	var SUCCESS = 1;
	var INVALID_FORMAT = -1;
	var sBuffer = VAL_StripNonNumeric(oTargetElement.value);

	if (sBuffer.length != 9)
		return INVALID_FORMAT;	
	else {
		VAL_FormatSSN(sBuffer, oTargetElement);
		return SUCCESS;	
	}//end else
}//end function

function VAL_CheckDate(oTargetElement) {
	var oDateArray = oTargetElement.value.split("/")
	var SUCCESS = 1;
	var INVALID_FORMAT = -1;
	var INVALID_MONTH = -2;
	var INVALID_DAY = -3;
	var INVALID_YEAR = -4;
	
	if (oDateArray.length < 3)
		return INVALID_FORMAT;
	else { 
		if (VAL_CheckDateMonth(oDateArray[0])) {
			if (VAL_CheckDateDay(oDateArray[1], oDateArray[0])) {
				if (VAL_CheckDateYear(oDateArray[2])) {
					try {
						var oDate = new Date(oTargetElement.value);
					}//end try
					catch (e) {
						return INVALID_FORMAT;
					}//end catch
						
					if (isNaN(oDate))
						return INVALID_FORMAT;
					else {
						VAL_FormatDate(oDate, oTargetElement);
						return SUCCESS;
					}//end else
				}//end if
				else
					return INVALID_YEAR;
			}//end if
			else
				return INVALID_DAY;
		}//end if
		else
			return INVALID_MONTH;
	}//end else
}//end function

function VAL_CheckDateMonth(sMonth) {
	var BASE = 10;
	var iMonth = parseInt(sMonth, BASE);
	return (iMonth >= 1 && iMonth <= 12) ? true : false;
}//end function

function VAL_CheckDateDay(sDay, sMonth) {
	var BASE = 10;
	var iDay = parseInt(sDay, BASE);
	var iMonth = parseInt(sMonth, BASE);
	
	switch (iMonth) {
		//31 days
		case  1 :
		case  3 : 
		case  5 :
		case  7 :
		case  8 :
		case 10 :
		case 12 :
			return (iDay >=1 && iDay <= 31) ? true : false;
			break;

		//30 days
		case  4 :
		case  6 :
		case  9 :
		case 11 :
			return (iDay >=1 && iDay <= 30) ? true : false;
			break;
			
		//28/29 days
		case  2 :
			return (iDay >=1 && iDay <= 29) ? true : false;
			break;
	}//end switch
}//end function

function VAL_CheckDateYear(sYear) {
	var iYear = parseInt(sYear);
	return (iYear >= 1000 && iYear < 10000) ? true : false;
}//end function

function VAL_CheckZIP(oTargetElement) {
	var sBuffer = VAL_StripNonNumeric(oTargetElement.value);

	if (sBuffer.length != 5 && sBuffer.length != 9)
		return false;
	else {
		VAL_FormatZIP(sBuffer, oTargetElement);
		return true;
	}//end else
}//end function

function VAL_CheckCreditCard(oTargetElement) {
	var sBuffer = VAL_StripNonNumeric(oTargetElement.value);

	if (sBuffer.length != 16)
		return false;
	else {
		VAL_FormatCreditCard(sBuffer, oTargetElement);
		return true;
	}//end else
}//end function

function VAL_CheckEmail(oTargetElement) {
	var oEmailRegEx = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/
	return oEmailRegEx.test(oTargetElement.value);
}//end function

function VAL_FormatSSN(sValue, oTargetElement) {
	var sFirst = sValue.substr(0,3);
	var sSecond = sValue.substr(3,2);
	var sThird = sValue.substr(5);
	oTargetElement.value = sFirst + "-" + sSecond + "-" + sThird;
}//end function

function VAL_FormatPhone(sValue, oTargetElement) {
	var sAreaCode = sValue.substr(0,3);
	var sPrefix = sValue.substr(3,3);
	var sExtension = sValue.substr(6);
	oTargetElement.value = "(" + sAreaCode + ")" + sPrefix + "-" + sExtension;
}//end function

function VAL_FormatDate(oDate, oTargetElement) {
	var sDay = oDate.getDate();
	var sMonth = oDate.getMonth() + 1;
	var sYear = oDate.getFullYear();
		
	var sDayZeroPadding = (sDay < 10) ? "0" : "";
	var sMonthZeroPadding = (sMonth < 10) ? "0" : "";
		
	// The month and day are purposely reversed due to the
	// way that javascript handles date objects that are
	// initialized as strings.
	oTargetElement.value = sMonthZeroPadding + sMonth.toString() + "/" 
	oTargetElement.value += sDayZeroPadding + sDay.toString() + "/" + sYear.toString();
}//end function

function VAL_FormatFirstLetterCapital(oTargetElement) {
	var sBuffer = "";
	var sValue = VAL_TrimWhiteSpace(oTargetElement.value);
		
	for (var i = 0; i < sValue.length; i++) {
		if (i == 0) 
			sBuffer += sValue.charAt(0).toUpperCase();
		else {
			if (sValue.charAt(i-1) == " ")
				sBuffer += sValue.charAt(i).toUpperCase();
			else
				sBuffer += sValue.charAt(i).toLowerCase();
		}//end else
	}//end for
	
	oTargetElement.value = sBuffer;
	return true;
}//end function

function VAL_FormatZIP(sValue, oTargetElement) {
	if (sValue.length == 9)
		oTargetElement.value = sValue.substr(0,5) + "-" + sValue.substr(5);
	else
		oTargetElement.value = sValue;
}//end function

function VAL_FormatCreditCard(sValue, oTargetElement) {
	var sFirst = sValue.substr(0,4);
	var sSecond = sValue.substr(4,4);
	var sThird = sValue.substr(8,4);
	var sFourth = sValue.substr(12);
	oTargetElement.value = sFirst + "-" + sSecond + "-" + sThird + "-" + sFourth;
}//end function

function VAL_ShowFormatError(oTargetElement, iStatusCode) {
	var sErrMsg = VAL_GetErrorMessage(oTargetElement.formatType, iStatusCode);
	VAL_ShowError(sErrMsg, oTargetElement);
}//end function
	
function VAL_ShowRequiredError(oTargetElement) {
	var sErrMsg = VAL_GetErrorMessage("REQUIRED");
	VAL_ShowError(sErrMsg, oTargetElement);
}//end function
	
function VAL_ShowError(sErrMsg, oTargetElement) {
	if (typeof(oTargetElement.errorDisplay) != "undefined") {
		var sFullName = oTargetElement.errorDisplay.fullName;
		var sFullErrMsg = sFullName + sErrMsg;
		VAL_SetFormLabel(oTargetElement, "red");
		VAL_SetErrorLabel(oTargetElement, sFullErrMsg);
	}//end if
}//end function

function VAL_GetErrorMessage(sErrorType, iStatusCode) {
	switch (sErrorType.toUpperCase()) {
		case "SSN" :
			return " is invalid. Valid format is '999-99-9999'."
			break;
		case "PHONE" :
			return " is invalid. Valid format is '(555)555-5555'."
			break;
		case "NAME" :
			return " is invalid. Only letters, ', and . are allowed.";
			break;
		case "MIDINITIAL" :
			return " is invalid. Only one letter is allowed."
			break;
		case "DATE" :
			switch (iStatusCode) {
				case -1 :
					return " is invalid. Valid format is 'mm/dd/yyyy'."
					break;
				case -2 :
					return " is invalid. Month(mm) is out of range."
					break;
				case -3 :
					return " is invalid. Day(dd) is out of range."
					break;
				case -4 :
					return " is invalid. Year(yyyy) is out of range."
					break;
			}//end switch	
		case "ZIP" :
			return " is invalid. Valid formats are '99999' and '99999-9999'."
			break;
		case "CREDIT" :
			return " is invalid. Valid format is '9999-9999-9999-9999'."
			break;
		case "EMAIL" :
			return " is invalid. Valid format is 'address@domain.extension' (jdoe@mycompany.com)."
			break;
		case "PASSWORD" :
			return " is invalid. Min 6 characters. First and last characters are none-numeric."
			break;
		case "REQUIRED" :
			return " is required."
			break;
	}//end switch
}//end function

function VAL_StripNonNumeric(sValue) {
	var sBuffer = "";
		
	sValue = VAL_StripWhiteSpace(sValue);

	for (var i = 0; i < sValue.length; i++) {
		if (!isNaN(sValue.charAt(i))) sBuffer += sValue.charAt(i);
	}//end for
		
	return sBuffer;
}//end function
	
function VAL_StripWhiteSpace(sValue) {
	oWhiteSpaceRegExp = /\s+/g
	return sValue.replace(oWhiteSpaceRegExp, "");
}//end function

function VAL_TrimWhiteSpace(sValue) {
	var sBuffer = ""
	var oWhiteSpaceRegExp = /\s+\B/g
	sBuffer = sValue.replace(oWhiteSpaceRegExp, "");
	if (sBuffer.charAt(0) == " ") sBuffer = sBuffer.substr(1);
	return sBuffer;
}//end function

function VAL_SetErrorLabel(oTargetElement, sErrorMessage) {

	var oErrorLabel = oTargetElement.errorDisplay.errorLabel;
	if (typeof(oErrorLabel) != "undefined") {

		if (sErrorMessage == "") {
			oErrorLabel.style.display = "none";
			oErrorLabel.innerText = "";			
		}//end if
		else {
			var sStyle = "";
			if (typeof(oTargetElement.errorDisplay.errorLabelCSS) != "undefined")
				sStyle = oTargetElement.errorLabelCSS;
			else
				sStyle = "color:red;font-size:10pt";
			
			oErrorLabel.style.cssText = sStyle;
			oErrorLabel.style.display = "block";
			//-------------------------------------------------------------------------
			//Code added by Ratish on Aug 20 2003 for displaying error message 
			//in Mozilla fix for Defect 2960 in Astra
			if (BROWSER_AGENT == "ie")
				oErrorLabel.innerText += sErrorMessage;
			else
				oErrorLabel.innerHTML = sErrorMessage;						
			//-------------------------------------------------------------------------				
		}//end else
	}//end if
	else {
	 	if (sErrorMessage != "") alert(sErrorMessage);
	
	}//end else
}//end function

function VAL_SetFormLabel(oTargetElement, sColor) {
	var oFormLabel = oTargetElement.errorDisplay.formLabel;
	if (typeof(oFormLabel) != "undefined") {
		oFormLabel.style.color = sColor;
	}//end if
}//end function

function VAL_ResetLabels(oTargetElement) {
	if (typeof(oTargetElement.errorDisplay) != "undefined") {
		VAL_SetFormLabel(oTargetElement, "");
		VAL_SetErrorLabel(oTargetElement, "");
	}//end if
}//end function

function VAL_CheckBusinessLogic(oTargetElement) {
	if (typeof(oTargetElement.busLogicValidator) != "undefined") {
		var sSuccess = "";
		sSuccess = eval(oTargetElement.busLogicValidator + "(oTargetElement)");
		if (sSuccess != "") {
			VAL_ShowError(sSuccess, oTargetElement);
			VAL_CleanUpCustomLogic(oTargetElement);
			return false;
		}//end if
	}//end if
	
	return true;
}//end function

function VAL_PerformCustomLogic(oTargetElement) {
	if (typeof(oTargetElement.customLogic) != "undefined") {
		eval(oTargetElement.customLogic + "(oTargetElement)");
	}//end if
}//end function

function VAL_CleanUpCustomLogic(oTargetElement) {
	if (typeof(oTargetElement.customLogicCleanUp) != "undefined") {
		eval(oTargetElement.customLogicCleanUp + "(oTargetElement)");
	}//end if			
}//end function

function VAL_SetFieldRequired(oTargetElement, bIsRequired) {
	var rexp1 = /<font color="?'?red'?"?>\*<\/font>/gi;
	var rexp2 = /<font color="?'?#ff0000'?"?>\*<\/font>/gi;
	var reqHTML = '<font color="red">*</font>'
	var oFormLabel = oTargetElement.errorDisplay.formLabel;
	
	oTargetElement.required = bIsRequired;
	if (typeof(oFormLabel) != "undefined"){
		if (bIsRequired) {
			if ((oFormLabel.innerHTML.search(rexp1) == -1) & (oFormLabel.innerHTML.search(rexp2) == -1))
				oFormLabel.innerHTML = reqHTML + oFormLabel.innerHTML;
		}
		else {
			if ((oFormLabel.innerHTML.search(rexp1) == 0) | (oFormLabel.innerHTML.search(rexp2) == 0))
				oFormLabel.innerHTML = oFormLabel.innerHTML.slice(oFormLabel.innerHTML.indexOf("*")+8);
		}
	}
//end if
}//end function

// Ensures that a user supplied date occurs in the future.
function VAL_FutureDateOnly(oTargetElement) {

	var iNowMS = Date.parse(new Date());
	var iSuppliedMS = Date.parse(oTargetElement.value);
	if (iNowMS - iSuppliedMS < 0)
		return "";
	else
		return " must be a date in the future.";
}//end function	


// Ensures that a user supplied date occurs in the past.
function VAL_PastDateOnly(oTargetElement) {
	var iNowMS = Date.parse(new Date());
	var iSuppliedMS = Date.parse(oTargetElement.value);
	if (iNowMS - iSuppliedMS > 0)
		return "";
	else
		return " must be a date in the past.";
}//end function


