﻿var debug = false;


//Called when the lightbox is initialized
function InitalizeLogInForm() {
    //Clear the form
    ClearLogInForm();

    $("#TB_LogInForm .step").show();
    $("#TB_LogInForm .results").hide();
}

function ClearLogInForm() {
    HideLogInSpinner();
    $("#TB_LogInForm input[type=text]").val('');
    $("#TB_LogInForm input[type=password]").val('');
    $("#TB_LogInForm fielduserinputempty").hide();
    $("#TB_LogInForm fieldpassinputempty").hide();
}

var callInProgress = false;

function ShowLogInSpinner() {
    $("#TB_LogInForm").css("height", "382px");
    $("#TB_LogInForm .wrapper").hide();
    $("#TB_LogInForm .spinner").show();
}

function HideLogInSpinner() {
    $("#TB_LogInForm").css("height", "");
    $("#TB_LogInForm .spinner").hide();
    $("#TB_LogInForm .wrapper").show();
}

function ValidateLogInInput() {
    var valid = true;

    var user = $("#TB_LogInForm .fielduserinput input").val();
    var pass = $("#TB_LogInForm .fieldpassinput input").val();

    if (user == '') {
        $("#TB_LogInForm .fielduserinputempty").show();
        valid = false;
    }
    else {
        $("#TB_LogInForm .fielduserinputempty").hide();
    }
    if (pass == '') {
        $("#TB_LogInForm .fieldpassinputempty").show();
        valid = false;
    }
    else {
        $("#TB_LogInForm .fieldpassinputempty").hide();
    }
    return valid;
}

function SendLogInInquery(username, password) {
    if (!callInProgress && ValidateLogInInput()) {

        //ShowLogInSpinner();

        var user = username; 
        var pass = password; 

        callInProgress = true;

        //alert( "Hér er eftir" );
        CurioWeb.Services.LogInService.LogIn(user, pass, InqueryCompleted, InqueryFailed);
    }
    else {
        //Display error message
        //$("#TB_LogInForm .results p.success").hide();
        //$("#TB_LogInForm .results p.error").show();
        //$("#TB_LogInForm .step").hide();
        $("#TB_LogInForm .results").show();
        HideLogInSpinner();
        callInProgress = false;
    }
    return false;
}

function InqueryCompleted(data, methodContext, methodName) {
    if (data != null) {
        if (data.Success == true) {//Success

            $.fancybox.close();
            //Display success message
            //$("#TB_LogInForm .results p.error").hide();
            //$("#TB_LogInForm .results p.success").show();
            //$("#TB_LogInForm .step").hide();
            //$("#TB_LogInaaForm .results").show();

            //Hide the spinner
            HideLogInSpinner();
            //Set the loading status to false
            callInProgress = false;
            //__doPostBack('ctl00_cphContent_LogInForm1_btnLogin', data.User + ";" + data.Pass);
            __doPostBack('ctl00_pageTop_LogInForm1_btnLogin', data.User + ";" + data.Pass);
        }
        else {//Failed           
            //Villa birta villuboð
            //alert("Error: Send LogIn inquery. (" + data.Status + ")");           

            //Display error message
            //$("#TB_LogInForm .results p.success").hide();
            $("#TB_LogInForm .results").show();            
            //$("#TB_LogInForm .step").hide();
            $("#TB_LogInForm .results").show();
            HideLogInSpinner();
            callInProgress = false;
        }
    }
    else {//Error
        //alert("Error: Send LogIn inquery.");
        //Villa birta villuboð

        //Display error message
        //$("#TB_LogInForm .results p").html('<span style="color: red;">Ekki tókst að koma fyrirspurn þinni til skila.<br />Vinsamlegast reynið aftur síðar.</span><br /><br /><a href="" title="Smelltu hér til að loka" onclick="$.fancybox.close();" >Smelltu hér til að loka.</a>');
        //$("#TB_LogInForm .step").hide();
        $("#TB_LogInForm .results").show();
        callInProgress = false;
        //$.fancybox.close();
    }



}

function InqueryFailed(exception, methodContext, methodName) {
    //Error
    //Villa birta villuboð
    alert("Error: Send LogIn inquery. " + exception.get_message());

    //Hide the spinner
    HideLogInSpinner();

    //Set the loading status to false
    callInProgress = false;
}

