﻿

/* Copyright © Website Engineers Limited */

/* Part Exchange Application */

jQuery(document).ready( function() { 

        $("#ctl00_Content_AddressSelection").hide();
        $(".PartExchange .PersonalDetails").hide();
        $(".PartExchange .TopDetails").hide();

        $("#ctl00_Content_AddressSelection").change( function() {
            var GetAddressesUrl = "http://services.postcodeanywhere.co.uk/json.aspx?action=fetch&id="+$("#ctl00_Content_AddressSelection option:selected").val()+"&account_code=JFTC111112&license_code=TM37-RP67-XW14-MB12&machine_id=&callback=?";
            $.getJSON(GetAddressesUrl, function(data) { 
                $.each(data, function(i, item) {
                    if (typeof(item.error_number) == "undefined")
                    {                      
                        $("#ctl00_Content_AddressSelection").hide("slow");
                        $("#ctl00_Content_Address1").val(item.line1);
                        $("#ctl00_Content_Address2").val(item.line2 + " " + item.line3 + " " + item.line4 + " " + item.line5 );
                        $("#ctl00_Content_Town").val(item.post_town);
                        $("#ctl00_Content_County").val(item.county);
                        
                        $(".PartExchange .PersonalDetails").show("slow");
                    }
                    else
                    {
                        //alert(item.message);
                    }
                });
            });
        });

        $("#ctl00_Content_GetAddresses").click(function(){
            var GetAddressesUrl = "http://services.postcodeanywhere.co.uk/json.aspx?action=lookup&type=by_postcode&postcode="+$("#ctl00_Content_Postcode").val()+"&account_code=JFTC111112&license_code=TM37-RP67-XW14-MB12&machine_id=&callback=?";
            $.getJSON(GetAddressesUrl, function(data) { 

                $("#ctl00_Content_AddressSelection").hide();
                $("#ctl00_Content_AddressSelection").empty();
        
                    $.each(data, function(i, item) {
                        if (typeof(item.description) != "undefined")
                        {
                            $("#ctl00_Content_AddressSelection").append('<option value="' +item.id+ '">' +item.description+ '</option>');
                        } else 
                        { 
                            $("#ctl00_Content_AddressSelection").append('<option value="">No Addresses Found - Please Try Again</option>');
                        }
                    });
       
                $("#ctl00_Content_AddressSelection").show("slow");
            
            });
            return false;
        });
        
        $("#ctl00_Content_Salesman").change(
            function() {
                $.ajax({  
                    type: "POST",  
                    url: "admin_service.asmx/Customers",  
                    data: "&SalesmanID=" + $("#ctl00_Content_Salesman option:selected").val(),  
                    dataType: "xml",  
                    success: function(data, statusText) {  
                        AjaxSucceededSalesman(data, statusText);  
                    },  
                    error: AjaxFailed
                }); 
            }
        );
      
});

function AjaxSucceededSalesman(data, statusText) {  
    $("#ctl00_Content_Customer").empty();
    $("#ctl00_Content_Customer").append('<option value="">Please select</option>');
    $('Customer', data).each (
        function(i) { 
            $("#ctl00_Content_Customer").append('<option value="' + $(this).find("CustomerID").text() + '">' + $(this).find("Name").text() + '</option>');
        }
    );
}  

function AjaxFailed(result) {  
    alert ("Error: " + result);
}

/* End */
