/**
 * This file collects all AJAX related JavaScript frameworks, utils.
 */
 
/**Don't remove this*/
var net = new Object();

/**
 * Class that use XMLHttpRequest to load a page specified by url.<b>
 * @param url the page to load, which is specified by the url. required. 
 * @param onload the callback handler to called after the content is loaded. required.
 * @param onerror the callback handler to called when error occurs, can be the same as onload. Optional.
 * @param paramsForHandler params that need to pass to the callback handler, can be simple or complex object. Optional.
 * @param method the method used to send the request, can be GET or POST. Optional.
 * @param params the params need to be sent to jsp/servlet. Can be ";" seperated string, array, and XML. When using GET method, only ";" seperated string will work. Optional.
 * @author Legend Peng 
 */
net.ContentLoader = function (url, onload, onerror, paramsForHandler, method, params) {
    this.url = url;
    this.req = null;
    this.onload = onload;
    this.onerror = (onerror) ? onerror : this.defaultError;
    this.paramsForHandler = paramsForHandler;
    this.method = (method) ? method.toUpperCase() : "POST";
    this.params = (params) ? params.toUpperCase() : "";
    this.errMsg = "";
    this.loadPage(url);
};
net.ContentLoader.prototype = {loadPage:function (url) {
    if (window.XMLHttpRequest) {
        this.req = new XMLHttpRequest();
    } else {
        if (window.ActiveXObject) {
            this.req = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    if (this.req) {
        try {
            var loader = this;
            this.req.onreadystatechange = function () {
                loader.onReadyState.call(loader);
            };
            //if (this.method == "GET" && this.params.length > 0) {
			if (this.params.length > 0) {
            	this.url += (this.url.match(/\?/) ? '&' : '?') + params;
            }
            this.req.open(this.method, url, true);
            this.req.send(this.method == "POST" ? this.params : null);
        }
        catch (err) {
            this.onerror.call(this);
        }
    }
}, onReadyState:function () {
    var req = this.req;
    var ready = req.readyState;
    if (ready == 4) {
        var httpStatus = req.status;
        if (httpStatus == 200 || httpStatus == 0) {
            this.onload.call(this);
        } else {
        	this.errMsg = "ERROR fetching page: " + this.url + "\n\nReadyState: " + this.req.readyState + "\nStatus: " + this.req.status + "\nHeaders: " + this.req.getAllResponseHeaders();
            this.onerror.call(this);
        }
    }
}, defaultError:function () {
    alert(this.errMsg);
}
};

/**
 * Define the callback handler to work with fillDivWithPage
 */
function fillDivWithPageHandler(){
    //this.paramsForHandler.innerHTML = this.req.responseText;
	fillContent(this.paramsForHandler, this.req.responseText);
}

/**
 * Cross browser/ version DHTML content
 */
function fillContent($target, $content)
{    
    if (document.createRange)
	{
        var rng = document.createRange();
        rng.setStartBefore($target);
        var htmlFrag = rng.createContextualFragment($content);
        while ($target.hasChildNodes()) $target.removeChild($target.lastChild);
        $target.appendChild(htmlFrag);
    }
	else if (document.getElementById)
        $target.innerHTML=$content;
}

/**
 * Define the error callback handler to work with fillDivWithPage
 */
function fillDivWithPageErrorHandler(){
    this.paramsForHandler.innerHTML = this.errMsg;
    alert(this.errMsg);
}
/**
 * Load a specified page and fill the content into the spcified div block.
 * @param prmDivId the id of the div to fill page content with 
 * @param prmUrl the url of the page to load
 * @author Legend Peng 
 */
function fillDivWithPage(prmDivId, prmUrl) {
    var oDiv = document.getElementById(prmDivId);
    if (!oDiv) {
        alert("Cannot find this div in page: " + prmDivId);
        return;
    }
    window.status = "Loading page: " + prmUrl;

    //call the content loader to load the page, after the page is loaded/error, the callback handler will be called
    var loader=new net.ContentLoader(prmUrl,fillDivWithPageHandler,null,oDiv,null,null);
}
function fillDivWithPageByGet(prmDivId, prmUrl) {
    var oDiv = document.getElementById(prmDivId);
    if (!oDiv) {
        alert("Cannot find this div in page: " + prmDivId);
        return;
    }
    window.status = "Loading page: " + prmUrl;

    //call the content loader to load the page, after the page is loaded/error, the callback handler will be called
    var loader=new net.ContentLoader(prmUrl,fillDivWithPageHandler,null,oDiv,"GET",null);
}
function fillElementWithPage(el, prmUrl) {
    if (!el) {
        alert("Cannot find this Element in page: " + prmDivId);
        return;
    }
    window.status = "Loading page: " + prmUrl;

    //call the content loader to load the page, after the page is loaded/error, the callback handler will be called
    var loader=new net.ContentLoader(prmUrl,fillDivWithPageHandler,null,el,null,null);
}