// Javascript to validate contact information, including
// Names, Email Addresses, Phone Numbers, Addresses, City
// and State Names, and Zip or Postal Codes.

// WARNING: THIS SCRIPT AND /SM/SCRIPTS/VALIDATEVISITOR.JS MUST BE IDENTICAL

// Loosely based on an automatically generated script from
// Microsoft Frontpage '97

// ValidName(aName, FieldName)
// ValidEmail(EmailAddr, FieldName)
// ValidPhone(Phone, FieldName)
// ValidAddress(Address, FieldName)
// ValidCity(City, FieldName)
// ValidState(State, FieldName)
// ValidZip(State, FieldName)
// ValidPassword(Password, FieldName)

/* Use this as a template for the main validation routine

sub ValidateTheForm(theForm) {
	if ((theForm.FIELDNAME.length > 0) && (!Valid(theForm.FIELDNAME, "FIELDDESCRIPTION"))) {
		theForm.FIELDNAME.focus();
		return (false);
	}
	return (true);
}

*/

// ########## Validate a Name

function ValidName(aName, FieldName) {
// WARNING: THIS SCRIPT AND /SM/SCRIPTS/VALIDATEVISITOR.JS MUST BE IDENTICAL

	if (aName.value.length == 0) {
		alert("Please enter your name.");
		return (false);
	}

	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,.-ŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ ";
	var checkStr = aName.value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
		if (j == checkOK.length) {
			allValid = false;
			break;
		}
	}
	if (!allValid) {
		alert("Please enter only letters, spaces, commas, hyphens, or periods\n\nin the \"" + FieldName + "\" field.");
		return (false);
	}
	return (true)
}

// ########## Validate an Email Address

function ValidEmail(EmailAddr, FieldName) {
// WARNING: THIS SCRIPT AND /SM/SCRIPTS/VALIDATEVISITOR.JS MUST BE IDENTICAL

	 if (EmailAddr.value.length == 0) {
		alert("Please enter your email address in the \"" + FieldName + "\" field.");
		return (false);
	}
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ@._-&0123456789";
	var checkStr = EmailAddr.value;
	var allValid = true;
	var atfound = false;
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		if (ch == "@") {atfound = true}
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
		if (j == checkOK.length) {
			allValid = false;
			break;
		}
	}

	var email = EmailAddr.value;
	email = email.toLowerCase();
	var extension = email.substring(email.length-3, email.length);
	if (!((extension == "com") || (extension == "net") || (extension == "gov") || (extension == "org") || (extension == "edu"))) {
		allValid = false;
	}

	if (!allValid || !atfound) {
		alert("Please enter a valid email address in the \"" + FieldName + "\" field.\n\nThe domain must be .com, .net., .org, .gov, or .edu.");
		return (false);
	}
	return (true)
}

// ########## Validate a Phone Number

function ValidPhone(Phone, FieldName) {
// WARNING: THIS SCRIPT AND /SM/SCRIPTS/VALIDATEVISITOR.JS MUST BE IDENTICAL

	if (Phone.value.length < 10) {
		alert("Please enter a telephone number in the \"" + FieldName + "\" field.\n\nMake sure you have given us your area code.");
		return (false);
	}

	var checkOK = "0123456789()-ext. ";
	var checkStr = Phone.value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
		if (j == checkOK.length) {
			allValid = false;
			break;
		}
	}
	if (!allValid) {
		alert("Please enter only numerals, parentheses, dashes, \"ext.\"\n\,or spaces in the \"" + FieldName + "\" field.");
		return (false);
	}
	return (true)
}

// ########## Validate a Street or Mailing Address

