/* Javascript Functions to Validate String, Number, Email and String with specified Characters */

var whitespace = " \t\n\r";

function isEmpty(s)  //Checks whether string is Empty
	{  
		return ((s == null) || (s.length == 0))
	}

function isWhitespace (s) //Checks for White spaces in the string 
	{  
		var i;
	 	if (isEmpty(s)) return true;
	 	   for (i = 0; i < s.length; i++)
	 	   {   
	 	       var c = s.charAt(i);
	 	       if (whitespace.indexOf(c) == -1) return false;
	 	   }
		   return true;
	}

function noWhitespace (s) //no white space in between the string
	{  
		var i;
	 	if (isEmpty(s)) return true;
	 	   for (i = 0; i < s.length; i++)
	 	   {   
	 	       var c = s.charAt(i);
				if (whitespace.indexOf(c) == 0){
			   	   return true;
				}
	 	   }
		   return false;
	}

function isNumber (s) //Checks whether the number is numeric
	{  
	    var i;
		var bag="1234567890"
	    for (i = 0; i < s.length; i++)
	    {   
	        var c = s.charAt(i);
	        if (bag.indexOf(c) == -1) return false;
	    }
	    return true;
	}

function isCharsInBag (s, bag) /* Checks whether the string only contains the characters 
								specified in the parameter 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 isString (s) //Checks whether the string contains only alphabets, . and _
	{  
	    var i;
		var bag="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz _."
	    for (i = 0; i < s.length; i++)
	    {   
	        var c = s.charAt(i);
	        if (bag.indexOf(c) == -1) return false;
	    }
	    return true;
	}
function isAlpha (s) //Checks whether the string contains only alphabets
	{  
	    var i;
		var bag="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
	    for (i = 0; i < s.length; i++)
	    {   
	        var c = s.charAt(i);
	        if (bag.indexOf(c) == -1) return false;
	    }
	    return true;
	}
function isAlphaNumeric (s) //Checks whether the string contains only alphabets and Numbers
	{  
	    var i;
		var bag="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ";
	    for (i = 0; i < s.length; i++)
	    {   var c = s.charAt(i);
			if (bag.indexOf(c) == -1) return false;
		}
		return true;
	}
	
function isTextNumeric (s) //Checks whether the string contains only alphabets and Numbers
	{  
	    var i;
		var bag="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	    for (i = 0; i < s.length; i++)
	    {   var c = s.charAt(i);
			if (bag.indexOf(c) == -1) return false;
		}
		return true;
	}
		
function isEmail (emailStr) //Checks whether the given string is a valid EMail address
 	{
		var emailPat=/^(.+)@(.+)$/
		var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
		var validChars="\[^\\s" + specialChars + "\]"
		var quotedUser="(\"[^\"]*\")"
		var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
		var atom=validChars + '+'
		var word="(" + atom + "|" + quotedUser + ")"
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")

		var matchArray=emailStr.match(emailPat)

		if (matchArray==null) 
		{
			return false
		}
		
		var user=matchArray[1]
		var domain=matchArray[2]
		
		
		if (user.match(userPat)==null) 
		{
		    return false
		}
		
		var IPArray=domain.match(ipDomainPat)
		
		if (IPArray!=null) 
		{
		  	for (var i=1;i<=4;i++) 
		  		{
			    if (IPArray[i]>255) 
					{
			    		return false
			    	}
		    	}
		    return true
		}
	
		var domainArray=domain.match(domainPat)
		
		if (domainArray==null) 
			{
		    	return false
			}
		
		var atomPat=new RegExp(atom,"g")
		var domArr=domain.match(atomPat)
		var len=domArr.length

		if (domArr[domArr.length-1].length<2 || 
		    domArr[domArr.length-1].length>3) 
			{
			   return false
			}
		
		if (len<2) 
			{
			   var errStr="This address is missing a hostname!"
	   		   return false
			}

		return true;
	}

function isValidDate(dateStr) 	// Checks for the following valid date formats:
	{							// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
									
	
		var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year
		
		var matchArray = dateStr.match(datePat); // is the format ok?
		if (matchArray == null) 
		{
			alert(dateStr + " Date is not in a valid format.")
			return false;
		}
		
		// parse date into variables
		
		month = matchArray[1]; 
		day = matchArray[3];
		year = matchArray[4];
		
		if (month < 1 || month > 12) 
		{ // check month range
			alert("Month must be between 1 and 12.");
			return false;
		}
		
		if (day < 1 || day > 31) 
		{	//check day range
			alert("Day must be between 1 and 31.");
			return false;
		}
		
		if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) 
		{ //check whether the month has 31 days
			alert("Month "+month+" doesn't have 31 days!")
			return false;
		}
		if (month == 2) 
		{ // check for february 29th
			var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); //leap year check
			if (day > 29 || (day == 29 && !isleap)) 
			{
				alert("February " + year + " doesn't have " + day + " days!");
				return false;
		    }
		}
		return true;
	}	
/* End of Validation Java Scripts */
/* Function added on 25/03/2002 By lingaraju */
function isGreaterDate(frmDate,toDate)
{
    //The function checks if toDate is greater than or equal to from date
	//Breaking up the start and End date into month, day, year formats
	var fromDatec =frmDate.split('/');
	var toDatec = toDate.split('/');

	if(eval(toDatec[2] > fromDatec[2]))
	    return true;
	else if(eval(toDatec[2] < fromDatec[2]))
	    return false;	
	else if(eval(toDatec[2] == fromDatec[2]))
	{
	   if (eval(toDatec[0] - fromDatec[0])>=1)
	   {
		     return true;
	   }
	   else if(eval(toDatec[0] - fromDatec[0])== 0)
	   {
		  if(eval(toDatec[1] -fromDatec[1]) >= 0)
		      return true;
	      else if(eval(toDatec[1] - fromDatec[1]) < 1)
		    { 
			   return false;
			}	 
	   }	
	   else if(eval(toDatec[0] - fromDatec[0]) < 1)
	   {
//	 	    alert('in month2 - month1'+eval(fromDatec[0] > toDatec[0]));
			return false;
	   }   	   	   	   
	}	
}
/* End of function to find wheter a give date is greater than equal to a date or not */	
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

