﻿var country;
var arrCurrency;
var g_sTempContinent;
var g_sTempShipCountry;
var g_sTempShipCountryDescr;
var g_sPrevPage;

//[BV] 26/05/2009 - Se viene cambiata la lingua siamo costretti a ricaricare la pagina (css diversi!!!)
//                  viene memorizzato l'url di provenienza per poter tornare alla pagina precedente a quella di impostazione shipto
var urlFromToCurrAndShip="";

function getCurrAndShip(fromHyst,fromBasket, fromMyAccount, fromSurvey, fromProduct)
{
    //  07/10/2009 [FP] - add2Log (aggiunto fromProduct)
    add2Log((fromProduct ? LOG_PAGE_PRODUCT : (fromHyst ? LOG_PAGE_HISTORY : LOG_PAGE_HEADER)), LOG_ACTION_SELSHIPTO);
    if (window.location.href.indexOf("CallType=CurrAndShip")==-1 && !fromHyst)
    {
        urlFromToCurrAndShip=window.location.href;
    }
    // 20/10/2009 [FP] - initMex
//    var suff=""
//    if (inMyArea) suff="../";
    if (fromBasket)
        cngCurrAndShipFromBasket=fromBasket;
    else
        cngCurrAndShipFromBasket=false;
    if (fromMyAccount)
        cngCurrAndShipFromMyAccount=fromMyAccount;
    else
        cngCurrAndShipFromMyAccount=false;
    if (fromSurvey)     // 28/07/2009 [FP] - Survey
        cngCurrAndShipFromSurvey=fromSurvey;
    else
        cngCurrAndShipFromSurvey=false;
    
    // 20/10/2009 [FP] - initMex
    //var el = document.getElementById("img_bnt_info");
    //el.src = suff+"immagini/action_buttons/" + document.getElementById("txtLangDir").value + "ok.gif";
    
    g_sPrevPage = window.location.href;
    
    if (fromHyst)
    {
        url=expectedHash.replace("#", "") + "&fromHyst="+fromHyst;
    }
    else 
    {
        url="myarea/getLogIn.aspx|CallType=CurrAndShip&action=vis" +
                "&fromBasket="+cngCurrAndShipFromBasket+
                "&fromMyAccount="+cngCurrAndShipFromMyAccount+
                "&fromSurvey="+cngCurrAndShipFromSurvey+ // 28/07/2009 [FP] - Survey
                "&season="+curSeason+"&gender="+curGender;
        if (!fromHyst) makeHistory(url);
    }
    
    var el = document.getElementById("lista_cty_text");
    el.innerHTML = STR_TXT_SHIPANDCURR;
    

    searchLine="";
    searchCateg="";
    searchDes="";
    searchColor="";
    searchSize="";
    searchPrice="";
    searchDiscount="";
    
    DisplayOnLoading(1);
    makeRequest(url, "LoadCurrAndShip");
}

function changeContinent(continent)
{
    overContinent("");
    setCountryDescrSelected(STR_SHIPDEFAULT);
    g_sTempContinent = continent;
    var curAlpha= "";
    //var nAlphaCars = parseInt(alphaCars);
    var onClickCode = "javascript:selShipCountry(this);";
    var el = document.getElementById("lista_cty_col3");
    el.innerHTML = "";
    el = document.getElementById("lista_cty_col2");
    el.innerHTML = "";
    el = document.getElementById("lista_cty_col1");
    el.innerHTML = "";
    if (g_sTempContinent != "")
    {
        DisplayDivBlock("lblContinentSelectedTitle");
        DisplayDivBlock("lblContinentSelected");
        DisplayDivBlock("lblCountriesTitle");
        var elTop = document.getElementById("lblContinentSelectedTitle");
        el = document.getElementById("imgContinentSelected");
        el.src = document.getElementById("txtShipAndCurrencyImgPath").value + document.getElementById("txtLangDir").value + "continent_" + g_sTempContinent + ".gif";
        el = document.getElementById("lista_cty_col1");
        var j = 1;
        var aNewCols=getContinentNewCols(g_sTempContinent);
        for (var i = 1; i < country.length; i++)
        { 
            if (country[i][cty_continent] == g_sTempContinent && country[i][cty_active] != "0")
            {
                 if (curAlpha!=country[i][cty_alphagrp])
                 {
                    curAlpha=country[i][cty_alphagrp];
                    if (j>1)
                    {
                        br = document.createElement("br");
                        el.appendChild(br);
                    }
                    if (curAlpha>=aNewCols[0] && curAlpha<aNewCols[1])
                        el = document.getElementById("lista_cty_col2");
                    else if (curAlpha>=aNewCols[1])
                        el = document.getElementById("lista_cty_col3");
                    var img = document.createElement("IMG");
                    img.setAttribute("alt", "");
                    img.className="alpha_des";
                    img.src = "immagini/a-z/"+country[i][cty_alphagrp]+".gif"
                    el.appendChild(img);
                    br = document.createElement("br");
                    el.appendChild(br);
                 }
                 var a = document.createElement("a");
                 a.setAttribute("id", "cty"+country[i][cty_id]);
                 a.setAttribute("href", "javascript:;");
                 setAInnerText(a, country[i][cty_descr]);
                 setEventCode(a,"onclick",onClickCode );
                 el.appendChild(a);
                 br = document.createElement("br");
                 br.className="menu";
                 el.appendChild(br);
                 j += 1;
            }
        }
        setScrollTop(getOffsetTop(elTop)-10);
    }
    else
    {
        DisplayDivNone("lblContinentSelectedTitle");
        DisplayDivNone("lblContinentSelected");
        DisplayDivNone("lblCountriesTitle");
    }
    if (window.event)
        window.event.returnValue=false;
}

