var browseCap = new BrowserCaps();

function BrowserCaps(){
	this.Layers = (document.layers)?true:false;
	this.All = (document.all)?true:false;
	this.DOM = (document.getElementById)?true:false;
	this.IE4 = (this.All && !this.DOM);
	this.NS6 = (this.DOM && !this.All);
	this.NS60 = false;
	if(this.NS6){
		var minVer = String(navigator.appVersion);
		dotPos = minVer.indexOf('.');
		minVer = minVer.substr(dotPos+1,minVer.length-dotPos);
		if(parseInt(minVer) == 0) {
			this.NS60 = true; //Wrong, they are all with minVer == 0 :(
		}
	}
	this.Opera = (window.opera)?true:false;
}

function loadURL(input_ID){
	nome_input = input_ID.getAttribute('name');
	loadHTML('http://www.fpx.com.br/fpxsys/rpc_equipes.asp?id='+input_ID.value+'&input='+nome_input, urlLoaded);
}

function urlLoaded(sHTML){
	//alert(sHTML);
	if(trim(sHTML) == 'VAZIO'){
		alert('ID não encontrado no banco de dados.');
	}else{
		//alert(sHTML);
		//preencher campos por aqui
		a_valores = sHTML.split("::");
		entidade_escolhida = document.getElementById('entidade_filiada').value;
		if(entidade_escolhida == '0'){
			alert('Escolha primeiro a entidade.');
			return false;
		}
		//alert(trim(a_valores[3]));
		//alert(entidade_escolhida);
		if(trim(a_valores[3]) != entidade_escolhida){
			if(trim(a_valores[3]) == '14'){//trata o avulso
				if(trim(a_valores[5]) != 'OK'){
					alert('Jogador avulso não consta como liberado para inscrições no sistema.');
					return false;
				}
			}else{
				alert('A entidade do jogador não confere com a entidade escolhida para a equipe.');
				return false;		
			}
		}
		if(a_valores[2].substring(0,13) == 'idfpx_reserva'){ //reserva
			//alert('reserva');
			//alert(a_valores[2].substring(0,13));
			//alert(a_valores[2].substring(13,14));
			c_linha = a_valores[2].substring(13,14);
			a_data_nasc = a_valores[1].split("/");
			c_data_nasc = strzero(a_data_nasc[1],2)+'/'+strzero(a_data_nasc[0],2)+'/'+strzero(a_data_nasc[2],4);
			document.getElementById('reserva'+c_linha).value = a_valores[0];
			document.getElementById('nasc_reserva'+c_linha).value = c_data_nasc;
		}else{//titular
			//alert('titular');
			//alert(a_valores[2].substring(0,5));
			//alert(a_valores[2].substring(5,6));
			c_linha = a_valores[2].substring(5,6);
			a_data_nasc = a_valores[1].split("/");
			c_data_nasc = strzero(a_data_nasc[1],2)+'/'+strzero(a_data_nasc[0],2)+'/'+strzero(a_data_nasc[2],4);
			document.getElementById('jogador'+c_linha).value = a_valores[0];
			document.getElementById('nasc_jogador'+c_linha).value = c_data_nasc;
		}
		//document.frm.targ.value = sHTML;
	}
}

function loadHTML(sURL, callback){
	if(!browseCap) {
		browseCap = new BrowserCaps();
	}

	if(browseCap.Layers){ //NS4 -> sURL have to be an absolute URI
		var res = '';
		buffer = new java.io.BufferedReader(new java.io.InputStreamReader(new java.net.URL(sURL).openStream()));
		while ((line = buffer.readLine())!=null) {
			res += line + '\n';
		}
		if (buffer != null) {
			buffer.close();
		}
		callback(res);
	}else if(browseCap.All){ //IE
        if(browseCap.IE4){ //IE4
          //No luck with this one :(
        }else{//IE5+
			/*
			var buffer = new Option();
			buffer.addBehavior("#default#download");
			buffer.startDownload(sURL,callback);
			buffer = null;
			*/
			xhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
			xhttp.open("GET", sURL, true);
			xhttp.onreadystatechange = CallBackIE;
			//finally send the call
			xhttp.send();     
        }
	}else{ //NS6.1+, Moz1+  ->>> NS6.0 does not recognize the XMLHttpRequest :( Nor was I abble to use java.io - access denied...
        var buffer = new XMLHttpRequest();
        buffer.open("GET",sURL,false);
        buffer.send(null);
        var res = buffer.responseText;
        callback(res);
	}
}

function CallBackIE(){
	if (xhttp.readyState == 4){
		urlLoaded(xhttp.responseText);
	}
}
