var al_status;
var al_loginForm, al_registerForm, al_lostPasswordForm;
var al_loginMessage, al_registerMessage, al_lostPasswordMessage;
var al_sack = new sack();

var al_otheronload = window.onload;
window.onload = al_init;
function al_init() {

	if (al_otheronload) al_otheronload();

	al_status = 0;

	al_loginForm = document.getElementById("al_loginForm");
	al_registerForm = document.getElementById("al_registerForm");
	al_lostPasswordForm = document.getElementById("al_lostPasswordForm");

	al_loginMessage = document.getElementById("al_loginMessage");
	al_registerMessage = document.getElementById("al_registerMessage");
	al_lostPasswordMessage = document.getElementById("al_lostPasswordMessage");
}


function al_showLogin() {

	document.getElementById("al_login").style.display = "none";
	document.getElementById("al_register").style.display = "none";
	document.getElementById("al_lostPassword").style.display = "none";

	if (0 != al_timeout) {
		document.getElementById("al_loading").style.display = "block";
		setTimeout('al_showLogin2();', al_timeout);
	} else {
		al_showLogin2();
	}
}

function al_showLogin2() {

	document.getElementById("al_loading").style.display = "none";
	document.getElementById("al_login").style.display = "block";
	//document.getElementById("al_login").style.height = "120px";
	al_loginForm.log.focus();

}

function al_showRegister() {

	document.getElementById("al_login").style.display = "none";
	document.getElementById("al_register").style.display = "none";
	document.getElementById("al_lostPassword").style.display = "none";

	if (0 != al_timeout) {
		document.getElementById("al_loading").style.display = "block";
		setTimeout('al_showRegister2();', al_timeout);
	} else {
		al_showRegister2();
	}
}

function al_showRegister2() {

	document.getElementById("al_loading").style.display = "none";
	document.getElementById("al_register").style.display = "block";

	al_registerForm.user_login.focus();
}


function al_showLostPassword() {

	document.getElementById("al_login").style.display = "none";
	document.getElementById("al_register").style.display = "none";
	document.getElementById("al_lostPassword").style.display = "none";

	if (0 != al_timeout) {
		document.getElementById("al_loading").style.display = "block";
		setTimeout('al_showLostPassword2();', al_timeout);
	} else {
		al_showLostPassword2();
	}
}

function al_showLostPassword2() {

	document.getElementById("al_loading").style.display = "none";
	document.getElementById("al_lostPassword").style.display = "block";

	al_lostPasswordForm.user_login.focus();
}


function al_login() {

	if (0 != al_status) {
		return;
	}

	if (al_loginForm.log.value == '') {
		alert("Introdu userul.");
		al_loginForm.log.focus();
		return;
	}

	if (al_loginForm.pwd.value == '') {
		alert("Introdu parola.");
		al_loginForm.pwd.focus();
		return;
	}

	al_sack.setVar("log", al_loginForm.log.value);
	al_sack.setVar("pwd", al_loginForm.pwd.value);
	al_sack.setVar("rememberme", al_loginForm.rememberme.value);

	al_sack.requestFile = al_base_uri + "/wp-content/plugins/ajax-login/login.php";
	al_sack.method = "POST";
	al_sack.onError = al_ajaxError;
	al_sack.onCompletion = al_loginHandleResponse;
	al_sack.runAJAX();
	al_status = 1;

}

function al_loginHandleResponse() {

	al_status = 0;

	var responselines = al_sack.response.split("\n",2);
	if (responselines[0] == al_failure) {
		alert(responselines[1]);
		return;
	}
	if (responselines[0] == al_success) {
		
		window.location.href='/puzzle-start';
		
		/*
		if (al_redirectOnLogin == '')
			window.location.reload(true);
		else
			window.location.href = al_redirectOnLogin;
		*/	
		return;
		
	}

	alert("Unknown login response.");

}

