﻿// JScript File

function browser () {
	if (window.XMLHttpRequest && !window.ActiveXObject){
		this.mozilla = true
	}
	else if (window.ActiveXObject) {
		// ie6 and below
		this.mozilla = false
	}			
}
//global variables
b = new browser();
xsl = "";
xml = "";
pHtml = "";
action = "";
needsHistory = true;

//adds functionality for loadXML function in mozilla
if (b.mozilla) {
	Document.prototype.loadXML = function (s) {
	   // parse the string to a new doc   
	   var doc2 = (new DOMParser()).parseFromString(s, "text/xml");
	      
	   // remove all initial children
	   while (this.hasChildNodes())
	      this.removeChild(this.lastChild);
	         
	   // insert and import nodes
	   for (var i = 0; i < doc2.childNodes.length; i++) {
	      this.appendChild(this.importNode(doc2.childNodes[i], true));
	   }
	}
}

function ajax(actionURL,xslToRun,functionCall,xHistory) {
	//sets function to call when done
	action = functionCall;
	//sets xsl passed to the global xsl variable
	xsl = xslToRun;
	
	//if this is the first time on the search page, don't need the ajax history
	//or if you just don't want any history set
	if (xHistory) {
		needsHistory = false;
	}
	
	//sets cursor to a wait icon
	if (b.mozilla) {
		document.body.style.cursor = 'wait';
	}
	
	// code for Mozilla, etc.		
	if (b.mozilla){
		try {
			xmlhttp=new XMLHttpRequest();
			xmlhttp.onreadystatechange=xmlhttpChange;			
			xmlhttp.open("GET",actionURL,true);
			xmlhttp.setRequestHeader("Content-Length","0");
			xmlhttp.send(null);
		}
		catch (e) {
			//good place to put fall back code
			alert("test2");
			alert(e.message);
			//set cursor back
			document.body.style.cursor = 'default';
		}
	}
	// code for IE
	else {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
		if (xmlhttp) {
			try {
				xmlhttp.onreadystatechange=xmlhttpChange;			
				xmlhttp.open("POST",actionURL,true);
				xmlhttp.send();
			}
			catch (e) {
				//good place to put fall back code
			alert("test3");
				alert(e.message);
				//set cursor back
				document.body.style.cursor = 'default';
			}
		}
	}
}

function xmlhttpChange () {
	if (xmlhttp.readyState == 4) {
		// if "OK"
		if (xmlhttp.status==200) {
			if (b.mozilla) {
				xml = xmlhttp.responseXML;
			}
			else {
				xml = xmlhttp.responseText;
			}
												
			if (xsl != "") {
				transformXML(xml); //processes the xml back from the action page and puts it in pHtml global
			}
			
			if (needsHistory) {
				ajaxHistory();
			}
			else {
				needsHistory = true;
				eval(action);
			}
			//set cursor back
			document.body.style.cursor = 'default';
		}
		else {
			//we should put our fall back code here
			alert("test");
			alert(xmlhttp.statusText);
			alert(xmlhttp.status);
			alert("Problem retrieving XML data")
			//set cursor back
			document.body.style.cursor = 'default';
		}
	}
}

function transformXML (xml) {
	if (b.mozilla) {
		xslDoc = document.implementation.createDocument("","",null);					
		xslDoc.async = false;
		xslDoc.loadXML(xsl);				
		
		xsltProcessor = new XSLTProcessor();
		xsltProcessor.importStylesheet(xslDoc);
		toDisplay = xsltProcessor.transformToFragment(xml, document)
		
		setPhtml(toDisplay.firstChild.innerHTML);
	}
	else if (!b.mozilla) {
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async=false;
		xmlDoc.loadXML(xml);
		
		xslDoc = new ActiveXObject("Microsoft.XMLDOM")
		xslDoc.async = false;
		xslDoc.loadXML(xsl);

		toDisplay = xmlDoc.transformNode(xslDoc);
		
		setPhtml(toDisplay);
	}
	else {
		alert('Your browser cannot handle this script');
	}
	xsl = "";				
}

function setPhtml(phtmlValue) {
	pHtml = phtmlValue;
}

i = 0
function ajaxHistory(theFrame) {
	document.getElementById('historyFrame').src = "ajaxHistory.asp?unique=" + i;
	i++;
}