//Funzione di callback della chiamata ajax per lista paesi di spedizione e valute
function LoadCurrAndShip(responseText)
{
	// 26/03/2009 [FP] - resetAll(fromHyst, 1=noResetLine, 2=noResetDesigner, 3=noResetMenuViewedItems, 4=noResetMenuSearch, 5=noResetNewArrivals, 6=noResetListaID, 7=noResetAddThisUrl, 8=noResetStyle, 9=noResetDiv)
    resetAll(fromHyst, false, false, false, false, false, false, false, false, false)
    DisplayDivBlock("lista_country");
       
    changeContinent("");
    if (myShipCountryDescr == "")
        myShipCountryDescr = STR_SHIPDEFAULT;
    g_sTempShipCountryDescr = myShipCountryDescr;
    g_sTempShipCountry = myShipCountry;

    var selIdx=0;
    var el = document.getElementById("sel_currency"); 
    el.options.length = 0;
    // 15/09/2009 [FP] - View Price
    var el = document.getElementById("view_currency"); 
    el.options.length = 0;
    el = document.getElementById("cmbSelectLanguage");
    el.options.length = 0;
    for (var i = 0; i < g_aLanguage.length; i++)
    {
        var op = document.createElement("option");
        op.setAttribute("value", g_aLanguage[i][lang_code]);
        setAInnerText(op, g_aLanguage[i][lang_descr]);
        el.appendChild(op); 
    }
    el.selectedIndex = 0;
    DisplayOnLoading(0);
}

function selShipCountry(node)
{        
    var idCtyNew = node.id.replace("cty","");
    var el = document.getElementById("pnlCountrySelected");
    g_sTempShipCountry = idCtyNew;
    g_sTempShipCountryDescr = getAInnerText(node);
    setCountryDescrSelected(g_sTempShipCountryDescr);
    var aCty=GetCountry(idCtyNew);
    // 22/06/2009 [FP] - Lingua di Default della Country
    var ctylang = aCty[cty_language];
    el = document.getElementById("sel_currency"); 
    el.options.length = 0;
    var idx = -1;
    var selIdx = 0;
    for (var j = 0; j < arrCurrency.length; j ++)
    {
        if (arrCurrency[j][cry_id]==aCty[cty_CurrencyID] || arrCurrency[j][cry_flgfatt]!= 0)
        {
            idx ++;
            el.options[idx] = new Option(arrCurrency[j][cry_descr], arrCurrency[j][cry_id]);
            if (arrCurrency[j][cry_id]==aCty[cty_CurrencyID])
                selIdx=idx;
        }
    }
    el.options[selIdx].selected=true;
    
    // 15/09/2009 [FP] - View Price
    el = document.getElementById("view_currency"); 
    el.options.length = 0;
    idx = -1;
    selIdx = 0;
    
    for (var j = 0; j < arrCurrency.length; j ++)
    {
        if (arrCurrency[j][cry_id]==aCty[cty_CurrencyIDShip] || arrCurrency[j][cry_flgfatt]!= 0)
        {
            idx ++;
            el.options[idx] = new Option(arrCurrency[j][cry_descr], arrCurrency[j][cry_id]);
            if (arrCurrency[j][cry_id]==aCty[cty_CurrencyIDShip])
                selIdx=idx;            
        }
    }
    el.options[selIdx].selected=true;
    
    // 26/03/2009 [BV]: per l'italia seleziona la lingua italiana
    el = document.getElementById("cmbSelectLanguage"); 
    el.selectedIndex=0;
    if (ctylang!="")
    {
        for (var i = 0; i < g_aLanguage.length; i++)
        { 
            // 22/06/2009 [FP] - Lingua di Default della Country
            //if (g_sTempShipCountry=="IT" && g_aLanguage[i][lang_code]=="IT")
            if (g_aLanguage[i][lang_code] == ctylang)
            {
                el.options[i].selected=true;
                break;
            }
        }
    }    
}

