﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference name="PopCart.js"/>

/*
Main Javascript For Ajax and Asyconous call 
Copyright PopUp Informatica srl
info@pop-up.i
www.pop-up.it
*/

var PopMsgPoints=null;
function MsgPoints() {
    if (PopMsgPoints == null) {
        PopMsgPoints = new PopUp.PopUpSCart("ctl00_panGenericMsg", "panGenericMsg-content", "ulVisCart", "popChead", "popCbottom", false, "I tuoi sconti");
    }

    PopMsgPoints.ShowIFrame("/orders/UserPoints.aspx");
    return false;
}
function MsgVouchers() {
    if (PopMsgPoints == null) {
        PopMsgPoints = new PopUp.PopUpSCart("ctl00_panGenericMsg", "panGenericMsg-content", "ulVisCart", "popChead", "popCbottom", false, "Inserisci un codice Voucher");
    }

    PopMsgPoints.ShowIFrame("/orders/VoucherInsert.aspx");
    return false;
}
// This function open a popup div and load the basket
function OpenCarrello(indexselected) {
    try
    {
        if (PopSCart == null || PopSCart == "undefined") return false;
    }
    catch(e)
    {
    return false;
    }

    var s = new String("/WS/carrello.aspx");
    if (indexselected != null) {
        s += "?iCombo=" + indexselected.toString();
        //var lnkcar = $get("ctl00_car_go");
        //if (lnkcar != null) lnkcar.href = "/checkout/checkout.aspx?iCombo=" + indexselected.toString();
    }
    PopSCart.ShowInnerPage(s);
    return false;
}

//Close the PopUp div
function ClosePopUp() {
    try {
        if (PopSCart == null || PopSCart == "undefined") return false;
    }
    catch (e) {
        return false;
    }
    PopSCart.Close();
    return false;
}

function OpenCarrelloOLD()
{
//    debugger
    PopSCart.SetLoading(true);
    var wRequest =  new Sys.Net.WebRequest();
    wRequest.set_url("/WS/carrello.aspx");  
    wRequest.set_httpVerb("GET");
    wRequest.set_userContext("basket");
    wRequest.add_completed(OnWebRequestCompleted);
    wRequest.invoke();
    return false;
}

function OnWebRequestCompleted(executor, eventArgs) 
{
    if(executor.get_responseAvailable()) 
    {
        if (executor.get_webRequest()._userContext=="basket")
        {
            //popup.style.left = "0";
            var popCont = document.getElementById("popup-content");
            popCont.innerHTML = executor.get_responseData();
            PopSCart.Show();
        }
        else
        if (executor.get_webRequest()._userContext == "PupBe1") {
            var popCont = $get("divPupBe1-content");
            popCont.innerHTML = executor.get_responseData();
        }
    }
    else
    {
        if (executor.get_timedOut())
            alert("Server timeout... retry later");
        else
            if (executor.get_aborted())
                alert("Aborted.. retry later");
    }
    PopSCart.SetLoading(false);
}

function IsValidQta(Expression)
{
    Expression = Expression.toLowerCase();
    RefString = "0123456789";
    
    if (Expression.length < 1) 
        return (false);
    
    for (var i = 0; i < Expression.length; i++) 
    {
        var ch = Expression.substr(i, 1);
        var a = RefString.indexOf(ch, 0);
        if (a == -1)
            return (false);
    }
    return(true);
}

function UpdCountry(newCountry) {
    var nc = new String(newCountry);
    if (nc == "") return;

    PopUp.Malossi.Store.WS.WSAJAX_MalossiStore.Country_SetMyCountry(nc, UpdCountryOK, null, null);
}

    function UpdCountryOK(result, context) {
        OpenCarrello();
    }
