// JavaScript Document

/*Hora no javascript*/
function HoraJavascript(){
	var now = new Date();
	var hours = now.getHours();
	var minutes = now.getMinutes();
	var seconds = now.getSeconds()
	//var timeValue = "" + ((hours >12) ? hours -12 :hours)
	var timeValue = "" + hours
	timeValue += ((minutes < 10) ? ":0" : ":") + minutes
	timeValue += ((seconds < 10) ? ":0" : ":") + seconds
	//timeValue += (hours >= 12) ? " P.M." : " A.M."
	document.getElementById('hora').innerHTML =  timeValue;
	timerID = setTimeout("HoraJavascript()",1000);
	timerRunning = true;	
}

/* Muda Cor de fundo das <TR>*/
function MudaCor(NameObj,cor){
	var Muda = document.getElementById(NameObj).style.backgroundColor = cor;
}

/* Função que volta valor default do campo input*/
function DefaultTextoInput(obj,valorPadrao){
	if(obj.value == "" || obj.value == null || obj.value == 0){
		obj.value = valorPadrao;
	}
}

/* Função que retira valor default do campo input*/
function RetiraTextoInput(obj,valorPadrao){
	if(obj.value == valorPadrao || obj.value == "" || obj.value == null || obj.value == 0){
		obj.value = "";
	}
}

/* Função que rabre pop-up*/
function abrirPopUp(pagina,largura,altura) {
	//pega a resolução do visitante
	w = screen.width;
	h = screen.height;
	
	//divide a resolução por 2, obtendo o centro do monitor
	meio_w = w/2;
	meio_h = h/2;
	
	//diminui o valor da metade da resolução pelo tamanho da janela, fazendo com q ela fique centralizada
	altura2 = altura/2;
	largura2 = largura/2;
	meio1 = meio_h-altura2;
	meio2 = meio_w-largura2;
	
	//abre a nova janela, já com a sua devida posição
	window.open(pagina,'','height=' + altura + ', width=' + largura + ', top='+meio1+', left='+meio2+' scrollbars=yes');
}

function Contar(Campo, Tamanho, DIV){
	if((Tamanho-Campo.value.length)<=1) Campo.value = Campo.value.substring(0,Tamanho);
	document.getElementById(DIV).value = Tamanho-Campo.value.length;
}

function msn(M){
    alert(M);
}

//FUNÇÃO DE RETIRAR CAMPO
function dropFile_uni(btn){
	
	var confirma = confirm("Deseja exluir este item? ");
	if(confirma == true){
		if(document.getElementById) {
			tr = btn;
			while (tr.tagName != 'TR') tr = tr.parentNode;
			tr.parentNode.removeChild(tr);
			checkForLast_uni();
		}
		
	}else{
		return false;
	}
}

//FUNÇÃO DE ADICIONAR CAMPO
function addFile_uni(btn){
	if(document.getElementById) {
		tr = btn;
		while (tr.tagName != 'TR') tr = tr.parentNode;
		var idSuffix = Math.round(Math.random()*1000);
		var newTr = tr.parentNode.insertBefore(tr.cloneNode(true),tr.nextSibling);
		thisChilds = newTr.getElementsByTagName('td');
		for (var i = 0; i < thisChilds.length; i++){
			if (thisChilds[i].className == 'header') thisChilds[i].innerHTML = '';
			if (thisChilds[i].className == 'files') thisChilds[i].innerHTML = 
			'';
		}
		checkForLast_uni();
	}
}

//FUNÇÃO DE RETIRAR CAMPO CHECADO
function checkForLast_uni(){
	btns = document.getElementsByName('drop');
	for (i = 0; i < btns.length; i++){
		btns[i].disabled = (btns.length == 1) ? true : false;
	}
}

function valida(){
	var cpf_meninao = "";
	cpf_meninao = document.getElementById('cpf_anuncio').value;
	cpf_meninao = cpf_meninao.replace(".", "");
	cpf_meninao = cpf_meninao.replace(".", "");
	cpf_meninao = cpf_meninao.replace(".", "");
	cpf_meninao = cpf_meninao.replace("-", "");
	
	//alert(cpf_meninao);
	
	if(valida_cpf(cpf_meninao))
		return true;
	else
		return false;
}
			
function valida_cpf(cpf){
	  var numeros, digitos, soma, i, resultado, digitos_iguais;
	  digitos_iguais = 1;
	  if (cpf.length < 11)
			return false;
	  for (i = 0; i < cpf.length - 1; i++)
			if (cpf.charAt(i) != cpf.charAt(i + 1))
				  {
				  digitos_iguais = 0;
				  break;
				  }
	  if (!digitos_iguais)
			{
			numeros = cpf.substring(0,9);
			digitos = cpf.substring(9);
			soma = 0;
			for (i = 10; i > 1; i--)
				  soma += numeros.charAt(10 - i) * i;
			resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
			if (resultado != digitos.charAt(0))
				  return false;
			numeros = cpf.substring(0,10);
			soma = 0;
			for (i = 11; i > 1; i--)
				  soma += numeros.charAt(11 - i) * i;
			resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
			if (resultado != digitos.charAt(1))
				  return false;
			return true;
			}
	  else
			return false;
}

