// JavaScript Document
UTF8 = {
    encode: function (s) {
        for (var c, i = -1, l = (s = s.split("")).length, o = String.fromCharCode; ++i < l;
            s[i] = (c = s[i].charCodeAt(0)) >= 127 ? o(0xc0 | (c >>> 6)) + o(0x80 | (c & 0x3f)) : s[i]
        );
        return s.join("");
    },
    decode: function (s) {
        for (var a, b, i = -1, l = (s = s.split("")).length, o = String.fromCharCode, c = "charCodeAt"; ++i < l;
            ((a = s[i][c](0)) & 0x80) &&
            (s[i] = (a & 0xfc) == 0xc0 && ((b = s[i + 1][c](0)) & 0xc0) == 0x80 ?
            o(((a & 0x03) << 6) + (b & 0x3f)) : o(128), s[++i] = "")
        );
        return s.join("");
    }
};

function html_scape($strIn) {
$arr_procura = array ('á', 'à', 'ã', 'â', 'ä', 'õ', 'ò','ó', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'ï', 'ç', 'Á', 'À', 'Ã', 'Â', 'Ä', 'Õ','Ò', 'Ó', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Ï', 'Ç');
$arr_troca = array ('&aacute;','&agrave;', '&atilde;', '&acirc;', '&auml;', '&otilde;', ' &ograve;', '&ocirc;', '&egrave;', '&eacute;', ' &ecirc;', '&euml;', '&igrave;', '&iacute;', '&iuml;','&ccedil;', '&Aacute;','&Agrave;', '&Atilde;', '&Acirc;', '&Auml;', '&Otilde;', '&Ograve;', '&Ocirc;', '&Egrave;', '&Eacute;', '&Ecirc;', '&Euml;', '&Igrave;', '&Iacute;', '&Iuml;','&Ccedil;');
return str_replace($arr_procura, $arr_troca, $strIn);
}

function Expande(obj)
{
	if (obj.style.visibility == "visible")
	{
	    obj.style.visibility = "hidden";
		obj.style.display = "none";
	}
	else
	{
    	obj.style.visibility = "visible";
		obj.style.display = "block";
	}
}

function abrirGols(Jogo, Camp) {
   var width = 600;
   var height = 170;
   var left = 100;
   var top = 150;
   window.open('jogo_detalhes.asp?jid='+Jogo+'&cid='+Camp,'_blank',
                'width='+width+', height='+height+', top='+top+', left='+left+
                ',toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0');
}

function Editar(vLink, vJanela, vAlt, vLarg)
{
    var objWindow=window.open(vLink, vJanela, 'width='+vLarg+',height='+vAlt+',toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0');
}

function WindowOpen(vLink, vJanela, vAlt, vLarg)
{
    var objWindow=window.open(vLink, vJanela, 'width='+vLarg+',height='+vAlt+',toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1');
}

try { xmlhttp = new XMLHttpRequest();} catch( ee ) {
try { xmlhttp = new ActiveXObject( "Msxml2.XMLHTTP" ); } catch( e ) {
try { xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" ); } catch( E ) {
	  xmlhttp = false; } } }

function goAjax(url, metodo, modo, tagRetorno, parametros)
{
    document.getElementById(tagRetorno).innerHTML='<br/><br/><center><div class="carregando"><img src="images/carregando.gif"><br/><br/>carregando...<br/><br/></div>';

    if(metodo == "GET")
        xmlhttp.open("GET", url, modo);
    else
    {
        xmlhttp.open("POST", url, modo);
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
        xmlhttp.setRequestHeader("Pragma", "no-cache");
    }

    xmlhttp.onreadystatechange = function()
    {
        if(xmlhttp.readyState == 4)
        {
            retorno = xmlhttp.responseText;
            document.getElementById(tagRetorno).innerHTML = (retorno);
            //findScript(retorno)
        }
    }

    if(metodo == "GET")
        xmlhttp.send(null);
    else
        xmlhttp.send(parametros);
}

function goDataAjax(url, metodo, modo, tagRetorno, parametros) {
    oldText = document.getElementById(tagRetorno).innerHTML;
    document.getElementById(tagRetorno).innerHTML = '<center><div class="carregando">Aguarde...</div>';

    if (metodo == "GET")
        xmlhttp.open("GET", url, modo);
    else {
        xmlhttp.open("POST", url, modo);
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
        xmlhttp.setRequestHeader("Pragma", "no-cache");
    }

    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4) {
            retorno = xmlhttp.responseText;
            if (retorno[0] == 'E' && retorno[1] == 'R' && retorno[2] == 'R' && retorno[3] == 'O' && retorno[4] == ':') {
                alert(retorno);
                document.getElementById(tagRetorno).innerHTML = oldText;
            }
            else
                document.getElementById(tagRetorno).innerHTML = retorno;
            //findScript(retorno)
        }
    }

    if (metodo == "GET")
        xmlhttp.send(null);
    else
        xmlhttp.send(parametros);
}

