﻿var ShownImagesHash = new Object ();

function ShowImage(item) {
    document.getElementById(item).style.display = "";
    HideAllShownImages1();
    ShownImagesHash [item] = "1";
}

function HideImage(item) {
    document.getElementById(item).style.display = "none";
    ShownImagesHash[item] = "0";
    HideAllShownImages1();
}

function HideAllShownImages1() {
    for (var item in ShownImagesHash) {
        if (ShownImagesHash[item] == "1") {
            document.getElementById(item).style.display = "none";
            ShownImagesHash[item] = "0";
        }
    }
}

function PopupProduct(ProductID) {
    window.open (OdarimPath + "/product.aspx?popup=1&id=" + ProductID, "ProductPopup" + ProductID, "toolbar=no,scrollbars=yes,directories=no, status=no,menubar=no,resizable=yes, width=800, height=900");
}

function getCookie(name) {
    var cookie = " " + document.cookie;
    var search = " " + name + "=";
    var setStr = null;
    var offset = 0;
    var end = 0;
    if (cookie.length > 0) {
        offset = cookie.indexOf(search);
        if (offset != -1) {
            offset += search.length;
            end = cookie.indexOf(";", offset)
            if (end == -1) {
                end = cookie.length;
            }
            setStr = unescape(cookie.substring(offset, end));
        }
    }

    if (setStr == null)
        setStr = "0";
    
    return (setStr);
}

function BasketAltert(ProductID, sum, cnt, ispm) {
    url = OdarimPath + "/basket/basketalert.aspx?id=" + ProductID;
    if (ispm)
        url = url + "&ispm=1";
        
    window.open(url, "BasketPopup", "toolbar=no,scrollbars=yes,directories=no, status=no,menubar=no,resizable=yes, width=720, height=470");
}

function KopilkaAltert(ProductID) {
    window.open(OdarimPath + "/Kopilka/Kopilkaalert.aspx?id=" + ProductID, "KopilkaPopup", "toolbar=no,scrollbars=yes,directories=no, status=no,menubar=no,resizable=yes, width=720, height=440");
}

var ImagesArray = new Array();
var ImageURLsArray = new Array();

function LoadImages() {
    for (i = 0; i < ImagesArray.length; i++) {
        document.getElementById(ImagesArray[i]).src = ImageURLsArray[i];
        ShownImagesHash [ImagesArray[i]] = "0";
    }
}

var PackCatID = 411;

function OpenPackWin(controlid, buttonid, productid) {
    window.open(OdarimPath + '/selectpack.aspx?controlid=' + controlid + '&catid=' + PackCatID + '&ispacking=1&productid=' + productid + '&buttonid=' + buttonid, 'Win1' + controlid, 'width=800,toolbar=no,scrollbars=yes,directories=no, status=no,menubar=no,resizable=yes');
}

function SelectPack(controlid, val, buttonid) {
    window.opener.document.getElementById(controlid).value = val;
    window.opener.document.getElementById(buttonid).click();
    window.close();
}

function addBookmark() {
    url = location.href;
    title = document.title;

    //Gecko
    if ((typeof window.sidebar == "object") && (typeof window.sidebar.addPanel == "function")) window.sidebar.addPanel(title, url, "");
    //IE4+ 
    else if (typeof window.external == "object") window.external.AddFavorite(url, title);
    //Opera7+ 
    else if (window.opera && document.createElement) {
        var a = document.createElement('A');
        if (!a) return false; //IF Opera 6 
        a.setAttribute('rel', 'sidebar');
        a.setAttribute('href', url);
        a.setAttribute('title', title);
        a.click();
    }
    else return false;

    return true;
}

function ShowProduct(TestDivID, ProductID) {
    //$("#" + TestDivID).load("/odarim/product.aspx?popup=1&id=" + ProductID);
    //$("#" + TestDivID).css("style", "");
}

