﻿// JScript File

var AddressTitles = '';
var Countries = null;
var AddressIDs = new Array();
var TotalAds = 0;
var CurrentBillingID =-1;
var CurrentShippingID = -1;

function GetAllAddresses(tbl, sCell, bCell)
{
    sndReq('/XML/AddressFunctions.aspx?TargetTable=' + tbl + '&sCell=' + sCell + '&bCell=' + bCell);
}

function BuildAddress(Title, FirstName, Surname, Address1, Address2, Address3, Town, County, PostCode, CountryName, TelephoneNumber)
{
    var Add = Title + ' ' + FirstName + ' ' + Surname + '<br>';
    Add += Address1 + '<br>';
    if (Address2 != '') Add += Address2 + '<br>';
    if (Address3 != '') Add += Address3 + '<br>';
    if (Town != '') Add += Town + '<br>';
    if (County != '') Add += County + '<br>';
    if (PostCode != '') Add += PostCode + '<br>';
    Add += CountryName;
    if (TelephoneNumber != '') Add += '<br>' + TelephoneNumber;
    return Add; 
}

function BuildLinks(CustomerAddressID, Title , FirstName, Surname, Address1, Address2, Address3, Town, County, PostCode, CountryID, CountryName, TelephoneNumber, tblid, sCellid, bCellid)
{
    var IsDefaultShipping = '<br><img border="0" src="' + jsImagePath + 'Account/Address/toggle_delivery_on.gif">';
    var SetAsDefaultShipping = 'Set as Delivery Address';
    var IsDefaultBilling = '<br><img border="0" src="' + jsImagePath + 'Account/Address/toggle_billing_on.gif">';
    var SetAsDefaultBilling = 'Set as Billing Address';
    var links = '<br><a id="lnkAddressEdit' + CustomerAddressID + '" href="javascript:StartAddressEdit(' + CustomerAddressID + ', ' +
                    '\'' + Title + '\', ' +
                    '\'' + FirstName + '\', ' +
                    '\'' + Surname + '\', ' +
                    '\'' + Address1 + '\', ' +
                    '\'' + Address2 + '\', ' +
                    '\'' + Address3 + '\', ' +
                    '\'' + Town + '\', ' +
                    '\'' + County + '\', ' +
                    '\'' + PostCode + '\', ' +
                    '\'' + CountryID + '\', ' +
                    '\'' + CountryName + '\', ' +
                    '\'' + TelephoneNumber + '\', ' +
                    '\'' + tblid + '\', ' +
                    '\'' + sCellid + '\', ' +
                    '\'' + bCellid + '\')' +
                    '">Edit</a>';
        if (TotalAds > 1)
        {
            links += '&nbsp;&nbsp;<a href="javascript:DeleteAddress(' + CustomerAddressID + ', ' +
                    '\'' + tblid + '\', ' +
                    '\'' + sCellid + '\', ' +
                    '\'' + bCellid + '\'' +       
                    ')">Delete</a>'
        }
        else
            links += '&nbsp;&nbsp;Delete';
        links += '&nbsp;&nbsp;';
        
        var btns = '';
        if (CustomerAddressID != CurrentShippingID)
            links += '<a href="javascript:SetBasketAddress(' + CustomerAddressID + ',\'Shipping\', \'' + tblid + '\', \'' + sCellid + '\', \'' + bCellid + '\')">' + SetAsDefaultShipping + '</a>&nbsp;&nbsp;';
        else
            btns += IsDefaultShipping;
            
        btns += '&nbsp;&nbsp;';
        if (CustomerAddressID != CurrentBillingID)
            links += '<a href="javascript:SetBasketAddress(' + CustomerAddressID + ',\'Billing\', \'' + tblid + '\', \'' + sCellid + '\', \'' + bCellid + '\')">' + SetAsDefaultBilling + '</a>&nbsp;&nbsp;';
        else
            btns += IsDefaultBilling;
            
        if (btns != '') btns = '<span style="line-height:200%; valign:bottom">' + btns + '</span>'
        return '<br>' + links + '&nbsp;&nbsp;' + btns;
}

