﻿//Date time field validator
$(document).ready(function() {

    //if script is enabled, we disable the DDL until the PostCode has been entered
    $('#State').attr('disabled', 'disabled');

    //updates the states if the form submitted contains error
    if ($('#PostCode').val().length > 2)
    { checkPC(); }

    /* Below are the functions for handling the postcode event*/
    //in here we use keyuptimer to allow user to finish input before we send ajax request.
    //therefore everytime the keyup event triggers we reset timer. Timeout set to 0.5 sec
    var keyuptimer;
    $("#PostCode").keyup(function() {
        clearTimeout(keyuptimer);
        //remove all current errors first
        $(".postcode").children(".field-validation-error").remove();
        $(".postcode").removeClass('error');
        $dError = $('.postcode').children('.field-validation-error');
        $dError.remove();

        if ($("#PostCode").val().length > 2) {
            keyuptimer = setTimeout(checkPC, 500);
        }
    });

    function checkPC() {
        loadStates("/Account/GetPostCodeAddress");
    }

    function loadStates(cbUrl) {
        $.ajax({
            url: "/Account/GetPostCodeAddress/",
            cache: false,
            data: "PostCode=" + $("#PostCode").val(),
            type: "GET",
            dataType: 'json',
            success: function(data) {
                $("#State").removeAttr("disabled");
                //will normally fall here no matter postcode search returns any results
                $(".states").html(printStatesDDLOptions(data));
            },
            error: function(xhr, error) {
                /*alert("error = " + error); 
                alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
                alert("responseText: " + xhr.responseText);*/
                //shouldnt normally fall here, fall here just disable state function    
                //alert(error);
                $("#State").attr("disabled", "disabled");
                // $("#stateLoadIndicator").html("");
            }
        });

    }

    function printStatesDDLOptions(list) {
        var htmlData = "<label for=\"State\" >State:</label><select id=\"State\" name=\"State\"";
        //disable ddl if list returned from ajax is empty
        if (list.length == 0)
            htmlData += "disabled=\"disabled\"";
        htmlData += ">";

        htmlData += "<option value=\"\">Please Select</option>";
        if (list.length == 0) //return no results
        {
            $('.postcode').addClass("error");
            //add attribute to #PostCode input field to tell Xval not to touch this
            $("#PostCode").attr("generated", "false");
            var errMsg = "Postcode is invalid.";
            if ($(".postcode").children('.field-validation-error').length > 0) {
                $(".postcode").children('.field-validation-error').replaceWith('<span class="field-validation-error">' + errMsg + '</span>');
                $(".postcode .field-validation-error").attr("display", "block");
            }
            else {
                $(".postcode").append('<span class="field-validation-error">' + errMsg + '</span>');
            }
        }
        else {
            for (var i = 0; i < list.length; i++) {
                if (i == 0) {
                    htmlData += "<option value=\"" + list[i] + "\" selected=\"selected\">" + list[i] + "</option>";
                }
                else {
                    htmlData += "<option value=\"" + list[i] + "\">" + list[i] + "</option>";
                }
            }
        }
        htmlData += "</select>";
        return htmlData;

    }

});





