///////////////////////////////////////////////////////////////////////////////
//
// Simple AJAX (Asynchronous JavaScript And XML) 1.0 - CircleTreeMedia
//
// 04/12/2006 Chris Given - Initial Revision
//
// 1. The AJAX Service
//   Every AJAX application has a back-end service that is required to respond
//   to your requests, this service can be set with the function AjaxSetService
//   as follows
//
// ] AjaxSetService("/MyAjax.aspx");
//
// 2. Callbacks
//   All AJAX calls in this implimentation have a callback, which is called
//   each time a request is completed, keep in mind this is a global variable so
//   simultanious AJAX calls should not be done asyncronously as they will
//   over-write the callback.
//
//   If you need to do asyncronous calls, I suggest using a global callback that
//   delegates to other handlers based on response content.
//
// ] Ajax(MyFunction, "DoesCustomerExist", "12151");
// ] function MyFunction(Data) {
// ]     alert(Data);
// ] }
//
// 2. Advanced calling functions
//   More advanced calls are AjaxGet and AjaxPost, they both accept 2 arguments
//   the first being a callback as seen above, the second is a object of type
//   AjaxMessage which has several functions
//
// ] AjaxGet(Callback, MessageObject)
// ] AjaxPost(Callback, MessageObject)
//
// 3. AjaxMessage object, calls with arbitrary numbers of arguments
//   The AjaxMessage is made up of only 1 useful public member function, the
//   others won't be much use when making a AJAX call, the one your intrested in
//   is called AddPair - it simply adds a key/value pair to the message, here
//   is a example of creating a AjaxMessage with 2 K/V pairs.
//
// ] var M = new AjaxMessage();
// ] M.AddPair("Type", "DoesCustomerExist");
// ] M.AddPair("CustomerID", "1241");
//
// 3.1. AjaxMessage debugging
//   Other members include GetXmlMessagePost and GetXmlMessageGet, these can be used
//   to get the raw string data used in the AJAX request, and could be used in
//   debugging if something really wierd happened.
//
// 4. Finalizing a AJAX call with AjaxMessage
//   Once a AjaxMessage is created, it can be sent two ways, GET or POST. With the
//   GET method all pairs are put into the URL string, and with POST method XML
//   will be POSTed to the AJAX page.
//
// ] AjaxGet(MyCallback, M);   -- or -- AjaxPost(MyCallback, M)
//
//
// 5. Responding with a callback
//   When the server responds to this AJAX request, the callback is called with 1
//   argument, defined like this.
//
// ] function MyCallback(Data) {
// ] }
//
//
// 6. Extracting data from the AJAX response
//   The function AjaxValue can be used to extract single values from the AJAX message
//   otherwise one can parse the Data element which is a XML object, see AjaxValue for
//   example of how to retreive specific data from a AJAX response
//
// ] var CustomerExists = AjaxValue(Data, "CustomerExists");
//
var AjaxCB = null;
var AjaxService = "/library/ajax/ajax.asp";

function G(n) {
	return document.getElementById(n);
}
function AjaxSetService(ServiceLocation) {
    AjaxService = ServiceLocation;
}
function AjaxMessage() {
    this.AddPair = AjaxMessageAddPair;
    this.GetXmlMessagePost = AjaxGetXmlMessagePost;
    this.GetXmlMessageGet = AjaxGetXmlMessageGet;
}
function AjaxMessageAddPair(K, V) {
    this[K] = V;
}
function AjaxGetXmlMessagePost() {
    var Data = '<XML>\n';
    for (K in this) {
        var V = this[K];
        if(typeof(V) == 'function')
            continue;

        Data+='<'+K+'>'+V+'</'+K+'>\n';
    }
    Data+= '</XML>\n';
    return(Data);
}
function AjaxGetXmlMessageGet() {
    var Data = '?';
    for (K in this) {
        var V = this[K];
        if(typeof(V) == 'function')
            continue;

        Data+=K+'='+escape(V)+'&';
    }
    Data = Data.substring(0, Data.length - 1);
    return(Data);
}

function AjaxPost(CB, Message) {
	AjaxCB = CB;
	if(window.XMLHttpRequest) {
		Request = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		Request = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if(Name) {
		Request.open("POST", AjaxService);
		Request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		Request.onreadystatechange = AjaxResponse;
		Request.send(Message.GetXmlMessagePost());
	}
}
function AjaxGet(CB, Message) {
	AjaxCB = CB;
	if(window.XMLHttpRequest) {
		Request = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		Request = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if(Message) {
		Request.open("GET", AjaxService + Message.GetXmlMessageGet());
		Request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		Request.onreadystatechange = AjaxResponse;
		Request.send(null);
	}
}
function Ajax(CB, Name, Value) {
    var M = new AjaxMessage();
    M.AddPair("Type", Name);
    M.AddPair("Value", Value);
    AjaxGet(CB, M);
}
function AjaxResponse() {
	var Response = 0;
	if(Request.readyState == 4) {
		if(Request.status == 200) {
			var itemNodes = Request.responseXML.getElementsByTagName("XML");
			AjaxCB(itemNodes[0]);
		}
	}
	return(Response);
}
function AjaxValue(Message, Name) {
	if(Message.getElementsByTagName(Name).length == 0) {
		alert('Item '+Name+' not found in XML message');
		return '';
	}
	if(Message.getElementsByTagName(Name)[0].childNodes.length == 0) {
		return '';
	}
	return(Message.getElementsByTagName(Name)[0].firstChild.nodeValue);
}
function AjaxStatus(SpanIdentifier, Status, Message) {
	var E = G(SpanIdentifier);
	if(E == null) {
		if(Status == 0) {
			alert('Success - ' + Message)
		}
		if(Status == 1) {
			alert('Error - ' + Message)
		}
	} else {
		E.innerHTML = Message;
		E.innerText = Message;
		if(Status == 0) {
			E.style.color = 'rgb(0, 150, 0)';
			E.style.fontWeight = '';
		}
		if(Status == 1) {
			E.style.color = 'rgb(190, 0, 0)';
			E.style.fontWeight = 'bold';
		}
		if(Status == 3) {
			E.style.color = 'rgb(0, 0, 0)';
			E.style.fontWeight = '';
		}
	}
}

