function MaisTipo(){
	if (document.getElementById('trtipo2').style.display == 'none')	{
		$('#trtipo2').show('slow'); 
	} else {
		$('#trtipo3').show('slow'); 
	}
}
function MenosTipo(){
	if (document.getElementById('trtipo3').style.display == 'block')
	//if ($('#trtipo3:hidden').length == 0)
		{ $('#trtipo3').hide('slow'); }
	else	
		{ $('#trtipo2').hide('slow'); }

}

function MaisSubTipo(){
	if (document.getElementById('trsubtipo2').style.display == 'none')
		{ $('#trsubtipo2').show('slow'); }
	else	
		{ $('#trsubtipo3').show('slow'); }

}
function MenosSubTipo(){
	if (document.getElementById('trsubtipo3').style.display == 'block')
		{ $('#trsubtipo3').hide('slow'); }
	else	
		{ $('#trsubtipo2').hide('slow'); }

}

function MaisBairro(){
	if (document.getElementById('trbairro2').style.display == 'none')
		{ $('#trbairro2').show('slow'); }
	else	
		{ $('#trbairro3').show('slow'); }

}
function MenosBairro(){
	if (document.getElementById('trbairro3').style.display == 'block')
		{ $('#trbairro3').hide('slow'); }
	else	
		{ $('#trbairro2').hide('slow'); }

}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
 
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function Trim(texto){
	texto = texto + "";
	texto = texto.replace(/^\s+|\s+$/g,"");
	return texto;
} 

function adicionaWishList(imovel){
		var imoveis = "";
		var verifica = "";
		var posicao = "";
		
		if (readCookie("WishList")){
			verifica = readCookie("WishList") + ','
			//alert(imovel);
			//alert(verifica);
			posicao = verifica.indexOf(imovel+',');
			//alert(posicao);
			
			if (verifica.indexOf(imovel+',') >= 0){
				alert("Este imóvel já está em seus favoritos.");	
				return false;
				imoveis = readCookie("WishList");
			} else {
				imoveis = imovel + ' ' + readCookie("WishList");
			}	
		} else {
			imoveis = imovel;
		}
		
		
		imoveis = Trim(imoveis).replace(" ",",");
		createCookie("WishList",imoveis,15);
		
		if (readCookie("WishList")){
			alert("Imóvel adicionado em seus favoritos.");
		} else {
			alert("Erro ao adicionar o imóvel em seus favoritos.");
		}			

} 

function removeWishList(imovel){
	if(confirm('Tem certeza que deseja remover este imóvel da sua lista de desejos?')){
		var imoveis = "";
				
		imoveis = readCookie("WishList") + ",";
		imoveis = imoveis.replace(imovel+",","");

		imoveis = imoveis.substring(0,(imoveis.length - 1));
		
		createCookie("WishList",imoveis,15);
		
		$("#imovel"+imovel).hide("slow");
		
			
	} else {
		return false;
	}
}

function addFav(){
    var url      = "http://www.movimentoimoveis.com.br";
    var title    = "Movimento Imóveis";
    if (window.sidebar) window.sidebar.addPanel(title, url,"");
    else if(window.opera && window.print){
        var mbm = document.createElement('a');
        mbm.setAttribute('rel','sidebar');
        mbm.setAttribute('href',url);
        mbm.setAttribute('title',title);
        mbm.click();
    }
    else if(document.all){window.external.AddFavorite(url, title);}
}			

function obterIndiceProximoElemento(campo) {
  var nObj, i;
  for (var i = 0; i < campo.form.elements.length; i++)
    if (campo == campo.form.elements[i]) {
      nObj = i;
    }
  for (var i = nObj + 1; i < campo.form.elements.length; i++)
    if (campo.form.elements[i].type != 'hidden' && !campo.form.elements[i].disabled && campo.form.elements[i].style.visibility != 'hidden') {
      return i;
    }
  return -1;
}

