// JavaScript Document
/* Scripts para validação de formularios de CONTATO, PRE-RESERVA, INDICAÇÃO */

	function Tecla(e){
		var tecla;
		if(window.event) {
			// for IE, e.keyCode or window.event.keyCode can be used
			tecla = e.keyCode; 
		}
		else if(e.which) {
			// netscape or firefox
			tecla = e.which; 
		}
		else {
			// no event, so pass through
			return true;
		}
	
		if(tecla > 47 && tecla < 58) // numeros de 0 a 9
			return true;
		else
		{
			if (tecla != 8) // backspace
				return false;
			else
				return true;
		}
	
	}

	function Formatar_Telefone(objeto,tammax,teclapres)
	{
		var tecla;// = teclapres.keyCode;
		if(window.event) {
			// for IE, e.keyCode or window.event.keyCode can be used
			tecla = teclapres.keyCode; //e.keyCode; 
		}
		else if(e.which) {
			// netscape or firefox
			tecla = teclapres.which;//e.which; 
		}

		vr = objeto.value;
		vr = vr.replace( "(", "" );
		vr = vr.replace( ")", "" );
		vr = vr.replace( " ", "" );
		vr = vr.replace( "-", "" );
		tam = vr.length;
		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 <= 4 ) { 
				objeto.value = vr ;
			}
			if ( (tam > 4) && (tam <= 8) ) {
				objeto.value = vr.substr(0,tam-4) + '-' + vr.substr( tam - 4, tam ) ;
			}
			if ( (tam >= 9) && (tam <= 10) ) {
				objeto.value = '(' + vr.substr(0,2) + ') ' + vr.substr(2,tam-6) + '-' + vr.substr(tam-4,tam) ;			
			}
		}		
	}

	function Validar_Email(email)
	{
		if(email.length < 6) {
			return false;
		}
		var x = 0;
		for (var c=0;c<email.length;c++) {
			if (email.substring(c,c+1) == '@') {
				x = c;
			}
		}
		var y = 0;
		if (x > 0) {
			for (c=x;c<email.length;c++) {
				if (email.substring(c,c+1)=='.') {
					y = c;
					var valida = 1;
				}
			}
			if (y > 0) {
				var dominio = '';
				for (c=x;c<y;c++) {
					dominio = dominio + email.substring(1,c);
				}
			}
		}
		else {
			return false;
		}					
		if (y <= x+2){
			return false;
		}				
		if (valida == 1){
			return true;
		}
	}

