// JavaScript Document


// Chama a função loadFunctions ao carregar a página
	window.onload = loadFunctions;

// Função que chama outras funções
	function loadFunctions() {
		iniciaListBox();
	}
	
	function iniciaListBox(){
		var campo = gE('irTipoL');
		if (gE('irTipoL')){
			campo.setAttribute('checked', 'checked');
			insereListBox(campo.value);
		}
	}
	

	
// Utilizada para evitar digitar o document.getElementById
function gE(ID){
	return document.getElementById(ID);
}

// Utilizada para evitar digitar o document.getElementsByTagName
function gEs(tag) {
	return document.getElementsByTagName(tag);
}


//função destinada para validar o cep
	function validaCEP(param){
		return param.search(/\d{8}/)==0;
	}

	function vCEP(a, url){
		// pega a qtde de caracteres que o o usuario digitou em um campo
		var cont = a.value.length;
				
		if (cont == 8){ // se o que o cara digitou for igual a 8 caracteres
		
			var validaCep = validaCEP(a.value);
				
			if (validaCep == true){ // verifica se o cara digitou apenas numeros
				criaEnviando('Carregando');
				document.location = url+'?vcep='+a.value;
			}else{
				alert("CEP inválido");
			}
		}
	}
	
	
	function criaEnviando(texto){
		var tagBody = gEs('body').item(0);
		var box = document.createElement('div');
		box.setAttribute('id','boxEnviando');
		if (!gE('boxEnviando')) {
			tagBody.insertBefore(box, tagBody.firstChild);
			gE('boxEnviando').innerHTML = texto;
		}
	}
	
	
	
	
/* função para pular de um inputtext para o outro automatico apos digitar um numero x de caracteres */
   VerifiqueTAB=true;
   function Mostra(quem, tammax)
   {
   if ( (quem.value.length == tammax) && (VerifiqueTAB) )
   { 
      var i=0,j=0, indice=-1;
      for (i=0; i<document.forms.length; i++) 
      { 
          for (j=0; j<document.forms[i].elements.length; j++)  
          { 
               if (document.forms[i].elements[j].name == quem.name) 
               { 
                     indice=i;
                     break;
          } 
          } 
          if (indice != -1)
            break; 
      } 
      for (i=0; i<=document.forms[indice].elements.length; i++) 
      { 
          if (document.forms[indice].elements[i].name == quem.name) 
          { 
              while ( (document.forms[indice].elements[(i+1)].type == "hidden") && (i < document.forms[indice].elements.length) )
              { 
                   i++;
              } 
              document.forms[indice].elements[(i+1)].focus();
              VerifiqueTAB=false;
              break;
          } 
      } 
   } 
  } 
function PararTAB(quem)
{ 
   VerifiqueTAB=false;
} 
function ChecarTAB()
{ 
   VerifiqueTAB=true;
} 


