﻿var hiddenFieldID;
function getCatalogueChildNumber(selectedRadio, selectedDropdown) {
    var result;
    for (i = 0; i < catalogueDictionary[selectedRadio].ArrayOfChildEntries.length; i++) {

        if (catalogueDictionary[selectedRadio].ArrayOfChildEntries[i].ID == selectedDropdown) {
            result = i;
            return result;
        }

    }
}

function getCatalogueNumber(selectedRadio) {
    var result;
    for (i = 0; i < catalogueDictionary.length; i++) {

        if (catalogueDictionary[i].ID == selectedRadio) {
            result = i;
            return result;
        }

    }
}

function fillDropDowns2(selectedRadio, selectedDropdown) {
    //if ($("input[name='D1']:checked").val() == 'V1')
    $("select[name='D2']").find('option').remove().html('<option></option>');
    $('#Select2').append(
        $('<option name="dropdown2" disabled>' + emptyMessage + '</option>'));
    var childNumber = getCatalogueChildNumber(selectedRadio, selectedDropdown);
    // } else if ($("input[name='D1']:checked").val() == 'V2') {
    $.each(catalogueDictionary[selectedRadio].ArrayOfChildEntries[childNumber].ArrayOfChildEntries, function(key, value) {
        $('#Select2').append(
        $('<option name="dropdown2"></option>').val(value.ID).html(value.Translation));
    });
}

function fillDropDowns1(selectedRadio) {
    //if ($("input[name='D1']:checked").val() == 'V1')
    
    $("select[name='D2']").find('option').remove().html('<option></option>');

    $("select[name='D1']").find('option').remove().html('<option></option>');
    
   
    $('#Select1').append(
        $('<option name="dropdown1" disabled>' + emptyMessage + '</option>'));
    $('#Select2').append(
        $('<option name="dropdown2" disabled>' + emptyMessage + '</option>'));
        
    var number;
    number = getCatalogueNumber(selectedRadio);
    $.each(catalogueDictionary[number].ArrayOfChildEntries, function(key, value) {
        $('#Select1').append(
        $('<option name="dropdown1"></option>').val(value.ID).html(value.Translation).attr('checkBoxID', number));
    });
    

}

function fillHiddenField(selectedValue) {
    hiddenFieldID = searchResultPage;
    if (selectedValue) {
        hiddenFieldID = searchResultPage + "?EntryId=" + selectedValue + "&doSearch=true";
    }
    document.getElementById(uniqueID).href = hiddenFieldID;
    
    
    //$('#hiddenValue').val = selectedValue;
       
}

