function txtPagina(pagina) {
	var ajax = openAjax();
	var recipiente = gE('con_esquerda');
	carregando();
	
	ajax.open('GET', 'bib_ajax/paginas.php?function='+pagina, true); 
	
	ajax.onreadystatechange = function() {
		if (ajax.readyState == 4) {
			if (ajax.status == 200) {
				document.body.style.cursor = 'default';
				recipiente.innerHTML = ajax.responseText;
				rndImagens();
				enviaInscricao();
				getDivs();
			}
		}
	}

	ajax.send(null);
	
}

function rndImagens() {
	var ajax = openAjax();
	var recipiente = gE('con_direita');
	
	ajax.open('GET', 'bib_ajax/paginas.php?function=ajaxRndImagens', true); 
	
	ajax.onreadystatechange = function() {
		if (ajax.readyState == 4) {
			if (ajax.status == 200) {
				document.body.style.cursor = 'default';
				recipiente.innerHTML = ajax.responseText;
				enviaInscricao();
				getDivs();
			}
		}
	}

	ajax.send(null);
	
}

function carregando() {
	var recipiente = gE('con_esquerda');
	recipiente.innerHTML = "<div id=\"loading\"><img src=\"img/indicator_lite.gif\" width=\"24\" height=\"24\" /><br />Carregando...</div>";
}

function valida_form(){
	//criando as variaveis de verificacao
	var i;
	var cur=0;
	var fp=0; // fp variavel para validacao de forma de pagamento
	
	with(document.form1){
		//curso0.length
		 
	   if (curso0.checked == true){
		   cur = 1;
	   }  
	
	
	
	   if (curso1.checked == true){
		   cur = 1;
	   }  
	
		
		for (i=0;i<pag.length;i++){ 
    	   if (pag[i].checked == true){
			   fp = 1;
		   }  
  	  	}
		
		if(!cur){
			alert("Por favor escolha o curso que irá se inscrever.");
			return false;
		}
		
		//If para o principal
		if(!nome.value || !cpf_cnpj.value || !endereco.value || !cidade.value || !cep.value || !estado.value || !ddd.value || !telefone.value || !email.value || !fp ){
			alert("Todos os campos que possuem (*) são de preenchimentos obrigatórios!");
			return false;
		}else{
			return true;	
		}
	}
}

function enviaInscricao(){
	if (!gE('btnEnvIns')) return false;
	gE('btnEnvIns').onclick = function(){
		var valida = valida_form();
		if(!valida) {return false;}
		enviandoInscricao();
		
		var ajax = openAjax();
		ajax.open('POST', 'bib_ajax/paginas.php?function=ajaxEnviaInscricao', true);
		ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		ajax.onreadystatechange = function() {
			if (ajax.readyState == 4) {
				if (ajax.status == 200) {
					//alert("Inscricao enviada com sucesso.");
					removerDivs();
					txtPagina(7);
				}
			}
		}
	
		if(document.form1.curso0.checked){
			var curso0 = document.form1.curso0.value;
		}
		
		if(document.form1.curso1.checked){
			var curso1 = document.form1.curso1.value;
		}
		
		for (i=0;i<5;i++){ 
			if(document.form1.pag[i].checked){
				var pagamento = document.form1.pag[i].value;
			}
		}

		for (i=0;i<2;i++){ 
			if(document.form1.assinante[i].checked){
				var assinante = document.form1.assinante[i].value;
			}
		}
	
		var dataPost = '&curso0=' + curso0;
		dataPost += '&curso1=' + curso1;
		//dataPost += '&curso2=' + curso2;
		//dataPost += '&curso3=' + curso3;
		dataPost += '&nome=' + gE('nome').value;
		dataPost += '&empresa=' + gE('empresa').value;
		dataPost += '&cargo=' + gE('cargo').value;
		dataPost += '&cpf_cnpj=' + gE('cpf_cnpj').value;
		dataPost += '&inscricao_estadual=' + gE('inscricao_estadual').value;
		dataPost += '&assinante=' + assinante;
		dataPost += '&endereco=' + gE('endereco').value;
		dataPost += '&cidade=' + gE('cidade').value;
		dataPost += '&cep=' + gE('cep').value;
		dataPost += '&estado=' + gE('estado').value;
		dataPost += '&ddd=' + gE('ddd').value;
		dataPost += '&telefone=' + gE('telefone').value;
		dataPost += '&email=' + gE('email').value;
		dataPost += '&codigo=' + gE('codigo').value;
		dataPost += '&associado=' + gE('associado').value;
		dataPost += '&maisp=' + gE('maisp').value;
		dataPost += '&pag=' + pagamento;
		ajax.send(dataPost);
		alert('Inscricao enviada co sucesso!');
		return false;
	}
}

function enviandoInscricao(){
	var objBody = gEs('body').item(0);
	var boxInscricao = document.createElement('div');
	var altTela = Math.floor(document.body.offsetHeight);
	var largTela =  Math.floor(document.body.offsetWidth);
	
	boxInscricao.setAttribute('id','boxInscricao');
	boxInscricao.style.left = ((largTela/2)-(500/2))+"px";
	
	var vScrollY = 0;
        
	if (document.all){
		if (!document.documentElement.scrollTop){
			vScrollY = document.body.scrollTop;
		}else{
			vScrollY = document.documentElement.scrollTop;
		}
	}else{
		vScrollY = window.pageYOffset;
	}
	
	boxInscricao.style.top=((screen.availHeight/2) + vScrollY - (650/2))+"px";
	objBody.insertBefore(boxInscricao, objBody.lastChild);
	boxInscricao.innerHTML = "<div id=\"loading\"><img src=\"img/indicator_lite.gif\" width=\"24\" height=\"24\" /><br />Enviando inscriçao...</div>";
}

function removerDivs() {
	var boxInscricao = gE('boxInscricao');
	boxInscricao.parentNode.removeChild(boxInscricao);
}