﻿
var $j = jQuery.noConflict();


$j(function() {

    var selectorOne = $j('#Selector_One');
    var selectorTwo = $j('#Selector_Two');
    var selectorThree = $j('#Selector_Three');
    var imageGo = $j('#Image_Go');

    selectorOne.change(function() {

        selectorTwo.attr('disabled', 'disabled')
        selectorThree.attr('disabled', 'disabled')

        selectorTwo.children().remove().end().append('<option value="">Select Category</option>');
        selectorThree.children().remove().end().append('<option value="">Select Range</option>');

        GetAJAXContent(

				$j(this).val(),
        // Callback method for results.
				function(ContentArray) {

				    if (ContentArray.SubCats.length > 0) {

				        selectorTwo.removeAttr("disabled")

				        for (var idxItem = 0; idxItem < ContentArray.SubCats.length; idxItem++) {
				            selectorTwo.append($j("<option></option>").attr("value", ContentArray.SubCats[idxItem].id).attr("path", ContentArray.SubCats[idxItem].path).text(ContentArray.SubCats[idxItem].title));
				        }
				    }

				},
				'/ajaxhandlers/SubCategoryHandler.ashx'
				)
    });

    selectorTwo.change(function() {


        selectorThree.attr('disabled', 'disabled')


        selectorThree.children().remove().end().append('<option value="">Select Range</option>');

        GetAJAXContent(

				$j(this).val(),
        // Callback method for results.
				function(ContentArray) {

				    if (ContentArray.SubCats.length > 0) {

				        selectorThree.removeAttr("disabled")

				        for (var idxItem = 0; idxItem < ContentArray.SubCats.length; idxItem++) {
				            selectorThree.append($j("<option></option>").attr("value", ContentArray.SubCats[idxItem].id).attr("path", ContentArray.SubCats[idxItem].path).text(ContentArray.SubCats[idxItem].title));
				        }
				    }

				},
				'/ajaxhandlers/SubCategoryHandler.ashx'
				)
    });

    imageGo.click(function() {

        if (!selectorThree.attr('disabled')) {
            var selectedOption = selectorThree.find('option:selected');

            if (selectedOption && selectedOption.attr('path'))
                window.location.href = selectedOption.attr('path');
        }
        return false;
    });


});



function GetAJAXContent(inputValue, fnCallback, requestUrl) {

    var data = null;
    $j.getJSON(requestUrl,
          { data: inputValue },
          function(data) {

              fnCallback(data);

          });
      }