$(document).ready(function() {
    fillHiddenField(queryEntryId);

   // document.getElementById(uniqueID).href = searchResultPage;
    $('#Select1').append(
        $('<option name="dropdown1" disabled>' + emptyMessage + '</option>'));
    $('#Select2').append(
        $('<option name="dropdown1" disabled">' + emptyMessage + '</option>'));

    $("#radiobuttons").change(function() {
        var selectedRadio = $("input[@name=radiocheck]:checked").val();
        // Clear all the option values of the dropdowns
        fillDropDowns1(selectedRadio);
        fillHiddenField(selectedRadio);
        $("#productSearchControl .customStyleSelectBox").remove();
        $("#Select1").customStyle();
        $("#Select2").customStyle();
        //alert($("input[@name=radiocheck]:checked").val());          
    });

    $("#OptionSelector").change(function() {
        //alert($("#Select1").val());
        var selectedRadio = ($(this).find("option[@name=checkBoxDropdown]:selected").val());
        fillDropDowns1(selectedRadio);
        fillHiddenField(selectedRadio);
        $("#productSearchControl .customStyleSelectBox").remove();
        $("#Select1").customStyle();
        $("#Select2").customStyle();
        $(this)
    });

    $("#Select1").change(function() {
        var selectedRadio = ($(this).find("option[@name=dropdown1]:selected").attr("checkBoxID"));
        var selectedDropdown = ($(this).find("option[@name=dropdown1]:selected").val());
        fillDropDowns2(selectedRadio, selectedDropdown);
        fillHiddenField(selectedDropdown);
        $("#productSearchControl .customStyleSelectBox").remove();
        $("#Select1").customStyle();
        $("#Select2").customStyle();
    });
    $("#Select2").change(function() {
        var selectedDropdown = ($(this).find("option[@name=dropdown2]:selected").val());
        fillHiddenField(selectedDropdown);
    });
    if (catalogueDictionary.length <= 3) {
        $("#radioDropdown").hide();
        $.each(catalogueDictionary, function(key, value) {
            $("#radiobuttons").append($('<input type="radio" name="radiocheck" />').val(value.ID));
            $("#radiobuttons").append('<label>' + value.Translation + '</label>');

        });
        //$("#radiobuttons input").first().attr('checked', true);
    }
    else {
        $("#radioDiv").hide();

        $.each(catalogueDictionary, function(key, value) {
            $("#OptionSelector").append($('<option name="checkBoxDropdown"></option>').val(value.ID).html(value.Translation));
        });
    }
    if (queryEntryId != "") {
        var chkbox1 = "";
        var ddbox1 = "";
        var ddbox2 = "";
        var cataloguelvl1;
        var cataloguelvl2;
        $.each(catalogueDictionary, function(key, value) {
            cataloguelvl1 = getCatalogueNumber(value.ID);
            if (value.ID == queryEntryId) {
                chkbox1 = value.ID;
            }
            else {
                $.each(catalogueDictionary[cataloguelvl1].ArrayOfChildEntries, function(key, value) {
                    cataloguelvl2 = getCatalogueChildNumber(cataloguelvl1, value.ID);
                    if (value.ID == queryEntryId) {
                        chkbox1 = catalogueDictionary[cataloguelvl1].ID;
                        ddbox1 = value.ID;

                    }
                    else {
                        $.each(catalogueDictionary[cataloguelvl1].ArrayOfChildEntries[cataloguelvl2].ArrayOfChildEntries, function(key, value) {
                            if (value.ID == queryEntryId) {
                                chkbox1 = catalogueDictionary[cataloguelvl1].ID;
                                ddbox1 = catalogueDictionary[cataloguelvl1].ArrayOfChildEntries[cataloguelvl2].ID;
                                ddbox2 = value.ID;
                            }
                        });
                    }
                });
            }
        });
        if (ddbox1 == "" && ddbox2 == "") {
            if (catalogueDictionary.length <= 3) {
                $("input").filter("[value=" + chkbox1 + "]").attr("checked", true);
                fillDropDowns1(chkbox1);
            }
            else {
                $("option").filter("[value=" + chkbox1 + "]").attr("selected", true);
                fillDropDowns1(chkbox1);
            }
        }
        else if (ddbox1 != "" && ddbox2 == "") {
            if (catalogueDictionary.length <= 3) {
                $("input").filter("[value=" + chkbox1 + "]").attr("checked", true);
                fillDropDowns1(chkbox1);
                $("option").filter("[value=" + ddbox1 + "]").attr("selected", true);
                fillDropDowns2(getCatalogueNumber(chkbox1), ddbox1);
            }
            else {
                $("option").filter("[value=" + chkbox1 + "]").attr("selected", true);
                fillDropDowns1(chkbox1);
                $("option").filter("[value=" + ddbox1 + "]").attr("selected", true);
                fillDropDowns2(getCatalogueNumber(chkbox1), ddbox1);
            }
        }
        else if (ddbox1 != "" && ddbox2 != "") {
            if (catalogueDictionary.length <= 3) {
                $("input").filter("[value=" + chkbox1 + "]").attr("checked", true);
                fillDropDowns1(chkbox1);
                $("option").filter("[value=" + ddbox1 + "]").attr("selected", true);
                fillDropDowns2(getCatalogueNumber(chkbox1), ddbox1);
                $("option").filter("[value=" + ddbox2 + "]").attr("selected", true);
            }
            else {
                $("option").filter("[value=" + chkbox1 + "]").attr("selected", true);
                fillDropDowns1(chkbox1);
                $("option").filter("[value=" + ddbox1 + "]").attr("selected", true);
                fillDropDowns2(getCatalogueNumber(chkbox1), ddbox1);
                $("option").filter("[value=" + ddbox2 + "]").attr("selected", true);
            }
        }
    }

});
