// JavaScript Document
//Funções JavaScript
function passa_foco(origem, destino, largura) {
	if (document.getElementById(origem).value.length == largura) {
		document.getElementById(destino).focus();
	}
}

function proximoCampo(campo) {
	var campos = document.getElementsByTagName('input');
	if (document.getElementById(campo).value.length == document.getElementById(campo).getAttribute("maxlength")) {
		for (i=0;i<campos.length;i++) {
			if (document.getElementById(campo) == campos[i]) {
				if (i < campos.length - 1) {
					campos[i+1].focus();
				}
				break;
			}
		}
	}
}
function mascara_hora(campo, digitos) {
	if (document.getElementById(campo).value.length == 2) {
		document.getElementById(campo).value += ":";
	}
	if (digitos == 8) {
		if (document.getElementById(campo).value.length == 5) {
			document.getElementById(campo).value += ":";
		}
	}
	proximoCampo(campo);
}
function mascara_cpf(campo) {
	if (document.getElementById(campo).value.length == 3) {
		document.getElementById(campo).value += ".";
	}
	if (document.getElementById(campo).value.length == 7) {
		document.getElementById(campo).value += ".";
	}
	if (document.getElementById(campo).value.length == 11) {
		document.getElementById(campo).value += "-";
	}
	proximoCampo(campo);
}

function mascara_data(campo) {
	if (document.getElementById(campo).value.length == 2) {
		document.getElementById(campo).value += "/";
	}
	if (document.getElementById(campo).value.length == 5) {
		document.getElementById(campo).value += "/";
	}
	proximoCampo(campo);
}

function mascara_cep(campo) {
	var cam = document.getElementById(campo);
	if (cam.value.length == 5) {
		cam.value += "-";
	}
	proximoCampo(campo);
}

function exibeMenu(id) {
	document.getElementById(id).style.display = "block";
}

function escondeMenu(id) {
	document.getElementById(id).style.display = "none";
}
function verifica_opcao(pagina, identificacao, tot) {
	var resultado;
	var IDop;
	for (i=1;i<=tot;i++) {
		if (document.getElementById(identificacao+String(i)).checked != false) {
			resultado = true;
			IDop = document.getElementById(identificacao+String(i)).value;
		}
	}
	if (resultado == true) {
		window.location = pagina+"&IDop="+IDop;
	} else {
		alert("Escolha uma opção de horário para realizar a inscrição");
	}
}

function mascara_preco(id) {
	var div = document.getElementById(id);
	if (div.value.length == 1) {
		div.value = '0,0'+div.value;
	} else if (div.value.length == 5 && div.value.indexOf('0,0') > -1) {
		div.value = div.value.replace('0,0', '');
		div.value = '0,'+div.value;
	} else {
		if (div.value.indexOf('0,') == 0) {
			div.value = div.value.replace('0,', '');
		}
		var str = div.value.charAt(div.value.length-2);
		str += div.value.charAt(div.value.length-1);
		var resto = div.value.charAt(0);
		for (var i=1;i<(div.value.length-2);i++) {
			if (div.value.charAt(i) != ",") {
				resto += div.value.charAt(i);
			}
		}
		
		div.value = resto + "," + str;
	}
}

function mascara_telefone(id) {
	var campo = document.getElementById(id);
	if (campo.value.length == 1) {
		campo.value = "(" + campo.value;
	}
	if (campo.value.length == 3) {
		campo.value += ") ";
	}
	if (campo.value.length == 9) {
		campo.value += "-";
	}
	proximoCampo(id);
}	
function rodape() {
	var cab = document.getElementById('cabecalho').clientHeight;
	var menu = document.getElementById('menu_busca').clientHeight;
	var tT = document.getElementById('tudo').clientHeight;
	var tBody = document.getElementsByTagName('html')[0].clientHeight;
	var total = tT;
	var dif = tBody - total;
	if (dif > 0) {
		document.getElementById('rodape').style.marginTop = dif+"px";
	}
}

function troca_cantos_menu(menu) {
	var tudo = document.getElementById(menu);
	tudo.getElementsByTagName('div')[0].style.background = 'url(imagens/canto_left_bot_over.gif)';
	tudo.getElementsByTagName('div')[1].style.background = 'url(imagens/canto_right_bot_over.gif)';
}

function inverte_troca(menu) {
	var tudo = document.getElementById(menu);
	tudo.getElementsByTagName('div')[0].style.background = 'url(imagens/canto_left_bot.gif)';
	tudo.getElementsByTagName('div')[1].style.background = 'url(imagens/canto_right_bot.gif)';
}

