	function addOption(selectbox,text,value )
	{
		var optn = document.createElement("OPTION");
		optn.text = text;
		optn.value = value;
		selectbox.options.add(optn);
	}
	
	
	function setSelectedIndexByValue(selectbox,value){
		var i=0;
		numar_optiuni = selectbox.length;
		for(i=0;i<numar_optiuni;i++){
			option = selectbox.options[i];
			if(option.value==value){
				selectbox.selectedIndex=i;
				return;
			}
		}
		selectbox.selectedIndex=0;
	}
	
	function setIndexesFromValues(selectbox,values){
		var i=0;
		numar_optiuni = selectbox.length;
		numar_valori = values.length;
		for(i=0;i<numar_optiuni;i++){
			for(j=0;j<numar_valori;j++){
				option = selectbox.options[i];
				value = values[j];
				if(option.value==value){
					selectbox.options[i].selected = true;
				}
			}
		}
	}
	
	function updateJudete(tipImobil,tipTranzactie,judet_default,zone_default)
	{
		form_tip_imobil = $('filterCLS[TIP_IMOBIL_EUROEST]').value ;
		tipImobil = (typeof tipImobil  == 'undefined') ? form_tip_imobil : tipImobil ;
		tipTranzactie = (typeof tipTranzactie  == 'undefined') ? $('filterCLS[TIP_TRANZ]').value : tipTranzactie ;
		domain_base = (typeof domain_base  == 'undefined') ? '/' : domain_base;


		judet_selectat  = (typeof judet_default == 'undefined') ? $F("filterEQ[ORAS]"): judet_default;

		//resetam orase
		$("filterEQ[ORAS]").options.length = 0;
		addOption($("filterEQ[ORAS]"),"Alege judet","");
		
		
		new Ajax.Request(domain_base+'index.php?section=ajax&ajax=1&screen=search_box&cmd=judete&tip_imobil=' + tipImobil + '&tip_tranz=' + tipTranzactie, 	
		{
			method:'get',
			onSuccess: function(transport)
			{
		  
					var d = transport.responseText;
					if(d !='[]')  
					{
						var json = d.evalJSON();
					}
					else
					{
						//resetam orase
						$("filterEQ[ORAS]").options.length = 0;
						addOption($("filterEQ[ORAS]"),"Nu exista oferte","");
					}
		
					for ( keyVar in json )
					{
						v = json[keyVar];
						var l = '';
						if(v=="Bucuresti") l="Bucuresti/Ilfov"; else l=v;
						addOption($("filterEQ[ORAS]"),l,v);						
					}
					setSelectedIndexByValue($("filterEQ[ORAS]"),judet_selectat);
					
					updateZone(tipImobil,tipTranzactie,judet_selectat,zone_default);
					
			}.bind(judet_selectat)
		});
	
		
	}
	
	
	function updateZone(tipImobil, tipTranzactie, oras,zone_default)
	{	
		tipImobil = (typeof tipImobil  == 'undefined') ? $('filterCLS[TIP_IMOBIL_EUROEST]').value : tipImobil ;
		tipTranzactie = (typeof tipTranzactie  == 'undefined') ? $('filterCLS[TIP_TRANZ]').value : tipTranzactie ;
		oras = (typeof oras  == 'undefined') ? $('filterEQ[ORAS]').value : oras ;
		
		
		if(typeof zone_default != 'undefined'){
			if(zone_default.localeCompare("null")!=0)
				zone_selectate = zone_default.evalJSON();
			else 
				zone_selectate = {};
		} else {
			zone_selectate = {};
		}

		$("filterEQ[ZONA]").options.length = 0;
		$("filterEQ[ZONA]").innerHTML='';
		
		//alert(tipImobil);
		
		new Ajax.Request(domain_base+'index.php?section=ajax&ajax=1&screen=search_box&cmd=zone&tip_imobil=' + tipImobil + '&tip_tranz=' + tipTranzactie + '&oras=' + oras, 
		{
				method:'get',
				onSuccess: function(transport)
				{
						var d = transport.responseText;
						var json = d.evalJSON();
						if(d !='[]');
						/*{
							addOption($("filterEQ[ZONA]"),"Alege zona","");
						}*/
						else 
						{
							addOption($("filterEQ[ZONA]"),"Nu exista oferte","");	
						}
			
						var nume_zona='';
						for ( keyVar in json ) 
						{
							
							if(nume_zona != keyVar)
							{
								nume_zona = keyVar;
							
								objSelect=$("filterEQ[ZONA]");
								optGroup = document.createElement('optgroup');
								optGroup.label = nume_zona;
								
							}
							for ( indx in  json[keyVar])
							{
								v = json[keyVar][indx];
								if(json[keyVar].hasOwnProperty(indx))
								{
									objOption=document.createElement("option");
									objOption.innerHTML = v;
									objOption.value = v;
									optGroup.appendChild(objOption)
								}
							}
							
							objSelect.appendChild(optGroup);

//							optGroup.appendChild(objOption);
												
						}
						
						//alert(zone_selectate);
						setIndexesFromValues($("filterEQ[ZONA]"),zone_selectate);
							
						try{
							selcb.redraw();
						} catch(e){ alert(e); }
		
				}.bind(zone_selectate)
		});
	
	}
	function validate_id(){
		if(!IsNumeric($F('id'))){
			alert('Id invalid!');
			return false;
		}else{
			document.idsForm.submit();
		}
	}

