//http://www.quirksmode.org/js/xmlhttp.html

/*********************** 
Usage: sendRequest('http://forum.omy.sg/omy_lastposting.php',handleRequest[,false]);
@param1: url of the feed
@param2: callback function, action to perform
@param3: true for "POST" action, false for "GET" action (max 512 bytes for "GET")

function handleRequest(req) {
	var writeroot = [some element];
	writeroot.innerHTML = req.responseText;
}

readyState
	0 = uninitialized
	1 = loading
	2 = loaded
	3 = interactive
	4 = complete
	
open("method","URL",async,"uname","pswd")
	The async parameter specifies whether the request should be handled asynchronously or not. 
	true means that script processing carries on after the send() method, without waiting for a response. 
	false means that the script waits for a response before continuing script processing	
***********************/
//searches a str eg. "http://forum.omy.sg/omy_lastposting.php?num=3" that was passed to proxy_curl.php
//for a variable, returns the corresp value
function getQryValueFrmStr(str, name) {
	var aryQry = str.split("?");
	aryQry = aryQry[1].split("&");
	for (var i=0; i<aryQry.length; i++) {
		var aryNameValue = aryQry[i].split("=");
		if (aryNameValue[0] == name) {
			return aryNameValue[1];	
		}
	}
	return false; //no such name found
}

function sendXMLRequest(url,callback,postData) {
	//netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
	var req = createXMLHTTPObject();
	if (!req) return;
	
	//somehow "GET" will not work on FF - so use "POST" for all
	var method = "POST";
	//use a php application proxy to get the xml response from cross-domain server
	var proxyUrl = '/global/common/php/curl_proxy.php?url='+ encodeURIComponent(url);
	
	req.open(method,proxyUrl,true); //3rd param is "async"
	
	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
	if (postData) req.setRequestHeader('Content-type','text/xml');
	//alert("getResponseHeader: "+req.getResponseHeader("headername")); //Returns the value of the specified http header
	//alert("getAllResponseHeaders: "+req.getAllResponseHeaders()); //Returns the complete set of http headers as a string
	
	req.onreadystatechange = function () {//alert('using req.onreadystatechange');
		if (req.readyState != 4) return;
		if (req.status != 200 && req.status != 304) {
			//alert('HTTP error ' + req.status + ' ' + req.statusText);
			return;
		}
		if (url.indexOf("num")!=-1) callback(req, getQryValueFrmStr(url, "num"));
		else callback(req, null);
	}
	if (req.readyState == 4) return;
	
	//need check cos IE7 is picky about this!!!
	//if (postData) req.send(postData);
	//else 
	if (document.implementation && document.implementation.createDocument) //FF
		req.send(postData);
	else req.send(); //IE
	
}

var XMLHttpFactories = [
	function () {return new XMLHttpRequest()},
	function () {return new ActiveXObject("Msxml2.XMLHTTP")},
	function () {return new ActiveXObject("Msxml3.XMLHTTP")},
	function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];

function createXMLHTTPObject() {
	var xmlhttp = false;
	for (var i=0;i<XMLHttpFactories.length;i++) {
		try {
			xmlhttp = XMLHttpFactories[i]();
		}
		catch (e) {
			continue;
		}
		break;
	}
	return xmlhttp;
}

function XMLtoString(elem){
	var serialized;
	try { // XMLSerializer exists in current Mozilla browsers
		serializer = new XMLSerializer();
		serialized = serializer.serializeToString(elem);
	} 
	catch (e) { // Internet Explorer has a different approach to serializing XML
		serialized = elem.xml;
	}
	
	return serialized;
}
