﻿
var account_data_checking = false;

function UserLogin(form_obj) { 
    if(account_data_checking) {
	    return;
	    }
	else {
	    account_data_checking = true;
	    }
	
	// Данные пользователя
    var u_login = trim(form_obj.u_login.value); form_obj.u_login.value = u_login;
	var u_password = trim(form_obj.u_password.value); form_obj.u_password.value = u_password;
	
	var u_remember_me = '';
	if(form_obj.u_remember_me.checked) {
	    u_remember_me = 'checked';
	    }
	
	if(u_login=='' || u_password=='') {
	    errorMessage('Заполните пожалуйста все информационные поля.');
	    account_data_checking = false;
	    }
	else {
	    checkUserData(u_login,u_password,u_remember_me);		
	    }
    }

function userReDirect() {
    window.location.href = '/profile';
}

function checkUserData(u_login,u_password,u_remember_me) {
    var b = new Date();
    $.ajax({    
    url: '/ajax/client/account/login.php',
	data: 'u_login='+u_login+'&u_password='+u_password+'&u_remember_me='+u_remember_me+'&date='+b.toLocaleString(),
    dataType: 'xml',
    type: 'POST',	
    success: function (xml) {
		
        $(xml).find('data').each(function(){ 
		    if($(this).find('errors_count').text()>0) {
			    errorMessage($(this).find('error_message').text());
			}
			else {
                successMessage('Успешная авторизация!');
				
				$('#create-account').fadeOut(200, function() {
					    var message = '<b>Добро пожаловать!</b><br><br>Вход на сайт успешно осуществлен. Теперь Вы можете оставлять комментарии в Блогах пользователей, а также публиковать собственные топики.<br><br>Через несколько секунд Вы будете перенаправлены на свою <b><a style="text-decoration:underline;" href="/profile">личную страничку</a></b>.';
					    $('#content').append('<div class="success-enter-holder" id="success-enter-holder">'+message+'</div>');
                        $('#success-enter-holder').fadeIn(700, function() {
						    setTimeout('userReDirect();', 20000);
						    });						
					    });	
			    }
            });		
        },
	error: function(){
        // Handle the error event
        },
    beforeSend: function(){
	    // Handle the beforeSend event
	    account_data_checking = true;
		document.getElementById('login-btn').className = 'с_small-btn-press';        
        },
    complete: function(){
	    // Handle the complete event
	    account_data_checking = false;
	    document.getElementById('login-btn').className = 'с_small-btn';        
        }	
    });    
}

function logout() {
    var b = new Date();
    $.ajax({    
        url: '/ajax/client/account/logout.php',
	    data: 'date='+b.toLocaleString(),
        dataType: 'xml',
        type: 'POST',	
        success: function (xml) {
	        // Handle the success event
            window.location.href = '/';		
            },
	    error: function(){
            // Handle the error event
            },
        beforeSend: function(){
            // Handle the beforeSend event
            },
        complete: function(){	    
            // Handle the complete event
            }	
        });    
    }

function ShowHidePassword(ctrl_flag) {
    if(ctrl_flag==0) {
	    window.document.getElementById('u_password').type = 'text';
		$('#password_ctrl').html('Скрыть пароль');
		document.getElementById('password_ctrl').onclick = function() { 
			ShowHidePassword(1);
			}
		$('#u_password').focus();  
	    }
	else {
	    document.getElementById('u_password').type = 'password';
		$('#password_ctrl').html('Показать пароль');
		document.getElementById('password_ctrl').onclick = function() { 
			ShowHidePassword(0);
			}
		$('#u_password').focus();
	    }
    }
