/**
* The function isEmail checks for the validity of the email address
* the email address should be in the proper format
* ie., yourname@somebody.something. if email is not proper it alerts
* to enter the correct mail address.
*/
function isEmail(mail)
{
	if(mail!="")
	{
		email=mail
		len=mail.length-1
		s='@'
		p='.'
		s1=email.indexOf(s)
		s2=email.indexOf(p)
		if((s1<1)||(s1==len)||((s2-s1)==1)||(s2==len))
		{
  		    // alert("Please Enter valid EMail id ")
		     return false
		}
	}
	return true
}

function isvalidemail(email)

{
 var emailStr = trim(email);  	 
 var emailPat = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/; 
 var matcharray	 = emailStr.match(emailPat); 
 // To validate the email	 
      if (matcharray == null)
     	return false;    
return true;
     	 
}


/**
* The function isPhoneNumber checks for the validity of a phone number
* which should allow all alphanumeric characters and some other characters
*/
function isPhoneNumber(form1)
{
	var len,str,str1,i
	len=form1.length
	str=form1

        str1="extensionEXTENSION0123456789-,.() "
	for(i=0;i<len;i++)
	{
		if((str1.indexOf(str.charAt(i)))==-1)
		{
    	        return false
		}
	}
	return true
}

/**
* The function isName checks for the validity of the entered fields
* which can be letters or space or dot only.
*/
function isName(form1)
{
	var len,str,str1,i
	len=form1.length
	str=form1

        str1="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz. "
	for(i=0;i<len;i++)
	{
		if((str1.indexOf(str.charAt(i)))==-1)
		{
    	        return false
		}
	}
	return true
}


/**
* The function isAlphaNumeric checks for the validity of the entered fields
* which should alphanumeric only. Other wise it alerts for proper valid data.
*/
function isAlphaNumeric(form1)
{
	var len,str,str1,i
	len=form1.length
	str=form1

        str1="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-,_. \n "
	for(i=0;i<len;i++)
	{
		if((str1.indexOf(str.charAt(i)))==-1)
		{
    	        return false
		}
	}
	return true
}

/**
* The function isAlphaNumeric checks for the validity of the entered fields
* which should alphanumeric ,comma,hiphen,fullstop,single quote and space only. Other wise it alerts for proper valid data.
*/
function isAlphaNumericQuote(form1)
{
	var len,str,str1,i
	len=form1.length
	str=form1

        str1="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-,_.' \n "
	for(i=0;i<len;i++)
	{
		if((str1.indexOf(str.charAt(i)))==-1)
		{
    	        return false
		}
	}
	return true
}
function isAlphaNumericlink(form1)
{
	var len,str,str1,i
	len=form1.length
	str=form1

        str1="\n\t\r ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-,_.:\\/#@&();*"
	for(i=0;i<len;i++)
	{
		if((str1.indexOf(str.charAt(i)))==-1)
		{
				return false
		}
	}
	return true
}

function isAlphaNumericlinkQuote(form1)
{
	var len,str,str1,i
	len=form1.length
	str=form1

        str1="\n\t\r ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-,_.:\\/#@&();*'"
	for(i=0;i<len;i++)
	{
		if((str1.indexOf(str.charAt(i)))==-1)
		{
				return false
		}
	}
	return true
}
/**
* The function isSearchPageTitle checks for the validity of the entered fields
* which should allow alphanumeric and single quote only. Other wise it alerts for proper valid data.
*/
function isSearchPageTitle(form1)
{
	var len,str,str1,i
	len=form1.length
	str=form1

        str1="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-'. "
	for(i=0;i<len;i++)
	{
		if((str1.indexOf(str.charAt(i)))==-1)
		{
    	        return false
		}
	}
	return true
}

/**
* The function isSearchLink checks the validity of the entered fields
* which should be alphanumeric only. Other wise it alerts for proper valid data.
*/

