// Calcular Ritmo de Prova
function calcularRitmoInterface() {
	// Parâmetros de Entrada
    var hora = document.getElementById('horaR').value;
    if (hora == null || hora.length == 0) {
        hora = "00";
    } else if (hora > 23) {
        alert('Hora deve ser entre 0 e 23!');
        document.getElementById('horaR').select();
        return;
    }

    var minuto = document.getElementById('minutoR').value;
    if (minuto == null || minuto.length == 0)  {
        minuto = "00";
    } else if (minuto > 59) {
        alert('Minuto deve ser entre 0 e 59!');
        document.getElementById('minutoR').select();
        return;
    }

    var segundo = document.getElementById('segundoR').value;
    if (segundo == null || segundo.length == 0)  {
        segundo = "00";
    } else if (segundo > 59) {
        alert('Segundo deve ser entre 0 e 59!');
		document.getElementById('segundoR').select();
        return;
    }

    if (hora == 0 && minuto == 0 && segundo == 0) {
        alert('Por favor digite um Tempo!');
		document.getElementById('horaR').focus();
        return;
    }

    var distancia = document.getElementById('distanciaKMR').value;
    if (distancia == null || distancia.length == 0) {
		alert('Por favor digite uma Distância!');
        document.getElementById('distanciaKMR').focus();
        return;
    } 

    // Calculos  
    var ritmo = calcularRitmo(hora, minuto, segundo, distancia);
	var tabHungMasc = obtemPontuacao(distancia, hora, minuto, segundo, 'masc');    
	var tabHungFem = obtemPontuacao(distancia, hora, minuto, segundo, 'fem');
	var velocidade = calcularVelocidade(distancia, hora, minuto, segundo);

    // Formatar    
    if (hora < 10 && hora.length < 2) {
        hora = "0" + hora;
    }
    if (minuto < 10 && minuto.length < 2) {
        minuto = "0" + minuto;
    }
    if (segundo < 10 && segundo.length < 2) {
        segundo = "0" + segundo;
    }
    document.getElementById('horaR').value = hora;
    document.getElementById('minutoR').value = minuto;
    document.getElementById('segundoR').value = segundo;
 
    // Atualizar os Campos
    alert("Ritmo " + ritmo + " min/km \n" + "Velocidade: " + velocidade + "km/h \n\n" + "Tabela Húngara: \n" + "Masc.: " + tabHungMasc + "\n" + "Fem.: " + tabHungFem);
    document.getElementById('pontuacaoMasR').value = tabHungMasc;
    document.getElementById('pontuacaoFemR').value = tabHungFem;
	
    document.getElementById('horaR').focus();
}

// Calcular Tempo Total de Prova
function calcularTempoInterface() {
	// Parâmetros de Entrada
    var distancia = document.getElementById('distanciaKMT').value;
    if (distancia == null || distancia.length == 0) {
        alert('Por favor digite uma Distância!');
        document.getElementById('distanciaKMT').focus();
        return;
    }

    var ritmo = document.getElementById('ritmoT').value;
    if (ritmo == null || ritmo.length == 0) {
        alert('Por favor digite uma Ritmo!');
        document.getElementById('ritmoT').focus();
        return;
    }

    var separador = ritmo.indexOf(':');
    if (ritmo.indexOf(':', separador+1) > -1) {
        alert('Ritmo deve estar no formato m:ss (ex: 5:10)!');
        document.getElementById('ritmoT').focus();
        return;
    }

    var minuto = ritmo.substring(0, separador);
    if (minuto == null || minuto.length == 0) {
        alert('Ritmo deve estar no formato m:ss (ex: 5:10)!');
        document.getElementById('ritmoT').focus();
        return;
    }

    var segundo = ritmo.substring(separador+1);
    if (segundo == null || segundo.length < 2) {
        alert('Ritmo deve estar no formato m:ss (ex: 5:10)!');
        document.getElementById('ritmoT').focus();
        return;
    }
    
    // Calculos
	calcularTempo(distancia, minuto, segundo);
   	var tabHungMasc = obtemPontuacao(distancia, _hora, _minuto, _segundo, 'masc');    
	var tabHungFem = obtemPontuacao(distancia, _hora, _minuto, _segundo, 'fem');
	var velocidade = calcularVelocidade(distancia, _hora, _minuto, _segundo);

    // Atualizar os Campos
    document.getElementById('horaT').value = _hora;
    document.getElementById('minutoT').value = _minuto;
    document.getElementById('segundoT').value = _segundo;
	document.getElementById('velocidadeT').value = velocidade;
   	document.getElementById('pontuacaoMasT').value = tabHungMasc;
   	document.getElementById('pontuacaoFemT').value = tabHungFem;
	
    document.getElementById('distanciaKMT').focus();
}

