
// Whitespace characters
var strWhitespace = " \t\n\r";

// Digits
var strDigits = "0123456789";

// Characters
var strCharacters = "ABCDEFGHIJKLMNOPQRSTVUWXYZabcdefghijklmnopqrstvuwxyz '-()";

//--------------------------------------------------------------------------------
// Returns true if string strValue is empty or whitespace characters only.
//--------------------------------------------------------------------------------
function IsWhitespace(strValue) {
    var i;

    // Is strValue empty?
    if (IsEmpty(strValue))
        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 < strValue.length; i++) {
        // Check that current character isn't whitespace.
        var c = strValue.charAt(i);
        if (strWhitespace.indexOf(c) == -1)
            return false;
    }

    // All characters are whitespace.
    return true;
}

//--------------------------------------------------------------------------------
// Check whether string s is empty.
//--------------------------------------------------------------------------------
function IsEmpty(strValue) {
    return ((strValue == null) || (strValue.length == 0))
}

//--------------------------------------------------------------------------------
// Returns true if string s is a valid character.
//--------------------------------------------------------------------------------
function IsAlphabetic(s) {
  var i;

  // Search through string's characters one by one
  // until we find a non-alphabetic character.
  // When we do, return false; if we don't, return true.
  for (i = 0; i < s.length; i++) {   
    // Check that current character is letter.
    var c = s.charAt(i);
    if (characters.indexOf(c) == -1)
      return false;
  }

  // All characters are letters.
  return true;
}
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// Returns true if string s is a valid number.
//-----------------------------------------------------------------------------
function IsNumeric(s) {
  var i;

  // Search through string's characters one by one
  // until we find a non-numeric character.
  // When we do, return false; if we don't, return true.
  for (i = 0; i < s.length; i++) {   
    // Check that current character is number.
    var c = s.charAt(i);
    if (digits.indexOf(c) == -1)
      return false;
  }

  // All characters are numbers.
  return true;
}
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// isName(TEXTFIELD theField, STRING s, BOOLEAN emptyOK)
//  - theField, Referrance to field.
//  - s, String containing label value.
//  - emptyOK, Boolean value indicating if field can be empty or not.
//
// Validate the name.
// Name must be alphabetic characters only.
//-----------------------------------------------------------------------------
function IsName(theField, s, emptyOK) {
  var sValue = theField.value;
  var sLength = theField.value.length;

  // Check if field is empty.
  if (isWhitespace(sValue))
    if (!emptyOK) {
      //-----------------------------------------------------------------------------
      // Notify user that required field theField is empty.
      // Remove content, put focus in it, and return false.
      //-----------------------------------------------------------------------------
	  theField.value = "";
	  theField.focus();
      alert("\"" + s + "\"" + " is a required field.\nPlease enter it now.");
      return false;
      //-----------------------------------------------------------------------------
    }
    else
	  return true;

  if (!isAlphabetic(sValue))
    return -2;

  // Field value is too long.
  if (sLength > 50)
    return -3;

  // Valid name.
  return 1;
}
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// isStreetAddress(TEXTFIELD theField, STRING s, BOOLEAN emptyOK)
//  - theField, Referrance to field.
//  - s, String containing label value.
//  - emptyOK, Boolean value indicating if field can be empty or not.
//
// Validate the street address.
//-----------------------------------------------------------------------------
function IsStreetAddress(theField, s, emptyOK) {
  var sValue = theField.value;
  var sLength = theField.value.length;

  // Check if field is empty.
  if (isWhitespace(sValue))
    if (!emptyOK) {
      //-----------------------------------------------------------------------------
      // Notify user that required field theField is empty.
      // Remove content, put focus in it, and return false.
      //-----------------------------------------------------------------------------
	  theField.value = "";
	  theField.focus();
      alert("\"" + s + "\"" + " is a required field.\nPlease enter it now.");
      return false;
      //-----------------------------------------------------------------------------
    }
    else
	  return true;

  // Field value is too long.
  if (sLength > 75)
    return -3;

  // Valid name.
  return 1;
}
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// isCity(TEXTFIELD theField, STRING s, BOOLEAN emptyOK)
//  - theField, Referrance to field.
//  - s, String containing label value.
//  - emptyOK, Boolean value indicating if field can be empty or not.
//
// Validate the city name.
// City name must be alphabetic characters only.
//-----------------------------------------------------------------------------
function isCity(theField, s, emptyOK) {
  var sValue = theField.value;
  var sLength = theField.value.length;

  // Check if field is empty.
  if (isWhitespace(sValue))
    if (!emptyOK) {
      //-----------------------------------------------------------------------------
      // Notify user that required field theField is empty.
      // Remove content, put focus in it, and return false.
      //-----------------------------------------------------------------------------
	  theField.value = "";
	  theField.focus();
      alert("\"" + s + "\"" + " is a required field.\nPlease enter it now.");
      return false;
      //-----------------------------------------------------------------------------
    }
    else
	  return true;

  if (!isAlphabetic(sValue))
    return -2;

  // Field value is too long.
  if (sLength > 50)
    return -3;

  // Valid name.
  return 1;
}
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// isState(TEXTFIELD theField, STRING s, BOOLEAN emptyOK)
//  - theField, Referrance to field.
//  - s, String containing label value.
//  - emptyOK, Boolean value indicating if field can be empty or not.
//
// Validate the province/state name.
//-----------------------------------------------------------------------------
function isState(theField, s, emptyOK) {
  var sValue = theField.value;
  var sLength = theField.value.length;

  // Check if field is empty.
  if (isWhitespace(sValue))
    if (!emptyOK) {
      //-----------------------------------------------------------------------------
      // Notify user that required field theField is empty.
      // Remove content, put focus in it, and return false.
      //-----------------------------------------------------------------------------
	  theField.value = "";
	  theField.focus();
      alert("\"" + s + "\"" + " is a required field.\nPlease enter it now.");
      return false;
      //-----------------------------------------------------------------------------
    }
    else
	  return true;

  if (!isAlphabetic(sValue)) {
    //-----------------------------------------------------------------------------
    // Notify user that content of field theField is invalid.
    // Select theField, put focus in it, and return false.
    //-----------------------------------------------------------------------------
    theField.focus();
    //theField.select();
    alert("Invalid \"" + s + "\".\nPlease enter again!");
    return false;
  }

  // Check if field value is too long.
  if (sLength > 45) {
    //-----------------------------------------------------------------------------
    // Notify user that content of field theField is too long.
    // Select theField, put focus in it, and return false.
    //-----------------------------------------------------------------------------
    theField.focus();
    theField.select();
    alert("\"" + s + "\" is too long!\nPlease enter again!");
    return false;
    //-----------------------------------------------------------------------------
  }

  // Valid province/state name.
  return true;
}
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// isCountry(TEXTFIELD theField, STRING s, BOOLEAN emptyOK)
//  - theField, Referrance to field.
//  - s, String containing label value.
//  - emptyOK, Boolean value indicating if field can be empty or not.
//
// Validate the country name.
//-----------------------------------------------------------------------------
function isCountry(theField, s, emptyOK) {
  var sValue = theField.value;
  var sLength = theField.value.length;

  // Check if field is empty.
  if (isWhitespace(sValue))
    if (!emptyOK) {
      //-----------------------------------------------------------------------------
      // Notify user that required field theField is empty.
      // Remove content, put focus in it, and return false.
      //-----------------------------------------------------------------------------
	  theField.value = "";
	  theField.focus();
      alert("\"" + s + "\"" + " is a required field.\nPlease enter it now.");
      return false;
      //-----------------------------------------------------------------------------
    }
    else
	  return true;

  if (!isAlphabetic(sValue)) {
    //-----------------------------------------------------------------------------
    // Notify user that content of field theField is invalid.
    // Select theField, put focus in it, and return false.
    //-----------------------------------------------------------------------------
    theField.focus();
    //theField.select();
    alert("Invalid \"" + s + "\".\nPlease enter again!");
    return false;
  }

  // Check if field value is too long.
  if (sLength > 45) {
    //-----------------------------------------------------------------------------
    // Notify user that content of field theField is too long.
    // Select theField, put focus in it, and return false.
    //-----------------------------------------------------------------------------
    theField.focus();
    theField.select();
    alert("\"" + s + "\" is too long!\nPlease enter again!");
    return false;
    //-----------------------------------------------------------------------------
  }

  // Valid country name.
  return true;
}
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// isZipCode(TEXTFIELD theField, STRING s, BOOLEAN emptyOK)
//  - theField, Referrance to field.
//  - s, String containing label value.
//  - emptyOK, Boolean value indicating if field can be empty or not.
//
// Validate the postal/zip code.
//-----------------------------------------------------------------------------
function isZipCode(theField, s, emptyOK) {
  var sValue = theField.value;
  var sLength = theField.value.length;

  // Check if field is empty.
  if (isWhitespace(sValue))
    if (!emptyOK) {
      //-----------------------------------------------------------------------------
      // Notify user that required field theField is empty.
      // Remove content, put focus in it, and return false.
      //-----------------------------------------------------------------------------
	  theField.value = "";
	  theField.focus();
      alert("\"" + s + "\"" + " is a required field.\nPlease enter it now.");
      return false;
      //-----------------------------------------------------------------------------
    }
    else
	  return true;

  // Field value is too long.
  if (sLength > 50)
    return -3;

  // Valid postal/zip code.
  return true;

}
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// isEmail(TEXTFIELD theField, STRING s, BOOLEAN emptyOK)
//  - theField, Referrance to field.
//  - s, String containing label value.
//  - emptyOK, Boolean value indicating if field can be empty or not.
//
// Validate the e-mail address.
// E-mail address must be of form a@b.c -- in other words:
//  - 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(theField, s, emptyOK) {
  var sValue = theField.value;
  var sLength = theField.value.length;

  // Check if field is empty.
  if (isWhitespace(sValue))
    if (!emptyOK) {
      //-----------------------------------------------------------------------------
      // Notify user that required field theField is empty.
      // Remove content, put focus in it, and return false.
      //-----------------------------------------------------------------------------
	  theField.value = "";
	  theField.focus();
      alert("\"" + s + "\"" + " is a required field.\nPlease enter it now.");
      return false;
      //-----------------------------------------------------------------------------
    }
    else
	  return true;

  //-----------------------------------------------------------------------------
  // There must be >= 1 character before @, so we start looking 
  // at character position 1 (i.e. second character).
  //-----------------------------------------------------------------------------
  var i = 1;

  // Look for @
  while ((i < sLength) && (sValue.charAt(i) != "@")) {
    i++
  }

  if ((i >= sLength) || (sValue.charAt(i) != "@")) {
    //-----------------------------------------------------------------------------
    // Notify user that content of field theField is invalid.
    // Select theField, put focus in it, and return false.
    //-----------------------------------------------------------------------------
    theField.focus();
    theField.select();
    alert("Invalid \"" + s + "\".\nPlease enter again!");
    return false;
    //-----------------------------------------------------------------------------
  }
  else
    i += 2;

  // Look for "."
  while ((i < sLength) && (sValue.charAt(i) != ".")) {
    i++
  }

  // There must be at least one character after the "."
  if ((i >= sLength - 1) || (sValue.charAt(i) != ".")) {
    //-----------------------------------------------------------------------------
    // Notify user that content of field theField is invalid.
    // Select theField, put focus in it, and return false.
    //-----------------------------------------------------------------------------
    theField.focus();
    theField.select();
    alert("Invalid \"" + s + "\".\nPlease enter again!");
    return false;
    //-----------------------------------------------------------------------------
  }
  //-----------------------------------------------------------------------------


  // Check if field value is too long.
  if (sLength > 45) {
    //-----------------------------------------------------------------------------
    // Notify user that content of field theField is too long.
    // Select theField, put focus in it, and return false.
    //-----------------------------------------------------------------------------
    theField.focus();
    theField.select();
    alert("\"" + s + "\" is too long!\nPlease enter again!");
    return false;
    //-----------------------------------------------------------------------------
  }

  // Valid e-mail.
  return true;
}
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// isPhoneNumber(TEXTFIELD theField, STRING s, BOOLEAN emptyOK)
//  - theField, Referrance to field.
//  - s, String containing label value.
//  - emptyOK, Boolean value indicating if field can be empty or not.
//
// Validate the phone number.
//-----------------------------------------------------------------------------
function IsPhoneNumber(theField, s, emptyOK) {
  var sValue = theField.value;
  var sLength = theField.value.length;

  // Check if field is empty.
  if (isWhitespace(sValue))
    if (!emptyOK) {
      //-----------------------------------------------------------------------------
      // Notify user that required field theField is empty.
      // Remove content, put focus in it, and return false.
      //-----------------------------------------------------------------------------
	  theField.value = "";
	  theField.focus();
      alert("\"" + s + "\"" + " is a required field.\nPlease enter it now.");
      return false;
      //-----------------------------------------------------------------------------
    }
    else
	  return true;

  // Field value is too long.
  if (sLength > 50)
    return -3;

  // Valid phone number.
  return true;

}
