﻿var page_error=false;

var landingFn = "";
var landingLn = "";
var landingEmail = "";

// 30/09/2009 [FP] - Login Landing da Newsletter
var landingMoreInfoOpen = false;
var landingFromHyst = false;

// 19/01/2010 [FP]
var landing_img_preload;
var landing_htm = "";
var landing_hashie = "";

// 01/07/2010 [FP] - BIF: more info su Pop_Up
var divInfoResult = "";

function initLanding(fromHyst)
{
    inHomePage=true;
    inLanding=true;
    inSurvey = false;   // 28/07/2009 [FP] - Survey
    
    divInfoResult = (isMobile ? "moreinforesult" : "div_moreinforesult");

    // 20/01/2010 [FP] - Controllo per IE6 (altrimenti dà out of memory sul mapping dell'immagine)
    if (landing_hashie != "" && landing_hashie == window.location.hash && fromHyst && fromHyst == true)
        return;
    landing_hashie = window.location.hash;
    
    if (fromHyst)
        landingFromHyst = fromHyst;
    else
        landingFromHyst = false;
    
    
    //initHomeLanding();
    initHeader();
    initFooter();
    initMex();
    GetUserReg();
    
    if (page_error)
        makeErrorMex(document.getElementById("page_not_found").value,false, 400);
    else
    {   
        initLandingComm(fromHyst);//[BV] LANDING MOBILE
    }
}

function initLandingComm(fromHyst)//[BV] LANDING MOBILE
{   
    // 18/01/2010 [FP]
    var myHash = window.location.hash.replace("#", "");
    var bif_id = "";
    if (myHash != "")
    {
        var qryStr = myHash.split("&");
        if (qryStr && qryStr.length > 0)
        {
            for (var i = 0; i < qryStr.length; i++)
            {
                ident = qryStr[i].split("=");
                if (ident.length > 1 && ident[0] == "bif_id")
                    bif_id = ident[1];
            }
        }
    }
    if (bif_id != "") // 18/01/2010 [FP]
    {
        var el = document.getElementById(bif_id);
        if (el)
        {
            var code = "";
            if (!document.all)
                code = el.getAttribute("onclick");
            else
            {
                code = String(el["onclick"]);
                // code è valorizzato con function anonimous { ... }
                var i = code.indexOf("{");
                if (i >= 0)
                    code = code.substring(i);
            }
            if (code != "")
            {
                code = code.replace("javascript:", "", 'g').replace("this.id", "'" + bif_id + "'", 'g');
                setTimeout(code, 1);
            }
        }
    }
    else
    {
        // 28/09/2009 [FP] - MoreInfo su Landing
        var itemCode = document.getElementById("item_landing").value;
        var imgItem = document.getElementById("img_landing").value;
        if (itemCode != "")
            LandingMoreInfo(itemCode, imgItem);
        else 
        {
            landingMoreInfoOpen = false;
            DisplayDivNone("div_moreinfo");
            DisplayDivNone(divInfoResult);//("moreinforesult");            
            // 20/07/2010 [FP] - Errore 71 - Con il back rimaneva opaco
            if (!isMobile)
                DisplayDivNone("containerMexBag");
        }
    }
    // 18/01/2010 [FP]
    pollHash();
}

function closeLandingMoreInfo()
{
    if (landingMoreInfoOpen) // vengo dall'history
    {
        url = "bif_id=";
        makeHistory(url);
    }
    // 30/09/2009 [FP] - Login Landing da Newsletter
    landingMoreInfoOpen = false;
    // 01/07/2010 [FP] - BIF: more info su Pop_Up
    if (!isMobile)
        DisplayDivNone("containerMexBag");
    DisplayDivNone("div_moreinfo");
    DisplayDivNone(divInfoResult);//("moreinforesult");
}

