
function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if (browser == "Microsoft Internet Explorer") {
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        ro = new XMLHttpRequest();
    }
    return ro;
}


//appends the output of a php file to the supplied div
function addPhpToDiv(div, phpFileName, overwrite, customCallback) {
	var xmlHttp = createRequestObject();
	xmlHttp.open('get', phpFileName, true);
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4) {
			if (xmlHttp.responseText == 'notLoggedIn')
				window.location = "/index.php";
			else {
				if (overwrite)
					div.innerHTML = '';

				div.innerHTML += xmlHttp.responseText;
			}
			if (customCallback != undefined) {
				eval(customCallback());
			}
		}
	};
	xmlHttp.send("");
}


//submit a php file, and provide an errorDiv to write the error out to, and a successFunction - a javascript function to call on success
function submitToPhp(queryUrl, onErrorDiv, successFunction, poststr) {
	var xmlHttp = createRequestObject();
	var method = 'get';
	if (poststr != '')
		method = 'post';
	xmlHttp.open(method, queryUrl, true);
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4) {
			data = xmlHttp.responseText.split('\n');

			if (data[0] == 'error') {
				if (onErrorDiv != null) {
					onErrorDiv.innerHTML = "";
					for (var i = 1; i < data.length; i++) {
						onErrorDiv.innerHTML += data[i] + "<br>";
					}
				}
			}
			else { //success
				if (successFunction != null)
					successFunction();
			}

		}
	};
	if (poststr != '') {
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", poststr.length);
		xmlHttp.setRequestHeader("Connection", "close");
	}
	xmlHttp.send(poststr);
}

