var mainInfoDivText = "";
var photoInfoDivText = "";
var noiseInfoDivText = "";
var currentInfoTab = "main";

function displayChildRecord(parenttablename,lookuptablename,parentrecordid) {



		// save the existing record
		if (currentInfoTab == "main") {
			mainInfoDivText = document.getElementById("infodescription").innerHTML;
		}
		if (currentInfoTab == "noisereadings") {
			noiseInfoDivText = document.getElementById('infodescription').innerHTML;
		}
		if (currentInfoTab == "photos") {
			photoInfoDivText = document.getElementById('infodescription').innerHTML;
		}


	currentInfoTab = lookuptablename;
	xhReq = createXMLHttpRequest();
	// remove any dodgy characters
	var poststring = "";

	poststring = poststring + "&parenttablename="+parenttablename+"&parentid="+parentrecordid;
	poststring = poststring + "&lookuptablename="+lookuptablename;
	poststring = poststring + "&action=showReadOnlyChild";
	
	//var sPath = window.location;
	//var paths = String(sPath).split("=");
	//minisitename = paths[1];
	minisitename = getMinisiteName();
	poststring = poststring+ "&minisitename="+minisitename

	 xhReq.open("POST", "processReadOnlyRequests.php", true);
	 xhReq.onreadystatechange = showReadOnlyChild;

	 //
	 try {
	   xhReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	   //xhReq.setRequestHeader('Content-Type','text/html')
	 }
	 catch (e) {
		// this only works in internet explorer
	 }
	//alert(poststring);
	xhReq.send(poststring);


}
function showReadOnlyChild() {

   if (xhReq.readyState != 4)  { 
   		return;
   	}
	
	var response = xhReq.responseText;
	processReadOnlyChild(response);
}
function processReadOnlyChild(response) {
	   var htmlscript = response.split("|XX|");
	   
	var headID = document.getElementsByTagName("head")[0];
	var newScript = document.createElement('script');
	newScript.type = 'text/javascript';

	newScript.text = htmlscript[1];
	headID.appendChild(newScript);

	  html = "";
	  //html += "<div style=\"position:relative;overflow-y:auto;overflow-x:auto;overflow: auto;margin-left:3px;height:400px;width:97%;\">";
	  //html += "<div style=\"position:relative;overflow-y:auto;overflow-x:auto;overflow: auto;margin-left:3px;height:100%;width:97%;\">";
	  
	  html += "<div style=\"position:relative;overflow-y:auto;overflow-x:auto;overflow: auto;margin-left:3px;height:80%;width:97%;\">";
	  //html += "<div style=\"margin-top:5px;border:2px solid green;border-style: double;\">";
	  html +=  htmlscript[0];
	  //alert(htmlscript[0]);
	  //html += "</div>"; // the actual data
	  html += "</div>"; // overflow div around the data

	// move the parent container to the left ..
	//if (parseInt(document.getElementById("container2").style.left) > 100) {
	//	document.getElementById('subTables').style.left = "-400px";
	//}
	// keep the content of the main info div
	
	//document.getElementById("topbuttonArea2").innerHTML = generateHeading();
	//document.getElementById('opaquetext2').innerHTML = html;
	//document.getElementById('buttonArea2').innerHTML = generatePrevNextButtons();
	//document.getElementById('subTables').style.display = "block";
	
	// the content of the info div
	//alert(generatePrevNextButtons());
	document.getElementById('infodescription').innerHTML = html + generatePrevNextButtons();
	
}
function doCloseChildInfoWindow() {
	//document.getElementById('subTables').style.display = "none";
	
	// make sure the checkbox is also off
	document.getElementById('showChildren').checked = false;
}


function nextrecord(tablename,resultid) {
	// this is the function that finds the next child record

	xhReq = createXMLHttpRequest();
	// remove any dodgy characters
	var poststring = "";

	poststring = poststring + "&lookuptablename="+tablename+"&lookupid="+resultid;
	poststring = poststring + "&action=getNextReadOnlyChild";
	 xhReq.open("POST", "processReadOnlyRequests.php", true);
	 xhReq.onreadystatechange = nextChild;

	 try {
	   xhReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	   //xhReq.setRequestHeader('Content-Type','text/html')
	 }
	 catch (e) {
		// this only works in internet explorer
	 }
	//alert(poststring);
	xhReq.send(poststring);

}
function nextChild() {
   if (xhReq.readyState != 4)  { 
   		return;
   	}
	   var response = xhReq.responseText;

	   if (response.indexOf("No further records") > -1) {
	   	alert("No further records");
	   }
	  else {
	  	processReadOnlyChild(response);
	  }
		
}
function prevChild() {
   if (xhReq.readyState != 4)  { 
   		return;
   	}
	   var response = xhReq.responseText;

	   if (response.indexOf("No previous records") > -1) {
	   	alert("No previous records");
	   }
	   else {
	   	processReadOnlyChild(response);
	   }

}
function prevrecord(tablename,resultid) {
	// this is the function that finds the previous child record
		xhReq = createXMLHttpRequest();
		// remove any dodgy characters
		var poststring = "";
	
		poststring = poststring + "&lookuptablename="+tablename+"&lookupid="+resultid;
		poststring = poststring + "&action=getPrevReadOnlyChild";
		
		//var sPath = window.location;
		//var paths = String(sPath).split("=");
		//minisitename = paths[1];
		minisitename = getMinisiteName();
		poststring = poststring+ "&minisitename="+minisitename

		
		 xhReq.open("POST", "processReadOnlyRequests.php", true);
		 xhReq.onreadystatechange = prevChild;
	
		 try {
		   xhReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		   //xhReq.setRequestHeader('Content-Type','text/html')
		 }
		 catch (e) {
			// this only works in internet explorer
		 }
		//alert(poststring);
		xhReq.send(poststring);
	
}


function showMainInfo() {
	//alert("main ifno2");
	if (currentInfoTab == "main") {
		mainInfoDivText = document.getElementById("infodescription").innerHTML;
	}
	if (currentInfoTab == "noisereadings") {
		noiseInfoDivText = document.getElementById('infodescription').innerHTML;
	}
	if (currentInfoTab == "photos") {
		photoInfoDivText = document.getElementById('infodescription').innerHTML;
	}
	currentInfoTab = "main";
	document.getElementById('infodescription').innerHTML = mainInfoDivText;

}