function ValidAddress(Address, FieldName) {
// WARNING: THIS SCRIPT AND /SM/SCRIPTS/VALIDATEVISITOR.JS MUST BE IDENTICAL

	if (Address.value.length < 5) {
		alert("Please enter a valid address in the \"" + FieldName + "\" field");
		return (false);
	}

	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ0123456789-,./@#&()-\\:' ";
	var checkStr = Address.value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
		if (j == checkOK.length) {
			allValid = false;
			break;
		}
	}
	if (!allValid) {
		alert("Please enter only letters, digits, spaces, commas, periods, ampersands (\"&\") or the number symbol (\"#\") in the \"" + FieldName + "\" field.");
		return (false);
	}
	return (true)
}

// ########## Validate a City

function ValidCity(City, FieldName) {
// WARNING: THIS SCRIPT AND /SM/SCRIPTS/VALIDATEVISITOR.JS MUST BE IDENTICAL

	if (City.value == "") {
		alert("Please enter a value for the \"" + FieldName + "\" field.");
		return (false);
	}

	if (City.value.length < 2) {
		alert("Please enter at least 2 characters in the \"" + FieldName + "\" field.");
		return (false);
	}

	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ-,./&()'\\ ";
	var checkStr = City.value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
		if (j == checkOK.length) {
			allValid = false;
			break;
		}
	}
	if (!allValid) {
		alert("Please enter only letters, spaces, dashes, periods, commas,\n\nslashes, parentheses, or ampersands (\"&\") in the \"" + FieldName + "\" field.");
		return (false);
	}
	return (true)
}

// ########## Validate a State Name

function ValidState(State, FieldName) {
// WARNING: THIS SCRIPT AND /SM/SCRIPTS/VALIDATEVISITOR.JS MUST BE IDENTICAL

	if (State.value == "") {
		alert("Please enter a value for the \"" + FieldName + "\" field.");
		return (false);
	}

	if (State.value.length < 2) {
		alert("Please enter at least 2 characters in the \"" + FieldName + "\" field.");
		return (false);
	}

	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ-./&()\\ ";
	var checkStr = State.value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
		if (j == checkOK.length) {
			allValid = false;
			break;
		}
	}
	if (!allValid) {
		alert("Please enter only letters, spaces, dashes, periods, commas,\n\nslashes, parentheses, or ampersands (\"&\") in the \"" + FieldName + "\" field.");
		return (false);
	}
	return (true)
}

// ########## Validate a Country

function ValidCountry(Country, FieldName) {
// WARNING: THIS SCRIPT AND /SM/SCRIPTS/VALIDATEVISITOR.JS MUST BE IDENTICAL

	if (Country.value == "NULL") {
		alert("Please select the " + FieldName);
		return (false);
	}
	return (true);
}

// ########## Validate a Zip or Postal Code

function ValidZip(Zip, FieldName) {
// WARNING: THIS SCRIPT AND /SM/SCRIPTS/VALIDATEVISITOR.JS MUST BE IDENTICAL

	if (Zip.value == "") {
		alert("Please enter a valid Zip or postal code in the \"" + FieldName + "\" field.");
		return (false);
	}

	if (Zip.value.length < 5) {
		alert("Please enter  a valid Zip or postal code in the \"" + FieldName + "\" field.");
		return (false);
	}

	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ0123456789-,. ";
	var checkStr = Zip.value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
		if (j == checkOK.length) {
			allValid = false;
			break;
		}
	}
	if (!allValid) {
		alert("Please enter only letters, digits, spaces, dashes, periods, or commas in the \"" + FieldName + "\" field.");
		return (false);
	}
	return (true)
}

// ########## Validate a Password

