//******************************************************************************
//Author/Coded By    :-Sharath K.H
//Module Name        :-FormValidations.js
//Type               :-Include file
//Created On         :-10/28/2003
//Called From        :-
//Functionality      :-General functions to validate all Fields
//Version No         :-2.0
//******************************************************************************/
//window.onerror = ErrorHandler;

//******************** Declare All Global Variables here **********************/
var strInitialErrMessage,strErrEmpty,strErrExceed,strErrLess,strErrChar;
var strErrEnter,strErrIn,strErrDate,strErrDate1,strErrEmail,strErrEmail1,strErrNotMatch; 
var strDeleteNoSelection, strConfirmDelete
var strErrMessage,objFocus;

//******************** Change Standard Error Messages here ********************/
strErrMessage = "";

strInitialErrMessage = "--------------------------------------------------\n Please correct the following information \n--------------------------------------------------\n    ";
strErrEmpty = "Information should be entered in"
strField = "field"
strErrExceed = "should not exceed"
strErrLess = "cannot be less than"
strErrChar = "characters"
strErrEnter = "Enter valid"
strErrIn = "in"
strErrDate = "year should be between 1900 to 2079"
strErrDate1 = "is invalid date."
strErrEmail = "address seems wrong. Please check the suffix '@' sign for accuracy."
strErrEmail1 = "address seems wrong. Please check the suffix '.' for accuracy."
strErrNotMatch = "does not match with"
strErrSelect = "should be selected"

strDeleteNoSelection = "Please select items to delete"
strConfirmDelete = "Are you sure you want to delete the selected item(s)?"

//******************************************************************************
//1.This function is used to display Error Alert and it is called from front end.
//Ex: return fnDisplayError();
//******************************************************************************/
function fnDisplayError(){
	if (strErrMessage != ""){
		alert(strInitialErrMessage + strErrMessage);
		eval(objFocus).focus();
		if ((eval(objFocus).type == "text")||(eval(objFocus).type == "textarea")){
			eval(objFocus).select();
		}
		objFocus = "";
		strErrMessage = ""
		return false;
	}	
	else {
		return true;
	}
}

//*****************************************************************************
//2.This function is used to Check blank spaces and it is used by other functions.
//Ex: return fnIsEmpty("Chandru")	
//******************************************************************************/
function fnIsEmpty(str){
	for (var i = 0; i < str.length; i++) 
		if (" " != str.charAt(i)) return false;
	return true;	
}
function isUndefined(property) {
    return (typeof property == 'undefined');
}
//*****************************************************************************
//3.This function is used to Check Textbox/Textarea/select box for the Empty,
//Maximum length,Minimum length.
//Note1:If Required is set to false,then it validates only if you enter in the input boxes.
//Note2:MinLength should be 0 when no minimum length validation required.
//Ex: fnCheckString("document.frmAddEdit.txtName","Name",25,0,true);	
//******************************************************************************/
function fnCheckString(ObjectName,ObjectCaption,MaxLength,MinLength,Required){
	var strErr = "";
	var strVal = "";
	var bolCombo = 0;
	var l;
	ObjectName=eval(ObjectName)
	if (ObjectName.type=='select-one'){
		strVal = (ObjectName.options.length>0)?ObjectName.options[ObjectName.selectedIndex].value:"";
		bolCombo = 1
	}else{
		strVal=ObjectName.value;
	}
	l=strVal.length;
	if (Required==true ||(Required==false && l>0)){
		if (((fnIsEmpty(strVal))||(strVal.length <= 0)) && bolCombo == 1){
			strErr = ObjectCaption + strErrSelect + "\n\    " ;
		}else if ((fnIsEmpty(strVal))||(strVal.length <= 0)){
			strErr = strErrEmpty +" "+ ObjectCaption + "\n\    " ;
		}else if (l>MaxLength){
			strErr = ObjectCaption + " " + strErrExceed + " " + MaxLength + " " + strErrChar + "\n\    " ;
		}else if (MinLength > 0){
			if (strVal.length < MinLength){
				strErr = ObjectCaption + " " + strErrLess + " " + MinLength + " " + strErrChar + "\n\    ";
			}
		} 
		if (strErr != ""){
			strErrMessage = strErrMessage + strErr;
			//if ((objFocus == undefined)||(objFocus == "")){
			if ((isUndefined(objFocus))||(objFocus == "")){
				objFocus = ObjectName
			}
		}		
	}else{return true;}
}

