﻿function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}
function isOldIE() {
    var ver = getInternetExplorerVersion();
    if (ver > -1) {
        if (ver < 8.0) {
            return true;
        }
    }
    return false;
}
function setCookie(c_name, value) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate());
    var c_value = escape(value) + "; expires=" + exdate.toUTCString();
    document.cookie = c_name + "=" + c_value;
}
function getCookie(c_name) {
    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == c_name) {
            return unescape(y);
        }
    }
    return "";
}
function savePopupState(key, value) {
    if (isOldIE()) {
        setCookie(key, value);
    } else {
        localStorage.setItem(key, value);
    }
}

function getPopupState(key) {
    if (isOldIE()) {
        return getCookie(key);
    } else {
        return localStorage.getItem(key);
    }
}
//make all buttons on the site unfocusable
$(":submit").focus(function () {
    $(this).blur();
});
$(":button").focus(function () {
    $(this).blur();
});

//show forgot password functionality on buttonclick
$(".forgotPasswordClick").click(function () {
    $(".glemtPassword").toggle("400");
});

//popup the dialogs after postback if they need to be open (wrong password etc.)
$(document).ready(function () {
    $(".contourSubmit").val('');
    $(".loginFailureText").text($(".loginFailureCopy").text());
    var t = $(".loginFailureText").text().replace(/^\s+|\s+$/g, '');
    var errorPassword = $(".forgotPasswordError").text().replace(/^\s+|\s+$/g, '');
    var forgotPassword = $(".tdForgotPas").length;
    if (t != '' || forgotPassword == 0 || errorPassword != '') {
        var login = $("#loginDiv").dialog({
            autoOpen: false,
            width: 'auto',
            modal: true
        });
        login.dialog('open');
        $(".forgotPasswordUserName").val($(".userName").val());
        $(".glemtPassword").show();
        $(".ui-dialog").appendTo("#form1");
        // prevent the default action, e.g., following a link
        return false;
    }
    if ($(".formShowroom").children(".contourMessageOnSubmit").length != 0) {
        var text = $(".contourMessageOnSubmit").text().replace(/^\s+|\s+$/g, '');
        if (text.indexOf("Vi") != -1) {
            var showroom = $("#showroom").dialog({
                autoOpen: false,
                width: 'auto',
                modal: true,
                close: function (event, ui) {
                    savePopupState('popup', '');
                    //localStorage.setItem('popup', '');
                }
            });
            //if (localStorage.getItem('popup') == 's') {
            if (getPopupState('popup') == 's') {
                showroom.dialog('open');
            }
            $(".ui-dialog").appendTo("#form1");
            $(".cntContentShowroom").children(".contentInfo").children(".txt").hide();
            $(".formheader").hide();
        }
    } else if ($(".contourMessageOnSubmit").length != 0) {
        var text = $(".contourMessageOnSubmit").text().replace(/^\s+|\s+$/g, '');
        if (text.indexOf("Tak") != -1) {
            var notice = $("#opret").dialog({
                autoOpen: false,
                width: 'auto',
                modal: true,
                close: function () {
                    savePopupState('popupOpret', '');
                    //localStorage.setItem('popupOpret', '');
                }
            });
            //if (localStorage.getItem('popupOpret') == 'o') {
            if (getPopupState('popupOpret') == 'o'){
                //$("#opretA").click();
                notice.dialog('open');
            }
            $(".cntContentUser").children(".txt").text('');
            $(".profilForm").children(".txt").text('');
        }
    }


//    $(".loginButton").click(function () {
//        ajaxReload({ macro: "ShiftEmptyBasketOnLogin" }, "#clearBasketOnLogin");
//    });
});
