// JavaScript Document



$(document).ready(function(){
	   $(".submenu").fadeTo("fast", 0.1); // This sets the opacity of the thumbs to fade down to 30% when the page loads
	   
	   $('.baixo_3').click(function(){
			//$(document.body).animate({scrollTop:0}, 'slow');
			$.scrollTo( 0, 1000 /*, { easing:'elasout' }*/ );
			//alert('asdfsd')
		});
	   
	$("#twn_id").change(function(){
		$('#ngh_id').empty();
		$('#ngh_id').html('<option value="">loading...</option>');
		$.get('../includes/twnBox.htm?twn='+$(this).val(), function(dados){
			$('#ngh_id').html('<option value="">('+tradQualq+')</option>').append((dados));
			return true;
		});
	});
	$("#ngh_id").change(function(){
		$('#zon_id').empty();
		$('#zon_id').html('<option value="">loading...</option>');
		$.get('../includes/nghBox.htm?ngh='+$(this).val(), function(dados){
			$('#zon_id').html('<option value="">('+tradQualq+')</option>').append((dados));
			return true;
		});
	});
	
	
	$("#twn2_id").change(function(){
		$('#ngh2_id').empty();
		$('#ngh2_id').html('<option value="">loading...</option>');
		$.get('../includes/twnBox.htm?twn='+$(this).val(), function(dados){
			$('#ngh2_id').html('<option value="">('+tradQualq+')</option>').append((dados));
			return true;
		});
	});
	$("#ngh2_id").change(function(){
		$('#zon2_id').empty();
		$('#zon2_id').html('<option value="">loading...</option>');
		$.get('../includes/nghBox.htm?ngh='+$(this).val(), function(dados){
			$('#zon2_id').html('<option value="">('+tradQualq+')</option>').append((dados));
			return true;
		});
	});
	
	
});

/* FAVORITOS detalhes */
function adFavorito(divID,estado){
	if(estado == "over"){
		alert("está over");
		document.getElementById(divID).style.backgroundImage = "../images/fundobotoesON.gif";
		//estado = "out";
	} else if(estado == "out"){
		alert("está out");
		document.getElementById(divID).style.backgroundImage = "../images/fundobotoes.gif";
		//estado = "over";
	}
}


/* menu COMERCIALIZAR (escolher por label) */
function selectrd(id)
{
	var opt=(id==1)?"yes":"no";
	var tg=document.getElementById("rd" + opt);
	tg.checked=true;
}


/* mapa CONTACTOS */
function mostraMapa(estado,loja){
	if(estado == "abrir"){
		document.getElementById('fundocor').style.display = "block";
		document.getElementById('fecharCaixa').style.display = "block";
		document.getElementById('localCaixa').style.display = "block";
		/*document.getElementById('centro2').style.zIndex = -1;
		document.getElementById('ref').style.zIndex = -1;
		document.getElementById('inipesq').style.zIndex = -1;*/
		$('#centro2').css({"z-index":-1});
		$('#ref').css({"visibility":"hidden"});
		$('.enviarPesqR').css({"z-index":-1});
		if(loja == 1){
			agencia = 1;
		} else if(loja == 2){
			agencia = 2;
		}
		
	} else if(estado == "fechar"){
		document.getElementById('fundocor').style.display = "none";
		document.getElementById('fecharCaixa').style.display = "none";
		document.getElementById('localCaixa').style.display = "none";
		$('#centro2').css({"z-index":0});
		$('#ref').css({"visibility":"visible"});
		$('.enviarPesqR').css({"z-index":0});
	}	
}

