var userLoginCallback = null;

function UserLogin()
{
    this.request = 'ajaxindex.php?';
}

UserLogin.prototype.show = function(callback)
{
    tb_show(null, this.request + 'start=showPanel&width=465&height=290&modal=true', null);
       
    if(callback != null) {
        userLoginCallback = callback;
    } else {
        userLoginCallback = null;
    }
}

UserLogin.prototype.check = function() 
{
    var ajaxResponseResult;
    var ajaxResponseErrors;
    var ajaxResponseMessage;
    
    $.ajax({
        'type'     : 'get',
        'url'      : this.request + '&start=checkLogged',
        'dataType' : 'text xml',
        'async'    : false,
        'success'  : function(xml) {
            $("response > result", xml).each (
                function()
                {
                    ajaxResponseResult = ($(this).text());
                }
                );
        
            $("response > error", xml).each (
                function()
                {
                    ajaxResponseErrors = ($(this).text());
                }
                );
        
            $("response > message", xml).each (
                function()
                {
                    ajaxResponseMessage = ($(this).text());
                }
                );
        }
    });
    
    if(ajaxResponseResult && ajaxResponseResult == 1) {
        return true;
    }
    return false;
}


UserLogin.prototype.logout = function()
{
    $.ajax({
        'type'     : 'post',
        'url'      : this.request + '&start=logout',
        'dataType' : 'xml',
        'async'    : false,
        'success'  : function(xml) 
        {
            $("response > result", xml).each (
                function()
                {
                    return ($(this).text());
                }
                );
        }
    });
}
UserLogin.prototype.reloadPanel = function()
{
    $.ajax({
        type: "post",
        url:  BASE_URL + 'ajaxindex.php?action=UserLogin&start=showLogged',
        dataType: "xml",
        global: false,
        async: false,
        success: function(xml)
        {
            $("response > html", xml).each (
                function()
                {
                    $('#log-in').html($(this).text());
                    ajaxResponseMessage = ($(this).text());
                }
            );
        }
    });
}

UserLogin.prototype.login = function(login, passwd, remember_me)
{
    var ajaxResponseResult;
    var ajaxResponseErrors;
    var ajaxResponseMessage;
       
    $.ajax({
        'type'     : 'post',
        'url'      : this.request + '&start=submit',
        'dataType' : 'text xml',
        'data'     : {
            'login'  : login,
            'passwd' : passwd,
            'remember_me' : remember_me
        },
        'async'    : false,
        'success'  : function(xml) {
            $("response > result", xml).each (
                function()
                {
                    ajaxResponseResult = ($(this).text());
                }
                );
        
            $("response > error", xml).each (
                function()
                {
                    ajaxResponseErrors = ($(this).text());
                }
                );
        
            $("response > message", xml).each (
                function()
                {
                    ajaxResponseMessage = ($(this).text());
                }
                );
        }
    });
    
    if(ajaxResponseResult && ajaxResponseResult == 1) {
       
        this.reloadPanel();
        if(typeof reloadComment == 'function') {
            reloadComment();
        }
        return true;
    }
    else if(ajaxResponseResult && ajaxResponseResult == 2) {
        //Konto zablokowane
        return 2;
    }
    	
    return false;
}

//Metoda aktywuje numer telefonu klubowicza
UserLogin.prototype.phoneActivate = function(usrzu_default_phone,usrzu_activation_code) 
{
    var ajaxResponseResult;
    var ajaxResponseMessage;
    var ajaxResponseErrors;

    $.ajax({
        'type'     : 'get',
        'url'      : this.request + '&start=phoneActivate&usrzu_default_phone='+usrzu_default_phone+'&usrzu_activation_code='+usrzu_activation_code,
        'dataType' : 'xml',
        'async'    : false,
        'success'  : function(xml) {
            $("response > result", xml).each (
                function()
                {
                    ajaxResponseResult = ($(this).text());
                }
            );
            
            $("response > error", xml).each (
                    function()
                    {
                        ajaxResponseErrors = ($(this).text());
                    }
                );
            
            $("response > message", xml).each (
                function()
                {
                    ajaxResponseMessage = ($(this).text());
                }
            );
        }
    });
    
    if(ajaxResponseResult && ajaxResponseResult == 1) {
    	$('.msgphone').fadeOut('slow').remove();
        return true;
    }
    else
    {
    	$('#msgphone-content-errors-id').html("Błędny kod lub numer telefonu. Spróbuj ponownie.");
    	return false;
    }
}
