//
// XML Processing Functions
//

// returns a cross browser XMLHttpRequest object
function Document_getXMLHTTP()
{
	var obj = null;
	if (document.all) {
		try {
			obj = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				obj =new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				obj = null;
			}
		}
	}
  if (!obj && typeof XMLHttpRequest != "undefined") {
    obj = new XMLHttpRequest();
  }
	return obj;
}

// create a cross browser XSLTProcessor object in xsltProcessor
function Document_loadStylesheet()
{
	this.xsltProcessor = null;
	if (document.all) {
		try {
			this.xsltProcessor = new ActiveXObject("Microsoft.XMLDOM");
			this.xsltProcessor.load(this.xslFileName);
		} catch (e) {
			this.xsltProcessor = null;
		}
	}
	if (!this.xsltProcessor && typeof XSLTProcessor != "undefined") {
		this.xsltProcessor = new XSLTProcessor();
		var xslReq = this.getXMLHTTP();
		xslReq.open("GET", this.xslFileName, false);
		xslReq.send(null);
  	this.xsltProcessor.importStylesheet(xslReq.responseXML);
	}
}

// does XSLT transform in a cross browser way
function Document_transform()
{
	if (typeof XSLTProcessor != "undefined") {
		var fragment = this.xsltProcessor.transformToFragment(this.xmlDoc, document);
		this.hostElement.innerHTML = "";
		this.hostElement.appendChild(fragment);
	} else {
		this.hostElement.innerHTML = this.xmlDoc.transformNode(this.xsltProcessor);
	}
}

// loads xmlFileName+param, and calls onLoadDocument when complete
function Document_loadDocument(param, form, method)
{
	param = param || '';
	if (form) {
		for (var i=0; i<form.elements.length; i++) {
			param += "&" + form.elements[i].name + "=" + encodeURI(form.elements[i].value);
		}
		param = param.replace(/^&/,'?');
	}
	if (this.env) {
		param = param.replace(/^\?/,'&');
		param = this.env + param;
	}

	this.xmlDocReq = this.getXMLHTTP();
	this.xmlDocReq.open(method || "GET", this.xmlFileName + param, true);
	this.xmlDocReq.onreadystatechange = new Function(this.varName + ".onLoadDocument()");
	if (method && method == "POST") {
		this.xmlDocReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8' );
		this.xmlDocReq.send('');
	} else {
		this.xmlDocReq.send(null);
	}
}

//
// called by loadDocument, calls either onReadyDocument
// or onErrorDocument
//
function Document_onLoadDocument()
{
	if (!this.xmlDocReq) {
		return;
	}
	if (this.xmlDocReq.readyState == 4) {
		if(this.xmlDocReq.status == 200) {
			this.xmlDoc = this.xmlDocReq.responseXML;
			this.onReadyDocument(this.xmlDocReq);
		} else {
			this.onErrorDocument(this.xmlDocReq);
		}
	}
}

//
// called by onLoadDocument, when document is ready, transforms the node, saves the results
// and displays the box
//
function Document_onReadyDocument(xmlDocReq)
{
	return;
}

//
// called by onLoadDocument, when there is an error loading the document
//
function Document_onErrorDocument(xmlDocReq)
{
	alert("There was a problem retrieving the XML data:\n" +
		xmlDocReq.statusText+","+xmlDocReq.status);
}

function Document_setXsl(xslFileName)
{
	// set the xsl
	this.xslFileName = xslFileName;
	this.xsltProcessor = null;
	this.loadStylesheet();
}

// create a new AutoCompleteBox based on some data, fixed by some stylesheet
function Document(xmlFileName, hostElementId, varName)
{
	//
	// XML Processing Functions
	//
	this.getXMLHTTP = Document_getXMLHTTP;
	this.loadStylesheet = Document_loadStylesheet;
	this.transform = Document_transform;

	//
	// Document Processing Functions
	//
	this.loadDocument = Document_loadDocument;
	this.onLoadDocument = Document_onLoadDocument;
	this.onReadyDocument = Document_onReadyDocument;
	this.onErrorDocument = Document_onErrorDocument;

	this.setXsl = Document_setXsl;
	
	// set the xml
	this.xmlFileName = xmlFileName;
	this.xmlDocReq = null;
	this.xmlDoc = null;
	this.env = '';

	// set our reference name
	this.varName = varName;
	this.hostElement = document.getElementById(hostElementId);
}