function SendToFriend(ProductID) {
    window.open(OdarimPath + "/sendtofriend.aspx?ProductID=" + ProductID + "&ref=" + escape(document.location.href.replace("popup=1", "").replace("product.aspx", "productbig.aspx")), "SendToFriendPopup", "toolbar=no,scrollbars=yes,directories=no, status=no,menubar=no,resizable=yes, width=500, height=440");
}

function ChangeDeliveryUrgency(ddl, OrderID) {
    var row = $("#PaymentMethodRow" + OrderID).get(0);
    var DeliveryDateSpan1 = $("#DeliveryDateSpan1_" + OrderID).get(0)
    var DeliveryDateSpan2 = $("#DeliveryDateSpan2_" + OrderID).get(0)

    if (ddl.value == "4" || ddl.value == "6" || ddl.value == "5") {
        row.style.display = "none";
        DeliveryDateSpan1.style.display = "none";
        DeliveryDateSpan2.style.display = "none";
    }
    else {
        row.style.display = "";
        DeliveryDateSpan1.style.display = "";
        DeliveryDateSpan2.style.display = "";
    }
}

function SetDeliveryUrgency(ddlID, OrderID) {
    
    var ddl = $("#" + ddlID).get(0);

    ChangeDeliveryUrgency(ddl, OrderID);
}

function ShowLoginForm () {
    if ($ ("#login").get (0).style.display == "")
        $ ("#login").get (0).style.display = "none";
    else
        $ ("#login").get (0).style.display = "";
}

function CheckUser () {
        var email = $ ("#EmailTextBox").get (0).value;
        var password = $ ("#PasswordTextBox").get (0).value;

        var LoginResultSpan = $ ("#LoginResultSpan");
        
        if (email == "")
        {
            LoginResultSpan.empty ();
            LoginResultSpan.append ("E-mail  не задан");
            return;
        }
        else
        {
            CheckUserFunc (email, password);
        }
    }
    
function ProcessUserCheckResult ()
{
    if (window.checkresult.result == "ok")
    {
        var email = $ ("#EmailTextBox").get (0).value;
        var password = $ ("#PasswordTextBox").get (0).value;

        rememberme = "0";

        if ($("#RememberMeCheckBox").get(0).checked)
            rememberme = "1";
        
        LoginRedirect (email, password, rememberme);
    }
    else
    {
        var LoginResultSpan = $ ("#LoginResultSpan");
        
        LoginResultSpan.empty ();
        LoginResultSpan.append ("Неверный e-mail/пароль");
    }
}

var LoginRedirectUrl = "";

function LoginRedirect(email, password, rememberme)
{
    var data = "email=" + email + ";password=" + password + ";rememberme=" + rememberme;

    var location = SSOServicePageURL + "?a=login&ReturnURL=" + escape(window.location) + "&data=" + escape(data) + "&WrongURL=" + escape(window.location);
    
    window.location = location;
}

function Logout() {
    if (LoginRedirectUrl == "")
        LoginRedirectUrl = window.location;

    window.location = SSOServicePageURL + "?a=logout&ReturnURL=" + escape(LoginRedirectUrl);
}

function CheckLogin() {
    var RequestString = SSOServicePageURL + "?a=getloggedin&ReturnURL=&callback=?";

    $.getJSON(RequestString, {}, onCheckLoginSuccess);
}

function onCheckLoginSuccess(obj) {
    var LoginImage = $("#LoginImage").get(0);
    
    if (obj.result == "loggedin") {
        LoginImage.src = OdarimPath + "/images/logout.gif";
        LoginImage.onclick = Logout;
    }
    else {
        LoginImage.src = OdarimPath + "/images/login.gif";
        LoginImage.onclick = ShowLoginForm;

        var s = window.location.toString ();

        if (s.indexOf("#loginlink") >= 0) {
            ShowLoginForm();
        }
    }
}

function ScrollToElement(theElement) {

    var selectedPosX = 0;
    var selectedPosY = 0;

    while (theElement != null) {
        selectedPosX += theElement.offsetLeft;
        selectedPosY += theElement.offsetTop;
        theElement = theElement.offsetParent;
    }

    window.scrollTo(selectedPosX, selectedPosY);

}
