/* 
function isEmpty(field,label)
Author Harpreet Singh
Dated 07-06-2k1.
Parameters : field-> The name of the field whose value is to be checked
			 label-> The label with that field to give the appropiate error message
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 = "The field '" + label + "' cannot be left blank."
	     alert(label)
	     field.focus()
	     return false;
	}
	else
    {
       return true;
    }
	       
}

//-------------------------------------------------------------------------------------------------------------------------------

/* 
function nameChk(field,label,boolEmpty,boolNum,splChar)
Author Harpreet Singh
Dated 07-06-2k1.
Parameters : field-> The name of the field whose value is to be checked
			 label-> The label with that field to give the appropiate error message
			 boolEmpty-> It can have only two values - 1 or 0.
			 if it is 1 then the value passed can not be blank else it can be blank.
			 boolNum->It can have only two values - 1 or 0.
			 if it is 1 then the value passed can not have numeric values else it can have numeric values.
			 splChar-> Will contain the string of special characters allowed in the field.
Purpose : To check the format of each type of name.For Eg: FirstName,LastName etc 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 a valid name for the field '" + label + "' before continuing."
			 alert(label);
             field.focus();
             return false;		
	     }	
	   
	 }
	 return true;
}


//-------------------------------------------------------------------------------------------------------------------------------

/* 
function isEmail(field,label,boolEmpty) 
Author Harpreet Singh
Dated 08-06-2k1.
Parameters : field-> The name of the field whose value is to be checked
			 label-> The label with that field to give the appropiate error message
			 boolEmpty-> It can have only two values - 1 or 0.
			 if it is 1 then the value passed can not be blank else it can b blank.
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 = "The email address you entered does not seem valid (ie. '" + label + "')."
		alert(label);
        field.focus();
        return false;
     }   
}

 
 
//------------------------------------------------------------------------------------------------------------------------------- 

  /*
function validPhone(field,label,boolEmpty)
Author :  Harpreet Singh.
Dated : 11-06-2k1.
Parameters : field-> The name of the field whose value is to be checked
			 label-> The label with that field to give the appropiate error message
			 boolEmpty-> It can have only two values - 1 or 0.
			 if it is 1 then the value passed can not be blank else it can b blank.
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 a valid phone number for the field '" + label + "' before continuing."
	             alert(label);
                 field.focus();
                 return false;		
	          }	
	   
	    }
	         
	return true;
}

//------------------------------------------------------------------------------------------------------------------------------

/* 
function isCharsInBag (s, bag)
Author Harpreet Kaur
Dated 11-06-2k1.
Parameters : s-> The string to be checked.
			 bag-> The characters to be checked in that string.
Purpose : To check existence of bag string 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;
}

//------------------------------------------------------------------------------------------------------------------------------

/* 
function validChar(field, label)
Author Harpreet Singh
Dated 28-06-2001.
Parameters : field-> The name of the field whose value is to be checked
			 label-> The label with that field to give the appropiate error message
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 = "The entry for '" + label + "' does not seem valid."
			          alert(label);
                      field.focus();
                      return false;		
	             }	
	   
	        }
		}
		else
		{
			return false;
		}	
	  
	  return true;
}

//------------------------------------------------------------------------------------------------------------------------------

/* 
function numChk(field,label,boolEmpty)
Author Harpreet Singh
Dated 07-06-2k1.
Parameters : field-> The name of the field whose value is to be checked
			 label-> The label with that field to give the appropiate error message
			 boolEmpty-> It can have only two values - 1 or 0.
			 if it is 1 then the value passed can not be blank else it can b blank.
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 = "The field '" + label + "' can only contain numbers."
			 alert(label);
             field.focus();
             return false;		
	     }	
	   
	 }
}


//-------------------------------------------------------------------------------------------------------------------------------

/* 
function trim(str)
Author Harpreet Singh
Dated 27-08-2k1.
Parameters : str -> String which is to be trimmed
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)
Author Harpreet Singh
Dated 27-08-2k1.
Parameters : str -> String which is to be trimmed
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)
Author Harpreet Singh
Dated 27-08-2k1.
Parameters : str -> String which is to be trimmed
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 chkMaxLength(field,label,maxChar)
Author Harpreet Singh
Dated 10-18-2k1.
Parameters : field-> The name of the field whose value is to be checked
			 label-> The label with that field to give the appropiate error message
Purpose : To check if the first character of the field is a space.
Return Value : False if value is a space else True.
*/	

function chkMaxLength(field,label,maxChar)
{
	if(field.value.length > maxChar)
			{
				label=label + " cannot be greater than " + maxChar +" characters.";
				alert(label);
				field.focus();
				return false;
			}
			return true;

}
//------------------------------------------------------------------------------------------------

/* 
function chkMinLength(field,label,minChar)
Author Harpreet Singh
Dated 10-18-2k1.
Parameters : field-> The name of the field whose value is to be checked
			 label-> The label with that field to give the appropiate error message
Purpose : To check if the first character of the field is a space.
Return Value : False if value is a space else True.
*/	

function chkMinLength(field,label,minChar)
{
	if(field.value.length < minChar)
			{
				label=label + " cannot be less than " + minChar +" characters.";
				alert(label);
				field.focus();
				return false;
			}
			return true;

}
//------------------------------------------------------------------------------------------------

/* 
function chkPass(field,label,minChar)
Parameters : field-> The name of the field whose value is to be checked
			 label-> The label with that field to give the appropiate error message
			 minChar-> The minimum character
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 = "The entry for '" + label + "' seems invalid."
			          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;
}		
