var checkTimer=0;
var useReadyState=true;
var minuntesR=5;
var secondsR=0;
var t;
var webSite=location.host + "/";
var registering=false;
var loadMovies=false;

function makePOSTRequest(url, parameters, updateAction, rscFunc) {
	var http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		 http_request = new XMLHttpRequest();
		 if (http_request.overrideMimeType) {
				http_request.overrideMimeType('text/html');
		 }
	} else if (window.ActiveXObject) { // IE
		 try {
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
		 } catch (e) {
				try {
					 http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
		 }
	}
	if (!http_request) {
		 alert('Cannot create XMLHTTP instance');
		 return false;
	}
	if(useReadyState){
		http_request.onreadystatechange = function() {
				rscFunc(updateAction, http_request);
		};
	}
	http_request.open('POST', url, useReadyState);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", parameters.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(parameters);
	if(!useReadyState)
		rscFunc(updateAction, http_request);
}

function alertContents(takeAction, http_request) {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			switch(takeAction) {
				//Purchase Actions
			case "getState":
				var retarr=http_request.responseText.split("|");
				var selarr=retarr[0].split("^~");
				var userarr=retarr[1].split("^~");
				if(userarr[0]==1 || registering){
					if( document.location.toString().indexOf( 'https://' ) != -1 ){
					startReloadUser();
					if(registering) slidedown('customer_body');
					} else {
						location.href="https://" + location.host + location.pathname + location.search;
						return;
					}
				}
				if(selarr[0]==3){
					startShowMovieDetails(selarr[1], true);
					if(!registering) slidedown('st_body');
				} else if(selarr[0]==2){
					startDisplaySection(selarr[1],selarr[2]);
				} else if(selarr[0]==1){
					loadMovies=true;
					startDisplayAuditorium(selarr[1]);
					
					if(!registering) slidedown('purchase_info');
				} else {
					startDisplaySchedule(document.getElementById('datelist').value);
					if(!registering) slidedown('st_body');
				}
		 }
		} else {
			alert('There was a problem with the request. '+http_request.status);
		}
	}
}

function sesCD(){
}

function startGetState(){
	var toDo="getState";
	posstr="toDo=" + toDo;
	makePOSTRequest("backend/general_be.php",posstr,toDo, alertContents);
}

function flashStatus(){
	document.getElementById("status_footer").style.background="#FFFF66";
	setTimeout("document.getElementById('status_footer').style.background=document.getElementById('info_display').style.background",500);
	setTimeout("document.getElementById('status_footer').style.background='#FFFF66'",1000);
	setTimeout("document.getElementById('status_footer').style.background=document.getElementById('info_display').style.background",1500);
}

function flashTicket(){
}

function clearPurchaseInfo(){
	document.getElementById('purchase_list').innerHTML="Ticket Purchase List Area";
	document.getElementById('surcharge_line').innerHTML="";
	document.getElementById('ticket_total_line').innerHTML="";
	document.getElementById('grand_total_line').innerHTML="";
}

function loginDisplay() {
	if( document.location.toString().indexOf( 'https://' ) != -1 ) {
		distx= "<table><tr><td>e-mail:<td><input type='text' id='username' size='40'>";
		distx+= "<tr><td>Password:<td><input type='password' id='password' size='40'>";
		distx+="<tr><td colspan='2'align='center'><input type='checkbox' id='remember'>Remember me on this machine</table>";
		distx+= "<input type='button' class='btn' style='width: 100px;' value='Log In' onclick=\"startLoginUser(document.getElementById('username').value, document.getElementById('password').value);\">";
		distx+= "<input type='button' class='btn' style='width: 100px;' value='New Account' onclick=\"startNewAccount();\"><br>";
		distx+= "<input type='button' class='btn' style='width: 200px;' value='Forgot Password' onclick=\"dispForgotPassword();\"><br>";
		distx+= "<a href='javascript: feedbackForm()'>Send Feedback</a> <a href='rtfaq.htm' target='_faq'>FAQ</a>";
		
	} else {
		distx= "<span style='color: red;'><a href='https://" + document.location.host +  document.location.pathname + document.location.search + "'>Click Here to switch to a secure connection to log in and purchase tickets!</a></span><br>";
		distx+= "<a href='javascript: startSendFeedback()'>Send Feedback</a> <a href='rtfaq.htm' target='_faq'>FAQ</a>";
	}
	document.getElementById("customer_info").innerHTML=distx;
}

function clearMovieHeader(){
	var sth="<div id='st_top' class='t sh_inc'></div><div id='mvcimg' class='cimg icimg'></div>";
	sth+=" <span class='headText'>Movie: <span id='mvn' class='bold'>Click a time to select tickets</span>";
	sth+=" Date: <span id='mvd' class='bold'></span>";
	sth+=" Time: <span id='mvt' class='bold'></span></span>";
	document.getElementById("st_header").innerHTML=sth;
	incHeader("st_", "mvcimg")
}

