
function atribuiValor(id_elemento, valor) {
	var elemento= document.getElementById(id_elemento);
	elemento.value= valor;
}

function atribuiClasse(id_elemento, classe) {
	var elemento= document.getElementById(id_elemento);
	elemento.className= classe;
}

function setaClasse(campo, classe) {
	if(classe != undefined) {
		if (campo.lastClass == undefined){
			campo.lastClass = null;
		}
		campo.lastClass = campo.className || "";
		campo.className = classe;
	}
	else
		campo.className = campo.lastClass;
}

function validaEmail(email) {
	var retorno= true;
	
	if (email=="")
		retorno= false;
	if (email.indexOf("@") < 2)
		retorno= false;
	if (email.indexOf(".") < 1)
		retorno= false;
	
	return(retorno);
}

function daFoco(id_elemento) {
	var elemento = document.getElementById(id_elemento);
	elemento.focus();
}

function validaData(data_nasc, periodo) {
	var erros = "";
	if (data_nasc=="")
		erros = "Entre com a data!\n";
	else {
		var dia= data_nasc.substring(0, 2);
		var mes= data_nasc.substring(3, 5);
		var ano= data_nasc.substring(6, 10);
		
		var barra1= data_nasc.substring(2, 3);
		var barra2= data_nasc.substring(5, 6);
		
		if ((barra1=="/") && (barra2=="/")) {
			var nonNumbers = /\D/;
						
			if ( (dia<=0) || (dia>31)  || (nonNumbers.test(dia)) )
				if (erros=="") erros = "Dia inválido\n";
			else {
				if ( ((mes=="02") || (mes=="04") || (mes=="06") || (mes=="09") || (mes=="11")) && (dia=="31") )
					if (erros=="") erros = "O mes informado não possui o dia 31!\n";
			}
			if ( (mes<=0) || (mes>12)  || (nonNumbers.test(mes)) )
				if (erros=="") erros = "Mês invalido\n";
			
			if (periodo=="n") {
				var dataAtual= new Date();
				var anoAtual= dataAtual.getFullYear();
				
				if ( (ano<=0) || (ano>anoAtual) || (nonNumbers.test(ano)) )
					if (erros=="") erros = "Ano inválido!\n";
			}
			//ano bissexto
			if ((ano%4!=0) && (mes==2) && (dia>28))
				if (erros=="") erros = "Dia inválido! \n";
		}
		else
			if (erros=="") erros = "Preencha a data no formato dd/mm/AAAA\n";
	}
	return(erros);
}

function validaForm(id_form) {
	
	var erro_alerta= "";
	var botao= document.getElementById("enviar");
	
	switch (id_form) {
		case 'formContato':
			
			var nome= document.formContato.nome;
			var email= document.formContato.email;
			var telefone= document.formContato.telefone;
			var mensagem= document.formContato.mensagem;
			var erro= null;
			var foco= null;
			
			if (nome.value=="") {
				if (foco==null) foco= nome;
				if (erro_alerta=="") erro_alerta= "Campo nome não pode ser vazio!";
				setaClasse(nome, "campo_erro");
				erro= 1;
			}
			if (!validaEmail(email.value))  {
				if (foco==null) foco= email;
				if (erro_alerta=="") erro_alerta= "E-mail inválido!";
				setaClasse(email, "campo_erro");
				erro= 1;
			}
			if (telefone.value=="") {
				if (foco==null) foco= telefone;
				if (erro_alerta=="") erro_alerta= "Campo telefone não pode ser vazio!";
				setaClasse(telefone, "campo_erro");
				erro= 1;
			}
			if (mensagem.value=="") {
				if (foco==null) foco= mensagem;
				if (erro_alerta=="") erro_alerta= "Campo mensagem não pode ser vazio!";
				setaClasse(mensagem, "campo_erro");
				erro= 1;
			}
			break;
		
		case 'formLogin':
			
			var email= document.formLogin.email;
			var senha= document.formLogin.senha;
			var erro= null;
			var foco= null;
			
			if (!validaEmail(email.value))  {
				if (foco==null) foco= email;
				if (erro_alerta=="") erro_alerta= "E-mail inválido!";
				setaClasse(email, "campo_erro");
				erro= 1;
			}
			if (senha.value=="") {
				if (foco==null) foco= senha;
				if (erro_alerta=="") erro_alerta= "Entre com a senha!";
				setaClasse(senha, "campo_erro");
				erro= 1;
			}
			break;
		case 'formNewsletter':
			
			var email= document.getElementById("email_news");
			var erro= null;
			
			if (!validaEmail(email.value))  {
				erro_alerta= "E-mail inválido!";
				erro= 1;
			}
			break;
		
		case 'formBusca':
			
			var t= document.getElementById("t");
			var erro= null;
			
			if (t.value=="")  {
				erro_alerta= "Selecione o tipo de imóvel!";
				erro= 1;
				t.focus();
			}
			break;
	}
	
	if (erro) {
		alert(erro_alerta);
		foco.focus();
		return false;
	}
	else {
		botao.disabled= true;
		botao.innerHTML= "Enviando...";
		//alert("merda");
		return true;
	}
}

function Limpar(valor, validos) {
	var result = "";
	var aux;
	for (var i=0; i < valor.length; i++) {
		aux = validos.indexOf(valor.substring(i, i+1));
		if (aux>=0) {
			result += aux;
		}
	}
	return result;
}

