$().ready(function(){
    //JobSearchCriteria.RegisterAjaxCombos();
    JobSearchCriteria.init();//RegisterAjaxCombos();
});

var Region="0";


JobSearchCriteria= {
    
   init: function(){
       JobSearchCriteria.setEventHandlers(); 
       JobSearchCriteria.setEventHandlerPredicitive();
   },


   //Unbind ASP.NET change events. Add new Ajax Events.    
   setEventHandlers: function(){
    $(".mainActivityDomain select").unbind().change(JobSearchCriteria.ActivityDomainChange);  
    $(".mainRegion select").unbind().change(JobSearchCriteria.RegionChange);
    $(".btnSearch").unbind().click(JobSearchCriteria.Search);  
   }, 
   
   //Attach EventHandler to TextBoxField
   setEventHandlerPredicitive:function(){
        //ID es oTextFieldPredictive
        $(".Predictive").unbind().keypress(JobSearchCriteria.predictive);
        $(".Predictive").attr("disabled",true);
   },
   
   predictive: function(){
        
        var options = {
	    script: "../../../CAPPages/AutoSuggest.aspx?Region="+Region+"///",
	    varname: oTextFieldPredictive,
	    json: false,
	    maxresults: 10,
	    callback: function (obj) { $(".townSelectedId")[0].value = obj.id;}
	    };
    var as= new bsn.AutoSuggest(oTextFieldPredictive, options);
   },
   
   onPredictiveReceived: function(){
   },
   
   hideDepartments: function(){
        $(".secondRegion").children("input, select").each(function(){ this.disabled=true;});
   },
   
   hideSecondActivityDomain: function(){
        $(".secondActivityDomain").children("input, select").each(function(){ this.disabled=true;});
   },
   
   showDepartments: function(){
        $(".secondRegion").children("input, select").each(function(){ this.disabled=false;});
   },
   
   showSecondActivityDomain: function(){
        $(".secondActivityDomain").children("input, select").each(function(){ this.disabled=false;});
   },
    
   ActivityDomainChange:function(){
                JobSearchCriteria.hideSecondActivityDomain();
		        optDiv=$(".secondActivityDomain");
		        optSelect=optDiv.children("select");
		        if(this.options[this.selectedIndex].value.length>0){
		            /*Load job category dropdownlist*/
  	                WSK.OffAng.ClientServices.JobOfferServices.GetCategories(this.options[this.selectedIndex].value,JobSearchCriteria.onJobCategoriesReceived.bind(this));
		            optDiv.animate({opacity:1},500);											    
		            optDiv.show(500);
			        optSelect.show(500);													
		        }else{
			        optDiv.hide(500);
			        optSelect[0].selectedIndex=0;
		        }
          
	       },

   RegionChange:function(){	
                JobSearchCriteria.hideDepartments();
			    optDiv=$(".secondRegion");
			    optSelect=optDiv.children("select");
        		if((this.options[this.selectedIndex].value<=0)&&($(".JSDepartment").is(":visible")==true))
    			    $(".Predictive").attr("disabled",true);
                else if(this.options[this.selectedIndex].value.length>0){
			            /*Load department dropdownlist*/
    			        Region=this.options[this.selectedIndex].value;
			            WSK.OffAng.ClientServices.JobOfferServices.JSGetDepartments(this.options[this.selectedIndex].value,JobSearchCriteria.onRegionReceived.bind(this));               			    
			            optDiv.animate({opacity:1},500);											    
			            if($(".secondRegion").is(":visible")==true){
			                optDiv.show(500);
				            optSelect.show(500);						
				        }							
			        }else
			        {
				        optDiv.hide(500);
				        optSelect[0].selectedIndex=0;
				     
    			    }
     },
      
    onRegionReceived: function(res){
       
       optSelect=$(".JSDepartment");
       optSelect.html(res.value);
       optSelect.attr("contentHTML",optSelect.get(0).outerHTML);
       JobSearchCriteria.showDepartments();
       $(".Predictive").attr("disabled",false);
    },
      
    onJobCategoriesReceived: function(res) {
          optSelect=$(".JSJobCategory");
          optSelect.html(res.value);
          optSelect.attr("contentHTML",optSelect.get(0).outerHTML);   
          JobSearchCriteria.showSecondActivityDomain();
      },
      
      
    Search:function(){
        var _aux=""; 
        var _str="";
        var _strCnt="";
        
       
        /*Getting values for each combo*/
        
        /*ActivityDomain*/
                
        _aux=$(".JSActivityDomain option:selected").val();//optSelActivityDomain.get(0).selectedIndex;
        if((_aux!=null)&&(_aux!=0))
            _str+="activitydomain="+_aux;
               

        
        /*Job Category*/
        
        _aux=$(".JSJobCategory option:selected").val();//optSeljobCategory.get(0).selectedIndex;
        if((_aux!=null)&&(_aux!=0)){
            if(_str.length>0)
              _str+="&";  
            _str+="jobcategory="+_aux;
         }
        
        /*Region*/
        
        _aux=$(".JSRegion option:selected").val();//optSelRegion.get(0).selectedIndex;
        if((_aux!=null)&&(_aux!=0)){
            if(_str.length>0)
              _str+="&";  
            _str+="region="+_aux;
         }
        
        
        /*Departments*/
        
        /*In Phase 3 : if(!$("JSDepartment").is(:visible)){_aux=$(".Predictive").val();} else.... */
        if($(".JSPredictive").is(":visible")==true)
           //_aux=$(".Predictive").val();
           _aux=$(".townSelectedId").val();
        else
            _aux=$(".JSDepartment option:selected").val();//optSelDepartment.get(0).selectedIndex;
        if((_aux!=null)&&(_aux!=0)){
              if(_str.length>0)
                _str+="&";  
              _str+="department="+_aux;
         }
        
        /*DatePublicated*/
        
        _aux=$(".JSDatePublication option:selected").val();//optSelDepartment.get(0).selectedIndex;
        if((_aux!=null)&&(_aux!=0)){
            if(_str.length>0)
              _str+="&";  
            _str+="publicationDate="+_aux;
         }
        
        /*ContractTypes*/
        
        if((document.getElementById(ocb0).checked==true)||
            (document.getElementById(ocb1).checked==true)||
            (document.getElementById(ocb2).checked==true)){
              if(_str.length>0)
              _str+="&";  
            _str+="contractTypes=";
        
        }
        
        if(document.getElementById(ocb0).checked==true){
            _strCnt+="9"; /*Contract*/
        }
        if(document.getElementById(ocb1).checked==true){
             if(_strCnt.length>0)
              _strCnt+="+";  
            _strCnt+="8"; /*Permanent*/
        }
        if(document.getElementById(ocb2).checked==true){
           if(_strCnt.length>0)/*Temporary*/
              _strCnt+="+";  
            _strCnt+="10"; 
        }
       
       if(_strCnt!="")
          _str+=_strCnt;
        

        
        _aux=$(".txtKeywords").val();
        
        if((_aux!="Insert your keywords here")&&(_aux!="")){
             if(_str.length>0)
              _str+="&";  
              _str+="keywords="+_aux;
        }
        if(_str.length!=0){
            WSK.OffAng.ClientServices.JobOfferServices.SearchResults(_str,resultsPage,JobSearchCriteria.BackResults.bind(this));    
        }
        else{
            alert("You must select at least one criteria");
        }
        
        return false;
   
    },
    BackResults:function(res){
        location.href=res.value;
    }
    
         
      
  }







