// Variável responsável pelo pedido assíncrono.
var req;

// Função responsável pelo pedido assíncrono.
function getXMLHttpRequest(metodo,url,tpChamada,callback) {
	// Trabalha com inicializações diferentes para IE e Outros.
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP")
	}
	// Faz a chamada ao servlet.
	req.open(metodo,url,tpChamada);	
	req.onreadystatechange = eval(callback);
	req.send(null);
//	alert(req.responseText);
 }

function PedidoGravaPos(){
	
	var aux = " " + stringCheckbox();
	var url = "ServletPos?acao=inserir";
	url = constroiUrl(document.getElementById("form1"),url); 
//	alert(url);
	url +="&"+"interesse"+"="+escape(aux);
	//alert(url);
	getXMLHttpRequest("POST",url,true,"gravaPos");

		
}

function gravaPos(){


	if(req.readyState ==4){
	
		if(req.status ==200){		
			var resposta = req.responseText;
			 
				if(resposta != "false"){
				
					alert("Informações enviadas com sucesso!");
					limpa();
	
									
				}else{
				
					alert("Informações não enviadas! Tente Novamente!");
				}
				
		  }else {
			alert(req.status);
		}
				
			
	
		}
		
	
}	

function constroiUrl(formulario,url){
	for (var i=0; i<formulario.elements.length; i++){
	if(formulario.elements[i].type == "text" || formulario.elements[i].id == "disciplinas" || formulario.elements[i].id == "carga" || formulario.elements[i].id == "1" ){
		url += "&"+formulario.elements[i].name+"="+  escape(formulario.elements[i].value);	
			 
	}
	
 }

return url;
}




function limpa(){
  
    document.getElementById("nome").value = "";
	document.getElementById("email").value = "";
	document.getElementById("telefone").value = "";
	document.getElementById("disciplinas").value = "";
	document.getElementById("carga").value = "";	
	document.getElementById("1").checked = "";
	document.getElementById("2").checked = ""; 
	document.getElementById("3").checked = "";
	document.getElementById("4").checked = ""; 
	document.getElementById("5").checked = ""; 
	document.getElementById("6").checked  = ""; 
	document.getElementById("7").checked  = "";
	document.getElementById("8").checked  = "";
	document.getElementById("9").checked  = ""; 
	document.getElementById("10").checked  = "";
	document.getElementById("11").checked  = "";
	document.getElementById("12").checked  = "";

}
function stringCheckbox() {
	formulario = document.form1;
	var retorno= "";
	for (var i = 0 ; i < formulario.length ; i++) {
		try {
		if (formulario.elements[i].type == "checkbox" && formulario.elements[i].className == "interesse") {
			if (formulario.elements[i].checked == true) {
				retorno += formulario.elements[i].value+",";
				
			}
		}
		} catch (e) {
			// error
			
		}	
	}
	
	return retorno;
}

function isCheck(campo) {
	if (campo.value == "S") {
		campo.value = "Não";
	} else {
		campo.value = "Sim";
	}
}


function limpaTela(){
limpaTabela(document.getElementById("tbory"));
document.getElementById("o").value = "";


}
//////////////////////////////////////////////////////////////////////////////////////////////
function constroiUrlCurso(){

	var form1 = document.getElementById("form1");
	
	for (var i=0; i<form1.elements.length; i++){

	if(form1.elements[i].id == "cursol" ){
	
		//var url ="ServletPos?acao=selecionarCurso"; 
		//url += "&"+form1.elements[i].name+"="+  escape(form1.elements[i].value);	
		//alert(url);
		
		var curso =  escape(form1.elements[i].value);
		 
		}
	 }
	 return curso;
}



function pedidoSelecionarCurso(){
	var url = "ServletPos?acao=selecionaCurso&curso="+constroiUrlCurso();	
	//url = constroiUrl(document.getElementById("form1"),url);	
	getXMLHttpRequest("POST",url,true,"selecionaCursos");


}

function selecionaCursos(){

limpaTela();
	if(req.readyState ==4){

		if(req.status ==200){
			var resposta = req.responseText;
		
				var arrayResposta = resposta.split(";");
			
				if(resposta != "false" ){
				//limpaTabela(document.getElementById("tb_todos"));
			//	var novoTbody = document.createElement("TBODY");
				
				for( var i = 0; i< arrayResposta.length; i++){
					
				var json1 = eval('('+arrayResposta[i]+')');
				
				var tablebody = document.getElementById("tbory");
				
				var novoTr = document.createElement("TR");
					novoTr.bgcolor = "#FFFFFF";
				
				
				var novoTd = document.createElement("TD");
				//var novoTd2 = document.createElement("TD");
				var novoTd3 = document.createElement("TD");
				//var novoTd4 = document.createElement("TD");
				//var novoTd5 = document.createElement("TD");
				//var novoTd6 = document.createElement("TD");
				//var novoTd7 = document.createElement("TD");
				//var novoTd8 = document.createElement("TD");
				var novoLink = document.createElement("A");
			
				novoLink.appendChild(document.createTextNode(json1.nome));
				
			
				novoLink.href ="javascript:pedidoSelecionarInsc('"+json1.nome+"')";
				
				novoTd.appendChild(novoLink);
				//novoTd.appendChild(document.createTextNode(json.nome));
				//novoTd2.appendChild(document.createTextNode(json.email));
				novoTd3.appendChild(document.createTextNode(json1.telefone));
				//novoTd4.appendChild(document.createTextNode(json.curso));
				//novoTd5.appendChild(document.createTextNode(json.marketing));
				//novoTd6.appendChild(document.createTextNode(json.interesse));
				//novoTd7.appendChild(document.createTextNode(json.auto_email));
				//novoTd8.appendChild(novoLink);
				
				novoTr.appendChild(novoTd);
				//novoTr.appendChild(novoTd2);
				novoTr.appendChild(novoTd3);
				//novoTr.appendChild(novoTd4);
				//novoTr.appendChild(novoTd5);
				//novoTr.appendChild(novoTd6);
				//novoTr.appendChild(novoTd7);
				//novoTr.appendChild(novoTd8);
				
				
				//novoTbody.appendChild(novoTr);
				tablebody.appendChild(novoTr);
				
				
				
				}
				}else{
				
					alert("Falha no carregamento!!!");
				}
		} 
		
	}
}
