function resetAddressStatus(country)
{
    // Get all address form controls
    var idPrefix = getIdPrefix(country.id);
    var addressControls = new AddressControls(document, idPrefix);

    // check whether Republic Of Ireland has been selected for the country
    var republicOfIrelandSelected = country.value == 'IRE';
    addressControls.ResetStatusByCountry(republicOfIrelandSelected);
}
function validatePostalCodeWithCountry(btnFindAddress)
{
    var idPrefix = getIdPrefix(btnFindAddress.id);
    var txtFindAddressPostalCode = getFieldById(document, idPrefix + '_txtFindAddressPostalCode');
    var country = getFieldById(document, idPrefix + '_ddlCountry');
    var addressType = idPrefix.indexOf('TJC_AddressForm1') > 0 ? 'Billing' : 'Shipping'
    
    return validatePostalCodeVsCountry(txtFindAddressPostalCode, country, addressType);
}
function validatePostalCodeVsCountry(txtOriginalPostalCode, country, addressType)
{
    var postalCode = txtOriginalPostalCode.value;
    if (postalCode.length < 2)
    {
        return GenerateErrorAlert(addressType + ' "Post Code" field for finding address must contain a valid post code.', txtOriginalPostalCode);
    }
    else
    {
        var firstTwoCharPostalCode = postalCode.substring(0, 2);

        var isValidJerseyPostalCode = firstTwoCharPostalCode.toUpperCase() == 'JE';
        var isValidGurnseyPostalCode = firstTwoCharPostalCode.toUpperCase() == 'GY';

        if (country.value == 'GJ') // Jersey (Channel Islands) selected
        {
            if (!isValidJerseyPostalCode)
            {
                return GenerateErrorAlert(addressType + ' "Post Code" field for finding address must start with JE.', txtOriginalPostalCode);
            }
        }
        else if (country.value == 'GG') // Guernsey (Channel Islands) selected
        {
            if (!isValidGurnseyPostalCode)
            {
                return GenerateErrorAlert(addressType + ' "Post Code" field for finding address must start with GY.', txtOriginalPostalCode);
            }
        }
        else if (country.value == 'UK') // United Kingdom
        {
            if (isValidJerseyPostalCode || isValidGurnseyPostalCode)
            {
                return GenerateErrorAlert(addressType + ' "Post Code" field for finding address must not start with JE or GY.', txtOriginalPostalCode);
            }
        }
    }

    return true;
}
function expandBillingAddress(chkCopyAddress)
{
    // Get id prefix such as ctl03
    var idPrefix = getIdPrefix(chkCopyAddress.id);
    var divAddressForm2 = getFieldById(document, idPrefix + '_TJC_AddressForm_div2');
    if (divAddressForm2 != null)
        divAddressForm2.style.display = chkCopyAddress.checked ? '' : 'none';
}
function addSelectedAddress(lbFindAddress)
{
    // Get id prefix such as ctl03_AddressForm1
    var idPrefix = getIdPrefix(lbFindAddress.id);
    var addressControls = new AddressControls(document, idPrefix);

    if (lbFindAddress.value == '')
    {
        addressControls.SetFocus(false);
        // Do nothing with clicking on the lbFindAddress listbox if no item in it
        return;
    }

    var fullAddress = lbFindAddress.value;
    var isDefaultAddressClicked = fullAddress == '$$$';

    if (!isDefaultAddressClicked)
    {
        var address1 = getAddress(fullAddress, 1);
        var address2 = getAddress(fullAddress, 2);
        var address3 = getAddress(fullAddress, 3);
        var address4 = getAddress(fullAddress, 4);

        // get town name from the selected address
        var townName = getTownName(fullAddress);

        addressControls.AssignAddresses(townName, address1, address2, address3, address4, false);
    }
    
    addressControls.ResetStatusByClickingAddress(isDefaultAddressClicked);
}
function assingAddressHiddenField(txtAddress, index)
{
    assingTextBoxValueToItsHiddenField(txtAddress, 'hdnAddress' + index);
}
function assingTownHiddenField(txtTown)
{
    assingTextBoxValueToItsHiddenField(txtTown, 'hdnTown');
}
function assingCountyHiddenField(txtCounty)
{
    assingTextBoxValueToItsHiddenField(txtCounty, 'hdnCounty');
}
function assingPostalCodeHiddenField(txtPostalCode)
{
    assingTextBoxValueToItsHiddenField(txtPostalCode, 'hdnPostalCode');
}
function assingTextBoxValueToItsHiddenField(textBox, hdnFieldId)
{
    // Get id prefix such as ctl03_AddressForm1
    var idPrefix = getIdPrefix(textBox.id);
    var hdnField = getFieldById(document, idPrefix + '_' + hdnFieldId);
    hdnField.value = textBox.value;
}
function getIdPrefix(id)
{
    return id.substring(0, id.lastIndexOf('_'));
}
function getFieldById(doc, id)
{
    return doc.getElementById(id);
}
function getAddress(fullAddress, addressIndex)
{
    var hdnAddress = getFieldById(document, fullAddress + '_' + addressIndex);
    return hdnAddress != null ? hdnAddress.value : '';
}
function getTownName(fullAddress)
{
    var hdnBuildingNumberAndTown = getFieldById(document, fullAddress);
    return hdnBuildingNumberAndTown != null ? hdnBuildingNumberAndTown.value : '';
}
function AddressControls(doc, idPrefix)
{
    this.txtFindAddressPostalCode = getFieldById(doc, idPrefix + '_txtFindAddressPostalCode');
    this.txtBuildingNumber = getFieldById(doc, idPrefix + '_txtBuildingNumber');
    this.btnFindAddress = getFieldById(doc, idPrefix + '_btnFindAddress');
    this.txtAddress1 = getFieldById(doc, idPrefix + '_txtAddress1');
    this.hdnAddress1 = getFieldById(doc, idPrefix + '_hdnAddress1');
    this.txtAddress2 = getFieldById(doc, idPrefix + '_txtAddress2');
    this.hdnAddress2 = getFieldById(doc, idPrefix + '_hdnAddress2');
    this.txtAddress3 = getFieldById(doc, idPrefix + '_txtAddress3');
    this.hdnAddress3 = getFieldById(doc, idPrefix + '_hdnAddress3');
    this.txtAddress4 = getFieldById(doc, idPrefix + '_txtAddress4');
    this.hdnAddress4 = getFieldById(doc, idPrefix + '_hdnAddress4');
    this.txtTown = getFieldById(doc, idPrefix + '_txtTown');
    this.hdnTown = getFieldById(doc, idPrefix + '_hdnTown');
    this.txtCounty = getFieldById(doc, idPrefix + '_txtCounty');
    this.hdnCounty = getFieldById(doc, idPrefix + '_hdnCounty');
    this.txtPostalCode = getFieldById(doc, idPrefix + '_txtPostalCode');
    this.hdnPostalCode = getFieldById(doc, idPrefix + '_hdnPostalCode');
    this.lbFindAddress = getFieldById(doc, idPrefix + '_lbFindAddress');
}
AddressControls.prototype.ResetStatusByCountry = function(republicOfIrelandSelected)
{
    this.txtFindAddressPostalCode.disabled = republicOfIrelandSelected;
    this.txtBuildingNumber.disabled = republicOfIrelandSelected;
    this.btnFindAddress.disabled = republicOfIrelandSelected;
    this.txtAddress1.disabled = !republicOfIrelandSelected;
    this.txtAddress2.disabled = !republicOfIrelandSelected;
    this.txtAddress3.disabled = !republicOfIrelandSelected;
    this.txtAddress4.disabled = !republicOfIrelandSelected;
    this.txtTown.disabled = !republicOfIrelandSelected;
    this.txtCounty.disabled = !republicOfIrelandSelected;
    this.txtPostalCode.disabled = !republicOfIrelandSelected;
    
    this.SetFocus(!republicOfIrelandSelected);
    
    this.ClearFindAddressListBox();
    this.AssignAddresses('', '', '', '', '', true);
}
AddressControls.prototype.ClearFindAddressListBox = function()
{
    for(i=this.lbFindAddress.length-1; i>=1; i--)
        this.lbFindAddress.options[i] = null;
}
AddressControls.prototype.ResetStatusByClickingAddress = function(isDefaultAddressClicked)
{
    this.txtAddress1.disabled = !isDefaultAddressClicked;
    this.txtAddress2.disabled = !isDefaultAddressClicked;
    this.txtAddress3.disabled = !isDefaultAddressClicked;
    this.txtAddress4.disabled = !isDefaultAddressClicked;
    this.txtTown.disabled = !isDefaultAddressClicked;
    this.txtCounty.disabled = !isDefaultAddressClicked;
    this.txtPostalCode.disabled = !isDefaultAddressClicked;
    
    this.SetFocus(!isDefaultAddressClicked);
}
AddressControls.prototype.AssignAddresses = function(townName, address1, address2, address3, address4, setPostalCodeEmpty)
{
    this.txtAddress1.value = address1;
    this.hdnAddress1.value = address1;

    this.txtAddress2.value = address2;
    this.hdnAddress2.value = address2;

    this.txtAddress3.value = address3;
    this.hdnAddress3.value = address3;

    this.txtAddress4.value = address4;
    this.hdnAddress4.value = address4;

    this.txtTown.value = townName;
    this.hdnTown.value = townName;

    if (setPostalCodeEmpty)
    {
        this.txtFindAddressPostalCode.value = '';
        this.txtPostalCode.value = '';
        this.hdnPostalCode.value = '';
    }
    else
    {
        var postalCode = this.txtFindAddressPostalCode.value;
        this.txtPostalCode.value = postalCode;
        this.hdnPostalCode.value = postalCode;
    }
}
AddressControls.prototype.SetFocus = function(onPostalCode)
{

    
    if (onPostalCode)
    {
        if (!this.txtPostalCode.disabled)
            this.txtPostalCode.focus();
        else if (!this.txtAddress1.disabled)
            this.txtAddress1.focus();
    }
    else
    {
        if (!this.txtAddress1.disabled)
            this.txtAddress1.focus();
        else if (!this.txtPostalCode.disabled)
            this.txtPostalCode.focus();
    }
}
