// The function is called when user clicks a address displayed in the found address listbox
// It will add the clicked address details into the "Try my address" address controls
function addAddress_onListBoxItemClick(lbFindAddress)
{

    var idPrefix = getIdPrefix(lbFindAddress.id);

    var addressType = 'Billing';
    if (lbFindAddress.id == idPrefix+ '_lbShippingFindAddress')
        addressType = 'Shipping';

    var fullAddress = lbFindAddress.value;

    if (fullAddress != '')
    {
        var addressControls = new AD_AddressControls(addressType, fullAddress, idPrefix);
        addressControls.AssignAddresses();
    }
}
function getIdPrefix(id)
{
    return id.substring(0, id.lastIndexOf('_'));
}
function getValue(id)
{
    var field = document.getElementById(id);
    return field == null ? '' : field.value;
}
function getInnerHTML(id)
{
    var field = document.getElementById(id);
    return field == null ? '' : field.options[field.selectedIndex].innerHTML;
}
// The js class holds all the address controls in the "Try my address" section
// It contains functions for:
// - assign address values stored in hidden fields to the address textbox and hidden fields
// - setting enable or disable status to the address textboxes
// - Set focus on the first address controls (i.e. txtAddress1)
function AD_AddressControls(addressType, fullAddress, idPrefix)
{
    // Address 1
    this.tbAddress1;
    this.hdnAddress1;
    // Address 2
    this.tbAddress2;
    this.hdnAddress2;
    // Address 3
    this.tbAddress3;
    this.hdnAddress3;
    // Address 4
    this.tbAddress4;
    this.hdnAddress4;
    // town
    this.tbTown;
    this.hdnTown;
    // Postal code
    this.tbPostalCode;
    this.hdnPostalCode;
    // Country
    this.tbCountry;
    this.hdnCountry;
    // Assign properties with the given parameter values
    this.addressType = addressType;
    this.fullAddress = fullAddress
    this.idPrefix = idPrefix;
    
    this.Initialize();
}
AD_AddressControls.prototype.Initialize = function()
{
    this.tbAddress1 = document.getElementById(this.idPrefix+ '_txt' + this.addressType + 'Address1');
    this.hdnAddress1 = document.getElementById(this.idPrefix+ '_hdn' + this.addressType + 'Address1');

    this.tbAddress2 = document.getElementById(this.idPrefix+ '_txt' + this.addressType + 'Address2');
    this.hdnAddress2 = document.getElementById(this.idPrefix+ '_hdn' + this.addressType + 'Address2');

    this.tbAddress3 = document.getElementById(this.idPrefix+ '_txt' + this.addressType + 'Address3');
    this.hdnAddress3 = document.getElementById(this.idPrefix+ '_hdn' + this.addressType + 'Address3');

    this.tbAddress4 = document.getElementById(this.idPrefix+ '_txt' + this.addressType + 'Address4');
    this.hdnAddress4 = document.getElementById(this.idPrefix+ '_hdn' + this.addressType + 'Address4');

    this.tbTown = document.getElementById(this.idPrefix+ '_txt' + this.addressType + 'Town');
    this.hdnTown = document.getElementById(this.idPrefix+ '_hdn' + this.addressType + 'Town');

    this.tbPostalCode = document.getElementById(this.idPrefix+ '_txt' + this.addressType + 'PostalCode');
    this.hdnPostalCode = document.getElementById(this.idPrefix+ '_hdn' + this.addressType + 'PostalCode');

    this.tbCountry = document.getElementById(this.idPrefix+ '_txt' + this.addressType + 'Country');
    this.hdnCountry = document.getElementById(this.idPrefix+ '_hdn' + this.addressType + 'Country');
}
AD_AddressControls.prototype.AssignAddresses = function()
{
    if (this.fullAddress != '')
    {
        var address1Value = document.getElementById(this.fullAddress + '_1').value;
        this.tbAddress1.value = address1Value;
        this.hdnAddress1.value = address1Value;

        var address2Value = document.getElementById(this.fullAddress + '_2').value;
        this.tbAddress2.value = address2Value;
        this.hdnAddress2.value = address2Value;

        var address3Value = document.getElementById(this.fullAddress + '_3').value;
        this.tbAddress3.value = address3Value;
        this.hdnAddress3.value = address3Value;

        var address4Value = document.getElementById(this.fullAddress + '_4').value;
        this.tbAddress4.value = address4Value;
        this.hdnAddress4.value = address4Value;

        var townValue = document.getElementById(this.fullAddress).value;
        this.tbTown.value = townValue;
        this.hdnTown.value = townValue;

        var postalCodeValue = getValue(this.idPrefix + '_txtOriginal' + this.addressType + 'PostalCode');
        this.tbPostalCode.value = postalCodeValue;
        this.hdnPostalCode.value = postalCodeValue;

        var countryValue = getInnerHTML(this.idPrefix + '_ddl' + this.addressType + 'Country');
        this.tbCountry.value = countryValue;
        this.hdnCountry.value = countryValue;
    }
}
AD_AddressControls.prototype.SetStatus = function(disabled)
{
    this.tbAddress1.disabled = disabled;
    this.tbAddress2.disabled = disabled;
    this.tbAddress3.disabled = disabled;
    this.tbAddress4.disabled = disabled;
    this.tbTown.disabled = disabled;
    this.tbPostalCode.disabled = disabled;
    this.tbCountry.disabled = true;
}
AD_AddressControls.prototype.SetFirstControlCursor = function()
{
    this.tbAddress1.focus();
}
// The function is called when user clicks "Try my address" button
// Enable or disable "Try my address" address controls
// according to the selection on the existing address radio button list
// e.g. if "Find Address" radio button is selected for shipping address,
// all the address controls under shipping address will be enabled.
// Otherwise, they remains as disabled (readonly)
function resetAddressControlsStatus(btnTypeMyAddress, addressType)
{
    var idPrefix = getIdPrefix(btnTypeMyAddress.id);
    
    // Get radio button list for existing billing addresses
    var rblAddressList = document.getElementById(idPrefix + '_rbl' + addressType + 'AddressList');
    // check whether the find address option is selected in the radio button list
    var findAddressSelected = isFindAddressSelected(rblAddressList);
    // If the find address radio button is selected
    // Enable all the address controls in the "Try my address" section
    // and set focus on the first address control
    if (findAddressSelected)
    {
        var addressControls = new AD_AddressControls(addressType, '', idPrefix);
        addressControls.SetStatus(false);
        addressControls.SetFirstControlCursor();
    }
}
// Returns true, if the find address radio button has been selected (ticked)
function isFindAddressSelected(rblAddressList)
{
    // Get find address radio button
    var rbFindAddress = rblAddressList.rows[rblAddressList.rows.length-1].children[0].children[0];
    // returns checked status
    return rbFindAddress.checked;
}
// Assign the given address(1 or 2 or 3 ro 4) textbox values to its correspondent hidden field
// By doing so, the value in the hidden field can persist,
// even though the textbox value may be lost once it's disabled
function assignAddressHiddenField(tbAddressField, addressType, index)
{
    var idPrefix = getIdPrefix(tbAddressField.id);
    var hdnAddress = document.getElementById(idPrefix+ '_hdn' + addressType + 'Address' + index);
    hdnAddress.value = tbAddressField.value;
}
function assignAddressTownHiddenField(tbTown, addressType)
{
    var idPrefix = getIdPrefix(tbTown.id);
    var hdnTown = document.getElementById(idPrefix+ '_hdn' + addressType + 'Town');
    hdnTown.value = tbTown.value;
}
function assignAddressPostalCodeHiddenField(tbPostalCode, addressType)
{
    var idPrefix = getIdPrefix(tbPostalCode.id);
    var hdnPostalCode = document.getElementById(idPrefix+ '_hdn' + addressType + 'PostalCode');
    hdnPostalCode.value = tbPostalCode.value;
}
function assignAddressCountryHiddenField(tbCountry, addressType)
{
    var idPrefix = getIdPrefix(tbCountry.id);
    var hdnCountry = document.getElementById(idPrefix+ '_hdn' + addressType + 'Country');
    hdnCountry.value = tbCountry.value;
}
function validatePostalCodeAgainstCountry(btnFindAddress, addressType)
{
    var idPrefix = getIdPrefix(btnFindAddress.id);
    var txtOriginalPostalCode = document.getElementById(idPrefix+ '_txtOriginal' + addressType + 'PostalCode');
    var country = document.getElementById(idPrefix+ '_ddl' + addressType + 'Country');
    return validatePostalCodeVsCountry(txtOriginalPostalCode, country, addressType);
}
function GenerateErrorAlert(message, txtOriginalPostalCode)
{
    alert(message);
    txtOriginalPostalCode.focus();
    return false;
}

