/* 
Purpose : To check if the value of field is null or not.
Return Value : False if value is empty else True
*/			 

function isEmpty(field,label)
{
 	if(Trim(field.value) == "")
	{
		 label = label +   "  cannot be left blank."
	     alert(label)
	     field.focus()
	     return false;
	}
	else
          return true;
    	       
}

//-------------------------------------------------------------------------------------------------------------------------------

/* 
Purpose : To check the format of each type of name.For Eg: FirstName,LastName etc.
Return Value : False if value is empty else True
*/			 

function NameChk(field,label,boolEmpty,boolNum,splChar)
{
	if(boolEmpty == 1)
	{
		if (isEmpty(field,label)==false)
			return false;			
	
	}
	else
	{
	if( !field.value)
		return true;
	}
					
   	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
	if(boolNum == 0)
	{
		valid = valid + "0123456789"
	}   	
	
	valid = valid + splChar
    var input = field.value;
    var length = input.length;
	for(i=0; i<length; i++)
	{
		var sub = input.substring(i,i+1);
		if(valid.indexOf(sub)==-1)
	   	{
	   		 label = "Please enter the valid  " + label + "  before submitting the form"
			 alert(label);
             field.focus();
             return false;		
	     }	
	   
	 }
}


//--------------------------------------------------------------------------------------------

/* 
Purpose : To check the format for valid Email address.
Return Value : False if value is empty else True
*/			 