function LoadCustomerAddresses(doc)
{
    CurrentBillingID =-1;
    CurrentShippingID = -1;
    var xml = GetXMLObject(doc).childNodes[0]; 
    var tbl = document.getElementById(xml.childNodes[0].getAttribute('Target'))
    var sCell = document.getElementById(xml.childNodes[0].getAttribute('sCell'));
    var bCell = document.getElementById(xml.childNodes[0].getAttribute('bCell'));
    AddressTitles = xml.childNodes[0].getAttribute('Titles');
    Countries = xml.childNodes[1];
    
    tbl.style.width = '100%';
    while (tbl.rows.length>0)
        tbl.deleteRow(0);
        
    var Ads = xml.childNodes[0].getElementsByTagName('Address');
    TotalAds = Ads.length;
    for (i=0; i<Ads.length; i++)
    {
        
        var rw = tbl.insertRow(tbl.rows.length);
        var cl = rw.insertCell(0);
        var Title = getInnerText(Ads[i].getElementsByTagName('Title')[0]);
        var FirstName = getInnerText(Ads[i].getElementsByTagName('FirstName')[0]);
        var Surname = getInnerText(Ads[i].getElementsByTagName('Surname')[0]);
        var Address1 = getInnerText(Ads[i].getElementsByTagName('Address1')[0]);
        var Address2 = getInnerText(Ads[i].getElementsByTagName('Address2')[0]);
        var Address3 = getInnerText(Ads[i].getElementsByTagName('Address3')[0]);
        var Town = getInnerText(Ads[i].getElementsByTagName('Town')[0]);
        var County = getInnerText(Ads[i].getElementsByTagName('County')[0]);
        var PostCode = getInnerText(Ads[i].getElementsByTagName('PostCode')[0]);
        var CountryID = getInnerText(Ads[i].getElementsByTagName('CountryID')[0]);
        var CountryName = getInnerText(Ads[i].getElementsByTagName('CountryName')[0]);
        var TelephoneNumber = getInnerText(Ads[i].getElementsByTagName('TelephoneNumber')[0]);
        var CustomerAddressID = Ads[i].getAttribute('CustomerAddressID');
        var add = BuildAddress(Title, FirstName, Surname, Address1, Address2, Address3, Town, County, PostCode, CountryName, TelephoneNumber);
        if (Ads[i].getAttribute('CurrentShipping') == 'True') 
        {
            CurrentShippingID = CustomerAddressID;
            sCell.innerHTML = add;
        }  
        if (Ads[i].getAttribute('CurrentBilling') == 'True') 
        {
            CurrentBillingID = CustomerAddressID;
            bCell.innerHTML = add;
        }
        
        AddressIDs[AddressIDs.length] = CustomerAddressID;
        cl.setAttribute('id', 'clAddress' + CustomerAddressID)
            
        cl.style.width='100%';
        cl.style.paddingTop = '15px';
        cl.style.paddingBottom = '15px';
    
        cl.innerHTML = add + BuildLinks(CustomerAddressID, Title, FirstName, Surname, Address1, Address2, Address3, Town, County, PostCode, CountryID, CountryName, TelephoneNumber, tbl.id, sCell.id, bCell.id);
        if (i != Ads.length-1)
        {
            rw = tbl.insertRow(tbl.rows.length);
            cl = rw.insertCell(0);
            cl.style.width = '100%';
            cl.style.textAlign = 'center';
            cl.innerHTML = '<img style="width:320px; height:20px;" src="' + jsImagePath + 'Account/Address/seperator.gif" />';
        }
    }
    
    document.getElementById('clAddAddress').innerHTML = BuildEditAddressForm('-1', '', '', '', '', '', '', '', '', '', '-1', '', '', tbl.id, sCell.id, bCell.id);

}

