function getHTTPObject() {
	try {
		http = new XMLHttpRequest();
		return http;
	} catch (e) {
		var MSXML_XMLHTTP_PROGIDS = new Array(
			'MSXML2.XMLHTTP.5.0',
			'MSXML2.XMLHTTP.4.0',
			'MSXML2.XMLHTTP.3.0',
			'MSXML2.XMLHTTP',
			'Microsoft.XMLHTTP'
		);
		var success = false;
		for (var i = 0; ((i < MSXML_XMLHTTP_PROGIDS.length) && (!success)); i++) {
			try {
			http = new ActiveXObject(MSXML_XMLHTTP_PROGIDS[i]);
			success = true;
			} catch (e) {}
		}
		if(!success) {
			document.getElementById('ohGodItsAllGoneWrong').style.display = "block";
			document.getElementById('ohGodItsAllGoneWrong').innerHTML = "<h1 class=\"windypops\">Oh dear. This is quite a problem.</h1>Your browser doesn't appear to support a thingy called the XMLHttpRequest object.<br /><em>(I didn't really expect you to understand that, but it made me sound clever, right?)<br /><br />Unfortunately, this means that you won't be able to view anything but this page until you update to a browser with more bells and whistles. Sorry.</em>";
		} else {
			return http;   
		}
	}
}

function handleResponse() {
	if(http.readyState == 4) {
		if(http.status == 200) {
			document.getElementById('ohGodItsAllGoneWrong').style.display = "none";
			document.getElementById('formSitsHere').innerHTML = http.responseText;
			enableForms();
		} else {
			document.getElementById('ohGodItsAllGoneWrong').style.display = "block";
			switch(http.status) {
				case 404:
					document.getElementById('ohGodItsAllGoneWrong').innerHTML = "<h1 class=\"windypops\">Oh dear.</h1>The content could not be loaded due to a missing file. This is my fault.<br /><br />I'll make it up to you somehow, I promise. Dinner?";
					break;
				default: 
					document.getElementById('ohGodItsAllGoneWrong').innerHTML = "<h1 class=\"windypops\">Oh dear.</h1>An unknown error has occured. I'm very, very moderately sorry about that.";
					break;
			}
		}
	}
}

function makePOSTRequest(onready, url, parameters) {
	http.onreadystatechange = onready;
	http.open('POST', url, true);
	var timeoutRequest = window.setTimeout(function() {
							http.abort();
							document.getElementById('ohGodItsAllGoneWrong').innerHTML = "<h1 class=\"windypops\">Er..</h1>It looks like your request's gone and timed out. Sorry about that.";
							enableForms();
						}, 3000);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", parameters.length);
	http.setRequestHeader("Connection", "close");
	http.send(parameters);
	clearTimeout(timeoutRequest);
}

function makeGETRequest(onready, url) {
	http.onreadystatechange = onready;
	http.open('GET', url, true);
	var timeoutRequest = window.setTimeout(function() {
							http.abort();
							document.getElementById('ohGodItsAllGoneWrong').innerHTML = "<h1 class=\"windypops\">Er..</h1>It looks like your request's gone and timed out. Sorry about that.";
							enableForms();
						}, 3000);
	http.send(null);
	clearTimeout(timeoutRequest);
}

function loadLovelyMessage() {
	makeGETRequest(handleResponse, '/wp-content/themes/luckettopia/contact.php');
}

function postLovelyMessage() {
	var emailRegex  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(document.messageForm.whatTheyWouldLikeToSay.value === "") {
		document.getElementById('ohGodItsAllGoneWrong').style.display = "block";
		document.getElementById('ohGodItsAllGoneWrong').innerHTML = "<h1 class=\"windypops\">Hold on.</h1>You've got to enter a message before you can send a message.</em>";
	} else if(document.messageForm.theirNameThingy.value === "") {
		document.getElementById('ohGodItsAllGoneWrong').style.display = "block";
		document.getElementById('ohGodItsAllGoneWrong').innerHTML = "<h1 class=\"windypops\">Hold on.</h1>You've got to enter your name before you can send a message.";
	} else if(document.messageForm.theirEmailThingy.value === "") {
		document.getElementById('ohGodItsAllGoneWrong').style.display = "block";
		document.getElementById('ohGodItsAllGoneWrong').innerHTML = "<h1 class=\"windypops\">Hold on.</h1>You've got to enter your e-mail address before you can send a message.";
	} else if(!emailRegex.test(document.messageForm.theirEmailThingy.value)) {
		document.getElementById('ohGodItsAllGoneWrong').style.display = "block";
		document.getElementById('ohGodItsAllGoneWrong').innerHTML = "<h1 class=\"windypops\">Hold on.</h1>That's not your real e-mail address, is it?";	
	} else {
		var poststr = "update=true&subject=" + encodeURI(document.messageForm.theirSubjectThingy.value) + "&name=" + encodeURI(document.messageForm.theirNameThingy.value) + "&email=" + encodeURI(document.messageForm.theirEmailThingy.value) + "&message=" + encodeURI(document.messageForm.whatTheyWouldLikeToSay.value);
		makePOSTRequest(handleResponse, '/wp-content/themes/luckettopia/contact.php', poststr);
		disableForms();
	}
}

function disableForms() {
	var counter;
	for(counter = 0; counter < document.forms.length; counter++) {
		document.forms[counter].submit.disabled = true;	
	}
}

function enableForms() {
	var counter;
	for(counter = 0; counter < document.forms.length; counter++) {
		document.forms[counter].submit.disabled = false;	
	}
}

var http = getHTTPObject();
window.onload = loadLovelyMessage;