/** 
 * The content of the file.
 * User can access this variable when the download 
 * is done.
 */
var strFileContent;
var getfile_loaded=1;

/**
 * This method uses a couple different methods of instantiating the
 * XMLHttpRequest object. The reason we do this is so we can support
 * multiple browser (I've only tested in IE and Firefox).
 */
function _GET_XMLHTTPRequest()
{
    var request;
    
    // Lets try using ActiveX to instantiate the XMLHttpRequest object
    try{
        request = new ActiveXObject("Microsoft.XMLHTTP");
    }catch(ex1){
        try{
            request = new ActiveXObject("Msxml2.XMLHTTP");
        }catch(ex2){
            request = null;
        }
    }

    // If the previous didn't work, lets check if 
    // the browser natively support XMLHttpRequest 
    if(!request && typeof XMLHttpRequest != "undefined"){
        //The browser does, so lets instantiate the object
        request = new XMLHttpRequest();
    }

    return request;
}

/**
 * Download a file from url. 
 * and call the cb function.
 * Please do not call this function before the download is done.
 * If there are two calls at the same time,
 * the behavior is undefined.
 */
function getFile(url, cb)
{
    //Instantiate a new XMLHttpRequest object
    var req = _GET_XMLHTTPRequest();
    
    //Make sure the XMLHttpRequest object was instantiated
    if (req)
    {
        //Tell the XMLHttpRequest object what we want it to do.
        //In the first parameter we're telling it to use HTTP GET for the request
        //In the second parameter we're telling it what page to request
        //In the third parameter we're telling it to do the request asychronously
        req.open("GET", url, true);

        //Lets define the method that gets called when the request finishes
	req.onreadystatechange = function (aEvt) {
	    // Any time the readyState of the XMLHttpRequest object changes this method is called.
	    // Loading is complete when the readyState equals 4, 
            // so that's the only value we care about right now.
    	    if(req.readyState == 4){
        	strFileContent = req.responseText;

		if (typeof cb == "function") {
		    cb(strFileContent);
		} else {
		    alert("Bad Programmer");
		}
	    }
	};
	
	//Lets fire off the request
        req.send(null);
    }
    else
    {
        // Oh no, the XMLHttpRequest object couldn't be instantiated.
	cb("Your browser is too old to load comments.");
    }
}


function postRequest(url, params, cb)
{

    //Instantiate a new XMLHttpRequest object
    var req = _GET_XMLHTTPRequest();

    //Make sure the XMLHttpRequest object was instantiated
    if (req)
    {
        //Tell the XMLHttpRequest object what we want it to do.
        //In the first parameter we're telling it to use HTTP POST for the request
        //In the second parameter we're telling it what page to request
        //In the third parameter we're telling it to do the request asychronously
        req.open("POST", url, true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.setRequestHeader("Content-length", params.length);
	req.setRequestHeader("Connection", "close");
	
        //Lets define the method that gets called when the request finishes
	req.onreadystatechange = function (aEvt) {
	    // Any time the readyState of the XMLHttpRequest object changes this method is called.
	    // Loading is complete when the readyState equals 4, 
            // so that's the only value we care about right now.
    	    if(req.readyState == 4){
		if (typeof cb == "function") {
		    cb();
		} else {
		    alert("Bad Programmer");
		}
	    }
	};
	//Lets fire off the request
        req.send(params);
    }
    else
    {
        // Oh no, the XMLHttpRequest object couldn't be instantiated.
	cb("Your browser is too old.");
    }
}