function StartAddressEdit(CustomerAddressID, Title, FirstName, Surname, Address1, Address2, Address3, Town, County, PostCode, CountryID, CountryName, TelephoneNumber, tbl, sCell, bCell)
{
    for (i=0; i<AddressIDs.length; i++)
    {
        var lnk = document.getElementById('lnkCancelEdit' + AddressIDs[i]);
        if (lnk)
        {
            var js = lnk.href.substr(11);
            eval(js);
        }
    }
    var cl = document.getElementById('clAddress' + CustomerAddressID)
    cl.innerHTML = BuildEditAddressForm(CustomerAddressID, Title, FirstName, Surname, Address1, Address2, Address3, Town, County, PostCode, CountryID, CountryName, TelephoneNumber, tbl, sCell, bCell)
}

function SetBasketAddress(CustomerAddressID, address, tbl, sCell, bCell)
{
    sndReq('/XML/AddressFunctions.aspx?Action=setbasketaddress&address=' + address + '&CustomerAddressID=' + CustomerAddressID + '&TargetTable=' + tbl + '&sCell=' + sCell + '&bCell=' + bCell);
}

function BuildEditAddressForm(CustomerAddressID, Title, FirstName, Surname, Address1, Address2, Address3, Town, County, PostCode, CountryID, CountryName, TelephoneNumber, tbl, sCell, bCell)
{
    var ttls = '<option value="-1">Please select</option>';
    var ShowOthers = false;
    if (AddressTitles.substr(AddressTitles.length-5, 5) == 'Other')
        ShowOthers = true;
        
    var ts = AddressTitles.split(';');
    var OtherText = '';
    var found = false;
    for (i=0; i<ts.length; i++)
    {
        if ((ts[i] != '') && (ts[i] != 'Other'))
        {
            ttls += '<option value="' + ts[i] + '" ';
            if (ts[i] == Title)
            {
                ttls += 'selected';
                found = true;
            }
            ttls += '>' + ts[i] + '</option>';
        }
    }
    var OtherJS = '';
    if (ShowOthers)
    {
        ttls += '<option value="Other" ';
        if ((!found) && (Title != ''))
        {
            ttls += 'selected';
            OtherText = Title;
        }
        ttls += '>Other</option>';
        
        OtherJS = 'CheckOtherTitleSelected(\'' + CustomerAddressID + '\', \'\');';
    }
    
    if (Title == '') found = true;
        
    var ret = '<table id="tblContainer">';
    ret += '<tr>';
    ret +=     '<td style="width:100px">Title:</td>';
    ret +=     '<td><select id="drp' + CustomerAddressID + 'Title" onblur="checkTitleFormEntry(\'drp' + CustomerAddressID + 'Title\', \'txt' + CustomerAddressID + 'Title\', \'clVal' + CustomerAddressID + 'Title\');" onchange="checkTitleFormEntry(\'drp' + CustomerAddressID + 'Title\', \'txt' + CustomerAddressID + 'Title\', \'clVal' + CustomerAddressID + 'Title\'); ' + OtherJS + '">' + ttls + '</select>&nbsp;<input type="text" value="' + OtherText + '" id="txt' + CustomerAddressID + 'Title"  onblur="checkTitleFormEntry(\'drp' + CustomerAddressID + 'Title\', \'txt' + CustomerAddressID + 'Title\', \'clVal' + CustomerAddressID + 'Title\');"';
    if (found)
        ret += ' style="visibility: hidden"';
    ret += '></td>';
    ret +=     '<td class="FormValidation" id="clVal' + CustomerAddressID + 'Title"></td>';
    ret += '</tr>';
    ret += '<tr>';
    ret +=     '<td>First Name:</td>';
    ret +=     '<td><input type="text" id="txt' + CustomerAddressID + 'FirstName" value="' + FirstName + '" onblur="checkTextBoxIsValid(\'txt' + CustomerAddressID + 'FirstName\', \'First name\', \'clVal' + CustomerAddressID + 'FirstName\');"></td>';
    ret +=     '<td class="FormValidation" id="clVal' + CustomerAddressID + 'FirstName"></td>';
    ret += '</tr>';
    ret += '<tr>';
    ret +=     '<td>Surname:</td>';
    ret +=     '<td><input type="text" id="txt' + CustomerAddressID + 'Surname" value="' + Surname + '" onblur="checkTextBoxIsValid(\'txt' + CustomerAddressID + 'Surname\', \'Surname\', \'clVal' + CustomerAddressID + 'Surname\');"></td>';
    ret +=     '<td class="FormValidation" id="clVal' + CustomerAddressID + 'Surname"></td>';
    ret += '</tr>';
    ret += '<tr>';
    ret +=     '<td>Address Line 1:</td>';
    ret +=     '<td><input type="text" id="txt' + CustomerAddressID + 'Add1" value="' + Address1 + '" onblur="checkTextBoxIsValid(\'txt' + CustomerAddressID + 'Add1\', \'Address\', \'clVal' + CustomerAddressID + 'Address1\');"></td>';
    ret +=     '<td class="FormValidation" id="clVal' + CustomerAddressID + 'Address1"></td>';
    ret += '</tr>';
    ret += '<tr>';
    ret +=     '<td>Address Line 2:</td>';
    ret +=     '<td><input type="text" id="txt' + CustomerAddressID + 'Add2" value="' + Address2 + '"></td>';
    ret +=     '<td></td>';
    ret += '</tr>';
    ret += '<tr>';
    ret +=     '<td>Address Line 3:</td>';
    ret +=     '<td><input type="text" id="txt' + CustomerAddressID + 'Add3" value="' + Address3 + '"></td>';
    ret +=     '<td></td>';
    ret += '</tr>';
    ret += '<tr>';
    ret +=     '<td>Town:</td>';
    ret +=     '<td><input type="text" id="txt' + CustomerAddressID + 'Town" value="' + Town + '" onblur="checkTextBoxIsValid(\'txt' + CustomerAddressID + 'Town\', \'Town\', \'clVal' + CustomerAddressID + 'Town\');"></td>';
    ret +=     '<td class="FormValidation" id="clVal' + CustomerAddressID + 'Town"></td>';
    ret += '</tr>';
    ret += '<tr>';
    ret +=     '<td>County:</td>';
    ret +=     '<td><input type="text" id="txt' + CustomerAddressID + 'County" value="' + County + '" onblur="checkTextBoxIsValid(\'txt' + CustomerAddressID + 'County\', \'County\', \'clVal' + CustomerAddressID + 'County\');"></td>';
    ret +=     '<td class="FormValidation" id="clVal' + CustomerAddressID + 'County"></td>';
    ret += '</tr>';
    ret += '<tr>';
    ret +=     '<td>Post Code:</td>';
    ret +=     '<td><input type="text" id="txt' + CustomerAddressID + 'PostCode" value="' + PostCode + '" onblur="checkTextBoxIsValid(\'txt' + CustomerAddressID + 'PostCode\', \'Post code\', \'clVal' + CustomerAddressID + 'PostCode\');"></td>';
    ret +=     '<td class="FormValidation" id="clVal' + CustomerAddressID + 'PostCode"></td>';
    ret += '</tr>';
    ret += '<tr>';
    ret +=     '<td>Country:</td>';
    ret +=     '<td><select id="drp' + CustomerAddressID + 'Country" onchange="checkDropDownIsValid(\'drp' + CustomerAddressID + 'Country\', \'Country\', \'clVal' + CustomerAddressID + 'Country\');" onblur="checkDropDownIsValid(\'drp' + CustomerAddressID + 'Country\', \'Country\', \'clVal' + CustomerAddressID + 'Country\');">';
    ret += '<option value="-1">Please select</option>';
    
    for (i=0; i<Countries.childNodes.length; i++)
    {
        var CID = Countries.childNodes[i].getAttribute('id')
        var nme = getInnerText(Countries.childNodes[i]);
        ret += '<option value="' + CID + '" '
        
        if ((1*CountryID) == (1*CID))
            ret += 'selected';
        ret += '>' + nme + '</option>';
    }
    ret += '</td>';
    ret +=     '<td class="FormValidation" id="clVal' + CustomerAddressID + 'Country"></td>';
    ret += '</tr>';
    ret += '<tr>';
    ret +=     '<td>Telephone:</td>';
    ret +=     '<td><input type="text" id="txt' + CustomerAddressID + 'Telephone" value="' + TelephoneNumber + '"></td>';
    ret +=     '<td></td>';
    ret += '</tr>';
    ret += '</table>';
    ret += '<div style="width:100%" align="right"><a href="javascript:SaveCustomer(' + CustomerAddressID + ', \'' + tbl + '\', \'' + sCell + '\', \'' + bCell + '\')"><img src="' + jsImagePath + 'Account/Address/save.jpg" border="0"></a>';
    if (CustomerAddressID != -1)
    {
        ret += '&nbsp;&nbsp;&nbsp;&nbsp;<a id="lnkCancelEdit' + CustomerAddressID + '" href="javascript:CancelEdit(' + CustomerAddressID + ', ' +
                    '\'' + Title + '\', ' +
                    '\'' + FirstName + '\', ' +
                    '\'' + Surname + '\', ' +
                    '\'' + Address1 + '\', ' +
                    '\'' + Address2 + '\', ' +
                    '\'' + Address3 + '\', ' +
                    '\'' + Town + '\', ' +
                    '\'' + County + '\', ' +
                    '\'' + PostCode + '\', ' +
                    '\'' + CountryID + '\', ' +
                    '\'' + CountryName + '\', ' +
                    '\'' + TelephoneNumber + '\', ' +
                    '\'' + tbl + '\', ' +
                    '\'' + sCell + '\', ' +
                    '\'' + bCell + '\')' +
                    '"><img src="' + jsImagePath + 'Account/Address/cancel.jpg" border="0"></a>';
    }
    ret += "</div>";
    return ret;
}

