/* 
function isEmpty(field,label)
Author Amardeep Singh
Dated 30-08-2004.
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 = label +   " cannot be left blank."
	     alert(label)
	     field.focus()
	     return false;
	}
	else
    {
       return true;
    }
	       
}

//-------------------------------------------------------------------------------------------------------------------------------

/* 
function NameChk(field,label,boolEmpty,boolNum,splChar)
Author Amardeep Singh
Dated 30-08-2004.
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 the valid  " + label + " before submitting the form"
			 alert(label);
             field.focus();
             return false;		
	     }	
	   
	 }
	 return true;
}
//-------------------------------------------------------------------------------------------------------------------------------

/* 
function isEmail(field,label,boolEmpty) 
Author Amardeep Singh
Dated 30-08-2004.
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 = "Please enter the valid " + label 
		alert(label);
        field.focus();
        return false;
     }
     return true;
}

/*-------------------------------------------------------------------------------------------------------------------------------*/

/* 
function NumChk(field,label,boolEmpty)
Author: Amardeep Singh
Dated 30-08-2004
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 = label + " can conatin only numeric data"
			 alert(label);
             field.focus();
             return false;		
	     }
	 }
	return true;
}
 
//-------------------------------------------------------------------------------------------------------------------------------

/* 
function Trim(str)
Author Amardeep Singh
Dated 30-08-2004.
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 Amardeep Singh
Dated 30-08-2004.
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 Amardeep Singh
Dated 30-08-2004.
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 validAddress(field,label,boolEmpty)
Author Amardeep Singh
Dated 30-08-2004.
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 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)
Author Amardeep Singh
Dated 30-08-2004.
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 the valid " + label + " before submitting the form"
	             alert(label);
                 field.focus();
                 return false;		
	          }	
	   
	    }
	         
	return true;
}