function addOpcao(numero) {
	link = document.getElementById('linkAdd');
	attrLink = link.getAttribute('onclick');
	link.setAttribute('onclick', "");
	var elemento = document.getElementById('msgAjax');
	var resposta = document.getElementById('abrigaOpcoes');
	/*CAPTURAR DADOS DOS FORMULARIOS PARA SEREM RECUPERADOS POSTERIORMENTE*/
	var inputs = resposta.getElementsByTagName('input');
	var totDados = inputs.length;
	var arrDados = new Array(totDados);
	for (var i=0;i<totDados;i++) {
		arrDados[i] = inputs[i].value;
	}
	ajax.open("POST", "http://www.abrae.org.br/processa.asp?object=opcao", true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.onreadystatechange = function() {
		//status carregando
		if(ajax.readyState == 1) {
			elemento.innerHTML = "<img src='../carregando.gif' alt='Carregando...'>";
		}
		//status pronto
		if(ajax.readyState == 4) {
			//pagina encontrada
			if(ajax.status == 200) {
				var numOpcoes = document.getElementById('curso_numOpcoes');
				elemento.innerHTML = "";
				link.setAttribute('onclick', "javascript:addOpcao("+(Number(numero)+1)+")");
				var resp = ajax.responseText;
				resposta.innerHTML += resp;
				resposta.innerHTML += '<a href="javascript:removeOpcao();" id="linkRemove'+numero+'">Remover opcao</a>';
				inputs = resposta.getElementsByTagName('input');
				for (i = 0; i<totDados;i++) {
					inputs[i].value = arrDados[i];
				}
				
				document.getElementById('curso_numOpcoes').value = String(Number(numOpcoes.value) + 1);
			} else {
				elemento.innerHTML = "Erro ao carregar os dados";
				link.setAttribute('onclick', attrLink);
			}
		}
	}
	ajax.send("ind="+numero);
	
}

function removeOpcao() {
	var resposta = document.getElementById('abrigaOpcoes');
	var inputs = resposta.getElementsByTagName('input');
	var link = document.getElementById('linkAdd');
	var totDados = inputs.length;
	var numOpcoes = document.getElementById('curso_numOpcoes');
	var arrDados = new Array(totDados);
	for (var i=0;i<totDados;i++) {
		arrDados[i] = inputs[i].value;
	}
	
	var div = document.getElementById('formOpcaoDeCurso'+(Number(numOpcoes.value)-1));
	resposta.removeChild(div);
	link.setAttribute('onclick', "javascript:addOpcao("+(Number(numOpcoes.value)-1)+")");
	inputs = resposta.getElementsByTagName('input');
	totDados = inputs.length;
	var linkRemove = document.getElementById('linkRemove'+(Number(numOpcoes.value)-1));
	resposta.removeChild(linkRemove);
	for (i = 0; i<totDados;i++) {
		inputs[i].value = arrDados[i];
	}
	numOpcoes.value = Number(numOpcoes.value) - 1;
	document.getElementById('curso_numOpcoes').value = numOpcoes.value;
}
var janela;
function printMinistrante(def) {
	var continua = true;
	if (janela == null) {
		janela = window.open("http://www.abrae.org.br/popup.asp?pagina=ministrante", "popup", "height=300, width=300, scrollbars=yes");
	} else if (janela.closed) {
		continua = false;
		var div = document.getElementById('conteinerMinistrante');
		var ministrante = document.getElementById('divMinistrante');
		div.removeChild(ministrante);
		var elemento = document.getElementById('msgAjax_ministrante');
		
		ajax.open("GET", "http://www.abrae.org.br/processa.asp?object=ministrante", true);
		ajax.send(null);
		ajax.onreadystatechange = function() {
			//status carregando
			if(ajax.readyState == 1) {
				elemento.innerHTML = "<img src='../carregando.gif' alt='Carregando...'>";
			}
			//status pronto
			if(ajax.readyState == 4) {
				//pagina encontrada
				if(ajax.status == 200) {
					elemento.innerHTML = "";
					div.innerHTML = ajax.responseText;
				} else {
					elemento.innerHTML = "Erro ao carregar os dados! Tente novamente";
				}
			}
		}
		janela = null;
	} else if (janela != null && def != "set") {
		alert("A janela de cadastro já está aberta");
	}
	if (continua) {
		setTimeout(function(){printMinistrante("set");}, 2000);
	}
	
}

function cadastraCidade(def, ind) {
	var continua = true;
	if (janela == null) {
		janela = window.open("http://www.abrae.org.br/popup.asp?pagina=cidade", "popupCidade", "height=300, width=300, scrollbars=yes");
	} else if (janela.closed) {
		continua = false;
		carregaCidades(ind);
	} else if (janela != null && def != "set") {
		alert("A janela de cadastro já está aberta");
	}
	if (continua) {
		setTimeout(function(){cadastraCidade("set", ind);}, 2000);
	}
}

function carregaCidades(ind) {
	var div = document.getElementById('cidadeSelect');
	var cidade = document.getElementById('cidade_'+ind+'_cidade');
	div.removeChild(cidade);
	var elemento = document.getElementById('cidadeSelect');
	
	ajax.open("POST", "http://www.abrae.org.br/processa.asp?object=lugar&tipo=cidade", true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.onreadystatechange = function() {
		//status carregando
		if(ajax.readyState == 1) {
			elemento.innerHTML = "<img src='../carregando.gif' alt='Carregando...'>";
		}
		//status pronto
		if(ajax.readyState == 4) {
			//pagina encontrada
			if(ajax.status == 200) {
				elemento.innerHTML = "";
				div.innerHTML = ajax.responseText;
			} else {
				elemento.innerHTML = "Erro ao carregar os dados! Tente novamente";
			}
		}
	}
	var uf = document.getElementById('estado_'+ind+'_estado').value;
	ajax.send("uf="+uf);
	janela = null;
}