function DeleteAddress(CustomerAddressID, tbl, sCell, bCell)
{
    var onyes = 'sndReq(\'/xml/AddressFunctions.aspx?action=deleteaddress&CustomerAddressID=' + CustomerAddressID +
                        '&TargetTable=' + tbl +
                        '&sCell=' + sCell +
                        '&bCell=' + bCell + '\');';
    confirmDHTML('DeleteCustomerAddress', onyes, '')
}

function CancelEdit(CustomerAddressID, Title, FirstName, Surname, Address1, Address2, Address3, Town, County, PostCode, CountryID, CountryName, TelephoneNumber, tbl, sCell, bCell)
{
    var cl = document.getElementById('clAddress' + CustomerAddressID)
    var links = '<a href="javascript:StartAddressEdit(' + CustomerAddressID + ', ' +
        '\'' + Title + '\', ' +
        '\'' + FirstName + '\', ' +
        '\'' + Surname + '\', ' +
        '\'' + Address1 + '\', ' +
        '\'' + Address2 + '\', ' +
        '\'' + Address3 + '\', ' +
        '\'' + Town + '\', ' +
        '\'' + County + '\', ' +
        '\'' + PostCode + '\', ' +
        '\'' + CountryID + '\', ' +
        '\'' + CountryName + '\', ' +
        '\'' + TelephoneNumber + '\', ' +
        '\'' + tbl + '\', ' +
        '\'' + sCell + '\', ' +
        '\'' + bCell + '\')' +
        '">Edit</a>';
        
    if (AddressIDs.length > 1)
    {
        links += '&nbsp;&nbsp;<a href="javascript:DeleteAddress(' + CustomerAddressID + ', ' +
                 '\'' + tbl.id + '\', ' +
                 '\'' + sCell.id + '\', ' +
                 '\'' + bCell.id + '\'' +       
                 ')">Delete</a>'
     }
     else
        links += '&nbsp;&nbsp;Delete';
                
    cl.innerHTML = BuildAddress(Title, FirstName, Surname, Address1, Address2, Address3, Town, County, PostCode, CountryName, TelephoneNumber) + '<br>' + links;
}