//*****************************************************************************
//4.This function is used to Check Textbox/Textarea/select box for the Numeric value
//along with Empty,Maximum length,Minimum length.
//Note1:If Required is set to false,then it validates only if you enter in the input boxes.
//Note2:MinLength should be 0 when no minimum length validation required.Below Ex
//validates for Zip code with 6 digit number
//Ex: fnCheckNumbers("document.frmAddEdit.txtZip","Zip Code",6,6,true);	
//******************************************************************************/
function fnCheckNumbers(ObjectName,ObjectCaption,MaxLength,MinLength,Required){
	var strErr = ""
	var strVal = "";
	var l;
	ObjectName=eval(ObjectName)
	if (ObjectName.type=='select-one'){
		strVal = (ObjectName.options.length>0)?ObjectName.options[ObjectName.selectedIndex].value:"";
	}else{
		strVal=ObjectName.value;
	}
	l=strVal.length;

    if (Required==true || (Required==false && l>0)){
		if ((fnIsEmpty(strVal))||(strVal.length <= 0)){
			strErr = strErrEmpty +" "+ ObjectCaption + "\n\    " ;
		}
		else if (l>MaxLength){
			strErr = ObjectCaption + " " + strErrExceed + " " + MaxLength + " " + strErrChar + "\n\    " ;  
		}
		else if (MinLength > 0){
			if (strVal.length < MinLength){
				strErr = ObjectCaption + " " + strErrLess + " " + MinLength + " " + strErrChar + "\n\    ";
			}
		}
		//else{
		//	for (var i = 0; i < strVal.length; i++){
		//		if (strVal.charAt(i) < '0' || strVal.charAt(i) > '9'){
		//			strErr = ObjectCaption + " should contain digits.\n\    "
		//		}
		//	}
		//} 
		
		if (strErr != ""){
			strErrMessage = strErrMessage + strErr;
			//if ((objFocus == undefined)||(objFocus == "")){
			if ((isUndefined(objFocus))||(objFocus == "")){
				objFocus = ObjectName
			}
		}		
	}else{return true;}
}

