﻿var al_status;
var al_loginForm, al_registerForm, al_lostPasswordForm;
var al_loginMessage, al_loginError, al_registerError, al_lostPasswordError, 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_loginError = document.getElementById("al_loginError");
	al_registerError = document.getElementById("al_registerError");
	al_lostPasswordError = document.getElementById("al_lostPasswordError");
	
	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";
	
	jQuery('#al_loading_buttom').hide();
	jQuery('#al_loginError').hide();
	
	jQuery('#al_loading_buttom2').hide();
	jQuery('#al_registerError').hide();
	
	jQuery('#al_loading_buttom3').hide();
	jQuery('#al_lostPasswordError').hide();

	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";
	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();
}


/* LOGIN -------------------------------------------------------- */

function al_login() {

	jQuery('#al_loading_buttom').show();	

	if (0 != al_status) {
		return;
	}

	/* Login */	
	if (al_loginForm.log.value == 'Login' | al_loginForm.log.value == '') {	
		if (0 != al_timeout) {
			jQuery('#al_loading_buttom').show();
			setTimeout('al_show_login_username();', al_timeout);
			} else {
				al_show_login_username();
			}			
		return;
	}				
	
	/* Password */
	if (al_loginForm.pwd.value == 'Password' | al_loginForm.pwd.value == '') {		
		if (0 != al_timeout) {
			jQuery('#al_loading_buttom').show();
			setTimeout('al_show_login_password();', al_timeout);
			} else {
				al_show_login_password();
			}			
		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;	
	
	//jQuery('#al_loading_buttom').hide();
}

		/*  Login Username */
		function al_show_login_username() {
			jQuery('#al_loading_buttom').hide();
			jQuery('#al_loginError').show();
			al_loginError.innerHTML = "Пожалуйста, введите логин";
			al_loginForm.log.focus();
		}
		
		/*  Login Password */
		function al_show_login_password() {
			jQuery('#al_loading_buttom').hide();
			jQuery('#al_loginError').show();
			al_loginError.innerHTML = "Пожалуйста, введите пароль";
			al_loginForm.pwd.focus();
		}	
		
		/*  Login Error */
		function al_show_login_error() {
			jQuery('#al_loading_buttom').hide();
			jQuery('#al_loginError').show();
			al_loginError.innerHTML = "Вы ввели неверный логин или пароль";
			al_loginForm.log.focus();
		}	
	
/* .LOGIN -------------------------------------------------------- */


function al_loginHandleResponse() {

	al_status = 0;

	var responselines = al_sack.response.split("\n",2);
	if (responselines[0] == al_failure) {
			jQuery('#al_loading_buttom').show();
			setTimeout('al_show_login_error();', al_timeout);
		return;
	}
	if (responselines[0] == al_success) {
		
		if (al_redirectOnLogin == '')
			window.location.reload(true);
		else
			window.location.href = al_redirectOnLogin;
			
		return;
	}

	alert("Вы ввели неверный логин или пароль");

	}	

	
/* REGISTER -------------------------------------------------------- */

function al_register() {

	jQuery('#al_loading_buttom2').show();	

	if (0 != al_status) {
		return;
	}

	/* Login */	
	if (al_registerForm.user_login.value == 'Login' | al_registerForm.user_login.value == '') {	
		if (0 != al_timeout) {
			jQuery('#al_loading_buttom2').show();
			setTimeout('al_show_register_username();', al_timeout);
			} else {
				al_show_register_username();
			}			
		return;
	}				
	
	/* E-mail */
	if (al_registerForm.user_email.value == 'E-mail' | al_registerForm.user_email.value == '') {		
		if (0 != al_timeout) {
			jQuery('#al_loading_buttom2').show();
			setTimeout('al_show_register_email();', al_timeout);
			} else {
				al_show_register_email();
			}			
		return;		
	}
	
	al_sack.setVar("user_login", al_registerForm.user_login.value);
	al_sack.setVar("user_email", al_registerForm.user_email.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;	
	
	jQuery('#al_loading_buttom2').hide();	
}

		/* Register Username */
		function al_show_register_username() {
			jQuery('#al_loading_buttom2').hide();
			jQuery('#al_registerError').show();
			al_registerError.innerHTML = "Пожалуйста, введите логин";
			al_registerForm.user_login.focus();
		}
		
		/* Register E-mail */
		function al_show_register_email() {
			jQuery('#al_loading_buttom2').hide();
			jQuery('#al_registerError').show();
			al_registerError.innerHTML = "Пожалуйста, введите e-mail";
			al_registerForm.user_email.focus();
		}	
		
		/*  Register Error */
		function al_show_register_error() {
			jQuery('#al_loading_buttom2').hide();
			jQuery('#al_registerError').show();
			al_registerError.innerHTML = "Вы ввели неверный логин или e-mail";
			al_registerForm.user_login.focus();
		}
	
/* .REGISTER -------------------------------------------------------- */


function al_registerHandleResponse() {

	al_status = 0;

	var responselines = al_sack.response.split("\n",2);
	if (responselines[0] == al_failure) {
			jQuery('#al_loading_buttom2').show();
			setTimeout('al_show_register_error();', al_timeout);
		return;
	}
	if (responselines[0] == al_success) {
		al_loginMessage.innerHTML = "Пароль отправлен на Ваш e-mail<br/>";
		al_loginForm.log.value = al_registerForm.user_login.value;
		al_registerForm.user_login.value = "Login";
		al_registerForm.user_email.value = "E-mail";
		al_showLogin();
		al_loginForm.pwd.focus();
		return;
	}

	alert("Вы ввели неверный логин или e-mail");

}


/* RetrievePassword -------------------------------------------------------- */

function al_retrievePassword() {

	jQuery('#al_loading_buttom3').show();	

	if (0 != al_status) {
		return;
	}

	/* Login */	
	if (al_lostPasswordForm.user_login.value == 'Login' | al_lostPasswordForm.user_login.value == '') {	
		if (0 != al_timeout) {
			jQuery('#al_loading_buttom3').show();
			setTimeout('al_show_retrievePassword_username();', al_timeout);
			} else {
				al_show_retrievePassword_username();
			}			
		return;
	}				
	
	/* E-mail */
	if (al_lostPasswordForm.user_email.value == 'E-mail' | al_lostPasswordForm.user_email.value == '') {		
		if (0 != al_timeout) {
			jQuery('#al_loading_buttom3').show();
			setTimeout('al_show_retrievePassword_email();', al_timeout);
			} else {
				al_show_retrievePassword_email();
			}			
		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;
	
	jQuery('#al_loading_buttom3').hide();
}

		/* RetrievePassword Username */
		function al_show_retrievePassword_username() {
			jQuery('#al_loading_buttom3').hide();
			jQuery('#al_lostPasswordError').show();
			al_lostPasswordError.innerHTML = "Пожалуйста, введите логин";
			al_lostPasswordForm.user_login.focus();
		}
		
		/* RetrievePassword E-mail */
		function al_show_retrievePassword_email() {
			jQuery('#al_loading_buttom3').hide();
			jQuery('#al_lostPasswordError').show();
			al_lostPasswordError.innerHTML = "Пожалуйста, введите e-mail";
			al_lostPasswordForm.user_email.focus();
		}	
		
		/*  RetrievePassword Error */
		function al_show_lostPassword_error() {
			jQuery('#al_loading_buttom3').hide();
			jQuery('#al_lostPasswordError').show();
			al_lostPasswordError.innerHTML = "Вы ввели неверный логин или e-mail";
			al_lostPasswordForm.user_login.focus();
		}
	
/* .RetrievePassword -------------------------------------------------------- */




function al_lostPasswordHandleResponse() {

	al_status = 0;

	var responselines = al_sack.response.split("\n",2);
	if (responselines[0] == al_failure) {
			jQuery('#al_loading_buttom3').show();
			setTimeout('al_show_lostPassword_error();', al_timeout);
		return;
	}
	if (responselines[0] == al_success) {
		al_loginMessage.innerHTML = "Пароль отправлен на Ваш e-mail<br/>";
		al_loginForm.log.value = al_lostPasswordForm.user_login.value;
		al_lostPasswordForm.user_login.value = "Login";
		al_lostPasswordForm.user_email.value = "E-mail";
		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();

}
