function checaResolucao() {
	var resolucao= screen.width;
	var largura;
	
	if (resolucao<801) {
		var conteudo= document.getElementById("conteudo");
		conteudo.style.width= "60%";
	}
}

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 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 'formHospedagem':
			
			var nome= document.getElementById("nome");
			var email= document.getElementById("email");
			var telefone= document.getElementById("telefone");
			var plano= document.getElementById("plano");
			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 (plano.value=="") {
				if (foco==null) foco= plano;
				if (erro_alerta=="") erro_alerta= "Selecione um plano!";
				setaClasse(mensagem, "campo_erro");
				erro= 1;
			}
			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;
}