function SaveCustomer(CustomerAddressID, tbl, sCell, bCell)
{
    ValidateAjaxAddress(CustomerAddressID);
    if (IsPageValid)
    {
        var Title = document.getElementById('drp' + CustomerAddressID + 'Title').value;
        if (Title == 'Other') Title = document.getElementById('txt' + CustomerAddressID + 'Title').value;
        var FirstName = document.getElementById('txt' + CustomerAddressID + 'FirstName').value;
        var Surname = document.getElementById('txt' + CustomerAddressID + 'Surname').value;
        var Address1 = document.getElementById('txt' + CustomerAddressID + 'Add1').value;
        var Address2 = document.getElementById('txt' + CustomerAddressID + 'Add2').value;
        var Address3 = document.getElementById('txt' + CustomerAddressID + 'Add3').value;
        var Town = document.getElementById('txt' + CustomerAddressID + 'Town').value;
        var County = document.getElementById('txt' + CustomerAddressID + 'County').value;
        var PostCode = document.getElementById('txt' + CustomerAddressID + 'PostCode').value;
        var CountryID = document.getElementById('drp' + CustomerAddressID + 'Country').value;
        var TelephoneNumber = document.getElementById('txt' + CustomerAddressID + 'Telephone').value;
        sndReq('/XML/AddressFunctions.aspx?Action=updateaddress&CustomerAddressID=' + CustomerAddressID +
        '&Title=' + Title +
        '&FirstName=' + FirstName +
        '&Surname=' + Surname +
        '&Address1=' + Address1 +
        '&Address2=' + Address2 +
        '&Address3=' + Address3 +
        '&Town=' + Town +
        '&County=' + County +
        '&PostCode=' + PostCode +
        '&CountryID=' + CountryID +
        '&TelephoneNumber=' + TelephoneNumber +
        '&TargetTable=' + tbl + '&sCell=' + sCell + '&bCell=' + bCell);
    }
}