function isSearchLink(form1)
{
	var len,str,str1,i
	len=form1.length
	str=form1

        str1="\n\t\r ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-,.:/#@&();*_?+="
	for(i=0;i<len;i++)
	{
		if((str1.indexOf(str.charAt(i)))==-1)
		{
				return false
		}
	}
	return true
}

/* Function to compare Two dates.
Returns  1 if value1 > value2
Returns  0 if value1 = value2
Returns -1 if value1 < value2
*/
function compareDates (value1, value2) {
   var date1, date2;
   var month1, month2;
   var year1, year2;

   date1 = value1.substring (0, value1.indexOf ("/"));
   month1 = value1.substring (value1.indexOf ("/")+1, value1.lastIndexOf ("/"));
   year1 = value1.substring (value1.lastIndexOf ("/")+1, value1.length);

   date2 = value2.substring (0, value2.indexOf ("/"));
   month2 = value2.substring (value2.indexOf ("/")+1, value2.lastIndexOf ("/"));
   year2 = value2.substring (value2.lastIndexOf ("/")+1, value2.length);

   if (year1 > year2) return 1;
   else if (year1 < year2) return -1;
   else if (month1 > month2) return 1;
   else if (month1 < month2) return -1;
   else if (date1 > date2) return 1;
   else if (date1 < date2) return -1;
   else return 0;
} 

// Function to check whether a given ipaddress is numeric or not
function isNumericip(ipaddr)
{
	var len,str,str1,i ;
	len=ipaddr.length ;
	str="0123456789."
	for(i=0;i<len;i++)
	{
		if((str.indexOf(ipaddr.charAt(i)))==-1)
		{
		  return false
		}
	}
	return true
}

// function to validate the format of IPAddress.
function validateip(chr)
{
	var len = chr.length;
	var cnt = 0;
	var chrindex = new Array(3);
	var chrip = new Array(3);
	for(i=0;i<len;i++)
	{
		if((".".indexOf(chr.charAt(i)))==-1)
		{
		}
		else
		{
		  chrindex[cnt] = i;
		  cnt++;
		}
	}
	if(cnt != 3)
		return false;
	else
	{
		 var validip = true;
		 for(i=0;i<3;i++)
		 {
		   chrip[i] = chr.substring(0,chrindex[i]);
		   if(chrindex[i] == chrindex[i+1] - 1)
		      validip = false;
		   if((chrip[i] < 0) || (chrip[i] > 255))
			  validip = false;
		 } 
		 if(chrindex[2] == len - 1)
		   validip = false;
    }
    if(validip)
      return true;
    else
     return false;
} 

// Function to validate the IPAddress 
function validipaddress(ipaddress)
{
 var ipaddr = ipaddress;
 var iplength = ipaddr.length;
	if( iplength < 7)
	{
	 return false;
	}
	if(isNumericip(ipaddr))
	{
	  if(!validateip(ipaddr))
	  { 
		return false;
	  }
	}
	else
	{   
	 return false
	 }
	 return true;
}


/**
* The function isNumeric checks for the validity of the entered fields
* which should numeric only. Other wise it alerts for proper valid data.
*/

function isNumeric(form1)
{
	var len,str,str1,i
	len=form1.length
	str=form1
	str1="0123456789-."
	for(i=0;i<len;i++)
	{
		if((str1.indexOf(str.charAt(i)))==-1)
		{
			//alert("Enter Numeric Data in this field")
			return false
		}
	}
	return true
}
/**
* The function isNumeric checks for the validity of the entered fields
* which should numeric only. Other wise it alerts for proper valid data.
*/

function isNumericVal(form1)
{
	var len,str,str1,i
	len=form1.length
	str=form1
	str1="0123456789."
	for(i=0;i<len;i++)
	{
		if((str1.indexOf(str.charAt(i)))==-1)
		{
			//alert("Enter Numeric Data in this field")
			return false
		}
	}
	return true
}
/**
* The function isNumeric checks for the validity of the entered fields
* which should numeric only. Other wise it alerts for proper valid data.
*/

