var al_status=0;
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";

}

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";

}


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";

}


function al_login() {
         
	if (0 != al_status) {
		return;
	}
	
	if (document.getElementById("logName").value == '') {
		alert("Моля въведете потребителско име.");
		document.getElementById("logName").focus();
		return;
	}

	if (document.getElementById("logPass").value == '') {
		alert("Моля въведете парола.");
		document.getElementById("logPass").focus();
		return;
	}

	al_sack.setVar("log", document.getElementById("logName").value);
	al_sack.setVar("pwd", document.getElementById("logPass").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) {
		
		if (al_redirectOnLogin == '')
			window.location.reload(true);
		else
			window.location.href = al_redirectOnLogin;
			
		return;
	}

	alert("Unknown login response.");

}

function al_register() {

	if (0 != al_status) {
		return;
	}

	if (document.getElementById("regUser").value == '') {
		alert("Моля въведете Потребителско име.");
		document.getElementById("regUser").focus();
		return;
	}

	if (document.getElementById("regMail").value == '') {
		alert("Моля въведете e-mail адрес.");
		document.getElementById("regMail").focus();
		return;
	}
	if (document.getElementById("regName").value == '') {
		alert("Моля въведете име.");
		document.getElementById("regName").focus();
		return;
	}
	if (document.getElementById("regFam").value == '') {
		alert("Моля въведете фамилия.");
		document.getElementById("regFam").focus();
		return;
	}
	if (document.getElementById("regPass").value == '') {
		alert("Моля въведете парола.");
		document.getElementById("regPass").focus();
		return;
	}
	if (document.getElementById("regPhone").value == '') {
		alert("Моля въведете телефонен номер.");
		document.getElementById("regPhone").focus();
		return;
	}          

	al_sack.setVar("user_login", document.getElementById("regUser").value);
	al_sack.setVar("user_email", document.getElementById("regMail").value);
	al_sack.setVar("user_name", document.getElementById("regName").value);
	al_sack.setVar("user_last", document.getElementById("regFam").value);
	al_sack.setVar("user_pass", document.getElementById("regPass").value); 
	al_sack.setVar("user_phone", document.getElementById("regPhone").value);     

	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) {
		alert("Регистрацията премина успешно. На посоченият и-мейл беше изпратен линк за активация.");
		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."+responselines[0]+responselines[1]+responselines[2]);

}

function al_retrievePassword() {
	if (0 != al_status) {
		return;
	}

	if (document.getElementById("lostName").value == '') {
		alert("Моля въведете потребителско име.");
		document.getElementById("lostName").focus();
		return;
	}

	if (document.getElementById("lostMail").value == '') {
		alert("Моля въведете и-мейл адрес.");
		document.getElementById("lostMail").focus();
		return;
	}

	al_sack.setVar("user_login", al_lostPasswordForm.user_login.value);
	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();

}

