/**
 * Loads the specified image and then asynchronously calls the callback function when the image has been loaded
 *
 * @param div the div into which the image is to be loaded
 * @param href the href of the image to be loaded
 * @param callbackFunction a pointer to the callback function to be called when the <code>GET</code> completes
 * @return status which basically just tells if something bad happened
 */
function ajaxLoadImage(div, href, callbackFunction) { 
    var status = false;
    var xmlHttpRequest = getHttpRequest();

    // Build the callback function
    if ((div == null) ||
        (document.getElementById(div) == null)) {
        alert("Error processing request; no <div> specified or <div> invalid");
    } else if (xmlHttpRequest == null) {
        document.getElementById(div).innerHTML = 
            formatResponse("Error processing request; unable to create XMLHttpRequest object");
    } else if (href == null) {
        document.getElementById(div).innerHTML = 
            formatResponse("Error processing request; no image specified");
    } else {
        xmlHttpRequest.onreadystatechange = function() {
            callbackFunction(xmlHttpRequest, div);
        };

        // Finally, send the request
        sendRequest(xmlHttpRequest, "GET", href, null);
        status = true;
    }

    return status;
}

/**
 * <code>formatResponse</code> formats the text for inclusion in a <div>
 *
 * @param text the text to be formatted
 * @return the formatted text
 */
function formatResponse(text) {
    if ((text == null) ||
        (text.length == 0)) {
        text = "Nothing to format";
    }

    return "<p><strong>" + text + "</strong></p>";
}

/**
 * <code>getHttpRequest</code> returns a valid XMLHttpRequest object if possible
 *
 * @return either a valid <code>XMLHttpRequest</code> object or <code>null</code> value
 */
function getHttpRequest() {
    debug("In getHttpRequest()");

    var xmlHttpRequest = null;
    try {
        xmlHttpRequest = new ActiveXObject('Msxml2.XMLHTTP');
    } catch (e) {
        try {
            xmlHttpRequest = new ActiveXObject('Microsoft.XMLHTTP');
        } catch (e2) {
            try {
                xmlHttpRequest = new XMLHttpRequest();
            } catch (e3) {
                xmlHttpRequest = null;
            }
        }
    }

    return xmlHttpRequest;
}

/**
 * <code>sendRequest</code> sends the request to the server-side script to perform the request
 *
 * @param xmlHttpRequest a valid XMLHttpRequest request object
 * @param postOrGet a string which is either "POST" or "GET"
 * @param serverSideScript the server-side script to which the request will be sent
 * @param parameters for a POST request, the parameters passed to the post
 */
function sendRequest(xmlHttpRequest, postOrGet, serverSideScript, parameters) {
    // Validate arguments
    if ((xmlHttpRequest == null) ||
        (postOrGet == null) ||
        (postOrGet.length == 0) ||
        (serverSideScript == null) ||
        (serverSideScript.length == 0)) {
        return;
    }

    if ((postOrGet != "POST") &&
        (postOrGet != "GET")) {
        return;
    }

    if ((postOrGet == "POST") &&
        ((parameters == null) ||
         (parameters.length == 0))) {
        return;
    }

    xmlHttpRequest.open(postOrGet, serverSideScript,  true); 
    if (postOrGet == "POST") {
        xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlHttpRequest.send(parameters); 
    } else {
        xmlHttpRequest.send(null);
    }
}

/**
 * <code>addParameterByName</code> adds corresponding name value pair to the parameter string if possible
 *
 * @param name, the name of the element which is to be looked up
 * @return the input parameter string
 */
function addParameterByName(name) {
    var parameter = "";

    // Test for valid parameters
    if ((name == null) ||
        (name.length == 0)) {
        return parameter;
    }

    // Test for null pointers during the dereference operation
    // Add multiple select option (why is this so freakin' complicated???)
    var namedElements = document.getElementsByName(name);
    if ((namedElements != null) &&
	(namedElements.length > 0)) {
	var element = namedElements.item(0);
	if (element.type == 'select-multiple') {
	    for (var i = 0; i < element.options.length; i++) {
		if (element.options[i].selected) {
		    if (parameter.length > 0) {
			parameter = parameter.concat("&");
		    }
		    parameter = parameter.concat(name, "=", encodeURIComponent(element.options[i].value));
		}
	    }
	} else {
	    parameter = parameter.concat(name, "=", encodeURIComponent(element.value));
	}
    }

    return parameter;
}

/**
 * <code>addParameterById</code> adds corresponding name value pair to the parameter string if possible
 *
 * @param id, the id of the element which is to be looked up
 * @return the input parameter string
 */
function addParameterById(id) {
    var parameter = "";

    if ((id == null) ||
        (id.length == 0)) {
        return parameter;
    }

    // Test for null pointers during the dereference operation
    if ((document.getElementById(id) != null) &&
        (document.getElementById(id).value != null)) {
        parameter = id.concat("=", encodeURIComponent(document.getElementById(id).value));
    } else {
        parameter = id.concat("=");
    }

    return parameter;
}

