function getAjax() {
	var xmlHttp;
	
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				return false;
			};
		};
	};
	
	return xmlHttp;
};

function StringtoXML(text) {
	if(window.ActiveXObject) {
		var doc = new ActiveXObject("Microsoft.XMLDOM");
		doc.async = "false";
		doc.loadXML(text);
	} else {
		var parser = new DOMParser();
		var doc = parser.parseFromString(text,"text/xml");
	};
	return doc;
};

function getXml(url) {
	xmlHttp = getAjax();
	xmlHttp.open("GET",url,false);
	xmlHttp.send();
	return StringtoXML(xmlHttp.responseText);
};

// Login/Logout Form
var loginForm;
var loginBox;
var xmlHttp;
var params;

function login() {
	xmlHttp = getAjax();
	
	if(!xmlHttp) {
		alert("Your browser must have javascript enabled in order to use this feature!");
	} else {
		loginForm = document.getElementById("loginForm");
		loginBox = document.getElementById("loginBox");
		
		xmlHttp.onreadystatechange = function() {
			if(xmlHttp.readyState == 4) {
				loginBox.innerHTML = xmlHttp.responseText;
			};
		};
		
		params = "action=login&username="+loginForm.username.value+"&password="+loginForm.password.value;
		
		loginBox.innerHTML = "Logging in...";
		
		xmlHttp.open("POST","/include/login_box.php","true");
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		xmlHttp.send(params);
	};
	
	return false;
};

function logout() {
	xmlHttp = getAjax();
	
	if(!xmlHttp) {
		alert("Your browser must have javascript enabled in order to use this feature!");
	} else {
		loginBox = document.getElementById("loginBox");
		
		xmlHttp.onreadystatechange = function() {
			if(xmlHttp.readyState == 4) {
				if(xmlHttp.responseText != "false") {
					loginBox.innerHTML = xmlHttp.responseText;
				};
			};
		};
		
		params = "action=logout";
		
		loginBox.innerHTML = "Logging out...";
		
		xmlHttp.open("POST","/include/login_box.php","true");
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		xmlHttp.send(params);
	};
	
	return false;
};

function updateMember(id) {
	xmlHttp = getAjax();
	
	if(!xmlHttp) {
		alert("Your browser must have javascript enabled in order to use this feature!");
	} else {
		xmlHttp.onreadystatechange = function() {
			if(xmlHttp.readyState == 4) {
				if(xmlHttp.responseText == "true") {
					endEdit2();
				} else {
					document.getElementById("action_"+id).innerHTML = "Error: " + xmlHttp.responseText;
				};
			};
		};
		
		user = document.getElementById("user_"+id).innerHTML;
		fname = document.getElementById("fname_"+id).innerHTML;
		lname = document.getElementById("lname_"+id).innerHTML;
		email = document.getElementById("email_"+id).innerHTML;
		perms = document.getElementById("group_"+id).innerHTML == "Site Admin" ? 3 : (document.getElementById("group_"+id).innerHTML == "Worship Admin" ? 2 : 1);
		
		params = "id="+id+"&user="+user+"&fname="+fname+"&lname="+lname+"&email="+email+"&perms="+perms;
		
		xmlHttp.open("POST","/include/update_member.php","true");
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		xmlHttp.send(params);
	};
	
	return false;
};

function deleteMember(id) {
	xmlHttp = getAjax();
	
	if(!xmlHttp) {
		alert("Your browser must have javascript enabled in order to use this feature!");
	} else {
		xmlHttp.onreadystatechange = function() {
			if(xmlHttp.readyState == 4) {
				if(xmlHttp.responseText == "true") {
					deleteUser2();
				} else {
					document.getElementById("action_"+id).innerHTML = "Error: " + xmlHttp.responseText;
				};
			};
		};
		
		params = "id="+id+"&delete=true";
		
		xmlHttp.open("POST","/include/update_member.php","true");
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		xmlHttp.send(params);
	};
	
	return false;
};
