function getForm(formName) {
    // Test if is IE or Netscape (document.layers)
    if(document.all)
        return eval(formName);
    else
        return document.forms[formName];
}

function ValidaPesq () {
  var searchForm = getForm("imoFastSearch");
	searchForm.action = "http://www.lardocelar.com/servlet/PesquisaResult";    	 
	searchForm.target = "";

	if(searchForm.tipologia_id1.selectedIndex > searchForm.tipologia_id2.selectedIndex) {
        alert("Seleccionar tipologia máxima igual ou superior\nà tipologia mínima.");
        searchForm.tipologia_id2.focus();
        return false;
    }


    if(searchForm.concelho_id.selectedIndex == 0) {
        alert("Por favor, escolha o Concelho onde pretende fazer a sua pesquisa.");
        searchForm.concelho_id.focus();
        return false;
    }   

    intTipoId = searchForm.tipo_id.options[searchForm.tipo_id.selectedIndex].value;
    if(intTipoId == 3) {
    	objWindow = window.open("", "wdOpenEscritorios");
    	searchForm.action = "http://www.lardocelar.com/imobiliario/redirect_to_escritorios.jsp";    	 
    	searchForm.target = "wdOpenEscritorios";
    } else if(intTipoId == 5) {
    	objWindow = window.open("", "wdOpenArmazens");
    	searchForm.action = "http://www.lardocelar.com/imobiliario/redirect_to_armazens.jsp";    	 
    	searchForm.target = "wdOpenArmazens";
    } else if(intTipoId == 6) {
    	objWindow = window.open("", "wdOpenLojas");
    	searchForm.action = "http://www.lardocelar.com/imobiliario/redirect_to_lojas.jsp";    	 
    	searchForm.target = "wdOpenLojas";
    }
    
    
    return true;
}

function ValidaPesqRapida () {
	var searchForm = getForm("imoFastSearch");
	searchForm.action = "http://www.lardocelar.com/servlet/PesquisaResult";    	 
	searchForm.target = "";

	if(searchForm.tipologia_id1.selectedIndex > searchForm.tipologia_id2.selectedIndex) {
		alert("Seleccionar tipologia máxima igual ou superior\nà tipologia mínima.");
		searchForm.tipologia_id2.focus();
		return false;
	}

	if(searchForm.concelho_id.selectedIndex == 0) {
		alert("Por favor, escolha o Concelho onde pretende fazer a sua pesquisa.");
		searchForm.concelho_id.focus();
		return false;
	}
   
    intTipoId = searchForm.tipo_id.options[searchForm.tipo_id.selectedIndex].value;
    if(intTipoId == 3) {
    	objWindow = window.open("", "wdOpenEscritorios");
    	searchForm.action = "http://www.lardocelar.com/imobiliario/redirect_to_escritorios.jsp";    	 
    	searchForm.target = "wdOpenEscritorios";
    } else if(intTipoId == 5) {
    	objWindow = window.open("", "wdOpenArmazens");
    	searchForm.action = "http://www.lardocelar.com/imobiliario/redirect_to_armazens.jsp";    	 
    	searchForm.target = "wdOpenArmazens";
    } else if(intTipoId == 6) {
    	objWindow = window.open("", "wdOpenLojas");
    	searchForm.action = "http://www.lardocelar.com/imobiliario/redirect_to_lojas.jsp";    	 
    	searchForm.target = "wdOpenLojas";
    }
    
	return true;
}


function ValidaPesqLDL () {
	
	regtelef = /[^0-9\s]/;
    
     
	if(document.getElementById("refLDL").value == "" || document.getElementById("refLDL").value == ":: Referência LDL" || regtelef.test(document.getElementById("refLDL").value)) {
		alert("Tem de preencher a referência.\nO campo só aceita valores numéricos.");
		document.getElementById("refLDL").focus();
		return false;
	}
    
	/*if (document.getElementById("refLDL").value.substring(0,3) != 'LDL') {
		
		alert("Erro na referência LardoceLar!\nDeve começar com os caracteres LDL seguido do identificador numérico do imóvel.")
		document.getElementById("refLDL").focus();
		return false;		
	}*/
            
	return true;
}   


function virtualKeyPressed(field, event) {
	
	var intCharCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		
	strInputBoxValue = field.value;

	if ((intCharCode < 48 || intCharCode > 57) && (intCharCode != 13)) {
	event.returnValue = false;
	}	
	
}



function ValidaPesqMediadora () {
	
	if(document.getElementById("refMed").value == "" || document.getElementById("refMed").value == ":: Referência Mediadora") {
		alert("Tem de preencher a referência");
		document.getElementById("refMed").focus();
		return false;
	}
   
    
	return true;
}

function FocusLDL() {

	//document.getElementById("refLDL").focus();
	//document.getElementById("refLDL").value="LDL";
	document.getElementById("refLDL").value='';
	document.getElementById("refMed").value=':: Referência Mediadora';

}

function FocusRefMed() {

	document.getElementById("refMed").value='';
	document.getElementById("refLDL").value=':: Referência LDL';

}
       

function populateSelect1(updateSelect, arrVal, arrTxt) {
    for(var i = updateSelect.options.length; i >= 0; i--)
        updateSelect.options[i] = null;

    var optElem = document.createElement("option");
    optElem.value = -1;
    optElem.text = ":: Preço Máximo";
    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 OnChangeOperFast() {
    var searchForm = getForm("imoFastSearch");
    var valor = parseInt(searchForm.operacao.value);
    var precoTxt = new Array("250 €","500 €","750 €","1.000 €","1.250 €","1.500 €","1.750 €",">2.000 €");
    var precoVal = new Array("250","500","750","100","1250","1500","1750","2000");
    var precoVendTxt = new Array("25.000 €","50.000 €","75.000 €","100.000 €","125.000 €","150.000 €","175.000 €","200.000 €","225.000 €","250.000 €","275.000 €","300.000 €","325.000 €","350.000 €","375.000 €","400.000 €",">425.000 €");
    var precoVendVal = new Array("25000","50000","75000","100000","125000","150000","175000","200000","225000","250000","275000","300000","325000","350000","375000","400000","425000");
    switch (valor){
        case 0:
            populateSelect1(document.getElementsByName("preco2")[0], new Array(), new Array());
            searchForm.preco2.disabled = true;
            break;
        case 1:     //Compra
            populateSelect1(document.getElementsByName("preco2")[0], precoVendVal, precoVendTxt);
            searchForm.preco2.disabled = false;
            break;
        case 2:     //Arredamento
            searchForm.preco2.disabled = true;
            break;
       case 3:     //Trespasse
            populateSelect1(document.getElementsByName("preco2")[0], precoVendVal, precoVendTxt);
            searchForm.preco2.disabled = false;
            break;
    }

}


function OnChangeTipo() {
    var searchForm = getForm("imoFastSearch");	
    if(searchForm.tipo_id.selectedIndex > 1) {
        searchForm.tipologia_id1.disabled = true;
        searchForm.tipologia_id2.disabled = true;
    }
    else {
        searchForm.tipologia_id1.disabled = false;
        searchForm.tipologia_id2.disabled = false;
    }	
}

function OnChangeTipoHPImo() {
    var searchForm = getForm("imoFastSearch");	
    if(searchForm.tipo_id.selectedIndex > 1) {
        searchForm.tipologia_id1.disabled = true;
        searchForm.tipologia_id2.disabled = true;
    }
    else {
        searchForm.tipologia_id1.disabled = false;
        searchForm.tipologia_id2.disabled = false;
    }	
}