
//returns true if character is a base 10 digit
function isDigit( input ) {
	if ((input.charCodeAt(0) >= 48) && (input.charCodeAt(0) <= 57))
		return true;
	else
		return false;
}


// returns true if character is a letter
function isLetter ( input ) {
	if (((input.charCodeAt(0) >= 65) && (input.charCodeAt(0) <= 90)) ||
		((input.charCodeAt(0) >= 97) && (input.charCodeAt(0) <= 122)))
		return true;
	else
		return false;
}

// returns true if input is a spacebar character
function isSpace (input) {
	if (input.charCodeAt(0) == 32)
		return true;
	else
		return false;
}

// returns true if valid Mexican postal code
function isMexicanPostalCode( input ) {
	if (input.length != 5)
		return false;

	for (counter = 0; counter < input.length; counter++)
		if (!isDigit(input.charAt(counter)))
			return false;

	return true;
}

// returns true if input is a valid Canadian Postal Code
function isCanadianPostalCode( input ) {
	if (isLetter(input.charAt(0)) && isDigit(input.charAt(1)) &&
		isLetter(input.charAt(2)) && isSpace(input.charAt(3)) &&
		isDigit(input.charAt(4)) && isLetter(input.charAt(5)) &&
		isDigit(input.charAt(6)))
			return true;
	else
		return false;
}

// returns true if input is a valid 5 or 9 digit US Zip code
function isUSZip(input) {
	if (!(input.length == 5) && !(input.length == 9) && !(input.length == 10))
		return false;

	for (counter = 0; counter < input.length; counter++)
		if ((!isDigit(input.charAt(counter)) && (input.charCodeAt(counter) != 45)))
			return false;

	return true;
}

// returns true if input is a US state
function isUSState( input ) {
	states = new Array("AL", "AK", "AS", "AZ",
	"AR", "CA", "CO", "CT", "DE",
	"DC", "FL", "GA", "GU", "HI",
	"ID", "IL", "IN", "IA", "KS", "KY",
	"LA", "ME", "MD", "MA", "MI",
	"MN", "MS", "MO", "MT", "NE",
	"NV", "NH", "NJ", "NM", "NY",
	"NC", "ND", "OH", "OK", "OR",
	"PA", "PR", "RI", "SC",
	"SD", "TN", "TX", "UT", "VT",
	"VI", "VA", "WA", "WV",
	"WI", "WY");

	for (counter = 0; counter < states.length; counter++)
		if (states[counter] == input)
			return true;

	return false;
}

// returns true if input is a Canadian province or territory
function isCanadianProvince( input ) {
	provinces = new Array("AB", "BC", "MB",
	"NF", "NB", "NT",
	"NS", "NU", "ON", "PE",
	"QC", "SK", "YT");

	for (counter = 0; counter < provinces.length; counter++)
		if (provinces[counter] == input)
			return true;

	return false;
}

// returns input parameter stripped of all characters
// but base 10 digits
function filterForNumeric( input ) {
	var output = new String;
	var temp;

	for (counter = 0; counter < input.length; counter++){
		temp = input.charCodeAt(counter);
		if ( (temp >= 48) && (temp <= 57) ){
			output += input.charAt(counter);
		}
	}
	return output;
}

//returns true iff input contains only
//numeric characters or dashes (-)
function checkUSZip( input ) {
	var output = new String;
	var temp;

	for (counter = 0; counter < input.length; counter++){
		temp = input.charCodeAt(counter);
		// verify that is digit or dash
		if ( !((temp >= 48) && (temp <= 57)) && temp != 45 && temp != 150){
			return false;
		}
	}
	return true;
}


// returns true iff the input contains
// only base 10 digits
function verifyNumeric( input ) {
	var temp;
	for (counter = 0; counter < input.length; counter++){
		temp = input.charCodeAt(counter);
		if ( (temp < 48) || (temp > 57) )
			return false;
	}
	return true;
}

function isEmail(email) {
	// email at least 8 characters long
	if (email.length < 8)
		return false;
	// email has an @ as at least the second character
	if (email.indexOf("@") < 1)
		return false;
	// email has a . as the third or fourth from last character
	if ((email.lastIndexOf(".") != (email.length - 3)) &&
		(email.lastIndexOf(".") != (email.length - 4)))
			return false;
	// @ and . separated by at least 2 characters
	if ((email.lastIndexOf(".") - email.indexOf("@")) < 2)
		return false;

	// email does not contain any illegal characters
	var illegals = "()<>,;:\[]*/";
	for (i=0; i<illegals.length; i++) {
		if (email.indexOf(illegals.charAt(i)) != -1)
			return false;
	}

	return true;
}

