function ajax() 
{
};

ajax.prototype.iniciar = function() {

    try
    {
       this.xmlhttp = new XMLHttpRequest();
    }
    catch(ee)
    {
       try
       {
          this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
       }
       catch(e)
       {
          try
          {
             this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
          }
          catch(E)
          {
             this.xmlhttp = false;
           }
       }
    }
    return true;
}

ajax.prototype.ocupado = function() 
{
   estadoAtual = this.xmlhttp.readyState;
   return (estadoAtual && (estadoAtual < 4));
}

ajax.prototype.processa = function() 
{
   if (this.xmlhttp.readyState == 4 && this.xmlhttp.status == 200) 
   {
      return true;
   }
}

ajax.prototype.enviar = function(url, metodo, modo) 
{
   if (!this.xmlhttp) 
   {
	   this.iniciar();
   }
   if (!this.ocupado()) 
   {
	  if(metodo == "GET") 
      {
		 this.xmlhttp.open("GET", url, modo);
         this.xmlhttp.send(null);
      } 
      else 
      {
         this.xmlhttp.open("POST", url, modo);    
         this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
         this.xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
         this.xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
         this.xmlhttp.setRequestHeader("Pragma", "no-cache");
         this.xmlhttp.send(url);
      }
	  if (this.processa) 
      {
         return unescape(this.xmlhttp.responseText.replace(/\+/g," "));        //     retorno = retorno.replace(/[\r\n]/g,"");
      }
   }
   return false;
}

function getMostraMenu(vs_menu)
{
   document.getElementById(vs_menu).style.display = 'block';
}

function getEscondeMenu(vs_menu)
{
   document.getElementById(vs_menu).style.display = 'none';
}

function mascaraData(vs_valorCampo, vs_nomeCampo)
{
   var vd_data = vs_valorCampo.value;
   if (vd_data.length == 2)
   {
      vd_data = vd_data + '/';
      document.getElementById(vs_nomeCampo).value = vd_data;
      return true;              
   }
   
   if (vd_data.length == 5)
   {
      vd_data = vd_data + '/';
      document.getElementById(vs_nomeCampo).value = vd_data;
      return true;
   }
}

function mascaraDataHora(vs_valorCampo, vs_nomeCampo, vs_teclapres)
{
   var vd_data = vs_valorCampo.value;
   var tecla = vs_teclapres.keyCode;
   
 	if (tecla != 8)
	{
	  
      if (vd_data.length == 2)
      {
         vd_data = vd_data + '/';
         document.getElementById(vs_nomeCampo).value = vd_data;
         return true;              
      }
      
      if (vd_data.length == 5)
      {
         vd_data = vd_data + '/';
         document.getElementById(vs_nomeCampo).value = vd_data;
         return true;
      }
      
      if (vd_data.length == 10)
      {
         vd_data = vd_data + ' ';
         document.getElementById(vs_nomeCampo).value = vd_data;
         return true;
      }
      
      if (vd_data.length == 13)
      {
         vd_data = vd_data + ':';
         document.getElementById(vs_nomeCampo).value = vd_data;
         return true;
      }
      
      if (vd_data.length == 16)
      {
         vd_data = vd_data + ':';
         document.getElementById(vs_nomeCampo).value = vd_data;
         return true;
      }
   }
}

function mascaraCep(vs_valorCampo, vs_nomeCampo, vs_teclapres)
{
   var tecla = vs_teclapres.keyCode;
   var vn_cep = vs_valorCampo.value;
 	if (tecla != 8)
	{
      if (vn_cep.length == 5)
      {
         vn_cep = vn_cep + '-';
         document.getElementById(vs_nomeCampo).value = vn_cep;
         return true;              
      }   
   }
}