//*****************************************************************************
//5.This function is used to Check Textbox/Textarea/select box for the Phone 
//Numbers along with Empty and specific format.
//Note1:If Required is set to false,then it validates only if you enter in the input boxes.
//Note2:Current format suported are "nnn-nnn-nnnn" ,"(nnn)nnn-nnnn" and "nnnnnnnnnn"
//if you need your own format then alter the regexp of below function
//Ex:fnCheckPhoneNumbers("document.frmAddEdit.txtPhone","Phone No","nnn-nnn-nnnn",true);
//******************************************************************************/
function fnCheckPhoneNumbers(ObjectName,ObjectCaption,Format,Required){
	var strErr = ""
	var regexp = ""
	var strVal = "";
	var l;
	ObjectName=eval(ObjectName)
	if (ObjectName.type=='select-one'){
		strVal = (ObjectName.options.length>0)?ObjectName.options[ObjectName.selectedIndex].value:"";
	}else{
		strVal=ObjectName.value;
	}
	l=strVal.length;
	//var regexp = /^(\d{10}|\d{3}-\d{3}-\d{4}|\(\d{3}\)\d{3}-\d{4})$/;
	if (Required==true || (Required==false && l>0)){
		if (Format == "nnn-nnn-nnnn"){
			regexp = /^(\d{3}-\d{3}-\d{4})$/;
		}else if (Format == "(nnn)nnn-nnnn"){
			regexp = /^(\(\d{3}\)\d{3}-\d{4})$/;
		}else if (Format == "nnnnnnnnnn"){
			regexp = /^(\d{10})$/;
		}else if (Format == ""){
			regexp = /^(\(?\+?[0-9]*\)?)?[0-9_\- \(\)]*$/;  //A regular expression to match phone numbers, allowing for an international dialing code at the start and hyphenation and spaces that are sometimes entered.
			//regexp = /^(\+\d{1,3} ?)?(\(\d{1,5}\)|\d{1,5}) ?\d{3}|d{2} ?\d{0,7}?$/i ;
			Format = ". It may include numbers with + or - characters."
		} 
		if ((fnIsEmpty(strVal))||(strVal.length <= 0)){
			strErr = strErrEmpty +" "+ ObjectCaption + "\n\    " ;
		}else if ((regexp == "")||(regexp.test(strVal)==false)){
			//strErr = strErrEnter +" "+ ObjectCaption + " " + strErrIn + " " + Format + "\n\    " ;
			strErr = strErrEnter + " number in " + ObjectCaption + Format + "\n\    " ;
		}    
		if (strErr != ""){
			strErrMessage = strErrMessage + strErr;
			//if ((objFocus == undefined)||(objFocus == "")){
			if ((isUndefined(objFocus))||(objFocus == "")){
				objFocus = ObjectName
			}
		}		
	}else {return true;}
}