function analisaCx(accao, id, valor){
	//alert('teste');
	if(accao == "esvazia" && document.getElementById(id).value == valor){
		document.getElementById(id).value = "";
	} else if(accao == "preenche" && document.getElementById(id).value == ""){
		document.getElementById(id).value = valor;
	}
}
function repoeCx(){
	//return false;
	 if(document.getElementById('caixaRef').value == "Referencia"){
		//alert("passa por aqui");
		document.getElementById('caixaRef').value = "";
	}
	return true;
}
function repoeCxEN(){
	//return false;
	 if(document.getElementById('caixaRef').value == "Reference"){
		//alert("passa por aqui");
		document.getElementById('caixaRef').value = "";
	}
	return true;
}
function repoeCxDE(){
	//return false;
	 if(document.getElementById('caixaRef').value == "Hinweis"){
		//alert("passa por aqui");
		document.getElementById('caixaRef').value = "";
	}
	return true;
}
function repoeCxES(){
	//return false;
	 if(document.getElementById('caixaRef').value == "Referencia"){
		//alert("passa por aqui");
		document.getElementById('caixaRef').value = "";
	}
	return true;
}
function repoeCx2(){
	var sErr = "Corrigir os seguintes erros: <br>";
	var repor = new Array();;
	repor[0] = "Nome";
	repor[1] = "Telefone";
	repor[2] = "Email";
	repor[3] = "Data de Nascimento";
	repor[4] = "Valor de IRS";
	repor[5] = "Valor do imovel";
	repor[6] = "Contribuinte (facultativo)";
	
	var idcaixa = new Array();;
	idcaixa[0] = "prim";
	idcaixa[1] = "telefone";
	idcaixa[2] = "email";
	idcaixa[3] = "nascimento";
	idcaixa[4] = "irs";
	idcaixa[5] = "imovel";
	idcaixa[6] = "contribuinte";
	
	for(i=0; i<=6; i++)
	{
	//alert(idcaixa[i]);
	
	 if(document.getElementById(idcaixa[i]).value == repor[i] ){
		
		if(i == 0){
			 sErr = sErr+"- nome inv&aacute;lido <br/> ";
			 //return false;
		 } if(i == 1){
			 sErr = sErr+"- telefone inv&aacute;lido";
			 //return false;
		 } else {
		//alert("passa por aqui");
		if(sErr == "Corrigir os seguintes erros: <br>")
		{
			document.getElementById(idcaixa[i]).value = "";
		}
		
		 }
	
	}
	
	
	}
	//document.simula.reset();
	if(sErr != "Corrigir os seguintes erros: <br>")
		{
			//document.simula.reset();
			//alert(sErr);
			$('#erro').html(sErr);
			
		}
		else
		{
			document.simula.submit();
		}
	
}

function repoeCx2EN(){
	var sErr = "Correct the following errors: <br>";
	var repor = new Array();;
	repor[0] = "Name";
	repor[1] = "Telephone";
	repor[2] = "Email";
	repor[3] = "Birthday";
	repor[4] = "IRS value";
	repor[5] = "Property value";
	repor[6] = "Contributor (optional)";
	
	var idcaixa = new Array();;
	idcaixa[0] = "prim";
	idcaixa[1] = "telefone";
	idcaixa[2] = "email";
	idcaixa[3] = "nascimento";
	idcaixa[4] = "irs";
	idcaixa[5] = "imovel";
	idcaixa[6] = "contribuinte";
	
	for(i=0; i<=6; i++)
	{
	//alert(idcaixa[i]);
	
	 if(document.getElementById(idcaixa[i]).value == repor[i] ){
		
		if(i == 0){
			 sErr = sErr+"- invalid name <br/> ";
			 //return false;
		 } if(i == 1){
			 sErr = sErr+"- invalid telephone";
			 //return false;
		 } else {
		//alert("passa por aqui");
		document.getElementById(idcaixa[i]).value = "";
		 }
	
	}
	
	
	}
	//document.simula.reset();
	if(sErr != "Correct the following errors: <br>")
		{
			document.simula.reset();
			//alert(sErr);
			$('#erro').html(sErr);
			
		}
		else
		{
			document.simula.submit();
		}
	
}

