﻿$(document).ready(function() {
    $('input[type="text"]').each(
        function() {
            $(this).data("text", { startText: $(this).attr("startText") })
        }
    )
    .blur(
        function() {
            if ($(this).val() == '') {
                $(this).val($(this).data('text').startText);
            }
        }
    )
    .focus(
        function() {
            if ($(this).val() == $(this).data("text").startText) {
                $(this).val('')
            }
        }
    );

    //    $.ajax({
    //        type: "POST",
    //        url: "/Listings/CategoryList/",
    //        contentType: "application/json; charset=utf-8",
    //        dataType: "json",
    //        success: function(data) {
    //            var categories = JSON.parse(data);
    //            var options = '<option value="0">Search ALL Business Categories</option><option value="0"></option>';
    //            for (var x = 0; x < categories.length; x++) {
    //                var category = categories[x];
    //                var categoryName = category.Name;
    //                var categoryId = category.Id;
    //                if (categoryId.indexOf('.') > 0) {
    //                    //if (categoryName.toUpperCase() != categoryName) { 
    //                    options += '<option value="' + categoryId + '">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + categoryName + '</option>';
    //                } else {
    //                    options += '<option value="' + categoryId + '">' + categoryName + '</option>';
    //                }
    //            }
    //            $('#ListingSearchFormViewModel_ListingSearch_Types').html(options);
    //        }
    //    });

    $('#ListingSearchFormViewModel_ListingSearch_States').live('click', function() {
        var count = new Array();
        count = $('#ListingSearchFormViewModel_ListingSearch_States').val() || [];
        if (count.length > 1) {
            var options = '';
            options += '<option>Select only one state</option><option> to search by county</option>';
            $("#ListingSearchFormViewModel_ListingSearch_Counties").html(options).attr("disabled", "disabled");
        } else {
            $.ajax({
                type: "POST",
                url: "/County/Index/" + $('#ListingSearchFormViewModel_ListingSearch_States').val(),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(data) {
                    var counties = JSON.parse(data);
                    var options = '';
                    for (var x = 0; x < counties.length; x++) {
                        var county = counties[x];
                        var countyName = county.Name;
                        var countyId = county.Id;
                        options += '<option value="' + countyId + '">' + countyName + '</option>';
                    }
                    $("#ListingSearchFormViewModel_ListingSearch_Counties").html(options).attr("disabled", "");
                }
            });
        }
    });

    //    $('#ListingSearchFormViewModel_ListingSearch_Types').change(function() {
    //        var multipleValues = new Array();
    //        var selected = new Array();
    //        var selectedCount = 0;
    //        multipleValues = $("#ListingSearchFormViewModel_ListingSearch_Types").val() || [];
    //        for (var i = 0; i < multipleValues.length; i++) {
    //            selected[selectedCount] = multipleValues[i];
    //            selectedCount++;
    //            if (multipleValues[i].indexOf('.') == -1) {
    //                var key = multipleValues[i] + '.';
    //                $("#ListingSearchFormViewModel_ListingSearch_Types option").each(function() {
    //                    if ($(this).val().indexOf(key) == 0) {
    //                        selected[selectedCount] = $(this).val();
    //                        selectedCount++;
    //                    }
    //                });
    //            }
    //        }
    //        $("#ListingSearchFormViewModel_ListingSearch_Types").val(selected);
    //        $("#ListingSearchFormViewModel_ListingSearch_Types option:selected").each(function() {
    //            $(this).css("background-color", 'light blue');
    //        });
    //    });

    //This function on loss of focus on the Category dropdown list populates 
    //the SubTypes and Types list so they can be bound on form submit.
    $('#ListingSearchFormViewModel_ListingSearch_Types').live('blur', function() {
        var multipleValues = new Array();
        var subTypes = new Array();
        var Types = new Array();
        var subTypeCount = 0;
        var TypeCount = 0;
        multipleValues = $("#ListingSearchFormViewModel_ListingSearch_Types").val() || [];
        for (var i = 0; i < multipleValues.length; i++) {
            if (multipleValues[i].indexOf('.') > 0) {   //this is a subType
                subTypes[subTypeCount] = multipleValues[i].substring(multipleValues[i].indexOf('.') + 1);
                subTypeCount++;
            } else {          //This is a Type
                Types[TypeCount] = multipleValues[i];
                TypeCount++;
            }
        }
        $("#ListingSearchFormViewModel_ListingSearch_ListingSubTypes").val(subTypes);
        $("#ListingSearchFormViewModel_ListingSearch_ListingTypes").val(Types);
    });

    $('#formReset').live('click', function() {
        //call to clear viewmodel
        $('#searchForm').load('/ListingSearches/ResetForm/');
    });

    //INDEPENDENT:On Checkbox change, this function will set the Listing Category multiselect for binding on submit
    $('#Independent').live('click', function() {
        if ($('#Independent').is(":checked")) {
            if ($("#ListingSearchFormViewModel_ListingSearch_ListingCategories option[value='1']").length > 0) {
                $("#ListingSearchFormViewModel_ListingSearch_ListingCategories option[value='1']").attr("selected", "selected");
            } else {
                $("#ListingSearchFormViewModel_ListingSearch_ListingCategories").append("<option value='1' selected='selected'>Established Business</option>")
            }
        } else {
            $("#ListingSearchFormViewModel_ListingSearch_ListingCategories option[value='1']").removeAttr("selected");
        }
        $(this).blur();
    });
    //FRANCHISE: On Checkbox change, this function will set the Listing Category multiselect for binding on submit
    $('#Franchise').live('click', function() {
        if ($('#Franchise').is(":checked")) {
            if ($("#ListingSearchFormViewModel_ListingSearch_ListingCategories option[value='2']").length > 0) {
                $("#ListingSearchFormViewModel_ListingSearch_ListingCategories option[value='2']").attr("selected", "selected");
            } else {
                $("#ListingSearchFormViewModel_ListingSearch_ListingCategories").append("<option value='2' selected='selected'>Franchise</option>")
            }
        } else {
            $("#ListingSearchFormViewModel_ListingSearch_ListingCategories option[value='2']").removeAttr("selected");
        }
        $(this).blur();
    });

});