function LandingMoreInfo(itemCode, imgItem, id, htmTotalLook)
{
    // 30/09/2009 [FP] - Login Landing da Newsletter
    var path = document.getElementById("httpSiteUrl").value;
    landingMoreInfoOpen = true;
    if (is_ie6 && imgItem != "")
    {
        landing_img_preload = new Image();
        landing_img_preload.src = path + imgItem;
    }
    // 18/01/2010 [FP]
    var myScrollTop = 0;    
    if (id && id != "") // 18/01/2010 [FP]
    {
        if (!landingFromHyst) // non vengo dall'history
        {
            var url = window.location.hash.replace("#", "");
            url = "bif_id=" + id ;
            makeHistory(url);
        }
        //if (isMobile) // 01/07/2010 [FP]
        {
            var nextId = -1;
            var prevId = -1;
            var curArray = id.split("_");
            var curId = parseInt(curArray[curArray.length -1]);
            var curSuff = id.substring(0, id.length - curArray[curArray.length -1].length);
            var myArrayBif = arrRows[0][2]; 
            for (var i = 0; i < myArrayBif.length; i++)
            {
                if (myArrayBif[i][0] == curId)
                {
                    if (i > 0)
                    {
                        // 16/07/2010 [FP] - BIF Balmain con solo link ai prodotti
                        if (myArrayBif[(i-1)][3].indexOf("@CLK@")>-1 && myArrayBif[(i-1)][3].indexOf("viewProduct")==-1)
                            prevId = i - 1;
                    }
                    if (i < (myArrayBif.length - 1))
                    {
                        // 16/07/2010 [FP] - BIF Balmain con solo link ai prodotti
                        if (myArrayBif[(i+1)][3].indexOf("@CLK@")>-1 && myArrayBif[(i+1)][3].indexOf("viewProduct")==-1)
                            nextId = i + 1;
                    }
                    break;
                }
            }
            var el = document.getElementById("img_moreinfo_previous");
            if (el)
            {
                el.setAttribute("src", "immagini" + (isMobile ? "" : "/action_buttons") + "/left" + (prevId >= 0 ? "" : "_off") + ".png");  
                // 05/07/2010 [FP]
                if (!isMobile)
                {
                    setEventCode(el, "onmouseover", "javascript:" + (prevId >= 0 ? "this.src='immagini/action_buttons/left_on.png'" : "")+ ";");
                    setEventCode(el, "onmouseout", "javascript:" + (prevId >= 0 ? "this.src='immagini/action_buttons/left.png'" : "")+ ";");
                }
            }
            var el = document.getElementById("a_moreinfo_previous"); 
            if (el)
            {
                var clkfunc = "javascript:;";
                if (prevId >= 0)
                    clkfunc = myArrayBif[prevId][3].replace("@CLK@", "javascript:").replace("@/CLK@", ";").replace("this.id", "'" + curSuff + myArrayBif[prevId][0] +"'");
                setEventCode(el, "onclick", clkfunc);
            } 
            var el = document.getElementById("img_moreinfo_next");
            if (el)
            {
                el.setAttribute("src", "immagini" + (isMobile ? "" : "/action_buttons") + "/right" + (nextId >= 0 ? "" : "_off") + ".png");  
                // 05/07/2010 [FP]
                if (!isMobile)
                {
                    setEventCode(el, "onmouseover", "javascript:" + (nextId >= 0 ? "this.src='immagini/action_buttons/right_on.png'" : "")+ ";");
                    setEventCode(el, "onmouseout", "javascript:" + (nextId >= 0 ? "this.src='immagini/action_buttons/right.png'" : "")+ ";");
                }
            }
            var el = document.getElementById("a_moreinfo_next"); 
            if (el)
            {
                var clkfunc = "javascript:;";
                if (nextId >= 0)
                    clkfunc = myArrayBif[nextId][3].replace("@CLK@", "javascript:").replace("@/CLK@", ";").replace("this.id", "'" + curSuff + myArrayBif[nextId][0] +"'");
                setEventCode(el, "onclick", clkfunc);
            } 
        }  
    }
    // 01/07/2010 [FP] - BIF: more info su Pop_Up
    if (!isMobile)
    {
        DisplayDivBlock("containerMexBag");
        DisplayDivBlock("div_moreinfo");
        var left=document.getElementById("allContainer").offsetLeft;
        // 16/07/2010 [FP] - non imposta lo ScrollTop se non sul Mobile
        //var top = document.getElementById("mydata_div").offsetTop;
        var top = document.getElementById("allContainer").offsetTop+getScrollTop();
        var el=document.getElementById("div_moreinfo");
        el.style.left=(left+15)+"px";
        el.style.top= (top+100)+"px";
        // 16/07/2010 [FP] - non imposta lo ScrollTop se non sul Mobile
        // myScrollTop = top;
        el=document.getElementById("containerMexBag");
        el.style.height=GetWindowHeight()+"px";
        el.style.width=document.documentElement.clientWidth+"px";
        setLayerOpacity(el);
        el=document.getElementById("lblLookmoreInfo");
        el.innerHTML = capitalizeTxt(itemCode);
    }
    else
        DisplayDivBlock("div_moreinfo");
    DisplayDivNone(divInfoResult);//("moreinforesult");
    if (isMobile) // 23/06/2010 [FP]
        DisplayDiv("div_moreinfo_title", !(htmTotalLook && htmTotalLook != ""));
    // 01/07/2010 [FP] - BIF: more info su Pop_Up
//    var el = document.getElementById((isMobile ? "div_imgmoreinfo": "lblMoreInfoTitle"));
//    myScrollTop = getOffsetTop(el); // 18/01/2010 [FP]
//    if (isMobile)
//        el = document.getElementById("lblMoreInfoTitle");
    if (isMobile)
    {
        var el = document.getElementById("div_imgmoreinfo");
        myScrollTop = getOffsetTop(el); 
    }
    var el = document.getElementById("lblMoreInfoTitle");

    if (htmTotalLook && htmTotalLook != "")
    {
        DisplayDivNone("div_infomoreinfo");
        DisplayDivBlock("div_totallookmoreinfo");
        el.innerHTML = STR_LANDING_TOTALLOOK;
        // 01/07/2010 [FP] - BIF: more info su Pop_Up
        // 22/01/2010 [FP]
        //el= document.getElementById("div_imgmoreinfo");
        //if (el && !isMobile)//[BV] LANDING MOBILE
        //    el.style.paddingTop="28px";
    }
    else
    {
        if (isMobile)//[BV] LANDING MOBILE
        {
            DisplayDivNone("div_totallookmoreinfo");
            el.innerHTML = STR_LANDING_MOREINFO.replace(":","");
            setEventCode(el, "onclick", "javascript:myMail("+fromMailMoreInfoBif+",'"+STR_LANDING_MOREINFO+"','"+STR_ITEMCODE_MOREINFO+"','"+itemCode+"');");
        }
        else
        {
            DisplayDivBlock("div_infomoreinfo");
            DisplayDivNone("div_totallookmoreinfo");
            if (isLoggedIn!=0)  // 28/09/2009 [FP] - MoreInfo in Landing
            {
                landingFn = fn;
                landingLn = ln;
                landingEmail = uId;
            }
            el.innerHTML = STR_LANDING_MOREINFO;

            el = document.getElementById("lblFirstName");
            el.innerHTML = STR_FN_MOREINFO;
            el = document.getElementById("lblLastName");
            el.innerHTML = STR_LN_MOREINFO;
            el = document.getElementById("lblEmail");
            el.innerHTML = STR_EMAIL_MOREINFO;
            el = document.getElementById("lblItemCode");
            el.innerHTML = STR_ITEMCODE_MOREINFO.toUpperCase();
            el = document.getElementById("lblMoreInfo");
            el.innerHTML = STR_QUEST_MOREINFO;
            if (!isMobile)//[BV] LANDING MOBILE
                myImgPath = document.getElementById("txtActionImgPath").value 
                    +document.getElementById("txtLangDir").value;
            el = document.getElementById("img_submitMoreInfo");
            el.setAttribute("src", myImgPath + "send.gif");
            setEventCode(el, "onmouseover","javascript:this.src = '"+myImgPath+"send_on.gif';");
            setEventCode(el, "onmouseout","javascript:this.src = '"+myImgPath+"send.gif';");
            el = document.getElementById("img_closeMoreInfo");
            if (el) // 20/01/2010 [FP] - l'immagine è stata tolta dalla pagina
            {
                el.setAttribute("src", myImgPath + "cancel.gif");
                setEventCode(el, "onmouseover","javascript:this.src = '"+myImgPath+"cancel_on.gif';");
                setEventCode(el, "onmouseout","javascript:this.src = '"+myImgPath+"cancel.gif';");
            }

            el = document.getElementById("txtFirstName");
            el.value = landingFn;
            el = document.getElementById("txtLastName");
            el.value = landingLn;
            el = document.getElementById("txtEmail");
            el.value = landingEmail;
            el= document.getElementById("txtItemCode");
            el.value = itemCode;
            el = document.getElementById("lndNeedInfoMex");
            el.innerHTML = "&nbsp;&nbsp;";
            el = document.getElementById("lndQuestion");
            el.value = "";
            // 01/07/2010 [FP] - BIF: more info su Pop_Up
            // 22/01/2010 [FP]
            //el= document.getElementById("div_imgmoreinfo");
            //if (el)
            //    el.style.paddingTop="0px";
        }
    }
    if (imgItem != "")
    {
        if (is_ie6)
            setTimeout("landing_loadimg_ie6();", 10);   // 19/01/2010 [FP] - necessario per far terminare il preload dell'immagine
        else
        {
            el = document.getElementById("img_moreinfo");
            el.setAttribute("src", path + imgItem);
            DisplayDivBlock("div_imgmoreinfo");
        }
    }
    else
        DisplayDivNone("div_imgmoreinfo");
    if (isMobile) // 16/07/2010 [FP] - non imposta lo ScrollTop se non sul Mobile
        setScrollTop(myScrollTop); // 18/01/2010 [FP]
    landingFromHyst = false;    // reset variabile
    if (htmTotalLook && htmTotalLook != "")
    {
        // 16/07/2010 [FP] - Azzera il div del Total Look
        var el = document.getElementById("div_totallookmoreinfo");
        el.innerHTML = "";
        var url = "getData.aspx|CallType=IncludeHtm&Variabile=landing_htm&Include=" + htmTotalLook;
        //if (isMobile) url=url.replace("landing/","landing/mobile/");
        makeRequest(aggiustaLink(url,false), "IncludeHtmLanding");
    }
}

