/*** Inicia Ajax *********************************************************/
function runAjax() {
	// verifica se é IE
	if (window.ActiveXObject) {
		//estancia o objeto Active X
		var ajax = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		//outros navegadores estancia o XMLHttpRequest
		var ajax = new XMLHttpRequest();
	}
	return ajax;
}

/*** Altera conteúdo ****************************************************/
//variavel global para id
var urlSend = ""; 
var idDiv = ""; 
var idNav = ""; 

//seleciona o conteudo que sera alterado na div
function selectContent(url,id,id_nav) {
	urlSend = url;
	idDiv = id;
	idNav = id_nav;
	//inicia o ajax
	ajax = runAjax();
	//aguarda a mudanca de status da comunicacao com o servidor 
	ajax.onreadystatechange = changeContent;
	//abre a conexao com o servidor
	ajax.open("GET",urlSend);
	//define o cabecalho
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	//envia a requisicao para o servidor
	ajax.send(null);
}

//altera o conteudo da div
function changeContent() {
	//verifica o status da requisicao, se o processamento está completo
	if (ajax.readyState == 4){
		//verifica o numero do status, se for diferente de 200 = erro
		if (ajax.status == 200) {
			//insere o texto na página
			w3Opacity.fading(idNav,100,0,500);
			document.getElementById(idNav).style.display = "none";
			w3Opacity.fading(idDiv,0,100,500);
			document.getElementById(idDiv).style.display = "block";
			document.getElementById(idDiv).innerHTML = ajax.responseText;
			//document.getElementById(idBt).innerHTML = "<a href='#' onClick='hideContent(\"" + idDiv + "\");'><img src='img/servicos/out/" + imgBt + ".gif' border='0' /></a>";
		} else {
			//erro
			alert ("Houve um problema ao carregar o texto:\n " + ajax.statusText);
		}
	}
}

//altera o conteudo da div
function hideContent() {		
	w3Opacity.fading(idDiv,100,0,500,hide);
}

function hide() {
	document.getElementById(idDiv).style.display = "none";
	document.getElementById(idNav).style.display = "block";
	w3Opacity.fading(idNav,0,100,500);
	//document.getElementById(idBt).innerHTML = "<a href='#' onClick='selectContent(\"" + urlSend + "\",\"" + idDiv + "\",\"" + idBt + "\",\"" + imgBt + "\");'><img src='img/servicos/in/" + imgBt + ".gif' border='0' /></a>";
}

/*** Newsletter ************************************************************/
function insertNews(idNews, idEmailMkt, tipo){
	//alert(idNews + " - " + idEmailMkt + " - " + tipo)
	ajax = runAjax();
	ajax.open("GET", "add_infonews.asp?id_emailmkt=" + idEmailMkt + "&id_news=" + idNews + "&tipo=" + tipo);
	ajax.send(null);								
}

function checkTime(){
	var d = new Date();
	var hour = (d.getHours());
	if (hour < 18) {
		alert("Esta newsletter só poderá ser enviada após as 18:00"); 
	}
}

function preview(){
	var tit = document.getElementById('tit').value;
	var data = document.getElementById('data').value;
	var numero = document.getElementById('numero').value;
	var texto = document.getElementById('texto').value;
	var id_emailmkt = document.getElementById('id_emailmkt').value;
	var dados = "id_emailmkt=" + id_emailmkt;
	dados += "&tit=" + tit;
	dados += "&data=" + data;
	dados += "&numero=" + numero;
	dados += "&texto=" + texto;
	window.open('preview.asp?' + dados + '&','_blank');
}

function sendNewsletter(){
	var msg = "Tem certeza que deseja enviar esta newsletter?";
	var confirmacao = confirm(msg);
	if (confirmacao == true){
		document.getElementById('sendNews').submit();
	} 
}

/*** Altera Questionario ****************************************************/

var idPoll = "";

//seleciona o conteudo que sera alterado na div
function selectPoll(url,value,id) {
	if (value != 0){
		idPoll = id;
		if (value == "all"){
			var idClientes = document.getElementById('id_clientes').value;
			urlPoll = "questionario.asp?id_clientes=" + idClientes;
		} else {
			urlPoll = url + value;	
		}
		ajax = runAjax();
		ajax.onreadystatechange = changePoll;
		ajax.open("GET",urlPoll);
		ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		ajax.send(null);	
	} else {
		alert("Por favor selecione uma opção!");
		document.getElementById(idPoll).style.display = "none";
		if (document.getElementById('setorContent').style.display != "none"){
			document.getElementById('setorContent').style.display = "none";
		}
		if (document.getElementById('questionarioContent').style.display != "none"){
			document.getElementById('questionarioContent').style.display = "none";
		}
		if (document.getElementById('filtroContent').style.display != "none"){
			document.getElementById('filtroContent').style.display = "none";
		}
	}	
}

