function ValidateTextElement(sElem, rePattern, sAlert)
{
	// Test regular expression
	if( !rePattern.test(eval(sElem + ".value")) )
	{
		// Show alert
		if( sAlert != "" )
			document.getElementById("formerror").innerHTML = sAlert + "<BR /><BR />";

		// Put focus on the element
		eval(sElem + ".select()");
		eval(sElem + ".focus()");
		
		// Go to top of page
		document.location.href = "#";
		return false;
	}
	
	return true;
}

function ValidateSelectElement(sElem, sAlert)
{
	// Make sure that the value that is selected is not empty
	if( eval(sElem + ".options[" + sElem + ".selectedIndex].value") == "" )
	{
		// Show alert
		if( sAlert != "" )
			document.getElementById("formerror").innerHTML = sAlert + "<BR /><BR />";

		// Put focus on the element
		eval(sElem + ".focus()");

		// Go to top of page
		document.location.href = "#";
		return false;
	}
	
	return true;
}

function PageQuery(q) 
{
	// Strip the question mark
	if( q.length > 1 ) 
		this.q = q.substring(1, q.length);
	else 
		this.q = null;

	// Create array to store key/value pairs
	this.keyValuePairs = new Array();
	if( q ) 
	{
		var pairs = this.q.split("&");
		for( var i = 0; i < pairs.length; i++ ) 
			this.keyValuePairs[i] = pairs[i];
	}

	// Function getKeyValuePairs
	this.getKeyValuePairs = function() 
	{ 
		return this.keyValuePairs; 
	}

	// Function getValue
	this.getValue = function(s) 
	{
		// Loop through pairs, split it and check for given name
		for( var i = 0; i < this.keyValuePairs.length; i++ ) 
		{
			// Check if this is it
			var pair = this.keyValuePairs[i].split("=");
			if( pair[0] == s )
				return pair[1];
		}
		return false;
	}

	// Function getParameters
	this.getParameters = function() 
	{
		// Build new array with just names
		var a = new Array(this.getLength());
		for( var i = 0; i < this.keyValuePairs.length; i++ ) 
			a[i] = this.keyValuePairs[i].split("=")[0];
		return a;
	}

	// Function getLength
	this.getLength = function() 
	{ 
		return this.keyValuePairs.length; 
	}
}

function QueryString(key)
{
	var page = new PageQuery(window.location.search);
	return unescape(page.getValue(key));
}

function CheckForErrors()
{
	// Get the error message from the query string if it exists
	var sError = QueryString("error");
	if( sError == "false" )
		sError = "";
	if( sError == "0" )
		sError = "Your information has been submitted. Thank you.";
	
	// Update the error message div
	document.getElementById("formerror").innerHTML = sError + (sError != "" ? "<BR /><BR />" : "");
}