// Calcular Pontuação para um determinado Tempo
function calcularTempoPontuacaoInterface() {
	// Parâmetros de Entrada
    var pontos = document.getElementById('pontuacaoH').value;
    if (pontos == null || pontos.length == 0 || pontos < 1 || pontos > 1400 || pontos == 'N/A') {
        alert('Por favor digite uma Pontuação entre 1 e 1400!');
		document.getElementById('pontuacaoH').value = '';
        document.getElementById('pontuacaoH').focus();
        return;
	}    

    var distancia = document.getElementById('distanciaKMH').value;
    if (distancia == null || distancia.length == 0) {
        alert('Por favor digite uma Distância em Km!');
        document.getElementById('distanciaKMH').focus();
        return;
	}
    
    var sexo = '';
    if (document.getElementById('masc').checked == true) {
	    sexo = 'masc';
    } else {
	 	sexo = 'fem';   
    } 
    
    // Calculos
    calcularTempoPontuacao(pontos, distancia, sexo);
    var ritmo = calcularRitmo(_hora, _minuto, _segundo, distancia);
    
    // Atualizar os Campos
    document.getElementById('horaH').value = _hora;
    document.getElementById('minutoH').value = _minuto;
    document.getElementById('segundoH').value = _segundo;
    document.getElementById('ritmoH').value = ritmo;
    
    document.getElementById('pontuacaoH').focus();
}

// Calcular Previsão de Tempo Total de Prova
function calcularPrevisaoInterface() {
	// Parâmetros de Entrada
    var hora = document.getElementById('horaRealP').value;
    if (hora == null || hora.length == 0) {
        hora = "00";
    } else if (hora > 23) {
        alert('Hora deve ser entre 0 e 23!');
        document.getElementById('horaRealP').select();
        return;
    }

    var minuto = document.getElementById('minutoRealP').value;
    if (minuto == null || minuto.length == 0)  {
        minuto = "00";
    } else if (minuto > 59) {
        alert('Minuto deve ser entre 0 e 59!');
        document.getElementById('minutoRealP').select();
        return;
    }

    var segundo = document.getElementById('segundoRealP').value;
    if (segundo == null || segundo.length == 0)  {
        segundo = "00";
    } else if (segundo > 59) {
        alert('Segundo deve ser entre 0 e 59!');
		document.getElementById('segundoRealP').select();
        return;
    }

    if (hora == 0 && minuto == 0 && segundo == 0) {
        alert('Por favor digite um Tempo!');
		document.getElementById('horaRealP').focus();
        return;
    }
    
    var distanciaReal = document.getElementById('distanciaRealP').value;
    if (distanciaReal == null || distanciaReal.length == 0) {
        alert('Por favor digite a Distância Real!');
        document.getElementById('distanciaRealP').focus();
        return;
    } 
    
    var distanciaPrevisao = document.getElementById('distanciaPrevisaoP').value;
    if (distanciaPrevisao == null || distanciaPrevisao.length == 0) {
        alert('Por favor digite a Distância Prevista!');
        document.getElementById('distanciaPrevisaoP').focus();
        return;
    } 
    
    // Calculos
    calcularPrevisao(hora, minuto, segundo, distanciaReal, distanciaPrevisao);
    
    // Formatar
    if (hora < 10 && hora.length < 2) {
        hora = "0" + hora;
    }
    if (minuto < 10 && minuto.length < 2) {
        minuto = "0" + minuto;
    }
    if (segundo < 10 && segundo.length < 2) {
        segundo = "0" + segundo;
    }
    document.getElementById('horaRealP').value = hora;
    document.getElementById('minutoRealP').value = minuto;
    document.getElementById('segundoRealP').value = segundo;
    
    // Atualizar os Campos
    document.getElementById('horaPrevisaoP').value = _hora;
    document.getElementById('minutoPrevisaoP').value = _minuto;
    document.getElementById('segundoPrevisaoP').value = _segundo;
    document.getElementById('ritmoPrevisaoP').value = _ritmo;
    
    document.getElementById('horaRealP').focus();
}