function repoeCx2FR(){
	var sErr = "Corrigez les situations suivantes: <br>";
	var repor = new Array();;
	repor[0] = "Nom";
	repor[1] = "Telephone";
	repor[2] = "Email";
	repor[3] = "Date de Naissance";
	repor[4] = "Montant du IRS";
	repor[5] = "Montant du bien";
	repor[6] = "Contributeur (optionnel)";
	
	var idcaixa = new Array();;
	idcaixa[0] = "prim";
	idcaixa[1] = "telefone";
	idcaixa[2] = "email";
	idcaixa[3] = "nascimento";
	idcaixa[4] = "irs";
	idcaixa[5] = "imovel";
	idcaixa[6] = "contribuinte";
	
	for(i=0; i<=6; i++)
	{
	//alert(idcaixa[i]);
	
	 if(document.getElementById(idcaixa[i]).value == repor[i] ){
		
		if(i == 0){
			 sErr = sErr+"- nom incorrect <br/> ";
			 //return false;
		 } if(i == 1){
			 sErr = sErr+"- numero de telephone incorrect";
			 //return false;
		 } else {
		//alert("passa por aqui");
		document.getElementById(idcaixa[i]).value = "";
		 }
	
	}
	
	
	}
	//document.simula.reset();
	if(sErr != "Corrigez les situations suivantes: <br>")
		{
			document.simula.reset();
			//alert(sErr);
			$('#erro').html(sErr);
			
		}
		else
		{
			document.simula.submit();
		}
	
}
function repoeCx2DE(){
	var sErr = "Korrigieren Sie die folgenden Situationen: <br>";
	var repor = new Array();;
	repor[0] = "Name";
	repor[1] = "Telefon";
	repor[2] = "Email";
	repor[3] = "Geburtsdatum";
	repor[4] = "IRS Betrag";
	repor[5] = "Immobilie Betrag";
	repor[6] = "Beitragendes (Optional)";
	
	var idcaixa = new Array();;
	idcaixa[0] = "prim";
	idcaixa[1] = "telefone";
	idcaixa[2] = "email";
	idcaixa[3] = "nascimento";
	idcaixa[4] = "irs";
	idcaixa[5] = "imovel";
	idcaixa[6] = "contribuinte";
	
	for(i=0; i<=6; i++)
	{
	//alert(idcaixa[i]);
	
	 if(document.getElementById(idcaixa[i]).value == repor[i] ){
		
		if(i == 0){
			 sErr = sErr+"- Falscher Name <br/> ";
			 //return false;
		 } if(i == 1){
			 sErr = sErr+"- Ungultige Telefonnummer";
			 //return false;
		 } else {
		//alert("passa por aqui");
		document.getElementById(idcaixa[i]).value = "";
		 }
	
	}
	
	
	}
	//document.simula.reset();
	if(sErr != "Korrigieren Sie die folgenden Situationen: <br>")
		{
			document.simula.reset();
			//alert(sErr);
			$('#erro').html(sErr);
			
		}
		else
		{
			document.simula.submit();
		}
	
}
function repoeCx2ES(){
	var sErr = "Corrigir los siguientes errores: <br>";
	var repor = new Array();;
	repor[0] = "Nombre";
	repor[1] = "Telefono";
	repor[2] = "Email";
	repor[3] = "Fecha de nacimiento";
	repor[4] = "valor del IRS";
	repor[5] = "valor del Inmueble";
	repor[6] = "Contribuyente (Opcional)";
	
	var idcaixa = new Array();;
	idcaixa[0] = "prim";
	idcaixa[1] = "telefone";
	idcaixa[2] = "email";
	idcaixa[3] = "nascimento";
	idcaixa[4] = "irs";
	idcaixa[5] = "imovel";
	idcaixa[6] = "contribuinte";
	
	for(i=0; i<=6; i++)
	{
	//alert(idcaixa[i]);
	
	 if(document.getElementById(idcaixa[i]).value == repor[i] ){
		
		if(i == 0){
			 sErr = sErr+"- Nombre incorrecto <br/> ";
			 //return false;
		 } if(i == 1){
			 sErr = sErr+"- Numero de telefono incorrecto";
			 //return false;
		 } else {
		//alert("passa por aqui");
		document.getElementById(idcaixa[i]).value = "";
		 }
	
	}
	
	
	}
	//document.simula.reset();
	if(sErr != "Corrigir los siguientes errores: <br>")
		{
			document.simula.reset();
			//alert(sErr);
			$('#erro').html(sErr);
			
		}
		else
		{
			document.simula.submit();
		}
	
}

