// This file contains the data validation JavaScript functions
// It is included in the HTML pages with forms that need these
// data validation routines.

// DEFINE VARIABLES

// 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;
}

/****************************************************************/

// Check whether string s is empty.

function isEmpty(s)
{ return ((s == null) || (s.length == 0))
}

/****************************************************************/

// Returns true if string s is empty or
// whitespace characters only.

function isWhitespace (s)

{ var i;

// Is s empty?
if (isEmpty(s)) 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;
}

/****************************************************************/

// isEmail (STRING s [, BOOLEAN emptyOK])
//
// Email 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
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isEmail (s)
{ if (isEmpty(s))
if (isEmail.arguments.length == 1) return defaultEmptyOK;
else return (isEmail.arguments[1] == true);

// is s whitespace?
if (isWhitespace(s)) return false;

// there must be >= 1 character before @, so we
// 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;
}

/****************************************************************/

// Checks to see if a required field is blank. If it is, a warning
// message is displayed...

function ForceEntry(objField, FieldName)
{
var strField = new String(objField.value);
if (isWhitespace(strField)) {
alert("You need to enter information for " + FieldName);
objField.focus();
objField.select();
return false;
}

return true;
}

/****************************************************************/

// Returns true if the string passed in is a valid number
// (no alpha characters), else it displays an error message

function ForceNumber(objField, FieldName)
{
var strField = new String(objField.value);

//if (isWhitespace(strField)) return true;

var i = 0;

for (i = 0; i < strField.length; i++)
if ((strField.charAt(i) < '0' || strField.charAt(i) > '9') && strField.charAt(i) != '-') {
//alert(FieldName + " must be a valid numeric entry. Please do not use commas or dollar signs or any non-numeric symbols.");
objField.focus();
return false;
}

return true;
}

// This function determines if the string passed in is a valid
// US zip code. It accepts either ##### or #####-####. If the
// string is valid, it returns true, else false.

function isZipcode(strZip)
{
var s = new String(strZip);

if (s.length != 5 && s.length != 10)
// inappropriate length
return false;


for (var i=0; i < s.length; i++)
if ((s.charAt(i) < '0' || s.charAt(s) > '9') && s.charAt(i) != '-')
return false;

return true;
}

/****************************************************************/

// This function ensures that a field is less than or equal to the
// Length passed in. You must call this function with the element
// name in your form (for example: "ForceLength(document.forms[0].txtElement)"
// as opposed to "ForceLength(document.forms[0].txtElement.value)"
// If the field's value is too large, an error message is displayed
// and false is returned, else true is returned.

function ForceLength(objField, nLength, strWarning)
{
var strField = new String(objField.value);

if (strField.length > nLength) {
alert(strWarning);
return false;
} else
return true;
}