// Calcular IMC
function calcularIMCInterface() {
	// Parâmetros de Entrada
    var alturaM = document.getElementById('alturaM').value;
    if (alturaM == null || alturaM.length == 0) {
        alert('Por favor digite sua Altura(m)!');
        document.getElementById('alturaM').focus();
        return;
	}

    var alturaCm = document.getElementById('alturaCm').value;
    if (alturaCm == null || alturaCm.length == 0) {
        alert('Por favor digite sua Altura(cm)!');
        document.getElementById('alturaCm').focus();
        return;
	}
	
	var peso = document.getElementById('peso').value;
    if (peso == null || peso.length == 0) {
        alert('Por favor digite seu Peso!');
        document.getElementById('peso').focus();
        return;
	}    
	
	// Calculos
	var altura = alturaM + '.' + alturaCm;
	calcularIMC(peso, altura);
	
    // Atualizar os Campos
	alert("IMC: " + _imc + "\n" + "Situação: " + _situacao);

	document.getElementById('peso').focus();
}

// Calcular VO2 Estimado
function calcularVO2Interface() {
	// Parâmetros de Entrada
    var hora = document.getElementById('horaV').value;
    if (hora == null || hora.length == 0) {
        hora = "00";
    } else if (hora > 23) {
        alert('Hora deve ser entre 0 e 23!');
        document.getElementById('horaV').select();
        return;
    }

    var minuto = document.getElementById('minutoV').value;
    if (minuto == null || minuto.length == 0)  {
        minuto = "00";
    } else if (minuto > 59) {
        alert('Minuto deve ser entre 0 e 59!');
        document.getElementById('minutoV').select();
        return;
    }

    var segundo = document.getElementById('segundoV').value;
    if (segundo == null || segundo.length == 0)  {
        segundo = "00";
    } else if (segundo > 59) {
        alert('Segundo deve ser entre 0 e 59!');
		document.getElementById('segundoV').select();
        return;
    }
    
    if (hora == 0 && minuto == 0 && segundo == 0) {
        alert('Por favor digite um Tempo!');
		document.getElementById('horaV').focus();
        return;
    }
	
	// Calculos
	var vo2 = calcularVO2(hora, minuto, segundo);
	
    // Formatar    
    if (hora < 10 && hora.length < 2) {
        hora = "0" + hora;
    }
    if (minuto < 10 && minuto.length < 2) {
        minuto = "0" + minuto;
    }
    if (segundo < 10 && segundo.length < 2) {
        segundo = "0" + segundo;
    }
    document.getElementById('horaV').value = hora;
    document.getElementById('minutoV').value = minuto;
    document.getElementById('segundoV').value = segundo;
	
	
    // Atualizar os Campos
	document.getElementById('vo2').value = vo2;

	document.getElementById('horaV').focus();
}

// Limpar Campos de Ritmo
function limparCamposRitmo(todos) {
	if (todos) {
		document.getElementById('horaR').value = '';
		document.getElementById('minutoR').value = '';
		document.getElementById('segundoR').value = '';
		document.getElementById('distanciaKMR').value = '';
		document.getElementById('distanciaKMR').focus();
	}
	document.getElementById('ritmoR').value = '';
	document.getElementById('velocidadeR').value = '';
	document.getElementById('pontuacaoMasR').value = '';
	document.getElementById('pontuacaoFemR').value = '';
}

