function submitLoginForm() {
    jQuery.post('/ajax/login.php',jQuery('#topJsLoginForm').formSerialize(),function (data,textStatus) {
    if (data.success) {
        jQuery('div.profile').fadeTo(800,0,function () {
            jQuery('div.profile').replaceWith(data.htmlContent);
            jQuery('div.profile').fadeTo(800,100);
            bindLogoutEvent();
            loadData();
            //if (forum) window.location.reload();
        });
        
    } else {
        if (data.reason=='wrong_pass') {
        jQuery('div.profile').fadeTo(800,0,function () {
            jQuery('div.profile').html('<p class="top_login_error">Incorrect username/password combination! The form will reappear in a few moments!</p>');
            jQuery('div.profile').fadeTo(800,100);
        });
        
        } else if (data.reason=='not_active') {
                    jQuery('div.profile').fadeTo(800,0,function () {
            jQuery('div.profile').html('<p class="top_login_error">Your account is not active! The form will reappear in a few moments!</p>');
            jQuery('div.profile').fadeTo(800,100);
        });    
            
        }
        setTimeout(function () {
            jQuery('div.profile').fadeTo(800,0,function() {
                jQuery('div.profile').replaceWith(data.htmlContent);
                jQuery('div.profile').fadeTo(800,100,function () {
                    bindLoginEvent();
                });
                
            });
        
        },5000);
        
    }       
    },'json');
    
    jQuery('div.login,div.reg').fadeTo(800,0,function () {
        jQuery('div.login').html('<img src="/images/ajax-loader.gif" style="float:right"/>');
        jQuery('div.login').fadeTo(800,100);
    
    });
    //alert(jQuery("#topJsLoginForm").formSerialize());
    
}

function logout() {
    jQuery.post('/ajax/logout.php','',function (data,textStatus) {
        jQuery('div.profile').fadeTo(800,0,function () {
            jQuery('div.profile').replaceWith(data);
            jQuery('div.profile').fadeTo(800,100,function () {
                window.location.reload();
                bindLoginEvent();
                loadData();
            }); 
            
        });
           
    
    },'html');

}
function bindLogoutEvent() {
 jQuery("#topLogoutBtn").bind('click',function (e) {
        logout();
        return false;
    });
}

function bindLoginEvent() {
    jQuery("#topJsLoginFormBtn").bind('click',function (e) {
        submitLoginForm();
        return false;
    });
}


jQuery(document).ready(function () {
    if (jQuery('#topJsLoginFormBtn')!=null)
        bindLoginEvent();
    if (jQuery('#topLogoutBtn')!=null)
        bindLogoutEvent();
    if (jQuery('#register_form_ok')!=null) {
        jQuery('#register_form_ok').click(register);
        jQuery('#forgot_pass_form_ok').click(forgotPassword);
    }

});

function register() {
    //alert('register');
    jQuery.post('/ajax/register.php',jQuery('#register_form').formSerialize(),function (data,textStatus) {
        if (data.success) {
            jQuery('div#rg span.pp_line').remove();
            
            jQuery('div#rg div.ppblock div').after('<p class="register_success success">'+data.successText+'</p>');
            
        } else {
            jQuery('div#rg div.ppblock span.pp_line:first').before('<p class="error">Please fix the following errors to continue:<br />'+data.errorText+'</p>');
        }
        
    },'json');
    
    return false;
}


function forgotPassword() {
    jQuery.post('/ajax/forgot_password.php',jQuery('#forgot_pass_form').formSerialize(),function (data,textStatus) {
        if (data.success) {
            jQuery('div#fp span.pp_line').remove();
            
            jQuery('div#fp div.ppblock div').after('<p class="register_success success">'+data.successText+'</p>');
            
        } else {
            jQuery('div#fp div.ppblock p.error').remove();
            jQuery('div#fp div.ppblock span.pp_line:first').before('<p class="error">Please fix the following errors to continue:<br />'+data.errorText+'</p>');
        }
        
    },'json');
    
    return false;
}

