// JavaScript Document

  $(document).ready(function(){
	
		//Länder
		$(".ort").hide();
		$(".ort").attr("name","");
		$(".bundesland").hide();
		$(".bundesland").attr("name","");
		
		$("#land").change(function(){
			
			landSelection();
			checkboxAllFields2();
		  }
		);
		
		$("#land").focus(function(){
			
			landSelection();
			checkboxAllFields2();
		  }
		);
		
		$(".bundesland").focus(function(){
						
			showOrtCombobox();
			checkboxAllFields();
		  }
		);
		$(".bundesland").change(function(){
				
			showOrtCombobox();
			checkboxAllFields();
		  }
		);
		
		$("#all").click(function(){
			
			checkboxAllFields();
		  }
		);
		
		$("#all2").click(function(){
			
			checkboxAllFields2();	
		  }
		);
  
  		//Gemeinde Genosenschaftswohnung
		$("#gemeinde").hide();
		$("#genosse").hide();
		
		$("#immobilie").change(function(){
			showGemeindeGenosseFelder();
			
		  }
		);
		$(".unterarten").change(function(){
			showGemeindeGenosseFelder();
			
		  }
		);
		$(".unterarten").focus(function(){
			showGemeindeGenosseFelder();
			
		  }
		);	
  });

  function landSelection()
  {
		var land = $("#land option:selected").val();

		$(".bundesland").hide();
		$(".bundesland").attr("name","");
		$(".ort").hide();
		$(".ort").attr("name","");
		
		$("#land_"+land).show();	
		$("#land_"+land).attr("name","bundesland");
 
  }

  function checkboxAllFields()
  {
			var land = $("#land option:selected").val();
			var elem = $("#land_"+land+" option:selected").val();	
			
			if ($("#all").attr("checked"))
				$("#ort_s"+elem).attr("disabled","disabled");
			else
				$("#ort_s"+elem).removeAttr("disabled");
  
  }
  
  function checkboxAllFields2()
  {	
			
			if ($("#all2").attr("checked"))
			{
				$(".bundesland").attr("disabled","disabled");
				$(".ort").attr("disabled","disabled");
			}
			else
			{
				$(".bundesland").removeAttr("disabled");
				$(".ort").removeAttr("disabled");
			}
  
  }

  function showGemeindeGenosseFelder()
  {
			$("#gemeinde").hide();
			$("#genosse").hide();
			
			if ($("#immobilie option:selected").val() == 1)
			{
				if ($("#1 option:selected").val() == 12)
					$("#genosse").show();
				if ($("#1 option:selected").val() == 13)
					$("#gemeinde").show();	
			}  
  }

  function showOrtCombobox()
  {
		var land = $("#land option:selected").val();
		var ort = $("#land_"+land+" option:selected").val();
		
		$(".ort").hide();
		$(".ort").attr("name","");
		
		$("#ort_"+ort).show();	
		$("#ort_"+ort).attr("name","ort");
		$("#ort_s"+ort).show();	
		$("#ort_s"+ort).attr("name","ort[]");
		
		if (land == 1)
		{
			$("#div_all").show();
		}
		else
			$("#div_all").hide();
			
  }