function isNumericDot(form1)
{
	var len,str,str1,i
	len=form1.length
	str=form1
	str1="0123456789"
	for(i=0;i<len;i++)
	{
		if((str1.indexOf(str.charAt(i)))==-1)
		{
			//alert("Enter Numeric Data in this field")
			return false
		}
	}
	return true
}

function isNumericPin(form1)
{
	var len,str,str1,i
	len=form1.length
	str=form1
	str1="0123456789()- "
	for(i=0;i<len;i++)
	{
		if((str1.indexOf(str.charAt(i)))==-1)
		{
			//alert("Enter Numeric Data in this field")
			return false
		}
	}
	return true
}



/**
* The function isAlpha checks for the validity of the entered fields 
* which should characters only. Other wise it alerts for proper valid data.
*/
function isAlpha(form1)
{
              
	var len,str,str1,i
	
	str=form1
	len=form1.length
	
        str1="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-,.\n\t\r "
	for(i=0;i<len;i++) 
        {
	    if((str1.indexOf(str.charAt(i)))==-1)
	    {
    	    //alert("Enter only alphabets in this field")
			return false
	    }
	}
        
	return true
        
}

/**
* The function isAlpha checks for the validity of the entered fields 
* which should characters only and single quotes . Other wise it alerts for proper valid data.
*/
function isAlphaQuote(form1)
{
              
	var len,str,str1,i
	
	str=form1
	len=form1.length
	
        str1="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-,.\n\t\r' "
	for(i=0;i<len;i++) 
        {
	    if((str1.indexOf(str.charAt(i)))==-1)
	    {
    	    //alert("Enter only alphabets in this field")
			return false
	    }
	}
        
	return true
        
}


/**
* The function isWebAdd checks for the validity of the entered fields
* which should in http://www.one.two only. Other wise it alerts for proper valid data.
*/

function isWebAdd(form2)
{
	if(form2!="")
	{
		var add=form2
		var len=add.length
		var p="."
		var q=":"
		var r=add.indexOf(q)
		var r1=q+1
		var s=add.indexOf(p)
		var s1=s+1
        var check=add.substring(0,3)
		var check1=add.substring(0,4)
		var count=add.substring(s1,len)
		var count1=count.indexOf(p)
		if(r==0)
		{
		 if(((s==0)||(add.charAt(len-1)==p)||(add.charAt(s)==add.charAt(s+1))||(count1==-1)||(check!="www")))
		 {
		   return false
		 }  
		}
		else
		{
		if((add.charAt(r+1) != "/") || (add.charAt(r+2) != "/") || ((add.charAt(len-1)==p)||(add.charAt(s)==add.charAt(s+1))||(count1==-1) || (check1 !="http")) )
		{
		  return false
		}
		}
		
	}
		return true
}



// whitespace characters only.

function isWhitespace (s)

{   var i;



    // whitespace characters
    var whitespace = " \t\n\r";	

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }


    // All characters are whitespace.
    return true;
}


function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}




function valName(k)                   //  Form level validation
{
  var a,s;
	a=k.value;
	s=a.length;
	for( var i=0;i<s;i++)
	{
    if ("1234567890!@#$%^&*()+-".indexOf(a.charAt(i))!=-1)
		{
      return false;
    }
  }
  return true; 
}




function valAlphaNum(k)                   //  Form level validation
{
  var a,s;
	a=k.value;
	s=a.length;
	for( var i=0;i<s;i++)
	{
    if ("!@#$%^&*()+-".indexOf(a.charAt(i))!=-1)
		{
      return false;
    }
  }
  return true; 
}




function valNum(k)                     // Form level validation
{
  var a,s;
	a=k.value;
	s=a.length;
	for( var i=0;i<s;i++)
	{
    if ("1234567890.".indexOf(a.charAt(i))==-1)
		{
  	   return false;
    }
	}
  return true;
}



