
var appName = "Login";
var appType = "P";

/***** Cookie goodness *****/
function createCookie(name,value,days, webroot) {
  if (days){
	var date = new Date();
	date.setTime(date.getTime()+(days*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path="+webroot;
}

function readCookie(name){
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++){
	var c = ca[i];
	while (c.charAt(0)==' ') c = c.substring(1,c.length);
	if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function eraseCookie(name){
	createCookie(name,"",-1);
}
/***************************/

function login_onload() {
		
 	var email = readCookie('email');
 	if (email != null){
		txtEmailLogin.setValue(email);
		txtPassword1.focus();
		chkRemember.setChecked(true);
	 }else{
		 txtEmailLogin.focus();
	 }
}

function btnToRegistration_onclick() { window.location = webroot + "/registreren"; }

function btnLostPassword_onclick() {
	var emailAddress = txtEmailLogin.getValue();
	// Alvorens we een email kunnen sturen met het paswoord
	// moeten we natuurlijk het emailadres hebben
	if(!emailAddress) {
		dlgEmptyEmailLogin1 = new Dialog("dlgEmptyEmailLogin1", gls("PleaseEnterEmailAddress"), "ok");
		dlgEmptyEmailLogin1.render();
		dlgEmptyEmailLogin1.show(); 
		
		txtEmailLogin.setError("EmailLogin");
		txtEmailLogin.focus();
	}
	else {
		var params = new Array();
		params["Email"] = emailAddress;
		oXmlRequest = new XmlRequest();
		oXmlRequest.functionName = "retrievePassword"
		oXmlRequest.params 	= params;	
		oXmlRequest.onsuccess = retrievePassword_success;
		oXmlRequest.start();
	}
	
}

function retrievePassword_success (oXmlResponse) {
	if (oXmlResponse.statusCode == "2000") {
		dlgPasswordSent = new Dialog("dlgPasswordSent", gls("PasswordSent"), "ok");
		dlgPasswordSent.render();
		dlgPasswordSent.show();
	}
	else {
		dlgPasswordSentFailed = new Dialog("dlgPasswordSentFailed", gls("PasswordSentFailed"), "failed");
		dlgPasswordSentFailed.render();
		dlgPasswordSentFailed.show();
	}
}

function btnLogin_onclick() {
	// Is het emailadres ingevuld?
	if (txtEmailLogin.getValue() == "") {
		dlgEmptyEmailLogin2 = new Dialog("dlgEmptyEmailLogin2", gls("PleaseEnterEmailAddress"), "ok");
		dlgEmptyEmailLogin2.render();
		dlgEmptyEmailLogin2.show(); 
		
	}
	// Is het paswoord ingevuld?
	else if (txtPassword1.getValue() == "") {
		dlgEmptyPassword1 = new Dialog("dlgEmptyPassword1", gls("PleaseEnterPassword"), "ok");
		dlgEmptyPassword1.render();
		dlgEmptyPassword1.show(); 
	}
	else {
		
		var params = new Array();
		params["Email"] = txtEmailLogin.getValue();
		var email = params["Email"];
		params["Password"] = txtPassword1.getValue();
		
		oXmlRequest = new XmlRequest();
		oXmlRequest.functionName = "checkLogin";
		oXmlRequest.params = params;	
		oXmlRequest.onsuccess = checkLogin_success;
		oXmlRequest.start();
		
	}
	
	function checkLogin_success(oXmlResponse) {
		
		if (oXmlResponse.statusCode == 2000) {
			
			var row = oXmlResponse.data[0];
			user.id = row["UserID"];
			user.email = row["Email"];
			user.fullName = row["FirstName"] + " " + row["LastName"];
			user.token = row["Token"];
			user.webroot = webroot;
			user.writeToCookie();
			
			if (chkRemember.getChecked() == true){
				createCookie('email', user.email , 365, user.webroot)
			}
			
			if (nextURL == "inloggen") {
				window.location = webroot + "/bestellingen";
			}
			else {
				window.location = webroot + "/" + nextURL;
			}
				
		}
		else {
			dlgLoginError = new Dialog("dlgLoginError", gls("LoginFailed"), "failed");
			dlgLoginError.render();
			dlgLoginError.show(); 	
			
			txtPassword1.setValue("");
			txtEmailLogin.focus();
			
		}
		
	}

}

function txtEmailLogin_onkeyup(ev, el) {
	if (ev.keyCode == 13) {
		btnLogin_onclick();
	}
}

function txtPassword1_onkeyup(ev, el) {
	if (ev.keyCode == 13) {	  
		btnLogin_onclick();
	}
}