/* t-pont kereso flash callback - - - - - - - - - - - - - - - - - - - - - - - */
function setRegion(regionID) {
    selectRegion(regionID);
}
/* t-pont kereso flash callback - - - - - - - - - - - - - - - - - - - - - - - */



/**Cities which belong to the given regio*/
matchCities = null;
/**Option object*/
selectNode = null;
/**The ID of the selected redion*/
selectedRegionID=null;
/**Budapest Regio is selected or not*/
isBudapestRegio=false;

/**Select a a regio, and fill combobox with its cities*/
function selectRegion(regionID) {

    selectedRegionID = regionID;
    matchCities = cityHash.get(regionID);
    //convert TO DB numbering 
    previousRegionID=$('searchForm:selectedRegion').value;
    $('searchForm:selectedRegion').value=selectedRegionID;
    //deleted previous selection
    if (selectedRegionID!=previousRegionID){
     $('searchForm:selectedCity').value="";
    }
    selectNode = document.getElementById("selectCityOption");
    
    var options = selectNode.getElementsByTagName('option');
    
    options = $A(options);
    setRegionOptions();
    
    displayCombo();
     
}


/**Display the combobox*/
 function displayCombo(){
     $('selectCityOption').setStyle({display: 'block'});
        keruletLabel = $('searchForm:selectedCityOptionLabelKerulet');
	cityLabel = $('searchForm:selectedCityOptionLabelCity');
     if (isBudapestRegio){
        if (keruletLabel!=null){
    	    keruletLabel.setStyle({display: 'block'});
	}
	if(cityLabel!=null){
	    cityLabel.setStyle({display: 'none'});
	}
     }else{
        if (keruletLabel!=null){
    	    keruletLabel.setStyle({display: 'none'});
	}
	if(cityLabel!=null){
	    cityLabel.setStyle({display: 'block'});
	}
     }
     
 }
 
/**Set the selected city name to the hidden input text*/
function  getSelectedElement(){
    var selectedIndex = $('selectCityOption').selectedIndex;
    var selectedElement = $('selectCityOption').options[selectedIndex];
    $('searchForm:selectedCity').value=selectedElement.text;
    
}
/**Set the selected city name to the option text*/
function setSelectedElement(){

    var selectedElement=$('searchForm:selectedCity').value;
    var cityOptionCombo = $('selectCityOption');
    for(i = 0;i<cityOptionCombo.options.length;  i++){
      if (cityOptionCombo.options[i].text == selectedElement){
        cityOptionCombo.selectedIndex=i;
	break;
         }
      }
 
}

/**Fill combox with cities*/
 function setRegionOptions(){
 
 /** removes items */ 
 for(i = $('selectCityOption').options.length-1; i>=0;  i--){
      $('selectCityOption').options[i] = null;
   }
   checkBudapest();
    /** builds new items  */
       for (i = 0; i < matchCities.length; i++) {
          var optn = document.createElement("OPTION");
	  var text = html_entity_decode(matchCities[i]);
          optn.value = text;
	  optn.text =  text;
	  //optn.setAttribute();
          $('selectCityOption').options.add(optn);
       }
     }

/**Decodes HTML entites*/     
function html_entity_decode(s) {
    var t = new Element('textarea',{'id':'temp'});
    t.innerHTML = s;
    var v = t.value;
    t = null;
    return v;
}

/**Check that the selected region is Budapest or not*/
function checkBudapest(){
    if (matchCities!=null){
	firstEntry = matchCities[0];
	if (firstEntry!=null){
	        decoded = html_entity_decode(firstEntry);
		if (decoded.match("kerület")!=null){
		     isBudapestRegio=true;
		    return;
		}else{
		    isBudapestRegio=false;
		    return;
		}
	}
    }
}


		 
