var blanks = " \t\n\r";  // aka whitespace chars
var ZIPCodeDelimeter = "-";
var ZIPCodeDelimiters = "-";
var digitsInZIPCode1 = 5;
var digitsInZIPCode2 = 9;
var digits = "0123456789";

// Returns true if string s is empty

function isEmpty(s)
  {
  return ((s == null) || (s.length == 0));
  }
// Returns true if string s is empty or all blank chars
function isBlank(s)
  {
  var i;
  // Is s empty?
  if (isEmpty(s))
    return true;
  // Search through string's chars one by one until we find first
  // non-blank char, then return false; if we don't, return true
  for (i=0; i<s.length; i++)
    {   
    // Check that current character isn't blank
    var c = s.charAt(i);
    if (blanks.indexOf(c) == -1) 
      return false;
    }
  // All characters are blank
  return true;
  }
  
  function isEmail(s)
  { 
  if (isBlank(s)) 
    return false;
  // there must be >= 1 character before @, so we start
  // start looking at character position 1 (i.e. second character)
  var i = 1;
  var sLength = s.length;

  // look for @
  while ((i < sLength) && (s.charAt(i) != "@"))
    i++

  if ((i >= sLength) || (s.charAt(i) != "@")) 
    return false;
  else 
    i += 2;

  // look for .
  while ((i < sLength) && (s.charAt(i) != "."))
    i++

  // there must be at least one character after the .
  if ((i >= sLength - 1) || (s.charAt(i) != ".")) 
    return false;
  else 
    return true;
  }
  
  function isZIPCode(s)
  { 
  if (isBlank(s)) 
    return false;

  s = stripLeadingTrailingBlanks(s);

  if ((isInteger(s)) && (s.indexOf("-") == -1) &&  // #####
      (s.length == digitsInZIPCode1))
    return true;

  if (s.indexOf("-") != 5)  // - in wrong place
    return false;

  s = stripCharsNotInBag(s, digits);

  if (s.length == digitsInZIPCode2)
    return true;   // #####-####
  else
    return false;  // not #####-####
  }
  
  function isInteger(s)
  {
  if (isBlank(s))
    return false;

  // skip leading + or -
  if ((s.charAt(0) == "-") || (s.charAt(0) == "+"))
    var i = 1;
  else
    var i = 0;

  // Search through string's chars one by one until we find a 
  // non-numeric char, then return false; if we don't, return true
  for (i; i<s.length; i++)
    {   
    // Check that current character is number
    var c = s.charAt(i);
    if (!isDigit(c)) 
      return false;
    }
  // All characters are numbers
  return true;
  }

  function isNumeric(s)
  {
  if (isBlank(s))
    return false;

  // skip leading + or -
  if ((s.charAt(0) == "-") || (s.charAt(0) == "+"))
    var i = 1;
  else
    var i = 0;
	

  // Search through string's chars one by one until we find a 
  // non-numeric char, then return false; if we don't, return true
  for (i; i<s.length; i++)
    {   
    // Check that current character is number
    var c = s.charAt(i);
	period_count = 0
    if (!isDigit(c)) 
		if (c == "."){
			period_count++;
			if (period_count > 1){
				return false;
			}
		}else{
	      	return false;
	  	}
    }
  // All characters are numbers
  return true;
  }

function isDigit(c)
  {
  return ((c >= "0") && (c <= "9"));
  }
  
  function stripLeadingBlanks(s)
  { 
  var i = 0;
  while ((i < s.length) && (blanks.indexOf(s.charAt(i)) != -1))
     i++;
  return s.substring(i, s.length);
  }

function stripTrailingBlanks(s)
  { 
  var i = s.length - 1;
  while ((i >= 0) && (blanks.indexOf(s.charAt(i)) != -1))
     i--;
  return s.substring(0, i+1);
  }

function stripLeadingTrailingBlanks(s)
  { 
  s = stripLeadingBlanks(s);
  s = stripTrailingBlanks(s);
  return s;
  }
function stripCharsNotInBag (s, bag)
  {
  var i;
  var returnString = "";

  // Search through string's characters one by one;
  // if character is in bag, append to returnString
  for (i = 0; i < s.length; i++)
    {   
    // Check that current character isn't blank
    var c = s.charAt(i);
    if (bag.indexOf(c) != -1) 
      returnString += c;
    }
  return returnString;
  }
