// init values
currentPage = ""; //current page id
doHistory=""; //do history
ajaxbackEnable=""; //disable history totally?
loadFront=true; //should we load a video on the front page

//initiates a new xho for multiple browsers
function getXmlHttpObject() {
	var xmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

//function called when there is a state change
function stateChanged() { 
	if (xhp.readyState==4) { //aka complete
		//MAIN RESPONSE HANDLER
		var rt = xhp.responseText;
		//if we are just loading the body without a new left nav, or if this is library search results page
		if ((rt.indexOf('leftnav') == -1) && (rt.indexOf('library_search_results') == -1)) {
			document.getElementById("rightbody").innerHTML=rt;
			document.getElementById("rightbody").innerHTML.evalScripts();  //run the eval on the completed script
		} else { //otherwise load the left nav and body
			document.getElementById("bottom").innerHTML=rt;
			document.getElementById("bottom").innerHTML.evalScripts(); //run the evail on the completed script
		}

		//POST EVAL HANDLERS.  Because IE 6 & 7 evalScripts is bad for complex functions, this section handles some javascript that i would normally do within the pages themselves....
		currentPage = id; //sets the new current page
		makeTopNavActive(currentTopNav); //make the top nav red		
		
		//FORUM DRILLDOWN - an id of 16 is the fourm page.  load forum in iframe if a forum id (fid) is selected.
		if (id==16) {
			if (fid == "" || fid==null) fid = "forum";
			objiframeforum = document.getElementById("iframeforum");
			if (objiframeforum.contentDocument){ //non ie/safari
			   rv = objiframeforum.contentDocument; 
			} else {   // IE & SAFARI
				if (BrowserDetect.browser=="Safari") {
					rv = self.frames[0].document; //safari
				} else {
					rv = document.frames['iframeforum'].document; //IE
				}
			}
			rv.location = fid;
		}
		fid="forum"; //reset when done
		
		//BACK BUTTON - enable the back button via the # technique.  ajaxbackEnable occurs for all browsers except those disabled--check the functions.js script to see who is disabled (basically ie6)
		//alert("id=" + id + " currentPage=" + currentPage  + "doHistory=" + doHistory + "op1=" + op1 + "op2=" + op2 + "currentTopNav=" + currentTopNav);
		if (doHistory && ajaxbackEnable) { //if doing the back button.  the optional parameters are used for the search results pages
			historydata = id + "*-*" + op1 + "*-*" + op2;
			dhtmlHistory.add(id, historydata);
			document.title = "Lakeshore Entertainment";
		}
		
		//SCROLLBARS
		/*
		window.focus();
		if (typeof(s0) != "undefined") {
			loadSlider(s1,s2,s3,s4,s5,s6,s7,s8,s9);
		}
		s0=undefined;
		*/
		
	}
}

//loads a page ala ajax
function loadPage(id, doHistory, optionalParam1, optionalParam2) {
	if (id != "" && id !="undefined") {
    //self.status = 'loading page: ' + id;
		if (id.toString().indexOf('forum') != -1) {
			window.open(id,"_self");
		} else if (id.toString().indexOf('http') != -1) { //launch as external url if the id is an http address
			window.open(id,"_blank");
		} else { //otherwise do ajax load
			if (doHistory != false) doHistory=true; //ensure do history is set
			hideLink(document.getElementById("getlink")); //hide link box at the top of the page
			
			var url="loaders/loader.php?id=" + id + "&cp=" + currentPage + "&dh=" + doHistory  + "&op1=" + optionalParam1 + "&op2=" + optionalParam2 + "&loadFront=" + loadFront;
			xhp=getXmlHttpObject();
			if (xhp==null) {
				document.getElementById("rightbody").innerHTML="This site requires a modern browser and JavaScript enabled.";
				return;
			}
			xhp.onreadystatechange=stateChanged;
			xhp.open("GET",url,true);
			xhp.send(null);
		}
	}
}


//ajax loader used to submit a form for the registration page
function registerPage() {
	var first_name = document.register.first_name.value;
	var last_name = document.register.last_name.value;
	var email = document.register.email.value;
	var phone = document.register.phone.value;
	var country = document.register.country.value;
	var zip = document.register.zip.value;
	var receive_updates = document.register.receive_updates.value;
	
	var cont = validateMessage(email, phone);
	if (cont) {
		var url="loaders/loaderRegister.php";
		var params = "first_name=" + first_name + "&last_name=" + last_name + "&email=" + email + "&phone=" + phone + "&country=" + country + "&zip=" + zip + "&receive_updates=" + receive_updates;
		
		xhp=getXmlHttpObject();
		if (xhp==null) {
			document.getElementById("rightbody").innerHTML="This site requires a modern browser and JavaScript enabled.";
			return;
		}
		
		xhp.onreadystatechange=registerStateChanged;
		xhp.open("POST",url,true);
		
		//Send the proper header information along with the request
		xhp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xhp.setRequestHeader("Content-length", params.length);
		xhp.setRequestHeader("Connection", "close");
		
		xhp.send(params);
	}
}

//handles state change for register submitPage
function registerStateChanged() { 
	if (xhp.readyState==4) { //aka complete
		var rt = xhp.responseText;
		document.getElementById("content_simple_overflow").innerHTML=rt;
	}
}

function ClearMovie(pageid)
{
  var styleId = document.getElementById('style_id_name').value;
  document.getElementById(styleId).innerHTML = '<h3 style="margin-top:3em; margin-left:4em; color:white;">Loading video...</h3>';
  loadPage(pageid);
}
