﻿var AddressSelector = [];
var LastSelectedAddress = [];

function SetViewMode(comboBox)
{
    document.getElementById(comboBox.ClientID + "Container").style["display"] = "block";
    document.getElementById(comboBox.ClientID + "State").value = "read";
    AddressSelector[comboBox.ClientID].AddressForm.style["display"] = "none";
    AddressSelector[comboBox.ClientID].SelectedAddressContainer.style["display"] = "block";
    AddressSelector[comboBox.ClientID].SelectedAddress.innerHTML = document.getElementById(comboBox.SelectedItem.ClientID).innerHTML;
    LastSelectedAddress[comboBox.ClientID] = comboBox.SelectedItem;
}




function SelectedAddressChanged(item)
{
    // récupère les DIV et INPUT
    if (item == null)
    {
        return;
    }
    var data = AddressSelector[item.ComboBox.ClientID];
    if (null == data)
    {
        return;
    }

    var ddlState = window[data.State];
    var ddlCountry = window[data.Country];
    
    if ("Add" == item.Value)
    {
        document.getElementById(item.ComboBox.ClientID + "Container").style["display"] = "none";
        document.getElementById(item.ComboBox.ClientID + "State").value = "edit";
        
        // ajoute une adresse, affiche un formulaire vide
        data.SelectedAddressContainer.style["display"] = "none";
        data.SelectedAddress.innerText = "";
        
        data.FirstName.value = "";
        data.LastName.value = "";
        data.Line1.value = "";
        data.Line2.value = "";
        data.City.value = "";
        //ddlState.Items[0].Select();
        data.ZipCode.value = "";
        //ddlCountry.Items[0].Select();
        if (data.Phone != null)
            data.Phone.value = "";
        if (data.Email != null)
            data.Email.value = "";
        if (data.Email2 != null)       
            data.Email2.value = "";
        
        data.AddressForm.style["display"] = "block";
        
        
        //data.Title[0].focus();
    }
    else
    {
        
        document.getElementById(item.ComboBox.ClientID + "Container").style["display"] = "block";
        document.getElementById(item.ComboBox.ClientID + "State").value = "read";
        
        data.AddressForm.style["display"] = "none";

        for (var i = 0 ; i < data.Title.length ; ++i)
        {
            if (data.Title[i].value == item.Attributes.Title)
            {
                data.Title[i].checked = true;
                break;
            }
        }        

        data.FirstName.value = item.Attributes.FirstName;
        data.LastName.value = item.Attributes.LastName;
        data.Line1.value = item.Attributes.AddrLine1;
        data.Line2.value = item.Attributes.AddrLine2;
        data.City.value = item.Attributes.City;
        data.ZipCode.value = item.Attributes.ZipCode;
        if (data.Phone != null)
            data.Phone.value = item.Attributes.PhoneNumber;
        if (data.Email != null)
            data.Email.value = item.Attributes.Email;
        if (data.Email2 != null)
            data.Email2.value = item.Attributes.Email;
        
        
        
        ////////
//        var a;
//        a = true;
//        if (a)
//        {
//        window["ctl00_cphContent_billingAddress_ddlState"] = new RadComboBox("ddlState","ctl00_cphContent_billingAddress_ddlState",true);
//        window["ctl00_cphContent_billingAddress_ddlState"].Initialize({"LoadOnDemandUrl":"/FO_USA/Checkout/Addresses.aspx?rcbID=ctl00_cphContent_billingAddress_ddlState%26rcbServerID=ddlState","PostBackReference":"__doPostBack('ctl00$cphContent$billingAddress$ddlState','TextChange')","MarkFirstMatch":true,"Skin":"default","Enabled":true,"ScrollDownImage":"/FO_USA/skins/default/ScrollDown.gif","ScrollDownImageDisabled":"/FO_USA/skins/default/ScrollDownDisabled.gif"},[{"Text":"Choose state...","ClientID":"ctl00_cphContent_billingAddress_ddlState_c0"},{"Text":"Alabama","Value":"AL","ClientID":"ctl00_cphContent_billingAddress_ddlState_c1"},{"Text":"Alaska","Value":"AK","ClientID":"ctl00_cphContent_billingAddress_ddlState_c2"},{"Text":"Arizona","Value":"AZ","ClientID":"ctl00_cphContent_billingAddress_ddlState_c3"},{"Text":"Arkansas","Value":"AR","ClientID":"ctl00_cphContent_billingAddress_ddlState_c4"},{"Text":"California","Value":"CA","ClientID":"ctl00_cphContent_billingAddress_ddlState_c5"},{"Text":"Colorado","Value":"CO","ClientID":"ctl00_cphContent_billingAddress_ddlState_c6"},{"Text":"Connecticut","Value":"CT","ClientID":"ctl00_cphContent_billingAddress_ddlState_c7"},{"Text":"Washington D.C.","Value":"DC","ClientID":"ctl00_cphContent_billingAddress_ddlState_c8"},{"Text":"Delaware","Value":"DE","ClientID":"ctl00_cphContent_billingAddress_ddlState_c9"},{"Text":"Florida","Value":"FL","ClientID":"ctl00_cphContent_billingAddress_ddlState_c10"},{"Text":"Georgia","Value":"GA","ClientID":"ctl00_cphContent_billingAddress_ddlState_c11"},{"Text":"Hawaii","Value":"HI","ClientID":"ctl00_cphContent_billingAddress_ddlState_c12"},{"Text":"Idaho","Value":"ID","ClientID":"ctl00_cphContent_billingAddress_ddlState_c13"},{"Text":"Illinois","Value":"IL","ClientID":"ctl00_cphContent_billingAddress_ddlState_c14"},{"Text":"Indiana","Value":"IN","ClientID":"ctl00_cphContent_billingAddress_ddlState_c15"},{"Text":"Iowa","Value":"IA","ClientID":"ctl00_cphContent_billingAddress_ddlState_c16"},{"Text":"Kansas","Value":"KS","ClientID":"ctl00_cphContent_billingAddress_ddlState_c17"},{"Text":"Kentucky","Value":"KY","ClientID":"ctl00_cphContent_billingAddress_ddlState_c18"},{"Text":"Louisiana","Value":"LA","ClientID":"ctl00_cphContent_billingAddress_ddlState_c19"},{"Text":"Maine","Value":"ME","ClientID":"ctl00_cphContent_billingAddress_ddlState_c20"},{"Text":"Maryland","Value":"MD","ClientID":"ctl00_cphContent_billingAddress_ddlState_c21"},{"Text":"Massachusetts","Value":"MA","ClientID":"ctl00_cphContent_billingAddress_ddlState_c22"},{"Text":"Michigan","Value":"MI","ClientID":"ctl00_cphContent_billingAddress_ddlState_c23"},{"Text":"Minnesota","Value":"MN","ClientID":"ctl00_cphContent_billingAddress_ddlState_c24"},{"Text":"Mississippi","Value":"MS","ClientID":"ctl00_cphContent_billingAddress_ddlState_c25"},{"Text":"Missouri","Value":"MO","ClientID":"ctl00_cphContent_billingAddress_ddlState_c26"},{"Text":"Montana","Value":"MT","ClientID":"ctl00_cphContent_billingAddress_ddlState_c27"},{"Text":"Nebraska","Value":"NE","ClientID":"ctl00_cphContent_billingAddress_ddlState_c28"},{"Text":"Nevada","Value":"NV","ClientID":"ctl00_cphContent_billingAddress_ddlState_c29"},{"Text":"New Hampshire","Value":"NH","ClientID":"ctl00_cphContent_billingAddress_ddlState_c30"},{"Text":"New Jersey","Value":"NJ","ClientID":"ctl00_cphContent_billingAddress_ddlState_c31"},{"Text":"New Mexico","Value":"NM","ClientID":"ctl00_cphContent_billingAddress_ddlState_c32"},{"Text":"New York","Value":"NY","ClientID":"ctl00_cphContent_billingAddress_ddlState_c33"},{"Text":"North Carolina","Value":"NC","ClientID":"ctl00_cphContent_billingAddress_ddlState_c34"},{"Text":"North Dakota","Value":"ND","ClientID":"ctl00_cphContent_billingAddress_ddlState_c35"},{"Text":"Ohio","Value":"OH","ClientID":"ctl00_cphContent_billingAddress_ddlState_c36"},{"Text":"Oklahoma","Value":"OK","ClientID":"ctl00_cphContent_billingAddress_ddlState_c37"},{"Text":"Oregon","Value":"OR","ClientID":"ctl00_cphContent_billingAddress_ddlState_c38"},{"Text":"Pennsylvania","Value":"PA","ClientID":"ctl00_cphContent_billingAddress_ddlState_c39"},{"Text":"Puerto Rico","Value":"PR","ClientID":"ctl00_cphContent_billingAddress_ddlState_c40"},{"Text":"Rhode Island","Value":"RI","ClientID":"ctl00_cphContent_billingAddress_ddlState_c41"},{"Text":"South Carolina","Value":"SC","ClientID":"ctl00_cphContent_billingAddress_ddlState_c42"},{"Text":"South Dakota","Value":"SD","ClientID":"ctl00_cphContent_billingAddress_ddlState_c43"},{"Text":"Tennessee","Value":"TN","ClientID":"ctl00_cphContent_billingAddress_ddlState_c44"},{"Text":"Texas","Value":"TX","ClientID":"ctl00_cphContent_billingAddress_ddlState_c45"},{"Text":"Utah","Value":"UT","ClientID":"ctl00_cphContent_billingAddress_ddlState_c46"},{"Text":"Vermont","Value":"VT","ClientID":"ctl00_cphContent_billingAddress_ddlState_c47"},{"Text":"Virginia","Value":"VA","ClientID":"ctl00_cphContent_billingAddress_ddlState_c48"},{"Text":"Washington","Value":"WA","ClientID":"ctl00_cphContent_billingAddress_ddlState_c49"},{"Text":"West Virginia","Value":"WV","ClientID":"ctl00_cphContent_billingAddress_ddlState_c50"},{"Text":"Wisconsin","Value":"WI","ClientID":"ctl00_cphContent_billingAddress_ddlState_c51"},{"Text":"Wyoming","Value":"WY","ClientID":"ctl00_cphContent_billingAddress_ddlState_c52"}]);
//        }
        /////////
        
        
//        data.State.SetValue(item.Attributes.StateCode); 
//        data.Country.SetValue(item.Attributes.CountryCode);
        
        if (document.getElementById(data.State) != null)
        {
            ddlState.SetValue(item.Attributes.StateCode); 
            ddlState.SetText(item.Attributes.StateName);
        }
        
//        var ddlItem = ddlState.FindItemByValue(item.Attributes.StateCode);
//        ddlState.ClearSelection();
//        if (null != ddlItem)
//        {
//            ddlState.Items[ddlItem.Index].Select();
//            ddlItem.Select();
//        }
//        else
//            ddlState.Items[0].Select();
//        
        ddlCountry.SetValue(item.Attributes.CountryCode);
        ddlCountry.SetText(item.Attributes.CountryName);
//        
//        ddlItem = ddlCountry.FindItemByValue(item.Attributes.CountryCode);
//        ddlCountry.ClearSelection();
//        if (null != ddlItem)
//            ddlItem.Select();
//        else
//            ddlCountry.Items[0].Select();
    
        data.SelectedAddress.innerHTML = document.getElementById(item.ClientID).innerHTML;
        data.SelectedAddressContainer.style["display"] = "block";
        
        LastSelectedAddress[item.ComboBox.ClientID] = item;
    }
}

