
function searchDomicils (mode, key){

  $("input.search_key:first").val(mode);
  $("input.search_key:last").val(key);

};


function setAreaName (area){

	$.ajax({
		type: "get",	
		url: "regio.php",
		data: "imode=true&Gebiet=" + area ,	
		success: function(area_name) {
      $("input.region_key:last").val(area_name);
    }
	}); 

};



function browseAreaSelect(mode, key) {

  if (mode == null) mode=2;

  parameters = new Array("Land=", "Region=", "Gebiet=");
  parameter = parameters[mode] + key;
  
  if (mode == 1) parameter = parameter + "&Land=" + key.substr(0, 2);
  
  //alert (parameter);
  

  $.ajax({
  	type: "get",	
  	url: "regio.php",
    data: parameter ,
  
  	success: function(multi_select) {
  
      var select = multi_select.split("|");

  		//alert (select[1].length);
			
  		$("select#country-sel").html(select[0]);

      if (select[1].length > 4) {
        $("select#region-sel").html(select[1]);
        $("select#region-sel").show();
      }
      else {
        $("select#region-sel").hide();
      };
      if (select[2].length > 4) {
        $("select#area-sel").html(select[2]);
        $("select#area-sel").show();
      }
      else {
        $("select#area-sel").hide();
      };
  	}
  }); // Schließe $.ajax(      
};


$(document).ready(function(){

  if ($("#SelectGebiet").eq(0).is(":visible")){
        
    mode = $("input.search_key:first").val();
    key = $("input.search_key:last").val();
  
    browseAreaSelect(mode, key);

    //$("#SelectGebiet").show();  	
  };
  
  
  // im Dokument exitiert eine Input-Variable mit class=region_key und diese ist mit einem Wert belegt
	var area = $("input.region_key:first").val();
  if (area != null){
  	setAreaName (area);
  };
/*
  $("input.region_key").eq(1).click(function () {

    if ($("div.region_select").eq(0).is(":hidden")) {

      $("input.region_key:last").hide();    // ... und verberge das Input-Feld
      $("div.region_select").eq(0).fadeIn("slow");             // zeige die Region-Selection ... 

			browseAreaSelect(2, area);
			
    } else {
      $("div.region_select").eq(0).hide();
      $("input.region_key:last").fadeIn("slow");
    }
  });
*/



  //-----------------------------------------
  // Select-Liste für die Länder-Auswahl
  //-----------------------------------------
  $("select#country-sel").change(function () {

		var country = $('select#country-sel').val();
		

    $.ajax({

			type: "get",	
			url: "regio.php",
			data: "Land="+country ,	//  Parameter

			success: function(multi_select) {

        var select = multi_select.split("|");
				
				$("select#country-sel").html(select[0]);
        if (select[1].length > 4) {
          $("select#region-sel").html(select[1]);
          $("select#region-sel").show();
        }
        else {
          $("select#region-sel").hide();
        };
        $("select#area-sel").hide();
        
    		searchDomicils(0, country);
        //$("input.country_key:first").val(country);
    		//document.select.submit();
			}
		}); 
  });

  //-----------------------------------------
  // Select-Liste für die Regionen-Auswahl
  //-----------------------------------------
  $("select#region-sel").change(function () {

		$.ajax({

			type: "get",	
			url: "regio.php",
			data: "Land="+$('select#country-sel').val() + "&Region="+$('select#region-sel').val() ,	//  Parameter

			success: function(multi_select) {

        var select = multi_select.split("|");
        var area = $('select#region-sel').val();
				
				$("select#country-sel").html(select[0]);
        $("select#region-sel").html(select[1]);
        
        if (select[2].length > 4) {
          // zu der gewählten Region sind Feriengebiete hinterlegt
          $("select#area-sel").html(select[2]);
          $("select#area-sel").show();

          searchDomicils(1, area);
          //$("input.parent_key:first").val(area);
        }
        else {
          // zu der gewählten Region sind KEINE Feriengebiete hinterlegt
          
          
          $("input.region_key:first").val(area);  // der region_key und ...
          
          setAreaName (area);                     // ...  der Name der Region werden gesetzt
          
          $("select#area-sel").hide();            // verberge die (leere!) Select-Liste für das Gebiet
          
          searchDomicils(2, area);
          //document.select.submit(); 
          
        };
        
        
        
			}
		});      
  });

  //-----------------------------------------
  // Select-Liste für das Gebiets-Auswahl
  //-----------------------------------------
  $("select#area-sel").change(function () {

    var area = $('select#area-sel').val();      
    
    $("input.region_key:first").val(area); // der region_key und ...
    
    setAreaName (area); // ...  der Name der Region werden gesetzt
    
    searchDomicils(2, area);
    //document.select.submit();

  });

});



