// JavaScript Document
function validaEnquete(){
	var enquete = document.enquetes.enquete;
	var escolhido;

	for (var i=0; i<enquete.length; i++)  { 
		if (enquete[i].checked)  {
			escolhido = enquete[i].value;
			} 
		}

	if(!escolhido){
		alert("Por favor escolha uma opcao");
		return false;
	}

}

//funcao para validacao de email
function Email(){
	var nome = document.getElementById('nome_newsletter');
	var email = document.getElementById('email_newsletter');
	
	//checka o campo nome
	if(nome.value == "" || nome.value.length < 4){
		alert('O campo nome e obrigatorio');
		nome.focus();
		return false;
	}
	// Checka o campo email
		if(email.value == null || email.value ==""){
			alert('Email inválido');
			email.focus();
			return false;
		}
		else{
			if(!validaEmail(email.value)){
				alert('Email inválido');
				email.focus();
				return false;
			}
		}// Fim Checka o campo email
}

// Validação de email
function validaEmail(str) {

		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		
		if (str.indexOf(at)==-1){
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false;
		}

		if (str.indexOf(at,(lat+1))!=-1){
		    return false;
		 }

		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false;
		 }

		if (str.indexOf(dot,(lat+2))==-1){
		    return false;
		 }
		
		if (str.indexOf(" ")!=-1){
		    return false;
		 }

 		 return true;					
}


function Contato(){
	var nome = document.getElementById('nome');
	var telefone = document.getElementById('telefone');
	var email = document.getElementById('email');
	var tipo = document.getElementById('tipo');
	var cliente = document.contato.cliente;
	var mensagem = document.getElementById('mensagem');
	var arquivo = document.getElementById('arquivo');
	
	if(nome.value == "" || nome.value.length < 4){
		alert('Preencha corretamente o campo nome');
		nome.focus();
		return false;
	}
	
	if(telefone.value == "" || telefone.value.length < 12){
		alert('Preencha corretamente o campo telefone');
		telefone.focus();
		return false;
	}
	
	// Checka o campo email
		if(email.value == null || email.value ==""){
			alert('Email inválido');
			email.focus();
			return false;
		}
		else{
			if(!validaEmail(email.value)){
				alert('Email inválido');
				email.focus();
				return false;
			}
		}// Fim Checka o campo email
	
	if(tipo.value == "" || tipo.value.length < 4){
		alert('Preencha corretamente o campo tipo');
		tipo.focus();
		return false;
	}
	
//Valida o campo de cliente
	var escolhido = "";
		for(var i = 0; i < cliente.length; i++){
			if(cliente[i].checked){
				escolhido = cliente[i].value;
			}
		}
	
	if(escolhido == ""){
		alert('Já é cliente?');
		return false;
	}

	if(mensagem.value == "" || mensagem.value.length < 4){
		alert('Preencha corretamente o campo mensagem');
		mensagem.focus();
		return false;
	}
	
	if(arquivo.disabled == false){
		if(arquivo.value == ""){
			alert('Voce escolheu incluir um curriculo');
			arquivo.focus();
			return false;
		}
		else{
			var estensaoValida = new Array('pdf','doc','docx');
			if(in_array(extensao,estensaoValida)){
				return true;
			}
			else{
				alert('Nao e permitido enviar esse tipo de arquivos.\nOs arquivos validos sao do tipo:\nWord e pdf');
				return false;
			}
		}
	}
}

function enviaCurriculo(){
	var formulario = document.contato;
	var tipo = document.getElementById('tipo');
	var arquivo = document.getElementById('arquivo');
	var como = document.getElementById('como');
	
	if(tipo.value == 'curriculo'){
		formulario.enctype = 'multipart/form-data';
		arquivo.disabled = false;
		como.value = 'Sim';
		
	}
	else{
		arquivo.disabled = true;
		como.value = 'Nao';
	}
	
}

function Formatar(src, mask){
	var i = src.value.length;
	var saida = mask.substring(0,1);
	var texto = mask.substring(i)
	if (texto.substring(0,1) != saida){
		src.value += texto.substring(0,1);
	}

}



function in_array( what, where ){
   var a=false;
   for(var i=0;i<where.length;i++){
      if(what == where[i]){
      a=true;
      break;
     }
   }
 return a;
}



function CheckaExtensao(){
	var arquivo = document.getElementById('arquivo');
	var vetor = arquivo.value.split(".");
	var numero = (vetor.length - 1);
	var extensao = vetor[numero];
	
	var estensaoValida = new Array('pdf','doc','docx');
	if(in_array(extensao,estensaoValida)){
		return true;
	}
	else{
		alert('Nao e permitido enviar esse tipo de arquivos.\nOs arquivos validos sao do tipo:\nWord e pdf');
	}
}