function EditAddress(comboBox)
{
    var data = AddressSelector[comboBox.ClientID];
    if (null == data)
    {
        return;
    }
    
    data.SelectedAddressContainer.style["display"] = "none";
    data.AddressForm.style["display"] = "block";
    //data.Title[0].focus();
    
    LastSelectedAddress[comboBox.ClientID] = comboBox.SelectedItem;

    document.getElementById(comboBox.ClientID + "Container").style["display"] = "none";
    document.getElementById(comboBox.ClientID + "State").value = "edit";
    
    //Force resize combobox
//        var newWidth = data.City.clientWidth;
//        newWidth = newWidth - 30;
//        if (newWidth > 0)
//        {
//            if (document.getElementById(data.State) != null)
//                document.getElementById(data.State + "_Input").style.width = newWidth + 'px';
//            document.getElementById(data.Country + "_Input").style.width = newWidth + 'px';
//        }

        
}

function UndoAddressChanges(comboBox)
{
    if ((comboBox.SelectedIndex == 1) && (comboBox.Items.length > 1))
    {
        comboBox.ClearSelection();
        LastSelectedAddress[comboBox.ClientID].Select();
        SelectedAddressChanged(LastSelectedAddress[comboBox.ClientID]);
    }
    else
    {
        SelectedAddressChanged(comboBox.SelectedItem);
    }
}