/*
'Filename:formvalidations.js
'Developer Name:Ajay Kumar
'Created On:12/12/2005
'Last Changed Date:19/12/2005
'Brief Description:Contains common functions that are used to validate the fields in the form.
'Company:
*/
//Some Global Constants
// whitespace characters
var whitespace = " \t\n\r";
phoneNumberDelimiters="()- ";
// function isEmpty checks whether the variable passed is empty or not.
function isEmpty(s)
{
	var str=new String(s);
	return((s==null)||(s.length==0));
}

/* function Trim eliminates the blank spaces from the var s on n side
n=left: trims from left side
n=right: trims from right side
n=both: trims from both sides
*/
function Trim(s,n)
{
	if(!isEmpty(s))
	{
		if(n=="left"||n=="Left"|| n=="LEFT")
		{
			return(s.replace( /^\s+/g, "" ));
		}
		if(n=="right"||n=="Right"||n=="RIGHT")
		{
			return(s.replace( /\s+$/g, "" ));
		}
		if(n=="both"||n=="Both"||n=="Both")
		{
			 s=s.replace( /^\s+/g, "" );// strip leading
			 return s.replace( /\s+$/g, "" );// strip trailing
		}
	}
	else
	{
		return("");
	}
}

// function isNumber returns true if the car is a valid number  otherwise returns false

function isNumber(n)
{
	return (!isNaN(n))
}

/*checks whether the supplied String contains whitespace or not.
 'true' if contains whitespace else 'false'
 These are whitespaces : " \t\n\r"
 Eg. "ab cd", "abc " are having whitespace.
 */
function hasWhitespace(data)
{
	for(var i = 0 ; i<data.length ; i++)
	{
		c = data.charAt(i) ;
		if(whitespace.indexOf(c) == -1)
		{
			continue ;
		} 
		else 
		{
			return true ;
		}
	}
	return false ;
}

/* function isEmailNonRE(s) returns true if the passed arg is a valid email address otherwise returns false 
This is to be used in case the system does not support regular expressions.
*/
function isEmailNonRE(s)
{
	if (isEmpty(s))
	{
		return false;
	}
	if (isEmailNonRE.arguments.length == 1)
	{
		return false;
	}
	else 
	{
		return (isEmailNonRE.arguments[1] == true);
	}
	if (hasWhitespace(s)) return false;
	var i = 1;
	var sLength = s.length;
	while ((i < sLength) && (s.charAt(i) != "@"))
	{
		i++
	}
	if ((i >= sLength) || (s.charAt(i) != "@")) 
	{
		return false;
	}
	else 
	{
		i += 2;
		while ((i < sLength) && (s.charAt(i) != "."))
		{
			i++
		}
		
	}
	if ((i >= sLength - 1) || (s.charAt(i) != ".")) 
	{
		return false;
	}
	else 
	{	
		return true;
	}
}
/* function isValidEmail() returns true if the passed arg is a valid email address otherwise returns false (RECOMMENDED) 
in most of the cases this is to be used as this function checks the systems capability to support reg exp and based on that gives 
the result.
*/
function isValidEmail(str)
{
	var supported = 0;
	if (isEmpty(str))
	{
		return false;
	}
	if (hasWhitespace(str)) return false;
	if (window.RegExp)
	{
	    var tempStr = "a";
	    var tempReg = new RegExp(tempStr);
	    if (tempReg.test(tempStr))
	    {	
	    	supported = 1;
	    }
	}
	if (!supported)
	{
	   return isEmailNonRE(str);
	}
	else
	{
		var r3= new RegExp("^([-a-zA-Z0-9_.']+)@(([-a-zA-Z0-9_]+[.])+[a-zA-Z]+)$");
		return(r3.test(str))
	}
}


/* Returns true if character c is a valid english alphabet  
 (a-z,A-Z).
 */
function isLetter (c)
{
	return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}


/* Returns true if character c is a digit 
 (0 .. 9).
 */

function isDigit (c)
{   
	return ((c >= "0") && (c <= "9"))
}


/* this function returns true if the phone number is valid otherwise returns false
eg
1-234-2323
(1)-(234)-(4354)
13225353
are valid phone numbers
*/
function isPhoneNumber(data)
{
	for(var i=0;i < data.length ; i++ )
	{
		if ( isDigit(data.charAt(i)) || ( phoneNumberDelimiters.indexOf(data.charAt(i)) != -1 ) )
		{
			continue ;
		}
		else
		{
			return false ;
		}
	}

	return true ;
}


/*
function checks whether the argument passed is a valid url or not
*/
function isValidUrl(url)
{
   var retVal = true;
   var i=new RegExp("http:\/\/([a-zA-Z\.0-9]{2,})\.([a-zA-Z\.0-9]{2,})");
   var j=new RegExp("https:\/\/([a-zA-Z\.0-9]{2,})\.([a-zA-Z\.0-9]{2,})");
   if(!i.test(url)) 
   { 
		retVal = false;
		
   }   
   if (!retVal)
   {
   	if(!j.test(url)) 
   	{ 
		retVal = false;
   	}
   	else 
   	{
   		retVal = true;
   	} 
   }
   
   return retVal;
   
}




/*
this returns true if the pattern entered is a valid IP Address otherwise  returns false
*/

function isIPAddress(ipaddr)
{
   var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
   if (re.test(ipaddr))
   {
      var parts = ipaddr.split(".");
      if (parseInt(parseFloat(parts[0])) == 0) { return false; }
      for (var i=0; i<parts.length; i++)
      {
         if (parseInt(parseFloat(parts[i])) > 255)
         { 
         	return false;
         }
      }
      return true;
   }
   else
   {
      return false;
   }
}

function isUKPostalCode(data)
{
	for(var i=0;i < data.length ; i++ )
	{
		if ( isDigit(data.charAt(i)) || isLetter(data.charAt(i)) || data.charAt(i)==" " || data.charAt(i)==")" || data.charAt(i)=="(" || data.charAt(i)=="-")
		{
			continue ;
		}
		else
		{
			return false ;
		}
	}

	return true ;
}