// This is a Javascript which runs on the client side, unlike the other scripts which are VBScript and
// run on the server.  This functions in this script are invoked by the contact_us.asp page.  These functions
// are mainly for cookie management.  All of the form field values are stored in cookies so that:
// 1. we can post data to the server side script and then return errors back to the client as cookies.
// 2. the form can display errors and re-fill its values once the server script re-posts the contact_us page.
// Note: this script is tightly bound to the contact_us.asp page and the contactscript VBscript.  Any changes
// to any of these pages have to be kept in sync.


// grab a specific cookie value from all the cookies for this document.  Cookies must be un-encoded
// because they have been encoded previously-- because spaces and other characters have to be encoded
function getCookieValue(cookieName)
{
	var cookieString = cookieName + "="; // look for the string "[cookieName]="
	var startCookie=document.cookie.indexOf(cookieString);
	if (startCookie != -1) {  
		startCookie= startCookie + cookieString.length; // start position for the value is the end of the cookie name
		var endCookie=document.cookie.indexOf(";", startCookie); // look for the end of the cookie value
		if (endCookie == -1) endCookie= document.cookie.length; // there might not be a ";"
		var cookieValue= document.cookie.substring(startCookie, endCookie); // grab the value and unencode it
		cookieValue = unescape(cookieValue);
		return cookieValue;
	}
	else {
		return (""); // return empty string if the cookie wasn't found
	}
}

// store all of the form values on this HTML page in cookies for this document
// the escape() function encodes the cookie since cookies can't have spaces and other stuff
function storeCookies()
{
	document.cookie= "firstNameField="+escape(document.contactInfo.firstNameField.value);
	document.cookie= "lastNameField="+escape(document.contactInfo.lastNameField.value)
	document.cookie= "phoneField="+escape(document.contactInfo.phoneField.value)
	document.cookie= "fromField="+escape(document.contactInfo.fromField.value)
	document.cookie= "subjectField="+escape(document.contactInfo.subjectField.value)
	document.cookie= "messageField="+escape(document.contactInfo.messageField.value);
}

// load all the form values on this HTML page from the cookies for this document
function loadFormValuesFromCookies()
{
	document.contactInfo.firstNameField.value=getCookieValue("firstNameField");
	document.contactInfo.lastNameField.value=getCookieValue("lastNameField");
	document.contactInfo.phoneField.value=getCookieValue("phoneField");
	document.contactInfo.fromField.value=getCookieValue("fromField");
	document.contactInfo.subjectField.value=getCookieValue("subjectField");
	document.contactInfo.messageField.value=getCookieValue("messageField");
}

// Read each error cookie that could have been set by the server.  Construct an error message string
// for each error so we can display it on the form
function getFormErrors() 
{
	var errorString = "";
	if (getCookieValue("noEmailError") == "1")
		errorString = "Please enter your E-Mail address.<br>";
	if (getCookieValue("noSubjectError") == "1")
		errorString += "Please enter a subject.<br>";
	if (getCookieValue("noMessageBodyError") == "1")
		errorString += "Please enter a message.<br>";
	if (errorString.length > 0) 
		errorString = "<b><font color='red'>There is a problem with the form:<br>" + errorString + "</b></font><br>";
	return errorString;
}

// When the form is first loaded, set the focus to the First Name Field, load any cookies if present
function startForm() 
{
	document.contactInfo.firstNameField.focus();
	loadFormValuesFromCookies();
}

// Clear all of the cookies for this document when the form is reset
function clearCookies() 
{
	document.cookie= "firstNameField=";
	document.cookie= "lastNameField=";
	document.cookie= "phoneField=";
	document.cookie= "fromField=";
	document.cookie= "subjectField=";
	document.cookie= "messageField=";
}

// If they posted successfully from the contact page, clear any previous errors stored in cookies
// so they can come back to the contact us page again and it won't show any errors.
function clearErrors()
{
	document.cookie= "noEmailError=";
	document.cookie= "noSubjectError=";
	document.cookie= "noMessageBodyError=";
}	
	
// Display the person's name to make the thank-you page a little warmer.
function displayName() {
	document.write(getCookieValue("firstNameField") + " " + getCookieValue("lastNameField"));
}