function clearPurchaseHeader(){
	document.getElementById("pu_header").innerHTML="<div id='pu_top' class='t sh_inc'></div><div id='tkcimg' class='cimg icimg'></div> <span class='headText' id='pu_text'>No Tickets Selected</span>";
	incHeader("pu_", "tkcimg");
}

function clearCustomerHeader(){
	document.getElementById("cu_header").innerHTML="<div id='cu_top' class='t sh_inc'></div><div id='cucimg' class='cimg icimg'></div> <span class='heatText' id='cu_text'>Not Logged In</span>";
	incHeader("cu_", "cucimg");
}

function cmpHeader(bgid, imgid){
	var hdMain=document.getElementById(bgid + "header");
	var hdTop=document.getElementById(bgid + "top");
	var hdBL=document.getElementById(bgid + "bottom_left");
	var hdBR=document.getElementById(bgid + "bottom_right");
	hdMain.className=hdMain.className.replace("sh_inc", "sh_cmp");
	hdTop.className=hdTop.className.replace("sh_inc","sh_cmp");
	hdBL.className=hdBL.className.replace("sh_inc","sh_cmp");
	hdBR.className=hdBR.className.replace("sh_inc","sh_cmp");
	var hdImg=document.getElementById(imgid);
	hdImg.className=hdImg.className.replace("icimg", "cpimg");
}

function incHeader(bgid, imgid) {
	var hdMain=document.getElementById(bgid + "header");
	var hdTop=document.getElementById(bgid + "top");
	var hdBL=document.getElementById(bgid + "bottom_left");
	var hdBR=document.getElementById(bgid + "bottom_right");
	hdMain.className=hdMain.className.replace("sh_cmp","sh_inc");
	hdTop.className=hdTop.className.replace("sh_cmp","sh_inc");
	hdBL.className=hdBL.className.replace("sh_cmp","sh_inc");
	hdBR.className=hdBR.className.replace("sh_cmp","sh_inc");
	var hdImg=document.getElementById(imgid);
	hdImg.className=hdImg.className.replace("cpimg", "icimg");
}

function show(id) {
var d = document.getElementById(id);
	document.getElementById('datelist').style.display='none';
	if(document.getElementById('movielist')) document.getElementById('movielist').style.display='none';
if (d) {d.style.display='block';}
}

function setBtnImg(bgid, oldcls, newcls){
	var hdMain=document.getElementById(bgid + "_header");
	var hdTop=document.getElementById(bgid + "_top");
	var hdBL=document.getElementById(bgid + "_bottom_left");
	var hdBR=document.getElementById(bgid + "_bottom_right");
	hdMain.className=hdMain.className.replace(oldcls, newcls);
	hdTop.className=hdTop.className.replace(oldcls, newcls);
	hdBL.className=hdBL.className.replace(oldcls, newcls);
	hdBR.className=hdBR.className.replace(oldcls, newcls);
}

function makeXML(txt) {
	try {//Internet Explorer
  	if(window.ActiveXObject) {
  		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async="false";
			xmlDoc.loadXML(txt);
			return xmlDoc;
		} else {
			parser=new DOMParser();
			xmlDoc=parser.parseFromString(txt,"text/xml");
			return xmlDoc;
		}
  }
catch(e) {
  return false;
  }
}

function retText(node){
	if(node===undefined){
		return "";
	}
	if(window.ActiveXObject){
		return node.text;
	} else {
		return node.textContent;
	}
}

function selectSingleNode(xmlDoc, elementPath) {
	if(window.ActiveXObject) {
		return xmlDoc.selectSingleNode(elementPath);
	} else {
		var xpe = new XPathEvaluator();
		var nsResolver = xpe.createNSResolver( xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement);
		var results = xpe.evaluate(elementPath,xmlDoc,nsResolver,XPathResult.FIRST_ORDERED_NODE_TYPE, null);
		return results.singleNodeValue; 
		/*var oEvaluator = new XPathEvaluator();
		// FIRST_ORDERED_NODE_TYPE returns the first match to the xpath.
		var oResult = oEvaluator.evaluate(elementPath, xmlDoc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
		if (oResult != null) {
			return oResult.singleNodeValue;
		} else {
			return null;
		} */
		
	}
}

function serializeXML(xmlDoc){
	if(window.ActiveXObject){
		return xmlDoc.xml;
	} else {
		var oSerializer=new XMLSerializer();
		return oSerializer.serializeToString(xmlDoc);
	}
}