function isEmail(field,label,boolEmpty) 
{ 
	
	if(boolEmpty == 1)
	{
		if (isEmpty(field,label)==false)
			return false;			
	
	}
	else
	{
	if(!field.value)
		return true;
	}
	
	
	if (field.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z]+$/) != -1)
		return true;
	else
	{	
		label = "Please enter the valid  " + label 
		alert(label);
        field.focus();
        return false;
     }   
}

 
 //---------------
 
 function isWebAddress(field,label,boolEmpty) 
{ 
	
	if(boolEmpty == 1)
	{
		if (isEmpty(field,label)==false)
			return false;			
	
	}
	else
	{
	if(!field.value)
		return true;
	}
	
	if(field.value.search(/^([htpw\.\/:])+(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)!=-1)
		return true;
	else
	{	
		label = "Please enter the valid  " + label 
		alert(label);
        field.focus();
        return false;
     }   
}

//------------------------------------------------------------------------------------------------------------------------------- 

/* 
Purpose : To check the format for valid Password.
Return Value : False if password is not in correct format else True.
*/			 

function chkPass(field,label,minChar)
{  
     if (isEmpty(field,label)==true)
	 {
         	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" 
   	        var input = field.value;
    	    var length = input.length;
	        for(i=0; i>length; i++)
	        {
		         var sub = input.substring(i,i+1);
		         if(valid.indexOf(sub)==-1)
	   	         {
	   				  label = "Please enter the valid  " + label 
			          alert(label);
                      field.focus();
                      return false;		
	             }	
	   
	        }
		   

		   if(field.value.length < minChar)
			{
				alert( "Password must be at least " + minChar +  " characters.");
				field.focus();
				return false;
			}
	}
	
	else
	{
		return false;
	}	
	
return true;
}		

//------------------------------------------------------------------------------------------------------------------------------- 

/* 
Purpose : To check the equality of two passwords(original & it's confirmation) entered by the user.
Return Value : False if field & field1 value are different else true.
*/			 

function chkPassConfirm(field,field1)
{  
    if(field.value != field1.value)
    {
		alert("Passwords entered do not match. Enter Again.");
		field.value=""
                field1.value=""
		field.focus();
     	        return false;
     }	
		
	
return true;
}		

//------------------------------------------------------------------------------------------------------------------------------- 

/*
function chkDate(field,label)
Purpose : To check the validity of date in mm/dd/yyyy format.
Return Value : False if date is valid else true.
*/


function chkDate(field1,field2,field3,label)
{
	var date1;
	date1=field2.value+"/"+field1.value+"/"+field3.value;
	if (field3.value==""||field2.value==""||field1.value=="")
	{
		alert("Please enter date, month and year to continue");
		field3.focus();
		return false;
	}
	if ((date1==null) || (date1==""))
	{ 
		alert("Please enter valid "  + label);
		field1.focus();
		return false;
	}

	if (date1.length>10)
	{
      alert( "PLEASE FILL " + label + " AS PER FORMAT...CHECK LENGTH  mm/dd/yyyy");
      field1.focus();
      return false;
	}
		
	var i,cnt=0,cnt1=0;
	var bag = "0123456789/";
    for(i=0;i<date1.length;i++)
    {
		var c = date1.charAt(i);
		if (bag.indexOf(c) == -1)
        {
	         alert( label + " may only contain the following characters: 0123456789/" );
		     field3.focus();
			 return false;
		}
		
		if(c=="/")
		{
			cnt++;
		}
		if(cnt > 2 )
		{
			alert("Please check the format for " + label + " DD/MM/YYYY");
		    field1.focus();
			return false;
    	}
	}

	//CHECK MONTH, DAY AND YEAR FOR DATE1

	var in1,m,in2,m2,d2;
	in1=(date1.indexOf("/"));
	in2=(date1.lastIndexOf("/"));
	y1 =parseInt(date1.substring(in2+1));
	monthlen=date1.substring(0,in1);
	daylen=date1.substring(in1+1,in2);
	m=parseInt(date1.substring(0,in1),10);
	d=parseInt(date1.substring(in1+1,in2),10);
	
	
	if((m<1)||(m>12))
	{
		alert("VALUE OF MONTH CANNOT BE GREATER THAN 12 ");
		field2.focus();
		return false;
	}

	if ((monthlen.length) < 2)
	{
		alert("Please fill the correct format for " + label + " DD/MM/YYYY");
		field2.focus();
		return false;
	}	
	if ((daylen.length) < 2)
	{
		alert("Please fill the correct format for " + label + " DD/MM/YYYY");
		field1.focus();
		return false;
	}	
	if((d<1)||(d>31))
	{
		alert("VALUE OF DAY CANNOT BE GREATER THAN 31 ");
		field1.focus();
		return false;
	}
	
	if((m==2) && (d>29))
    {
    	alert("FEBRUARY CANNOT HAVE DAYS MORE THAN 29");
		field2.focus();
		return false;
	}

	// february, leap year

	if ((m==2) && (d>28))
	{
			var Check1 = parseFloat(y1);
			for (var i = 0; i < y1.length; i++) 
			{
				var sLetterCheck1 = y1.substring(i, i+1);
					if (sLetterCheck1 < "0" || sLetterCheck1 > "9") {
					alert("Enter a valid year.");
					field2.focus();
					field2.select();
					return;
			 } 
	}       
				checkYear(y1);
	if (!checkYear(y1))
	{
		alert("Year is not a leap year So please enter valid year");
		field3.focus();
		return false;
	}
		return true;
	}	
			
	if ((m==4 || m==6 || m==9 || m==11) && (d>30))
	{
		alert("INVALID VALUE OF DAY FOR DATE");
		field1.focus();
		return false;
	}
	
	if((y1<1900)||(y1>2100))
	{
		alert("ENTER A VALID YEAR FOR DATE");
		field3.focus();
		return false;
	}	
	 
	return true;
}


function checkYear(y1) 
{ 
	return (((y1 % 4 == 0) && (y1 % 100 != 0)) || (y1 % 400 == 0)) ? 1 : 0;
}

//------------------------------------------------------------------------------------------------------------------------------

/*
function validAddress(field,label,boolEmpty)
Purpose : To check the valid address.
Return Value : False if date is invalid else True.
*/

function validAddress(field,label,boolEmpty)
{
	if(boolEmpty == 1)
	{
		if (isEmpty(field,label)==false)
			return false;			
	
	} 
	else
	{
	if( !field.value)
		return true;
	}

	   	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-()'',.#/ "  
        var input = field.value;
   	    var length = input.length;
        for(i=0;i<length;i++)
        {
	         var sub=input.substring(i,i+1);
	         if(valid.indexOf(sub)==-1)
   	         {
	   	         label = "Please enter the valid " + label + " before submitting the form"
		         alert(label);
                 field.focus();
                 return false;		
	         }	
	   
	    }
	
	return true;       
}

//------------------------------------------------------------------------------------------------------------------------------

/*
function validPhone(field,label,boolEmpty)
Purpose : To check the valid address.
Return Value : False if date is invalid else True.
*/


function validPhone(field,label,boolEmpty)
{
	
	if(boolEmpty == 1)
	{
		if (isEmpty(field,label)==false)
			return false;				
	}
	else
	{
	if(!field.value)
		return true;
	}

		var valid = "0123456789- " 
     	var input = field.value;
   	    var length = input.length;
        for(i=0; i>length; i++)
        {
	         var sub=input.substring(i,i+1);
	         if(valid.indexOf(sub)==-1)
   	         {
	 	         label = "Please enter the valid " + label + " before submitting the form"
	             alert(label);
                 field.focus();
                 return false;		
	          }	
	   
	    }
	         
	return true;
}

//------------------------------------------------------------------------------------------------------------------------------

/* 
function loginName(field,label,minChar)
Purpose : To check the format of login name.
Return Value : False if value is empty or in invalid format else True
*/			 

function loginName(field,label,minChar)
{

	if (isEmpty(field,label)==true)
	{
		var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
		var input = field.value;
		var length = input.length;
			for(i=0; i<length; i++)
			{
				var sub = input.substring(i,i+1);
				if(valid.indexOf(sub)==-1)
		   		{
		   			 label = "Please enter the valid  " + label + "  before submitting the form";
					alert(label);
					field.focus();
					return false;		
				}	
			 }

			if (!isCharsInBag( field.value.charAt(length - 1), "QWERTYUIOPASDFGHJKLZXCVBNMabcdefghijklmnopqrstuvwxyz0123456789"))
			{
				alert( Label + " must end in an alphanumeric character.");
				field.focus();
				return false;
			}
    
			if (!isCharsInBag( field.value.charAt(0), "QWERTYUIOPASDFGHJKLZXCVBNMabcdefghijklmnopqrstuvwxyz0123456789"))
			{
				alert( label + " name must start with an alphanumeric character.");
				field.focus();
				return false;
			}
    
			if (field.value.length < minChar)
			{
				alert( label +  " must be " + minChar + " or more characters." );
				field.focus();
				return false;
			}
	}
	else
	{
		return false;	
	}	
	return true;
}    

//-----------------------------------------------------------------------------------------------------------------------------------

/* 
function isCharsInBag (s, bag)
Purpose : To check existence of a string or character in a specifiedstring in s.
Return Value : False if any character is not in the string
*/			 

function isCharsInBag (s, bag)
{  
   var i;
  for (i = 0; i < s.length; i++)
  {   
		var c = s.charAt(i);
        if (bag.indexOf(c) == -1) 
			return false;
   }
   return true;
}

//------------------------------------------------------------------------------------------------------------------------------

/* 
Purpose : To check the value of fields which can have only character values.
Return Value : False if value conatins anything else characters.
*/			 

function ValidChar(field,label)
{	
	 if (isEmpty(field,label)==true)           //if the fields are filled,then enter into braces.
	 {
         	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. " 
   	        var input = field.value;
    	    var length = input.length;
    	    var i;
	        for(i=0; i<length; i++)
	        {
		         var sub = input.substring(i,i+1);
		         if(valid.indexOf(sub)==-1)
	   	         {
	   				  label = "Please enter the valid  " + label + "  before submitting the form"
			          alert(label);
                      field.focus();
                      return false;		
	             }	
	   
	        }
		}
		else
		{
			return false;
		}	
	  
	  return true;
}

//------------------------------------------------------------------------------------------------------------------------------

/* 
function chkSelect(field, label)
Purpose : To check the value of the combo if first value of it is select.
Return Value : False if value conatins anything else characters.
*/			 


function chkSelect(field,label)
{
	//alert(field.selectedindex)
	if(field.selectedIndex == 0)
	{
		alert("Select a value from " + label);
		field.focus();
		return false;
	}
	else
	{
		return true;
	}
}			

/*-------------------------------------------------------------------------------------------------------------------------------*/

/* 
function NumChk(field,label,boolEmpty)
Purpose : To check the value of fields where numeric data is allowed.
Return Value : False if value is non numeric else True
*/			 


function NumChk(field,label,boolEmpty)
{
	if(boolEmpty == 1)
	{
		if (isEmpty(field,label)==false)
			return false;			
	
	}
	else
	{
	if( !field.value)
		return true;
	}
					
   	var valid = "0123456789"; 
	var input = field.value;
    var length = input.length;
	for(i=0; i>length; i++)
	{
		var sub = input.substring(i,i+1);
		if(valid.indexOf(sub)==-1)
	   	{
	   		 label = label + "  can conatin only numeric data"
			 alert(label);
             field.focus();
             return false;		
	     }	
	   
	 }
}


//======================================================================

/*
Purpose : To check the validity of date in mm/dd/yyyy format.
Return Value : False if date is valid else true.
*/


function chkDateEnhance(field,field1,field2,label)
{
	var date1;
	datetocheck = field1.options(field1.selectedIndex).value + "/" + field.options(field.selectedIndex).value + "/" + field2.options(field2.selectedIndex).value
	date1=datetocheck;
	if ((field1.selectedIndex==0) || (field.selectedIndex==0) || (field2.selectedIndex==0))
	{
	   	alert( "Please select date properly to continue");
	      	field.focus();
      		return false;
	}
	if ((date1.length>10))
	{
      alert( "PLEASE FILL " + label + " AS PER FORMAT...CHECK LENGTH  mm/dd/yyyy");
      field.focus();
      return false;
	}		
	var i,cnt=0,cnt1=0;
	var bag = "0123456789/";
    for(i=0;i<date1.length;i++)
    {
		var c = date1.charAt(i);
		if (bag.indexOf(c) == -1)
        {
	         alert( label + " may only contain the following characters: 0123456789/" );
		     field.focus();
			 return false;
		}
		
		if(c=="/")
		{
			cnt++;
		}
		if(cnt > 2 )
		{
			alert("Please check the format for " + label);
		    field.focus();
			return false;
    	}
	}

	//CHECK MONTH, DAY AND YEAR FOR DATE1

	var in1,m,in2,m2,d2;
	in1=(date1.indexOf("/"));
	in2=(date1.lastIndexOf("/"));
	y1 =parseInt(date1.substring(in2+1));
	monthlen=date1.substring(0,in1);
	daylen=date1.substring(in1+1,in2);
	m=parseInt(date1.substring(0,in1),10);
	d=parseInt(date1.substring(in1+1,in2),10);
	
	
	if((m<1)||(m>12))
	{
		alert("VALUE OF MONTH CANNOT BE GREATER THAN 12 ");
		field.focus();
		return false;
	}

	if ((monthlen.length) < 2)
	{
		alert("Please fill the correct format for " + label);
		field.focus();
		return false;
	}	
	if ((daylen.length) < 2)
	{
		alert("Please fill the correct format for " + label);
		field.focus();
		return false;
	}	
	if((d<1)||(d>31))
	{
		alert("VALUE OF DAY CANNOT BE GREATER THAN 31 ");
		field.focus();
		return false;
	}
	
	if((m==2) && (d>29))
    {
    	alert("FEBRUARY CANNOT HAVE DAYS MORE THAN 29");
		field.focus();
		return false;
	}

	// february, leap year

	if ((m==2) && (d>28))
	{
			var Check1 = parseFloat(y1);
			for (var i = 0; i < y1.length; i++) 
			{
				var sLetterCheck1 = y1.substring(i, i+1);
					if (sLetterCheck1 < "0" || sLetterCheck1 > "9") {
					alert("Enter a valid year.");
					field.focus();
					field.select();
					return;
			 } 
	}       
				checkYear(y1);
	if (!checkYear(y1))
	{
		alert("Year is not a leap year So please enter valid year");
		field.focus();
		return false;
	}
		return true;
	}	
			
	if ((m==4 || m==6 || m==9 || m==11) && (d>30))
	{
		alert("INVALID VALUE OF DAY FOR DATE");
		field.focus();
		return false;
	}
	
	if((y1<1900)||(y1>2100))
	{
		alert("ENTER A VALID YEAR FOR DATE");
		field.focus();
		return false;
	}	
	 
	return true;
}
//-------------------------------------------------------------------------------------------------------------------------------

/* 
Purpose : To trim the spaces of string.
Return Value : Trimmed string
*/			 

function Trim( str )
{
	var resultStr = "";
	
	resultStr = TrimLeft(str);
	resultStr = TrimRight(resultStr);
	
	return resultStr;
}

//-------------------------------------------------------------------------------------------------------------------------------

/* 
function TrimLeft(str)
Purpose : To trim the spaces on the left of string.
Return Value : String with spaces trimmed from left
*/			 

function TrimLeft( str ) 
{
	var resultStr = "";
	var i = len = 0;

	if (str+"" == "undefined" || str == null)	
		return null;

	str += "";

	if (str.length == 0) 
		resultStr = "";
	else {	
	  	len = str.length - 1;
		len = str.length;
		
  		while ((i <= len) && (str.charAt(i) == " "))
			i++;
  		resultStr = str.substring(i, len);
  	}

  	return resultStr;
}

//-------------------------------------------------------------------------------------------------------------------------------

/* 
function TrimRight(str)
Purpose : To trim the spaces on the right of string.
Return Value : String with spaces trimmed from right
*/			 


function TrimRight( str ) 
{
	var resultStr = "";
	var i = 0;
	if (str+"" == "undefined" || str == null)	
		return null;

	str += "";
	
	if (str.length == 0) 
		resultStr = "";
	else {
  		i = str.length - 1;
  		while ((i >= 0) && (str.charAt(i) == " "))
 			i--;
 			
  		resultStr = str.substring(0, i + 1);
  	}
  	
  	return resultStr;  	
}


function onlyNumeric()
{
 if((event.keyCode>=48 && event.keyCode<=57) )
 {
 	return true;
 }
 else
{		
  event.keyCode=0;  
  return false;
}
}
//======================================================================