function editarCampo(evento, campo, tipo, mascara, prox, funcao) {
  var t, msk, virgula;
  if (campo.onblur == null)
    campo.onblur = campo.onfocus;
  if (campo.onkeyup == null)
    campo.onkeyup = campo.onfocus;
  if (campo.onkeypress == null)
    campo.onkeypress = campo.onfocus;
  if (evento.type == "focus") {
    campo.onfocus = null;
    return;
  }
  t = evento.keyCode;
  if (t == 9 || t == 16) {
    campo.focus();
    return;
  }
  if (funcao != null && evento.type == "keypress" && t == 13) {
    eval(funcao);
    return;
  }
  if (evento.type == "keypress") {
    return;
  }
  if (evento.type == "keypress" || evento.type == "keyup" || evento.type == "blur") {
    campo.value = eliminarCaracteresInvalidos(campo.value, tipo);
    campo.value = inserirMascara(campo.value, mascara);
    if (campo.value.length > campo.maxLength)
      campo.value = campo.value.substr(0, campo.maxLength);
    if (campo.value.length == campo.maxLength)
      if (prox != null){
      
        prox.focus();
      }  
      else {
        nOrd = obterIndiceProximoElemento(campo);
        if (nOrd > -1 && event.type != "blur") 
          campo.form.elements[nOrd].focus();
      }
  }
  if ((t>=16&&t<=20) || (t>=33&&t<=39) || (t>=44&&t<=45) || (t>=112&&t<=123) || (t>=144&&t<=145) || t==12)
    return true; 
  if ('ALNX'.indexOf(tipo, 0) != -1)
    campo.value = campo.value.toUpperCase();
  if (mascara != null && mascara != "" )
    if (campo.style.textAlign == "right") {
      msk = right(mascara, campo.value.length);
      campo.value = eliminarCaracteresInvalidos(campo.value, tipo);
      campo.value = eliminarZerosEsquerda(campo.value);
      mascara = inverter(mascara);
      virgula = mascara.indexOf(",")
      if (virgula > -1 && campo.value.length > 0)
        campo.value = preencheZeros(campo.value, virgula + 1);
      valor = inverter(campo.value);
      valor = inserirMascara(valor, mascara);
      campo.value = inverter(valor);
    }
    else {
      campo.value = eliminarCaracteresInvalidos(campo.value, tipo);
      campo.value = inserirMascara(campo.value, mascara);
      if (campo.value.length > campo.maxLength)
        campo.value = campo.value.substr(0, campo.maxLength);
    }
  return true;
}

function editarCampo2(evento, campo, tipo, mascara, prox, funcao) {
  var t, msk, virgula;
  if (campo.onblur == null)
    campo.onblur = campo.onfocus;
  if (campo.onkeyup == null)
    campo.onkeyup = campo.onfocus;
  if (campo.onkeypress == null)
    campo.onkeypress = campo.onfocus;
  if (evento.type == "focus") {
    campo.onfocus = null;
    return;
  }
  t = evento.keyCode;
  if (t == 9 || t == 16) {
    campo.focus();
    return;
  }
  if (funcao != null && evento.type == "keypress" && t == 13) {
    eval(funcao);
    return;
  }
  if (evento.type == "keypress") {
    return;
  }
  if (evento.type == "keypress" || evento.type == "keyup" || evento.type == "blur") {
    if (campo.value.length == campo.maxLength)
      if (prox != null){
      
        prox.focus();
      }  
      else {
        nOrd = obterIndiceProximoElemento(campo);
        if (nOrd > -1 && event.type != "blur") 
          campo.form.elements[nOrd].focus();
      }
  }
  if ((t>=16&&t<=20) || (t>=33&&t<=39) || (t>=44&&t<=45) || (t>=112&&t<=123) || (t>=144&&t<=145) || t==12)
    return true; 
  return true;
}



function leftTrim(cTexto) {
  var cNovoTexto = "";
  for (var i=0; i < cTexto.length; i++) {
    if (cTexto.substr(i, 1) != " ") { 
      cNovoTexto = cTexto.substr(i);
      break;
    }
  }
  return cNovoTexto;
}

