var digits = "0123456789";

var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz";

var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";


// whitespace characters
var whitespace = " \t\n\r";

var defaultEmptyOK = false;


function setHeight() {
	var copyH = parseInt(document.getElementById("copy").offsetHeight);
	var bodH = copyH + 60;
	var topH = copyH + 315;
	document.getElementById("bod").style.height = bodH + "px";
	document.getElementById("top").style.height = topH + "px";	
}



function validateForm() {
	var element;
	var iReturn=true;
	var strMessage;

	strMessage = "All elements on this form must be completed.\r\n";

	element = window.document.getElementById("firstname");
	if (!element || isWhitespace(element.value)) {
		alert(strMessage + "Please enter your First Name.");
		
		return false;
	}

	element = window.document.getElementById("lastname");
	if (!element || isWhitespace(element.value)) {
		alert(strMessage + "Please enter your Last Name.");
		
		return false;
	}

	element = window.document.getElementById("company");
	if (!element || isWhitespace(element.value)) {
		alert(strMessage + "Please enter your Company Name.");
		
		return false;
	}

	element = window.document.getElementById("address");
	if (!element || isWhitespace(element.value)) {
		alert(strMessage + "Please enter your Street Address.");
		
		return false;
	}

	element = window.document.getElementById("city");
	if (!element || isWhitespace(element.value)) {
		alert(strMessage + "Please enter your City.");
		
		return false;
	}

	element = window.document.getElementById("state");
	var state = null;
	if( element && element.selectedIndex > -1 ){

		state = element.options[element.selectedIndex].value;

		if( isWhitespace(state)) {
			alert(strMessage + "Please select a State.");
		
			return false;
		}
		else
		{
			var stValue = element.value;
			
			if ( state.length != 2 || state.substr(0,1) == "-" ){
				alert(strMessage + "Please select a state.");
		
				return false;
			}
		}
	}
	else
	{
		alert(strMessage + "Please select a State.");
		
		return false;
	}

	element = window.document.getElementById("zip");
	if (!element || isWhitespace(element.value)) {
		alert(strMessage + "Please enter your Zip Code.");
		
		return false;
	}

	element = window.document.getElementById("phone");
	if (!element || isWhitespace(element.value)) {
		alert(strMessage + "Please enter your Telephone Number.");
		
		return false;
	}

	element = window.document.getElementById("email");
	if (!element || isWhitespace(element.value)) {
		alert(strMessage + "Please enter a valid e-mail address.\nExample: name@server.domain.");
		
		return false;
	}
	else if (isEmail(element.value) == false) {
			alert(strMessage + "Please enter a valid e-mail address.\nExample: name@server.domain");
			
			return false;
		}

	return iReturn;
}


// added SRF 2008-08-31 to validate forms on landing pages

function validateLandingForm() {
	var element;
	var iReturn=true;
	var strMessage;

	strMessage = "Your form appears incomplete.\r\n";

	element = window.document.getElementById("username");
	if (!element || isWhitespace(element.value)) {
		alert(strMessage + "Please enter your Name.");
		
		return false;
	}

	element = window.document.getElementById("phone");
	if (!element || isWhitespace(element.value)) {
		alert(strMessage + "Please enter your Telephone Number.");
		
		return false;
	}

	element = window.document.getElementById("email");
	if (!element || isWhitespace(element.value)) {
		alert(strMessage + "Please enter a valid e-mail address.\nExample: name@server.domain");
		
		return false;
	}
	else if (isEmail(element.value) == false) {
			alert(strMessage + "Please enter a valid e-mail address.\nExample: name@server.domain");
			
			return false;
		}

	return iReturn;
}

// end SRF 2008-08-31



// 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;
}



// Removes all characters which appear in string bag from string s.

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;
}



// Removes all characters which do NOT appear in string bag 
// from string 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 whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }

    return returnString;
}



// Removes all whitespace characters from s.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.

function stripWhitespace (s){   
	return stripCharsInBag (s, whitespace);
}




// WORKAROUND FUNCTION FOR NAVIGATOR 2.0.2 COMPATIBILITY.
//
// The below function *should* be unnecessary.  In general,
// avoid using it.  Use the standard method indexOf instead.
//
// However, because of an apparent bug in indexOf on 
// Navigator 2.0.2, the below loop does not work as the
// body of stripInitialWhitespace:
//
// while ((i < s.length) && (whitespace.indexOf(s.charAt(i)) != -1))
//   i++;
//
// ... so we provide this workaround function charInString
// instead.
//
// charInString (CHARACTER c, STRING s)
//
// Returns true if single character c (actually a string)
// is contained within string s.

function charInString (c, s)
{   for (i = 0; i < s.length; i++)
    {   if (s.charAt(i) == c) return true;
    }
    return false;
}



// Removes initial (leading) whitespace characters from s.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.

function stripInitialWhitespace (s)

{   var i = 0;

    while ((i < s.length) && charInString (s.charAt(i), whitespace)){
		i++;
	}
    
    return s.substring (i, s.length);
}

// Returns true if character c is an English letter 
// (A .. Z, a..z).
//
// NOTE: Need i18n version to support European characters.
// This could be tricky due to different character
// sets and orderings for various languages and platforms.

function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) );
}



// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{   return ((c >= "0") && (c <= "9"));
}



// Returns true if character c is a letter or digit.

function isLetterOrDigit (c)
{   return (isLetter(c) || isDigit(c));
}



// isInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if all characters in string s are numbers.
//
// Accepts non-signed integers only. Does not accept floating 
// point, exponential notation, etc.
//
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
//
// By default, returns defaultEmptyOK if s is empty.
// There is an optional second argument called emptyOK.
// emptyOK is used to override for a single function call
//      the default behavior which is specified globally by
//      defaultEmptyOK.
// If emptyOK is false (or any value other than true), 
//      the function will return false if s is empty.
// If emptyOK is true, the function will return true if s is empty.
//
// EXAMPLE FUNCTION CALL:     RESULT:
// isInteger ("5")            true 
// isInteger ("")             defaultEmptyOK
// isInteger ("-5")           false
// isInteger ("", true)       true
// isInteger ("", false)      false
// isInteger ("5", false)     true

function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       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;
}


// isAlphabetic (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is English letters 
// (A .. Z, a..z) only.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
//
// NOTE: Need i18n version to support European characters.
// This could be tricky due to different character
// sets and orderings for various languages and platforms.

function isAlphabetic (s)

{   var i;

    if (isEmpty(s)) 
       if (isAlphabetic.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphabetic.arguments[1] == true);

    // 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 (!isLetter(c))
        return false;
    }

    // All characters are letters.
    return true;
}




// isAlphanumeric (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is English letters 
// (A .. Z, a..z) and numbers only.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
//
// NOTE: Need i18n version to support European characters.
// This could be tricky due to different character
// sets and orderings for various languages and platforms.

function isAlphanumeric (s)

{   var i;

    if (isEmpty(s)) 
       if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphanumeric.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-alphanumeric 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 or letter.
        var c = s.charAt(i);

        if (! (isLetter(c) || isDigit(c) ) )
        return false;
    }

    // All characters are numbers or letters.
    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 TWO character after the .
    if ((i >= sLength - 2) || (s.charAt(i) != ".")) return false;
    else return true;
}
