function IsStrValEmpty(strVal){
	if(strVal.length == 0)
		return true;
	else
		return false;				
}

function IsValZero(Val){
	if(Val == 0)
		return true;
	else
		return false;				
}

function IsValNumber(strVal)
{	
	var REvalidNumber = /^[0-9]+$/;
	if(!REvalidNumber.test(strVal))	
		return false;
	else
		return true;
}

function DisplayErrorMessage(strMessage){
	alert(strMessage)
}

function DisplayUserMessage(strMessageHeader, arrErrorFields, arrMessageIndex,strClassName){	
	var intIndex;
	var strMessage=strMessageHeader;
	for (intIndex=0; intIndex<arrMessageIndex.length; intIndex++)
		strMessage += intIndex+1 + ". " + arrMessageIndex[intIndex] + "\n";			
	for (intIndex=0; intIndex<arrErrorFields.length; intIndex++)
		document.getElementById(arrErrorFields[intIndex]).className = strClassName	
	alert(strMessage);	
	document.getElementById(arrErrorFields[0]).focus();	
}

function DisplayMessageForInvalidField(strMessageHeader, arrErrorFields,strClassName){
	for (intIndex=0; intIndex<arrErrorFields.length; intIndex++)
		document.getElementById(arrErrorFields[intIndex]).className = strClassName
	alert(strMessageHeader);	
	document.getElementById(arrErrorFields[0]).focus();	
}

function ClearErrorColorInText(arrErrorFields){
	for (intIndex=0; intIndex<arrErrorFields.length; intIndex++){	
		document.getElementById(arrErrorFields[intIndex]).className = "textClearError"
	}
}


function IsUsernameValid(strUsername){
	var reUser = /^([a-zA-Z0-9_])+$/;	
	if (!reUser.test(strUsername))
		return false;
	else
		return true;
}

function FormatField(keyCode,txtObject,strMask){	
	if (keyCode==46 || keyCode==9 || keyCode==17 || keyCode==18 || keyCode==144 || keyCode==20){	
		return true
	}	
	
	if (txtObject.value.length >= strMask.length && keyCode != 8) {
		return false;
	}
	if (keyCode != 8){
		for(i=txtObject.value.length; i<=strMask.length;i++)
			if (strMask.charAt(i) != '#')				
				txtObject.value += strMask.charAt(i)
			else
				break;
	}	
	else if (keyCode == 8)
		txtObject.value = txtObject.value.substring(0,txtObject.value.length-1);
	if (keyCode >= 96 && keyCode <= 105)
		keyCode -= 48
	if ( (keyCode > 47 && keyCode < 58) &&  keyCode != 8)
		txtObject.value = txtObject.value + String.fromCharCode(keyCode);	
	return false;	
}

function hasFieldExceededMaxLength(txtObject,maxLength){
	if (txtObject.value.length > maxLength) return false;
	else return true;
}

function fnCheckMaxLength(strTAName,lngMaxLength){								
	if (document.getElementById(strTAName).value.length <= lngMaxLength){					
		return true;
	}
	else{								
		if (event.keyCode != 8 && event.keyCode != 9 && event.keyCode != 20 && event.keyCode != 17)
			return false;
	}
}

//The following function checks for empty fields belonging to the *Compulsary* level.

function fnDisplayEmptyFieldAlert(arrFieldList,arrFieldDispList){
	var strDispMess = arrUserMessages[0]		
	var error = false
	for (i=0;i<arrFieldList.length;i++){				
		if (document.getElementById(arrFieldList[i]).tagName.toLowerCase() == "input"){
			document.getElementById(arrFieldList[i]).style.borderColor = "black"
			if (IsStrValEmpty(document.getElementById(arrFieldList[i]).value)){
				document.getElementById(arrFieldList[i]).style.borderColor = "red"			
				strDispMess += arrFieldDispList[i] + "\n"			
				document.getElementById(arrFieldList[i]).focus();
				error = true;
			}
		}
		if (document.getElementById(arrFieldList[i]).tagName.toLowerCase() == "select"){
			document.getElementById(arrFieldList[i]).style.color = "black"			
			if (fnGetCurrentlySelectedItems(arrFieldList[i]) == "-1" || fnGetCurrentlySelectedItems(arrFieldList[i]) == ""){
				document.getElementById(arrFieldList[i]).style.color = "red"			
				strDispMess += arrFieldDispList[i] + "\n"			
				document.getElementById(arrFieldList[i]).focus();
				error = true;		
			}
		}
		
	}
	if (error == true){
		alert(strDispMess);
		return false;		
	}
	else return true;
}

