// /javascript/ajaxNavigation.js
var xmlHttp;
var responseDivHolder;
var responseTextHolder;
//-----------------------------
window.onload = function() {
	dhtmlHistory.initialize();
	dhtmlHistory.addListener(recallHistory);
}
//-----------------------------
window.dhtmlHistory.create();
var recallHistory = function(command, historyData) {
	var url = "/application.php?";
	if (command) { 
		//do something;
		var pathArray = command.split("/");
		var statusMessage = "Getting " + pathArray[1];
		if(pathArray[2]){ statusMessage += " " + pathArray[2]; }
		if(pathArray[3]){ statusMessage += " " + pathArray[3]; }
		status(statusMessage);
		url = url + "doAction=" + pathArray[1];
		if (pathArray[2]){
			url = url + "&command2=" + pathArray[2];
		}
		if (pathArray[3]){
			url = url + "&command3=" + pathArray[3];
		}
		if (pathArray[4]){
			url = url + "&command4=" + pathArray[4];
		}
	}
	else{
		url = "/application.php";
	}
	getResponse(url);
}
//-----------------------------
function replaceHtml(element, html) {
	if(element){
		var oldElement = (typeof element === "string" ? document.getElementById(element) : element);
		var newElement = document.createElement(oldElement.nodeName);
		// Preserve any properties we care about (id and class for example)
		newElement.id = oldElement.id;
		newElement.className = oldElement.className;
		//set the new HTML and insert back into the DOM
		newElement.innerHTML = html;
		if(oldElement.parentNode)
			oldElement.parentNode.replaceChild(newElement, oldElement);
		else
		oldElement.innerHTML = html;
		// check to run javascript:
		if(html.search("hideDiv:") > 0){  // <!-- hideDiv:buttonRegister,buttonLogIn -->
			var commandRegex = new RegExp("<!-- hideDiv:(.*?)-->", "im");
			var divIds = commandRegex.exec(html);
			var divIdArray = divIds[1].split(','); // divIds[1] is comma separated list of divs
			for(thisDivId in divIdArray){
				hideDiv(divIdArray[thisDivId]);
			}
		}
		if(html.search("showDiv:") > 0){  
			var commandRegex = new RegExp("<!-- showDiv:(.*?)-->", "im");
			var divIds = commandRegex.exec(html);
			var divIdArray = divIds[1].split(',');
			for(thisDivId in divIdArray){
				showDiv(divIdArray[thisDivId]);
			}
		}
		if(html.search("scrollTo:") > 0){
			var commandRegex = new RegExp("<!-- scrollTo:(.*?)-->", "im");
			var scrollTo = commandRegex.exec(html);
			window.scrollTo(scrollTo[1],0);
		}
		if(html.search("timerLocation:") > 0){
			var commandRegex = new RegExp("<!-- timerLocation:(.*?)-->", "im");
			var urlAndTimer = commandRegex.exec(html);
			var fields = urlAndTimer[1].split("|");
			var t=setTimeout("location.href=" + fields[1],fields[0]);
		}
		else if(html.search("location:") > 0){
			var commandRegex = new RegExp("<!-- location:(.*?)-->", "im");
			var url = commandRegex.exec(html);
			location.href=url[1];
		}
		if(html.search("zebraStripe:") > 0){
			var commandRegex = new RegExp("<!-- zebraStripe:(.*?) -->", "im");
			var tableId = commandRegex.exec(html);
			zebraStripe(tableId[1]);
		}
		if(html.search("status:") > 0){  
			var commandRegex = new RegExp("<!-- status:(.*?)-->", "im");
			var content = commandRegex.exec(html);
			status(content[1]);
		}

		// check for title to modify title tag in document:
		var title = "";
		var title2 = "";
		var myregexp = new RegExp("h2(.*?)<", "im");
		var myregexp2 = new RegExp("h3(.*?)<", "im");
		var titleMatchRegExp = myregexp.exec(html);
		var titleMatchRegExp2 = myregexp2.exec(html);
		if(titleMatchRegExp != null){
			title = titleMatchRegExp[0];
			title = title.replace("h2>","");
			title = title.replace("<","");
		}
		if(titleMatchRegExp2 != null){
			title2 = titleMatchRegExp2[0];
			title2 = title2.replace("h3>","");
			title2 = title2.replace("<","");
		}
		if(title.length){
			document.title = title;
			if(title2.length){
				document.title += " :: " + title2;
			}
		}
		//return a reference to the new element in case we need it
		return newElement;
	}
}
//-----------------------------
function stateChanged() {
	var idToChange = responseDivHolder; 
	if(!idToChange){ idToChange = "content"; }
	//	alert ("id to change: " + idToChange);
	if (xmlHttp.readyState==4) {
		if (!document.getElementById(responseDivHolder)) {
			responseTextHolder = xmlHttp.responseText;
			hideDiv('timer');
			document.body.style.cursor = 'default';
		}
		replaceHtml(idToChange,xmlHttp.responseText)
		hideDiv('timer');
		document.body.style.cursor = 'default';
		// analytics: 
		var pathArray = ("" + window.location).split('#!');
		//pageTracker._trackPageview("/ajax/" + pathArray[1]);
		//alert(pathArray[1]);
	}
	else {
		showDiv('timer');
		document.body.style.cursor = 'wait';
		//alert ("ready state:" + xmlHttp.readyState);
	}
}
//-----------------------------
function getResponse(url,divForResponse){
	responseDivHolder = divForResponse;
	xmlHttp = GetXMLHttpObject()
	if (xmlHttp == null) {
		alert ("Browser does not support HTTP request")
		return
	}
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url, true)
	xmlHttp.send(null)
	//turnOnButtonCurrentPage();
}
//-----------------------------
function GetXMLHttpObject(){
	var objXMLHttp = null
	if (window.XMLHttpRequest) {
		objXMLHttp = new XMLHttpRequest(); //Mozilla
	}
	else if  (window.ActiveXObject) {
		objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP"); //Explorer
	}
	return objXMLHttp
}
//-----------------------------
function submitQueryToServer(theFormId,theBaseURL,divForResponse,dontDisable) {
	// build query string
	var theForm = document.getElementById(theFormId);
	var queryString = '';
	// check to see if theBaseURL has any passed items:
	var passedQuery = theBaseURL.split("?");
	if(passedQuery.length > 0){
		queryString = queryString + passedQuery[1] + "&";
		theBaseURL = passedQuery[0];
	}
	for (i=0; i < theForm.elements.length; i++) {
		var element = theForm.elements[i];
		if (element.name){
			if(!dontDisable){
				element.disabled=true;
			}
			if (element.type == 'radio' || element.type == 'checkbox'){
				if (element.checked == true){
					queryString += element.name + '=' + element.value + "&";
				}
			}
			else{
				element.value = element.value.replace(/\n/g,'^^');
				element.value = element.value.replace(/#/g,'%23');
				queryString += element.name + '=' + element.value + "&";
			}
		}
	}
	//alert("Testing:" + theBaseURL + '?' + queryString);
	getResponse(theBaseURL + '?' + queryString,divForResponse);
}
//-----------------------------
function refreshAjax(){
	var pathArray = ("" + window.location).split("/");
	var url = "/application.php";
	if (pathArray[4]){
		url = url + "?doAction=" + pathArray[4];
	}
	if (pathArray[5]){
		url = url + "&command2=" + pathArray[5];
	}
	if (pathArray[6]){
		url = url + "&command3=" + pathArray[6];
	}
	if (pathArray[7]){
		url = url + "&command4=" + pathArray[7];
	}
	status ("Refreshing " + pathArray[4]);
	getResponse(url,"content");
}