function OffMenu(menu)
{
   document.getElementById("m" + menu).bgColor = "#fff";
   document.getElementById("m" + menu).style.color = "#999";
}
function OnMenu(menu)
{
   document.getElementById("m" + menu).bgColor = "#ccc";
   document.getElementById("m" + menu).style.color = "#fff";
}
function enviaPage(url, metodo, modo, tagId, parametros)
{
    if (parametros != "")
        goAjax( url+"?"+parametros+"&rnd"+ Math.random() , metodo, modo , "retMenu", "");
    else
        goAjax( url + "?rnd"+ Math.random() , metodo, modo , "retMenu", "");
    Menu(tagId);
}

function AbaClick(url, parametros, metodo, modo, _target, vAbaId, vAbaAtual, vAbaTotal)
{
    if (parametros != "")
        goAjax( url+"?"+parametros+"&rnd"+ Math.random() , metodo, modo , _target, "");
    else
        goAjax( url + "?rnd"+ Math.random() , metodo, modo , _target, "");
    OrganizaAbas(vAbaId, vAbaAtual, vAbaTotal);
}

function OrganizaAbas(vAbaId, vAbaAtual, vAbaTotal)
{
    var i;
    for (i=0;i<vAbaTotal;i++)
    {
        if (vAbaAtual != vAbaTotal)
        {
            alert("c" + vAbaId + i);
           	document.getElementById("c" + vAbaId + i).style.color = "#FFFFFF";
        	document.getElementById("c" + vAbaId + i).style.backgroundImage = "url(images/menu_center_off.jpg)";
        	document.getElementById("l" + vAbaId + i).style.backgroundImage ="url(images/menu_left_off.jpg)";
        	document.getElementById("r" + vAbaId + i).style.backgroundImage ="url(images/menu_right_off.jpg)";

        }
        else
        {
            alert("c" + vAbaId + i);
        	document.getElementById("c" + vAbaId + i).style.color = "#000000";
        	document.getElementById("c" + vAbaId + i).style.backgroundImage ="url(images/green_ml.jpg)";
        	document.getElementById("l" + vAbaId + i).style.backgroundImage ="url(images/green_ml.jpg)";
        	document.getElementById("r" + vAbaId + i).style.backgroundImage ="url(images/green_ml.jpg)";
        }
    }
}

function SendPage(url, metodo, modo, tagId, parametros)
{
    if (parametros != "")
        goAjax( url+"?"+parametros+"&rnd"+ Math.random() , metodo, modo , tagId, "");
    else
        goAjax( url + "?rnd"+ Math.random() , metodo, modo , tagId, "");
}

function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
		return new XMLHttpRequest();

	if (window.ActiveXObject)
		return new ActiveXObject("Microsoft.XMLHTTP");

	return null;
}

function sendAjax(url, metodo, modo, parametros, tagRetorno, formatoRetorno)
{
    document.body.style.cursor = 'wait';

	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null){return "Seu browser não suporta AJAX!";}

  	if (tagRetorno != "")
		document.getElementById(tagRetorno).innerHTML='<div class="carregando" style="position:fixed;width:100%;height:100%;text-align:center;vertical-align:middle;"><div style="top:50%;position:relative;background-color:black;color:white;border:solid 1px white;"><img src="images/carregando.gif"><br/><br/>carregando...<br/><br/></div></div>';

    var vUrl = url + "?tp=" + formatoRetorno;

    if (metodo == "GET")
        vUrl += "&" + parametros;
    xmlhttp.open(metodo, vUrl, modo);

    if (metodo == "GET")
        xmlhttp.send(null);
    else
    {
        xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlhttp.send(parametros);
    }

    document.body.style.cursor = 'default';
    var retorno
    if (formatoRetorno == "XML")
        retorno = xmlhttp.responseXML;
    else if (formatoRetorno == "HTML")
        retorno = xmlhttp.responseHTML;
    else
        retorno = xmlhttp.responseText;

    return retorno;
}

function EnviarLoteca(vId)
{
    var Param = 'op=Loteca&id=' + vId + '&concurso=' + document.getElementById('concurso' + vId).value + '&numero=' + document.getElementById('numero' + vId).value;
    alert(sendAjax('sendAjax.asp', 'POST', false, Param, "", "Text"));
}

function LotecaResults(vConcurso, vJogoId)
{
    var Param = 'concurso=' + vConcurso + "&jogo=" + vJogoId + "&tipo=janela&guid=" + guid();
    var Ret = sendAjax('loteca.asp', 'GET', false, Param, "divAjax", "Text");
    document.getElementById('divAjax').style.visibility = 'visible';
    document.getElementById('divAjax').innerHTML = Ret;
}

function BolaoPalpites(vJogoId)
{
    var Param = 'id=' + vJogoId + "&tipo=janela&guid=" + guid();
    var Ret = sendAjax('bolao_palpites_div.asp', 'GET', false, Param, "divAjax", "Text");
    document.getElementById('divAjax').style.visibility = 'visible';
    document.getElementById('divAjax').innerHTML = Ret;
}

