/*
File with AJAX functions:
-request a page with post data
-get the xmlhttprequest object
*/


/* A httpobjects state has changed: load the page  */
function stateChange(httpobject, handler) {
	if (httpobject.readyState==4) {
		if (httpobject.status==200) {
			if (handler)
				handler(httpobject, true);
		} else {
			if (handler)	
				handler(httpobject, false);
		}
	} 
}  


/*
Loads an (xml)page
page: url of webpage
postdata: data sended by post
type: kind of action, used by stateChange() to recognize the root of the page  
*/
function loadXMLDoc(page, postdata, handler) {
	var xmlhttp = getXMLHTTPRequest();

	var url = page;		
	
	xmlhttp.open("POST",url, true);

	xmlhttp.onreadystatechange = function() {stateChange(xmlhttp, handler);};
	
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", postdata.length); 
	try {
		xmlhttp.send(postdata);
	} catch (err1) {
		stateChange(xmlhttp, handler);
	}
}

// Function gets the XMLHTTPRequest for use with js.
function getXMLHTTPRequest() {
	try {
		req = new XMLHttpRequest();
	} catch (err1) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (err2) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (err3) {
				req = null;
			}
		}
	}	
	return req;
}




