function resetPassword() {
	if (document.getElementById("password_reset_email").value == "") {
		alert("Please provide an e-mail address");
		return false;
	}
	xhReq = createXMLHttpRequest();

	// now add the tablename
	 poststring = "email="+document.getElementById("password_reset_email").value;
	 poststring = poststring +"&action=changePassword&mfcAction=userDetails";
	 xhReq.open("POST", "mfcPhpRequests.php", true);
	 xhReq.onreadystatechange = passwordChanged;
	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(poststring);
}

function changePassword() {
	if (document.getElementById("password_change_email").value == "") {
		alert("Please provide your e-mail address");
		return false;
	}
	if (document.getElementById("existing_password").value == "") {
		alert("Please provide your existing password");
		return false;
	}
	if (document.getElementById("new_password1").value == "") {
		alert("Please type in a new password");
		return false;
	}
	if (document.getElementById("new_password1").value != document.getElementById("new_password2").value) {
		alert("Please make sure you type in the same password twice");
		return false;
	}
	xhReq = createXMLHttpRequest();

	// now add the tablename
	poststring = "email="+document.getElementById("password_change_email").value;
	poststring = poststring + "&newpassword="+document.getElementById("new_password1").value;
	poststring = poststring + "&existingpassword="+document.getElementById("existing_password").value;
	 poststring = poststring +"&action=chosePassword&mfcAction=userDetails";
	 xhReq.open("POST", "mfcPhpRequests.php", true);
	 xhReq.onreadystatechange = passwordChosen;
	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(poststring);
}

function passwordChosen() {

   if (xhReq.readyState != 4)  {
   		return;
   	}
	var result = xhReq.responseText;
	alert(result);
		// now go back to the community map that the user came from ..
		minisitename = getMinisiteName();
		redirectPage();
		/*if (minisitename == ""|| minisitename=="communitymaps") {
			// this is the community maps page
			//window.location = theURL+"/CommunityMaps.php";
			window.location = getURL()+ "includes/CommunityMaps.php";
		}
		else {
			//window.location = theURL+"/MiniSite.php?minisitename="+minisitename;
			window.location = getURL()+"includes/MiniSite.php?minisitename="+minisitename;
		}*/


}



function passwordChanged() {

   if (xhReq.readyState != 4)  {
   		return;
   	}
	var result = xhReq.responseText;
	alert(result);
		// now go back to the community map that the user came from ..
		minisitename = getMinisiteName();
		redirectPage();
		/*if (minisitename == ""||minisitename=="communitymaps") {
			// this is the community maps page
			//window.location = theURL+"/CommunityMaps.php";
			window.location = getURL()+"includes/CommunityMaps.php";
		}
		else {
			//window.location = theURL+"/MiniSite.php?minisitename="+minisitename;
			window.location = getURL()+"includes/MiniSite.php?minisitename="+minisitename;
		}*/


}

