$(document).ready(function(){
  $("a.new_window").attr("target", "_blank");
 });

// ==============================================================================================================
// ==============================================================================================================
// FUNÇÕES PARA FORMATAR CAMPOS GERAL ***************************************************************************
// ==============================================================================================================
// ==============================================================================================================


function createQCObject() { 
	var req; 
	if(window.XMLHttpRequest){	req = new XMLHttpRequest(); } 
	else if(window.ActiveXObject) { req = new ActiveXObject("Microsoft.XMLHTTP"); } 
	else { alert('Problem creating the XMLHttpRequest object');	} 
	return req; 
} 











var ajaxFaleConosco = createQCObject(); 


function enviarMensagemFaleConosco() {
	
		var nome, email, assunto, mensagem, cidade, estado, telefone;
		nome = document.getElementById("contato_nome").value;
		email = document.getElementById("contato_email").value;
		telefone = document.getElementById("contato_telefone").value;
		cidade = document.getElementById("contato_cidade").value;	
		estado = document.getElementById("contato_estado").value;	
		assunto = document.getElementById("contato_assunto").value;	
		mensagem = document.getElementById("contato_mensagem").value;	
		
		document.getElementById("content-esquerda").innerHTML = '<h5>Aguardando resposta do servidor...</h5><p>Dentro de instantes seu email será enviado.</p>';
		
		ajaxFaleConosco.open('get', 'javascript/ajax/enviarMensagemFaleConosco.php?nome=' + nome + '&email=' + email + '&telefone=' + telefone + '&cidade=' + cidade + '&estado=' + estado + '&assunto=' + assunto + '&mensagem=' + mensagem);
		ajaxFaleConosco.onreadystatechange = function() {			
			if(ajaxFaleConosco.readyState == 4 && ajaxFaleConosco.status == 200) { 					
				var response = ajaxFaleConosco.responseText;
				if(response) {document.getElementById("content-esquerda").innerHTML = response;} 
				faleConoscoRecarregaFormulario = window.setInterval(enviarMensagemFaleConoscoRecarregaFormulario, 5000)
			} 
		
		} 
		
		ajaxFaleConosco.send(null); 
			
}


function enviarMensagemFaleConoscoRecarregaFormulario() {

		window.clearInterval(faleConoscoRecarregaFormulario);			
		
		ajaxFaleConosco.open('get', 'javascript/ajax/enviarMensagemFaleConoscoRecarregaFormulario.php');
		ajaxFaleConosco.onreadystatechange = function() {			
			if(ajaxFaleConosco.readyState == 4 && ajaxFaleConosco.status == 200) { 					
				var response = ajaxFaleConosco.responseText;
				if(response) {document.getElementById("content-esquerda").innerHTML = response;} 
			} 
		
		} 
		
		ajaxFaleConosco.send(null); 
}











var ajaxInteresse = createQCObject(); 


function enviarMensagemInteresse() {
	
		var nome, email, curso, cidade, estado, telefone;
		nome = document.getElementById("contato_nome").value;
		email = document.getElementById("contato_email").value;
		telefone = document.getElementById("contato_telefone").value;
		cidade = document.getElementById("contato_cidade").value;	
		estado = document.getElementById("contato_estado").value;	
		curso = document.getElementById("contato_curso").value;	
		
		document.getElementById("janela-modal-1").innerHTML = '<h5>Aguardando resposta do servidor...</h5><p>Dentro de instantes seu email será enviado.</p>';
		
		ajaxInteresse.open('get', 'javascript/ajax/enviarMensagemInteresse.php?nome=' + nome + '&email=' + email + '&telefone=' + telefone + '&cidade=' + cidade + '&estado=' + estado + '&curso=' + curso);
		ajaxInteresse.onreadystatechange = function() {			
			if(ajaxInteresse.readyState == 4 && ajaxInteresse.status == 200) { 					
				var response = ajaxInteresse.responseText;
				if(response) {document.getElementById("janela-modal-1").innerHTML = response;} 
			} 
		
		} 
		
		ajaxInteresse.send(null); 
			
}











// PERMITE APENAS NÚMERO *****************************************************************************************

function numero( evento ) {
	
	/************************************************
	backspace = 8
	enter = 13
	tab = 0 ou 9
	*************************************************/
	
	if( navigator.appName == "Netscape" ) {	var tecla = evento.which; } else { var tecla = evento.keyCode; }
	
	if ((tecla > 47 && tecla < 58) || tecla == 8 || tecla == 13 || tecla == 0) {
		return true; 
	}else{
		return false;
	}            
	
}




// FORMATA TELEFONE *****************************************************************************************

function formata_telefone(objeto, evento){
	
	if( navigator.appName == "Netscape" ) {	var tecla = objeto.which; } else { var tecla = objeto.keyCode; }
	var telefone = evento;
	if(tecla != 8 && tecla != 9){
		if(telefone.value.length == 0)  telefone.value += '(';
		if(telefone.value.length == 3)  telefone.value += ') ';
		if(telefone.value.length == 9)  telefone.value += '-';
		
	}
}




// FORMATA HORÁRIO *****************************************************************************************

function formata_horario(objeto, evento){
	var tecla = objeto.keyCode;
	if(tecla != 18 && tecla != 8){
		if(evento.value.length == 2)  evento.value += ':';														
	}
}



