function get_ajaxobject()
{
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
    	return new XMLHttpRequest();
	}
	else if (window.ActiveXObject) { // IE
    	return new ActiveXObject("Microsoft.XMLHTTP");
	}
}

function adduser()
{
    window.location="/adduser.php";
}

function chklogin()
{
	var form = document.getElementById('loginform');

	httpobj = get_ajaxobject();
	httpobj.onreadystatechange = function() { notifylogin(httpobj); };
	httpobj.open('POST', '/login.inc.php' ,true);
	httpobj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	httpobj.send("email=" + escape(form.lemail.value)+"&pwd=" + escape(form.pwd.value));

	return false;
}

function parseXML(http_request, element)
{
	var message = http_request.responseXML.getElementsByTagName(element)[0];
	return message.childNodes[0].nodeValue;
}

function notifylogin(http_request) 
{
    if (http_request.readyState == 4) 
	{
		if (http_request.status == 200) 
		{
			var form = document.getElementById('loginform');
						
			if (parseXML(http_request, "message") == "activate")
				window.location="/adduser.php?logonactivate="+parseXML(http_request, "email");
			
			else if (parseXML(http_request, "message") == "valid")
			{
				document.getElementById('login-field').innerHTML = "<br/>Du er nu logget ind som<br/>"+parseXML(http_request, "user")+"<br/><br/>Tryk <a href='/logout.php'>her</a> for at logge af";
			}
			else
			{
				alert("Du har indtastet en forkert e-mail adresse eller kodeord");
				form.lemail.value="";
				form.pwd.value="";
				form.lemail.focus();
			}
		} 
		else 
		{
			alert('There was a problem with the request.');
		}
	}
}

