// COMMON FUNCTIONS SHARED ACROSS THE GENERAL AND ADMIN PAGES ...
var xhAdminReq; // the http xml request to see if the user is logged in as an administrator or not!
var xhAddMinisiteReq;

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", "isLoggedIn.php");
	 xhReq.onreadystatechange = isAdminUserLoggedIn;
	 xhReq.send(null);
}

function checkAdminUserDetails() {
	//alert("in check");
	var theUsername = document.getElementById("adminusername").value;
	var thePassword = document.getElementById("adminpassword").value;
	if (theUsername.length == 0) {
		alert("Please type in your username");
		return false;
	}
	if (thePassword.length == 0) {
		alert("Please type in your password");
		return false;
	}
	return true;
}

function doAdminLogin() {
	//alert("check pre check");
	if (checkAdminUserDetails()){
		//alert("details");
		var theUsername = document.getElementById("adminusername").value;
		var thePassword = document.getElementById("adminpassword").value;
		 xhReq = createXMLHttpRequest();
		 xhReq.open("POST", "processAdminLogin.php", true);
		 xhReq.onreadystatechange = isAdminUserLoggedIn;

		 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("pre send");
		 xhReq.send("username="+theUsername+"&password="+thePassword);
	}
}



function isAdminUserLoggedIn() {
   if (xhReq.readyState != 4)  { 
   		return;
   	}
   	
   	var serverResponse = xhReq.responseText;
   	//alert(serverResponse);
	// if the user is logged in then actually switch to the tab!
	
	// split out the response ... some of this could be the java code that 
	// calls the actual generation of the lists of data to be moderated/edited
	
	
	serverArray = serverResponse.split("||");
	serverResponse = serverArray[0]; // admin, user, login etc ...
	
	var headID = document.getElementsByTagName("head")[0];
	var newScript = document.createElement('script');
	newScript.type = 'text/javascript';
	//newScript.src = splitresult[1]; //'http://www.somedomain.com/somescript.js';
	//alert(serverArray[1]);
	newScript.text = serverArray[1];
	headID.appendChild(newScript);
	if (serverResponse.indexOf("User") > -1) { 
		ManageTabPanelDisplay('tab1ready','tab2ready','tab4ready','tab3focus','content3');
		
		// switch off all the map layers
		switchAllLayersOff();
		setupAdminInstructions();
		generateListsForModeration(0);
	}
	if (serverResponse == "Admin") {
		ManageTabPanelDisplay('tab1ready','tab2ready','tab4ready','tab3focus','content3');
		switchAllLayersOff();
		setupAdminInstructions();
		generateListsForModeration(0); // this is created dynamically by the php code
	}
	if (serverResponse == "Login" || serverResponse == "Not Logged In") { 
		joinHTML = "You must log in to Community Maps to edit or moderate data";
		joinHTML = joinHTML + "<br><br>";
		joinHTML = joinHTML + "If you are not registered as a London21 user, click <br><a href=\"#\" onclick=\"javascript:gotoLoginPage()\">here</a>";
		joinHTML = joinHTML + " to register.  You can add data once your registration is activated.";
		joinHTML = joinHTML + "<br><br>";
		joinHTML = joinHTML + "<table>";
		joinHTML = joinHTML + "<tr><td>User e-mail:" + "</td><td><input id=\"adminusername\" type=\"text\"></td></tr>";
		joinHTML = joinHTML + "<tr><td>Password: "+ "</td><td><input id=\"adminpassword\" onkeypress=\"checkEnterUserLogin(event)\" type=\"password\"></td></tr>";
		joinHTML = joinHTML + "<tr><td colspan = 2><button id=\"doAdminlogin\" type=\"button\" name=\"doAdminlogin\"  onclick=\"doAdminLogin()\">Login</button></td></tr>";
		joinHTML = joinHTML + "</table>";
		document.getElementById('adminInstructions').innerHTML = joinHTML;
		ManageTabPanelDisplay('tab1ready','tab2ready','tab4ready','tab3focus','content3');
	}
	
 }




