/**
 * Javascript library to support Ajax and related methods.
 *
 */
 
/** Globals **/
var gLoadingMessage = "Loading";
var gErrorMessage = "There was an error with your request.";
 
/**
 * Generic way to grab httpRequest and call a function with the information.
 *
 * @param The url to get the information from
 * @param The callback function to give the result to
 * @param Boolean to determine whether to return xml or text.
 * @param The block (div) to send the loading/error messages to.
 */
function makeHttpRequest(url, callback_function, return_xml, content_block) {
	var http_request = false;
	var errorMessage = "";
	
	// Get the request object
	http_request = getRequestObject();
	
	if (!http_request) {
		alert('Your browser doesn\'t support this feature.');
		return false;
	}
	
	// If we have a content_block that is useful, start the loading
	if (content_block != null) {
		displayLoadingMessage(content_block);
	}
	
	// Anony function to determine what to do with the results
	http_request.onreadystatechange = function() {
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				if (return_xml) {
                   eval(callback_function + '(http_request.responseXML)');
				} else {
					eval(callback_function + '(http_request.responseText)');
				}
			} else {
				errorMessage = "There was a problem with the request. " +
				               "(Code: " + http_request.status + ")";
				if (content_block != null) {
					displayErrorMessage(content_block,errorMessage);
				} else {
					alert(errorMessage);
				}
			}
		}
	}

	// Get the request info
	http_request.open('GET', url, true);
	http_request.send(null);
} // makeHttpRequest()

/**
 * Get a request object to use.
 */
function getRequestObject() {
	if (typeof XMLHttpRequest != "undefined") {
		return new XMLHttpRequest();
    }
    var msv = ["Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", 
               "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", 
               "Microsoft.XMLHTTP"];
    for(var j=0;j<=msv.length;j++) {
        try {
            A = new ActiveXObject(msv[j]);
            if (A) { 
              return new ActiveXObject(msv[j]);
            }
        } catch(e) { }
     }
     return false;
} // getRequestObject()

/**
 * Get the value of a node, returning a blank string if no value so
 * not an error.
 */
function getResultNodeValue(resultSet,element) {
	var returnValue = "";
	if (resultSet != null && element != null) {
		if (resultSet.getElementsByTagName(element)[0].firstChild) {
			returnValue = resultSet.getElementsByTagName(element)[0].firstChild.nodeValue;
		}
	}
	return returnValue;
} // getResultNodeValue()

/**
 * Clear out a block with blank spaces.
 *
 * @param The block to clear out.
 */
function clearBlock(block) {
	if (block != null) {
		if (block.type =='text' || block.type=='textarea') {
			block.value = "    ";
		} else{
			block.innerHTML = "    ";
		}
	}
} // clearBlock()

/**
 * Crawls the XML and returns a ul list representation.
 */
function crawlXML(doc) {
	var treeReturn = "";
	if (doc.hasChildNodes()) {
		treeReturn += '<ul><li>'+ doc.tagName +'> ';
		for(var i =0; i < doc.childNodes.length; i++) {
			crawlXML(doc.childNodes[i]);
		}
		treeReturn += '</li></ul>';
	} else {
		treeReturn += doc.nodeValue;
	}
	
	return treeReturn;
}  

/**
 * Display an error message in the block we are trying to load in.
 *
 * @param The block to put the loading message in.
 */
function displayErrorMessage(block,message) {
	var errorMessage = "";
	if (message == null) {
		message = gErrorMessage;
	}
	errorMessage = "<b style=\"color:red;\">" + message + "</b>";
	
	if (block.type =='text' || block.type=='textarea') {
		block.value = errorMessage;
	} else{
		block.innerHTML = errorMessage;
	}
} // displayErrorMessage()

/**
 * Print a loading message in the object that we are attempting to
 * load in.
 *
 * @param The block to put the loading message in.
 */
function displayLoadingMessage(block) {
	var loadingMessage = "<img src=\"/bigadmin/home/images/loading_1.gif\" width=\"16\" height=\"16\" hspace=\"3\"><b>" + gLoadingMessage + "...</b>";
	
	if (block.type =='text' || block.type=='textarea') {
		block.value = loadingMessage;
	} else{
		block.innerHTML = loadingMessage;
	}
} // displayLoadingMessage()

