/*  
THE FUNCTIONS TO CALL THE PHP FOR GENERATING GENERIC MAP FUNCTIONALITY AND POPULATING THE TABS
*/

/*
STARTUP SCRIPT - THIS LOADS THE JAVASCRIPT THAT WE NEED FOR THE SETUP PROCESS OF THE MAP ITSELF
*/
function getMapScripts(callbackfunction) {
	 // if no callback function is provided, then use the standard mfc callback function
	 if (typeof callbackfunction == "undefined") {
    		callbackfunction = "mapDataAvailable";
  	 }
	 minisitename = getMinisiteName();
	 xmlUpdateMap = createXMLHttpRequest();
	 var poststring = "mfcAction=mapDataAvailable&minisitename="+minisitename;
	 xmlUpdateMap.open("POST", "mfcPhpRequests.php");
	 xmlUpdateMap.onreadystatechange = window[callbackfunction];
	 
	 try {
	   xmlUpdateMap.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	 }
	 catch (e) {
	 	// this only works in internet explorer
	 }
	 xmlUpdateMap.send(poststring);
}


/*
GET THE HTML INFORMATION POP UP
*/
function getInfo(resultID, tablename,callbackfunction) {

	 // if no callback function is provided, then use the standard mfc callback function
	 if (typeof callbackfunction == "undefined") {
    		callbackfunction = "showInfoWindow";
  	 }
	 if (typeof tablename == "undefined") {
    		alert("no tablename call to getInfo");
  	 }

	 xmlGetInfo = createXMLHttpRequest();
	 var poststring = "mfcAction=infoHTML&resultID="+resultID+"&tablename="+tablename;
	 xmlGetInfo.open("POST", "mfcPhpRequests.php");
	 xmlGetInfo.onreadystatechange = window[callbackfunction];
	 
	 try {
	   xmlGetInfo.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	   //xmlGetInfo.setRequestHeader('Content-Type','text/html')
	 }
	 catch (e) {
	 	// this only works in internet explorer
	 }
	 xmlGetInfo.send(poststring);
}


/* 
GET THE javascript files loaded into the map - this loads the java for the editing functionality
*/
function setupJavaScript(callbackfunction) {

	 // if no callback function is provided, then use the standard mfc callback function
	 if (typeof callbackfunction == "undefined") {
    		callbackfunction = "loadJavaScript";
  	 }

	minisitename = getMinisiteName();
	 xhReqJava = createXMLHttpRequest();
	 xhReqJava.open("POST", "mfcPhpRequests.php");

	 xhReqJava.onreadystatechange = window[callbackfunction];
	try {
	   xhReqJava.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	 }
	 catch (e) {
		// this only works in internet explorer
	 }
	 xhReqJava.send("mfcAction=setupJavaScript&minisitename="+minisitename);

}

/* 
GET THE HTML FOR THE SEARCH TAB - INCLUDES PULL-DOWN WITH THE LIST OF LAYERS BUT ALSO THE GEOCODING SEARCH - BY ADDRESS, POSTCODE ETC
*/
function callSearchHTTP(callbackfunction) {
	 // if no callback function is provided, then use the standard mfc callback function
	if (typeof callbackfunction == "undefined") {
    		callbackfunction = "setupSearch";
  	 }
	
	minisitename = getMinisiteName();
	minisite_group = getMinisiteGroup();
	 xmlSearchMap = createXMLHttpRequest();
	 var poststring = "mfcAction=setupSearchDiv&minisitename="+minisitename+"&minisite_group="+minisite_group;
	 xmlSearchMap.open("POST", "mfcPhpRequests.php");
	 xmlSearchMap.onreadystatechange = window[callbackfunction];
	 try {
	    xmlSearchMap.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	 }
	 catch (e) {
		// this only works in internet explorer
	 }
	 xmlSearchMap.send(poststring);
}





/*
SHOW THE ASSOCIATED 'CHILD' READINGS FOR AN INFO BOX - THIS COULD BE PHOTOGRAPHS OR NOISE READINGS 
*/
function displayChildRecord(parenttablename,lookuptablename,parentrecordid,callbackfunction) {

	 // if no callback function is provided, then use the standard mfc callback function
	 if (typeof callbackfunction == "undefined") {
    		callbackfunction = "showReadOnlyChild";
  	 }


	// 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&mfcAction=processReadOnlyRequest";
	
	//var sPath = window.location;
	//var paths = String(sPath).split("=");
	//minisitename = paths[1];
	minisitename = getMinisiteName();
	poststring = poststring+ "&minisitename="+minisitename

	 xhReq.open("POST", "mfcPhpRequests.php", true);
	 xhReq.onreadystatechange = window[callbackfunction];  //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);

}



