/********************************************************************* 
* ValidateData.js.                                                     *
* Form validation functions for validating entire forms (onSubmit)     *
* or on-the-fly validation. (onBlur / onChange)                        *
* Copyright 2004 SunArc Technologies Pvt.Ltd.                          *
*********************************************************************/

// Functions for onSubmit form validation.
function BisEmail(val, req) {
// Check for a properly formatted email address.
   if ((val.value.length == 0) && (req == "R")) {
      alert("This field is required.");
      val.focus();
      val.select();
      return false;
   }
   var email = val.value.toLowerCase();
   if (val.value.length != 0) 
   {
		 // var emailformat = /^[^@\s]+@([-a-z0-9]+\.)+([a-z]{2}|com|net|edu|org|gov|mil|int|biz|pro|info|arpa|aero|coop|name|museum)$/;
		var emailformat = /^.+@.+\..{2,3}$/;
	  	if (!emailformat.test(email)) {
         alert("Please enter a valid email Id.");
         val.focus();
         val.select();
         return false;
      }
   }
	val.value=email;
   return true;
}

function BisNum(val, len, req) {
// Check for "0-9", ".", "-", and optionally length.
   if ((val.value.length == 0) && (req == "R")) {
      alert("This field is required.");
      val.focus();
      val.select();
      return false;
   }
   if (val.value.length != 0) {
      for (i = 0; i < val.value.length; i++){
         var ch = val.value.charAt(i);
         if ((ch >= "0" && ch <= "9") || (ch == ".") || (ch == "-")) {
            continue;
         } else {
            alert("Please enter numeric characters only.");
            val.focus();
            val.select();
            return false;
         }
      }
      /*if (len != "0") {
         if (val.value.length != len) {
            alert("This field must contain " + len + " NUMERIC characters.");
            val.focus();
            val.select();
            return false;
         }
      }*/
   }
   return true;
}
// function for checking that field is number
function BisNumber(val,req)
 {
  var dotcount=0; //count the in the field
// Check for "0-9", ".", "-", and optionally length.
   if ((val.value.length == 0) && (req == "R")) {
      alert("This field is required.");
      val.focus();
      val.select();
      return false;
   }
   if (val.value.length != 0) {
      for (i = 0; i < val.value.length; i++)
	   {
         var ch = val.value.charAt(i);
		 if(ch==".")
		 {
		   dotcount=dotcount+1;
		   if(dotcount==2)
		   {
            alert("Please enter valid numeric value greater than zero .");
            val.focus();
			val.value=1;
            val.select();
            return false;
		   }
		 }
         if ((ch >= "0" && ch <= "9") || (ch == "."))
		  {
            continue;
          } 
		  else
		   {
            alert("Please enter valid numeric value greater than zero .");
            val.focus();
			val.value=1;
			val.select();
            return false;
          }
      }
     
   }
   return true;
}
//=======================================================\\
// function for checking that field is integer
function BisInteger(val)
 {
  var dotcount=0; //count the in the field
   if (val.value.length != 0) {
      for (i = 0; i < val.value.length; i++)
	   {
         var ch = val.value.charAt(i);
         if ((ch >= "1" && ch <= "9")||(ch == "0" && i>=1))
		  {
            continue;
          } 
		  else
		   {
            alert("Please enter valid integer value greater than zero .");
            val.focus();
			val.value=1;
            val.select();
            return false;
          }
      }
   }
   return true;
}

//==============================================================\\

function CountLetter(FieldName, MaxLen, DisplayField)
{
	var CurLen = FieldName.value.length + 1;
	
	//if max letters has been entered, don't accept the character
		if(CurLen >= MaxLen)
		{	
			BisLen(FieldName,MaxLen);
			DisplayField.value = 'Limit over.';
			FieldName.value = FieldName.value.substring(0,MaxLen-1);
			return;
		}
		else
		{
			DisplayField.value = (MaxLen-CurLen+1) + ' characters more.';		
		}
		
}