function Etapas(etapa,validcep){
		var DivEtapa = new Array('Etapa_1','Etapa_2','Etapa_3','Etapa_4','Etapa_5','Etapa_6');
		var EtapaAtual = etapa;
		
		/* Inibe todas as Etapas*/
		for(i=0;i < DivEtapa.length; i++){
			document.getElementById(DivEtapa[i]).style.display = 'none';
		}
		
		var VariablesValid = new Array();
		
		if(etapa == false){
			document.getElementById('Etapa_1').style.display = 'block';
		}else{
			/* REGRAS DAS ETAPAS*/
			switch(EtapaAtual){
				case 'Etapa_1':
					var titulo = document.getElementById('titulo_anuncio').value;
					var TIP = document.getElementById('TIPDEENDERECO').value;
					var cpf = document.getElementById('cpf_anuncio').value;
					if(titulo.length < 3){
						alert("O Título do anuncio deve conter mais de 3 caracteres!");
						document.getElementById('Etapa_1').style.display = 'block';
						document.getElementById('titulo_anuncio').focus();
						return false;
					}
					
					if(document.getElementById('nivel_user_adm').value != '1'){
						if(valida() != true){
							alert("CPF inválido. Campo obrigatório no cadastro do imóvel.!");
							document.getElementById('Etapa_1').style.display = 'block';
							document.getElementById('cpf_anuncio').focus();
							return false;
						}
					}
					
					if(TIP == '2'){
						var end = document.getElementById('end_endereco').value;
						var VAR = end;
						var VAR2 = document.getElementById('tipo_logr').value;
						if(end.length < 1){
							alert("Você deve informar o endereço!");
							document.getElementById('Etapa_1').style.display = 'block';
							document.getElementById('end_endereco').focus();
							return false;
						}
					}else{
						var cep = document.getElementById('cep_endereco').value;
						var VAR = cep;
						var VAR2 = 0 +'&nro='+document.getElementById('nro_atu').value;
						if(validcep == true){
							if(cep.length < 9){
							alert("Você deve informar o CEP");
							document.getElementById('Etapa_1').style.display = 'block';
							document.getElementById('cep_endereco').focus();
							return false;
							}
						}
					}
					
					document.getElementById('Etapa_2').style.display = 'block';
					LocalizaEndereco(VAR,VAR2,TIP);
					document.getElementById('Macro_titulo').innerHTML = document.getElementById('titulo_anuncio').value;
					document.getElementById('img_et_02').src = "../img/etapas/etapa_2_3.png";
					
				break;
				case 'Etapa_2':
					document.getElementById('Etapa_3').style.display = 'block';
					document.getElementById('Macro_cidade').innerHTML = document.getElementById('vis_uf').value;
					document.getElementById('Macro_cidade').innerHTML += ' - '+ document.getElementById('vis_cid').value;
					document.getElementById('Macro_cidade').innerHTML += ' - '+ document.getElementById('vis_bai').value;
					document.getElementById('Macro_endereco').innerHTML = document.getElementById('vis_end').value;
					var num = document.getElementById('n_endereco').value;
					num = num.length > 0 ? ', '+num : "";
					document.getElementById('Macro_endereco').innerHTML += num ;
					/*Desabilita todas etapas de tipo de imóvel*/
					document.getElementById('apartamento').style.display = 'none';
					document.getElementById('casa').style.display = 'none';
					document.getElementById('comercial').style.display = 'none';
					document.getElementById('loteamento').style.display = 'none';
					/*Verificando qual o tipo do imóvel*/
					switch(document.getElementById('idtipo_imovel').value){
						case '1':
							document.getElementById('apartamento').style.display = 'block';
							document.getElementById('Macro_tipoimovel').innerHTML = 'Tipo: Apartamento';
						break;
						case '2':
							document.getElementById('casa').style.display = 'block';
							document.getElementById('Macro_tipoimovel').innerHTML = 'Tipo: Casa';
						break;
						case '6':
							document.getElementById('comercial').style.display = 'block';
							document.getElementById('Macro_tipoimovel').innerHTML = 'Tipo: Comercial';
						break;
						case '5':
							document.getElementById('loteamento').style.display = 'block';
							document.getElementById('Macro_tipoimovel').innerHTML = 'Tipo: Loteamento';
						break;
						default:
							document.getElementById('apartamento').style.display = 'block';
							document.getElementById('Macro_tipoimovel').innerHTML = 'Tipo: Apartamento';
					}
					document.getElementById('img_et_03').src = "../img/etapas/etapa_3_3.png";
				break;
				case 'Etapa_3':
					switch(document.getElementById('idtipo_imovel').value){
						case '2':
							var idade = document.getElementById('idade_emp').value;
							var area = document.getElementById('area_util_casa').value;
							var valor = document.getElementById('preco_do_imovel_casa').value;
							if(area.length < 1 || area < 16){
								alert("Você deve informar a área útil do imóvel. Não pode ser menor do que 16m²!");
								document.getElementById('Etapa_3').style.display = 'block';
								document.getElementById('area_util_casa').focus();
								return false;
							}
							if(valor.length < 1){
								alert("Você deve informar o preço do imóvel!");
								document.getElementById('Etapa_3').style.display = 'block';
								document.getElementById('preco_do_imovel_casa').focus();
								return false;
							}
							if(idade.length < 1){
								alert("Você deve informar a idade do imóvel!");
								document.getElementById('Etapa_3').style.display = 'block';
								document.getElementById('idade_emp').focus();
								return false;
							}
						break;
						case '6':
							var idade = document.getElementById('idade_emp').value;
							var area = document.getElementById('area_util_comercial').value;
							var valor = document.getElementById('preco_do_imovel_comercial').value;
							if(area.length < 1 || area < 16){
								alert("Você deve informar a área útil do imóvel. Não pode ser menor do que 16m²!");
								document.getElementById('Etapa_3').style.display = 'block';
								document.getElementById('area_util_comercial').focus();
								return false;
							}
							if(valor.length < 1){
								alert("Você deve informar o preço do imóvel!");
								document.getElementById('Etapa_3').style.display = 'block';
								document.getElementById('preco_do_imovel_comercial').focus();
								return false;
							}
							if(idade.length < 1){
								alert("Você deve informar a idade do imóvel!");
								document.getElementById('Etapa_3').style.display = 'block';
								document.getElementById('idade_emp').focus();
								return false;
							}
						break;
						case '5':
							var area = document.getElementById('area_util_loteamento').value;
							var valor = document.getElementById('preco_do_imovel_loteamento').value;
							
							if(valor.length < 1){
								alert("Você deve informar o preço do terreno/loteamento!");
								document.getElementById('Etapa_3').style.display = 'block';
								document.getElementById('preco_do_imovel_loteamento').focus();
								return false;
							}
							
						break;
						default:
							var idade = document.getElementById('idade_emp').value;
							var area = document.getElementById('area_util_apartamento').value;
							var valor = document.getElementById('preco_do_imovel_apartamento').value;
							if(area.length < 1 || area < 16){
								alert("Você deve informar a área útil do imóvel. Não pode ser menor do que 16m²!");
								document.getElementById('Etapa_3').style.display = 'block';
								document.getElementById('area_util_apartamento').focus();
								return false;
							}
							if(valor.length < 1){
								alert("Você deve informar o preço do imóvel!");
								document.getElementById('Etapa_3').style.display = 'block';
								document.getElementById('preco_do_imovel_apartamento').focus();
								return false;
							}
							if(idade.length < 1){
								alert("Você deve informar a idade do imóvel!");
								document.getElementById('Etapa_3').style.display = 'block';
								document.getElementById('idade_emp').focus();
								return false;
							}
						
					}
					document.getElementById('Etapa_4').style.display = 'block';
					document.getElementById('img_et_04').src = "../img/etapas/etapa_4_3.png";
				break;
				case 'Etapa_4':
					document.getElementById('Etapa_5').style.display = 'block';
					document.getElementById('img_et_05').src = "../img/etapas/etapa_5_3.png";
				break;
				case 'Etapa_5':
					document.getElementById('Etapa_6').style.display = 'block';
					document.getElementById('img_et_06').src = "../img/etapas/etapa_6_3.png";
				break;
				case 'Etapa_6':
					
				break;
				default:
					document.getElementById('Etapa_2').style.display = 'block';
			}
		}
		
}

