﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("PopUp");

PopUp.PopUpSCart = function (divMainCartID, DivContentID, IDLoadingObj, divHeaderID, divFooterID, showFooter, msgTitle) {

    this._divMainCartID = divMainCartID;
    this._DivContentID = DivContentID;
    this._divMainCart = null;
    this._DivContent = null;
    this._IDLoadingObj = IDLoadingObj;
    this._divHeaderID = divHeaderID;
    this._divFooterID = divFooterID;
    this._isIFrame = false;
    this._showFooter = showFooter;
    this._msgTitle = new String(msgTitle);
}

PopUp.PopUpSCart.prototype = {
    get_divMainCartID: function () {
        return this._divMainCartID;
    },
    get_DivContentID: function () {
        return this._DivContentID;
    },
    get_DivMainCart: function () {
        if (this._divMainCart == null)
            this._divMainCart = $get(this._divMainCartID);
        return this._divMainCart;
    },
    get_DivContent: function () {
        if (this._DivContent == null)
            this._DivContent = $get(this._DivContentID);
        return this._DivContent;
    },
    get_popExtender: function () {
        return $find("ctl00_divPopUpSCart_ModalPopupExtender");
    },
    SetAutoHeight: function () {
        /// <summary>Funzione che imposta l'altezza del div di scroll in base a quella della finestra </summary>
        //debugger
        var clientBounds = $common.getClientBounds();
        var clientWidth = clientBounds.width;
        var clientHeight = clientBounds.height;

        var h = clientHeight;
        if (isNaN(h))
            return;

        h = h - 60;
        this.get_DivMainCart().style.height = h + "px";

        //setto quello interno
        var o = $get(this._divHeaderID, this.get_DivMainCart());
        var b;
        if (o != null) {
            b = Sys.UI.DomElement.getBounds(o);
            if (b.height > 0)
                h = h - Sys.UI.DomElement.getBounds(o).height;
            else h -= o.clientHeight;
        }
        o = Sys.UI.DomElement.getElementById(this._divFooterID, this.get_DivMainCart());
        if (o != null) {
            if (this._showFooter) {
                if (Sys.UI.DomElement.containsCssClass(o, "displayNone"))
                    Sys.UI.DomElement.removeCssClass(o, "displayNone");
                if (!Sys.UI.DomElement.containsCssClass(o, "displayInherit"))
                    Sys.UI.DomElement.addCssClass(o, "displayInherit");
                b = Sys.UI.DomElement.getBounds(o);
                if (b.height > 0)
                    h = h - Sys.UI.DomElement.getBounds(o).height;
                else h -= o.clientHeight;
            }
            else {
                if (Sys.UI.DomElement.containsCssClass(o, "displayInherit"))
                    Sys.UI.DomElement.removeCssClass(o, "displayInherit");
                if (!Sys.UI.DomElement.containsCssClass(o, "displayNone"))
                    Sys.UI.DomElement.addCssClass(o, "displayNone");
            }
        }
        h -= 43;
        var v = this.get_DivContent()
        v.style.height = h + "px";
        if (this._isIFrame) {
            var vif = $get(this.get_DivContentID() + "_fr");
            vif.style.height = h + "px";
            vif.style.width = Sys.UI.DomElement.getBounds(v).width + "px";
        }
        h -= 0;
    },
    Close: function () {
        var pop = this.get_popExtender();
        if (pop == null) return;
        pop.hide();
    },
    Show: function () {
        var pop = this.get_popExtender();
        if (pop == null) return;
        if (this._msgTitle != null || this._msgTitle != "") {
            var tit = $get("ctl00_Ctrad_hLevel1", this.get_DivMainCart());
            if (tit != null) tit.innerText = this._msgTitle;
        }
        this.SetAutoHeight();
        pop.show();
        this.SetAutoHeight();
        PopSCart.SetLoading(false);
    },
    ShowIFrame: function (link) {
        //creazione IFrame
        var conte = this.get_DivContent();
        if (conte == null) return;

        conte.innerHTML = "<iframe id='" + this.get_DivContentID() + "_fr' src='" + link + "'></iframe>";
        if (conte.style.removeAttribute)
            conte.style.removeAttribute("overflow");
        else conte.style.removeProperty("overflow");
        //conte.style.overflow = "none";
        this._isIFrame = true;
        this.Show();
    },
    ShowInnerPage: function (linkPage) {
        var conte = this.get_DivContent();
        if (conte == null) return;

        conte.innerHTML = "<img src='/images/AjaxLoadBig.gif' />";
        conte.style.overflow = "scroll";
        this.SetLoading(true);
        var wRequest = new Sys.Net.WebRequest();
        wRequest.set_url(linkPage); /*"/WS/carrello.aspx");*/
        wRequest.set_httpVerb("GET");
        wRequest.set_userContext(this);
        wRequest.add_completed(this.OnWebRequestCompleted);
        wRequest.invoke();
        return false;

        this._isIFrame = false;

    },
    OnWebRequestCompleted: function (executor, eventArgs) {
        if (executor.get_responseAvailable()) {
            var popc = executor.get_webRequest()._userContext;
            var popCont = popc.get_DivContent();
            popCont.innerHTML = executor.get_responseData();
            popc.Show();
        }
        else {
            if (executor.get_timedOut())
                alert("Server timeout... retry later");
            else
                if (executor.get_aborted())
                    alert("Aborted.. retry later");
        }

    },
    SetLoading: function (setbool) {
        if (this._IDLoadingObj == null || this._IDLoadingObj == "") return;
        var loading = $get(this._IDLoadingObj); //ulVisCart
        if (loading == null) return;
        if (setbool)
            Sys.UI.DomElement.addCssClass(loading, "loading");
        else Sys.UI.DomElement.removeCssClass(loading, "loading");
    }
}


function cart_onenterpressed(ctrl, cod, qt, intKey) {


    if (intKey == 13) 
        UpdBasket(cod, qt, 0, okBasket_Open);
    else
        Sys.UI.DomElement.addCssClass(ctrl, "changed");

}


function cart_apllychanges(txtArray, codArray) {
    if (txtArray == null || codArray == null) return;
    try {
        var l = txtArray.length;
        var i, c, el, q;

        for (i = 0; i < l; i++) {
            el = $get(txtArray[i]);
            if (el) {
                c = codArray[i];
                q = new Number(el.value);
                if (i == l - 1)
                    UpdBasket(c, q, 0, okBasket_Open);
                else    UpdBasket(c, q, 0, null);
            }
        }
        
    }
    catch (ex) {
        okBasket_Open();
        cart_NoRefreshOnAll = false;
    }


}

PopUp.PopUpSCart.registerClass("PopUp.PopUpSCart", null, Sys.IDisposable);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
