﻿
var Cetrine = window.Cetrine || {};
var Website = window.Website || {};

Cetrine.Mposto = {};

var buttonsBugReport = null;
var buttonsSignIn = null;

jQuery(document).ready(function () {

    jQuery("#txtPassword").keypress(function (e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            InterPassport.Submit('', '');
        };
    });

});

//==========================================================================================================================================================================================================================================================

Cetrine.Tools = (function () {

    return {

        LockPage: function () {

            if (jQuery("#JD_overlay").length == 0) jQuery("body").append("<div id='JD_overlay'></div>");

            jQuery("#JD_overlay").addClass("JD_overlayBG"); //use background and opacity

        },
        UnlockPage: function () {
            jQuery("#JD_overlay").remove();
        },
        PageBack: function () {
            history.back();
        },
        ShowLayerLoader: function () {

            Cetrine.Tools.LockPage();
            jQuery("body").append("<div id='divLayerLoader' style=\"position:absolute;top:45%;left:49%;z-index:1001;\"><img src=\"/website/images/layerLoader.gif\"/></div>");

        },
        HideLayerLoader: function () {

            Cetrine.Tools.UnlockPage();
            jQuery("#divLayerLoader").remove();

        },
        CatchException: function (error, userContext, methodName, Title, callback) {

            Cetrine.Tools.HideLayerLoader();

            jQuery('#divWait').hide();
            jQuery('#divForm').show();

            Cetrine.Tools.DisplayErrorPopup(Title, error.get_message(), callback);

            if (error.get_exceptionType() == 'System.Exception') { //log it

                try {

                    var WS = new mposto.cetrine.net.ws.wsDwarf();
                    mposto.cetrine.net.ws.wsDwarf.set_path('/ws/WsDwarf.asmx');

                    if (!methodName) methodName = '';

                    WS.LogException(window.location.href, methodName, error.get_message(), error.get_stackTrace());

                } catch (err) { }

            };

        },
        DisplayErrorPopup: function (Title, Message, Callback) {

            var PopupTitle = Attention;

            if (Title) PopupTitle = Title;

            jQuery("body").append("<div id='divError' style='display:none;'></div>");

            jQuery("#divError").html(Message.replace("'", "\'"));

            setTimeout(function () {

                jQuery("#divError").dialog({
                    title: PopupTitle,
                    buttons: { "Ok": function () {

                        jQuery(this).dialog("destroy");
                        jQuery("#divError").remove();

                        jQuery("html").css("overflow-x", "auto");
                        jQuery("html").css("overflow-y", "auto");

                        if (Callback) { Callback() };
                    }
                    }
                })

            }, 100);

        },
        LoadCountries: function (ctl) {

            var WS = new mposto.cetrine.net.ws.wsDwarf();
            mposto.cetrine.net.ws.wsDwarf.set_path('/ws/WsDwarf.asmx');

            WS.CountriesList(IP,
                function (countries) {

                    ctl.empty();
                    ctl.removeAttr("disabled");

                    var CountryToSelect = -1;

                    jQuery.each(countries, function () {

                        var option = document.createElement("option");
                        option.value = this.Id;
                        option.text = this.Name;

                        ctl.get(0)[jQuery('#ddlCountry option').length] = option;

                        if (this.Selected) CountryToSelect = this.Id;

                    });

                    if (CountryToSelect > -1) ctl.val(CountryToSelect);

                }, Cetrine.Tools.CatchException
             );
        },
        PostTo: function (Contacts) {

            location.href = urlPostTo.replace("{0}",Contacts);

        }
    };

})();

