var http = null;
var nocookies = null;
var rqArray = new Array();

function setNoCookies(strCookie) {
	nocookies = strCookie;
	return true;
}


/* Handles a left to right hash and builds an array of elements to retrieve before looping over it all */
function loadHash() {
	var hash = window.location.hash;
	if(hash.length > 0) {
		var hashVal = hash.substring(1,hash.length);
		if(hash.length >= 2) {
			rqArray.push(hashVal.substring(0,2));
			if(hash.length >= 7) {
				rqArray.push(hashVal.substring(0,7));
			}
		}
	}
	rqArray.reverse()
	getResultArray();
}

function getResultArray() {
	getResults(rqArray.pop());
}

//Example of how to use the loadHash
function getResults(filter) {
	var dataUrl = 'act_getData.cfm?q='+filter;
	ajaxCall(dataUrl,displayResults,0,0);
	return true;
}

//function called once a result is returned from our lookup page
function displayResults() {
	//readystate of 4 means the request is complete
	if (http.readyState == 4) {
		//status code of 200 means OK (regular status codes)
		if (http.status != 200) {
			alert("page not found"+http.status);
			return false;
		} else {
			//If remote page returns session timeout, get them to log in again
			if (http.responseText == "Session timed out") {
				alert("This update failed because you have been logged out of the system.  Please refresh this page to re-enter your login information");
			} else if (http.responseText == "Bad request") {
				return false;
			} else {
				//do custom thingy
				if (rqArray.length > 0) {getResultArray();}
			}
		}
	} 
}

function ajaxCall(dataUrl,returnFunction,nocache,debug,returnVar) {
	//create a variable for handling requests to be reused
	var http = null;
	//If nocache is passed, make each call unique
	if (nocache != null && nocache == 1) {
		var dt = new Date();
		var dtString = ''+dt.getFullYear()+dt.getMonth()+dt.getDate()+dt.getHours()+dt.getMinutes()+dt.getMilliseconds();
		//check for cookie - if disabled then append request.nocookies
		dataUrl = dataUrl + '&dtm='+dtString;
	} 
	if (debug != null && debug == 1 ) {prompt('',dataUrl);};
	
	//try to create the xmlHttpRequest object with non-IE code first, else fallback on IE
	try {
		http = new XMLHttpRequest(); // non-IE
		} catch (error){
			try {
				http = new ActiveXObject("Microsoft.XMLHTTP"); // IE
			} catch (error) {
				return false;
			}
	}	
	// more error checking
	try {
		//open the http page
		http.open("GET", dataUrl , true);
	} catch (error) {
		alert('Page not found');
		return false;
	}
	//upon a change of status of the request for the lookup page, call the javascript handler
	http.onreadystatechange = function() {
		//readystate of 4 means the request is complete
		if (http.readyState == 4) {
			//status code of 200 means OK (regular status codes)
			if (http.status != 200) {
				alert('Page not found');
				return false;
			} else {
				returnFunction(http,returnVar);
			}
		} 
	}
	//close the connection (very important for memory leaks)
	http.send(null);
	return false;
}