/*Basket*/
    function UpdBasket(codart, qta, numbasket, funcOK) {
       // debugger
        var n = new Number(qta);
        if (isNaN(n))
        {
            var txtq = document.getElementById(qta);
            if (txtq==null) return;
            if (!IsValidQta(txtq.value))
            {
                alert(txtq.value + " Not a Number!");
                return;
            }
            n = new Number(txtq.value);
        }
        PopUp.Malossi.Store.WS.WSAJAX_MalossiStore.UpdateBasket(codart, n, numbasket, false, funcOK, FailBasket, null);
    }
    function AddBasket(codart, qta, numbasket, funcOK, IDPulsante) {
        //debugger
        if (IDPulsante != null && IDPulsante != "undefined") {
            var objPulsante = $get(IDPulsante);
            if (Sys.UI.DomElement.containsCssClass(objPulsante, "acq-link-loading")) return;
            Sys.UI.DomElement.addCssClass(objPulsante, "acq-link-loading");
        }
        var n = new Number(qta);
        if (isNaN(n))
        {
            var txtq = document.getElementById(qta);
            if (txtq==null) return;
            if (!IsValidQta(txtq.value))
            {
                alert(txtq.value + " Not a Number!");
                return;
            }
            n = new Number(txtq.value);
        }
            
        PopUp.Malossi.Store.WS.WSAJAX_MalossiStore.UpdateBasket(codart, n, numbasket, true, funcOK, FailBasket, objPulsante);
    }
    function AddBasketNote(codart, qta, numbasket, funcOK, IDPulsante,Nota) {
        //debugger
        if (IDPulsante != null && IDPulsante != "undefined") {
            var objPulsante = $get(IDPulsante);
            if (Sys.UI.DomElement.containsCssClass(objPulsante, "acq-link-loading")) return;
            Sys.UI.DomElement.addCssClass(objPulsante, "acq-link-loading");
        }
        var n = new Number(qta);
        if (isNaN(n)) {
            var txtq = document.getElementById(qta);
            if (txtq == null) return;
            if (!IsValidQta(txtq.value)) {
                alert(txtq.value + " Not a Number!");
                return;
            }
            n = new Number(txtq.value);
        }

        PopUp.Malossi.Store.WS.WSAJAX_MalossiStore.UpdateBasketNote(codart, n, numbasket, true, Nota, funcOK, FailBasket, objPulsante);
    }
    function okBasket(result, context)
    {
   
        UpdateCartIndicator();
        WarnCart();
        if (context != null && context !== "undefined")
            Sys.UI.DomElement.removeCssClass(context, "acq-link-loading");
    }
    function okBasket_Open(result, context) {
 
        OpenCarrello();
        UpdateCartIndicator();
        if (context != null && context !== "undefined")
          Sys.UI.DomElement.removeCssClass(context, "acq-link-loading"); 
    }
    function FailBasket(result, context)
    {
        alert("Error occurred during basket update... please try later");
    }
    
    function UpdateCartIndicator()
    {
        try {
            __doPostBack('ctl00$btHiddenCart','');
        }
        catch(e)
        {
            alert(e.message);
        }
    }
/*End Basket*/

    function PBMarca(args) {
    try
        {
            __doPostBack('ctl00$PGetMod',args);
        }
        catch(e)
        {
            alert(e.message);
        }
}

/* Funzioni per la filtro risultato*/
var sb_TimerFilter =null;

function FilterListPressKey()
{
    if (sb_TimerFilter!=null)
        clearTimeout(sb_TimerFilter);
    sb_TimerFilter = setTimeout(FilterList, 500);
    return false;
}

