/**************************************************
 * AjaxHandler.js
 * 30.06.2008
 * Paul Krix
 **************************************************/

/**************************************************
 * usage is something like:

 * sending the request:
 ** if you want to recieve a response then :
	AjaxHandler.sendRequest(url, field)
 ** or if you don't want anything back from the cgi script then:
	AjaxHandler.sendRequest(url)

 ***************************************************/

var AjaxHandler = {	

	GetXmlHttpObject : function() { //may need to take 'handler' as an argument??
		var objXMLHttp = null
		try {
			//firefox, safari, opera
			objXMLHttp = new XMLHttpRequest()
		}
		catch (e) {
			//Internet explorer
			try {
				objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP")
			}
			catch (e) {
				objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP")
			}
		}

		if (objXMLHttp == null) {
			alert ("Browser does not support HTML Request")
			return
		}

		return objXMLHttp
	},
                

	sendRequest : function(url, response) {
		var xmlHttp = AjaxHandler.GetXmlHttpObject()
		//response can either be a field to update, the name of a span/div, or a function to run
		if (response) {
			if(typeof(response) == 'function') {
				//response is a function
				xmlHttp.onreadystatechange = function() {
					if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
						response(xmlHttp.responseText)
					}
				}
			} else if(typeof(response) == 'object'){
				//response is a field
				xmlHttp.onreadystatechange = function() {
					if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
						response.value = xmlHttp.responseText
					}
				}
			} else if(typeof(response) == 'string'){
				//response is the name of a span or div
				xmlHttp.onreadystatechange = function() {
					if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
						document.getElementById(response).innerHTML = xmlHttp.responseText
					}
				}
			}
		}
		xmlHttp.open("GET", url, true)
		xmlHttp.send(null)
	}
}
