//This will work when the select box ctlCategory is changed and Jquery.js is included on the page
$(function(){
    $("select#ctlChannel").change(function(){
            //Set all orther select boxes to hidden
            var sel1=document.getElementById('ctlCategory');
            sel1.style.visibility="hidden";
            sel1.value='';
            var sel3=document.getElementById('ctlProduct');
            sel3.style.visibility="hidden";
            sel3.value='';
            var sel=document.getElementById('ctlChannel');
            var button=document.getElementById('ask_button');
            var q_list=document.getElementById('question_list');
            //If movies Music or Videos Games is selected don't populate any more boxes
            if(sel.selectedIndex!=0 && $('select#ctlChannel').val()!=5 && $('select#ctlChannel').val()!=6 && $('select#ctlChannel').val()!=7)
            {
            //Set the subcategory(subchannel) drop down visible
            var sel1=document.getElementById('ctlsubChannel');
            sel1.style.visibility="visible";
            button.style.top='-70px';
            q_list.style.top='-70px';
            //If Mobile isn't selected then get the subcategory items in a JSON format from qa_getSub.php
            if($('select#ctlChannel').val()!=19){
            $.getJSON("qa_getSub.php",{id: $(this).val()}, function(j){
                    var options = '';
                    //Set the inital two options
                    options='<option value="0">Select a Subcategory (Optional)</option>';
                    options+='<option value="0">None</option>';
                    //Populate the options from the json
                    for (var i = 0; i < j.length; i++) {
                            options += '<option value="' + j[i].id + '">' + j[i].display_name + '</option>';
                    }
                    //Set the options to the subcategory dropdown
                    $("#ctlsubChannel").html(options);
                    $('#ctlsubChannel option:first').attr('selected', 'selected');
            
            })
            }
            else{
                    //If Mobile is selected poplulate the Mobile Categories
                     $.getJSON("qa_getSub.php",{id: $(this).val(),mobile:1}, function(j){
                    var options = '';
                    //Set the inital two options
                    options='<option value="0">Select a Subcategory (Optional)</option>';
                    options+='<option value="0">None</option>';
                    //Populate the options from the json
                    for (var i = 0; i < j.length; i++) {
                            options += '<option value="' + j[i].id + '">' + j[i].name + '</option>';
                    }
                    //Set the options to the subcategory dropdown
                    $("#ctlsubChannel").html(options);
                    $('#ctlsubChannel option:first').attr('selected', 'selected');
            
            })
            }
    }
    else
    {
        //IF movies, music or games or nothing is selected hide other drop downs
            var sel1=document.getElementById('ctlsubChannel');
            sel1.style.visibility="hidden";
            sel1.value='';
            var sel2=document.getElementById('ctlCategory');
            sel2.style.visibility="hidden";
            sel2.value='';
            var sel3=document.getElementById('ctlProduct');
            sel3.style.visibility="hidden";
            sel3.value='';
            button.style.top='-90px';
            q_list.style.top='-90px';
            
    }
    })			
    })
    
$(function(){
    $("select#ctlsubChannel").change(function(){
            var sel3=document.getElementById('ctlProduct');
            sel3.style.visibility="hidden";
            sel3.value='';
            var sel=document.getElementById('ctlsubChannel');
            var button=document.getElementById('ask_button');
            var q_list=document.getElementById('question_list');
            if(sel.selectedIndex!=0 && sel.selectedIndex!=1)
            {
            var sel1=document.getElementById('ctlCategory');
            sel1.style.visibility="visible";
             button.style.top='-35px';
            q_list.style.top='-35px';
            if($('select#ctlChannel').val()!=19)
            {
                $.getJSON("qa_getSub.php",{id:$('select#ctlChannel').val() ,subcategory:$(this).val()}, function(j){
                        var options = '';
                        options='<option value="0">Select a Site (Optional)</option>';
                        options+='<option value="0">None</option>';
                        for (var i = 0; i < j.length; i++) {
                                options += '<option value="' + j[i].id + '">' + j[i].name + '</option>';
                        }
                        $("#ctlCategory").html(options);
                        $('#ctlCategory option:first').attr('selected', 'selected');
                
                })
            }
            else
            {
                 $.getJSON("qa_getSub.php",{id:$('select#ctlChannel').val() ,parent_id:$(this).val()}, function(j){
                        var options = '';
                        options='<option value="0">Select a Site (Optional)</option>';
                        options+='<option value="0">None</option>';
                        for (var i = 0
                             ; i < j.length; i++) {
                                options += '<option value="' + j[i].id + '">' + j[i].name + '</option>';
                        }
                        $("#ctlCategory").html(options);
                        $('#ctlCategory option:first').attr('selected', 'selected');
                
                })
            }
    }
    else
    {
            var sel1=document.getElementById('ctlCategory');
            sel1.style.visibility="hidden";
            sel1.value='';
            var sel3=document.getElementById('ctlProduct');
            sel3.style.visibility="hidden";
            sel3.value='';
            button.style.top='-70px';
            q_list.style.top='-70px';
    }
    })			
    })

$(function(){
    $("select#ctlCategory").change(function(){
            var sel=document.getElementById('ctlCategory');
            var button=document.getElementById('ask_button');
            var q_list=document.getElementById('question_list');
            if(sel.selectedIndex!=0 && sel.selectedIndex!=1)
            {
            var sel1=document.getElementById('ctlProduct');
            sel1.style.visibility="visible";
             button.style.top='-5px';
            q_list.style.top='-5px';
            if($('select#ctlChannel').val()!=19)
            {
                $.getJSON("qa_getSub.php",{id:$('select#ctlChannel').val() ,subcategory:$('select#ctlsubChannel').val() ,site:$(this).val()}, function(j){
                        var options = '';
                        options='<option value="0">Select a Product (Optional)</option>';
                        options+='<option value="0">None</option>';
                        
                        for (var i = 0; i < j.length; i++) {
                                options += '<option value="' + j[i].product_id + '">' + j[i].name + '</option>';
                        }
                        $("#ctlProduct").html(options);
                        $('#ctlProduct option:first').attr('selected', 'selected');
                
                })
            }
            else
            {
                $.getJSON("qa_getSub.php",{id:$('select#ctlChannel').val() , mobile_category:$(this).val()}, function(j){
                        var options = '';
                        options='<option value="0">Select a Product (Optional)</option>';
                        options+='<option value="0">None</option>';
                        
                        for (var i = 0; i < j.length; i++) {
                                options += '<option value="' + j[i].product_id + '">' + j[i].name + '</option>';
                        }
                        $("#ctlProduct").html(options);
                        $('#ctlProduct option:first').attr('selected', 'selected');
                
                })
            }
    }
    else
    {
            var sel1=document.getElementById('ctlProduct');
            sel1.style.visibility="hidden";
            sel1.value='';
             button.style.top='-35px';
             q_list.style.top='-35px';
    }
    })			
    })