// validações de formulários

	function validaEmail(email){
		return email.search(/(\w[\w\.\+]+)@(.+)\.(\w+)$/)==0;
	}


	function validaFrmTenhoImovel(){
		
		var cep = gE('f_cep');
		var tplogradouro = gE('f_tplogradouro');
		var logradouro = gE('f_logradouro');
		var numero = gE('f_numero');
		var bairro = gE('f_bairro');
		var cidade = gE('f_cidade');
		var uf = gE('f_uf');
		var tipo = gE('f_tipo');
		var tpimovel = gE('f_tpimovel');
		var nome = gE('f_nome');
		var email = gE('f_email');
		
		var ddd1 = gE('f_ddd1');
		var pref1 = gE('f_pref1');
		var num1 = gE('f_num1');		
		var telefone = ddd1.value + '' + pref1.value + '' + num1.value;
		
		var ddd2 = gE('f_ddd2');
		var pref2 = gE('f_pref2');
		var num2 = gE('f_num2');		
		var celular = ddd2.value + '' + pref2.value + '' + num2.value;
		
		
		if (cep.value == '' || cep.value == null){
			alert('Campo CEP em branco');
			cep.focus();
			return false;
		}
		
		if (tplogradouro.value == '' || tplogradouro.value == null){
			alert('Informe o CEP do imóvel');
			tplogradouro.focus();
			return false;
		}
		
		if (logradouro.value == '' || logradouro.value == null){
			alert('informe o logradouro do imóvel');
			logradouro.focus();
			return false;
		}
		
		if (numero.value == '' || numero.value == null){
			alert('Informe o número do imóvel');
			numero.focus();
			return false;
		}
		
		if (bairro.value == '' || bairro.value == null){
			alert('Informe o bairro do imóvel.');
			bairro.focus();
			return false;
		}
		
		if (cidade.value == '' || cidade.value == null){
			alert('Informe a cidade do imóvel.');
			cidade.focus();
			return false;
		}
		
		if (uf.value == '' || uf.value == null){
			alert('Informe o estado do imóvel.');
			uf.focus();
			return false;
		}
		
		if (tipo.value == '' || tipo.value == null){
			alert('Selecione um tipo.');			
			return false;
		}
		
		if (tpimovel.value == '' || tpimovel.value == null){
			alert('Selecione um tipo de imóvel.');		
			return false;
		}
		
		if (nome.value == '' || nome.value == null){
			alert('Informe um nome para contato.');
			nome.focus();
			return false;
		}
		
		
		if (email.value == '' || email.value == null){
			alert('Informe um e-mail para contato.');
			email.focus();
			return false;
		}
		
		var vemail = validaEmail(email.value);
		
		if (vemail == false){
			alert('E-mail inválido');
			email.focus();
			return false;
		}
		
		if (telefone == '' || telefone == null){
			telefone = celular;
			if (telefone == '' || telefone == null){
				alert('Informe pelo menos 1 telefone para contato');
				return false;
			}
		}
		
		
		return true;
	}
	
	function validaFrmCotato(){
		var nome = gE('f_cont_nome');
		var email = gE('f_cont_email');
		var assunto = gE('f_cont_assunto');
		var msg = gE('f_cont_msg');
		
		if (nome.value == '' || nome.value == null){
			alert('Campo Nome em branco');
			nome.focus();
			return false;
		}
		
		if (email.value == '' || email.value == null){
			alert('Campo e-mail em branco');
			email.focus();
			return false;
		}
		
		var vemail = validaEmail(email.value);
		
		if (vemail == false){
			alert("Informe um e-mail válido.");
			email.focus();
			return false;
		}
		
		if (assunto.value == '' || assunto.value == null){
			alert('Campo Assunto em branco');
			assunto.focus();
			return false;
		}
		
		if (msg.value == '' || msg.value == null){
			alert('Campo Mensagem em branco');
			msg.focus();
			return false;
		}		
		
		return true;
	}
	
	
	
	// 