//check that all form fields are filled out adequately
//return true (pass on form data to action URL) if so
//return false (do not send form data to action URL) otherwise
function checkForm( form ) {


	if ((form.email.value != "") && (form.first_name.value == "")
		&& (form.last_name.value == "") && (form.address.value == "")
		&& (form.city.value == "") && (form.zip.value == "")) {
			if (!isEmail(form.email.value)) {
				alert("The email address does not appear to be correct.");
				form.email.select();
				return false;
			}

	}
	else if (form.supporteremail.value == "") {

	var stateIndex = form.state.selectedIndex;
//	var countryIndex = form.country.selectedIndex;

	if (form.title1.value == "" && (form.title2.value == "Other" || form.title2.value == "")){
		alert("Please select a title.");
		form.title2.select();
		return false;
	}

	if (form.first_name.value == ""){
		alert("Please fill out the first name field.");
		form.first_name.select();
		return false;
	}

	if (form.last_name.value == ""){
		alert("Please fill out the last name field.");
		form.last_name.select();
		return false;
	}

	if (form.address.value == ""){
		alert("Please fill out the address field.");
		form.address.select();
		return false;
	}

	if (form.city.value == ""){
		alert("Please fill out the city field.");
		form.city.select();
		return false;
	}

//following can be deleted if SOE begins taking country related stuff
	// checks for USZip
	if ( !isUSZip(form.zip.value) ){
			alert("Please check the zip value.");
			form.zip.select();
			return false;
	}

	// checks Zip length
	if ( filterForNumeric(form.zip.value).length != 5 && filterForNumeric(form.zip.value).length != 9 ){
			alert("Please provide a five digit or nine digit zip code.");
			form.zip.select();
			return false;
	}


/*  SOE not currently asking for country related stuff
	// checks for state if USA or Canada
	if (((form.country.options[countryIndex].value == "USA") ||
		(form.country.options[countryIndex].value == "Canada"))
		&& (form.state.options[stateIndex].value == "")) {
			alert("Please fill out the state/province field.");
			form.state.focus();
			return false;
	}

	// checks for USZip if USA
	if ( (form.country.options[countryIndex].value == "USA")
		&& (!isUSZip(form.zip.value)) ){
			alert("Please check the zip/postal code value.");
			form.zip.select();
			return false;
	}

	// checks Zip length if USA
	if ((form.country.options[countryIndex].value == "USA") &&
		(filterForNumeric(form.zip.value).length != 5
		&& filterForNumeric(form.zip.value).length != 9)){
			alert("Please provide a five digit or nine digit zip code.");
			form.zip.select();
			return false;
	}

	// checks for Canadian Postal Code if Canada
	if ( (form.country.options[countryIndex].value == "Canada")
		&& (!isCanadianPostalCode(form.zip.value)) ){
			alert("Please make sure that the postal code field is correct.");
			form.zip.select();
			return false;
	}

	// checks for Mexican postal code is Mexico
	if ((form.country.options[countryIndex].value == "Mexico")
		&& (!isMexicanPostalCode(form.zip.value))) {
			alert("Please make sure that the postal code field is correct.");
			form.zip.select();
			return false;
	}

	// check that state and country match
	if (((form.country.options[countryIndex].value == "USA")
		&& !(isUSState(form.state.options[stateIndex].value)))
		|| ((form.country.options[countryIndex].value == "Canada")
		&& !(isCanadianProvince(form.state.options[stateIndex].value)))) {
			alert("Please check the state/province and country fields.");
			form.state.select();
			return false;
	}

	if (((form.state.options[stateIndex].value == "Other")
		&& (form.otherstate.value == "Other"))
		|| ((form.state.options[stateIndex].value == "Other")
		&& (form.otherstate.value == ""))
		|| ((form.state.options[stateIndex].value == "")
		&& (form.otherstate.value == "Other"))
		|| ((form.state.options[stateIndex].value == "")
		&& (form.otherstate.value == ""))) {
			alert("Please check the state fields.");
			form.state.select();
			return false;
	}

	if (((form.country.options[countryIndex].value == "Other") && (form.othercountry.value == "Other"))
		|| ((form.country.options[countryIndex].value == "Other") && (form.othercountry.value == "")) ) {
			alert("Please enter a country.");
			form.country.select();
			return false;
	}
*/
	if (form.email.value == ""){
	   alert("Please fill out the email field.");
	   form.email.select();
	   return false;
	}

	if (!isEmail(form.email.value)) {
		alert("The email address that was entered is not valid. Please revise.");
		form.email.select();
		return false;
	}
  }
  else {
  	if (form.supporteremail.value == ""){
	   alert("Please fill out the email field.");
	   form.email.select();
	   return false;
	}

	if (!isEmail(form.supporteremail.value)) {
		alert("The email address that was entered is not valid. Please revise.");
		form.email.select();
		return false;
	}

  }

	return true;
}