function pera(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) 
		level_padding += "    ";
		
		if(typeof(arr) == 'object') { //Array/Hashes/Objects
			for(var item in arr) {
				var value = arr[item];
			
				if(typeof(value) == 'object') { //If it is an array,
					dumped_text += level_padding + "'" + item + "' ...\n";
					dumped_text += dump(value,level+1);
				} else {
					dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
				}
			}
		} else { //Stings/Chars/Numbers etc.
			dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
		}
	//return dumped_text;
	alert(dumped_text);
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function al_register() {

	if (0 != al_status) {
		return;
	}


	
	if (al_registerForm.recive_news.checked==true)
	{	
		al_sack.setVar("recive_news", '1');
	}else{
		al_sack.setVar("recive_news", '0');
	}
	
	
	if (al_registerForm.user_login.value == '') {
		alert("Nu ati introdus un username-ul.");
		al_registerForm.user_login.focus();
		return;
	}
	if (al_registerForm.user_pass.value == '') {
		alert("Nu ati introdus o parola.");
		al_registerForm.user_pass.focus();
		return;
	}
	if (al_registerForm.user_email.value == '') {
		alert("Nu ati introdus email-ul.");
		al_registerForm.user_email.focus();
		return;
	}
	
	if (al_registerForm.user_lastname.value == '') {
		alert("Nu ati introdus numele.");
		al_registerForm.user_lastname.focus();
		return;
	}
	if (al_registerForm.user_firstname.value == '') {
		alert("Nu ati introdus prenumele.");
		al_registerForm.user_firstname.focus();
		return;
	}

	if (al_registerForm.localitate.value == '') {
		alert("Nu ati introdus localitatea.");
		al_registerForm.localitate.focus();
		return;
	}

	if (al_registerForm.str.value == '') {
		alert("Nu ati introdus strada.");
		al_registerForm.str.focus();
		return;
	}
	if (al_registerForm.str_nr.value == '') {
		alert("Nu ati introdus numarul.");
		al_registerForm.str_nr.focus();
		return;
	}
	
		
	var phoneValue=al_registerForm.user_phone.value;
	var first2ch=phoneValue.substring(0,2);

	if (al_registerForm.user_phone.value == '') {
		alert("Nu ati introdus telefonul.");
		al_registerForm.user_phone.focus();
		return;
	}

	if( (phoneValue.length!=10) || (first2ch!="07" && first2ch!="02" && first2ch!="03") ){
		alert("Numarul de telefon introdus nu este corect.\nReintrodu numarul tau de telefon cu prefix si fara spatii sau alte caractere.");
		al_registerForm.user_phone.focus();
		return;	
	}

	/*
	var usersex = getCheckedValue(al_registerForm.user_sex);

	if (!usersex) {
		alert("Nu ati selectat sex-ul.");
		al_registerForm.user_sex.focus();
		return;
	}
	

	var userbirthday = al_registerForm.user_birthday_year.value + "-" + al_registerForm.user_birthday_month.value + "-" + al_registerForm.user_birthday_day.value;

	if (al_registerForm.user_birthday_day.value == '') {
		alert("Nu ati introdus ziua nasterii.");
		al_registerForm.user_birthday_day.focus();
		return;
	}
	if (al_registerForm.user_birthday_month.value == '') {
		alert("Nu ati introdus luna nasterii.");
		al_registerForm.user_birthday_month.focus();
		return;
	}
	if (al_registerForm.user_birthday_year.value == '') {
		alert("Nu ati introdus anul nasterii.");
		al_registerForm.user_birthday_year.focus();
		return;
	}

	*/


	al_sack.setVar("user_login", al_registerForm.user_login.value);
	al_sack.setVar("user_email", al_registerForm.user_email.value);

	al_sack.setVar("user_firstname", al_registerForm.user_firstname.value);
	al_sack.setVar("user_lastname", al_registerForm.user_lastname.value);
	al_sack.setVar("user_phone", al_registerForm.user_phone.value);
	
	al_sack.setVar("str", al_registerForm.str.value);
	al_sack.setVar("str_nr", al_registerForm.str_nr.value);
	al_sack.setVar("bl", al_registerForm.bl.value);
	al_sack.setVar("sc", al_registerForm.sc.value);
	al_sack.setVar("et", al_registerForm.et.value);
	al_sack.setVar("ap", al_registerForm.ap.value);
	al_sack.setVar("localitate", al_registerForm.localitate.value);
	al_sack.setVar("sector", al_registerForm.sector.value);
	//al_sack.setVar("judet", al_registerForm.judet.value);
	al_sack.setVar("user_pass", al_registerForm.user_pass.value);


	//al_sack.setVar("user_sex", usersex);
	//al_sack.setVar("user_birthday", userbirthday);

	
	
	

	al_sack.requestFile = al_base_uri + "/wp-content/plugins/ajax-login/register.php";
	al_sack.method = "POST";
	al_sack.onError = al_ajaxError;
	al_sack.onCompletion = al_registerHandleResponse;
	al_sack.runAJAX();
	al_status = 1;

}

