function toggle(id)
{
	if (document.getElementById(id))
	{
		if (document.getElementById(id).style.display == 'none')
			document.getElementById(id).style.display = '';
		else
			document.getElementById(id).style.display = 'none';
	}
}

function navFoto (el, acao) {
	var curr = document.getElementById ('curr_foto');

	curr.value = parseInt (curr.value) + (acao == 'prev' ? -1 : 1);

	var _url = document.getElementById ('foto_' + (curr.value));

	if (_url) {
		el.href = _url.value;
		Trainer.Galeria.trocaImagem (el);
	} else {
		curr.value = parseInt (curr.value) + (acao == 'prev' ? 1 : -1);
	}

	return false;
}

function showFoto (id) {
	var _url = document.getElementById ('foto_' + id);

	if (_url)

	return false;
}

/**
 * Abre uma popup p/ freq. cardiaca
 * @date: 05/05/2008
 *
 * @param Integer atlet_id
 * @return False
 */
function abrePopUpFC(atlet_id) {
	if (window.open)
		window.open(_HTTP_PATH_+'adm_atletas_freq_cardiaca.php?' + atlet_id + '&view', "freq_card", 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=720,height=260, left=100, top=100');
	else
		alert("Não foi possível abrir a janela de Freq. Cardiaca, desabilite o bloqueio de pop-up");
	return false;
}
/**
 * Abre uma popup p/ impressão do treino
 * @date: 27/06/2008
 *
 * @param Integer atlet_id
 * @return False
 */
function abrePopUpTreino() {
	if (window.open){
		var semanaTreino = "semana_treino="+document.formTreinoData.semana_treino.value;
		var anoTreino = "&ano_treino="+document.formTreinoData.ano_treino.value;
		var mesTreino = "&mes_treino="+document.formTreinoData.mes_treino.value;

		window.open(_HTTP_PATH_+'meus-treinos/treinos-impressao?' + semanaTreino + anoTreino + mesTreino, "treinos_impressao", 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=720,height=260, left=100, top=100');
	}
	else
		alert("Não foi possível abrir a janela de Freq. Cardiaca, desabilite o bloqueio de pop-up");
	return false;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function validaemail(email) {
  var objRegExp  = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/i;
  return objRegExp.test(email);
}

function envia(param) {
  if (param == 1) {
    if (document.f1.nome.value.length < 3) {
      alert("Preencha o campo NOME!");
      document.f1.nome.focus();
    }
    else {
      if (!validaemail(document.f1.emai.value)) {
        alert("Preencha o campo E-MAIL!");
        document.f1.emai.focus();
      } else {
       	if (document.f1.mensagem.value.length == 0) {
       		alert("Preencha MENSAGEM!");
       		document.f1.mensagem.focus();
       	} else
        	document.f1.submit();
      }
    }
    return false;
  }
  else if (param == 2) {
    if (!validaemail(document.f2.emai.value)) {
      alert("Preencha o campo E-MAIL!");
      document.f2.emai.focus();
    }
    else {
      document.f2.submit();
    }
    return false;
  }
  else if ( param == 3 ) {
    if (document.f1.nome.value.length < 3) {
        alert("Preencha o campo CONTATO!");
        document.f1.nome.focus();
    }
    else if (!validaemail(document.f1.emai.value)) {
        alert("Preencha o campo E-MAIL!");
        document.f1.emai.focus();
    }
    else if ( ! validaCPF( document.f1.cpf.value ) )
    {
        alert("Preencha corretamente o campo CPF!");
        document.f1.cpf.focus();
    }
    else if ( ! validaCNPJ( document.f1.cnpj.value ) )
    {
        alert("Preencha corretamente o campo CNPJ!");
        document.f1.cnpj.focus();
    }
    else if ( document.f1.mensagem.value.length < 3 ){
        alert("Preencha o campo MENSAGEM!");
        document.f1.mensagem.focus();
    } else {
        document.f1.submit();
    }
    return false;
  }
}


function validaCPF( cpf )
{
    // Tira os pontos e o traço
    exp = /[^0-9]/g;
    cpf = cpf.replace( exp, '' );

    // Verifica se o campo é nulo
    if (cpf == '')
        return false;

    // Aqui começa a checagem do CPF
    var posicao, i, soma, dv, dv_informado;
    var digito = new Array(10);
    dv_informado = cpf.substr(9, 2); // Retira os dois últimos dígitos do número informado

    // Desemembra o número do CPF na array DIGITO
    for (i=0; i<=8; i++)
        digito[i] = cpf.substr( i, 1);

    // Calcula o valor do 10º dígito da verificação
    posicao = 10;
    soma = 0;
    for (i=0; i<=8; i++)
    {
        soma    = soma + digito[i] * posicao;
        posicao = posicao - 1;
    }
    digito[9] = soma % 11;
    if (digito[9] < 2)
        digito[9] = 0;
    else
        digito[9] = 11 - digito[9];

    // Calcula o valor do 11º dígito da verificação
    posicao = 11;
    soma = 0;
    for (i=0; i<=9; i++)
    {
        soma    = soma + digito[i] * posicao;
        posicao = posicao - 1;
    }
    digito[10] = soma % 11;
    if (digito[10] < 2)
        digito[10] = 0;
    else
        digito[10] = 11 - digito[10];

    // Verifica se os valores dos dígitos verificadores conferem
    dv = digito[9] * 10 + digito[10];

    if (dv != dv_informado)
        return false;
    else
        return true;
}

function validaCNPJ( cnpj ) {

    // Tira os pontos e o traço
    exp = /[^0-9]/g;
    cnpj = cnpj.replace( exp, '' );

    if (cnpj.length < 14)
        return false;

    var a = [];
    var b = new Number;
    var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];

    for (i=0; i<12; i++){
        a[i] = cnpj.charAt(i);
        b += a[i] * c[i+1];
    }

    if ((x = b % 11) < 2)
        a[12] = 0;
    else
        a[12] = 11-x;

    b = 0;
    for (y=0; y<13; y++)
        b += (a[y] * c[y]);

    if ((x = b % 11) < 2)
        a[13] = 0;
    else
        a[13] = 11-x;

    if ((cnpj.charAt(12) != a[12]) || (cnpj.charAt(13) != a[13]))
        return false;

    return true;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function MM_showHideLayers2() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers2.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function MM_showHideLayers3() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers3.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function MM_showHideLayers4() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers4.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}
function abre_popup(foto) {
  window.open('foto.htm?'+foto,'imagem','width=100, height=100, status=no');
}
function tour() {
  window.open('tour.htm','tour','width=650, height=350, status=no');
}

var res = screen.height;
	if (res <= 600)
	{
		document.write('<style type="text/css">#geral{min-height:86%;_height:86%;}<\/style>')
	}
	else if ( res == 768 )
	{
		document.write('<style type="text/css">#geral{min-height:85%;_height:85%;}<\/style>')
	}
	else if ( res == 864 )
	{
		document.write('<style type="text/css">#geral{min-height:88%;_height:88%;}<\/style>')
	}
	else if ( res == 1024 )
	{
		document.write('<style type="text/css">#geral{min-height:91%;_height:91%;}<\/style>')
	}
	else if ( res == 1200 )
	{
		document.write('<style type="text/css">#geral{min-height:93%;_height:93%;}<\/style>')
	}
	else
	{
		document.write('<style type="text/css">#geral{min-height:95%;_height:95%;}<\/style>')
	}


/*
    COMO USAR
    EX: onkeypress="return Mascaras ( event, 'FONE', 'nome_id' );"
*/
function Mascaras ( e, formato, objeto )
{
    var key;

    if ( window.event ) keyPressed = window.event.keyCode; // IE hack
    else keyPressed = e.which; // <B>standard method</B>

	var campo = document.getElementById(objeto);
    if ( keyPressed >= 48 && keyPressed <= 57 )
    {
        /*
            DATA
            FORMATO: 00/00/0000
        */
        if ( formato == 'DATA' )
        {
            separador = '/';
            conjunto1 = 2;
            conjunto2 = 5;
            if ( campo.value.length == conjunto1 )
                campo.value = campo.value + separador;

            if ( campo.value.length == conjunto2 )
                campo.value = campo.value + separador;

            return true;
        }

        /*
            HORA
            FORMATO: 00:00
        */
        if (formato == 'HORA')
        {
            separador = ':';
            conjunto1 = 2;
            if ( campo.value.length == conjunto1 )
                campo.value = campo.value + separador;

            return true;
        }

        /*
            CEP
            FORMATO: 00000-000
        */
        if (formato == 'CEP')
        {
            separador = '-';
            conjunto1 = 5;
            if (    campo.value.length == conjunto1 )
                campo.value = campo.value + separador;

            return true;
        }

        /*
            CPF
            FORMATO: 000.000.000-00
        */
        if (formato == 'CPF')
        {
            separador = '.';
            separador1 = '-';
            conjunto1 = 3;
            conjunto2 = 7;
            conjunto3 = 11;
            if ( campo.value.length == conjunto1 )
                campo.value = campo.value + separador;

            if ( campo.value.length == conjunto2 )
                campo.value = campo.value + separador;

            if ( campo.value.length == conjunto3 )
                campo.value = campo.value + separador1;

            return true;
        }

        /*
            CNPJ
            FORMATO: 00.000.000/0000-00
        */
        if (formato == 'CNPJ')
        {
            separador1 = '.';
            separador2 = '/';
            separador3 = '-';
            conjunto1 = 2;
            conjunto2 = 6;
            conjunto3 = 10;
            conjunto4 = 15;
            if ( campo.value.length == conjunto1 )
                campo.value = campo.value + separador1;

            if ( campo.value.length == conjunto2 )
                campo.value = campo.value + separador1;

            if ( campo.value.length == conjunto3 )
                campo.value = campo.value + separador2;

            if ( campo.value.length == conjunto4 )
                campo.value = campo.value + separador3;

            return true;
        }

        /*
            FONE
            FORMATO: (00) 0000-0000
        */
        if ( formato == 'FONE' )
        {
            separador1 = '(';
            separador2 = ') ';
            separador3 = '-';
            conjunto1 = 0;
            conjunto2 = 3;
            conjunto3 = 9;
            if ( campo.value.length == conjunto1 )
                campo.value = campo.value + separador1;

            if ( campo.value.length == conjunto2 )
                campo.value = campo.value + separador2;

            if ( campo.value.length == conjunto3 )
                campo.value = campo.value + separador3;

            return true;
        }

        /*
            FONE2
            FORMATO: 0000-0000
        */
        if ( formato == 'FONESIMPLES' )
        {
            separador1 = '-';
            conjunto1 = 4;
            if ( campo.value.length == conjunto1 )
                campo.value = campo.value + separador1;

            return true;
        }
    }
    else if ( keyPressed == 8 || keyPressed == 13 || keyPressed == 0 )
        return true;
	else if ( keyPressed == 44 && campo.value.replace (/\d/g, '') != ',' )
		return true;
	else
        return false;
}

function calculaRitmo () {
	var tempo = document.getElementById ('tempo').value;
	var distancia = parseFloat (document.getElementById ('distancia').value.replace (',','.'));
	if (tempo.replace (/_/g,'').replace (/:/g,'').length == 6 && distancia > 0) {
		tempo = tempo.replace (/_/g, '').split (':');
		hora = parseFloat (tempo[0]);
		min  = parseFloat (tempo[1]);
		sec  = parseFloat (tempo[2]);
		min += hora * 60 + sec / 60;

		var ritmo = min / distancia;
		var _mins = Math.floor(ritmo);
		var _secs = Math.floor ((ritmo - _mins) * 60);
		if (_secs < 10) _secs = '0' + _secs;
		ritmo = _mins + ':' + _secs;
		var velocidade = number_format (min == 0 ? 0 : (distancia / (min / 60)), 2, ',', '.');

		document.getElementById ('ritmo_valor').innerHTML = ritmo;
		document.getElementById ('velocidade_valor').innerHTML = velocidade;
	} else {
		document.getElementById ('ritmo_valor').innerHTML = '00:00';
		document.getElementById ('velocidade_valor').innerHTML = '0,00';
	}
	return false;
}

/*
    COMO USAR
    EX: onkeypress="return TextoSimples ( event );"
*/
function TextoSimples ( e )
{
    if ( window.event ) keyPressed = window.event.keyCode; // IE hack
    else keyPressed = e.which; // <B>standard method</B>

    if ( keyPressed == 8 || keyPressed == 95  || keyPressed == 13  || keyPressed == 0 ) return true;
    else if ( ( keyPressed >= 48 && keyPressed <= 57 ) || (keyPressed > 96 && keyPressed < 123) || keyPressed == 95 ) return true;
    else if ( ( keyPressed > 191 && keyPressed < 221 ) || ( keyPressed > 223 && keyPressed < 253 ) ) return true;

    return false;
}

/*
    COMO USAR
    EX: onkeypress="return Numeros ( event );"
*/
function Numeros ( e )
{
    if ( window.event ) keyPressed = window.event.keyCode; // IE hack
    else keyPressed = e.which; // <B>standard method</B>

    if ( keyPressed == 8 || keyPressed == 13 || keyPressed == 0 ) return true;
    else if ( keyPressed >= 48 && keyPressed <= 57 ) return true;

    return false;
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function strpos( haystack, needle, offset){
    // http://kevin.vanzonneveld.net
    // *     example 1: strpos('Kevin van Zonneveld', 'e', 5);
    // *     returns 1: 14

    var i = haystack.indexOf( needle, offset ); // returns -1
    return i >= 0 ? i : false;
}

function number_format( number, decimals, dec_point, thousands_sep ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://crestidg.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +     bugfix by: Howard Yeend
    // *     example 1: number_format(1234.5678, 2, '.', '');
    // *     returns 1: 1234.57

    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "." : dec_point;
    var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;

    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}

function visualizarDica(enderecoNoticia)
{
	window.location.href=enderecoNoticia;
}