﻿/// <reference path="../Services/CatalogServices.js" />

//dynamic attempt
var sDDLsID = "divDDLs";

function ShowCategoryDDLs() {
    GetCategories(GetCategoriesSucceededCallback, null);
}


function GetCategoriesSucceededCallback(result) {
    //alert('GetCategoriesSucceededCallback');
    MakeCategoryDDLs(result);
    
}

function MakeCategoryDDLs(categories) {
    var divDDLs = document.getElementById(sDDLsID);
    var ddl = document.createElement('select');    
    for (var i=0; i < categories.length; i++){
        //alert(categories[i]);
        var option = document.createElement('option');
        option.setAttribute('value', categories[i].ID);
        option.text = categories[i].Name;
        ddl.appendChild(option);
        ddl.setAttribute('onchange', 'ShowDDL(this, this.value)');
        if (categories[i].Children != null && categories[i].Children.length > 0) {
            MakeCategoryDDL(categories[i].Children, categories[i].ID);
        }
    }
    divDDLs.appendChild(ddl);
}

function MakeCategoryDDL(categories, parentID) {
    var divDDLs = document.getElementById(sDDLsID);
    var ddl = document.createElement('select');
    ddl.setAttribute('id', parentID);
    ddl.style.display = 'none';
    for (var i = 0; i < categories.length; i++) {
        //alert(categories[i]);
        var option = document.createElement('option');
        option.setAttribute('value', categories[i].ID);
        option.text = categories[i].Name;
        ddl.appendChild(option);
    }
    divDDLs.appendChild(ddl);
}

function ShowDDL(ddl, id) {
    var childDDL = document.getElementById(id);
    for (var i = 0; i < ddl.options.length; i++) {
        HideDDL(ddl.options[i].id);
    }
    if (childDDL != null)
        childDDL.style.display = 'block';
    
}
function HideDDL(id) {
    var ddl = document.getElementById(id);
    if (ddl != null)
        ddl.style.display = 'none';
}

//static attempt
var statPrepend = 'ctl00_cphBody_';
function ResetCategories(thisDDL) {
    var rootCategoriesActive = document.getElementById(statPrepend + 'rootCategoriesActive');
    rootCategoriesActive.selectedIndex = 0;
    ShowSubCats(rootCategoriesActive);

    var rootCategoriesDiscontinued = document.getElementById(statPrepend + 'rootCategoriesDiscontinued');
    rootCategoriesDiscontinued.selectedIndex = 0;
    ShowSubCats(rootCategoriesDiscontinued);
}
function ShowSubCats(thisDDL, thisStatus) {
    var thisOption;
    var subCatDDL;
    //alert(thisDDL.options.length);
    //alert(thisStatus);
    for (var i = 0; i < thisDDL.options.length; i++) {
        thisOption = thisDDL.options[i];
        subCatDDL = document.getElementById(statPrepend + thisStatus + '_subcat' + thisOption.value);
        if (subCatDDL != null) {
            if (thisOption.selected) {
                //alert(thisOption.value);
                subCatDDL.style.display = 'block';
            } else {
                subCatDDL.style.display = 'none';
                for (var ii = 0; ii < subCatDDL.options.length; ii++) {
                    prodOption = subCatDDL.options[ii];
                    prodDDL = document.getElementById(statPrepend + thisStatus + '_prod' + prodOption.value);
                    if (prodDDL != null) {
                        prodDDL.style.display = 'none';                        
                    }
                }
            }
        }
            
    }
}
function ShowProducts(thisDDL, thisStatus) {
    var thisOption;
    var prodDDL;
    //alert(thisStatus);
    for (var i = 0; i < thisDDL.options.length; i++) {
        thisOption = thisDDL.options[i];
        prodDDL = document.getElementById(statPrepend + thisStatus + '_prod' + thisOption.value);
        if (prodDDL != null) {
            if (thisOption.selected) {
                prodDDL.style.display = 'block';
            } else {
                prodDDL.style.display = 'none';
            }
        }
    }
}

function SelectProduct(sHDF, thisDDL) {
    var thisHDF = document.getElementById(sHDF);
    //alert(thisHDF.value);
    thisHDF.value = thisDDL.options[thisDDL.selectedIndex].text;
}