function ResponseIncludeHtmLanding(responseText)
{
    var el = document.getElementById("div_totallookmoreinfo");
    el.innerHTML = landing_htm;
    if (isMobile)   // 23/06/2010 [FP]
    {
        var el = document.getElementById("div_imgmoreinfo");
        var myScrollTop = getOffsetTop(el) - 8; 
        setScrollTop(myScrollTop);
    }
    else if (landing_htm.indexOf("row_moreinfo_total_look")==-1) // 01/07/2010 [FP] - BIF: more info su Pop_Up
    {
        var myArrayBif = arrRows[0][2]; 
        var nomeStilista = replaceAll(replaceAll(myArrayBif[myArrayBif.length -1][4],"</br>", ""),"</BR>", "");
        el=document.getElementById("lblLookmoreInfo");
        el.innerHTML = nomeStilista;
    }
}


function landing_loadimg_ie6()
{
    var el = document.getElementById("img_moreinfo");
    el.src = landing_img_preload.src;
    DisplayDivBlock("div_imgmoreinfo");
}

function submitLandingMoreInfo()
{
    var mx = document.getElementById("lndNeedInfoMex");
    var myEmail = document.getElementById("txtEmail").value;
    var myFn = document.getElementById("txtFirstName").value;
    var myLn = document.getElementById("txtLastName").value;
    var myItemCode = document.getElementById("txtItemCode").value;
    var myQuest = document.getElementById("lndQuestion").value;
    var pageLanding = document.getElementById("page_landing").value ;
    
    // 30/09/2009 [FP] - Login Landing da Newsletter
    landingMoreInfoOpen = false;
    

    if (!isEmail(myEmail))
    {
        setAInnerText(mx,MEX_INVALID_EMAIL);
        return;    
    }
    else if (myQuest=="")
    {
        setAInnerText(mx,STR_INSERT_QUEST);
        return;
    }

    // 18/01/2010 [FP]
    var objParam = 
        {
            pagina: pageLanding
            , product: myItemCode
        };
    add2Log(LOG_PAGE_BIF, LOG_ACTION_MOREINFO, objParam);

    landingFn = myFn;
    landingLn = myLn;
    landingEmail = myEmail;

    if (myFn!="")
        myFn=encodeURIComponent(myFn);
    if (myLn!="")
        myLn=encodeURIComponent(myLn);
    if (pageLanding!="")
        pageLanding=encodeURIComponent(pageLanding);
    myQuest= replaceSpecialChars(myQuest);
    var url = "getData.aspx|CallType=ProductInfo&email="+myEmail
        +"&fn=" + myFn
        +"&ln=" + myLn
        +"&des=" + pageLanding
        +"&itemLanding=" + myItemCode
        +"&body=" + myQuest;
    makeRequest(url, "MoreInfoLanding");
}

