//AJAX scripts

function loadXML()						//AJAX request object
{
	this.url=null
	this.req=null
	var me=null							//Needed because when processStateChange is invoked, 
										//  'this' is unknown!
	
	this.request=function(url)			//XML request method
	{
		me=this
		if (window.XMLHttpRequest) {						//Generic Version
			req=new XMLHttpRequest()
			req.onreadystatechange=processStateChange
			req.open("GET",url,true);
			req.send(null);
		} else if (window.ActiveXObject) {					//Windows version
			req = new ActiveXObject("Microsoft.XMLHTTP")
			if (req) {
				req.onreadystatechange=processStateChange
				req.open("GET",url,true)
				req.send()
			}
		}
	}

	var processStateChange=function()
	{
//		alert(req.readyState+":"+req.statusText)
		if((req.readyState==4)&&(req.status==200)){
			var doc=req.responseXML.documentElement
			if(!doc){
				alert("Can't retreive this data. Please report this error!")
			}
			if(doc.getElementsByTagName('xmlerror').length==1){
				alert(doc.getElementsByTagName('xmlerror')[0].firstChild.data)
				window.location=window.location
				return
			}
			if(me.onComplete){
				me.onComplete(doc)
			}
		}
	}
	
	this.onComplete=function(doc){alert("Completion method not set!")}	//Completion handler
}

function fillFormValues(form,doc)		//Fills in form values based on data returned in XML document
{										//  (doc is the XML document)
	var ids=doc.childNodes
	for(var i=0;i<ids.length;i++){
		var tag=ids[i].tagName
		if(!tag) continue;
		var elmnt=eval("form."+tag)		//Get the element that is to receive the data
		if(!elmnt){						//If not in form, check to see if id=tag in whole doc
			elmnt=document.getElementById(tag)
			if(!elmnt) continue;			//If nowhere to be found, give up
		}
		var value=ids[i].firstChild? ids[i].firstChild.data:""	//Extract the value from the XML data

		if(!elmnt.tagName && elmnt.length){		//Check for RADIO BUTTONs (elmnt is the node list of radio buttons not an individual radio button);
			for(var j=0;j<elmnt.length;j++) elmnt[j].checked=(elmnt[j].value==value)	//Set the state of each button in the list
			continue;
		}
		switch(elmnt.tagName.toLowerCase()){
			case 'input':
				switch(elmnt.type.toLowerCase()){
					case 'hidden': 
					case 'text': elmnt.value=value; break;
					case 'checkbox': elmnt.checked=value; break;
				}
				break;
			case 'textarea': elmnt.value=value;break;
			case 'img': elmnt.src=value; break;
			case 'select':
				var eo=elmnt.options
				for(var j=0;j<eo.length;j++){
					eo[j].selected=(eo[j].value==value)
				}
				break;
		}
	}
	if(IN_DATA) rememberData(form)
}
	