//The following 2 functions check for Invalid entry in all the Phone Number fields 

function fnDisplayInvalidFieldAlert(arrInvalidFieldList,arrFieldDisplayList){
	var strDisplayErrorMessage = arrUserMessages[30]
	var error = false
	for (i=0;i<arrInvalidFieldList.length;i++){
		document.getElementById(arrInvalidFieldList[i]).style.borderColor = "black"
		if (IsInvalidPhoneNumber(document.getElementById(arrInvalidFieldList[i]).value)){
			document.getElementById(arrInvalidFieldList[i]).style.borderColor = "red"
			strDisplayErrorMessage += arrFieldDisplayList[i] + "\n"						
			document.getElementById(arrInvalidFieldList[i]).focus();
			error = true;
		}
	}
	if (error == true){
	alert(strDisplayErrorMessage);
	return false;		
	}
	else return true;
}


function IsInvalidPhoneNumber(strInvalid){
	if(strInvalid.length != 14 && strInvalid.length != 0)
		return true;
	else
		return false;				
}

//The following 2 functions check for validity of Email

function fnDisplayInvalidEmailAlert(arrInvalidEmailFieldList,arrInvalidEmailFieldDisplayList){
	var strDisplayErrorMessageForEmail = arrUserMessages[35]
	var error = false
	for (i=0;i<arrInvalidEmailFieldList.length;i++){
		document.getElementById(arrInvalidEmailFieldList[i]).style.borderColor = "black"
		if (! IsEmailValid(document.getElementById(arrInvalidEmailFieldList[i]).value)){
			document.getElementById(arrInvalidEmailFieldList[i]).style.borderColor = "red"
			strDisplayErrorMessageForEmail += arrInvalidEmailFieldDisplayList[i] + "\n"						
			document.getElementById(arrInvalidEmailFieldList[i]).focus();
			error = true;
		}
	}
	if (error == true){
	alert(strDisplayErrorMessageForEmail);
	return false;		
	}
	else return true;
}

function IsEmailValid(strEmail)
{
	// Commented Temporarily
	/*var e_mail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/;	
	if (e_mail.test(strEmail))
		return true;
	else
		return false;
	*/
	return true;
}

//SSN Validation

function SSNValidation(ssn) {
	var matchArr = ssn.match(/^(\d{3})-?\d{2}-?\d{4}$/);
	var numDashes = ssn.split('-').length - 1;
	if (matchArr == null || numDashes == 1) {
	alert('Invalid SSN. Must be 9 digits or in the form NNN-NN-NNNN.');
	msg = "does not appear to be valid";
	return false;
	}
	else 
	if (parseInt(matchArr[1],10)==0) {
	alert("Invalid SSN: SSN's can't start with 000.");
	msg = "does not appear to be valid";
	return false;
	}
}

// Zip Code validation for 5digit or 5+4 digit zipcode.

function validateZIP(field) {
	var valid = "0123456789-";
	var hyphencount = 0;

	if (field.value.length!=5 && field.value.length!=10) {
	alert("Please enter your 5 digit(in 12345 format) or 5 digit+4 zip code(in 12345-1234 format).");
	field.style.borderColor = "red"
	field.focus
	return false;
	}
	for (var i=0; i < field.value.length; i++) {
	temp = "" + field.value.substring(i, i+1);
	if (temp == "-") hyphencount++;
	if (valid.indexOf(temp) == "-1") {
	alert("Invalid characters in your zip code. Please try again.");
	field.style.borderColor = "red"
	field.focus
	return false;
	}
	if ((hyphencount > 1) || ((field.value.length==10) && ""+field.value.charAt(5)!="-")) {
	alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-1234'.   Please try again.");
	field.style.borderColor = "red"
	field.focus
	return false;
	   }
	}
	return true;
}

function fnGetCurrentlySelectedItems(selObjectID){
	var strReturn=""
	for(var i=0;i<document.getElementById(selObjectID).options.length;i++){
		if (document.getElementById(selObjectID).options[i].selected == true)
			strReturn += document.getElementById(selObjectID).options[i].value + ";"
	}
	if (strReturn.length) strReturn = strReturn.substr(0,strReturn.length-1)
	return strReturn
}

function CheckQuote(keyCode,txtObject)
{
	// single quote
	if (keyCode == 39 || keyCode == 34){
		alert(String.fromCharCode(keyCode) + " is invalid");
		return false;
	}
	else return true;	
}