// stores the reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject();

// retrieves the XMLHttpRequest object
function createXmlHttpRequestObject() {
	var xmlHttp = false;
	if (window.ActiveXObject) { // if running Internet Explorer
		try {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
		    xmlHttp = false;
		}
	}
	else { // if running Mozilla or other browsers
		try {
		    xmlHttp = new XMLHttpRequest();
		} catch (e) {
		    xmlHttp = false;
		}
	}
	if (!xmlHttp) 
	    debug("Error creating the XMLHttpRequest object.");
    return xmlHttp;
}

function callAjaxFunction(encUserId, ajaxKey, funcArgs, valparam) {
    var httpGetString = "http://" + document.domain + "/ajaxlib/ajax.html";
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
        xmlHttp.open("POST", httpGetString, true);
        var params = "uid=" + encUserId + "&ajaxkey=" + ajaxKey + "&ajaxargs=" + funcArgs + "&valparam="
                   + valparam;
        //Send the proper header information along with the request
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlHttp.setRequestHeader("Content-length", params.length);
        xmlHttp.setRequestHeader("Connection", "close");
        // define the method to handle server responses
        xmlHttp.onreadystatechange = handleServerResponse;
        // make the server request
        xmlHttp.send(params);
    }
}

//executed automatically when a message is received from the server
function handleServerResponse() {
	// move forward only if the transaction has completed
	if (xmlHttp.readyState == 4) { 
		if (xmlHttp.status == 200) { // status of 200 indicates the transaction completed successfully
			// obtain the document element (the root element) of the XML structure
            handleResponse(xmlHttp.responseText);
	    }
		else // a HTTP status different than 200 signals an error
		    debug("There was a problem accessing the server: " + xmlHttp.statusText);
    }
}

function handleResponse(res) {
    if (res.search(AJAX_SEPARATOR_1)==-1)
        return;
    var isEvil = (window.ActiveXObject);
    var elementsToChange = res.split(AJAX_SEPARATOR_1);
	for (var i = 0; i < elementsToChange.length; i++) {
	    var elementToChange = elementsToChange[i].split(AJAX_SEPARATOR_2);
	    if (elementToChange[0]==AJAX_FUNCTION_CALL_ELEMENT_ID) {
	       eval(elementToChange[1]);
	       continue;
	    }
        element = document.getElementById(elementToChange[0]);
        if (!element) {
            debug(elementToChange[0] + " does not exist!");
            continue; 
        }
        if (isEvil && elementToChange[1]=="onchange" && element.getAttribute("type")=="radio") {
            var funcToCall = new Function(elementToChange[2]);
            element.onpropertychange = function () {
                if (event.propertyName=='checked' && event.srcElement.checked)
                    funcToCall();
            }
        } else if (isEvil && elementToChange[1]=="onmouseover") {
            element.onmouseover = new Function(elementToChange[2]);
        } else if (isEvil && elementToChange[1]=="onclick") {
            element.onclick = new Function(elementToChange[2]);
        } else
	       element.setAttribute(elementToChange[1], elementToChange[2]);
	}
	/*
    if (window.ActiveXObject) { // if running Internet Explorer
        window.setTimeout("window.resizeBy(-1, -1)", 100);
        window.setTimeout("window.resizeBy(1, 1)", 200); 
    }
    */
}