function findOption(objSelect, optionText) {
	for (i=0 ; i < objSelect.options.length ; i++){
		try {
			if (objSelect.options[i].text.substr(0,optionText.length) == optionText) {
				objSelect.selectedIndex = i;
				return;
			}
		} catch (er) { }
	}
	objSelect.selectedIndex = -1;
}

function moveToOption(objSelect, optionValue) {
	for (i=0 ; i < objSelect.options.length ; i++){
		if (objSelect.options[i].value == optionValue) {
			objSelect.selectedIndex = i;
			return;
		}
	}
	objSelect.selectedIndex = -1;
}

	
function addOption(objSelect, optionValue, optionText) {
	var optElem = document.createElement("option");	
	optElem.value = optionValue;
	optElem.text = optionText;
	
	objSelect.options[objSelect.options.length] = optElem;
}

function removeOption(objSelect, optionValue) {
	for (i=0 ; i < objSelect.options.length ; i++){
		if (objSelect.options[i].value == optionValue) {
			objSelect.remove(objSelect.options[i]);
			return;
		}
	}
	objSelect.selectedIndex = -1;
}

function selectAllOptions(objSelect) { 
	for (var i = 0; i < objSelect.options.length; i++) { 
		objSelect.options[i].selected = true; 
	} 
}

function getAllOptions(objSelect) {
	strValues = "";
	try { 
		for (var i = 0; i < objSelect.options.length; i++) { 
			if (i > 0) strValues += ",";  
			strValues += objSelect.options[i].value; 
		} 
	} catch (e) {}
	
	return strValues;
}

function swapOptions(objSelectFrom, objSelectTo) {
	selDistritos = document.getElementById(objSelectFrom);

	var arrOptions = new Array();
	for (idx = 0; idx < selDistritos.options.length; idx++) {
		if (selDistritos.options[idx].selected) arrOptions[arrOptions.length] = selDistritos.options[idx];
	}
    		
            		
	for (idx = 0; idx < arrOptions.length; idx++) {
		optDistrito = arrOptions[idx];
		strOptValue = optDistrito.value;
		strOptText = optDistrito.text;
			
		addOption(document.getElementById(objSelectTo), strOptValue, strOptText);
		
		selDistritos.remove(optDistrito.index);				
	}
}

function populateSelect(updateSelect, arrVal, arrTxt, strFirstOption) {
	if (strFirstOption == null) strFirstOption = "...Seleccionar";
	
	for(var i = updateSelect.options.length; i >= 0; i--)
		updateSelect.options[i] = null;

	var optElem = document.createElement("option");
	optElem.value = 0;
	optElem.text = strFirstOption;
	updateSelect.options[0] = optElem;

	if(arrTxt != null && arrVal != null) {
		for(var i = 0; i < arrTxt.length; i++) {
			var optElem = document.createElement("option");			
			optElem.value = arrVal[i];
			optElem.text = arrTxt[i];
			updateSelect.options[i + 1] = optElem;
		}
	}
}

function populateSelectEscritorios(updateSelect, arrVal, arrTxt, strFirstOption) {
	if (strFirstOption == null) strFirstOption = "...Concelho";
	
	updateSelect.innerHTML = "";
	
	var optElem = document.createElement("option");
	optElem.value = 0;
	optElem.innerHTML = strFirstOption;
	updateSelect.appendChild(optElem);

	optElem = document.createElement("option");		
	optElem.value = '1106';
	optElem.innerHTML = 'Lisboa';
	updateSelect.appendChild(optElem);
	
	optElem = document.createElement("option");		
	optElem.value = '1110';
	optElem.innerHTML = 'Oeiras';				
	updateSelect.appendChild(optElem);
	
	optElem = document.createElement("option");		
	optElem.value = '1312';
	optElem.innerHTML = 'Porto';				
	updateSelect.appendChild(optElem);
	
	optElem = document.createElement("optgroup");		
	optElem.label = "-------------------------";
	updateSelect.appendChild(optElem);
	
	if(arrTxt != null && arrVal != null) {
		for(var i = 0; i < arrTxt.length; i++) {
			optElem = document.createElement("option");		
			optElem.value = arrVal[i];
			optElem.innerHTML = arrTxt[i];				
			updateSelect.appendChild(optElem);
		}
	}
}

function OnChangeOperacao(origem) {
	var objSelect= document.getElementsByName("preco1")[0];
	var objSelect2= document.getElementsByName("preco2")[0];	
	
	objSelect.innerHTML ="";
	objSelect2.innerHTML ="";
	
	var objOption=document.createElement("option");		
	if(origem == "portal")
		objOption.innerHTML = "...Preço Mínimo";
	else
			objOption.innerHTML = "...Mín.";
			
	objOption.value = 0;
	objSelect.appendChild(objOption);

	var objOption=document.createElement("option");
	if(origem == "portal")
		objOption.innerHTML = "...Preço Máximo";
	else
		objOption.innerHTML = "...Máx.";
		
	objOption.value = 0;
	objSelect2.appendChild(objOption);


	PrecomTxt = new Array("Preço / m2","5&euro; / m&sup2;","10&euro; / m&sup2;","15&euro; / m&sup2;","20&euro; / m&sup2;","25&euro; / m&sup2;","Preço","2.500&euro;","5.000&euro;","10.000&euro;","15.000&euro;","25.000&euro;","50.000&euro;","75.000&euro;","100.000&euro;","200.000&euro;","300.000&euro;","400.000&euro;","500.000&euro;","750.000&euro;","1.000.000&euro;");
	PrecomVal = new Array("-1","5","10","15","20","25","-1","2500","5000","10000","15000","25000","50000","75000","100000","200000","300000","400000","500000","750000","1000000");

	valor = parseInt(document.getElementById('operacao').value);	

	if(PrecomTxt != null && PrecomVal != null) {
		for(var i = 0; i < PrecomTxt.length; i++) {
			if(PrecomVal[i] == -1 && valor != 3){			
				/*var optGroup = document.createElement('optgroup');				
				optGroup.label = PrecomTxt[i];						
				objSelect.appendChild(optGroup);
				
				var optGroup2 = document.createElement('optgroup');
				optGroup2.label = PrecomTxt[i];		
				objSelect2.appendChild(optGroup2);*/
			}else{				
				// Se se escolher Trespasse
				//if(valor== 3){
					if(PrecomVal[i] > 25){
						var objOption=document.createElement("option");
						objOption.innerHTML = "&nbsp;" + PrecomTxt[i];		
						objOption.value = PrecomVal[i];								
						objSelect.appendChild(objOption);
					
						var objOption2=document.createElement("option");
						objOption2.innerHTML = "&nbsp;" +PrecomTxt[i];		
						objOption2.value = PrecomVal[i];				
						objSelect2.appendChild(objOption2);
					}
					// Compra e Arrendamento
			  /*}else{
				  var objOption=document.createElement("option");
					objOption.innerHTML = "&nbsp;" + PrecomTxt[i];		
					objOption.value = PrecomVal[i];								
					objSelect.appendChild(objOption);
					
					var objOption2=document.createElement("option");
					objOption2.innerHTML = "&nbsp;" +PrecomTxt[i];		
					objOption2.value = PrecomVal[i];				
					objSelect2.appendChild(objOption2);			  
			  }		*/	  
			}	
		}	
	}
}