﻿// JavaScript File
Entegris.ProductCatalog.ProductSearch = function( IndustryData, ApplicationData, IndustrydropdownID, ApplicationdropdownID, Url,SiteId, IsBlueBox,LanguageId)
{
    this.selectedApplicationId  = 0;
    this.selectedProcessStepId  = 0;
    this.selectedIndustryId     = 0;
    this.selectedkeyword ='';
    this.url = Url;
    this.siteId = SiteId;
    this.IsBlueBox = IsBlueBox;
    this.LangugageId = LanguageId;
    this.SearchDirections = Ext.get('SearchDirections');
   
   
    var applicationStore = new Ext.data.SimpleStore({'id': 0,fields: ['value', 'text', 'class', 'type'],data : ApplicationData});
    
    this.ApplicationFilter = new Ext.form.ComboBox({
        tpl: '<div class="x-combo-list-item"><div class="{class}">{text}</div></div>',
        store: applicationStore,
        typeAhead: false, 
        width:160,
        listWidth: 275,
        forceSelection:true,
        editable: false,
        mode: 'local',
        valueField: 'value',
        displayField: 'text' ,
        transform:ApplicationdropdownID 
    });
   this.ApplicationFilter.on('select', this.onApplicationProcessSelect, this);
   this.ApplicationFilter.on('beforequery', this.onBeforeQuery, this);
   this.ApplicationFilter.setValue('ALL');
    
    var Industrystore = new Ext.data.SimpleStore({
        'id': 0,
        fields: ['value', 'text', 'class', 'type'],
        data : IndustryData
    });
    
    this.IndustryFilter = new Ext.form.ComboBox({
        store: Industrystore ,
        typeAhead: false,
        transform: IndustrydropdownID,
        width:175,
        listWidth: 175,
        forceSelection:true,
        editable: false,
        mode: 'local',
        valueField: 'value',
        displayField: 'text', 
        tpl: '<div class="x-combo-list-item"><div class="{class}">{text}</div></div>' 
    });

   this.IndustryFilter.on('select', this.onIndustrySelect, this);
   this.IndustryFilter.on('beforequery', this.onBeforeQuery, this);
   this.IndustryFilter.setValue('ALL');
   
   this.textKeyword= new Ext.form.TextField({
                fieldLabel: '',
	            name: 'keyword',
	            width:150,
	            allowBlank:false,
	            invalidText:'Keyword or product name is required'
                });
       
   this.textKeyword.applyTo(Ext.get("txtKeyword"));
   
   //this.btnSearch = new Ext.Button(Ext.get("search"),{text: "Search", scope:this,tooltip:"Search",type:"button", cls:"ProductSearchButton"});
   Ext.get(Ext.select('.SearchButton').elements[0]).on('click',this.onSearchProductsClick,this,{});
   
   
   
    this.selectedApplicationId = C4.Utility.QueryString.get('A', 0);
    this.selectedProcessStepId = C4.Utility.QueryString.get('P', 0);
    this.selectedIndustryId = C4.Utility.QueryString.get('I', 0);
    this.selectedkeyword = C4.Utility.QueryString.get('K', '');
    
    if(this.selectedApplicationId > 0 ) 
        this.ApplicationFilter.setValue(this.selectedApplicationId);
    else if (this.selectedProcessStepId > 0 )
        this.ApplicationFilter.setValue(this.selectedProcessStepId);
    
    if(this.selectedIndustryId > 0 ) 
        this.IndustryFilter.setValue(this.selectedIndustryId );
    
    this.textKeyword.setValue(this.selectedkeyword);
            
    this.textKeyword.on('specialkey', this.onSpecialKey, this);
    this.IndustryFilter.on('specialkey', this.onSpecialKey, this);
    this.ApplicationFilter.on('specialkey', this.onSpecialKey, this);
    
            
   
}
    
Ext.extend(Entegris.ProductCatalog.ProductSearch, Ext.util.Observable, {
    
   onBeforeQuery: function(combo, e)
   {
    combo.forceAll = true;
    combo.query="";
   },   
   
   
    onSpecialKey: function(field, e) {
        if(e.getKey() == e.ENTER){
            this.onSearchProductsClick();
        }
    },
    
   onApplicationProcessSelect: function(combo, record, index) {
        var applicationItemID = 0;
        var processStepItemID = 0;
        
        if (record.data.type == 'Application') {
            applicationItemID = record.data.value;
        }
        if (record.data.type == 'ProcessStep') {
            processStepItemID = record.data.value;
        }
        this.selectedApplicationId = applicationItemID;
        
        this.selectedProcessStepId = processStepItemID;
    },
     onIndustrySelect: function(combo, record, index) {
        var industryItemID = 0;
        
        if (record.data.type == 'Industry') {
            industryItemID = record.data.value;
        }
        this.selectedIndustryId = industryItemID;
        
    },
    onSearchProductsClick:function()
    {
        this.selectedApplicationId = this.selectedApplicationId == undefined ? 0 : this.selectedApplicationId;
        this.selectedProcessStepId = this.selectedProcessStepId == undefined ? 0 : this.selectedProcessStepId;
        this.selectedIndustryId = this.selectedIndustryId == undefined ? 0 : this.selectedIndustryId;
        
        
        var destinationUrl= "";
        if( !this.textKeyword.validate() && this.selectedApplicationId == 0 && this.selectedProcessStepId == 0 && this.selectedIndustryId == 0){
            // make directions red
            this.SearchDirections.addClass('invalid');
            
            this.textKeyword.focus(true);
            return;
        }
        if ( this.url.indexOf("/") == 0){
            destinationUrl = destinationUrl + this.url;
        }
        else{
            destinationUrl = "/" + this.url;
        }

        if ( this.url.indexOf("?") <= 0 ){
            destinationUrl = destinationUrl + "?";
        }
        

        destinationUrl = destinationUrl + "&A=" + this.selectedApplicationId;
        destinationUrl = destinationUrl + "&P=" + this.selectedProcessStepId;
        destinationUrl = destinationUrl + "&I=" + this.selectedIndustryId;
        destinationUrl = destinationUrl + "&K=" + this.textKeyword.getValue();
        location.href = destinationUrl;
    }
});
