
var strInitialErrMessage,strErrEmpty,strErrExceed,strErrChar;
var strErrEnter,strErrIn,strErrDate,strErrDate1,strErrEmail,strErrEmail1,strErrNotMatch,strErrAnd; 
var strGreaterThan,strLessThan,strEqual,strLessThanEqual,strGreaterThanEqual;
var strErrMessage="";
var objFocus,strLanguage;




strInitialErrMessage = "--------------------------------------------------\n Please correct the following information \n--------------------------------------------------\n    ";
strErrEmpty = "Information should be entered in";
strField = "field";
strErrExceed = "should not exceed";
strErrChar = "characters";
strChar="Enter only characters for";
strErrEnter = "Enter";
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";
//For Date 
strLessThan = "should not be less than";
strGreaterThan = "should not be greater than";
strEqual = "should not be equal to";
strLessThanEqual = "should not be less than or equal to";
strGreaterThanEqual = "should not be greater than or equal to";
strList="Select the Item in"

//--------For Display Error messages----------------
function fnDisplayError(){
	if (strErrMessage != ""){
		alert(strInitialErrMessage + strErrMessage);
		if (!isUndefined(objFocus)){
			eval(objFocus).focus();
			//eval(objFocus).value="";
			if ((eval(objFocus).type == "text")||(eval(objFocus).type == "textarea")){
				eval(objFocus).select();
			}
		}
		objFocus = "";
		strErrMessage = ""
		return false;
	}	
}
//-------Function for cheking for blank spaces----------
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');
}


//This function is used to Check Textbox/Textarea/select box for the Numeric value along with Empty,Maximum length,Minimum length
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 +" "+ strField + "\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{
			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 ((isUndefined(objFocus))||(objFocus == "")){
				objFocus = ObjectName
			}
		}		
	}else{return true;}
}

//This function is used to Check Textbox/Textarea/select box for the Empty,Maximum length,Minimum length
function fnCheckString(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+" "+ strField +"\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\    ";
			}
		} 
		for(var i=0;i<l;i++)
   			{	
   				  				
				car = strVal.substring(i,i+1);
				c=car.charCodeAt();
				if(c==32)
    			{
    			strErr = ObjectCaption +" "+ strField +" cannot start with blank space "+ "\n\    " ;    				
				}
	   			else
	   			{
	   			   for(var i=0;i<l;i++)
   				   {	
						car = strVal.substring(i,i+1);
						c=car.charCodeAt()					
						
						if(((c>=33)&&(c<=44))||((c>=46) && (c<=64))||((c>=91) && (c<=96))||((c>=123) && (c<=19345671)))
    					{
    						strErr= strChar +" "+ strField +" "+ ObjectCaption + "\n\      ";
	   						
						}						
				
					}
				}
			}
		if (strErr != ""){
			strErrMessage = strErrMessage + strErr;
			if ((isUndefined(objFocus))||(objFocus == "")){
				objFocus = ObjectName;
			}
		}		
	}
	else{return true;}
}
//This function is used for checking email
function fnCheckEmail(ObjectName,ObjectCaption,MaxLength,MinLength,Required){
	var strErr = ""
	ObjectName=eval(ObjectName)
	var strVal = "";
	var l;
	ObjectCaption="Email"
	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 +" "+ strField + "\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 {
	    	for(var i=0;i<l;i++)
   			{	
   				  				
				car = strVal.substring(i,i+1);
				c=car.charCodeAt();
				if(c==32)
    			{
    			strErr = ObjectCaption +" "+ strField +" cannot start with blank space "+ "\n\    " ;    				
				}
				
			}
			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;}
	}

//--------validation for list box-------------
function fnCheckdropdown(ObjectName,ObjectCaption)
{
	var strErr = "";
	var strVal = "";
	var l;
	ObjectName=eval(ObjectName)
	
		strVal = ObjectName.selectedIndex;
		if (strVal<="0")
		{
			strErr = strList +" "+ ObjectCaption +" "+ strField + "\n\    " ;
		}
	
		if (strErr != "")
		{
			strErrMessage = strErrMessage + strErr;	
			if ((isUndefined(objFocus))||(objFocus == "")){
				objFocus = ObjectName.focus();		
			   //ObjectName.selectedIndex=0;
			}
		}	
			
	return true;
}

//--------Validate adress---------------------------------------
	function fnValidateAddress(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+" "+ strField +"\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\    ";
			}
		} 
		for(var i=0;i<1;i++)
   			{	
   				  				
				car = strVal.substring(i,i+1);
				c=car.charCodeAt();
				if(c==32)
    			{
    			strErr = ObjectCaption +" "+ strField +" cannot start with blank space "+ "\n\    " ;    				
				}
				}
		
		if (strErr != ""){
			strErrMessage = strErrMessage + strErr;
			if ((isUndefined(objFocus))||(objFocus == "")){
				objFocus = ObjectName;
			}
		}		
	}
	else{return true;}			
	}