function al_registerHandleResponse() {

	al_status = 0;

	var responselines = al_sack.response.split("\n",2);
	if (responselines[0] == al_failure) {
		alert(responselines[1]);
		return;
	}
	if (responselines[0] == al_success) {
		window.location.href = "/user-register/?login";
		
		alert("Multumim! Ai fost inregistrat cu succes.\nPentru activarea contului trebuie sa confirmi email-ul introdus!\n\nVei primi un email de confirmare.");
		al_loginMessage.innerHTML = "Parola ta a fost trimisa pe mail.<br/>Pentru activarea contului trebuie sa confirmi email-ul introdus<br/>";
		al_loginForm.log.value = al_registerForm.user_login.value;
		al_registerForm.user_login.value = "";
		al_registerForm.user_email.value = "";
		al_showLogin();
		al_loginForm.pwd.focus();
		return;
	}

	alert("Unknown registration response.");

}

function al_retrievePassword() {
	if (0 != al_status) {
		return;
	}

	if (al_lostPasswordForm.user_email.value == '') {
		alert("Nu ati introdus un adresa de e-mail.");
		al_lostPasswordForm.user_email.focus();
		return;
	}

	al_sack.setVar("user_email", al_lostPasswordForm.user_email.value);

	al_sack.requestFile = al_base_uri + "/wp-content/plugins/ajax-login/lostpassword.php";
	al_sack.method = "POST";
	al_sack.onError = al_ajaxError;
	al_sack.onCompletion = al_lostPasswordHandleResponse;
	al_sack.runAJAX();
	al_status = 1;
}

function al_lostPasswordHandleResponse() {

	al_status = 0;

	var responselines = al_sack.response.split("\n",2);
	if (responselines[0] == al_failure) {
		alert(responselines[1]);
		return;
	}
	if (responselines[0] == al_success) {
		alert("Check your e-mail for the confirmation link.");
		al_loginMessage.innerHTML = "Your confirmation link is in your mail.<br/>";
		al_loginForm.log.value = al_lostPasswordForm.user_login.value;
		al_lostPasswordForm.user_login.value = "";
		al_lostPasswordForm.user_email.value = "";
		al_showLogin();
		al_loginForm.pwd.focus();
		return;
	}

	alert("Unknown password retrieval response.");

}

function al_ajaxError() {
	alert("We are sorry, there was an error while sending the request.\nPlease try again!\nIf error persists, please contact the webmaster.");
	
	alert(al_sack.responseStatus[0] + ':\n' + al_sack.response);
	al_sack = new sack();
}

function al_loginOnEnter(e) {

	if(window.event) // IE
		keynum = e.keyCode;
	else if(e.which) // Netscape/Firefox/Opera
		keynum = e.which;
	else
		keynum = 0;

	if (keynum==13)
		al_login();

}
function al_registerOnEnter(e) {

	if(window.event) // IE
		keynum = e.keyCode;
	else if(e.which) // Netscape/Firefox/Opera
		keynum = e.which;
	else
		keynum = 0;

	if (keynum==13)
		al_register();

}
function al_retrievePasswordOnEnter(e) {

	if(window.event) // IE
		keynum = e.keyCode;
	else if(e.which) // Netscape/Firefox/Opera
		keynum = e.which;
	else
		keynum = 0;

	if (keynum==13)
		al_retrievePassword();

}