function VoltarEtapa(Etapa){
		var DivEtapa = new Array('Etapa_1','Etapa_2','Etapa_3','Etapa_4','Etapa_5','Etapa_6');
		var EtapaAtual = Etapa;
		
		/* Inibe todas as Etapas*/
		for(i=0;i < DivEtapa.length; i++){
			document.getElementById(DivEtapa[i]).style.display = 'none';
		}
		
		document.getElementById(EtapaAtual).style.display = 'block';
}

function LocalizaEndereco(VAR,VAR2,TIPO){
	AjaxEnvia('GET','Formulario','MultiplosEndereco.php?var='+VAR+'&var2='+VAR2+'&tipo='+TIPO,'Etapa_2',true);	
}

function NewAddEnd(){
	AjaxEnvia('GET','Formulario','novoEndereco.php','Etapa_2',true);	
}

function LocalizeCidade(uf){
	AjaxEnvia('GET','Formulario','locCidade.php?uf='+uf,'newEndCidade',false);
}

function LocalizeBairro(idcidade){
	AjaxEnvia('GET','Formulario','locBairro.php?idcidade='+idcidade,'newEndBairro',false);
}


function GravaNewEnderecoJob(uf,cidade,bairro,tipo_logr,endereco,cep){
	if(uf.length =='0' || cidade.length =='0' || bairro.length =='0' || endereco.length =='0'){
		alert('Campos UF, Cidade, Bairro, endereco sao obrigatorios!');
	}else{
		AjaxEnvia('GET','Formulario','GravaEnderecoNovo.php?uf='+uf+'&cidade='+cidade+'&bairro='+bairro+'&tipo_logr='+tipo_logr+'&endereco='+endereco+'&cep='+cep,'Etapa_2',false);
	}
}
