// This file contains general functions for using AJAX.
// Created by Christopher Docherty (21/01/2008)
function createHttpRequestObj(){
	/*
	Ths function creates an instance of the "XMLHttpRequest" object.
	The instance is created using different methods for different browsers.
	Once the instance of the object has been created, the function then returns it.
	*/
	// Declare the variable "HttpRequest" and give it an initial value of "null".
	var HttpRequest = null;
	// Check to see if the "window.XMLHttpRequest" exists.
	// This is for Mozilla, Safari etc
	if (window.XMLHttpRequest){
		// Creates an instance of the "XMLHttpRequest" object an assign it to the variable "HttpRequest".
		HttpRequest = new XMLHttpRequest();
		// Check to see if the "HttpRequest" object has a method called "overrideMimeType()".
		if (HttpRequest.overrideMimeType){
			// "HttpRequest" object DOES have a method called "overrideMimeType()".
			// Set the mime type to "text/xml" using the "overrideMimeType()".
			HttpRequest.overrideMimeType("text/xml");
		}
		// End of IF Estatement.
	}
	// End of IF Estatement.
	// Check to see if the "window.ActiveXObject" exists.
	// This is for Internet Explorer. 
	else if (window.ActiveXObject){
		// TRY to create an instance of the ActiveX Object "Msxml2.XMLHTTP".
		// This is a newer version of the "XMLHTTP" for Internet Explorer.
		try {
			HttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		// If the ActiveX Object "Msxml2.XMLHTTP" could not be created,
		// Then try to create it using an older version.
		catch (e){
			// TRY to create an instance of the ActiveX Object "Microsoft.XMLHTTP".
			// This is an version of the "XMLHTTP" for Internet Explorer.
			try{
				HttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){}
			// End of TRY and Catch.
		}
		// End of TRY and Catch.
	}
	// End of IF Else statement.
	// Return the "HttpRequest" object.
	return HttpRequest;
}
// End of function "createHttpRequestObj()"

function getXMLDocument(url){
	/*
	This function sends an HTTP request to an XML document in order to retrive XML data.
	The XML Document is depicted by the parameter that is passed to this function, "url".
	Once the XML Document has been retrieved, it will then be returned by the function.
	*!* Please note that this function does not send the request in asynchronous mode. (i.e. it will wait until the XML Document object has been obtaind before it does anything else)*!*
	*/
	// Create XmlHttpObject
	var xmlHttp = createHttpRequestObj();
	// Check to see if the "xmlHttp" object was created
	if (xmlHttp == null){
		// The "xmlHttp" object was NOT created
		alert ("Browser does not support HTTP Request");
		// Return a null value.
		return null;
	}
	// Build URL and append querystring variables
	url=url+"&sid="+Math.random();
	// Build the request.
	xmlHttp.open("GET", url, false);
	// Send the HTTP request.
	xmlHttp.send(null);
	// Get the XML Document object.
	var xmlDocument = xmlHttp.responseXML;
	// Return the XML Document object.
	return xmlDocument;
}
// End of function "getXMLDocument()"

function getNodeValue(currentNode){
	/*
	This function checks the "currentNode" object to see if it has any child elements or not.
	If it has, then it will obtain the node value of the first child and this will be returned by the function.
	If it hasn't, then the function will return an empty string.
	*/
	// Declare the variable "nodeValue"
	// This will be used to hold a value that will be returned.
	var nodeValue;
	// Check to see if the "currentNode" object has any child elements or not.
	if(!(currentNode.hasChildNodes() == true)){
		// "currentNode" does NOT have child elements.
		// Assign the variable "nodeValue" the value of an empty string.
		nodeValue = "";
	}
	else{
		// "currentNode" DOES have child elements.
		// Get the node value of the first child in this object and assign it to the variable "nodeValue"
		nodeValue = currentNode.firstChild.nodeValue;
	}
	// End of IF Else statement.
	// Return the variable "nodeValue"
	return nodeValue;	
}
// End of function "getNodeValue()"

function createQueryString(form){
	function isInArray(arr, val){
		for(x=0; x<arr.length; x++){
			if(arr[x] == val) return true;
		}
		// End of FOR Loop.
		return false;
	}
	/*
	This function creates a QueryString from all of the elements in a form that is reffered to by "form".
	*/
	// Get all of the form elements.
	// Put all of the form elements into the array "elements".
    var elements = form.elements;
	// Create a new array called "pairs".
    var pairs = new Array();
	// Loop through each element in the "elements" array.
    for (var i = 0; i < elements.length; i++) {
		// Check to see if the current element is a checkbox item.
		if(elements[i].type == "checkbox"){
			// Check to see if the item is NOT ticked. If so, then set the value of the current element to "off".  
			if(elements[i].checked == false) elements[i].value = "off";
		}
		// End of IF statement.
		// Check to see if the current element is a radio item.
		if(elements[i].type !== "radio"){
			// Assign the value of the "name" property of the current element to the variable "name".
			// Assign the value of the property of the current element to the variable "value".
			if ((name = elements[i].name) && (value = elements[i].value)){
				// Add the current name/value pair to the "pairs" array.
				pairs.push(name + "=" + encodeURIComponent(value));
			}
			// End of IF statement.
		}
		else{
			var radios = document.getElementsByName(elements[i].name);
			for(r=0; r<radios.length; r++){
				if(!isInArray(pairs, radios[r].name + "=" + encodeURIComponent(radios[r].value))){
					if(radios[r].checked == true){
						// Add the current name/value pair to the "pairs" array.
						pairs.push(radios[r].name + "=" + encodeURIComponent(radios[r].value));	
					}
					// End of IF statement.
				}
			}
			// End of FOR Loop.	
		}
		// End of IF Else statement.
    }
	// End of FOR Loop.
	// Join all elements in the array into a string and seperate them by the ampersand (&) character.
	// Return the string value.
    return pairs.join("&");
}
// End of function "CreateQueryString()".

function getLoader(sTxt){
	var loader = null;
	if(document.createElement("div")){
		loader = document.createElement("div");
		loader.setAttribute("id", "loader");
		
		var loadingImage = new Image();
		loadingImage.src = "/graphics/ajax-loader.gif";
		loadingImage.alt = "Loading";
		loadingImage.setAttribute("title", "Loading");
		loader.appendChild(loadingImage);
		
		if(sTxt == "") sTxt = "Loading...";
		
		var loadingText = document.createElement("p");
		loadingText.appendChild(document.createTextNode(sTxt));
		loader.appendChild(loadingText);
	}
	return loader;
}