//*****************************************************************************
//6.This function is used to Check Textbox/Textarea/select box for the Date along 
//with Empty and specific format.
//Note1:If Required is set to false,then it validates only if you enter in the input boxes.
//Note2:Current format suported are "dd/mm/yyyy" and "mm/dd/yyyy"
//if you need your own format then alter the regexp of below function
//Ex:fnCheckDate("document.frmAddEdit.txtDate","11/12/2003","Date","dd/mm/yyyy",true);
//******************************************************************************/
function fnCheckDate(ObjectName,ObjectValue,ObjectCaption,Format,Required){
//	^\d{1,2}\/\d{1,2}\/\d{4}$ 
//  ^(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$ 
// The above expression validates dates in the US m/d/y format from 1/1/1600 - 12/31/9999. Checks for valid days for a given month. The days are validated for the given month and year. Leap years are validated for all 4 digits years from 1600-9999, and all 2 ... 
	var strErr = ""
	var regexp1 = ""
	var regexp2 = ""
	var regexp3 = ""
	var regexp4 = ""
	var da,dd,mm,yy,strLeft,strCenter,strLast
	var strVal = "";
	var l;
	ObjectName=eval(ObjectName)
	
	strVal=ObjectValue;

	l=strVal.length;
	da=strVal
	if ((Required==true) || (Required==false && l>0)){
		if ((Format == "dd/mm/yyyy")||(Format == "mm/dd/yyyy")){
			regexp1 = /^(\d{2}\/\d{2}\/\d{4})$/;
			regexp2 = /^(\d{1}\/\d{1}\/\d{4})$/;
			regexp3 = /^(\d{1}\/\d{2}\/\d{4})$/;
			regexp4 = /^(\d{2}\/\d{1}\/\d{4})$/;
		}else if ((Format == "dd-mm-yyyy")||(Format == "mm-dd-yyyy")){
			regexp1 = /^(\d{2}\-\d{2}\-\d{4})$/;
			regexp2 = /^(\d{1}\-\d{1}\-\d{4})$/;
			regexp3 = /^(\d{1}\-\d{2}\-\d{4})$/;
			regexp4 = /^(\d{2}\-\d{1}\-\d{4})$/;
		}else if ((Format == "dd.mm.yyyy")||(Format == "mm.dd.yyyy")){
			regexp1 = /^(\d{2}\.\d{2}\.\d{4})$/;
			regexp2 = /^(\d{1}\.\d{1}\.\d{4})$/;
			regexp3 = /^(\d{1}\.\d{2}\.\d{4})$/;
			regexp4 = /^(\d{2}\.\d{1}\.\d{4})$/;
		}
		
		if (regexp1 == ""){
			strErr = strErrEnter +" "+ ObjectCaption + " " + strErrIn + " " + Format + " format \n\    " ;
		}else if (regexp1.test(strVal)==true){
			strLeft=da.substr(0,2);
			strCenter=da.substr(3,2);
			strLast=da.substr(6,4);
		}else if (regexp2.test(strVal)==true){	
			strLeft="0" + da.substr(0,1);
			strCenter="0" + da.substr(2,1);
			strLast=da.substr(4,4);
		}else if (regexp3.test(strVal)==true){	
			strLeft="0" + da.substr(0,1);
			strCenter=da.substr(2,2);
			strLast=da.substr(5,4);
		}else if (regexp4.test(strVal)==true){	
			strLeft=da.substr(0,2);
			strCenter="0" + da.substr(3,1);
			strLast=da.substr(5,4);
		}else{
			strErr = strErrEnter +" "+ ObjectCaption + " " + strErrIn + " " + Format + " format \n\    " ;
		}
		
		if ((Format == "dd/mm/yyyy")||(Format == "dd-mm-yyyy")||(Format == "dd.mm.yyyy")){
			dd = strLeft
			mm = strCenter
			yy = strLast
		}else if ((Format == "mm/dd/yyyy")||(Format == "mm-dd-yyyy")||(Format == "mm.dd.yyyy")){
			dd = strCenter
			mm = strLeft
			yy = strLast
		}
		
		if (strErr == ""){
			if((parseInt(yy) < 1900)||(parseInt(yy) > 2079)){
				strErr = ObjectCaption + " " + strErrDate + "\n\    " ;
			}else if ((dd=="00" || mm=="00")){
				strErr = ObjectCaption + " " + strErrDate1 + "\n\    " ;
			}else if ((dd>31 || mm>12) || ((mm == 1 || mm == 3 || mm ==5 || mm == 7 || mm == 8 || mm == 10 || mm == 12) && dd > 31) || ((mm == 04 || mm == 06 || mm == 9 || mm == 11) && dd > 30)  || ((yy % 4) == 0 && mm==2 && dd > 29) || ((yy % 4) != 0 && mm==2 && dd > 28)){
				strErr = ObjectCaption + " " + strErrDate1 + "\n\    " ;
			}
			
		}
		if (strErr != ""){
			strErrMessage = strErrMessage + strErr;
			//if ((objFocus == undefined)||(objFocus == "")){
			if ((isUndefined(objFocus))||(objFocus == "")){
				objFocus = ObjectName
			}
		}			
	
	}
}