function setCountryDescrSelected(country)
{
    if (country != STR_SHIPDEFAULT) {
        makeCountrySelector();
        var el = document.getElementById("descr_country_sel"); 
        setAInnerText(el, country);        
    }
}

function saveShipAndCurSelection()
{
    //NB:Il salvataggio di valuta/shipto viene eseguito nell'ambito della getLogin 
    //e la risposta gestita nella stessa funzione UserReg (logIn.js)
    myShipCountry = g_sTempShipCountry;
    var el = document.getElementById("sel_currency");
    fattCurrID = el[el.selectedIndex].value;
    // 15/09/2009 [FP] - View Price
    el = document.getElementById("view_currency");
    shipCurrID = el[el.selectedIndex].value;
    el = document.getElementById("cmbSelectLanguage");
    langId = el[el.selectedIndex].value;

    //  07/10/2009 [FP] - add2Log
    var objParam =
        {
            shipcountry: myShipCountry
            , fattcurrency: fattCurrID
            , viewcurrency: shipCurrID
        };
    add2Log(LOG_PAGE_SHIPCURRENCY, LOG_ACTION_CONFIRM, objParam);
    
    var url="myarea/getLogIn.aspx|CallType=CurrAndShip&action=save&myCountryId="+replaceDBChars(myShipCountry)+
        "&myCurrency="+fattCurrID+"&myLangId="+langId+
        "&myViewCurrency="+shipCurrID+   // 15/09/2009 [FP] - View Price
        "&fromBasket="+cngCurrAndShipFromBasket +
        "&fromMyAccount="+cngCurrAndShipFromMyAccount +
        "&fromSurvey="+cngCurrAndShipFromSurvey; // 28/07/2009 [FP] - Survey
    DisplayOnLoading(1);
    curLang=langId;//[BV] 02/09/2009 - Servere alla UserReg per capire se è cambiata lingua
    makeRequest(url, "SaveCurrAndShip");
}

//  07/10/2009 [FP] - add2Log
function add2LogCloseShipCurr()
{
    add2Log(LOG_PAGE_SHIPCURRENCY, LOG_ACTION_CANCEL);
}

function getContinentName(continent)
{
    switch (continent)
    {
        case "AM":
            return TXT_AM;
        case "AF":
            return TXT_AF;
        case "EU":
            return TXT_EU;
        case "AS":
            return TXT_AS;
        case "OC":
            return TXT_OC;
    }
    return "";
}

function getContinentNewCols(continent)
{
    var aNewCols=new Array();
    aNewCols[0]="F";
    aNewCols[1]="N";
    switch (continent)
    {
        case "AM":
            aNewCols[0]="E";
            aNewCols[1]="N";
            break;
        case "AF":
            aNewCols[0]="G";
            aNewCols[1]="N";
            break;
        case "EU":
            aNewCols[0]="G";
            aNewCols[1]="N";
            break;
        case "AS":
            aNewCols[0]="J";
            aNewCols[1]="Q";
            break;
        case "OC":
            aNewCols[0]="G";
            aNewCols[1]="P";
    }
    return aNewCols;
}

function overContinent(continent)
{
    var imgWorldmap = document.getElementById("imgWorldmap");
    if (continent == "")
        imgWorldmap.src = "immagini/ship_and_currency/worldmap.jpg";
    else
        imgWorldmap.src = "immagini/ship_and_currency/worldmap_" + continent + ".jpg";
    if (continent != "")
        showToolTip(getContinentName(continent),0,4);
        //Tip(getContinentName(continent), LEFT, true, BGCOLOR, 'white', FADEIN, 400);
    if (window.event)
        window.event.returnValue=false;
}


// 05/10/2009 [FP] - selectCountry
function selectCountry(continent, countryID)
{
    changeContinent(continent);
    var el = document.getElementById("cty" + countryID);
    if (el) selShipCountry(el);
}