function formataValor(campo,teclapres) {
	var tammax = 200;
	var decimal = 2;
	var tecla = teclapres.keyCode;
	vr = Limpar(campo.value,"0123456789");
	tam = vr.length;
	dec=decimal
	
	if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }
	
	if (tecla == 8 )
	{ tam = tam - 1 ; }
	
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
	{
	
	if ( tam <= dec )
	{ campo.value = vr ; }
	
	if ( (tam > dec) && (tam <= 5) ){
	campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ; }
	if ( (tam >= 6) && (tam <= 8) ){
	campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ;
	}
	if ( (tam >= 9) && (tam <= 11) ){
	campo.value = vr.substr( 0, tam - 8 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; }
	if ( (tam >= 12) && (tam <= 14) ){
	campo.value = vr.substr( 0, tam - 11 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; }
	if ( (tam >= 15) && (tam <= 17) ){
	campo.value = vr.substr( 0, tam - 14 ) + "." + vr.substr( tam - 14, 3 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - 2, tam ) ;}
	}
} 

function formataData(val) {
   	var pass = val.value;
	var expr = /[0123456789]/;
		
	for(i=0; i<pass.length; i++){
		var lchar = val.value.charAt(i);
		var nchar = val.value.charAt(i+1);
	
		if(i==0) {
		   if ((lchar.search(expr) != 0) || (lchar>3)){
			  val.value = "";
		   }
		   
		}
		else if(i==1){
			   if(lchar.search(expr) != 0){
				  var tst1 = val.value.substring(0,(i));
				  val.value = tst1;				
 				  continue;			
			   }
			   
			   if ((nchar != '/') && (nchar != '')){
				 	var tst1 = val.value.substring(0, (i)+1);
				
					if(nchar.search(expr) != 0) 
						var tst2 = val.value.substring(i+2, pass.length);
					else
						var tst2 = val.value.substring(i+1, pass.length);
	
					val.value = tst1 + '/' + tst2;
			   }

		 }else if(i==4){
			
				if(lchar.search(expr) != 0){
					var tst1 = val.value.substring(0, (i));
					val.value = tst1;
					continue;			
				}
		
				if	((nchar != '/') && (nchar != '')){
					var tst1 = val.value.substring(0, (i)+1);

					if(nchar.search(expr) != 0) 
						var tst2 = val.value.substring(i+2, pass.length);
					else
						var tst2 = val.value.substring(i+1, pass.length);
	
					val.value = tst1 + '/' + tst2;
				}
   		  }
		
		  if(i>=6){
			  if(lchar.search(expr) != 0) {
					var tst1 = val.value.substring(0, (i));
					val.value = tst1;			
			  }
		  }
	 }
	
     if (pass.length>10)
		val.value = val.value.substring(0, 10);
	 	return true;
}

function sohNumeros(numero) {
	var nonNumbers = /\D/;
	if (nonNumbers.test(numero))
		return(false);
	else
		return(true);
}

function verificaNumeros(evtKeyPress) {
	if (document.all) { // Internet Explorer
		nTecla = evtKeyPress.keyCode;
		} else if(document.layers) { // Nestcape
			nTecla = evtKeyPress.which;
		} else {
			nTecla = evtKeyPress.which;
			if (nTecla == 8) {
				return true;
			}
		}
	
	if (((nTecla > 47) && (nTecla < 58)) || (nTecla==0) || (nTecla==8))
		return(true);
	else
		return(false);
}

function calculaTudo() {
	var l_h= parseFloat(document.getElementById("l_h").value);
	var l_m= parseFloat(document.getElementById("l_m").value);
	var duracao= parseFloat(document.getElementById("duracao").value);
	
	var valor= ((l_m+l_h)*duracao);
	valor= valor+"";
	
	if (valor.indexOf('.')!==-1) {
		var quebrada= valor.split('.', 2);
		var decimal= quebrada[1].substr(0, 2);
		retorno= quebrada[0] +","+decimal;
	}
	else
		retorno= valor;
	
	var total_final= document.getElementById("total_final");
	total_final.innerHTML= "<div class='fl'>Para sua festa bombar, voc&ecirc; vai precisar de:</div><br class='limpa' /><br /> <div id='calculo' class='calculo_total'>"+retorno+"</div> <div class='cpl'>litros de Chopp Strauss!</div><br class='limpa' /><br />Para solicitar o Chopp ligue para: <div class='fontedestaque'>(48) 3462 6006</div>";
}

function calculaLitros(tipo) {
	var litros;
	var retorno;
	var campo= document.getElementById("l_"+tipo);
	
	switch(tipo) {
		case 'h': litros= 1.5; break;
		case 'm': litros= 1; break;
		case 'p': litros= 2.5; break;
	}
	
	var qtde= document.getElementById("qtde_"+tipo).value;
	
	if (isNaN(parseInt(qtde)))
	   alert("Somente n&uacute;meros!");
	else {
		var calculo= document.getElementById("calculo_"+tipo);
		var valor_real= (qtde*litros);
		
		campo.setAttribute("value", valor_real);
		
		var valor= valor_real+"";
		if (valor.indexOf('.')!==-1) {
			var quebrada= valor.split('.', 2);
			var decimal= quebrada[1].substr(0, 2);
			retorno= quebrada[0] +","+decimal;
		}
		else
			retorno= valor;
		
		calculo.innerHTML= retorno;
	}
}