function insereListBox(tipo){
	var lbValor = gE('itValor');
	
	
	
		if ( tipo == 'L'){
			lbValor.innerHTML = '';	
			
			var novo = document.createElement("option");
			novo.setAttribute("id", "opcoes0");
			novo.value = '0';
			novo.text  = 'Todos';
			lbValor.options.add(novo);
			
			var novo = document.createElement("option");
			novo.setAttribute("id", "opcoes1");
			novo.value = '1';
			novo.text  = 'Até 200,00';	
			lbValor.options.add(novo);
			
			var novo1 = document.createElement("option");
			novo1.setAttribute("id", "opcoes2");
			novo1.value = '2';
			novo1.text  = '201,00 até 300,00';	
			lbValor.options.add(novo1);
			
			var novo2 = document.createElement("option");
			novo2.setAttribute("id", "opcoes3");
			novo2.value = '3';
			novo2.text  = '301,00 até 400,00';	
			lbValor.options.add(novo2);
			
			var novo3 = document.createElement("option");
			novo3.setAttribute("id", "opcoes4");
			novo3.value = '4';
			novo3.text  = '401,00 até 500,00';	
			lbValor.options.add(novo3);
			
			var novo4 = document.createElement("option");
			novo4.setAttribute("id", "opcoes5");
			novo4.value = '5';
			novo4.text  = '501,00 até 600,00';	
			lbValor.options.add(novo4);
			
			var novo5 = document.createElement("option");
			novo5.setAttribute("id", "opcoes6");
			novo5.value = '6';
			novo5.text  = '601,00 até 800,00';	
			lbValor.options.add(novo5);
			
			var novo6 = document.createElement("option");
			novo6.setAttribute("id", "opcoes7");
			novo6.value = '7';
			novo6.text  = 'Acima de R$ 800,00';	
			lbValor.options.add(novo6);		
		}
		
		if (tipo == 'V'){
			lbValor.innerHTML = '';			
			
			var novo = document.createElement("option");
			novo.setAttribute("id", "opcoes0");
			novo.value = '0';
			novo.text  = 'Todos';
			lbValor.options.add(novo);
			
			var novo = document.createElement("option");
			//atribui um ID a esse elemento
			novo.setAttribute("id", "opcoes1");
			novo.value = '1';
			novo.text  = 'Até 20.000,00';	
			lbValor.options.add(novo);
			
			var novo1 = document.createElement("option");
			novo1.setAttribute("id", "opcoes2");
			novo1.value = '2';
			novo1.text  = '20.001,00 até 40.000,00';	
			lbValor.options.add(novo1);
			
			var novo2 = document.createElement("option");
			novo2.setAttribute("id", "opcoes3");
			novo2.value = '3';
			novo2.text  = '40.001,00 até 60.000,00';	
			lbValor.options.add(novo2);
			
			var novo3 = document.createElement("option");
			novo3.setAttribute("id", "opcoes4");
			novo3.value = '4';
			novo3.text  = '60.001,00 até 80.000,00';	
			lbValor.options.add(novo3);
			
			var novo4 = document.createElement("option");
			novo4.setAttribute("id", "opcoes5");
			novo4.value = '5';
			novo4.text  = '80.001,00 até 120.000,00';	
			lbValor.options.add(novo4);
			
			var novo5 = document.createElement("option");
			novo5.setAttribute("id", "opcoes6");
			novo5.value = '7';
			novo5.text  = 'Acima de R$ 120.000,00';	
			lbValor.options.add(novo5);			
		}
	
}





// destinada a ajax

// função que faz a mágica, rsss
function openAjax() {
	var ajax;
	try {
		ajax = new XMLHttpRequest();
	} catch(ee) {
		try {
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				ajax = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(E) {
				ajax = false;
			}
		}
	}
	return ajax;
}

	function setLoading(opt, local){
		var refer = gE(local);
		
		if (opt == true) {						
			refer.style.cursor = 'wait';						
			var img = document.createElement('img');			
			img.setAttribute('src','img/loader.gif');			
			img.setAttribute('id','loading');			
			img.setAttribute('width','16');						
			if (!document.getElementById('loading')) {				
				refer.insertBefore(img, refer.firstChild);				
			}
		}else if (opt == false) {			
			var imgLoading = gE('loading');	
			refer.style.cursor = 'default';
			if (imgLoading) {
				imgLoading.parentNode.removeChild(imgLoading);
			}
		}
	}
	
	function abrePaginaAjax(url, local){
		var conteudo = gE(local);
		var ajax = openAjax();
		ajax.open('GET', url, true);
		ajax.onreadystatechange = function() {
			if (ajax.readyState == 1){
				conteudo.innerHTML = '';
				setLoading(true, local);
			} // if->readyState->1
			if (ajax.readyState == 4){				
				if (ajax.status == 200){				
					setLoading(false, local);										
					conteudo.innerHTML = ajax.responseText;
				}// id->ajax.status->200
				else{
					conteudo.innerHTML = "Erro ao carregar a dados";			
				}
			}// if->readyState->4
		}// ajax.readystatechange
		ajax.send(null);
		return false;		
	}
