//	AJAX functions
//
//	v 1.3 - SWC July 2008
//	Updates to placate FFox which parses the tree differently
//	v 1.2 - SWC - Sept 2007
//	Added DIV support and ability to specify target element (allowing for > 1 of same type).
//	v 1.1 - SWC - Mar 2006
//	enhanced with handling for SELECT as well as INPUT
//	v 1.0 -  first encapsulated version - SWC Feb 2006
//	Notes:
// we need an explicit DIV named "error" to hold returned error msgs
// the output from the table returned as string must not include "&"
	
// the HTTP Objectm - this global var is required
// NEED TO MAKE NON-GLOBAL!
var http=getHTTPObject(); 

// new object to simplify passing values about
objAJAX = new myObject();

function myObject()	{
	// constructor
	this.controlName="";
	this.controlType="";
	this.updateTarget="";
	this.updateInProgress=false;
}

function getHTTPObject() {
var xmlhttp;

	// conditional compilation, commented out for unsupporting browsers
	/*@cc_on

	@if (@_jscript_version >= 5) {
		try
			{	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");	}
		catch (e) 
			{
			try 
				{	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");	}
			catch (E) 
				{	xmlhttp = false;	}
			}
		}
	@else
		xmlhttp = false;	// catchall for non-supporting browsers
	@end @*/
 
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try
			{	xmlhttp = new XMLHttpRequest();	}
		catch (e) 
			{	xmlhttp = false;	}
	}
	return xmlhttp;
}

function updateData(myParam, targetControlName, myHandler) {
//  myParam = value from control that is initiating the change
//	targetControl = name of control to be updated
//	myHandler = optional custom procedure, rather than provided basic one
var url = "xml-UpdateBasket.asp?itemcode=";
var myControl;
var strTagType;

	if (!objAJAX.updateInProgress)	{

		objAJAX.updateInProgress=true;

		if (targetControlName.length) {

			// get a handle on the target control
//			myControl=eval("document.all."+targetControlName);
			myControl=document.getElementById(targetControlName);

			// find out what type it is, eg SELECT
			strTagType=myControl.tagName;

			// we can't pass params to an Event Handler, so we 
			// make them globally available via this object
			objAJAX.controlName=myControl.id;
			objAJAX.controlType=strTagType;
			objAJAX.updateTarget=targetControlName;
			myHandler=handleHttpResponse;
		}
		else	{
			// custom handler, ie control not handled here
		}
		
		// clear existing data in either case
		switch(strTagType) {
		
			case "SELECT" :
				myControl.length=0;
				break;
				
			case "INPUT" :
				myControl.value=0;
				break;
			
			case "DIV": 
				myControl.innerText="";
				break;
		}
		// initialise the request
		http.open("GET", url + escape(myParam), true);
		// trigger event handler for this request
		http.onreadystatechange=eval(myHandler);
		// trigger the request
		http.send(null);
	}
}

function handleHttpResponse() {
// this is the more generic handler, which we will hopefully expand
var xmlDocument = http.responseXML;
var i
var strError=""
var myValue
var myText
var strTagType 
var objErrorList 
var myOutput=""
var iQty=0

	if (http.readyState == 4)	{

		// operation complete
		if (http.status == 200) 	{
			
			// point to the HTML element 
			myControl=document.getElementById(objAJAX.controlName);

			// did we get an error?
			objErrorList = xmlDocument.getElementsByTagName('error');

			if (objErrorList.length) 	{
				for (i=0; i < objErrorList.length; i++)	{
					var x=xmlDocument.getElementsByTagName("error")[i]
					var y=x.childNodes[0];
					myValue=y.nodeValue;
					strError += myValue + "\n";
				}
				document.getElementById('Error').innerHTML = strError;
			}
			else
				{	document.getElementById('Error').innerHTML = ""; }

			// check to see if we got anything returned
			if (xmlDocument.getElementsByTagName("item").length)	{
				
				try	{
					// handle the control type appropriately						
					strTagType = objAJAX.controlType;

					switch(strTagType) {
					
						case "SELECT" :

							if (xmlDocument.getElementsByTagName("option").length)	{						
								// rebuild the SELECT entries
								// new Option([text, [value, [defaultSelected, [selected]]]])

								// add first item - "Please Select"
								myControl[myControl.options.length]=new Option("Please Select",'0',false,true);

								// loop for the rest of the list
								for (i=0;i<xmlDocument.getElementsByTagName("option").length;i++)	{
									if (xmlDocument.getElementsByTagName("value").item(0).hasChildNodes)	{
										myValue = xmlDocument.getElementsByTagName("value").item(i).firstChild.data;
										if (xmlDocument.getElementsByTagName("text").length)
											{	myText = xmlDocument.getElementsByTagName("text").item(i).firstChild.data;	}

										myControl[myControl.options.length]=new Option(myText,myValue);
									}
								}
								//	default is for first item to be selected
							}
							break;
							
						case "INPUT" :
							
							myValue = xmlDocument.getElementsByTagName('text').item(0).firstChild.data;
							myControl.value = myValue;
							break;

						case "DIV" :
							var objNodeList=xmlDocument.getElementsByTagName("item");
							var arrClass=new Array();
							arrClass[0]="AltRow";
							arrClass[1]="StdRow";

							if (objNodeList.length) {
								for (var i=0; i < objNodeList.length; i++)	{
									myOutput+="<div class='"+arrClass[i % 2]+"'>"

									var x=xmlDocument.getElementsByTagName("artist")[i]
									var y=x.childNodes[0];
									myValue=y.nodeValue;
									
									x=xmlDocument.getElementsByTagName("title")[i]
									y=x.childNodes[0];
									myOutput+=myValue+"&nbsp;/ <em>"+y.nodeValue+"</em>";
									
									x=xmlDocument.getElementsByTagName("quantity")[i]
									y=x.childNodes[0];
									//alert(y.nodeValue)
									iQty+=Math.round(y.nodeValue)
									
									myOutput+="</div>"
								}
							}
					}
					document.getElementById(objAJAX.updateTarget).innerHTML=myOutput;
				}
				catch(err)
					{	alert(err)	}
			}	//if (xmlDocument.getElementsByTagName("item").length)
			var trolleyStatus = document.getElementById("trolleyStatus");
	
			if (iQty)	{
				myOutput="You have "+iQty+" item"
				if (iQty>1)
					{	myOutput+="s"	} 
				myOutput+=" in your trolley:"
			}
			else
				{	myOutput+="<span class='ReflexRed'>No items returned</span>"	}
			trolleyStatus.innerHTML = myOutput;
		}
		else	{
			strError="Unable to retrieve Trolley details\n"
			if (http.status==500)
				{	strError+="due to a problem on the server.\n";	}
			else
				{	strError+="Reported error code was: "+http.status+"\n"	}
			strError+="Please refresh this page to update,\nor follow the link below:"
			document.getElementById('error').innerText = strError;
		}
		
		objAJAX.updateInProgress=false;
	}
	else	{
		document.getElementById('trolleyStatus').innerHTML=http.readyState+" - Updating...";
	}
}
	// ---      ends AJAX code ---