var AJ_PHP = "/"+ABS_SITE_LIB +"/user_auth/user_auth.aj.php";

var auth = {
	loginMinLength : 5,
	preloaderImg : '<img src="/'+ABS_IMAGES +'/ajax-loader.gif" align=absmiddle>',
	/*
	 * Проверка поля ввода логина
	 */
	isUserAuth : function() {
    	$j.get(
    		AJ_PHP,
    		{
    			act: 'isUserAuth'
			},
			function(data){
				if(data){
					 $j('#loginreg').html(data);
					 $j("a.showLoginForm").click(function(){
					 	$j("#visForm").show();
					 	$j("#prevForm").hide();
					 	return false;
					 });
				}	
			}
		);
	},
	/*
	 * Проверка поля ввода логина
	 */
	checkLogin : function() {
    	var login = $j('#auth_login').val;
    	if(!login){
    		return false;
    	}
    	if(Validator.latinANDnum(login)){
    		return false;
    	}
    	return true;
  	},
  	/*
	 * Фильтр нажатия клавиши в поле Логин 
	 */
  	testLoginKey : function(event) {
  		if(!Validator.testKeyLoginType(event)){
  			return false;
  		}
  	},
  	/*
	 * Проверка поля ввода пароль
	 */
  	checkPassword : function() {
    	var password = $j('#auth_password');
    	if(!password){
    		return false;
    	}
    	if(Validator.latinANDnum(password)){
    		return false;
    	}
    	return true;
  	},
  	/*
	 * Фильтр нажатия клавиши в поле пароль 
	 */
  	testPasswordKey : function(event) {
  		if(!Validator.testKeylatinANDnum(event)){
  			return false;
  		}
  	},
    /*
  	* Check form before user auth 
  	*/ 
  	checkForm : function() {
  		console.info('checkForm');
  		if((this.checkLogin() + this.checkPassword()) != 2){	
  			return false; 
  		}	
  		return true; 
  	},
  	userAct : function(authCode,authLogin) {
  		this.loginForm = $j('#loginreg').html();
  		this.wait = 7;
  		switch(authCode) {
    		// авторизация
    		case 1:{ 
    			//window.location.reload();
    			window.location = "http://"+ABS_HOST+'/';
    			break;
    		}
			// пользователь не найден
			case 2:{ 
    			var cont = 'Ошибка авторизации.<br>Пользователь не найден.<br>Ждем:&nbsp;<span id="sec_remain"/>';
    			break;
    		}
    		// неправильный пароль
    		case 3:{ 
    			var cont = 'Ошибка авторизации.<br>Неверный пароль.<br>Ждем:&nbsp;<span id="sec_remain"/>';
    			break;
    		}
    		// пользователь не активен
    		case 4:{ 
    			var cont = 'Ошибка авторизации.<br>Пользователь заблокирован.<br>Ждем:&nbsp;<span id="sec_remain"/>';
    			break;
    		}
    		case 5:{ 
    			//alert('case 5');
    			window.location = "http://"+ABS_HOST+'/';
    			break;
    		}
		}
		$j('#loginreg').html(cont);
		this.switchVis();
  	},
   /*
  	* Отложенная смена видимости слоев
  	*/ 
  	switchVis : function() {
		//alert('switchVis - sec_field = '+sec_field+' to_hide = '+to_hide+' this.wait = '+this.wait);
		if (1 == this.wait) {
			$j('#loginreg').html(this.loginForm);
			//$j("#"+to_hide).hide();
			//$j('#non_authorized').show();
			//$j('#auth_password').val('');
		}
		else {
			this.wait -= 1;
			$j("#sec_remain").html(this.wait);
			setTimeout('auth.switchVis()', 1000);
		}
	},
   /*
  	* Отложенная смена видимости слоев
  	*/ 
  	switchVis666 : function(sec_field,to_hide) {
		//alert('switchVis - sec_field = '+sec_field+' to_hide = '+to_hide+' this.wait = '+this.wait);
		if (1 == this.wait) {
			$j("#"+to_hide).hide();
			$j('#non_authorized').show();
			$j('#auth_password').val('');
		}
		else {
			this.wait -= 1;
			$j("#"+sec_field).html(this.wait);
			setTimeout('auth.switchVis("'+sec_field+'","'+to_hide+'")', 1000);
		}
	},
	/*
	 * Logout
	 */
	logout : function() {
    	$j.get(
    		AJ_PHP,
    		{
    			act: 'user_logout'
			},
			function(data){
				window.location.reload();
			}
		);
	},
	/*
  	* Loading
  	*/ 
  	loading : function(div) {
  		$(div).update(this.preloaderImg);
  	},
  	unLoading : function(div) {
  		$(div).update('<img src="/'+SITE_LIB +'/user_auth/sp1.gif" align=absmiddle>');
  	}
};




var authKC = {
	capsLock : {
		on : false,
		_catch : function(id,act){
			if(!id) obj = document;
			else obj = $(id);
			this.act = act;
			if (obj.addEventListener){
				obj.addEventListener("keypress", this.detect, false);
				obj.addEventListener("keydown", this.detect, false);
			}
			else if (document.attachEvent){
				obj.attachEvent("onkeypress", this.detect);
				obj.attachEvent("onkeydown", this.detect);
			}
		},
		detect : function(e){
			var n = e.keyCode?e.keyCode:e.charCode;
			if (e.type=="keypress"){
				var c = String.fromCharCode(n);
				var cUC = c.toUpperCase();
				var cLC = c.toLowerCase();
				if (cUC!=cLC) 
					authKC.capsLock.action((e.shiftKey && cLC==c)||(!e.shiftKey && cUC==c));
 			}
 			else if (e.type=="keydown" && n==20) 
 				authKC.capsLock.action(false);
		},
		action : function(pressed){ 
			this.act(pressed);
		}
	}
} 

$j(document).ready(
	function(){
		 auth.isUserAuth();
	}
);


/*
Event.observe(window, 'load', function() {
	authKC.capsLock._catch('auth_password',function(on){
		if(on){$('auth_caps_lock').setStyle({color:'red',borderColor:'red'});}
    	else{$('auth_caps_lock').setStyle({color:'#fff', borderColor:'#fff'});}
    });
});
*/



