var Federal_state = new Array();
var Houses_represent = new Array();
var SearchFieldSelectedHouse_ids = new Array();
function GetFederal_state(country_code) {
    
    var federal_states = new Array();
    if (Federal_state.length > 0){
	    for(var i = 0; i < Federal_state.length; i++) {
	    	if (country_code == Federal_state[i].country_code){
		 		federal_states.push(Federal_state[i]);
	    	}
	    }
    }
    
    return federal_states;
}

function LoadFederal_state(element, country_code, selectedHouses) {   
	var federal_states = '';
	
    if (country_code != ''){
	     federal_states = GetFederal_state(country_code);
    }
    else{
		if (selectedHouses != null && selectedHouses.length > 0){
    		country_code = GetCountry_codeByHouse(selectedHouses);
    		federal_states = GetFederal_state(country_code);
    		SearchFieldSelectedFederalState = GetFederalStateIDByHouse(selectedHouses);
    		selectCountry(country_code);
    	}
    	else{
		    federal_states = GetFederal_state(element.value);    	    		
    	}
    }

    var selectNode = document.getElementById('federal_states');

	/*    
    if (selectNode != null){
    	var lengthOption = selectNode.options.length + 1;      
    	if (selectNode.options.length > 0){
		    for (var i = 0; i <= lengthOption; i++){
	    	    selectNode.options[selectNode.options.length - 1] = null;
		    }
    	}
    }
  	*/
  	
    if (selectNode != null){
    	var lengthOption = selectNode.options.length;      
    	if (lengthOption > 0){
		    for (var i = 0; i < lengthOption; i++){
		    	if (selectNode.options[selectNode.options.length - 1] != null){
		    	    selectNode.options[selectNode.options.length - 1] = null;
		    	}
		    }
    	}
    }  	
  
    if (federal_states.length > 0){
	   	var option = new Option(LabelWithoutRestrictions, '');
        selectNode.options[selectNode.options.length] = option;    	
	    for(var i = 0; i < federal_states.length; i++) {
		   	var option = new Option(federal_states[i].name, federal_states[i].id);
	        selectNode.options[selectNode.options.length] = option;
		    if (SearchFieldSelectedFederalState == federal_states[i].id){
			    selectNode.options[i+1].selected  = true;
		    }        
	    }
    }
    LoadHouses_represent('', SearchFieldSelectedFederalState, selectedHouses);
    SearchFieldSelectedFederalState = '';
}

function selectCountry(country_code){
	var selectNode = document.getElementById('country');
	if (selectNode != null){
		var lengthOption = selectNode.options.length - 1;
		if (lengthOption > 0){
		    for (var i = 0; i <= lengthOption; i++){
	    	    if (selectNode.options[i].value == country_code){
	    	    	selectNode.options[i].selected  = true;
	    	    }
		    }	
		}
	}
}

function GetCountry_codeByHouse(selectedHouses) {
    var houses_represent = new Array();
    if (Houses_represent.length > 0) {
	    for(var i = 0; i < Houses_represent.length; i++) {
	    	if (selectedHouses == Houses_represent[i].Haus_ID){
		 		return(Houses_represent[i].country_code);
	    	}
	    }
    }
    return '';
}

function GetFederalStateIDByHouse(selectedHouses) {
    var houses_represent = new Array();
    if (Houses_represent.length){
	    for(var i = 0; i < Houses_represent.length; i++) {
	    	if (selectedHouses == Houses_represent[i].Haus_ID){
		 		return(Houses_represent[i].federal_state_id);
	    	}
	    }
    }
    return '';
}

function in_array(item,arr) {
for(p=0;p<arr.length;p++) if (item == arr[p]) return true;
return false;
}

function LoadHouses_represent(element, federal_state_id, selectedHouses) {
	var houses_represent = '';
	var alreadyInserted = Array();
	if (federal_state_id != ''){
		 houses_represent = GetHouses_represent(federal_state_id);
	}
	else{
	    houses_represent = GetHouses_represent(element.value);
	}

    var selectNode = document.getElementById('house_id');
    
    if (selectNode != null){
    	if (selectNode.options.length > 0){
	    	var lengthOption = selectNode.options.length;      
		    for (var i = 0; i < lengthOption; i++){
		    	if (selectNode.options[selectNode.options.length - 1] != null){
		    	    selectNode.options[selectNode.options.length - 1] = null;
		    	}
		    }
    	}
    }
    
    if (houses_represent.length > 0){
	   	var option = new Option(LabelWithoutRestrictions, '');
        selectNode.options[selectNode.options.length] = option;     	
	    for(var i = 0; i < houses_represent.length; i++) {
	      if(houses_represent[i].name == "" || in_array(houses_represent[i].Haus_ID,alreadyInserted)){
          continue;
        }
        alreadyInserted[houses_represent[i].Haus_ID] = houses_represent[i].Haus_ID;
		   	var option = new Option(houses_represent[i].name, houses_represent[i].Haus_ID);
	        selectNode.options[selectNode.options.length] = option;
	        
	        if (selectedHouses.length > 0){
	        	for(var j = 0; j < selectedHouses.length; j++) {
				    if (selectedHouses[j] == houses_represent[i].Haus_ID){
					    selectNode.options[i+1].selected  = true;
				    }        	
	        	}
	        }         
	    }
    }
}

function GetHouses_represent(federal_state_id) {
    var houses_represent = new Array();
    if (Houses_represent.length > 0){
	    for(var i = 0; i < Houses_represent.length; i++) {
	    	if (federal_state_id == Houses_represent[i].federal_state_id){
		 		houses_represent.push(Houses_represent[i]);
	    	}
	    }
    }
    return houses_represent;
}

function CountryJobsSearch(countryCode) {
	
	if(countryCode.length == 0) return;
	
	var selectNode = document.getElementById('country');
	var options = selectNode.options;
	if (options.length > 0){
		for(var i = 0; i < options.length; i++) {
			if(options[i].value == countryCode) {
				options[i].selected = true;
			}
		}
	}
	document.SearchForm.submit();	
}