function mascaraCpf(vs_campo, vs_teclapres)
{
	var tecla = vs_teclapres.keyCode;
	var vr = new String(vs_campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace("/", "");
	vr = vr.replace("-", "");
	tam = vr.length + 1;
	if (tecla != 8)
	{
		if (tam == 4)
			vs_campo.value = vr.substr(0, 3) + '.';
		if (tam == 7)
			vs_campo.value = vr.substr(0, 3) + '.' + vr.substr(3, 6) + '.';
		if (tam == 11)
			vs_campo.value = vr.substr(0, 3) + '.' + vr.substr(3, 3) + '.' + vr.substr(7, 3) + '-' + vr.substr(11, 2);
	}
}

function mascaraTel(vs_valorCampo, vs_nomeCampo, vs_teclapres)
{
   var vn_cep = vs_valorCampo.value;
   var tecla  = vs_teclapres.keyCode;
   if (tecla != 8)
	{
      if (vn_cep.length == 4)
      {
         vn_cep = vn_cep + '-';
         document.getElementById(vs_nomeCampo).value = vn_cep;
         return true;              
      }
   }
}

function mascaraVlr( vs_campo
		           , vs_teclapres 
		           , vv_evento  
		           )
{
	if (vv_evento == undefined)
    vv_evento = "OnKeyUp";

	var tecla = vs_teclapres.keyCode;
	var vr = new String(vs_campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace(",", "");
	
	tam = vr.length;
	
	if ( tecla != 8 && 
	     vv_evento == 'OnKeyUp' ) 
	{
		if ( tam == 3 ) 
			vs_campo.value = vr.substr(0, 3) + ',';
	    if ( tam == 4 )
	    	vs_campo.value = vr.substr(0, 3) + ',' + vr.substr(3, 3);
		if ( tam == 5 )
			vs_campo.value = vr.substr(0, 3) + ',' + vr.substr(3, 3);
	}

	if ( vv_evento == 'onblur' )
    {
       if (tam == 0 )
       vs_campo.value = "000,00";
       
       if (tam == 1)
       vs_campo.value = "00" + vs_campo.value + ",00";
       
       if (tam == 2)
       vs_campo.value = "0" + vs_campo.value + ",00";
       
       if (tam == 3)
       vs_campo.value = vs_campo.value + "00";
       
       if (tam == 4)
       vs_campo.value = vs_campo.value + "0";
       
    }

}


function getCategoria( a_metodo
		             , a_funcao
		             , div_conteudo
		             , frm_vn_peso
		             , frm_tp_peso
		             , frm_vn_faixa_id
		             , vn_edicaoevento_id
		             , div_valor
		             , vn_pessoa_id
		             , vn_frm_federado
		             )
{

   vn_faixa_id = frm_vn_faixa_id;
   
   // Div
   div_conteudo           = document.getElementById(div_conteudo);
   div_conteudo.innerHTML = "<img src='./app.img/prod/images/img_carregando.gif' border='0'>";
   
   // Limpando valor
   div_valor           = document.getElementById(div_valor);
   div_valor.innerHTML = "<font color='red'> <b>ATENÇÃO !!! </br> Selecione apenas a(s) categoria(s) que irá competir. </br> Valores são calculados automaticamente. </b> </font> ";

   // Variaveis
   vn_peso      = document.getElementById(frm_vn_peso).value.replace(",",".");

    // Pegando a opção selecionada no radio de kimono
   for (i=0; i<document.formTEventoCadastro.frm_tp_peso.length; i++)
   {
      if (document.formTEventoCadastro.frm_tp_peso[i].checked==true)
      {
         vv_tp_peso = document.formTEventoCadastro.frm_tp_peso[i].value;
      }
   }
   
    // Pegando a opção selecionada no radio
   for (i=0; i<document.formTEventoCadastro.frm_faixa_id.length; i++)
   {
      if (document.formTEventoCadastro.frm_faixa_id[i].checked==true)
      {
         vn_faixa_id = document.formTEventoCadastro.frm_faixa_id[i].value;
      }
   }

   vs_variaveis = "&vn_peso="+vn_peso+"&vv_tp_peso="+vv_tp_peso+"&vn_faixa_id="+vn_faixa_id+"&vn_edicaoevento_id="+vn_edicaoevento_id+"&vn_pessoa_id="+vn_pessoa_id+"&frm_federado="+vn_frm_federado;

   // Inicio transação
   xmlhttp      = new ajax();
   vs_retorno   = xmlhttp.enviar('index.php?modulo=TAjax&a_metodo='+a_metodo+'&a_funcao='+a_funcao+vs_variaveis, "POST", false);
   div_conteudo.innerHTML = vs_retorno;

}

function setCategoria( a_metodo
		             , a_funcao
		             , div_conteudo
		             , vn_edicaoevento_id
		             , vn_sexo
		             , vn_valor
		             , vn_frm_federado
		             )
{
   
   div_conteudo     = document.getElementById(div_conteudo);
   div_conteudo_aux = div_conteudo.innerHTML;
   div_conteudo.innerHTML="<img src='./app.img/prod/images/img_carregando.gif' border='0'>";

   va_categoria = checkboxValues('cb_categoria_id');

   vs_variaveis = "&va_categoria="+va_categoria+"&vn_edicaoevento_id="+vn_edicaoevento_id+"&vn_sexo="+vn_sexo;

   xmlhttp      = new ajax();
   vs_retorno   = xmlhttp.enviar('index.php?modulo=TAjax&a_metodo='+a_metodo+'&a_funcao='+a_funcao+vs_variaveis+'&frm_federado='+vn_frm_federado, "POST", false);

   //alert("vs_retorno = "+vs_retorno);
   if(vs_retorno==0)
   {
      div_conteudo.innerHTML = "<font color='red'> <b>ATENÇÃO !!! </br> Selecione apenas a(s) categoria(s) que irá competir. </br> Valores são calculados automaticamente. </b> </font> ";
   }
   else if(vs_retorno==1)
   {
      // Desmarcando
      var va_nodes = document.forms["formTEventoCadastro"].elements["cb_categoria_id"];

      for (var i = 0; n = va_nodes[i]; i++) 
      {
//    	 alert('vlr1='+n.value+' - vlr2='+vn_valor);
//    	 if(n.value==vn_valor)
//         {
    		n.checked = 0;
//         }
      } 

      div_conteudo.innerHTML = "<font color='red'> <b>ATENÇÃO !!! </br> Selecione apenas a(s) categoria(s) que irá competir. </br> Valores são calculados automaticamente. </b> </font> ";

      // Dando o alerta
      alert('Opção inválida para competir ! ');
      //div_conteudo.innerHTML = div_conteudo_aux;
   }
   else
   {
      div_conteudo.innerHTML = vs_retorno;
   } 
}

function checkboxValues(name){
   var va_nodes = document.getElementsByName(name);
   var n;
   var p;
   var p = 0;
   var va_values = new Array();
   for (var i = 0; n = va_nodes[i]; i++) 
   {
      if (n.checked == true) {
         va_values[p]=n.value;
         p = p +1;
      }
   }
   return va_values;
}

function getCadastroAcademia(a_metodo, a_funcao, div_escuro, div_sobreposto)
{
   document.getElementById(div_escuro).style.display     = "block";
   document.getElementById(div_sobreposto).style.display = "block";
   xmlhttp      = new ajax();
   vs_retorno   = xmlhttp.enviar('index.php?modulo=TAjax&a_metodo='+a_metodo+'&a_funcao='+a_funcao, "POST", false);
   div_conteudo = document.getElementById(div_sobreposto);
   div_conteudo.innerHTML = vs_retorno;
}

function setFecharCadastroAcademia(div_escuro, div_sobreposto)
{
   document.getElementById(div_escuro).style.display = "none";
   document.getElementById(div_sobreposto).style.display = "none";
}

function setCadastroAcademia(a_metodo, a_funcao, frm_vs_academia, div_escuro, div_sobreposto)
{
   vs_academia = document.getElementById(frm_vs_academia).value;
   div_conteudo     = document.getElementById('c_cad_academia_retorno');
   div_conteudo.innerHTML="<img src='./app.img/prod/images/img_carregando.gif' border='0'>";
   xmlhttp          = new ajax();
   vn_academia_id   = xmlhttp.enviar('index.php?modulo=TAjax&a_metodo='+a_metodo+'&a_funcao='+a_funcao+'&vs_academia='+vs_academia, "POST", false);
   
   if(vn_academia_id>0)
   {
      document.getElementById(div_escuro).style.display     = "none";
      document.getElementById(div_sobreposto).style.display = "none";
      div_list_academia = document.getElementById('d_cadastro_academia');
      xmlhttp           = new ajax();
      vs_listitem       = xmlhttp.enviar('index.php?modulo=TAjax&a_metodo='+a_metodo+'&a_funcao=getAcademiaLista&vn_academia_id='+vn_academia_id, "POST", false);
      div_list_academia.innerHTML = vs_listitem;
   }
   else
   {
      div_conteudo.innerHTML = vn_academia_id;
   }
}

function getValidaTEventoCadastro(frm_nomeform)
{
   if(document.formTEventoCadastro.frm_academia_id.value.length<1)
   {
      alert('Você deve preencher a academia!');
      return false;
   }
   if(document.formTEventoCadastro.frm_professor.value.length<2)
   {
      alert('Você deve preencher o nome do seu professor!');
      return false;
   }
   if(document.formTEventoCadastro.frm_peso.value<=0 || document.formTEventoCadastro.frm_peso.value=='0,000' || document.formTEventoCadastro.frm_peso.value=='0.000')
   {
      alert('Você deve informar seu peso!');
      return false;
   } 
   else if(document.formTEventoCadastro.termo_aceite_site.checked==false)
   {
      alert('Você deve aceitar o termo do site para se cadastrar!');
      return false
   }
   else if(document.formTEventoCadastro.termo_aceite_evento.checked==false)
   {
      alert('Você deve aceitar o termo do evento para se cadastrar!');
      return false
   }
   else if(document.formTEventoCadastro.termo_aceite_medico.checked==false)
   {
      alert('Você deve aceitar o termo médico para se cadastrar!');
      return false;      
   }
   
   
   return true;
}

function getValidaTAlteraInscricao(frm_nomeform, a_metodo, a_funcao)
{
   if(document.formTEventoCadastro.frm_academia_id.value.length<1)
   {
      alert('Você deve preencher a academia!');
      return false;
   }
   if(document.formTEventoCadastro.frm_professor.value.length<2)
   {
      alert('Você deve preencher o nome do seu professor!');
      return false;
   }
   if(document.formTEventoCadastro.frm_peso.value<=0 || document.formTEventoCadastro.frm_peso.value=='0,000' || document.formTEventoCadastro.frm_peso.value=='0.000')
   {
      alert('Você deve informar seu peso!');
      return false;
   } 
   
   xmlhttp        = new ajax();
   vn_vlr_igual   = xmlhttp.enviar('index.php?modulo=TAjax&a_metodo='+a_metodo+'&a_funcao='+a_funcao, "POST", false);
   if(vn_vlr_igual==0)
   {
      alert('O valor da nova inscrição deve ser igual a inscrição original!');
      return false;
   }
   else
   {
      return true;
   }
   
   return true;
}

function getTermoSite(div_termo_site)
{
   document.getElementById(div_termo_site).style.display = "block"; 
}

function getTermoEvento(div_termo_evento)
{
   document.getElementById(div_termo_evento).style.display = "block"; 
}

function getParamConta(vn_vlr, a_metodo, a_funcao)
{
   div_conteudo     = document.getElementById('d_retorno_param');
   div_conteudo.innerHTML="<img src='./app.img/prod/images/img_carregando.gif' border='0'>";
   xmlhttp          = new ajax();
   vs_formulario    = xmlhttp.enviar('index.php?modulo=TAjax&a_metodo='+a_metodo+'&a_funcao='+a_funcao+'&organizador_id='+vn_vlr, "POST", false);
   div_conteudo.innerHTML = vs_formulario;
}

function getIntervalos(vn_vlr, a_metodo, a_funcao)
{
   div_conteudo     = document.getElementById('d_retorno_intervalos');
   div_conteudo.innerHTML="<img src='./app.img/prod/images/img_carregando.gif' border='0'>";
   xmlhttp          = new ajax();
   vs_formulario    = xmlhttp.enviar('index.php?modulo=TAjax&a_metodo='+a_metodo+'&a_funcao='+a_funcao+'&edicaoevento_id='+vn_vlr, "POST", false);
   div_conteudo.innerHTML = vs_formulario;
}

function getTroca()
{
   // Envia a solicitação xmlhttp
   $.post("?modulo=TAjax&a_metodo=TTrocaEdicaoEvento&a_funcao=getForm"
         , null
         , function(retorno)
   	     {
              // se retornou 1 entao os dados nao foram enviados
              if(retorno == false)
              { 
                 // Mostra os fundos
                 $("#troca_evento").addClass("troca_evento");
                 $("#c_fundo_escuro").show("slow");
                 
                 $("#troca_evento").html("Ocorreu um erro!!");
                 $("#troca_evento").show("slow");
              }
              else
              {
                 // Mostra os fundos
                 $("#troca_evento").addClass("troca_evento");
                 $("#c_fundo_escuro").show("slow");
                 
                 $("#troca_evento").html(retorno);
                 $("#troca_evento").show("slow");
              }
           }
        )
}

function setRetiraDivTrocaEvento()
{
   $('#c_fundo_escuro').hide("fast");
   $("#troca_evento").hide("fast");
}

function setTroca()
{
   // recupera os dados do form
	frm_edicaoevento_id = $('frm_edicaoevento_id').val();
   
   // Envia os dados
   $.post( "?modulo=TAjax&a_metodo=TTrocaEdicaoEvento&a_funcao=setTroca"
   	   , {edicaoevento_id: frm_edicaoevento_id}
         , function(retorno)
	        {
              // se retornou false entao os dados nao foram enviados
         	  if(retorno != false)
              {
                  $('#c_fundo_escuro').hide("fast");
                  $("#troca_evento").hide("fast");
                  $(location).attr('href',window.location.href);
              }
           }
         )
}

function getAtletaFederado( vv_element_id 
		                  , vn_federacao_id
		                  , vn_compfeder_id
		                  )
{

   div_conteudo   = document.getElementById(vv_element_id);

   // Inicio transação
   xmlhttp      = new ajax();
   vv_retorno   = xmlhttp.enviar('index.php?modulo=TAjax&a_metodo=CompetidorFederado&a_funcao=getCompetidorFederado&compfeder_id='+vn_compfeder_id+'&federacao_id='+vn_federacao_id, "POST", false);
   div_conteudo.innerHTML = vv_retorno;

}

function getValidaEmail_TUsuCadastro()
{

   if(document.formTUsuCadastro.frm_email.value.length<1)
   {
      alert('Preencha o email corretamente !');
      return false;
   }

   xmlhttp        = new ajax();
   vn_vlr_igual   = xmlhttp.enviar('index.php?modulo=TAjax&a_metodo=ValidaEmail&a_funcao=getValidaEmail&email='+document.formTUsuCadastro.frm_email.value, "POST", false);

   if(vn_vlr_igual==1)
   {
      alert('O email informado já existe em nosso banco de dados. Gere nova senha atravé do link "Esqueci minha senha". ');
      return false;
   }
   else
   {
      return true;
   }
   
}

function getValidaEmail_TUsuEsqueceuSenha()
{

   if(document.formTUsuEsqueceuSenha.vs_email.value.length<1)
   {
      alert('Preencha o email corretamente !');
      return false;
   }

   xmlhttp        = new ajax();
   vn_vlr_igual   = xmlhttp.enviar('index.php?modulo=TAjax&a_metodo=ValidaEmail&a_funcao=getValidaEmail&email='+document.formTUsuEsqueceuSenha.vs_email.value, "POST", false);

   if(vn_vlr_igual==0)
   {
      alert('O email informado não existe em nosso banco de dados. Cadastre-se pelo link "Quero me cadastrar". ');
      return false;
   }
   else
   {
      return true;
   }
   
}
