//VARIAVEL XMLHTTP -- INICIO DO AJAX
var xmlhttp = getXmlHttpRequest();

//FUNCAO RESPONSÁVEL POR DETERMINAR O TIPO DO PROTOCOLO A SER UTILIZADO PELO AJAX
function getXmlHttpRequest(){
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
		return new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				return new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	} 
}
function divEnviarDepoimento(){
	document.getElementById('txtNome').value="";
	document.getElementById('txtEmail').value="";
	document.getElementById('intNota').value="5";
	document.getElementById('txtMsg').value="";


	document.getElementById('enviarDepoimento').style.display='block';
	document.getElementById('verDepoimentos').style.display='none';
}

function CheckEnviarDepoimento(){
	var enviar = true;
	var msg = "";
	var Nome = document.getElementById('txtNome').value;
	var Email = document.getElementById('txtEmail').value;
	var Msg = document.getElementById('txtMsg').value;
	
	if(Nome == ""){
		enviar = false;
		msg += "O Campo Nome precisa ser preenchido!\n";
		document.getElementById('txtNome').focus();
	}
	if(Email == ""){
		enviar = false;
		msg += "O Campo Email precisa ser preenchido!\n";
		document.getElementById('txtEmail').focus();
	}
	else{
		if ((Email.length != 0) && ((Email.indexOf("@") < 1) || (Email.indexOf('.') < 7))){
			enviar = false;
			msg += "O Email está incorreto!\n";			
			document.getElementById('txtEmail').focus();
		}
	}
	if(Msg == ""){
		enviar = false;
		msg += "O Campo Mensagem precisa ser preenchido!\n";
		document.getElementById('txtMsg').focus();
	}
	
	if(enviar)
		EnviarDepoimento();
	else
		alert(msg);
}

function EnviarDepoimento(){
	var Nome = document.getElementById('txtNome').value;
	var Email = document.getElementById('txtEmail').value;
	var Nota = document.getElementById('intNota').value;
	var Msg = document.getElementById('txtMsg').value;

	xmlhttp.open("POST", "../includes/js/colonia/inc_depoimento.php?acao=enviar", true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 ) {
			if(xmlhttp.responseText){
//				alert("Seu depoimento foi enviado com sucesso!");
				alert(xmlhttp.responseText);
				VerDepoimentos();
			}
		}
	}
	xmlhttp.send("&txtNome="+Nome+"&txtEmail="+Email+"&intNota="+Nota+"&txtMsg="+Msg);	
}
function VerDepoimentos(pag){
	
	document.getElementById('verDepoimentos').style.display='block';
	document.getElementById('enviarDepoimento').style.display='none';
	
	xmlhttp.open("POST", "../includes/js/colonia/inc_depoimento.php?acao=ver&pagina="+pag, true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 ) {
			if(xmlhttp.responseText){
				document.getElementById("verDepoimentos").innerHTML=xmlhttp.responseText;
			}
		}
	}
	xmlhttp.send(null);	
}
function limitTextArea(campo,digitado, falta, total){ 
	tam = document.getElementById(campo).value.length; 
	str=""; 
	str=str+tam; 
	document.getElementById(digitado).innerHTML = str; 
	document.getElementById(falta).innerHTML = total - str; 
	if (tam > total){ 
		aux = document.getElementById(campo).value; 
		document.getElementById(campo).value = aux.substring(0,total); 
		document.getElementById(digitado).innerHTML = total 
		document.getElementById(falta).innerHTML = 0 
	} 
}

$(function() {
	$("#enviar").click(function(){
		var enviar = true;
		var msg = "";
		var Nome = $("#txtNome").val();
		var Email = $("#txtEmail").val();		
		var intOficina = $("#intOficina").val();
		var intGincAqua = $("#intGincAqua").val();
		var intHidro = $("#intHidro").val();
		var intSurf = $("#intSurf").val();
		var intGincRecre = $("#intGincRecre").val();
		var intCampEsport = $("#intCampEsport").val();
		var intConcursos = $("#intConcursos").val();
		var intCampBaralho = $("#intCampBaralho").val();
		var intBingo = $("#intBingo").val();
		var intYahoo = $("#intYahoo").val();
		var intFrescobol = $("#intFrescobol").val();
		var intCanoagem = $("#intCanoagem").val();
		var intMassagem = $("#intMassagem").val();
		var intEscuna = $("#intEscuna").val();
		var intRoteiro = $("#intRoteiro").val();
		var intMonitores = $("#intMonitores").val();
		var intHorarioAtividades = $("#intHorarioAtividades").val();
		var intRecreacao = $("#intRecreacao").val();
		var intNumMonitores = $("#intNumMonitores").val();
		var intAtividades = $("#intAtividades").val();
		var intLocal = $("#intLocal").val();
		var txtSugestoes = $("#txtSugestoes").val();
		
		if(Nome == ''){
			enviar = false;
			msg = "Preencha seu nome";
			$("#txtNome").focus();
		}
		if(Email == ''){
			enviar = false;
			msg = "Preencha seu E-Mail";
			$("#txtEmail").focus();
			
		}
		
		if(enviar){
			$.ajax({
				type:'POST',
				url: '../includes/js/ajax/academiaPerguntas.php',
				data: 'Nome='+Nome+'&Email='+Email+'&intOficina='+intOficina+'&intGincAqua='+intGincAqua+'&intHidro='+intHidro+'&intSurf='+intSurf+'&intGincRecre='+intGincRecre+'&intCampEsport='+intCampEsport+'&intConcursos='+intConcursos+'&intCampBaralho='+intCampBaralho+'&intBingo='+intBingo+'&intYahoo='+intYahoo+'&intFrescobol='+intFrescobol+'&intCanoagem='+intCanoagem+'&intMassagem='+intMassagem+'&intEscuna='+intEscuna+'&intRoteiro='+intRoteiro+'&intMonitores='+intMonitores+'&intHorarioAtividades='+intHorarioAtividades+'&intRecreacao='+intRecreacao+'&intNumMonitores='+intNumMonitores+'&intAtividades='+intAtividades+'&intLocal='+intLocal+'&txtSugestoes='+txtSugestoes,
				beforeSend: function(){
					$("#quest").fadeOut(400, function(){
						$(this).fadeIn(400).html("<div style='padding-top:120px;'><center>Por favor, aguarde<br><img src='../includes/images/ajaxload.gif'></center></div>");
					});
				},
				success: function(result){
					if(result != "erro"){
						$("#quest").fadeOut(400, function(){
							$(this).fadeIn(400).html(result);
						});
					};
				}
			});
		}
		else{
			alert(msg);
		}
	});
	
});