function saveUserDetailsChanges() {

	if (checkData()) {
		xhReq = createXMLHttpRequest();
		poststring = formData("userDataCapture");

		// remove any dodgy characters

		//poststring = poststring.replace("\'"," ");
		poststring = stripNonValidXMLCharacters(poststring);
		//poststring = escape(poststring);
		// remove any single quotes ..

		poststring = poststring.substr(0,poststring.length - 1);

		// now add the tablename
		//poststring = poststring + "&tablename="+tablename;
		// now add the user id
		//poststring = poststring + "&userID="+myUsername;
		 poststring = poststring +"&action=saveUserDetailChanges&mfcAction=userDetails";
		 //alert(poststring);
		 xhReq.open("POST", "mfcPhpRequests.php", true);
		 xhReq.onreadystatechange = processed;
		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 processed() {
   if (xhReq.readyState != 4)  {
   		return;
   	}
	var result = xhReq.responseText;
	// now go back to the community map that the user came from ..
	//alert(minisitename);
	result = result.replace(/(\r\n|\n|\r)/gm,"");
	alert(result);
	if(result == "Your data has been saved.  Your password will be e-mailed to you shortly." ||
		result == "Your changes have been saved.")
	{
		redirectPage();
	}
}
function registerUser() {
	if (checkData()) {
		xhReq = createXMLHttpRequest();
		poststring = formData("userDataCapture");

		// remove any dodgy characters

		//poststring = poststring.replace("\'"," ");
		poststring = stripNonValidXMLCharacters(poststring);
		//poststring = escape(poststring);
		// remove any single quotes ..

		poststring = poststring.substr(0,poststring.length - 1);

		// now add the user id
		 poststring = poststring +"&action=createNewUser&mfcAction=userDetails";
		 //alert(poststring);
		 xhReq.open("POST", "mfcPhpRequests.php", true);
		 xhReq.onreadystatechange = processed;
		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(poststring);
	}	
}


function loadCheckData() {

		xhReq = createXMLHttpRequest();
		poststring = "tablename=userDetails";
		 poststring = poststring +"&action=loadNewUserForm&mfcAction=userDetails";
		 xhReq.open("POST", "mfcPhpRequests.php", true);
		 xhReq.onreadystatechange = newUserFormResponse;
		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(poststring);


}

function newUserFormResponse() {
   if (xhReq.readyState != 4)  {
   		return;
   	}
	var result = xhReq.responseText;
	var htmlscript = result.split("|XX|");
	var headID = document.getElementsByTagName("head")[0];
	var newScript = document.createElement('script');
	newScript.type = 'text/javascript';

	newScript.text = htmlscript[1];
	headID.appendChild(newScript);


	document.getElementById('registerdiv').innerHTML = htmlscript[0];
		
}


function loadMyDetails() {

		xhReq = createXMLHttpRequest();
		poststring = "tablename=userDetails";
		 poststring = poststring +"&action=loadExistingUserForm&mfcAction=userDetails";
		 xhReq.open("POST", "mfcPhpRequests.php", true);
		 xhReq.onreadystatechange = existingUserForm;
		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(poststring);


}

function existingUserForm() {
   if (xhReq.readyState != 4)  {
   		return;
   	}
	var result = xhReq.responseText;
	var htmlscript = result.split("|XX|");
	var headID = document.getElementsByTagName("head")[0];
	var newScript = document.createElement('script');
	newScript.type = 'text/javascript';

	newScript.text = htmlscript[1];
	headID.appendChild(newScript);

	
	document.getElementById('myDetailsDiv').innerHTML = htmlscript[0];
		
}

function checkUserDetails() {
	var theUsername = document.getElementById("username").value;
	var thePassword = document.getElementById("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;
	}
	return true;
}



function isLoggedIn() {

   if (xhReq.readyState != 4)  { 
   		return;
   	}
   	
   	var serverResponse = xhReq.responseText;

	//storeCurrentDIVText();
	currentTab = "addContent";
	if (serverResponse.indexOf("failed") > 0) {
	//	// the user is not logged in so don't have a user id to generate the appropriate
	//	// list of items to edit
		alert("Login failed.  Please try again");
	}
	else {
		document.getElementById('controlcontent').innerHTML = serverResponse;
		
		// must also update the header
		updateHeader();
	}

}



function checkAdminUserDetails() {
	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 isAdminUserLoggedIn() {
   if (xhReq.readyState != 4)  { 
   		return;
   	}
   	
   	var serverResponse = xhReq.responseText;
	// if the user is logged in then actually switch to the tab!
	
	if (serverResponse.indexOf("failed") > 0) {
	//	// the user is not logged in so don't have a user id to generate the appropriate
		alert("Login failed.  Please try again.");
	}
	else {
		storeCurrentDIVText();
		currentTab = "edit";
		document.getElementById('controlcontent').innerHTML = serverResponse;
		trackLayerStatus();
		switchAllLayersOff();
		//alert("switch all layers off");
		generateEditJavascript();
		updateHeader();
	
	}	
 }


// ========================================


//=====================================
function showLoginOnHeader() {


   if (xhReqJava.readyState != 4)  { 
   		return;
   	}
	var serverResponse = xhReqJava.responseText;
	//alert(" in showLoginOnHeader: " + serverResponse );
	document.getElementById("logindiv").innerHTML = serverResponse;
}





function doLogin() {
	if (checkUserDetails()){
		var theUsername = document.getElementById("username").value;
		var thePassword = document.getElementById("password").value;
		var minisitename = getMinisiteName();
		var minisite_group = getMinisiteGroup();
		 xhReq = createXMLHttpRequest();
		 xhReq.open("POST", "mfcPhpRequests.php", true);
		 xhReq.onreadystatechange = isLoggedIn;

		 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=processUserLogin&username="+theUsername+"&password="+thePassword+"&minisitename="+minisitename+"&minisite_group="+minisite_group);
	}
}


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", "mfcPhpRequests.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("mfcAction=processAdminLogin&username="+theUsername+"&password="+thePassword);
	}
}





function updateHeader() {

	 xhReqJava = createXMLHttpRequest();
	 xhReqJava.open("POST", "mfcPhpRequests.php",true);

	 xhReqJava.onreadystatechange = showLoginOnHeader;
	try {
	   xhReqJava.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	 }
	 catch (e) {
		// this only works in internet explorer
	 }
	 minisitename = getMinisiteName();
	 minisite_group = getMinisiteGroup();
	 //alert("minisite is "+minisitename);
	 xhReqJava.send("mfcAction=isUserLoggedInHeader&minisitename="+minisitename+"&minisite_group="+minisite_group);
}


function logout() {
		 xhReq = createXMLHttpRequest();
		 xhReq.open("POST", "mfcPhpRequests.php", true);
		 xhReq.onreadystatechange = loggedOut;
		//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
		 }
		minisite_group = getMinisiteGroup();
		
		 xhReq.send("mfcAction=processUserLogout&minisite_group="+minisite_group);

}




/*=========================================
NEWS AND OTHER FUNCTIONS NOT FOR MAIN PAGE 
===========================================*/

/*function saveNews() {

	if (checkData()) {
		xhReq = createXMLHttpRequest();
		poststring = formData("userDataCapture");

		// remove any dodgy characters

		//poststring = poststring.replace("\'"," ");
		poststring = stripNonValidXMLCharacters(poststring);
		//poststring = escape(poststring);
		// remove any single quotes ..

		poststring = poststring.substr(0,poststring.length - 1);

		// now add the tablename
		//poststring = poststring + "&tablename="+tablename;
		// now add the user id
		//poststring = poststring + "&userID="+myUsername;
		poststring = poststring + "&minisitename="+getMinisiteName();
		 poststring = poststring +"&action=saveNews";
		 //alert(poststring);
		 xhReq.open("POST", "processAddCommunityNews.php", true);
		 xhReq.onreadystatechange = processedNews;
		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 loadAddNewsCheckData() {
		xhReq = createXMLHttpRequest();
		poststring = "tablename=minisitenews";
		 poststring = poststring +"&action=loadNewsForm";
		 xhReq.open("POST", "processAddCommunityNews.php", true);
		 xhReq.onreadystatechange = newsCheckData;
		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(poststring);
}


*/

