if (typeof(JSON) == "undefined") {
	JSON = {parse: function (j) { return eval('(' + j + ')'); }	} /* Create poor-mans JSON.parse if not supported by browser */
}

var ajaxdone;
var errordata;
var messagedata;
function request(config) {
	var callback = this; /* Set callback */
	this.config = config; /* Load config from parameter */
	this.uri = (!this.config.uri) ? window.location.href : this.config.uri; /* Request URI */
	this.method = (!this.config.method) ? "GET" : this.config.method; /* Request method, default to GET */
	this.data = (!this.config.data) ? null : this.config.data; /* Post data? */
	this.onReturn = this.config.onReturn; /* on return function */
	this.onErr = this.config.onErr; /* on error function */
	this.xmlhttp = (!window.XMLHttpRequest) ? new ActiveXObject("MSXML2.XMLHTTP") : new XMLHttpRequest(); /* Create Object */
	this.xmlhttp.open(this.method, this.uri, true); /* Open Connection */
	if (this.data.length) { this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); } /* Set content-type if we have post data */
	this.xmlhttp.send(this.data); /* Send post data, or empty request */
	this.xmlhttp.onreadystatechange = function() { /* Handle Callback */
		if (callback.xmlhttp.readyState == 4) {
			try {
				callback.responseData = JSON.parse(callback.xmlhttp.responseText); /* Parse response as JSON */
				callback.onReturn(callback); /* Run on return function and pass callback to the script */
			} catch(e) { callback.onErr(e); } /* Run on error if we can't parse the data */
		}
	}
}

function postForm(fr) { /* Post Form Function - Takes a form and creates a POST request */
	ajaxdone = undefined;
	pd = "";

	for (i=0;i<fr.elements.length;i++) { /* Run through each element to create post data */
		if ((fr.elements[i].type == "radio" || fr.elements[i].type == "checkbox" ) && fr.elements[i].checked) { pd += ((pd.length)?"&":"") + escape(fr.elements[i].name) + "=" + escape(fr.elements[i].value); }
		if (fr.elements[i].name && fr.elements[i].type != "radio" && fr.elements[i].type != "checkbox" && fr.elements[i].disabled != true ) { pd += ((pd.length)?"&":"") + escape(fr.elements[i].name) + "=" + escape(fr.elements[i].value); }
	}

	new request({

		uri: fr.action, /* Get ACTION from form parameters which points to page to post form to */
		data: pd + "&json=1", /* Post Data */
		method: "POST", /* Method */

		onReturn: function (callback) { /* Perform when the form is submitted */
			ajaxdone = true;
			if (!callback.responseData.errors) {
				messagedata = callback.responseData.messages;
				for (objname in callback.responseData.messages) {
				document.getElementById(objname).setAttribute("class","message");
				//document.getElementById(objname).innerHTML = callback.responseData.messages[objname];
				alert(callback.responseData.messages[objname]);
				}
			} else {
				errordata = callback.responseData.errors;
				for (objname in callback.responseData.errors) {
					document.getElementById(objname).setAttribute("class","error");
					document.getElementById(objname).innerHTML = callback.responseData.errors[objname];
				}
			}
		},

		onErr: function (e) { /* Perform when there's an error */
			alert("There was an error with the following: \n" + e.message);
		}

	});
	return false; /* Return false to stop the form submitting in the traditional way */
}

function clearResponses() {
	if (typeof(errordata) != "undefined") {
		for (objname in errordata) {
			document.getElementById(objname).innerHTML = "";
			document.getElementById(objname).removeAttribute("class");
		}
		errordata = undefined;
	}
	if (typeof(messagedata) != "undefined") {
		for (objname in messagedata) {
			document.getElementById(objname).innerHTML = "";
			document.getElementById(objname).removeAttribute("class");
		}
		errordata = undefined;
	}			

}