//*****************************************************************************
//7.This function is used to Check Textbox/Textarea/select box for the Empty,
//Maximum length,Minimum length and valid email.
//Note1:If Required is set to false,then it validates only if you enter in the input boxes.
//Note2:MinLength should be 0 when no minimum length validation required.
//Note3:Email address must be of form a@b.c 
//	  There must be at least one character before the @,There must be at least one character before and after the .
//	  The characters @ and . are both required
//Ex: fnCheckEmail("document.frmAddEdit.txtEmail","Email Address",50,0,true);	
//******************************************************************************/
function fnCheckEmail(ObjectName,ObjectCaption,MaxLength,MinLength,Required){
	var strErr = ""
	ObjectName=eval(ObjectName)
	var strVal = "";
	var l;
	ObjectName=eval(ObjectName)
	if (ObjectName.type=='select-one') {
		strVal = (ObjectName.options.length>0)?ObjectName.options[ObjectName.selectedIndex].value:"";
	}
	else {
		strVal=ObjectName.value;
	}
	l=strVal.length;
	txt=strVal;
	if (Required == true || (Required==false && l>0)) {
		if ((fnIsEmpty(strVal))||(strVal.length <= 0)) {
			strErr = strErrEmpty +" "+ ObjectCaption + "\n\    " ;
		}
		else if (l>MaxLength) {
			strErr = ObjectCaption + " " + strErrExceed + " " + MaxLength + " " + strErrChar + "\n\    " ;
	    }
	    else if (MinLength > 0) {
			if (strVal.length < MinLength) {
				strErr = ObjectCaption + " " + strLessThan + " " + MinLength + " " + strErrChar + "\n\    ";
			}
		}
		else {
			var i = 1;
			// look for @
		    while ((i < l) && (txt.charAt(i) != "@"))
			{
				 i++
			}
			if ((i >= l) || (txt.charAt(i) != "@")) {
				strErr = ObjectCaption + " " + strErrEmail +"\n\    " ;
			}
			else {
				i += 2;
			}	

			// look for .
			if(strErr =="") {
				while ((i < l) && (txt.charAt(i) != ".")) { 
					i++
				}
				// there must be at least one character after the .
				if ((i >= l - 1) || (txt.charAt(i) != ".")) {
					strErr = ObjectCaption + " " + strErrEmail1 +"\n\    " ;
				}
			 }
		}
		if (strErr != "") {
			strErrMessage = strErrMessage + strErr;
			if ((isUndefined(objFocus))||(objFocus == "")) {
				objFocus = ObjectName
			}
		}			 
	}
	else{return true;}
}

//*****************************************************************************
//8.This function is used to varifiy the password entered in two Textbox.
//Note: if CaseSencitive is true ,it verifiys for case in both text boxes
//Ex: fnVerifyPWD("document.frmEdit.txtPassword","Password","document.frmEdit.txtConfirmpassword","Confirm Password",true);	
//******************************************************************************
function fnVerifyPWD(ObjectName1,ObjectCaption1,ObjectName2,ObjectCaption2,CaseSensitive){
	var strErr = ""
	var strVal1 = "";
	var strVal2 = "";
	var l;
	ObjectName1=eval(ObjectName1)
	if (ObjectName1.type=='select-one'){
		strVal1 = (ObjectName1.options.length>0)?ObjectName1.options[ObjectName1.selectedIndex].value:"";
	}else{
		strVal1=ObjectName1.value;
	}
	ObjectName2=eval(ObjectName2)
	if (ObjectName2.type=='select-one'){
		strVal2 = (ObjectName2.options.length>0)?ObjectName2.options[ObjectName2.selectedIndex].value:"";
	}else{
		strVal2=ObjectName2.value;
	}	
	
	if (fnIsEmpty(strVal1)){
		strErr = strErrEmpty +" "+ ObjectCaption1 + "\n\    " ;
	}else if (fnIsEmpty(strVal2)){
		strErr = strErrEmpty +" "+ ObjectCaption2 + "\n\    " ;    
	}else if (CaseSensitive==false){
	   if ((strVal1.toUpperCase()) != (strVal2.toUpperCase())){
           strErr = ObjectCaption2 +" " + strErrNotMatch +" "+ ObjectCaption1 + "\n\    " ;    
      }
    }else if (strVal1 != strVal2){
		strErr = ObjectCaption2 +" "+ strErrNotMatch +" "+ ObjectCaption1 + "\n\    " ;    
    }
		if (strErr != ""){
			strErrMessage = strErrMessage + strErr;
			if ((objFocus == undefined)||(objFocus == "")){
				objFocus = ObjectName2
			}
		}
}

//*****************************************************************************
//9.This function is used to Handle the Javascript Error.
//*****************************************************************************
function ErrorHandler(message, url, lineno){
	strErrHandlerMessage = "An Unhandled Javascript Error has accured in the application"
	alert(strErrHandlerMessage);
	return true;
} 


