$(document).ready( function() {

  $("#country").change(
    function (){
      var oSel1=document.getElementById('region');
      if(!oSel1) {
         return false;
      }

      oSel1.length=0;
      oSel1[0]=new Option('Loading...',0);  
      
      
    
      $.get('/bare/georefs/regions/'+this[this.selectedIndex].value,function(xml){
        changeSelect(oSel1,xml.split("\n"));
      });
      
      var oSel2=document.getElementById('city');
      if(!oSel2) {
         return false;
      }

      oSel2.length=0;
      oSel2[0]=new Option('Loading...',0);
      
      $.get('/bare/georefs/citiesbycountry/'+this[this.selectedIndex].value,function(xml){
        changeSelect(oSel2,xml.split("\n"));
      });
      
      var oSel3=document.getElementById('area');
      if(!oSel3) {
         return false;
      }

      oSel3.length=0;
      oSel3[0]=new Option('Loading...',0);
      
    
      $.get('/bare/georefs/areasbycountry/'+this[this.selectedIndex].value,function(xml){
        changeSelect(oSel3,xml.split("\n"));
      });
      
     });


  $("#region").change(
    function (){
    
      if (this[this.selectedIndex].value != 0)
      {
          var oSel1=document.getElementById('city');
          if(!oSel1) {
             return false;
          }

          oSel1.length=0;
          oSel1[0]=new Option('Loading...',0);
          
          $.get('/bare/georefs/cities/'+this[this.selectedIndex].value,function(xml){
            changeSelect(oSel1,xml.split("\n"));
          });
          
          var oSel2=document.getElementById('area');
          if(!oSel2) {
             return false;
          }

          oSel2.length=0;
          oSel2[0]=new Option('Loading...',0);
          
        
          $.get('/bare/georefs/areasbyregion/'+this[this.selectedIndex].value,function(xml){
            changeSelect(oSel2,xml.split("\n"));
          });
      }
      else
      {
        document.getElementById('country').onchange();
      }
      
      
     });

  $("#city").change(
    function (){
    
    if (this[this.selectedIndex].value != 0)
    {
      var oSel1=document.getElementById('area');
      if(!oSel1) {
         return false;
      }

      oSel1.length=0;
      oSel1[0]=new Option('Loading...',0);
      
    
      $.get('/bare/georefs/areas/'+this[this.selectedIndex].value,function(xml){
        changeSelect(oSel1,xml.split("\n"));
      })
    }
    else
    {
            document.getElementById('region').onchange();
    }
    
     });

});
    
