/******************************************************************/
// FORM VALIDATION FUNCTIONS
/******************************************************************/

// DEFINE VARIABLES
var whitespace = " \t\n\r";    // whitespace characters

/******************************************************************/
// Is (s) whitespace
function IsWhitespace (s) {
    var i;
    // Is s empty?
    if (s == null) 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;
}  // END IsWhitespace()


/******************************************************************/
// Is (s) blank or just space
function IsBlank(s) {
  if ((s == null) || (s.length==0) || IsWhitespace(s)) {return true;}
  return false;
}  // END IsBlank()


/******************************************************************/
// Is (s) a valid number
function IsNumber(s) {
  if (IsBlank(s)) {return false}
  for(var i=0; i<s.length; i++) {
    var c = s.charAt(i);
    if ((c<'0') || (c>'9')) {return false;} // If Contains Other Than 0-9 >> Invalid
  }
  return true;
} // END IsNumber()


/******************************************************************/
// Is (s) a valid decimal number (accepts integer too)
function IsDecimal(s) {
	if (IsBlank(s)) {return false}

	var one_decimal = false;

	for(var i=0; i<s.length; i++) {
		var c = s.charAt(i);
		if ((c<'0') || (c>'9')) {	// If Contains Other Than 0-9.. might be invalid
			if ((c=='.') && (one_decimal==false)) {one_decimal=true}
			else {return false}
		}
	}
	return true;
} // END IsDecimal()


/******************************************************************/
// Is (s) a valid amount (number with commas like 1,000,000)
function IsAmount(s) {
  if (IsNumber(s)) {
    return true;
  } else {
    var okay=0;
    while (! okay) {
      var len=s.length;
      if (len > 4){ 
        var tmp=s.substring(len-4, len-1);
        if (tmp.charAt(0)!=',' || !(IsNumber(tmp.substring(1,4)))) {return false;}
        s = s.substring(0,len-4);
      } else {
        if (!IsNumber(s) && (s.charAt(0)!='0')) {return false;}
        okay++;
      }
    }
    return true;
  } // End If
} // END IsAmount()


/******************************************************************/
// Is (s) a valid date (not perfect)
function IsDate(s) {
  if (IsBlank(s)) {
    return false;
  } else {
    if (!isNaN(Date.parse(s))) { return true; }
  }
} // END IsDate()


/****************************************************************/
// IsEmail (STRING s)
// 
// Email address must be of form a@b.c :
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required

function IsEmail (s) {
    if (IsBlank(s) ||IsWhitespace(s)) { return false; }
    
    // there must be at least 1 character before @
    // so start looking at character position 1
    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;
} // END IsEmail()