// this code is linked to the header links of the document, so is not a direct part of the community maps panel

var xhReq;
//var theURL ="http://"+window.location.host+"/version4/includes";
//var theURL = document.location;
//alert(theURL);
var minisitename;
function doMainLogin(theminisite) {

	// first make sure we have an e-mail and psasword
	
	// then submit the details to a login form, along with the minisite name
	
	// this will redirect the user to the correct minisite once logged in
	
	// if login fails ... redirect to this page

	//alert("do main login"+theminisite);
	
	// put the minisite name in a global variable so that the redirect can work
	minisitename = theminisite;
	var minisitegroup = getMinisiteGroup();
	var theUsername = document.getElementById("login_email_address").value;
	var thePassword = document.getElementById("login_password").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;
	}

	 xhReq = createXMLHttpRequest();
	 xhReq.open("POST", "mfcPhpRequests.php", true);
	 xhReq.onreadystatechange = isMainLoggedIn;

	 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.send("mfcAction=processHeaderUserLogin&username="+theUsername+"&password="+thePassword+"&minisitename="+minisitename+"&minisite_group"+minisitegroup);

}

function isMainLoggedIn() {

	if (xhReq.readyState != 4)  { 
		return;
	}

	var serverResponse = xhReq.responseText;
	// was the login succesful?

	var minisitename = getMinisiteName();
	var minisitegroup =getMinisiteGroup() 
	// if not, just pop up an error message to the user
	serverResponse = serverResponse.replace(/(\r\n|\n|\r)/gm,"");

	//alert(serverResponse);

	if (serverResponse == "failed") {
		alert("Your login failed.  Please retype your username and password and try again");
	}
	else {
		redirectPage();
	}

	
	

}

function doResetPassword(minisitename) {

	//alert("do reset password "+minisitename);
}

function doHeaderLogout() {
		 xhReq = createXMLHttpRequest();
		 xhReq.open("POST", "mfcPhpRequests.php", true);
		 xhReq.onreadystatechange = headerLoggedOut;
		//alert("logging out");
		 try {
		   xhReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		   //xhReq.setRequestHeader("Content-Type","text/html")
		 }
		 catch (e) {
			// this only works in internet explorer
		 }
		 minisitename = getMinisiteName();
		 minisite_group = getMinisiteGroup();
		 //alert("minisite is "+minisitename);
		 xhReq.send("mfcAction=processHeaderUserLogout&minisitename="+minisitename+"&minisite_group="+minisite_group);

}

function headerLoggedOut() {

	if (xhReq.readyState != 4)  { 
		return;
	}
	alert("You have succesfully logged out");
	minisitename = getMinisiteName();
	minisitegroup = getMinisiteGroup();
	// now need to change the header to remove the mydetails and logged in link, and add teh login link instead
	//alert(document.getElementById("loggedin").innerHTML);
	var loggedOut = "<ul id='login'><li><img src='../images/icon_login.gif' alt='Login' width='21' height='16'/>";
	loggedOut = loggedOut + "<a href='mfcPhpRequests.php?mfcAction=login&minisite_group="+minisitegroup+"&minisitename="+minisitename+"'>Login</a> / <a href='mfcPhpRequests.php?";
	loggedOut = loggedOut + "mfcAction=register&minisite_group="+minisitegroup+"&minisitename="+minisitename+"'>Register</a></li></ul>";
	document.getElementById("loggedin").innerHTML = loggedOut;
	var minisitegroup = getMinisiteGroup();
	if (window.location.toString().indexOf("myDetails.php") > 0) {
		// take the user back to the main minisite
		// as there is no longer a current user so should be no details available
		//window.location = theURL+"/MiniSite.php?minisitename="+minisitename;	
		//window.location = getURL()+"includes/MiniSite.php?minisitename="+minisitename+"&minisite_group="+minisitegroup;	
		redirectPage();
	}


	// if the user is on an edit or admin tab and has logged out, then need to take them back to the legend tab
	if (window.location.toString().indexOf("MiniSite.php") > 0) {
		showLegend();
	}

		
}
function showMyDetails() {
		var minisitename = getMinisiteName();
		var minisitegroup = getMinisiteGroup();
		
		//window.location = theURL+"/myDetails.php?minisitename="+minisitename;	
		window.location = getURL()+"includes/myDetails.php?minisitename="+minisitename+"&minisite_group="+minisitegroup;	

}