//altera o conteudo da div
function changePoll(){
	if (ajax.readyState == 4){
		if (ajax.status == 200) {
			if (ajax.responseText != 0){
				document.getElementById(idPoll).style.display = "block";
				document.getElementById(idPoll).innerHTML = ajax.responseText;
			} else {
				document.getElementById(idPoll).style.display = "none";
				alert("Nenhum setor cadastrado para este cliente!");
				if (document.getElementById('setorContent').style.display != "none"){
					document.getElementById('setorContent').style.display = "none";
				}
				if (document.getElementById('questionarioContent').style.display != "none"){
					document.getElementById('questionarioContent').style.display = "none";
				}
				if (document.getElementById('filtroContent').style.display != "none"){
					document.getElementById('filtroContent').style.display = "none";
				}
			}
		} else {
			alert ("Houve um problema ao carregar o texto:\n " + ajax.statusText);
		}
	}
}

/*** Filtro ****************************************************/

//seleciona o conteudo que sera alterado na div
function selectFiltro(value) {
	if (value != 0){
		ajax = runAjax();
		ajax.onreadystatechange = changeFiltro;
		ajax.open("GET","filtro.asp?tipo=" + value);
		ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		ajax.send(null);	
	} else {
		alert("Por favor selecione uma opção!");
		document.getElementById('filtroContent').style.display = "none";
	}	
}

//altera o conteudo da div
function changeFiltro(){
	if (ajax.readyState == 4){
		if (ajax.status == 200) {
			document.getElementById('filtroContent').style.display = "block";
			document.getElementById('filtroContent').innerHTML = ajax.responseText;
		} else {
			alert ("Houve um problema ao carregar o texto:\n " + ajax.statusText);
		}
	}
}

/*** Insere acesso ****************************************************/

function insertAccess(idPoll){
	var idSetor = document.getElementById('id_setor').value;
	var idClientes = document.getElementById('id_clientes').value;
	ajax = runAjax();
	ajax.open("GET", "add_access_id.asp?id_setor=" + idSetor + "&id_poll=" + idPoll + "&id_clientes=" + idClientes);
	ajax.send(null);								
}
function insertAtivo(ativo, idPoll){
	var idSetor = document.getElementById('id_setor').value;
	if (idPoll != ""){
		ajax = runAjax();
		ajax.open("GET", "add_ativo.asp?id_setor=" + idSetor + "&id_poll=" + idPoll);
		ajax.send(null);	
	} else {
		alert("Selecione o questinário!")
	}					
}

/*** Valida formulário ****************************************************/

function validRadio(radioButton) {
	var error = 1;
	for (i = 0; i < radioButton.length; i++) {
		if (radioButton[i].checked) {
			error = 0;
		} 
	}
	return error;
}
function validPoll() {
	var message = "";
	var error1 = validRadio(document.poll.cargo);
	if (error1 == 1){
		message += "Cargo de liderança é obrigatório! \n";
	}
	var error2 = validRadio(document.poll.vinculo);
	if (error2 == 1){
		message += "Vínculo é obrigatório! \n";
	}	
	var error3 = validRadio(document.poll.escolaridade);
	if (error3 == 1){
		message += "Grau de escolaridade é obrigatório! \n";
	}
	var error4 = validRadio(document.poll.idade);
	if (error4 == 1){
		message += "Idade é obrigatório! \n";
	}
	var error5 = validRadio(document.poll.tempo);
	if (error5 == 1){
		message += "Tempo de empresa é obrigatório! \n";
	}
	var error6 = validRadio(document.poll.experiencia);
	if (error6 == 1){
		message += "Tempo de experiência na função é obrigatório! \n";
	}
	var iloop = document.getElementById('iloop').value;
	var count_errors = 0;
	for (count = 0; count < iloop; count++){
		var errors = validRadio(eval("document.poll.pg" + count));
		count_errors = (count_errors + errors);
	}
	if (count_errors != 0){
		message += "Por favor responda todas as perguntas!";
	}
	if (message != ""){
		alert(message);
		return false;
	}
}