InterPassport = (function () {

    this.SignInCallbackFn = null;
    this.signed = false;
    this.AuthorisationRequired = false;

    return {

        OpenMyAccount: function () {
            InterPassport.IsSignedIn(function () {
                InterPassport.OpenMyAccountProcess();
            });
        },
        OpenMyAccountProcess: function () {
            window.location.href = urlAccount;
        },
        SignOut: function () {

            var WS = new mposto.cetrine.net.ws.wsInterPassport();
            mposto.cetrine.net.ws.wsInterPassport.set_path('/WS/wsInterPassport.asmx');
            WS.SignOut(
                function () {
                    window.location.href = urlIndex;
                }, Cetrine.Tools.CatchException
             );

        },
        IsSignedIn: function (ContinueTo) {

            var WS = new mposto.cetrine.net.ws.wsInterPassport();
            mposto.cetrine.net.ws.wsInterPassport.set_path('/WS/wsInterPassport.asmx');

            WS.IsSignedIn(function (status) {

                if (status == 'true') {
                    ContinueTo();
                } else {
                    InterPassport.AuthorisationRequired = true;
                    ShowSignIn(true, ContinueTo);
                };

            }, Cetrine.Tools.CatchException
            );

        },
        SignIn: function (modal, callback) {

            if (!SignInActive) {
                Cetrine.Tools.DisplayErrorPopup(null, alertSignInOffline);
                return;
            };

            SignInCallbackFn = callback;

            jQuery('#txtUsername').val(CookieRead("username"));

            if (jQuery('#txtUsername').val().length > 0) {
                setTimeout("jQuery('#txtPassword').focus()", 1100);
            } else {
                setTimeout("jQuery('#txtUsername').focus()", 1100);
            };

            jQuery("html").css("overflow-x", "hidden");

            jQuery('#div_login').dialog({

                resizable: false
                , title: CetrineInterPassport
                , width: 470
                , height: 345
                , beforeclose: function (event, ui) {
                    if (InterPassport.AuthorisationRequired && !InterPassport.signed) return false
                }
                , close: function (event, ui) {
                    InterPassport.Close(true);
                }
                , buttons: buttonsSignIn

            });

            jQuery('.ui-dialog').show("drop", { direction: "left" }, 1000);
            setTimeout('Cetrine.Tools.LockPage()', 1000);

        },
        Close: function (Closed) {

            Cetrine.Tools.UnlockPage();

            jQuery('.ui-dialog').hide("drop", { direction: "right" }, 1000);

            if (!Closed) {
                setTimeout(function () {
                    jQuery("#div_login").dialog("close");
                }, 1000);
            };

            jQuery("html").css("overflow-x", "auto");

        },
        Submit: function (Username, Password, DoNotHide) {

            var username = Username;
            var password = Password;

            if (jQuery('#txtUsername').length > 0 && username.length==0) username = jQuery('#txtUsername').val();
            if (jQuery('#txtPassword').length > 0 && password.length==0) password = jQuery('#txtPassword').val();

            if (username.length < 2) {
                Cetrine.Tools.DisplayErrorPopup(null, AlertEnterUsername);
                return false;
            };

            if (password.length < 2) {
                Cetrine.Tools.DisplayErrorPopup(null, AlertEnterPassword);
                return false;
            };

            if (!DoNotHide) {
                jQuery('#frm_signin').hide();
                jQuery("#div_login").dialog("option", "buttons", null);
                jQuery('#frm_loading').show();
            };

            var WS = new mposto.cetrine.net.ws.wsInterPassport();
            mposto.cetrine.net.ws.wsInterPassport.set_path('/WS/wsInterPassport.asmx');

            WS.SignIn(username,
              password,
              IP,
              document.getElementById('chk_remember').checked,
                function (VisitorsResourceId) {

                    InterPassport.signed = true;

                    CookieCreate("username", username, 14);

                    if (InterPassport.SignInCallbackFn) {

                        InterPassport.SignInCallbackFn();

                        jQuery('#ctl00_Content_litSignedAs').html(SignedAs + username);

                    } else if (!DoNotHide) { // normal sign in

                        jQuery('#frm_signin').hide();
                        jQuery('#frm_loading').hide();

                        jQuery('#ctl00_ctl00_Content_div_header_menu').hide();
                        jQuery('#ctl00_ctl00_Content_div_header_menu_signed').show();
                        jQuery('#div_signedinas').html(SignedAs + username);

                        top.location.reload();

                    } else { // forced sign in on admin pages
                        top.location.reload();
                    };

                },

                 function (error, userContext, methodName) {

                     if (InStr(error.get_message(), '(019)') > 1) {
                         location.href = SignInPage + '?action=invalidpassword';
                     } else {
                         jQuery('#frm_signin').show();
                         jQuery('#frm_loading').hide();
                         jQuery("#div_login").dialog("option", "buttons", buttonsSignIn);

                         if (error) Cetrine.Tools.DisplayErrorPopup(null, error.get_message());
                     };
                 }
             );

            jQuery('#txtPassword').val('');

        }

    };

})();