function BisAlpha(val, len, req) {
// Check for "a-z", "A-Z", " ", and optionally length.
   if ((val.value.length == 0) && (req == "R")) {
      alert("This field is required.");
      val.focus();
      val.select();
      return false;
   }
   if (val.value.length != 0) {
      for (i = 0; i < val.value.length; i++) {
         var ch = val.value.charAt(i);
         if ((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z") || (ch == " ")) {
            continue;
         } else {
            alert("Please enter only alphabets");
            val.focus();
            val.select();
            return false;
         }
      }
      if (len != "0") {
         if (val.value.length != len) {
            alert("This field must contain " + len + " alphabets characters.");
            val.focus();
            val.select();
            return false;
         }
      }
   }
   return true;
}

//function to trim a string
function Trimmer(controlName) { 
	pVal = controlName.value;
    TRs=0; 
    for (i=0; i<pVal.length; i++) 
	{ 
        if (pVal.substr(i,1)==" ") 
		{
			TRs++;
		} 
		else 
			{break;} 
    } 

    TRe=pVal.length-1; 
    for (i=TRe; i>TRs-1;i--)
	{ 
        if (pVal.substr(i,1)==" ") 
		{
			TRe--;
		}
		else 
			{break;} 
    } 

    controlName.value=pVal.substr(TRs, TRe-TRs+1); 
} 
//end trim function

//-----------------------------------------------------
//function for date
//compare date
function DateComparison(date1, date2)
{
   var d1,d2,m1,m2,y1,y2;
    var fromdate=date1.split('-');
   var todate=date2.split('-');   
   
   d1=eval(fromdate[0]);
   d2=eval(todate[0]);
   m1=eval(fromdate[1]);
   m2=eval(todate[1]);
   y1=eval(fromdate[2]);
   y2=eval(todate[2]);
  
   //return true if second is greater  or equal to first date
   	if (y2 > y1)
	{
   		return true;
	}
	else
	{
		if(y1 > y2)
		{
			return false;
		}
		else
		{
			if(m2>m1)
			{
				return true;
			}
			else
			{
				if(m1>m2)
				{
					return false;
				}
				else
				{
					if(d2>=d1)
					{
						return true;
					}
					else
					{
						return false;
					}
				}
			}
		}
	}   
 }
//============================================================   
//validate the license form 
//============================================================
function validate_add_form(val)
{

var error=0;
var message='';

  if(val.productcode.value=="")
    {
        if(error==0)
		 val.productcode.focus();
       message=message+" Please enter the productcode. \n";
	   error=1;
	}
  
  if(val.productname.value=="")
    {
        if(error==0)
	       val.productname.focus();
       message=message+" Please enter the pro. \n";
	   error=1;
	}


 //display message on error
 if(error==1)
   {
    alert(message);
	return false;
   
   }
   if(val.domain.value!="")
    {
	  if(!DomainCheck(val.domain))
	   {
	    return false;
	   }
	
	}
//check that expiredate not be greater than current date 

  if(DateComparison(val.expiredate.value,val.serverdate.value)==true)
    {
       val.expiredate.focus();
      alert(" Please enter the expiry date greater than current date.  \n");
	  return false;
	}				
return true;
}   
//============================================================   
   
//======================Validate seller login=================   
function valid_login_seller(val)
{
var error=0;
var message='';
  if(val.loginid.value=="")
    {
       val.loginid.focus();
       message=message+" Please enter the user name. \n";
	   error=1;
	}
  if(val.password.value=="")
    {
        if(error==0)
		 val.password.focus();
       message=message+" Please enter the password. \n";
	   error=1;
	}
	if(error==1)
	{
	 alert(message);
	 return false;
	}
	return true;
}
//============================================================      
//=======FUNCTION FOR VALIDATE CHAGEPASSWORD FORM=============      
function Validate_Changepass_Form(val)
{
  var error=0;
  var message='';
  if(val.oldpassword.value=="")
    {
       val.oldpassword.focus();
       message=message+" Please enter the old password. \n";
	   error=1;
	}
  if(val.newpassword.value=="")
    {
        if(error==0)
		 val.newpassword.focus();
       message=message+" Please enter the new password. \n";
	   error=1;
	}
  if(val.confirmpassword.value=="")
    {
        if(error==0)
		 val.confirmpassword.focus();
       message=message+" Please confirm the new password. \n";
	   error=1;
	}
	//check that newpassword and confirm password values are same or not
	if(val.confirmpassword.value!="" && val.confirmpassword.value!=val.newpassword.value)
	 {
		if(error==0)
			 val.confirmpassword.focus();
		   message=message+" Confirm password not match with new password.\n";
		   error=1;
		 
	 }

	if(error==1)
	{
	 alert(message);
	 return false;
	}
	return true;

}


//============================================================         
 //function for redirecting the page   

function Validate_Criteria_Form(val)
{
  if(val.keyword.value=="" && val.expirefrom.value=="" && val.expireto.value=="") 
   {
     alert("Please enter/select atleast one criteria for listing.");
	 return false;
   }
   
  if(val.expirefrom.value!="" && val.expireto.value!="")
  {
     if(DateComparison(val.expirefrom.value,val.expireto.value)==false)
      {
        val.expirefrom.focus();
        alert(" Please enter a valid expiry date range.\n");
	    return false;
	  }
  }	  
   return true;

}
//THIS IS AddCategoryType() FUNCTION, TO ADD CATEGORY TYPE
function AddCategoryType(CatgType,  VarType)
{

			//remove all types from type select control
			VarType.length = 0;
			//select all type from array, related with  category
			var thecount = 0;
			var optcount = 0;

			if(CatgType != '')//if any hotel selected, then selected type  
			{
					
					while(thecount < CountArr)
					{
						if(TypeArray[thecount] == CatgType)
						{
							var option = new Option(NameArray[thecount], IdArray[thecount]);
							VarType.options[optcount] = option;
							optcount = optcount + 1;
						}
						thecount = thecount + 1;
					}//end while
					if(optcount ==0)
					{
						var option = new Option();
						VarType.options[0] = option;
						VarType.options[0].text='Value does not exist';
						VarType.options[0].value='0';
					}
			}//end if
			else
			{
					var option = new Option();
					VarType.options[0] = option;
					VarType.options[0].text='Please select';
					VarType.options[0].value='0';
			}
}
//END FUNCTION 

/********************************************************************************************
	THIS IS SetFieldLabel() FUNCTION, TO PUT VALUE OF SELECTED FIELD INTO FIELD LABEL
*********************************************************************************************/
function SetFieldLabel(SelectedField, FieldLabel1, FieldLabel2)
{
	var FieldValue = SelectedField.options[SelectedField.selectedIndex].text.toLowerCase() + " : ";

	if(FieldValue == '--Please select--')
	{
		FieldLabel1.value = 'value(s) : ';
		FieldLabel2.value = 'value : ';
	}
	else
	{
		FieldLabel1.value = FieldValue;
		FieldLabel2.value = FieldValue;
	}
}
/************************************************************
end function
************************************************************/
//============================================================         
   
//function for redirecting the page   

function CallPage(PageName)
{
	
	location.href = PageName;
}
//============================================================    
//function for pagination
//============================================================  

function DomainCheck(val)
{
 
// Check for a properly formatted url.
   
   var email = val.value.toLowerCase();
   var checkemail="";
    if (val.value.length >0) 
     {
	    if(val.value.substr(0,4)=='www.')
		 {
		   alert("Please don't prefix * www * in domain name ");
		    val.focus();
            val.select();
		    return false;
		 }
		
		//var emailformat = /^(([-a-z0-9])+(.com|.net|.edu|.org|.gov|.mil|.int|.biz|.pro|.info|.arpa|.aero|.coop|.name|.museum|(\.{1}+[a-z]{2})+((\.{1}+[a-z]{2,3})))+(|(\.{1}+[a-z]{2,3})))$/;
		//var emailformat = /^(([-a-z0-9])+(|(\.{1}+[a-z]{2})|.com|.net|.edu|.org|.gov|.mil|.int|.biz|.pro|.info|.arpa|.aero|.coop|.name|.museum)+(|(\.{1}+[a-z]{2,3})))$/;
		//var emailformat = /^(([-a-z0-9])+(|.com|.net|.edu|.org|.gov|.mil|.int|.biz|.pro|.info|.arpa|.aero|.coop|.name|.museum))$/;
		var emailformat = /^.+\..{2,3}$/;
	  	if (!emailformat.test(email)) {
         alert("Please enter a valid domain name.");
         val.focus();
         val.select();
         return false;
      }
   }
	val.value=email;
   return true;


}

//=====================================================================================

//=============================================================================================================================
//THIS FUNCTION POSTS FORM FOR NEXT PAGE ....
//THIS FUNCTION POSTS FORM FOR NEXT PAGE ....
//=============================================================================================================================

function NextPage()
{
	document.frmlist.PageCounter.value = eval(document.frmlist.PageCounter.value) + 1  ;
	
	document.frmlist.submit();
}
//=============================================================================================================================


//=============================================================================================================================
//THIS FUNCTION POSTS FORM FOR PREVIOUS PAGE ....
//THIS FUNCTION POSTS FORM FOR PREVIOUS PAGE ....
//=============================================================================================================================

function PrePage()
{
	document.frmlist.PageCounter.value = eval(document.frmlist.PageCounter.value) - 1  ;
	document.frmlist.submit();
}
//=============================================================================================================================


//=============================================================================================================================
//THIS FUNCTION POSTS FORM FOR SPECIFIED PAGE NUMBER....
//THIS FUNCTION POSTS FORM FOR SPECIFIED PAGE NUMBER....
//=============================================================================================================================
function NextPageLink(val)
{
	document.frmlist.PageCounter.value = eval(val);
	document.frmlist.submit();
}
//=============================================================================================================================

//=============================================================================================================================
//THIS FUNCTION POSTS FORM FOR ORDER BY....
//THIS FUNCTION POSTS FORM FOR ORDER BY....
//=============================================================================================================================
function OrderPage(OrderBy)
{
	document.frmlist.PageCounter.value = eval(document.frmlist.PageCounter.value);
	if(document.frmlist.orderby.value.search(OrderBy)<0)
		document.frmlist.orderby.value = OrderBy;
	else
	{
		if(document.frmlist.orderby.value.search(' desc')<0)
			document.frmlist.orderby.value = OrderBy+' desc';
		else
			document.frmlist.orderby.value = OrderBy;
	}
	//alert(document.frmlist.orderby.value );
	document.frmlist.submit();
}
//=============================================================================================================================


//=============================================================================================================================
//THIS FUNCTION POSTS FORM FOR VIEW/EDIT/BLOCK....
//THIS FUNCTION POSTS FORM FOR VIEW/EDIT/BLOCK....
//=============================================================================================================================

function CheckOne(postAt,memId)
{
	document.frmlist.ProductId.value = memId;
	document.frmlist.action=postAt;
//	document.frmlist.PageCounter.value = 0;
	document.frmlist.submit();
}
//=============================================================================================================================

function OrderFunction(orderstr,type)
{
	//alert(orderstr + type);
    document.frmlist.order.value = "order by " + orderstr +" " + type ;
	document.frmlist.submit();
}


/********************************************************************************************
	THIS IS SetSelectedValue() FUNCTION, TO PUT VALUE OF SELECTED FIELD INTO ANOTHER FIELD
*********************************************************************************************/
function SetSelectedValue(SelectedField, AnotherField)
{
	AnotherField.value = SelectedField.options[SelectedField.selectedIndex].text;
}
/************************************************************
end function
************************************************************/
/********************************************************************************************
	THIS IS SetFieldLabel() FUNCTION, TO PUT VALUE OF SELECTED FIELD INTO FIELD LABEL
*********************************************************************************************/
function SetFieldLabel(SelectedField, FieldLabel1, FieldLabel2)
{
	var FieldValue = SelectedField.options[SelectedField.selectedIndex].text.toLowerCase() + " : ";

	if(FieldValue == 'please select : ')
	{
		FieldLabel1.value = 'value(s) : ';
		FieldLabel2.value = 'value : ';
	}
	else
	{
		FieldLabel1.value = FieldValue;
		FieldLabel2.value = FieldValue;
	}
}
/************************************************************
end function
************************************************************/
//=========================================================================================\\
//========HELP MESSAGE ON DATE TEXTBOX FOR DATE FORMAT=====================================\\
function OnDateBoxFocus(theField)
{
 if(theField.value=="DD-MM-YYYY" || theField.value=="dd-mm-yyyy")
 {
  theField.value="";
 }

}
//=========================================================================================\\

function OrderPageQuote(OrderBy)
{
	document.mycheckfrm.PageCounter.value = eval(document.mycheckfrm.PageCounter.value);
	if(document.mycheckfrm.orderby.value.search(OrderBy)<0)
		document.mycheckfrm.orderby.value = OrderBy;
	else
	{
		if(document.mycheckfrm.orderby.value.search(' desc')<0)
			document.mycheckfrm.orderby.value = OrderBy+' desc';
		else
			document.mycheckfrm.orderby.value = OrderBy;
	}
	//alert(document.frmlist.orderby.value );
	document.mycheckfrm.submit();
}

//display current time
function todayTime()
{
	var dt=new Date();
	var todaytime;
	var hours,minutes,seconds;
	hours=eval(dt.getHours());	minutes=eval(dt.getMinutes());	seconds=eval(dt.getSeconds());
	if(hours > 12)
	{
		hours-=12;
	 	todaytime=hours+":"+minutes+":"+seconds+" pm";
	}
	else	
		todaytime=hours+":"+minutes+":"+seconds+" am";
	return(todaytime);		
}