//--------check date--------------------
function fnCheckDate(ObjectName,ObjectCaption,Date,Format,Required){
	strLanguage = "en";
	var strErr = "";
	var da,dd,mm,yy
	var strVal = "";
	var l;
	ObjectName=eval(ObjectName)
	if (ObjectName.type=='select-one'){
		//strVal = (ObjectName.options.length>0)?ObjectName.options[ObjectName.selectedIndex].value:"";
		strVal=Date
	}else{
		//strVal=ObjectName.value;
		strVal=Date
	}
	l=strVal.length;
	
	if ((Required==true) || (Required==false && l>0)){
		if ((Format == "dd/mm/yyyy")||(Format == "mm/dd/yyyy")){
			da=strVal.split("/");
			dd = da[0];mm = da[1];yy = da[2];		
		}else if ((Format == "dd-mm-yyyy")||(Format == "dd-MM-yyyy")){
			da=strVal.split("-");
			dd = da[0];mm = da[1];yy = da[2];		
		}else if ((Format == "dd.mm.yyyy")||(Format == "mm.dd.yyyy")){
			da=strVal.split(".");
			dd = da[0];mm = da[1];yy = da[2];		
		}else if (Format == "yyyy-mm-dd"){
			da=strVal.split("-");
			dd = da[2];
			mm = da[1];
			yy = da[0];	
		}
		
		//if (strVal == ""){
		//	strErr = strErrEnter +" "+ ObjectCaption + " " + strErrIn + " " + Format + "\n\    " ;
		//}
		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)){				
				if (strLanguage == "en"){
					strErr = ObjectCaption + " " + strErrDate1 + "\n\    " ;
				}else if (strLanguage == "no"){					
					strErr = strErrDate1 + " " + ObjectCaption + " " + strErrDate2 + "\n\    " ;
				}
			}
		}
		
		if (strErr != ""){
			strErrMessage = strErrMessage + strErr;
			if ((isUndefined(objFocus))||(objFocus == "")){
				objFocus = ObjectName
			}
		}			
	
	}
}

//------------compare two dates--------------
function fnCompareTwoDates(ObjectName,ObjectCaption,Date,DateToCompare,ObjectCaption1,Operator)
{
	var strErr = "";
	var strVal = "";
	var FromArr, FromVar;
	var ToArr, ToVar
	var l;
	strVal=Date;
			
	FromVar = strVal ;
	if (DateToCompare==""){
		ToVar = "";
	}else{
		ToVar = DateToCompare ;
	}
		
	FromArr = FromVar.split("-");
	ToArr = ToVar.split("-");
	
	//FromVar = parseInt(FromArr[0]+FromArr[1]+FromArr[2]);
	//ToVar = parseInt(ToArr[0]+ToArr[1]+ToArr[2]);
	FromVar = parseInt(FromArr[2]+FromArr[1]+FromArr[0]);
	ToVar = parseInt(ToArr[2]+ToArr[1]+ToArr[0]);
	

	if (Operator == ">"){
		if(FromVar > ToVar){
			strErr = ObjectCaption +" "+ strGreaterThan +" "+ ObjectCaption1 +"\n\    " ;			
		}
	}

	if (Operator == "<"){
		if(FromVar < ToVar){
			strErr = ObjectCaption +" "+ strLessThan +" "+ ObjectCaption1 +"\n\    " ;
		}
	}
	
	if (Operator == "="){
		if(FromVar < ToVar){
			strErr = ObjectCaption +" "+ strEqual +" "+ ObjectCaption1 +"\n\    " ;
		}
	}
	
	if (Operator == "<="){		
		if(FromVar <= ToVar){
			strErr = ObjectCaption +" "+ strLessThanEqual +" "+ ObjectCaption1 +"\n\    " ;
		}
	}
	
	if (Operator == ">="){
		if(FromVar >= ToVar){
			strErr = ObjectCaption +" "+ strGreaterThanEqual +" "+ ObjectCaption1 +"\n\    " ;
		}
	}
	
	if (strErr != ""){
			strErrMessage = strErrMessage + strErr;
			if ((isUndefined(objFocus))||(objFocus == "")){
				objFocus = ObjectName
			}
	}else{
		return true;
	}
}






//--------validation for Shipping address-------------
function validateShipDate(MDeliveryDate,selDeliveryDate)
{
		var strErr = "";
		var strVal = "";
		var l;
		Dedate=new Date(MDeliveryDate);
		Dtdeliverydate=new Date(selDeliveryDate);
		if (Dtdeliverydate<Dedate)
		{
				strErr ="Delivery date should be greater than or equal to "+MDeliveryDate+" (dd/mm/yyyy)"
			
		}
		if (strErr != "")
		{
			strErrMessage = strErrMessage + strErr;	
		}	
			
	return true;
}
function setFocus(object){
object.focus();


}