/**
* The function isAlpha checks for the validity of the entered fields
* which should characters only. Other wise it alerts for proper valid data.
*/
function isAlphaNull(form1)
{
              
	var len,str,str1,i
	
	
	 if (isWhitespace(form1.value))
         {
		alert("This field can not be null")
                return false
         }
       	else
	{
	    str=form1.value
	    len=form1.value.length
	    str=form1.value
            str1="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-,.\n "
	    for(i=0;i<len;i++) 
            {
		if((str1.indexOf(str.charAt(i)))==-1)
		{
    	                alert("Enter only alphabets in this field")
			
			return false
		}
	    }
         }
	return true
        
}



function isAlphaNumericNull(form1)
{
	var len,str,str1,i
	

	 if (isWhitespace(form1))
         {
		alert("This field can not be null")
                return false
         }
         else
         {
	   str=form1.value
	   len=form1.value.length
	   str1="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-,.\n "
	   for(i=0;i<len;i++)
	   {
		if((str1.indexOf(str.charAt(i)))==-1)
		{
    	                alert("Enter AlphaNumeric Data in this field")
			
			return false
		}
	   }
        }
	return true
}


/**
* The function isNumeric checks for the validity of the entered fields
* which should numeric only. Other wise it alerts for proper valid data.
*/

function isNumericNull(form1)
{
	var len,str,str1,i
	str=form1.value

	if (isWhitespace(form1))
        {
	    alert("This field can not be null")  
	    return false
        }
	else
	{
	len=form1.value.length
	
	   str1="0123456789-."
	   for(i=0;i<len;i++)
	   {
		if((str1.indexOf(str.charAt(i)))==-1)
		{
			alert("Enter Numeric Data in this field")
			
			return false
		}
	   }
	}
	return true
}


          
   

//  FUNCTION clearing the all text areas the elements in the form 
var clearstatus=0;

function checkTextareaLength(field,maxlimit) 
{
if (field.value.length > maxlimit) 
field.value = field.value.substring(0, maxlimit);
}


function fn_clear(a)
 {
 var lelecount=a.elements.length;
   var num=0;
 for( num=0;num<lelecount;num++)
  {
 if (a.elements[num].type=="text")
     {

    a.elements[num].value='';
     }
 if (a.elements[num].type=="Select-one")
     {
      a.elements[num].selectedIndex=0;
     }
    
   }
   clearstatus=1;
  
 }

/* DATE VALIDATION
  
THIS BELOW FUNCTION CHECKS THE DATE WETHER IT IS CORRECT DATE OR NOT. 
INTERNALLY IT CALLS chkdate(objName) function  */
// Function to check whether the given year is leap year or not.Returns true is yes otherwise returns false
function LeapYear(intYear)
{
  if (intYear % 100 == 0)
  {
     if (intYear % 400 == 0)
	 { 
	   return true;
	 }
  }
  else 
  {
    if ((intYear % 4) == 0)
    { 
      return true; 
    }
  }
  return false;
}

// Fucntion to validate a given date. Date should be in dd/mm/yyyy format,else it returns false
function checkdate(formobj)
{

  	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	strDate = formobj;
	
    if (strDate.length < 8 || strDate.length > 10) 
	{
	  return false;
	}
	
	strDateArray = strDate.split("/");
    if (strDateArray.length != 3)
	{
	   return false;
	}
	else
	{
		strDay = strDateArray[0];
		strMonth = strDateArray[1];
		strYear = strDateArray[2];
	}
    if (strYear.length != 4 )
	{
	  return false;
	} 
	intday = parseInt(strDay, 10);
	if (isNaN(intday)) 
	{
	  return false;
	}
	
	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) 
	{
	   return false;
	}
	
	intYear = parseInt(strYear, 10);
	if (isNaN(intYear))
	{
	  return false;
	}
	
	if (intMonth>12 || intMonth<1)
	{
	  return false;
	}
	
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1))
	{
    	return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) 
	{
		return false;
	}
	if (intMonth == 2)
	{
	  if (intday < 1)
	  {
	    return false;
	  }
	  if (LeapYear(intYear) == true) 
	  {
	 	if (intday > 29)
		{
		  return false;
	    }
	  }
	  else
	  {
		if (intday > 28)
		{
		  return false;
	    }
	  }
	}
    return true;
 }