function ValidateAjaxAddress(CustomerAddressID)
{
    IsPageValid = true;
    checkTitleFormEntry('drp' + CustomerAddressID + 'Title', 'txt' + CustomerAddressID + 'Title', 'clVal' + CustomerAddressID + 'Title');
    checkTextBoxIsValid('txt' + CustomerAddressID + 'FirstName', 'First name', 'clVal' + CustomerAddressID + 'FirstName');
    checkTextBoxIsValid('txt' + CustomerAddressID + 'Surname', 'Surname', 'clVal' + CustomerAddressID + 'Surname');
    checkTextBoxIsValid('txt' + CustomerAddressID + 'Add1', 'Address', 'clVal' + CustomerAddressID + 'Address1');
    checkTextBoxIsValid('txt' + CustomerAddressID + 'Town', 'Town', 'clVal' + CustomerAddressID + 'Town');
    checkTextBoxIsValid('txt' + CustomerAddressID + 'County', 'County', 'clVal' + CustomerAddressID + 'County');
    checkTextBoxIsValid('txt' + CustomerAddressID + 'PostCode', 'Post code', 'clVal' + CustomerAddressID + 'PostCode');
    checkDropDownIsValid('drp' + CustomerAddressID + 'Country', 'Country', 'clVal' + CustomerAddressID + 'Country');
    return IsPageValid;
}