function enviaForm(area){
	switch (area){
		case "contatoFerro":
			if(valida_contatoFerro()){
				document.formContato.submit();
			}
		break;	
		case "contatoEletro":
			if(valida_contatoEletro()){
				document.formContato.submit();
			}
		break;	
		case "contatoAluminio":
			if(valida_contatoAluminio()){
				document.formContato.submit();
			}
		break;	
		case "boletimEletro":
			if(valida_boletimEletro()){
				document.formBeletim.submit();
			}
		break;	
	}
}
function limpaForm(area){
	switch (area){
		case "contatoFerro":
			document.getElementById('txt_nome').value = "";
			document.getElementById('txt_email').value = "";
			document.getElementById('txt_fone').value = "";
			document.getElementById('txt_endereco').value = "";
			document.getElementById('txt_cidade').value = "";
			document.getElementById('txt_estado').value = "";
			document.getElementById('txt_assunto').value = "";
			document.getElementById('txt_msg').value = "";
		break;	
		case "contatoEletro":
			document.getElementById('txt_nome').value = "";
			document.getElementById('txt_email').value = "";
			document.getElementById('txt_fone').value = "";
			document.getElementById('txt_endereco').value = "";
			document.getElementById('txt_cidade').value = "";
			document.getElementById('txt_estado').value = "";
			document.getElementById('txt_assunto').value = "";
			document.getElementById('txt_msg').value = "";
		break;	
		case "contatoAluminio":
			document.getElementById('txt_nome').value = "";
			document.getElementById('txt_email').value = "";
			document.getElementById('txt_fone').value = "";
			document.getElementById('txt_endereco').value = "";
			document.getElementById('txt_cidade').value = "";
			document.getElementById('txt_estado').value = "";
			document.getElementById('txt_assunto').value = "";
			document.getElementById('txt_msg').value = "";
		break;	
		case "boletimEletro":
			document.getElementById('txt_nome').value = "";
			document.getElementById('txt_email').value = "";
			document.getElementById('txt_fone').value = "";
			document.getElementById('txt_endereco').value = "";
			document.getElementById('txt_cidade').value = "";
			document.getElementById('txt_estado').value = "";
			document.getElementById('txt_area').value = "";
		break;	
	}
}	
	
	function valida_contatoFerro(){
		if(document.formContato.txt_nome.value == ''){
			alert('Informe o Nome!');
			document.formContato.txt_nome.focus();
			return false;
		}
		if(document.formContato.txt_email.value == ''){
			alert('Informe o E-mail!');
			document.formContato.txt_email.focus();
			return false;
		}
		else{
			if (!Validar_Email(document.formContato.txt_email.value)) {
				document.formContato.txt_email.focus();
				alert("Informe um E-mail válido!");
				return false;
			}
		}

		if(document.formContato.txt_assunto.value == ''){
			alert('Informe o Assunto!');
			document.formContato.txt_assunto.focus();
			return false;
		}	
		if(document.formContato.txt_msg.value == ''){
			alert('Informe a sua Mensagem!');
			document.formContato.txt_msg.focus();
			return false;
		}	
		return true;
	}
	
	function valida_contatoEletro(){
		if(document.formContato.txt_nome.value == ''){
			alert('Informe o Nome!');
			document.formContato.txt_nome.focus();
			return false;
		}
		if(document.formContato.txt_email.value == ''){
			alert('Informe o E-mail!');
			document.formContato.txt_email.focus();
			return false;
		}
		else{
			if (!Validar_Email(document.formContato.txt_email.value)) {
				document.formContato.txt_email.focus();
				alert("Informe um E-mail válido!");
				return false;
			}
		}

		if(document.formContato.txt_assunto.value == ''){
			alert('Informe o Assunto!');
			document.formContato.txt_assunto.focus();
			return false;
		}	
		if(document.formContato.txt_msg.value == ''){
			alert('Informe a sua Mensagem!');
			document.formContato.txt_msg.focus();
			return false;
		}	
		return true;
	}
	
	function valida_boletimEletro(){
		if(document.formBeletim.txt_nome.value == ''){
			alert('Informe o Nome!');
			document.formBeletim.txt_nome.focus();
			return false;
		}
		if(document.formBeletim.txt_email.value == ''){
			alert('Informe o E-mail!');
			document.formBeletim.txt_email.focus();
			return false;
		}
		else{
			if (!Validar_Email(document.formBeletim.txt_email.value)) {
				document.formBeletim.txt_email.focus();
				alert("Informe um E-mail válido!");
				return false;
			}
		}

		if(document.formBeletim.txt_fone.value == ''){
			alert('Informe o Telefone!');
			document.formBeletim.txt_fone.focus();
			return false;
		}	
		if(document.formBeletim.txt_endereco.value == ''){
			alert('Informe o Endereço!');
			document.formBeletim.txt_endereco.focus();
			return false;
		}
		if(document.formBeletim.txt_cidade.value == ''){
			alert('Informe a Cidade!');
			document.formBeletim.txt_cidade.focus();
			return false;
		}
		if(document.formBeletim.txt_estado.value == ''){
			alert('Informe o Estado!');
			document.formBeletim.txt_estado.focus();
			return false;
		}
		if(document.formBeletim.txt_area.value == ''){
			alert('Informe a Área de Atauação!');
			document.formBeletim.txt_area.focus();
			return false;
		}
		return true;
	}
	

function valida_contatoAluminio(){
		if(document.formContato.txt_nome.value == ''){
			alert('Informe o Nome!');
			document.formContato.txt_nome.focus();
			return false;
		}
		if(document.formContato.txt_email.value == ''){
			alert('Informe o E-mail!');
			document.formContato.txt_email.focus();
			return false;
		}
		else{
			if (!Validar_Email(document.formContato.txt_email.value)) {
				document.formContato.txt_email.focus();
				alert("Informe um E-mail válido!");
				return false;
			}
		}

		if(document.formContato.txt_assunto.value == ''){
			alert('Informe o Assunto!');
			document.formContato.txt_assunto.focus();
			return false;
		}	
		if(document.formContato.txt_msg.value == ''){
			alert('Informe a sua Mensagem!');
			document.formContato.txt_msg.focus();
			return false;
		}	
		return true;
	}
	


	



	function countChars(num_chars, obj_mostrador, obj_texto, msg) {
		campo=obj_texto.value;
		obj_mostrador.value=campo.length;
		if (campo.length>parseInt(num_chars)) {
			alert(msg+" "+num_chars);
			obj_texto.value=campo.substring(0,parseInt(num_chars));
			obj_mostrador.value=num_chars;
			return false;
		}
		return true;
	}
	
