
// whitespace characters
var whitespace = " \t\n\r";

function replaceAll (s, fromStr, toStr)
{
	var new_s = s;
	for (i = 0; i < 100 && new_s.indexOf (fromStr) != -1; i++)
	{
		new_s = new_s.replace (fromStr, toStr);
	}
	return new_s;
}
function sqlSafe (s)
{
	var new_s = s;
	new_s = replaceAll (new_s, "'", "|");
	new_s = replaceAll (new_s, "|", "''");
	new_s = replaceAll (new_s, "\"", "|");
	new_s = replaceAll (new_s, "|", "''");
	return new_s;
}

function makeSafe (i)
{
	i.value = sqlSafe (i.value);
}


function isEmpty(s)
{   
   return ((s == null) || (s.length == 0))
}

function isWhitespace (s)
{   var i;

    if (isEmpty(s)) 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;
    }
    return true;
}

function LTrim(str){
	for(var i=0;str.charAt(i)==" ";i++);
	return str.substring(i,str.length);
	}
function RTrim(str){
	for(var i=str.length-1;str.charAt(i)==" ";i--);
	return str.substring(0,i+1);
	}
function Trim(str){return LTrim(RTrim(str));}


function isEmail (s)
{   
   s = Trim(s);
   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return false;
       else return (isEmail.arguments[1] == true);

    if (isWhitespace(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 isNumber(str)
{
	
	var i = 0;

	for (i = 0; i < str.length; i++)
	{
		if (str.charAt(i) < '0' || str.charAt(i) > '9') 
		{
			return false;
		}
    }
	
	return true;
}



function isZipcode(strZip)
{
	var s = new String(strZip);
	s = Trim(s);

	if (s.length == 5)
	{
	   if(isNumber(s))
	   {
	       return (true);
	   }
	   else
	   {
	   
	       return false;
	   }
	}
	    
	if (s.length == 6)
	{
	   if (s.charAt(3) != ' ')
	   {
	      return false;
	   }
	   else
	   {
		   for (i=0; i<3; i++)
		 	  if (s.charAt(i) < '0' || s.charAt(i) > '9') 
		 	  {
		 	      return false;
		 	  }

		   for (i=4; i<6; i++)
		 	  if (s.charAt(i) < '0' || s.charAt(i) > '9') 
		 	  {
		 	  
		 	      return false;
		 	      
		 	  }
		 	  
		   return true;
	   }
	 }
	 else
	 {
	    return false;
	 }
	 
	 
}