function ValidPassword(PWord, FieldName) {
// WARNING: THIS SCRIPT AND /SM/SCRIPTS/VALIDATEVISITOR.JS MUST BE IDENTICAL

	if (PWord.value == "") {
		alert("Please enter a valid password in the \"" + FieldName + "\" field.");
		return (false);
	}

	// var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ0123456789";
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ0123456789~`!@#$%^&*()_+=-{}|[]\;:?><,./";
	var checkStr = PWord.value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
		if (j == checkOK.length) {
			allValid = false;
			break;
		}
	}
	if (!allValid) {
		alert("Please enter only letters, numbers, spaces, or symbols\n\nOTHER THAN single- or double-quotes in the \"" + FieldName + "\" field.");
		return (false);
	}
	return (true)
}

// ########## Validate the form ##########

function Validate(theForm) {
// WARNING: THIS SCRIPT AND /SM/SCRIPTS/VALIDATEVISITOR.JS MUST BE IDENTICAL

	if (! ValidName(theForm.Name, "Your Name")) {
		theForm.Name.focus();
		return (false);
	}
	if (! ValidEmail(theForm.EmailAddr, "Email Address")) {
		theForm.EmailAddr.focus();
		return (false);
	}
	if (! ValidCity(theForm.City, "City")) {
		theForm.Name.focus();
		return (false);
	}
	if ((theForm.StateSelector.selectedIndex == 0) && (theForm.OtherState.value == "In Other Country" || theForm.OtherState.value == "" || theForm.OtherState.value.length < 5)) {
		alert("You must give the state or province in which you live.");
		theForm.StateSelector.selectedIndex = 0;
		theForm.OtherState.value = "In Other Country";
		theForm.StateSelector.focus();
		return (false);
	}
	if (! ValidCity(theForm.State, "State")) {
		theForm.StateSelector.selectedIndex = 0;
		theForm.OtherState.value = "In Other Country";
		theForm.StateSelector.focus();
		return (false);
	}
	if (theForm.Country.selectedIndex == 0) {
		alert("You must give the country in which you live.");
		theForm.Country.focus();
		return (false);
	}
	return (true);
}

function ValidateVisitorAcct(theForm) {
// WARNING: THIS SCRIPT AND /SM/SCRIPTS/VALIDATEVISITOR.JS MUST BE IDENTICAL

	// Assign State
	if ((theForm.OtherState.value != "") && (theForm.OtherState.value != "In Other Country")) {
		theForm.State.value = theForm.OtherState.value;
	}
	else {
		theForm.State.value = theForm.StateSelector.value;
	}

	if (theForm.Password.value != theForm.ConfirmPassword.value) {
		alert("The Passwords that you entered do not match.");
		theForm.Password.value = "";
		theForm.ConfirmPassword.value = "";
		theForm.Password.focus();
		return (false);
	}

	if (theForm.Password.value.length < 6) {
		alert("Your password must have at least 6 characters.")
		theForm.Password.value = "";
		theForm.ConfirmPassword.value = "";
		theForm.Password.focus();
		return (false);
	}

	return (Validate(theForm));
}

function SetOtherState(SelectField, OtherField, CountryField) {
// WARNING: THIS SCRIPT AND /SM/SCRIPTS/VALIDATEVISITOR.JS MUST BE IDENTICAL

	if (SelectField.value != "NULL") { // Check if US/Canada field is set
		OtherField.value = "In Other Country";
		// if Country field is NOT set to US or Canada, set it to NULL
	}
}

function SetXState (SelectField, OtherField, CountryField) {
// WARNING: THIS SCRIPT AND /SM/SCRIPTS/VALIDATEVISITOR.JS MUST BE IDENTICAL

	if ((OtherField.value != "In Other Country") && (OtherField.value != "")) { // Check if "Other" field is not default
		SelectField.selectedIndex = 0;
		// if Country field is set to US or Canada, set it to NULL
		CountryField.selectedIndex = 0;
	}
}

function ValidateLogin(theForm) {
// WARNING: THIS SCRIPT AND /SM/SCRIPTS/VALIDATEVISITOR.JS MUST BE IDENTICAL

	if ((theForm.EmailAddr.value.length == 0) || ! ValidEmail(theForm.EmailAddr, "Email Address")) {
		alert("Please enter a valid email address to log in.");
		theForm.EmailAddr.focus();
		return (false);
	}
	if (theForm.Password.value.length == 0) {
		alert("Please enter your password to log in.");
		theForm.Password.focus();
		return (false);
	}
	return (true);
}