function CheckValidPostCode(strPostalCode)
{
    try
    {

        var Char ='';
        //The total length must be 6,7, or 8 characters, a gap (space character) must be included 
        if(strPostalCode.length > 8 || strPostalCode.length < 6)
            return false;
            
        //The first character of the outward code must be alpha
        Char = strPostalCode.charAt(0);
        var keyCode = strPostalCode.charCodeAt(i)
        if(keyCode > 47 && keyCode < 58)
            return false;
        
        for (var i=strPostalCode.length; i > 1; i--)
        {
            //The second and third characters of the inward code must be alpha
            if(i == (strPostalCode.length - 1))
            {
                Char = strPostalCode.charAt(i);
                var keyCode = strPostalCode.charCodeAt(i)
                if(keyCode > 47 && keyCode < 58)
                    return false;
            }
            if(i == (strPostalCode.length - 2))
            {
                Char = strPostalCode.charAt(i);
                var keyCode = strPostalCode.charCodeAt(i)
                if(keyCode > 47 && keyCode < 58)
                    return false;
            }
            
            //The first character of the inward code must be numeric 
            if(i == (strPostalCode.length - 3))
            {
                Char = strPostalCode.charAt(i);
                var keyCode = strPostalCode.charCodeAt(i)
                if((keyCode > 64 && keyCode < 91) || (keyCode > 96 && keyCode < 123))
                    return false;
            }
            
            //The inward code, the part to the right of the gap, must always be 3 characters 
            if(i == (strPostalCode.length - 4))
            {
                Char = strPostalCode.charAt(i);
                if(Char != ' ')
                    return false;
            }
            
        }    
         //The outward code, the part to the left of the gap, can be 2,3, or 4 characters 
        PstCd = strPostalCode.substring(0, strPostalCode.length - 4)
        if(PstCd.length < 2 || PstCd.length > 4)
            return false;
               
         return true;
        
    }
    catch(objException)
    {
		return false;	
	}
}