/*
SHOW THE NEXT CHILD RECORD FOR A READ-ONLY INFO BOX - E.G. THE NEXT PHOTOGRAPH
*/
function nextrecord(tablename,resultid,callbackfunction) {
	// this is the function that finds the next child record

	 // if no callback function is provided, then use the standard mfc callback function
	 if (typeof callbackfunction == "undefined") {
    		callbackfunction = "nextChild";
  	 }


	xhReq = createXMLHttpRequest();
	// remove any dodgy characters
	var poststring = "";

	poststring = poststring + "&lookuptablename="+tablename+"&lookupid="+resultid;
	poststring = poststring + "&action=getNextReadOnlyChild&mfcAction=processReadOnlyRequest";
	 xhReq.open("POST", "mfcPhpRequests.php", true);
	 xhReq.onreadystatechange = window[callbackfunction];

	 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);

}



/*
SHOW THE PREVIOUS RECORD - E.G. PREVIOUS PHOTO - FOR A POP-UP INFO BOX
*/
function prevrecord(tablename,resultid,callbackfunction) {

	 // if no callback function is provided, then use the standard mfc callback function
	 if (typeof callbackfunction == "undefined") {
    		callbackfunction = "prevChild";
  	 }


	// 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&mfcAction=processReadOnlyRequest";

	//var sPath = window.location;
	//var paths = String(sPath).split("=");
	//minisitename = paths[1];
	minisitename = getMinisiteName();
	poststring = poststring+ "&minisitename="+minisitename


	 xhReq.open("POST", "mfcPhpRequests.php", true);
	 xhReq.onreadystatechange = window[callbackfunction];

	 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);
	
}




// ZUPA APPROACH - CHANGE THE CONTENT OF THE controlcontent DIV depending on which bit we are looking at..
// has the problem that we also need to store the layers that we have unclicked/clicked ...


function showEdit(callbackfunction) {

	 // if no callback function is provided, then use the standard mfc callback function
	 if (typeof callbackfunction == "undefined") {
    		callbackfunction = "showEditTab";
  	 }
	doCloseInfoWindow();	
	loadHTMLIntoDIV('../html/editTab.html','maptabs');

	 xhReq = createXMLHttpRequest();
	 xhReq.open("POST", "mfcPhpRequests.php",true);
	var minisitename = getMinisiteName();
	var minisite_group = getMinisiteGroup();
	 xhReq.onreadystatechange = window[callbackfunction];
	try {
	   xhReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	 }
	 catch (e) {
		// this only works in internet explorer
	 }
	 xhReq.send("minisitename="+minisitename+"&minisite_group="+minisite_group+"&mfcAction=isAdminLoggedIn");
	return false;
}


function generateEditJavascript(callbackfunction) {
	 if (typeof callbackfunction == "undefined") {
    		callbackfunction = "loadEditJavascript";
  	 }
	 xhReq = createXMLHttpRequest();
	 xhReq.open("POST", "mfcPhpRequests.php",true);
	var minisitename = getMinisiteName();
	 xhReq.onreadystatechange = window[callbackfunction];
	try {
	   xhReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	 }
	 catch (e) {
		// this only works in internet explorer
	 }
	 xhReq.send("minisitename="+minisitename+"&mfcAction=editJavascript");
}

function checkAdminUserLoggedIn() {
	// xhReq = createXMLHttpRequest();
	// try {
	//   xhReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	//   //xhReq.setRequestHeader("Content-Type","text/html")
	// }
	// catch (e) {
	//	// this only works in internet explorer
	// }
	// xhReq.open("POST", "isAdminLoggedIn.php");
	// xhReq.onreadystatechange = isAdminUserLoggedIn;
	// xhReq.send(null);
	showEdit();
}


function showAddContent(callbackfunction) {
	if (typeof callbackfunction == "undefined") {
    		callbackfunction = "isUserLoggedIn";
  	}
	if (currentTab == "legend") {
		trackLayerStatus();
	}
	if (currentTab  == "edit") {
		switchAllLayersOn();
	}
 //Clear Unapproved Edit Point - Added by KC
      map.clearMarkers();
      updateMap(true);
      //Clear Unapproved Edit Point - END
	doCloseInfoWindow();	
	loadHTMLIntoDIV('../html/addContentTab.html','maptabs');
	storeCurrentDIVText();
	currentTab = "addContent";
	// check whether the user is logged in first ..
	minisitename = getMinisiteName();
	minisite_group = getMinisiteGroup();
	 xhReq = createXMLHttpRequest();
	 xhReq.open("POST", "mfcPhpRequests.php",true);
	 xhReq.onreadystatechange = window[callbackfunction];
	try {
	   xhReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	 }
	 catch (e) {
		// this only works in internet explorer
	 }
	 xhReq.send("minisitename="+minisitename+"&minisite_group="+minisite_group+"&mfcAction=isLoggedIn");
}

