
function getXMLHttpRequest() {
	if (window.ActiveXObject) {
		try {
			return new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				return new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e1) { return null; }
		}
	} else if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else {
		return null;
	}
}

function sendXMLRequest(url, params, callback, method) {
    var httpRequest = getXMLHttpRequest();
    var httpMethod = method ? method : 'GET';
    if (httpMethod != 'GET' && httpMethod != 'POST') {
        httpMethod = 'GET';
    }
    var httpParams = (params == null || params == '') ? null : params;
    var httpUrl = url;
    if (httpMethod == 'GET' && httpParams != null) {
        httpUrl = httpUrl + "?" + httpParams;
    }
		
    httpRequest.open(httpMethod, httpUrl, true);
    httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    httpRequest.onreadystatechange =  function() {
        eval(callback + "(httpRequest);");
    }
    httpRequest.send(httpMethod == 'POST' ? httpParams : null);
}
	
function displayXMLRequest(url, params, method, resultdiv) {

    if (document.getElementById("_loadingDiv") == null) {

                var sourceDiv = document.getElementById(resultdiv);
                sourceDiv.style.position = "relative";
                sourceDiv.style.zindex = 1;

                var objDiv = document.createElement('div');
                objDiv.id = "_loadingDiv";
                objDiv.style.border = "3px";
                objDiv.style.position = "absolute";
                objDiv.style.width = "100%";
                objDiv.style.align = "center";
                objDiv.style.zindex = 2;
                objDiv.style.top = (sourceDiv.offsetHeight / 2 - 15) + 'px';
                objDiv.style.left = (sourceDiv.offsetWidth / 2 - 75) + 'px';

                var objImg = document.createElement('img');
                objImg.src = "/images/ajax-loader.gif";
                objImg.style.width = 150;
                objImg.style.height = 32;

                objDiv.appendChild(objImg);

                document.getElementById(resultdiv).appendChild(objDiv);

            /*var objDiv = document.createElement('div');
            objDiv.id = "_loadingDiv";
            var objImg = document.createElement('img');
            objImg.src = "/images/loading.gif";
            objImg.style.position = "absolute";
            objImg.style.top = 300;
            objImg.style.left = 200;
            objImg.style.width = 820;
            objImg.style.height = 700;
            objDiv.appendChild(objImg);
            document.body.appendChild(objDiv);
            */
    } else {
            document.getElementById("_loadingDiv").style.display = "";
    }

    var httpRequest = getXMLHttpRequest();
    var httpMethod = method ? method : 'GET';
    if (httpMethod != 'GET' && httpMethod != 'POST') {
        httpMethod = 'GET';
    }
    var httpParams = (params == null || params == '') ? null : params;
    var httpUrl = url;
    if (httpMethod == 'GET' && httpParams != null) {
        httpUrl = httpUrl + "?" + httpParams;
    }
	
    httpRequest.open(httpMethod, httpUrl, true);
    httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    httpRequest.onreadystatechange =  function() {
        eval("displayResult(httpRequest, resultdiv);");
    }
    httpRequest.send(httpMethod == 'POST' ? httpParams : null);
}

function displayXMLRequestWithoutLoading(url, params, method, resultdiv) {

    var httpRequest = getXMLHttpRequest();
    var httpMethod = method ? method : 'GET';
    if (httpMethod != 'GET' && httpMethod != 'POST') {
        httpMethod = 'GET';
    }
    var httpParams = (params == null || params == '') ? null : params;
    var httpUrl = url;
    if (httpMethod == 'GET' && httpParams != null) {
        httpUrl = httpUrl + "?" + httpParams;
    }

    httpRequest.open(httpMethod, httpUrl, true);
    httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    httpRequest.onreadystatechange =  function() {
        eval("displayResult(httpRequest, resultdiv);");
    }
    httpRequest.send(httpMethod == 'POST' ? httpParams : null);
}

function displayXMLRequestWithReturnFunction(url, params, method, resultdiv, returnFunction) {
    var httpRequest = getXMLHttpRequest();
    var httpMethod = method ? method : 'GET';
    if (httpMethod != 'GET' && httpMethod != 'POST') {
        httpMethod = 'GET';
    }
    var httpParams = (params == null || params == '') ? null : params;
    var httpUrl = url;
    if (httpMethod == 'GET' && httpParams != null) {
        httpUrl = httpUrl + "?" + httpParams;
    }

    httpRequest.open(httpMethod, httpUrl, true);
    httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    httpRequest.onreadystatechange =  function() {
        eval("displayResultWithReturnFunction(httpRequest, resultdiv, returnFunction);");
    }
    httpRequest.send(httpMethod == 'POST' ? httpParams : null);
}

function displayResultWithImageDiv(req, resultdiv, imageDiv) {
    if (req.readyState == 4) {
        if (req.status == 200) {
            document.getElementById(resultdiv).innerHTML = req.responseText;
        } else if (req.status == 404) {
            alert("displayResult():404");
        } else if (req.status == 500) {
            alert("displayResult():500");
        }

        if (document.getElementById(imageDiv) != null) {
           document.getElementById(imageDiv).style.display = "none";
        }
    }
}

function displayResult(req, resultdiv) {
    if (req.readyState == 4) {
        if (req.status == 200) {
            document.getElementById(resultdiv).innerHTML = req.responseText;
        } else if (req.status == 404) {
            alert("displayResult():404");
        } else if (req.status == 500) {
            alert("displayResult():500");
        }
        if (document.getElementById("_loadingDiv") != null) {
           document.getElementById("_loadingDiv").style.display = "none";
        }
    }
}

function displayResultWithReturnFunction(req, resultdiv, returnFunction) {
	if (req.readyState == 4) {
		if (req.status == 200) {
			document.getElementById(resultdiv).innerHTML = req.responseText;
                        eval(returnFunction);
		} else if (req.status == 404) {
			alert("displayResult():404");
		} else if (req.status == 500) {
			alert("displayResult():500");
		}
	}
}
