$(function() {

    $("select[name=billing[country]], select[name=billing_country]").change(function() {
        if($(this).val() == "GB") {
            $("input[name=billing_address_lookup]").show();
        }
        else {
            $("input[name=billing_address_lookup]").hide();
            $("#billing-addresses-box").hide();
        }
    });

    $("select[name=shipping[country]], select[name=shipping_country]").change(function() {
        if($(this).val() == "GB") {
            $("input[name=shipping_address_lookup]").show();
        }
        else {
            $("input[name=shipping_address_lookup]").hide();
            $("#shipping-addresses-box").hide();
        }
    });

    if($("select[name=billing[country]], select[name=billing_country]").val() == "GB")
    {
        $("input[name=billing_address_lookup]").show();
    }
    else
    {
        $("input[name=billing_address_lookup]").hide();
    }
    if($("select[name=shipping[country]], select[name=shipping_country]").val() == "GB")
    {
        $("input[name=shipping_address_lookup]").show();
    }
    else
    {
        $("input[name=shipping_address_lookup]").hide();
    }

    $("input[name=sameshipping]").click(function() {
       if($(this).attr("checked") == true) {
            if($("select[name=billing[country]]").val() == "GB")
            {
                $("input[name=shipping_address_lookup]").show();
            }
            else
            {
                $("input[name=shipping_address_lookup]").hide();
            }
       }
    });

    $("input[name=billing_address_lookup]").click(function() {
        $.ajax({
            url : 'index.php',
            data : ({
               page : 'modules/address_lookup/ajax_get_address',
               command : 'getPostCodePremises',
               zip : $("input[name=billing[zip]], input[name=billing_zip]").val()
            }),
            dataType : "json",
            success : function(data) {
                if(data['xml'])
                {
                    $("#billing-addresses-box").show();
                    $("#billing-address-error").remove();
                    $("select[name=billing_addresses]").empty();
                    for(row in data['xml']['rs:data']['z:row'])
                    {
                        var description = data['xml']['rs:data']['z:row'][row]["description"];
                        $("select[name=billing_addresses]").append('<option value="' + description + '">' + description + '</option>');
                    }
                }
                else
                {
                    $("#billing-addresses-box").hide();
                    $("#billing-address-error").remove();
                    $("input[name=billing_address_lookup]").after('<p id="billing-address-error" class="error">No address found!</p>');
                }
            }
        });
    });

    $("select[name=billing_addresses]").change(function() {
        $("input[name=sameshipping]").attr("checked",false);
        var premise = $(this).val();
        $.ajax({
            url : 'index.php',
            data : ({
               page : 'modules/address_lookup/ajax_get_address',
               command : 'getPostCodeAddress',
               zip : $("input[name=billing[zip]], input[name=billing_zip]").val(),
               address : String(premise)
            }),
            dataType : "json",
            success : function(data) {
                $("input[name=billing[address1]], input[name=billing_address1]").val(data.address1);
                $("input[name=billing[address2]], input[name=billing_address2]").val(data.address2);
                $("input[name=billing[city]], input[name=billing_city]").val(data.city);
                $("input[name=billing[state_other]], input[name=billing_state_other]").val(data.state);
                $("input[name=billing[company]], input[name=billing_company]").val(data.company);
            },
            type : "POST"
        });
    });


    $("input[name=shipping_address_lookup]").click(function() {
        $.ajax({
            url : 'index.php',
            data : ({
               page : 'modules/address_lookup/ajax_get_address',
               command : 'getPostCodePremises',
               zip : $("input[name=shipping[zip]], input[name=shipping_zip]").val()
            }),
            dataType : "json",
            success : function(data) {
                if(data['xml'])
                {
                    $("#shipping-addresses-box").show();
                    $("#shipping-address-error").remove();
                    $("select[name=shipping_addresses]").empty();
                    for(row in data['xml']['rs:data']['z:row'])
                    {
                        var description = data['xml']['rs:data']['z:row'][row]["description"];
                        $("select[name=shipping_addresses]").append('<option value="' + description + '">' + description + '</option>');
                    }
                }
                else
                {
                    $("#shipping-addresses-box").hide();
                    $("#shipping-address-error").remove();
                    $("input[name=shipping_address_lookup]").after('<p id="shipping-address-error" class="error">No address found!</p>');
                }
            }
        });
    });

    $("select[name=shipping_addresses]").change(function() {
        $("input[name=sameshipping]").attr("checked",false);
        var premise = $(this).val();
        $.ajax({
            url : 'index.php',
            data : ({
               page : 'modules/address_lookup/ajax_get_address',
               command : 'getPostCodeAddress',
               zip : $("input[name=shipping[zip]], input[name=shipping_zip]").val(),
               address : String(premise)
            }),
            dataType : "json",
            success : function(data) {
                $("input[name=shipping[address1]], input[name=shipping_address1]").val(data.address1);
                $("input[name=shipping[address2]], input[name=shipping_address2]").val(data.address2);
                $("input[name=shipping[city]], input[name=shipping_city]").val(data.city);
                $("input[name=shipping[state_other], input[name=shipping_state_other]").val(data.state);
                $("input[name=shipping[company]], input[name=shipping_company]").val(data.company);
            },
            type : "POST"
        });
    });
});
