function submitenter(myfield,e) {
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;

	if (keycode == 13) {
   		myfield.form.submit();
   		return false;
   	} else
   		return true;
}

function loadLogin(message) {
	var myHttpObject = createXMLHttpRequest(); 
	
	var cb = (function() {
		var httpObject = myHttpObject;
		if (httpObject.readyState == 4) {
			document.getElementById("ibau_formular").innerHTML = httpObject.responseText;
			loadShoppinglist(true);
	  	}
	}).bind(this);

	myHttpObject.onreadystatechange = cb;
	myHttpObject.open("POST", "ajax/loadLogin.php");
	myHttpObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	myHttpObject.send("message=" + message);
}


function login() {
	var myHttpObject = createXMLHttpRequest(); 
	
	var password	= document.getElementsByName("password")[0].value;
	var username	= document.getElementsByName("username")[0].value;
	
	var cb = (function() {
		var httpObject = myHttpObject;
		if (httpObject.readyState == 4) {
			var aw = httpObject.responseText;

			if(aw == true) {
				loadFormular();
			} else {
				document.getElementById("loginMessage").innerHTML = "Login failure";
				document.getElementsByName("password")[0].value = "";
			}
	  	}
	}).bind(this);

	myHttpObject.onreadystatechange = cb;
	myHttpObject.open("POST", "ajax/login.php");
	myHttpObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	myHttpObject.send("username=" + username + "&password=" + password);
}

function loadRegistration(func) {
	var myHttpObject = createXMLHttpRequest(); 

	var cb = (function() {
		var httpObject = myHttpObject;
		if (httpObject.readyState == 4) {
			document.getElementById("ibau_formular").innerHTML = httpObject.responseText;
	  	}
	}).bind(this);

	myHttpObject.onreadystatechange = cb;
	myHttpObject.open("POST", "ajax/loadRegistration.php");
	myHttpObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	myHttpObject.send("func=" + func);
}


function loadPasswordForgotten() {
	var myHttpObject = createXMLHttpRequest(); 
	
	var cb = (function() {
		var httpObject = myHttpObject;
		if (httpObject.readyState == 4) {
			document.getElementById("ibau_formular").innerHTML = httpObject.responseText;
	  	}
	}).bind(this);

	myHttpObject.onreadystatechange = cb;
	myHttpObject.open("POST", "ajax/loadPasswordForgotten.php");
	myHttpObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	myHttpObject.send("");
}


function saveUser() {
	var myHttpObject = createXMLHttpRequest(); 

	var data = new Object();
	
	for(var i=0; i < document.getElementById("loginform").length; i++) with (document.getElementById("loginform").elements[i]) {
		if(name) {
			if(type == "checkbox") {
				data[name] = checked;
			} else {
				data[name] = value;
			}
		}
	}
	
	var cb = (function() {
		var httpObject = myHttpObject;
		if (httpObject.readyState == 4) {
			var aw = httpObject.responseText;

			if(aw == true) {
				document.getElementById("form").innerHTML = "You will have access to our website after you will have confirmed your registration.<br><br>Please follow the instructions given in the confirmation message in order to complete the registration procedure.<br><br>";
			} else {
				document.getElementById("error_message").innerHTML = aw;
			}
				
	  	}
	}).bind(this);

	myHttpObject.onreadystatechange = cb;
	myHttpObject.open("POST", "ajax/saveUser.php");
	myHttpObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	myHttpObject.send("data=" + encodeURIComponent(JSON.stringify(data)));
}

function saveForgotPassword() {
	var myHttpObject = createXMLHttpRequest(); 

	var Benutzername = document.getElementById("Benutzername").value
	var EMail = document.getElementById("EMail").value
	
	var cb = (function() {
		var httpObject = myHttpObject;
		if (httpObject.readyState == 4) {
			var aw = httpObject.responseText;

			if(aw == true) {
				document.getElementById("form").innerHTML = "We have generated a new password. Please check your Mail-Account.<br><br>";
			} else {
				document.getElementById("error_message").innerHTML = aw;
			}
	  	}
	}).bind(this);

	myHttpObject.onreadystatechange = cb;
	myHttpObject.open("POST", "ajax/saveForgotPassword.php");
	myHttpObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	myHttpObject.send("Benutzername=" + Benutzername + "&EMail=" + EMail);
}