function rightTrim(cTexto) {
  var cNovoTexto = "";
  for (var i = (cTexto.length - 1); i > -1 ; i--) {
    if (cTexto.substr(i, 1) != " ") { 
      cNovoTexto = cTexto.substr(0, i + 1);
      break;
    }
  }
  return cNovoTexto;
}

function allTrim(cTexto) {
  return leftTrim(rightTrim(cTexto));
}
function preencheZeros(Numero, Tamanho) {

  var Diferenca

  Numero = allTrim(Numero)
  Diferenca = Tamanho - Numero.length
  for (var i=0; i < Diferenca; i++)
    {
    Numero = "0" + Numero
    }

  return Numero

}
function inverter(s) {
  var i, ret;
  ret = "";
  i = s.length - 1;
  while (i >= 0) {
    ret += s.substr(i, 1);
    i--;
  }
  return ret;
}
function eliminarZerosEsquerda(valor) {
  var ret = "";
  var status = 0;
  for (var i = 0; i < valor.length; i++) {
    if (valor.substr(i, 1) != "0")
      status = 1;
    if (status == 1)
      ret += valor.substr(i, 1);
  }
  return ret;
}
function right(s, n) {
  return s.substr(s.length - n, n);
}
function inserirMascara(valor, mascara) {
  var fimValor, fimMascara, posV, posM, ret;
  ret = "";
  posV = 0;
  posM = 0;
  fimValor = false;
  fimMascara = (mascara.length == 0) ? true : false;
  while (posV < valor.length) {
    if (!fimMascara && mascara.substr(posM, 1) != "X") {
      ret += mascara.substr(posM, 1);
      posM++;
      if (posM == mascara.length)
        fimMascara = true;
      }
    else {
      ret += valor.substr(posV, 1);
      posV++;
      posM++;
    }
  }
  return ret;
}
function eliminarCaracteresInvalidos(valor, tipo) {
  var i, ret;
  if (tipo == "9")
    caracteres = "0123456789";
  else if (tipo == "A" || tipo == "a" )
    caracteres = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
  else if (tipo == "L" || tipo == "l" )
    caracteres = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  else if (tipo == "N" || tipo == "n" )
    caracteres = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  else if (tipo == "T" || tipo == "t" )
    caracteres = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ÁáÉéÍíÓóÚúÀàÇçÃãÕõ-=*&#+\/,.;:";
  ret = "";
  for (i = 0; i < valor.length; i++)
    if (tipo == 'x' || tipo == 'X')
      ret += valor.substr(i, 1);
    else
      if (caracteres.indexOf(valor.substr(i, 1), 0) != -1)
        ret += valor.substr(i, 1);
  return ret; 
}
function identBrowser() {
  if (document.all)
	return "ie";
  if (document.layers)
	return "ns4";
  if (document.getElementById)
	return "ns6";
}
function procurarObjetoPorTipo(nome, tipo) {
  var  xx, ii, obj;
  obj = null;
  if (identBrowser() == "ns6") {
    var vetor = document.getElementsByTagName(tipo);
    for (ii = 0; vetor.item(ii) != null; ii++) {
      if (nome == vetor.item(ii).getAttribute("name"))
        return vetor.item(ii);
    }
  }
  else {
    xx = (!document.all);
    if (!xx) 
      for (ii = 0; ii < document.all.length; ii++)
        if (document.all[ii].nodeName == tipo)
            if (document.all[ii].name == nome || document.all[ii].id == nome)
              return document.all[ii];
  }
  return obj;
}

function tiraPonto(str,numDecimais) 
//Tira ponto e define o número de casas decimais
{
  for (i = 0; i < str.length; i++) 
  {
    if (str.charAt(i) == ".")
    {
		return str.substring(0,i)+str.substring(i+1,i+numDecimais+1);
	}
  }
  return str;
}


