function trim(string) {
  var re= /^\s*|\s*$/g;
  return string.replace(re,"");
}
function validateEmail(e) {
  if (e != "") {
    var objRegExp = /^.+@.+\..{2,3}$/;
    return objRegExp.test(e);
  } else {
    return true;
  }
}
function subBack() {
  history.back();
}

function frmContactos_Validate() {
  if(trim(document.getElementById("tfAssunto").value) == "" || trim(document.getElementById("tfNome").value) == "" ||
	 trim(document.getElementById("tfEmpresa").value) == "" || trim(document.getElementById("tfTelefone").value) == "" ||
	 trim(document.getElementById("tfEmail").value) == "" || trim(document.getElementById("tfMensagem").value) == "") {
    alert ("Não é possível enviar o formulário.\nTodos os campos de preenchimento obrigatório.");
    return false;
  }
  if(!validateEmail(document.getElementById("tfEmail").value)) {
    alert("Não é possível enviar o formulário.\nIntroduza um endereço de e-mail válido.");
    return false;
  }
  return true;
}
function backWindow() {
  history.back();
}
function redirectTo(url) {
  document.location = url
}
function frmVender_Validate() {
  if(trim(document.getElementById("ddlFinalidade").value) == "" || trim(document.getElementById("ddlTipo").value) == "" ||
	 trim(document.getElementById("ddlTipologia").value) == "" || trim(document.getElementById("tfArea").value) == "" || 
	 trim(document.getElementById("ddlEstado").value) == "" || trim(document.getElementById("tfPreco").value) == "" ||
	 trim(document.getElementById("tfDescricao").value) == "" || trim(document.getElementById("ddlDistrito").value) == "" ||
	 trim(document.getElementById("ddlConcelho").value) == "" || trim(document.getElementById("tfNome").value) == "" ||
	 trim(document.getElementById("tfTelefone").value) == "" || trim(document.getElementById("tfEmail").value) == "") {
    alert ("Não é possível enviar o formulário.\nTodos os campos de preenchimento obrigatório.");
    return false;
  }
  if(!validateEmail(document.getElementById("tfEmail").value)) {
    alert("Não é possível enviar o formulário.\nIntroduza um endereço de e-mail válido.");
    return false;
  }
}
function frmPesquisar_Validate() {
  if(document.getElementById("ddlTipologiaL").selectedIndex != 0 && document.getElementById("ddlTipologiaH").selectedIndex != 0) {
    if(document.getElementById("ddlTipologiaL").selectedIndex > document.getElementById("ddlTipologiaH").selectedIndex){
      alert("Não é possível iniciar a pesquisa.\nIntroduza um intervalo de tipologias válido.");
	  return false;
    }
  }
  var pL=0;
  var pH=0;
  if(document.getElementById("tfPrecoL").value.length > 0) {
    if(isNaN(document.getElementById("tfPrecoL").value * 1)) {
      alert("Não é possível iniciar a pesquisa.\nIntroduza um intervalo de preços válido.");
	  return false;
	} else { 
	  pL = document.getElementById("tfPrecoL").value * 1;
	}
  }
  if(document.getElementById("tfPrecoH").value.length > 0) {
    if(isNaN(document.getElementById("tfPrecoH").value * 1)) {
	  alert("Não é possível iniciar o processo.\nIntroduza por favor um intervalo de preços válido.");
	  return false;
	} else {
	  pH = document.getElementById("tfPrecoH").value * 1;
	}
  }
  if(pH > 0) {
    if(pL > pH) {
	  alert("Não é possível iniciar o processo.\nIntroduza por favor um intervalo de preços válido.");
	  return false;
    }
  }
  if(document.getElementById("ddlDistrito").value == "") {
    alert ("Não é possível iniciar a pesquisa.\nSeleccione um distrito.");
    return false;
  }
  return true;
}
function setColor(strColor,rowIndex) {
  document.getElementById("tdReferencia"+rowIndex).style.color = strColor
  document.getElementById("tdTipoImovel"+rowIndex).style.color = strColor
  document.getElementById("tdTipologia"+rowIndex).style.color = strColor
  document.getElementById("tdLocalizacao"+rowIndex).style.color = strColor
  document.getElementById("tdPreco"+rowIndex).style.color = strColor
  document.getElementById("tdImagem"+rowIndex).style.color = strColor
}
function subChange(image, width, height) {
  clearTimeout(intID);
  if(parent.all) {
  	parent.imgPrincipal.style.filter="blendTrans(duration=2)";
    parent.imgPrincipal.style.filter="blendTrans(duration=crossFadeDuration)";
    parent.imgPrincipal.filters.blendTrans.Apply();
  }
  parent.imgPrincipal.src = image;
  parent.imgPrincipal.width = width;
  parent.imgPrincipal.height = height;
  intID = setTimeout("playSlide()", intSpeed);
}
function frmPedidoInformacoes_Validate() {
  if(trim(document.getElementById("tfNome").value) == "" || trim(document.getElementById("tfTelefone").value) == "" ||
	 trim(document.getElementById("tfEmail").value) == "") {
    alert ("Não é possível enviar o formulário.\nPreencha os campos de preenchimento obrigatório.");
    return false;
  }
  if(!validateEmail(document.getElementById("tfEmail").value)) {
    alert("Não é possível enviar o formulário.\nIntroduza um endereço de e-mail válido.");
    return false;
  }
  return true;
}