// Limpar Campos Tempo Total de Prova
function limparCamposTempoTotal(todos) { 
	if (todos) {
		document.getElementById('distanciaKMT').value = '';
		document.getElementById('distanciaKMT').focus();
	    document.getElementById("unidadeVelocidadeT").innerHTML = '&nbsp;&nbsp;km/h';
    	document.getElementById("unidadeT").innerHTML = '&nbsp;&nbsp;min/km&nbsp;';
		document.getElementById('ritmoT').value = '';
	}
	document.getElementById('velocidadeT').value = '';
	document.getElementById('horaT').value = '';
	document.getElementById('minutoT').value = '';
	document.getElementById('segundoT').value = '';
	document.getElementById('pontuacaoMasT').value = '';
	document.getElementById('pontuacaoFemT').value = '';
}

// Limpar Campos para Tabela Hungara
function limparCamposTabelaHungara(todos) {
	if (todos) {
		document.getElementById('pontuacaoH').value = '';
		document.getElementById('distanciaKMH').value = '';
		document.getElementById('pontuacaoH').focus();
	}
	document.getElementById('horaH').value = '';
	document.getElementById('minutoH').value = '';
	document.getElementById('segundoH').value = '';
	document.getElementById('ritmoH').value = '';
}

// Limpar Campos para Previsão de Tempo Total de Prova
function limparCamposPrevisao(todos) {
	if (todos) {
		document.getElementById('horaRealP').value = '';
		document.getElementById('minutoRealP').value = '';
		document.getElementById('segundoRealP').value = '';
		document.getElementById('distanciaRealP').value = '';
		document.getElementById('distanciaPrevisaoP').value = '';
		document.getElementById('horaRealP').focus();
	}
	document.getElementById('horaPrevisaoP').value = '';
	document.getElementById('minutoPrevisaoP').value = '';
	document.getElementById('segundoPrevisaoP').value = '';
	document.getElementById('ritmoPrevisaoP').value = '';
}

// Limpar Campos IMC
function limparCamposIMC(todos) {
	if (todos) {
		document.getElementById('peso').value = '';
		document.getElementById('alturaM').value = '';
		document.getElementById('alturaCm').value = '';
	}
	document.getElementById('imc').value = '';
	document.getElementById('situacao').value = '';
}

// Limpar Campos VO2
function limparCamposVO2(todos) {
	if (todos) {
		document.getElementById('horaV').value = '';
		document.getElementById('minutoV').value = '';
		document.getElementById('segundoV').value = '';
	}
	document.getElementById('vo2').value = '';
}

// Parâmetros de Entrada:
// 1. evt
// 2. separador
// 3. ponto
// 4. input
// 5. funcao
//
// Parâmetros de Saída:
// Retorna se é ou não um valor válido para o tipo de campo
//
function isNumberKey(evt, separador, ponto, input, funcao) {
    var charCode = (evt.which) ? evt.which : event.keyCode;
    var limite = 57;
    if (separador == true) {
        limite++;
    }
    
	var valor = document.getElementById(input).value;
    var umPonto = valor.indexOf('.');
    if (charCode == 46 && (ponto != true || umPonto != -1)) {
	 	return false;   
    }
    if (charCode > 31 && charCode != 46 && (charCode < 48 || charCode > limite)) {
        return false;
    }
    
    if (funcao == 'ritmo') {
		limparCamposRitmo(false);
	} else if (funcao == 'tempo') {
		limparCamposTempoTotal(false);
	} else if (funcao == 'hungara') {
		limparCamposTabelaHungara(false);
	} else if (funcao == 'previsao') {
		limparCamposPrevisao(false);
	} else if (funcao == 'imc') {
		limparCamposIMC(false);	
	} else if (funcao == 'vo2') {
		limparCamposVO2(false);	
	}
    
    return true;
}

// JQuery
$(document).ready(function() {
	$("#content > div").hide();
	$("#content > div:eq(0)").show();
	$("#tabs > a:eq(0)").css("background", "url(../imagens/tab-selected.jpg) top left no-repeat");
	
	$("#ritmoT").setMask({mask:'9:99'});
});
 
function opentab(num) {
	$("#content > div").hide();
	$("#content > div:eq(" + (num-1) + ")").fadeIn();
	$("#tabs > a").css("background", "url(../imagens/tab.jpg) top left no-repeat");
	$("#tabs > a:eq(" + (num-1) + ")").css("background", "url(../imagens/tab-selected.jpg) top left no-repeat");	
}