function calculaTotal(campo){

  var a = 0;
  
  if (campo.name == "dependente")
	mascara = "XX";
  else
    mascara = "X.XXX.XXX.XXX.XXX.XXX.XXX.XXX.XXX.XXX,XX";
  msk = right(mascara, campo.value.length);
  campo.value = eliminarCaracteresInvalidos(campo.value, '9');
  campo.value = eliminarZerosEsquerda(campo.value);
  mascara = inverter(mascara);
  virgula = mascara.indexOf(",")
  if (virgula > -1 && campo.value.length > 0)
    campo.value = preencheZeros(campo.value, virgula + 1);
  valor = inverter(campo.value);
  valor = inserirMascara(valor, mascara);
  campo.value = inverter(valor);
  if (campo.value == "")
    {
	campo.value = "";
	}

  a = parseFloat(eliminarCaracteresInvalidos(document.form.valor.value, '9'));
  if ((document.form.valor.value == "") || (document.form.valor.value == null))
	document.form.valor.value= "";
}

jQuery(document).ready(function() {


	$("#frmAvalie input[type=text], #frmAvalie textarea, #frmAvalie select, #frmAvalie input[type=password]").blur(function() {
		if ($(this).val()=="") {
			$("#error_"+$(this).attr("id")).fadeIn("fast");
			$(this).addClass("input-error");				 
		} else {
			$("#error_"+$(this).attr("id")).hide();
			$(this).removeClass("input-error");
		}
		
		if ($(this).attr("id")=="email") {
			if (!(/^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,4}$/.test($("#email").val()))) {
				$("#error_email").fadeIn("fast");
				$("#email").addClass("input-error");
			}
		}		
	});	
	
	$("#frmAvalie").submit(function() {
		$("input[type=text], textarea").removeClass("input-error");
//		$("#error_contato").hide("fast");
		if ($("#nome").val()=="") {
			$("#error_nome").fadeIn("fast");
			$("#nome").addClass("input-error");
			return false;
		} else if (!(/^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,4}$/.test($("#email").val()))) {
			$("#error_email").fadeIn("fast");
			$("#email").addClass("input-error");
			return false;
		} else if ($("#tel").val()=="") {
			$("#error_tel").fadeIn("fast");
			$("#tel").addClass("input-error");
			return false;
		} else if ($("#endereco").val()=="") {
			$("#error_endereco").fadeIn("fast");
			$("#endereco").addClass("input-error");
			return false;
		} else if ($("#cidade").val()=="") {
			$("#error_cidade").fadeIn("fast");
			$("#cidade").addClass("input-error");
			return false;
		} else if ($("#uf").val()=="") {
			$("#error_uf").fadeIn("fast");
			$("#uf").addClass("input-error");
			return false;
		} else if ($("#mensagem").val()=="") {
			$("#error_mensagem").fadeIn("fast");
			$("#mensagem").addClass("input-error");
			return false;
		} else {
			return true;
		}
	});		

	$("#frmContato input[type=text], #frmContato textarea, #frmContato select, #frmContato input[type=password]").blur(function() {
		if ($(this).val()=="") {
			$("#error_"+$(this).attr("id")).fadeIn("fast");
			$(this).addClass("input-error");				 
		} else {
			$("#error_"+$(this).attr("id")).hide();
			$(this).removeClass("input-error");
		}
		
		if ($(this).attr("id")=="email") {
			if (!(/^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,4}$/.test($("#email").val()))) {
				$("#error_email").fadeIn("fast");
				$("#email").addClass("input-error");
			}
		}		
	});	
	
	$("#frmContato").submit(function() {
		$("input[type=text], textarea").removeClass("input-error");
//		$("#error_contato").hide("fast");
		if ($("#nome").val()=="") {
			$("#error_nome").fadeIn("fast");
			$("#nome").addClass("input-error");
			return false;
		} else if (!(/^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,4}$/.test($("#email").val()))) {
			$("#error_email").fadeIn("fast");
			$("#email").addClass("input-error");
			return false;
		} else if ($("#mensagem").val()=="") {
			$("#error_mensagem").fadeIn("fast");
			$("#mensagem").addClass("input-error");
			return false;
		} else {
			return true;
		}
	});		

	
});	

