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

function isWhitespace (s)

{   var i;
    var whitespace = " \t\n\r";
    // 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;
}

function isEmail (s)
{   
   // 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;
}

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return false;
       else return (isInteger.arguments[1] == true);

    // 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 (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

function isUSPhoneNumber (s)
{   if (isEmpty(s)) 
       if (isUSPhoneNumber.arguments.length == 1) return false;
       else return (isUSPhoneNumber.arguments[1] == true);
    return (isInteger(s) && s.length == 10)
}

function stripCharsInBag (s, bag)

{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}

function checkUSPhone (theFieldvalue)
{   
	var phoneNumberDelimiters = "()- ";
	var normalizedPhone = stripCharsInBag(theFieldvalue, phoneNumberDelimiters)
       if (!isUSPhoneNumber(normalizedPhone))  {
          return false; 
       } else 
       {
          return true;
       }

}

// check form values in information request form
function checkForm()
{

	// perform checks
	// check for empty name field
	if (isEmpty(document.form1.name.value) 
		|| isWhitespace(document.form1.name.value))
	{
		alert("Please enter a name");
		return false;
	}

	// check for empty phone field
	if (!checkUSPhone(document.form1.phone.value)
		|| isWhitespace(document.form1.phone.value))
	{
		alert("Please enter a valid US phone number");
		return false;
	}

	// check for empty email address
	if (!isEmail(document.form1.email.value) || isEmpty(document.form1.email.value)
		|| isWhitespace(document.form1.email.value))

	{
		alert("Please enter a valid email address");
		return false;
	}

	// check for non empty message body
	if (isEmpty(document.form1.textarea.value)
		|| isWhitespace(document.form1.textarea.value))
	{
		alert("Please enter your information request");
		return false;
	}

	// all successfully validated
	return true;
}

// check form values in the mail form
function checkMailForm()
{


	// check for empty email address
	if (!isEmail(document.form1.email.value) || isEmpty(document.form1.email.value)
		|| isWhitespace(document.form1.email.value))

	{
		alert("Please enter a valid email address");
		return false;
	}

	// check for non empty message body
	if (isEmpty(document.form1.textarea.value)
		|| isWhitespace(document.form1.textarea.value))
	{
		alert("Please enter your mail text");
		return false;
	}

	// all successfully validated
	return true;
}