﻿
/* Copyright © Website Engineers Limited */

var cId = "#ctl00_Content_";

/* Finance Application */
jQuery(document).ready(
    function() {

        $(".PreviousAddressPanel").hide();
        $(".PreviousEmployerPanel").hide();
        $(".PostcodeSelector").hide();
        $(".Employer").hide();
        $(".Vehicle").hide();
        $(".FinanceDetails").hide();
        $(".PersonalDetails").hide();

        $(".TimeAtAddress").change(
            function() {
                var i = 0;
                var ShowPreviousEmployerBefore = 4;
                for (i = 0; i <= 10; i++) {
                    if ($(this).val() != "Please select") {
                        if ($(this).val() == i + " Years" | $(this).val() == i + " Year") {
                            if (i < ShowPreviousEmployerBefore) {
                                $(".PreviousAddressPanel").show("slow");
                                break;
                            }
                            else {
                                $(".PreviousAddressPanel").hide("slow");
                                break;
                            }
                        }
                    }
                    else {
                        $(".PreviousAddressPanel").hide("slow");
                        break;
                    }
                }
            }
        );
        $(".TimeWithEmployer").change(
            function() {
                var i = 0;
                var ShowPreviousAddressBefore = 4;
                for (i = 0; i <= 10; i++) {
                    if ($(this).val() != "Please select") {
                        if ($(this).val() == i + " Years" | $(this).val() == i + " Year") {
                            if (i < ShowPreviousAddressBefore) {
                                $(".PreviousEmployerPanel").show("slow");
                                break;
                            }
                            else {
                                $(".PreviousEmployerPanel").hide("slow");
                                break;
                            }
                        }
                    }
                    else {
                        $(".PreviousEmployerPanel").hide("slow");
                        break;
                    }
                }
            }
        );

        function useAddress(Id) {
            var getAddressesUrl = "http://services.postcodeanywhere.co.uk/json.aspx?action=fetch&id=" + $(cId + Id + "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") {
                                    $(cId + Id + "AddressSelection").hide("slow");
                                    $(cId + Id + "Address1").val(item.line1);
                                    $(cId + Id + "Address2").val(item.line2 + " " + item.line3 + " " + item.line4 + " " + item.line5);
                                    $(cId + Id + "Town").val(item.post_town);
                                    $(cId + Id + "County").val(item.county);
                                }
                            }
                        );
                    }
                );
        }
        $(".PostcodeSelector").change(
            function() {
                var Id = this.id.toString().replace(cId.replace("#", ""), "");
                switch (Id) {
                    case ("AddressSelection"):
                        useAddress("");
                        $(".Finance .PersonalDetails").show("slow");
                        $(".Finance .Employer").show("slow");
                        $(".Finance .Vehicle").show("slow");
                        $(".Finance .FinanceDetails").show("slow");
                        $(cId + "Save").show("slow");
                        break;
                    case ("PreviousAddressSelection"):
                        useAddress("Previous");
                        break;
                    case ("EmployerAddressSelection"):
                        useAddress("Employer");
                        break;
                    case ("PreviousEmployerAddressSelection"):
                        useAddress("PreviousEmployer");
                        break;
                }
            }
        );

        function getAddress(Id) {
            var GetAddressesUrl = "http://services.postcodeanywhere.co.uk/json.aspx?action=lookup&type=by_postcode&postcode=" + $(cId + Id + "Postcode").val() + "&account_code=JFTC111112&license_code=TM37-RP67-XW14-MB12&machine_id=&callback=?";
            $.getJSON(GetAddressesUrl,
                function(data) {
                    $(cId + Id + "AddressSelection").hide();
                    $(cId + Id + "AddressSelection").empty();
                    $.each(data,
                        function(i, item) {
                            if (typeof (item.description) != "undefined") {
                                $(cId + Id + "AddressSelection").append('<option value="' + item.id + '">' + item.description + '</option>');
                            }
                            else {
                                $(cId + Id + "AddressSelection").append('<option value="">No Addresses Found - Please Try Again</option>');
                            }
                        }
                    );
                    $(cId + Id + "AddressSelection").show("slow");
                }
            );
        }
        $(".GetAddress").click(
            function() {
                switch (this.id) {
                    case "GetAddress":
                        getAddress("");
                        break;
                    case "GetPreviousAddress":
                        getAddress("Previous");
                        break;
                    case "GetEmployerAddress":
                        getAddress("Employer");
                        break;
                    case "GetPreviousEmployerAddress":
                        getAddress("PreviousEmployer");
                        break;
                }
            }
        );


        function ajaxFailed(result) {
            alert("Error: " + result);
        }
        function salesmanChange(data, statusText) {
            $(cId + "Customer").empty();
            $(cId + "Customer").append('<option value="">Please select</option>');
            $('Customer', data).each(
                function(i) {
                    $(cId + "Customer").append('<option value="' + $(this).find("CustomerID").text() + '">' + $(this).find("Name").text() + '</option>');
                }
            );
        }  
        $(cId + "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) { salesmanChange(data, statusText) },
                        error: ajaxFailed
                    }
                );
            }
        );
        function makeChange(data, statusText) {
            $(cId + "Model").empty();
            $('Description', data).each(
                function(i) {
                    $(cId + "Model").append('<option value="' + $(this).find("Model").text() + '">' + $(this).find("Model").text() + '</option>');
                }
            );
        }
        $(cId + "Make").change(
            function() {
                $.ajax(
                    {
                        type: "POST",
                        url: "cap_service.asmx/Models",
                        data: "&Make=" + $("#ctl00_Content_Make option:selected").val(),
                        dataType: "xml",
                        success: function(data, statusText) { makeChange(data, statusText) },
                        error: ajaxFailed
                    }
                );
            }
        );

    }
);





/* End */