$(document).ready(function() {
    if ($Auth.valid()){
        openpriv();
    }            
    //$U.onEnter($("#username"), function() {$('#password').focus();});
    $U.onEnter($("#password"), function() {authUserPost();});
    //$('#login-form').find("#username").clearingInput();
    //$('#login-form').find("#password").clearingInput();
    
    $('.sign-in-js').click(function () {
        $('#sign-in-box').slideToggle(400);
        return false;
    });

    var username = $U.readCookie('sUsername');
    if (username) {
        $('#login-form').find('#username').val(username);
    }
    if ($U.readCookie('loginFailed')) {
        $U.eraseCookie('loginFailed');
        $('#sign-in-box').show();
        $U.errMsg("The Username or Password you have entered is incorrect.");
    }

    if ($U.readCookie('auto_logout')) {
        $U.eraseCookie('auto_logout');
        $U.okMsg("Either a duplicate sign in was detected, or your session has timed out.", null, 'You have been signed out');
    }
});

function openpriv(){
    var host=$U.readCookie('host');
    if (host){
        if (host=='Kevin' || host=='Carla' || host=='ali' || host=='michele' ){
             $Client.httpsGo('dashboard', true);
        }
        else{
            open('/page/private#welcome','_self');
        }
        
    }
    else{        
        open('/page/private#welcome','_self');
    }
}

/*
 * tries to log me in
 */
function authUser() {
    if ($("#FPO").length != 0) {
        if ($("#start_tour").length != 0) {
            $("#FPO").hide();
            $("#start_tour").show();
        } else {
            $("#FPO").stop();
        }
    }
    var u = $("#username").val();
    var p = $("#password").val();
    var d = $("#domain_id").val();
	
	
    d=$G.LOC_ID;
    $Loading.addRequest();
	$Auth.authUser(u, p, d, function (data) {authCallback(data)});
}

function authUserPost() {
    /*if ($("#FPO").length != 0) {
        if ($("#start_tour").length != 0) {
            $("#FPO").hide();
            $("#start_tour").show();
        } else {
            $("#FPO").stop();
        }
    }*/


    var u = $("#username").val();
    var p = $("#password").val();

    if (u=="" || p=="") return;

    $("#domain_id").val($G.LOC_ID);
    //$Loading.addRequest();
    $('#login-form').attr('action', $Client.loginURL());
    $('#login-form').submit();
}

/*
 * log in callback
 */
function authCallback(data) {
    //	open('/page/training#category?id=489','_blank');
    //	return;

    var remember=$("#rememberMe").attr("checked");
    var days=0;
    if (remember) days=90;

    u = $("#username").val();
    if (data['status']) {
        $U.createCookie('host', data['host'], days);
        var subscription_type = 'Anonymous';
        if (data['subscription_type'] == 'promo_code') {
            subscription_type = "Promo subscriber";
        } else if (data['subscription_type'] == 'monthly') {
            subscription_type = "Monthly subscriber";
        } else if (data['subscription_type'] == 'yearly') {
            subscription_type = "Annual subscriber";
        }
        $U.createCookie('SubscriptionType', subscription_type, days)
        $U.createCookie('Gender', ((data['gender'] == 'M') ? 'Male' : 'Female'), days);
        $Auth.createSession(u, data['skey'], data['domain'], data['perms'],days);
        if ($G.MAGENTO_AUTH) {
            //magentoAfterLogin();
        } else {
            //open('/page/training#category?id=489','_blank');
        }
    } else {
        $Loading.removeRequest();
        $U.errMsg("Wrong credentials provided. Please try again.");
        $("#username").val("Username ...");
        $("#password").val("");
        $("#username").clearingInput();
        $("#password").clearingInput();

    }
    if (data['status']) openpriv();
    return;
}