//*****************************************************************************
//10.This function is used to Compare Dates
//******************************************************************************/
function fnCompareDates(ObjectName,ObjectValue, ObjectCaption,DateToCompare,ObjectCaption1,Operator)
{
	var strErr = "";
	var strVal = "";
	var FromArr, FromVar;
	var ToArr, ToVar
	var l;
	ObjectName=eval(ObjectName)
	
	strVal=ObjectValue;
		
	FromVar = strVal ;
	if (DateToCompare==""){
		ToVar = "";
	}else{
		ToVar = DateToCompare ;
	}
		
	FromArr = FromVar.split("/");
	ToArr = ToVar.split("/");
	
	FromVar = parseInt(FromArr[2]+FromArr[0]+FromArr[1]);
	ToVar = parseInt(ToArr[2]+ToArr[0]+ToArr[1]);
			
	if (Operator == ">"){
		if(FromVar > ToVar){
			strErr = ObjectCaption + " should not be greater than " + ObjectCaption1 +"\n\    " ;
		}
	}

	if (Operator == "<"){
		if(FromVar < ToVar){
			strErr = ObjectCaption + " should not be less than "+ ObjectCaption1 +"\n\    " ;
		}
	}
	
	if (Operator == "="){
		if(FromVar < ToVar){
			strErr = ObjectCaption + " should be equal to "+ ObjectCaption1 +"\n\    " ;
		}
	}
	
	if (Operator == "<="){
		if(FromVar <= ToVar){
			strErr = ObjectCaption + " should be less than or equal to "+ ObjectCaption1 +"\n\    " ;
		}
	}
	
	if (Operator == ">="){
		if(FromVar >= ToVar){
			strErr = ObjectCaption + " should be grater than or equal to "+ ObjectCaption1 +"\n\    " ;
		}
	}
	
	if (strErr != ""){
			strErrMessage = strErrMessage + strErr;
			//if ((objFocus == undefined)||(objFocus == "")){
			if ((isUndefined(objFocus))||(objFocus == "")){
				objFocus = ObjectName
			}
	}else{
		return true;
	}
}

//The below function Replaces all characters in 'strFind' to 'strReplace'
function Replace(strInput,strFind,strReplace) {
    var output = "";  // initialise output string
    for (var i=0; i < strInput.length; i++){
		if (strFind.indexOf(strInput.charAt(i)) == -1){
			output += strInput.charAt(i);
		}else{
			output += strReplace;
		}
    }
    return output;
}


//Function used in list screen to check all the list checkboxes
function CheckAll(formname,chkIndicator)
{
	var FormName = eval(formname);
	var CheckBoxChecked=true;
	var element;
	
	//when the chkall checkbox is clicked
	if (chkIndicator == 0) {
		for (var i=0;i<FormName.elements.length;i++)
		{
			var e = FormName.elements[i];
			if (e.name.substring(0,6) == 'chksel')
			{
					e.checked = FormName.chkall.checked ;
			}
		}
	}	
	else if (chkIndicator == 1) { 
		//when the chksel checkbox is clicked
		for(var i=0; i<FormName.elements.length; i++)
		{
			var e = FormName.elements[i]; 	
			if (e.name.substring(0,6)=='chksel')
			{
				if (FormName.elements[i].checked==0)
				{
					CheckBoxChecked=false;
					FormName.chkall.checked=0
					return true;
				}				 
			}
		}
		if (CheckBoxChecked==true)
		{
			FormName.chkall.checked=1
			return false;
		}
	}
}

//Function to check for delete option in lookup screens
function fnDeleteLookupData(frm) {
	var bolDel=true;
	var bolConfirm;
	dml=eval(frm);
	dml.hdDelete.value="NOT";
	iLen = dml.elements.length;
	for(var i=0; i<iLen; i++)
		if (dml.elements[i].name=='chksel')
			if (dml.elements[i].checked==1){
				bolDel=false;
				dml.hdDelete.value="OK";
			}
	if (bolDel)
		alert(strDeleteNoSelection);
	else {
		bolConfirm = confirm(strConfirmDelete);
		if (bolConfirm) 
			dml.submit();
		}
}