function Sumula(vJogoId)
{
    var Param = 'JogoId=' + vJogoId + '&tipo=janela&guid=' + guid();
    var Ret = sendAjax('jogoInfo.asp', 'GET', false, Param, 'divAjaxAtualiza', 'Text');
    document.getElementById('divAjaxAtualiza').style.visibility = 'visible';
    document.getElementById('divAjaxAtualiza').innerHTML = Ret;
}

function S4() {
   return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}

function guid() {
   return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}

function menuclick(menu)
{
    if (document.getElementById(menu).style.display == 'none')
    {
        document.getElementById(menu).style.display = 'block';
        document.getElementById('i'+menu).src = "/figuras/collapse.gif";
    }
    else
    {
        document.getElementById(menu).style.display = 'none';
        document.getElementById('i'+menu).src = "/figuras/expand.gif";
    }
}

/* Novas funções para integrar com o envio de dados por ajax */
function editarData(campo, param) {
    if (campo.substr(0, 7) == 'JogoAux') {
        document.getElementById(campo).style.display = 'block';
        document.getElementById(campo).style.visibility = 'visible';
    }
    goDataAjax('sendDataAjax.asp', "POST", 'true', campo, param);
}
function enviarData(oper, campo) {
    var param = document.getElementById(campo + '_Param').value;

    if (campo.substr(0, 7) == 'JogoAux')
        document.getElementById(campo).style.visibility = 'hidden';

    if (oper == 'd') {
        param += '&d=';
        param += document.getElementById(campo + '_DataJogo').value;
        param += '&h=';
        param += document.getElementById(campo + '_HoraJogo').value;
    }
    else if (oper == 'r') {
        param += '&r=';
        param += document.getElementById(campo + '_Rodada').value;
        param += '&campId=';
        param += document.getElementById(campo + '_CampId').value;
    }
    else if (oper == 'placar') {
        param += '&gm=';
        param += document.getElementById(campo + '_GM').value;
        param += '&gv=';
        param += document.getElementById(campo + '_GV').value;
    }
    else if (oper == 'situacao') {
        param += '&sit=';
        param += document.getElementById(campo + '_Sit').value;
    }
    goDataAjax('sendDataAjax.asp', "POST", 'true', campo, param);
}

function enviarEquipe(oper, campo) {
    var param = document.getElementById(campo + '_Param').value;
    param += '&EquipeId=';
    param += document.getElementById(campo + '_EquipeId').value;
    param += '&EquipeNome=';
    var indice = document.getElementById(campo + '_EquipeId').selectedIndex;
    var textoEscolhido = document.getElementById(campo + '_EquipeId').options[indice].text;
    param += textoEscolhido;
    var img = "<img border='0' height='30' src='figuras/escudos/";
    img += document.getElementById(campo + '_EquipeId').value;
    img += ".png'>";
    //alert(campo + 'Escudo');
    document.getElementById(campo + 'Escudo').innerHTML = img;
    goDataAjax('sendDataAjax.asp', "POST", 'true', campo, param);
}

function enviarEstadio(campo, estadioId, jogoId, estadioNome, mandanteId) {
    var param = 'op=enviaEstadio&st=E';
    param += '&eid=';
    param += estadioId;
    param += '&id=';
    param += jogoId;
    param += '&eNome=';
    param += estadioNome;
    param += '&mid=';
    param += mandanteId;
    goDataAjax('sendDataAjax.asp', "POST", 'true', campo, param);
    document.getElementById('JogoAux' + jogoId).style.visibility = 'hidden';
    document.getElementById('JogoAux' + jogoId).style.display = 'none';
}

function buscarEstadio(divBusca, campoBusca, campoRetorno, jogoId, mandanteId) {
    if (document.getElementById(campoBusca).value == '') {
        alert('Preencha o campo busca.');
        document.getElementById(campoBusca).focus();
        return;
    }
    var param = 'op=buscaEstadio&st=V&estadioBusca=';
    param += document.getElementById(campoBusca).value;
    param += '&campoRetorno=';
    param += campoRetorno;
    param += '&id=';
    param += jogoId;
    param += '&mid=';
    param += mandanteId;
    goDataAjax('sendDataAjax.asp', "POST", 'true', divBusca, param);
}

function estatisticas(jogo)
{
    obj1 = document.getElementById('1' + jogo);
    obj2 = document.getElementById('2' + jogo);
    if (obj1.style.visibility == "visible")
    {
      obj1.style.visibility = "hidden";
      obj1.style.display = "none";
      document.getElementById(jogo).innerHTML = '+ Info';
    }
    else
    {
      obj1.style.visibility = "visible";
      obj1.style.display = "block";
      document.getElementById(jogo).innerHTML = '- Info';
    }
    obj2.style.visibility = obj1.style.visibility;
    obj2.style.display = obj1.style.display;
}

function BuscaNome(operacao, campoId, campoNome) {
    var param = 'op='
    param += operacao;
    param += '&Id=';
    param += document.getElementById(campoId).value;
    goDataAjax('getDataAjax.asp', "POST", 'true', campoNome, param);
}
