window.onload=initPage;

var warnings = {
	"txtFname" : {
		"required" :"Please enter your first name.",
		"letters" :"Only Alphabetic letters are allowed in the first name field.",
		"err" :0
	},
	"txtLName" : {
		"required" :"Please enter your last name.",
		"letters" :"Only Alphabetic letters are allowed in the last name field.",
		"err" :0
	}
}

function initPage() {
	document.contactForm.txtFname.focus();
	document.getElementById("cmdSubmit").disabled = false;
	document.getElementById("txtEmail").className = "txtEmail";
	document.getElementById("txtEmail").onblur = validateEmailAddress; // assign
	// address
	// of
	// function
	// here.
	addEventHandler(document.getElementById("txtFname"), "blur", fieldIsFilled);
	addEventHandler(document.getElementById("txtFname"), "blur", fieldIsLetters);
	addEventHandler(document.getElementById("txtLName"), "blur", fieldIsFilled);
	addEventHandler(document.getElementById("txtLName"), "blur", fieldIsLetters);
}

function validateEmailAddress() {
	// need to get our request object and send to server
	// show status below.
	document.getElementById("txtEmail").className = "inProgress";
	request = createRequest();
	if (request == null) {
		alert("Sorry, unable to communicate with server. Are you using IE 5 on the Mac perhaps? "
				+ "Please try again later.");
	} else {
		var emailAddr = document.getElementById("txtEmail").value;

		var cleanEmailAddr = escape(emailAddr);

		// local serverURL for testing...
		/*
		 * var serverUrl =
		 * "http://localhost:8080/HurricaneTechnology/servlet/ValidateEmailServlet?emailAddressToTest=" +
		 * cleanEmailAddr;
		 */

		// actual HT Server for production
		// Could not get it working using localhost.
		var serverUrl = "http://www.hurricanetechnology.net/ValidateEmailServlet?emailAddressToTest="
				+ cleanEmailAddr;

		// callback to browser below

		request.onreadystatechange = showEmailAddressValidationStatus;
		request.open("GET", serverUrl, true);
		request.send(null); // null => not sending additional info.
	}
}

function showEmailAddressValidationStatus() {
	// display statuses
	if (request.readyState == 4) {
		if (request.status == 200) {
			if (request.responseText.length > 0) {
				// "OK" is hard-coded response from Servlet & Validation class.
				if (request.responseText.indexOf("OK") >= 0) {
					document.getElementById("txtEmail").className = "formatCorrect";
					document.getElementById("cmdSubmit").disabled = false;
				} else {
					document.getElementById("txtEmail").className = "formatIncorrect";
					// and dissable the save button!
					document.getElementById("cmdSubmit").disabled = true;
				}
			}
			// no error messages....
		} else
		// oops problem ... tell user.
		{
			alert("sorry, there is some system problem, please try later.");
		}
	}
}

function fieldIsFilled(e) {
	var me = getActivatedObject(e);
	if (me.value == "") {
		warn(me, "required");
	} else {
		unwarn(me, "required");
	}
}

function fieldIsLetters(e) {
	var me = getActivatedObject(e);
	var nonAlphaChars = /[^a-zA-Z]/;
	if (nonAlphaChars.test(me.value)) {
		warn(me, "letters");
	} else {
		unwarn(me, "letters");
	}
}

function warn(field, warningType) {
	var parentNode = field.parentNode;
	// create warning dynamically using eval() from JSON data structure at top of HT.js
	var warning = eval('warnings.' + field.id + '.' + warningType);
	if (parentNode.getElementsByTagName('p').length == 0) {
		var p = document.createElement('p');
		field.parentNode.appendChild(p);
		var warningNode = document.createTextNode(warning);
		p.appendChild(warningNode);
	} else {
		var p = parentNode.getElementsByTagName('p')[0];
		p.childNodes[0].nodeValue = warning;
	}
	document.getElementById("enroll").disabled = true;
}

function unwarn(field, warningType) 
{
	if (field.parentNode.getElementsByTagName("p").length > 0) {
		var p = field.parentNode.getElementsByTagName("p")[0];
		var currentWarning = p.childNodes[0].nodeValue;
		var warning = eval('warnings.' + field.id + '.' + warningType);
		if (currentWarning == warning) {
			field.parentNode.removeChild(p);
		}
	}
}