function FilterList()
{
        //setTimeout('__doPostBack(\'ctl00$CBody$txtFiltra\',\'\')', 0)
    __doPostBack("ctl00$CBody$bthidd", "");
    }

    /*funzioni per Spot malossi
    function PopUp_RollSpot(timeout) {
        var arr = new Array();
        arr.pop
        if (timeout!=5000)
            {
            debugger
            alert(timeout);
            }
        PopUp.Malossi.Store.WS.WSAJAX_MalossiStore.SpotMalossi_GetNext(PopUp_RollSpot_ok, PopUp_RollSpot_fail, timeout);
        

    }

    function PopUp_RollSpot_fail(result, context) {
        alert("Rollup Malossi Spot faild");
        setTimeout("PopUp_RollSpot(" + context + ")", context)
    }

    function PopUp_RollSpot_ok(result, context) {

        if (!result.Error)
            PopUp_RollSpot_DoIt(result.HTML, result.InfoMessage, result.OtherData);
        setTimeout("PopUp_RollSpot(" + context + ")", context)
    }*/

    function PopUp_RollSpot_DoIt(s_img, s_lnk, s_target) {
        //debugger
        var objA = $get("ctl00_lnkSpotMal");
        if (objA == null) return;
        objA.href = s_lnk;
        objA.target = s_target;
        var arr = objA.getElementsByTagName("img");
        if (arr.length > 0) {
            arr[0].src = s_img;
        }
    }

    var PopUp_RollSpotMain_Target = "_self";
    var PopUp_RollSpotMain_link = "";
    var PopUp_RollSpotMain_lastIndex = 0;
    var PopUp_RollSpotMain_dl = true;
    function PopUp_RollSpotMain_Dolink() {
        if (PopUp_RollSpotMain_link == "") return;
        if (!PopUp_RollSpotMain_dl) {
            PopUp_RollSpotMain_dl = true;
            return;
        }
        if (PopUp_RollSpotMain_Target == "_blank")
            window.open(PopUp_RollSpotMain_link);
        else window.location = PopUp_RollSpotMain_link;
    }
    function PopUp_RollSpotMain_DoIt(s_index, s_img, s_lnk, s_target, s_title, s_descr, s_price, s_other, s_color1, s_color2, s_color3, s_color4) {
        //debugger
        var panSpot = $get("ctl00_CBody_panRoll1");
        if (panSpot == null) return;
        panSpot.style.backgroundImage = "url(" + s_img + ")";

        var o = $get("ctl00_CBody_lblR1_Tit", panSpot);
        if (o != null) {
            o.innerHTML = s_title;
            if (s_color1 != "#" && s_color1 != "")
                o.style.color = s_color1;
        }
        o = $get("ctl00_CBody_lblR1_Descr", panSpot);
        if (o != null) {
            o.innerHTML = s_descr;
            if (s_color2 != "#" && s_color2 != "")
                o.style.color = s_color2;
        }
        o = $get("ctl00_CBody_lblR1_Price", panSpot);
        if (o != null) {
            o.innerHTML = s_price;
            if (s_color3 != "#" && s_color3 != "")
                o.style.color = s_color3;
        }
        o = $get("ctl00_CBody_lblR1_Info", panSpot);
        if (o != null) {
            o.innerHTML = s_other;
            if (s_color4 != "#" && s_color4 != "")
                o.style.color = s_color4;
        }
        PopUp_RollSpotMain_link = s_lnk;
        PopUp_RollSpotMain_Target = s_target;
        o = $get("ctl00_CBody_pr_Puls" + PopUp_RollSpotMain_lastIndex, panSpot);
        if (o != null)
            Sys.UI.DomElement.removeCssClass(o, "attivo");
        o = $get("ctl00_CBody_pr_Puls" + s_index, panSpot);
        if (o != null)
            Sys.UI.DomElement.addCssClass(o, "attivo");
        PopUp_RollSpotMain_lastIndex = s_index;
        
    }

    function PopUp_RollSpot() {
        PopUp_RollSpot_Main();
        PopUp_RollSpot_Mal();
    }

    var SMn_Abilita = true;
    function PopUp_RollSpot_Main(index) {
        //debugger
        if (index == null || isNaN(index)) {
            if (!SMn_Abilita) return;
            SMn_idx = SMn_idx + 1;
        }
        else {
            SMn_idx = index;
            SMn_Abilita = false;
            PopUp_RollSpotMain_dl = false;
        }
            if (SMn_idx >= SMn_Arr.length)
                SMn_idx = 0;
            if (SMn_idx < SMn_Arr.length) {
                var s = new String();
                s = SMn_Arr[SMn_idx];
                var arr = s.split("§")
                if (arr.length>=11)
                    PopUp_RollSpotMain_DoIt(SMn_idx+1, arr[0], arr[1], arr[2], arr[3], arr[4], arr[5]
                        , arr[6], arr[7], arr[8], arr[9], arr[10]);
            }
    }

    function PopUp_RollSpot_Mal() {
            SM_idx = SM_idx + 1;
            if (SM_idx >= SM_Arr.length)
            SM_idx = 0;
            if (SM_idx < SM_Arr.length) {
                var s = new String();
                s = SM_Arr[SM_idx];
                var arr = s.split("§");
                if (arr.length>=3)
                    PopUp_RollSpot_DoIt(arr[0], arr[1], arr[2]);
            }
    }
    function BeTheFirst_LoadData() {
        var popCont = $get("divPupBe1-content");

        var wRequest = new Sys.Net.WebRequest();
        wRequest.set_url("/WS/popbe1.aspx");
        wRequest.set_httpVerb("GET");
        wRequest.set_userContext("PupBe1");
        wRequest.add_completed(OnWebRequestCompleted);
        wRequest.invoke();
    }