// FORMATA CAMPOS DE VALOR *****************************************************************************************

function formata_valor_limpar(valor, validos) {
	// retira caracteres invalidos da string
	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 formata_valor(campo,tammax,teclapres,decimal) {
	
	var tecla = teclapres.keyCode;
	vr = formata_valor_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 ) ;}
	}

}




// FORMATA CEP *****************************************************************************************

function formata_cep(objeto, evento){
	
	var tecla = objeto.keyCode;
	var cep = evento;
	if(tecla != 8){
		if(cep.value.length == 5)  cep.value += '-';
		
	}
}









// ==============================================================================================================
// ==============================================================================================================
// FUNÇÕES PARA VALIDAR CAMPOS GERAL ***************************************************************************
// ==============================================================================================================
// ==============================================================================================================






// VALIDA CPF *****************************************************************************************

function valida_cpf (numcpf)
{
	x = 0;
	soma = 0;
	dig1 = 0;
	dig2 = 0;
	texto = "";
	numcpf1="";
	len = numcpf.length; x = len -1;
	// var numcpf = "12345678909";
	for (var i=0; i <= len - 3; i++) {
		y = numcpf.substring(i,i+1);
		soma = soma + ( y * x);
		x = x - 1;
		texto = texto + y;
	}
	dig1 = 11 - (soma % 11);
	if (dig1 == 10) dig1=0 ;
	if (dig1 == 11) dig1=0 ;
	numcpf1 = numcpf.substring(0,len - 2) + dig1 ;
	x = 11; soma=0;
	for (var i=0; i <= len - 2; i++) {
		soma = soma + (numcpf1.substring(i,i+1) * x);
		x = x - 1;
	}
	dig2= 11 - (soma % 11);
	if (dig2 == 10) dig2=0;
	if (dig2 == 11) dig2=0;
	//alert ("Digito Verificador : " + dig1 + "" + dig2);
	if ((dig1 + "" + dig2) == numcpf.substring(len,len-2)) {
		return true;
	}
	return false;
}





// VALIDA CPF *****************************************************************************************

function valida_email (email)
{
	if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))){ 
		return false;
	} 	
	else {
		return true;
	}
}





// ABRE POPUP CPF *************************************************************************************

function MM_openBrWindow(theURL,winName,features) {
	window.open(theURL,winName,features); 
}







// VALIDA CAMPOS *************************************************************************************

function limpa_campos(input){document.getElementById(input).value = '';}





// FONT SIZE     *************************************************************************************

var minFontSize=11;
var maxFontSize=16;

function increaseFontSize() {
	var id = document.getElementById('conteudo');
   var p = id.getElementsByTagName('p');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=maxFontSize) {
         s += 1;
      }
      p[i].style.fontSize = s+"px"
   }
}

function decreaseFontSize() {
	var id = document.getElementById('conteudo');
   var p = id.getElementsByTagName('p');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=minFontSize) {
         s -= 1;
      }
      p[i].style.fontSize = s+"px"
   }   
}





// VALIDA CAMPOS *************************************************************************************

function valida_campos(acao){


		if(acao == 'fale-conosco'){
				
				if(document.getElementById("contato_nome").value == ''){
					alert("Digite o nome completo.");
					document.getElementById("contato_nome").focus();
					return false;
				}
				
				if(valida_email(document.getElementById("contato_email").value) == false){
					alert("Digite o email corretamente.");
					document.getElementById("contato_email").focus();
					return false;
				};		
				
				if(document.getElementById("contato_telefone").value.length != 14){
					alert("Digite o telefone corretamente.");
					document.getElementById("contato_telefone").focus();			
					return false;
				}
				
				if(document.getElementById("contato_cidade").value == ''){
					alert("Digite o nome da cidade.");
					document.getElementById("contato_cidade").focus();
					return false;
				}
				
				if(document.getElementById("contato_estado").value == ''){
					alert("Digite o nome do estado.");
					document.getElementById("contato_estado").focus();
					return false;
				}
				
				if(document.getElementById("contato_mensagem").value == ''){
					alert("Digite a sua mensagem.");
					document.getElementById("contato_mensagem").focus();			
					return false;
				}
				
				enviarMensagemFaleConosco();
			
		} 		


		if(acao == 'interesse'){
				
				if(document.getElementById("contato_nome").value == ''){
					alert("Digite o nome completo.");
					document.getElementById("contato_nome").focus();
					return false;
				}
				
				if(valida_email(document.getElementById("contato_email").value) == false){
					alert("Digite o email corretamente.");
					document.getElementById("contato_email").focus();
					return false;
				};		
				
				if(document.getElementById("contato_telefone").value.length != 14){
					alert("Digite o telefone corretamente.");
					document.getElementById("contato_telefone").focus();			
					return false;
				}
				
				if(document.getElementById("contato_cidade").value == ''){
					alert("Digite o nome da cidade.");
					document.getElementById("contato_cidade").focus();
					return false;
				}
				
				if(document.getElementById("contato_estado").value == ''){
					alert("Digite o nome do estado.");
					document.getElementById("contato_estado").focus();
					return false;
				}
				
				enviarMensagemInteresse();
			
		} 		
}