function ResponseLandingMoreInfo(responseText)
{
    // 01/07/2010 [FP] - BIF: more info su Pop_Up
    DisplayDivBlock(divInfoResult);
    if (!isMobile)
    {
        // ContainerMexBag è già visualizzato
        var left=document.getElementById("allContainer").offsetLeft;
        var top = document.getElementById("mydata_div").offsetTop;
        var el=document.getElementById("div_moreinforesult");
        el.style.left=(left+200)+"px";
        el.style.top= (top+200)+"px";
    }
    DisplayDivNone("div_moreinfo");
    el=document.getElementById("moreinforesult");
    if (errSendMex=="")
        el.innerHTML = "<br/>" + "<br/>" + "<br/>" + "<br/>" + "<br/>" + STR_LBL_SENT_MOREINFO + "<br/>" + STR_LBL_MOREINFO;
    else
        el.innerHTML = errSendMex;
    // 01/07/2010 [FP] - BIF: more info su Pop_Up
    if (!isMobile)
        setTimeout(closeMoreInfoResult, 2000);

}

// 01/07/2010 [FP] - BIF: more info su Pop_Up
function closeMoreInfoResult()
{
     DisplayDivBlock("div_moreinfo");
     DisplayDivNone("div_moreinforesult");  // E' chiamata solo da !isMobile
     document.getElementById("lndQuestion").value = "";
}