//validate if the PIN code is numeric & is 6 digits
function isPIN(formname)
{
	var PIN;
	var len;
	if(isNumeric(formname))
	{
		if(!(isWhitespace(formname.value)))
		{
		  PIN = formname.value;
		  len = PIN.length;
		  if (len < 6)
		  {
		    alert("PIN Code should contain 6 characters");			
		  }	
		}
		else
		{
		    alert("PIN Code can't be null");
		}
	}
}

// Replaces a set of characters by another set of characters in a string
function replace( originalsentence, oldword, newword )
{
  var newsentence="", walk=0, index=0, first="", last="";

  newsentence=""+originalsentence;
  walk=0;
  index=newsentence.indexOf(oldword,walk);
      
  while( (walk<newsentence.length) && (index!=(-1)) )
  {
    first=newsentence.substring(0,index);
    last=newsentence.substring(index+oldword.length,newsentence.length);
    newsentence=first+""+newword+""+last;

    walk=index+newword.length;
    index=newsentence.indexOf(oldword,walk);
  }

  return(newsentence);
}

//Trims a set of character
function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function


function countOf(formValue)
{
  var strValue = formValue;
  var count = 0; 
  var index = strValue.indexOf(".") ;
  while ((index != -1) && (count <= 1))
  {
    count = count + 1 ;
	index = (strValue.substring(index+1,strValue.length)).indexOf(".")
  }
  if (count > 1)
    return (false) ;
 else
    return (true) ;	 
}

function Decimalcount(formValue)
{
  var strValue = formValue;
  var count = 0; 
  var index = strValue.indexOf(".") ;
  while ((index != -1) && (count <= 1))
  {
    count = count + 1 ;
	index = (strValue.substring(index+1,strValue.length)).indexOf(".")
  }
  if (count > 0)
    return (true) ;
 else
    return (false) ;	 
}

function DateTime(formValue)
{
 
  var strValue = formValue;
     
  ind1 = strValue.indexOf (" ");
  len  = strValue.length ;

  date1 = strValue.substring (0,ind1);
 
  if(!(checkdate(date1)))
	  return (false) ;
 

  time1 = strValue.substring(ind1+1, len);

  indhr   = time1.indexOf(":") ;
  indmin  = time1.lastIndexOf(":") ;
  timelen = time1.length ;

  
  hr1 = time1.substring(0,indhr) ;
  min1 = time1.substring(indhr+1,indmin) ;
  sec1 = time1.substring(indmin+1,timelen) ;
	 
  if((hr1 < 0) || (hr1 > 23))
	  return (false) ;

  if((min1 < 0) || (min1 > 59))
	  return (false) ;
  
  if((sec1 < 0) || (sec1 > 59))
	  return (false) ;

  return (true)

}

function checkdateInd(formobj)
{
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	strDate = formobj;
	
    if (strDate.length < 8 || strDate.length > 10) 
	{
	  return false;
	}
	
	strDateArray = strDate.split("/");
    if (strDateArray.length != 3)
	{
	   return false;
	}
	else
	{
		strDay = strDateArray[0];
		strMonth = strDateArray[1];
		strYear = strDateArray[2];
	}
    if (strYear.length < 4 )
	{
	  return false;
	} 
	intday = parseInt(strDay, 10);
	if (isNaN(intday)) 
	{
	  return false;
	}
	
	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) 
	{
	   return false;
	}
	
	intYear = parseInt(strYear, 10);
	if (isNaN(intYear))
	{
	  return false;
	}
	
	if (intMonth>12 || intMonth<1)
	{
	  return false;
	}
	
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1))
	{
    	return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) 
	{
		return false;
	}
	if (intMonth == 2)
	{
	  if (intday < 1)
	  {
	    return false;
	  }
	  if (LeapYear(intYear) == true) 
	  {
	 	if (intday > 29)
		{
		  return false;
	    }
	  }
	  else
	  {
		if (intday > 28)
		{
		  return false;
	    }
	  }
	}
    return true;
 }


