/* -----Validation for numeric field  begins here-------- */


function isnumeric(number)
 {
    if (isNaN(number))
    
	return false;
    	
    else
    
	return true;
    
 }
/*-------- function to validate whether field is empty ------------*/

function empty(text)
{
    var=value
	with (text)
    {    	
		while(value.charAt(0)=='') {value=value.substring(1,value.length); }
		
		if (value==null || value=="")
	        return false;
		else 
		  {
			while(value.charAt(value.length-1)==' ')value=value.substring(0,value.length-1); 
			return true;
		  }	
		
   }
}

/* -----Function that does not allow decimal values in a numeric field  -------- */
function decimal(textval)
  {
	var inputStr=textval
		for ( var i=0; i < inputStr.length; i++) 
		{
			var oneChar=inputStr.substring(i,i+1)
			if ( (oneChar < "0" || oneChar > "9" ))
			  {
				
				return false
				break;
				
    			  }
		}
               return true;		
  }


/*------    Validation of number- to check whether it is positive. -----*/ 
 
function positive(number)
   {
     if (number<0)
   
	return false;
	
     else

    	return true;
    
   }


/*-----	A function to test for alphanumeric fields ------*/
function validalphanum(element)
{
	var str1= new RegExp("[^a-zA-Z_0-9 ]") //Matches all non-alphanumeric chars
	var str2= new RegExp("[a-zA-Z_0-9 ]")   //Matches alphanumeric chars

	//If the string has only  alpanumeric characters and no non alphanumeric characters
	//then return true.
	if(str2.test(element.value) && !str1.test(element.value))
	
		return true
	else
		return false
	
}

/*------ This Function eliminates leading whitespaces -------*/
function trim(str)
	{
		var val;
		val="";		
		for (i=0;i<str.length;i++)
		{			
			if ((str.charAt(i)!=' '))
			{				
				val+=str.charAt(i)					
			}
		}		
		return val;					
	}


/*------ This function checks for special characters.----------*/
function chkspecialchar(data) 

{   // checks if all characters 
       
	var invalid = "`!@#^$%()<>?{}[]~_'";     // are invalid characters
	var ok = 1; 
	var checktemp;
        
	for (var i=0; i<data.length; i++) 
	{
		checktemp = "" + data.substring(i, i+1);
		
		if (invalid.indexOf(checktemp) != "-1") 
		{
			
			return false; 
		}
	}
		return true;
} 
/*------ This function checks for special characters.----------*/
function chkspecialcharDoc(data) 

{   // checks if all characters 
       
	var invalid = "[]~";     // are invalid characters
	var ok = 1; 
	var checktemp;
	
	for (var i=0; i<data.length; i++) 
	{
		checktemp = "" + data.substring(i, i+1);
		
		if (invalid.indexOf(checktemp) != "-1") 
		{
			
			return false; 
		}
	}
		return true;
} 


/*------ This function checks for alphabets.----------*/
function chkalphabets(data) 

{   // checks if all characters 
       
	var invalid = "`!@#^&$&%()*<>?{}[]~_1234567890,.;/\|=+-";     // are invalid characters
	var ok = 1; 
	var checktemp;
	
	for (var i=0; i<data.length; i++) 
	{
		checktemp = "" + data.substring(i, i+1);
		
		if (invalid.indexOf(checktemp) != "-1") 
		{
			
			return false; 
		}
	}
		return true;
} 

function checksearch(data)
{
	var invalid = "[`%~";
	var ok=1;
	var checktem;
	for (var i=0; i<data.length; i++) 
	{
		checktemp = "" + data.substring(i, i+1);
		
		if (invalid.indexOf(checktemp) != "-1") 
		{
			
			return false; 
		}
	}
		return true;
}

/*------This function is used to check if the value in a text box has * or %.-------*/

function validatecharacters(txtValue,field)
{
	var intLength=0;
	intLength=txtValue.length;
	var intIndex;
	var txtNewValue="";
	var flag=true;
	for(intIndex=0;intIndex<intLength;++intIndex)
	{
		if ((txtValue.charAt(intIndex)=="*")	||(txtValue.charAt(intIndex)=="%"))
		{
			flag=false;
			break;
		}
	}

	if (flag==false) alert("* and % not allowed in "+field + ".");
	return flag;	
}

