﻿

var SearchForm = { 

    loaded: false,

    countryId: -1,
    provinceId: -1,
    regionId: -1,
    cityId: -1,
      
    load: function() { 
        // set the countries select box to the right value
        
        if( this.loaded ) return;
        
        if( this.countryId >= 0 ) { 
         
            var countries = $('advanced_ccd');   
             
            this.setSelect( countries, this.countryId );                      
            this.countryChanged( countries );
        }
            
        if( this.provinceId >= 0 ) {        
            var provinces = $('advanced_pid');
            this.setSelect( provinces, this.provinceId );
            this.provinceChanged( provinces );
        }
                
        if( this.regionId >= 0 ) {        
            var regions = $('advanced_rid');
            this.setSelect( regions, this.regionId );
            this.regionChanged( regions );
        }                     
                
        if( this.cityId >= 0 ) { 
            this.setSelect( $('advanced_cid'), this.cityId );
        }  
        
        this.loaded = true;
      }, 
      
      
      HH_SF: function(objSelect){

        if(objSelect.options[ objSelect.options.selectedIndex].text == 'heden'){
          document.forms['yearSF']._a.value = 'lcurper';
          
        }else if(objSelect.options[ objSelect.options.selectedIndex].text == 'nieuw deze week'){
             document.forms['yearSF']._a.value = 'lnewper';
        }
      },   
      
      setSelect: function( objSelect, iValue ) { 
         
            for( var i=0; i<objSelect.options.length; i++ ) { 
                   
                if ( objSelect.options[i].value == iValue ) { 
                    objSelect.selectedIndex = i;
                    return true;
                }
            }
            
            // didn't find 
            return false;

      },
    
      countryChanged: function( objCountries ) {
            
            var form = objCountries.form;
            objProvinces = form.pid;
            objRegions = form.rid;
            
            linkCountriesToProvinces( objCountries, objProvinces );
            
            var countryId = $F('advanced_ccd');
            
            if( objProvinces.options.length == 2 ) { 
                objProvinces.selectedIndex = 1;            
                this.provinceChanged( objProvinces );
            }
            else {
                this.noProvinceChosen( form, countryId );
            }
            
            this.chooseDisplay( objProvinces, "provinceTR" );
            this.setProvinceLabel( countryId );
            
            if( !countryHasRegions( countryId ) ) { 
                Element.hide( "regionTR" );
            }
            else { 
                Element.show( "regionTR" );
            }
            
    }, 
    
    chooseDisplay: function( objSelect, containerId ) { 
        
        if( objSelect.options[ objSelect.options.selectedIndex].text.charAt(0) == '[' ) {
            Element.hide( containerId );
        }
        else {
            Element.show(containerId);
        }              
    },
    
    noProvinceChosen: function ( form, countryId ) { 
    
        var optionText = "(kies een " + this.getProvinceLabel( countryId ) + ")"; 
        
        form.rid.options.length = 0;
        form.rid.options[0] = new Option( optionText, "" );
    
        form.cid.options.length = 0;        
        form.cid.options[0] = new Option( optionText, "" );
    },
    
    getProvinceLabel: function( countryId ) { 
        var provinceLabel = 'provincie';        
               
        if( countryId == 153 || countryId == 102 ) { 
            // Nederlandse Antillen or Indonesië
            provinceLabel = 'eiland';
        }
        else if( countryId == 202 ) { 
            // Suriname
            provinceLabel = 'district';
        }
        else if( countryId == 225 ) { 
            // Verenigde Staten
            provinceLabel = 'staat';
        }
        
        return provinceLabel;
    },
    
    setProvinceLabel: function( countryId ) {       
        $('provinceLabel').innerHTML = this.getProvinceLabel( countryId );
    },
    
    provinceChanged: function( objProvinces ) { 
        objCities = objProvinces.form.cid;
        objRegions = objProvinces.form.rid;
        
        linkProvincesToRegions( objProvinces, objRegions );
        linkProvincesToCities( objProvinces, objCities );
         
        if( objRegions.options.length == 2 ) { 
            objRegions.selectedIndex = 1; 
        } 
         
        if( objCities.options.length == 2 ) { 
            objCities.selectedIndex = 1;
        }       
    },
    
    regionChanged: function( objRegions ) { 
        
        var regionId = objRegions.options[ objRegions.selectedIndex ].value;
        
        objCities = objRegions.form.cid;
        
        if( regionId == -1 ) { 
            objProvinces = objRegions.form.pid;
            linkProvincesToCities( objProvinces, objCities );
        }
        else { 
            linkRegionsToCities( objRegions, objCities ); 
        }
        
        if( objCities.options.length == 2 ) { 
            objCities.selectedIndex = 1;
        }
    
    }
    
}

var SearchTab = {

    onTabClick: function( anchorObject ) {
        
        if( $F('terms') != ''){ 
            var url = anchorObject.href + "?" + Form.serialize('search_form'); 
             window.location.href = anchorObject.href + "?" + Form.serialize('search_form'); 
             return false;
        }
    }
    
}
