<!--
var xmlHttp;

// Called when the form is submitted.  If form is valid, uses AJAX techniques
// to asynchronously send the form values to another jsp for processing and 
// sets an event handler method to process the results.
function submitForm()
{
	// Don't submit the form if it has invalid data
	if (!isFormValid()) 
	{
		return;
	}
	
	// Display message while waiting for the server response
	var statusMessageDiv = document.getElementById("statusMessage");
	statusMessageDiv.innerHTML = getWaitingMessage();

	// Query the server
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleStateChange;
	xmlHttp.open("GET", getRequestURL(), true);
	xmlHttp.send(null);
}

// Browser-independent method to create an XMLHttpRequest object
function createXMLHttpRequest()
{
	// Works for IE
	if (window.ActiveXObject)
	{
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	// Works for most other browsers
	else if (window.XMLHttpRequest)
	{
		xmlHttp = new XMLHttpRequest;
	}
}

// Event handler processes XHLHttpRequest state changes
function handleStateChange()
{
	// Request is done processing
	if (xmlHttp.readyState == 4)
	{
		// OK status
		if ((xmlHttp.status == 200)||(xmlHttp.status==0))
		{
			parseResults();
		}
		
		else
		{
			// TODO: Add any desired error handling
			// alert("xmlHttp.status = " + xmlHttp.status);
		}
	}
}

// Parse the response generated by the XMLHttpRequest
function parseResults()
{ 
	// Handle successful response (template-specific)
	if (xmlHttp.responseText.indexOf('success') > -1)
	{
		handleSuccessfulResponse();
	}
	
	// Display error / failure message
	else
	{
		var statusMessageDiv = document.getElementById("statusMessage");
	
		while (statusMessageDiv.hasChildNodes())
		{
			statusMessageDiv.removeChild(statusMessageDiv.childNodes[0]);
		}
	
		statusMessageDiv.innerHTML = xmlHttp.responseText;
	}
}

// Wrapper which calls encodes strings using encodeURIComponent and encodeSpecialCharacters
function encodeString(input)
{
	return encodeURIComponent(encodeSpecialCharacters(input));
}

// Replace each instance of "." with "#DOT#", "+" with "#PLUS#" and "/" with "#SLASH#".  
// This function is used to encode selector values that contain these characters.
// Note: encodeURIComponent() seems to take care of all the other special characters
// (and it works better than encode() and encodeURI()).
function encodeSpecialCharacters(input)
{
	// Note: the "." has to be escaped (i.e., "\.") otherwise it matches any character.
	var output = input.replace(/\./g, "#DOT#");
	output = output.replace(/\+/g, "#PLUS#");
	output = output.replace(/\//g, "#SLASH#");
	
	return output;
}

//-->