/*----- This function compares the given two strings-------*/


function strcmp(str1, str2)
	{
		var i
		str1 = toString(str1);
		str2 = toString(str2);
		if(str1.length != str2.length)
			{
			return false;
			}
		else
			{
				for (i=0; i<str1.length; i++)
				{
					if(str1.charCodeAt(i) != str2.charCodeAt(i))
						{
							return false;
						}
					else
						{
							return true;
						}
				}
			}
		
	}

/*----This function checks for length of a given string ------*/

function alertlen(item,len)
{
	alert(item+" cannot be more than "+len+" characters.");
}


/*------- Function checks for Single Quote and replaces with 2 single quotes --------*/

function encrypt(txtValue)
{
	var intLength=0;
	intLength=txtValue.length;
	var intIndex;
	var txtNewValue="";
	for(intIndex=0;intIndex<intLength;++intIndex)
	{
		if(txtValue.charAt(intIndex)=="'")		
			txtNewValue+="'";							
		txtNewValue+=txtValue.charAt(intIndex)	
		
	}
	return txtNewValue;
}

/*------- This function sets the focus to the given item --------*/
function focusitem(formname,txtname)
{
     document.forms[formname].elements[txtname].focus();
	 		   
}

/*------   A function to test for valid email ids.  ---------*/

function validateemail(formname,field)
{		    
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)"); //invalid mail id
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$"); // valid mail id
	//If the string has only characters in r2 and no characters in r1 then return true.
	if(!r1.test(eval("document."+formname+"." + field + ".value"))&& r2.test(eval("document."+formname+"." + field + ".value")))
		return true
	else
		return false
			
}


/*------- This function determines if the string passed in is a valid
	US zip code.  It accepts either ##### or #####-####.  If the
 string is valid, it returns true, else false.-------*/

/*function iszipcode(strzip)
{
	var s = new String(strzip);

	if (s.length <5)
	{
		// inappropriate length
		alert("Please enter a valid Zip Code ");
		return false;
	}
	for (var i=0; i < s.length; i++)
	{	
		if ((s.charAt(i) < '0' || s.charAt(s) > '9') && s.charAt(i) != '-')
		{
			alert("Please enter a valid Zip Code.");
			return false;
		}
	}
	return true;
}*/
function iszipcode(strzip)
{
	var invalid = "`!@#^&$&%()*<>?{}[]~_,.;/\|=+";     // are invalid characters
	var ok = 1; 
	var checktemp;
	var data;
	
	data=new String(strzip);
	if(data=="")
	{
	 alert("Please enter a valid Postal Code.");
	 return false; 
	}
	for (var i=0; i<data.length; i++) 
	{
		
		checktemp = "" + data.substring(i, i+1);
		
		if (invalid.indexOf(checktemp) != "-1") 
		{
			alert("Please enter a valid Postal Code.");
			return false; 
		}
		
	}
		return true;
}

/*-------- This function checks for a valid Phone No ---------*/

function validphone(value)
{  
 var inti;
 var data;
 data=new String(value);
	if (data.length <2)
	{
	    return false;
	}

				
for(inti=0;inti<value.length;inti++)
{	
		

	if (isNaN(parseInt(value.charAt(inti))))
	{
		if((value.charAt(inti)!="")&&(value.charAt(inti)!="(")&&(value.charAt(inti)!=")")&&(value.charAt(inti)!="-")&&(value.charAt(inti)!="+"))
		{	
			return false;
		}	
	}			
}
}
 function validatelength(value,len)
{
 var str 
 str=new String(value)
 
 if(str.length>len)
 return false; 

}

function validatephone(data) 

{   // checks if all characters 
       
	var invalid = "!@#^$%<>?{}[]~_'";     // are invalid characters
	var ok = 1; 
	var checktemp;
        
	for (var i=0; i<data.length; i++) 
	{
		checktemp = "" + data.substring(i, i+1);
		
		if (invalid.indexOf(checktemp) != "-1") 
		{
			
			return false; 
		}
	}
		return true;
} 