// 30/09/2009 [FP] - Login Landing da Newsletter
function LandingUserReg()
{
    if (landingMoreInfoOpen && landingEmail=="" && isLoggedIn!=0)
    {
        landingFn = fn;
        landingLn = ln;
        landingEmail = uId;
        el = document.getElementById("txtFirstName");
        el.value = landingFn;
        el = document.getElementById("txtLastName");
        el.value = landingLn;
        el = document.getElementById("txtEmail");
        el.value = landingEmail;
    }
}


//[BV] 26/01/2010
function Add2LogLandingItem(myItemCode, season, gender, myHttp)
{
    var pageLanding = document.getElementById("page_landing").value ;
    var objParam = 
        {
            pagina: pageLanding
            , product: myItemCode
        };
    add2Log(LOG_PAGE_BIF, LOG_ACTION_SCHEDAPROD, objParam);
    if (season !== undefined)   // 25/06/2010 [FP] - Back da Product di BuyItFirst
        viewProduct(myItemCode.substring(4), season, gender, myItemCode.substring(0,3), myHttp);
    else
        return true;
}

function viewProduct(prodid, season, gender, seasonID, myHttp)
{
    var url = "";
    if (myHttp !== undefined)   // 25/06/2010 [FP] - Back da Product di BuyItFirst
        url = myHttp;
    else
        url = document.getElementById("httpSiteUrl").value;
    if (isMobile) //[BV] LANDING MOBILE
        url += "mobile/index.aspx#getData.aspx|CallType=ProdMobile&prodId="+prodid+
            "&season="+season+"&gender="+gender+"&seasProdID="+seasonID;
    else
        url += "index.aspx#getData.aspx|CallType=Product&prodId="+prodid+
            "&season="+season+"&gender="+gender+"&seasProdID="+seasonID;
    window.location.href =url;
    if (window.event)
        window.event.returnValue=false;
}

