// JavaScript Document

function clearFocus(theText) {
	if (theText.value == theText.defaultValue) {
		theText.value = "";
		theText.style.color = "#000000";
	}
}

function resetBlur(theText) {
	if (theText.value == "") {
		theText.value = theText.defaultValue;
		theText.style.color = "#000000";
	}
}

//Browser Support Code
function ajaxFunction(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 1)
		{
		var ajaxDisplay = document.getElementById('ajaxDiv');
		ajaxDisplay.innerHTML = "<div id='ajax' style='padding-right:35px;'><img src='/images/layout/ajax-loader.gif'><h2 style='padding:7px 0px 0px 40px;'>Loading...</h2></div>";
		}
		else if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById('ajaxDiv');
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		}
	}
	var name = document.getElementById('name');
	var email = document.getElementById('email');
	var comments = document.getElementById('comments');
 if (validate(name,email)) {
	var queryString = "?name=" + name.value + "&email=" + email.value + "&comments=" + comments.value;
	ajaxRequest.open("GET", "/process-form.php" + queryString, true);
	ajaxRequest.send(null); 
	}
}

function validate(name,email) {
	var why = "";
	var elem;
	var emailFilter=/^.+@.+\..{2,3}$/;
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
	
	elem = name;
	if (elem) {
		if (elem.value=='' || elem.value==elem.defaultValue) {
			why += "Please enter your name.\n";
		}
	}
	
	elem = email;
	if (elem) {
		if (elem.value=='' || elem.value==elem.defaultValue) {
			why += "Please enter your e-mail address.\n";
		}
		else if (!emailFilter.test(elem.value) || elem.value.match(illegalChars)) { 
			why += "Please enter a valid e-mail address.\n";
		}
	}

	if (why != "") {
		alert('There are problems with your request:\n\n'+why);
		return false;
	}
	else {
		return true;
	}	
}