function abrirCaixas(caixa,estado){
	
		if(estado == "on"){
			document.getElementById(caixa).style.display = "block";
		} 
		else if(estado == "off"){
			document.getElementById(caixa).style.display = "none";
		}
}
/* referencia pesquisa rapida */

function caract(estado){
	
		if(estado == "on"){
			document.getElementById('caracteristicas').style.display = "block";
		} 
		else if(estado == "off"){
			document.getElementById('caracteristicas').style.display = "none";
		}
}

function showFeat(divId,btID){
	if(
			document.getElementById(divId).style.display == "none")
		{
			document.getElementById(divId).style.display = "block";
			document.getElementById(btID).src="../images/det_menos.gif";
		} else{
			document.getElementById(divId).style.display = "none";
			document.getElementById(btID).src="../images/det_mais.gif";
		}
}


/* carregar flashes */
	function carregaFlash(destino,url,largura,altura){
    $(destino).flash(
        { src: url,
          width: largura,
          height: altura },
        { version: 8 }
    );
	}
	
function scrollLeft(elementId)
	  {
		if(!window.scrolling) window.scrolling = new Scroll(elementId, -1);
	  }
	  
	  function scrollRight(elementId)
	  {
		if(!window.scrolling) window.scrolling = new Scroll(elementId, 1);
	  }
	  
	  function Scroll(elementId, direction)
	  {
		var scrollElement = document.getElementById(elementId);
		var targetPosition = scrollElement.scrollLeft + 150* direction;
		if(targetPosition < 0) targetPosition = 0;
		if(targetPosition > scrollElement.scrollWidth) targetPosition = scrollElement.scrollWidth;
		var lastPosition = -1;
		var interval = setInterval("window.scrolling.doScroll()", 50);
		
		this.doScroll = function()
		{
		  var currentPosition = scrollElement.scrollLeft;
		  if(currentPosition == lastPosition)
		  {
			clearInterval(interval);
			window.scrolling = false;
		  }
		  else
		  {
			scrollElement.scrollLeft = (scrollElement.scrollLeft + targetPosition) / 2;
			lastPosition = currentPosition;
		  }
		}
}

/* ARRASTAR CAIXAS */

function Browser() {

  var ua, s, i;

  this.isIE    = false;
  this.isNS    = false;
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

var browser = new Browser();

// Global object to hold drag information.

var dragObj = new Object();
dragObj.zIndex = 99999;

function dragStart(event, id) {

  var el;
  var x, y;

  // If an element id was given, find it. Otherwise use the element being
  // clicked on.

  if (id)
    dragObj.elNode = document.getElementById(id);
  else {
    if (browser.isIE)
      dragObj.elNode = window.event.srcElement;
    if (browser.isNS)
      dragObj.elNode = event.target;

    // If this is a text node, use its parent element.

    if (dragObj.elNode.nodeType == 3)
      dragObj.elNode = dragObj.elNode.parentNode;
  }

  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Save starting positions of cursor and element.

  dragObj.cursorStartX = x;
  dragObj.cursorStartY = y;
  dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
  dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);

  if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 300;
  if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;

  // Update element's z-index.

  dragObj.elNode.style.zIndex = ++dragObj.zIndex;

  // Capture mousemove and mouseup events on the page.

  if (browser.isIE) {
    document.attachEvent("onmousemove", dragGo);
    document.attachEvent("onmouseup",   dragStop);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS) {
    document.addEventListener("mousemove", dragGo,   true);
    document.addEventListener("mouseup",   dragStop, true);
    event.preventDefault();
  }
}

function dragGo(event) {

  var x, y;

  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Move drag element by the same amount the cursor has moved.

  dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
  dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";

  if (browser.isIE) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS)
    event.preventDefault();
}

function dragStop(event) {

  // Clear the drag element global.

  dragObj.elNode = null;

  // Stop capturing mousemove and mouseup events.

  if (browser.isIE) {
    document.detachEvent("onmousemove", dragGo);
    document.detachEvent("onmouseup",   dragStop);
  }
  if (browser.isNS) {
    document.removeEventListener("mousemove", dragGo,   true);
    document.removeEventListener("mouseup",   dragStop, true);
  }
}
