// global flag
var isIE = false;

// global request and XML document objects
var req;

var tipo;

// retrieve XML document (reusable generic function);
// parameter is URL string (relative or complete) to
// an .xml file whose Content-Type is a valid XML
// type, such as text/xml; XML source must be from
// same domain as HTML file
function loadXMLDoc(url) {
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.setRequestHeader( "Pragma", "no-cache" );
		req.setRequestHeader( "Cache-Control", "must-revalidate" );
		req.setRequestHeader( "Cache-Control", "no-cache" );
		req.setRequestHeader( "Cache-Control", "no-store" );
		req.send(null);
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		isIE = true;
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = processReqChange;
			req.open("GET", url, true);
			req.setRequestHeader( "Pragma", "no-cache" );
			req.setRequestHeader( "Cache-Control", "must-revalidate" );
			req.setRequestHeader( "Cache-Control", "no-cache" );
			req.setRequestHeader( "Cache-Control", "no-store" );
			req.send();
		}
	}
}

function processReqChange() {
	// only if req shows "loaded"
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			switch (tipo) {
				case "VerificaEmailAtleta": ApagaFotoAtleta(); break;
				case "ApagaFotoAtleta" :    ApagaFotoAtleta(); break;
				case "VerificaCpfAtleta":   VerificaCpfAtleta(); break;
				case "ApagaFotoTecnico":    ApagaFotoTecnico(); break;
				case "RetornoFotoRepresentante":    RetornoFotoRepresentante(); break;
				
			}
			/*if ( tipo == "VerificaEmailAtleta" ) {
				VerificaEmailAtleta();
			}else if(tipo == "ApagaFotoAtleta"){
				ApagaFotoAtleta();
			}else if(tipo == "VerificaCpfAtleta"){
				VerificaCpfAtleta();
			}else if(tipo == "ApagaFotoTecnico"){
				ApagaFotoTecnico();
			}*/
		} else {
			alert("Ocorreu um problema no recebimento do XML data:\n" + req.statusText);
		}
	}
}

function loadDocUrl(tipo_selec,url) {
	try {
		tipo = tipo_selec;
		loadXMLDoc(url);
	}
	catch(e) {
		var msg = (typeof e == "string") ? e : ((e.message) ? e.message : "Unknown Error");
		alert("Não foi possível recuperar o XML data:\n" + msg);
		return;
	}
}

function loadDoc(tipo_selec, arquivo, valor) {
	if (valor!="")
	{
		try {
			tipo = tipo_selec;
			url = arquivo + ".php?codigo="+valor;
			loadXMLDoc(url);
		}
		catch(e) {
			var msg = (typeof e == "string") ? e : ((e.message) ? e.message : "Unknown Error");
			alert("Não foi possível recuperar o XML data:\n" + msg);
			return;
		}
	}
}
function loadDoc2(tipo_selec,arquivo,valor,valor2) {
	if (valor!="")
	{
		try {
			tipo = tipo_selec;
			url = arquivo + ".php?codigo="+valor+"&codigo2="+valor2;
			loadXMLDoc(url);
		}
		catch(e) {
			var msg = (typeof e == "string") ? e : ((e.message) ? e.message : "Unknown Error");
			alert("Não foi possível recuperar o XML data:\n" + msg);
			return;
		}
	}
}

function VerificaEmailAtleta(){
	BoolEmail = req.responseText;
	if (BoolEmail=="1"){
		document.frmAtleta.ALU_DSC_EMAIL.value="";
		alert("Email já cadastrado.\n\nPor favor, digite outro.");
		document.frmAtleta.ALU_DSC_EMAIL.focus();
	}
}
function VerificaCpfAtleta(){
	BoolCpf = req.responseText;
	if (BoolCpf=="1"){
		document.frmAtleta.ALU_DSC_CPF.value="";
		alert("CPF já cadastrado.\n\nPor favor, digite outro.");
		document.frmAtleta.ALU_DSC_CPF.focus();
	}
}

function ApagaFotoAtleta(){
	Apagou = req.responseText;
	if (Apagou=="OK"){
		document.getElementById("RemoverFoto").style.display="none";
		alert("Foto apagada com sucesso!");
	}else if(Apagou=="ERRO"){
		alert("Ocorreu um erro ao tentar apagar a foto.\nEntre em contato com o administrador.");
	}
}
function ApagarFoto(codigo){
	if (confirm("Tem certeza que deseja apagar a foto?")){
		loadDoc('ApagaFotoAtleta','apagafotoatleta',codigo);
	}
}

function ApagarFotoArbitro(codigo){
	if (confirm("Tem certeza que deseja apagar a foto?")){
		loadDoc('ApagaFotoArbitro','apagafotoarbitro',codigo);
	}
}

function ApagaFotoTecnico(){
	Apagou = req.responseText;
	if (Apagou=="OK"){
		document.getElementById("fotoTecnico").style.display="none";
		alert("Foto apagada com sucesso!");
	}else if(Apagou=="ERRO"){
		alert("Ocorreu um erro ao tentar apagar a foto.\nEntre em contato com o administrador.");
	}
}

function ApagarFotoTec(codigo){
	if (confirm("Tem certeza que deseja apagar a foto?")){
		loadDoc('ApagaFotoTecnico','apagafototecnico',codigo);
	}
}

function ApagarFotoRepresentante(codigo){
	if (confirm("Tem certeza que deseja apagar a foto?")){
		url = "apagafotorepresentante.php?codigo=" + codigo;
		tipo = "RetornoFotoRepresentante";
		loadXMLDoc(url);
	}
}

function RetornoFotoRepresentante () {
	Apagou = req.responseText;
	if (Apagou=="OK"){
		document.getElementById("RemoverFoto").style.display="none";
		alert("Foto apagada com sucesso!");
	}else if(Apagou=="ERRO"){
		alert("Ocorreu um erro ao tentar apagar a foto.\nEntre em contato com o administrador.");
	}
}