var Support = {

    Manuals: {

        Search: {

            validate: function(event){
                var v = new Validator(event);
                var opt = ["serialnumber", "product", "model"];
                if ( $("serno") && $("serno").visible() ){
                    opt = opt.without("serialnumber");
                }
                if ( $("product") && $("product").visible() ){
                    opt = opt.without("product");
                }
                if ( $("models") && $("models").visible() ){
                    opt = opt.without("model");
                }
                v.setOpt(opt);
                return v.validate();
            },

            validateSearchFor: function(event){
                var v = new Validator(event);
                return v.validateUnmatchedChecks($(Event.element(event).form).getInputs("checkbox"),
                                        "Please Select at least one Manual Type.");
            },

            SNVinSearch: function(){
                $("serno").show();
                $("product").hide();
                if ($("models") && $("models").visible()){
                    $("models").hide();
                }
            },

            handleProduct: function(value){
                value = (!value || typeof(value) == 'object')?$F("category"):value;
                doc_types = new Array;
                $$('input.check_choice').each(function(d){
                    if (d.checked){doc_types.push(d.value);}
                });
                if ($$('input.check_choice')[3].checked){
                    Support.Manuals.Search.displayEuropeanModels(value);    
                }else{
                    Support.Manuals.Search.displayModels(value);
                }

            },

            productSearch: function(){
                $("serno").hide();
                $("product").show();
                if ( !$F("category").empty() ){
                    Support.Manuals.Search.handleProduct();
                }
                $("category").observe("change", Support.Manuals.Search.handleProduct.bindAsEventListener(Support.Manuals.Search));
            },

            is_checked: function(e){
                if (Event.element(e).checked){
                    confirm('Selecting this choice will limit models to European only.');
                }
            },

            prepare: function(){
                $("manualSearch").observe("submit", Support.Manuals.Search.validate.bindAsEventListener(Support.Manuals.Search));
                $$('input.check_choice')[3].observe('click', Support.Manuals.Search.is_checked.bindAsEventListener(Support.Manuals.Search));
                $("button").observe("click", Support.Manuals.Search.validateSearchFor.bindAsEventListener(Support.Manuals.Search));
                $("reset").observe("click", Support.Manuals.Search.reset.bindAsEventListener(Support.Manuals.Search));
                $("vin").observe("click", Support.Manuals.Search.SNVinSearch.bindAsEventListener(Support.Manuals.Search));
                $("prod").observe("click", Support.Manuals.Search.productSearch.bindAsEventListener(Support.Manuals.Search));
                Support.Manuals.Search.preset();
            },

            preset: function(){
                if ($("prod").checked){
                    $("prod").click();
                    if ( !$F("category").empty() ){
                        Support.Manuals.Search.handleProduct();
                    }
                }

            },

            reset: function(event){
                $("category").options[0].selected = true;
                if ( $("models") && $("models").visible() ){
                   $("mdls").selectedIndex = 0;
                    $("models").hide();
                }
                $("prod").click();
                $("sn").clear();
                $A($("manualSearch").getInputs("checkbox")).each(function(i){
                    i.checked = false;
                });
            },

            modelSelect: function(modelNodeList){
                modelObjs = $A([Builder.node('option', {value:""}, 'Choose Your Model:')]);
                $A(modelNodeList).each(function(m){
                    opt = Builder.node('option', {value:m.firstChild.data}, m.firstChild.data);
                    if (m.firstChild.data == $F("selectedmodel")){
                        opt.selected="selected";
                    }
                    modelObjs.push(opt);
                });
                selectlist = Builder.node('select', {
                        id:"mdls",
                        name:"model",
                        size:1
                    }, [modelObjs]);

                selectlist.selectedIndex = 0;

                return selectlist;
            },

            displayModels: function(value){
                var url = '/common/techsupport/modelsByCategory.xml';
                var a = new Ajax.Request(url, {
                    parameters: {'category':value},
                    method: "get",
                    onSuccess: function(transport){
                        var response = transport.responseXML.documentElement;
                        if (response && response.hasChildNodes()){
                            var models = $("models");
                            var m = response.getElementsByTagName('models')[0];
                            var modelList = m.getElementsByTagName('model');
                            var selectList = Support.Manuals.Search.modelSelect(modelList);
                            try{
                                models.replaceChild(selectList, $('mdls'));
                            }catch(e){
                                models.appendChild(selectList);
                            }
                            models.show();
                        }
                    }
                });
            },
            
            displayEuropeanModels: function(value){
                var url = '/common/techsupport/euroModelsByCategory.xml';
                var a = new Ajax.Request(url, {
                    parameters: {'category':value},
                    method: "get",
                    onSuccess: function(transport){
                        var response = transport.responseXML.documentElement;
                        if (response && response.hasChildNodes()){
                            var models = $("models");
                            var m = response.getElementsByTagName('models')[0];
                            var modelList = m.getElementsByTagName('model');
                            var selectList = Support.Manuals.Search.modelSelect(modelList);
                            try{
                                models.replaceChild(selectList, $('mdls'));
                            }catch(e){
                                models.appendChild(selectList);
                            }
                            models.show();
                        }
                    }
                });
            }

        }
    }
}

