function valida_boleto_interclubes(){
	entidade = document.getElementById('entidade_filiada').value;
	if(entidade == '0'){
		alert('Escolha uma entidade.');
		return false;
	}
	return true;
}

function limpa_tudo(){
	num_titulares = document.getElementById('num_titulares').value;
	num_reservas = document.getElementById('num_reservas').value;	
	for(k=1;k<=num_titulares;k++){
		limpa_linha('titular', k);
	}
	for(k=1;k<=num_reservas;k++){
		limpa_linha('reserva', k);
	}
}

function limpa_linha(tipo, linha){
	if(tipo == 'titular'){
		document.getElementById('jogador'+linha).value = '';
		document.getElementById('idfpx'+linha).value = '';
		document.getElementById('nasc_jogador'+linha).value = '';
	}else{
		document.getElementById('reserva'+linha).value = '';
		document.getElementById('idfpx_reserva'+linha).value = '';
		document.getElementById('nasc_reserva'+linha).value = '';
	}
}

function strzero(string, casas){
	tamanho_string = string.length;
	numero_zeros = casas - tamanho_string;
	c_zeros = '';

	if(numero_zeros > 0){
		for(k=0;k<numero_zeros;k++){
			c_zeros = c_zeros + '0';
		}
	}
	return c_zeros+string;
}

function valida_cpf(s){
	var i;
	var c = s.substr(0,9);
	var dv = s.substr(9,2);
	var d1 = 0;
	for (i = 0; i < 9; i++){
		d1 += c.charAt(i)*(10-i);
	}
	if (d1 == 0){
		return false;
	}
	d1 = 11 - (d1 % 11);
	if (d1 > 9) {
		d1 = 0;
	}
	if (dv.charAt(0) != d1){
		return false;
	}

	d1 *= 2;
	for (i = 0; i < 9; i++){
		d1 += c.charAt(i)*(11-i);
	}
	d1 = 11 - (d1 % 11);
	if (d1 > 9){
		d1 = 0;
	}
	if (dv.charAt(1) != d1){
		return false;
	}
	return true;
}

function valida_outros_pagamentos(){
	entidade_filiada = document.outros_pagamentos.entidade_filiada.value;	
	entidade_nao_filiada = document.outros_pagamentos.entidade_nao_filiada.value;	
	organizador = document.outros_pagamentos.organizador.value;	
	valor_pagamento = document.outros_pagamentos.valor_pagamento.value;	
	obs1 = document.outros_pagamentos.obs1.value;	

	if(entidade_filiada == '0' && entidade_nao_filiada == '0' && organizador == '0'){
		alert('Escolha ao menos uma entidade, filiada ou não-filiada, ou um organizador.');
		return false;
	}

	if(entidade_filiada != '0' && entidade_nao_filiada != '0'){
		alert('Escolha somente uma entidade, filiada ou não-filiada, ou um organizador.');
		return false;
	}

	if(entidade_filiada != '0' && organizador != '0'){
		alert('Escolha somente uma entidade, filiada ou não-filiada, ou um organizador.');
		return false;
	}
	
	if(entidade_nao_filiada != '0' && organizador != '0'){
		alert('Escolha somente uma entidade, filiada ou não-filiada, ou um organizador.');
		return false;
	}

	if(trim(valor_pagamento) == ''){
		alert('Preencha o valor do pagamento.');
		return false;
	}

	if(Number(valor_pagamento) == 0){
		alert('O valor do pagamento deve ser maior do que zero.');
		return false;
	}

	if(!isMoney(valor_pagamento)){
		alert('O valor do pagamento digitado é inválido.');
		return false;
	}

	if(trim(obs1) == ''){
		alert('Preencha a descrição do pagamento.');
		return false;
	}
	return true;
}

function isNumeric(sText){

	var ValidChars = "0123456789";
	var isNumber = true;
	var Char;

	for (i = 0; i < sText.length && isNumber == true; i++){ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1){
			isNumber = false;
		}
	}
	return isNumber;
}

function isMoney(sText){

	var ValidChars = "0123456789,";
	var isMoney = true;
	var Char;

	for (i = 0; i < sText.length && isMoney == true; i++){ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1){
			isMoney = false;
		}
	}

	num_virgulas = 0;
	for (i = 0; i < sText.length && isMoney == true; i++){ 
		Char = sText.charAt(i);
		if(Char == ","){
			num_virgulas++;
		}
		if (num_virgulas > 1){
			isMoney = false;
		}
	}

	if(isMoney){
		posicao = sText.indexOf(",");
		if(posicao != -1){ //tem vírgula
			decimais = sText.substring(posicao+1,sText.length);
			if(decimais.length != 2){
				alert('Número de decimais no valor do pagamento é inválido.');
				isMoney = false;
			}
		}
	}

	return isMoney;
}

function limpa_input(input){
	input.value = limpa_string(input.value);
}

function valida_taxa_21(){
	entidade_filiada = document.taxa_jogador21.entidade_filiada.value;	
	entidade_nao_filiada = document.taxa_jogador21.entidade_nao_filiada.value;	
	organizador = document.taxa_jogador21.organizador.value;	
	numero_jogadores = document.taxa_jogador21.numero_jogadores.value;	

	if(entidade_filiada == '0' && entidade_nao_filiada == '0' && organizador == '0'){
		alert('Escolha ao menos uma entidade, filiada ou não-filiada, ou um organizador.');
		return false;
	}

	if(entidade_filiada != '0' && entidade_nao_filiada != '0'){
		alert('Escolha somente uma entidade, filiada ou não-filiada, ou um organizador.');
		return false;
	}

	if(entidade_filiada != '0' && organizador != '0'){
		alert('Escolha somente uma entidade, filiada ou não-filiada, ou um organizador.');
		return false;
	}
	
	if(entidade_nao_filiada != '0' && organizador != '0'){
		alert('Escolha somente uma entidade, filiada ou não-filiada, ou um organizador.');
		return false;
	}

	if(trim(numero_jogadores) == ''){
		alert('Preencha o número de jogadores.');
		return false;
	}

	if(!isNumeric(numero_jogadores)){
		alert('O número de jogadores digitado é inválido.');
		return false;
	}

	if(Number(numero_jogadores) == 0){
		alert('O número de jogadores deve ser maior do que zero.');
		return false;
	}
	return true;
}

function valida_anuidade(){
	entidade = document.confirma_anuidade.lista_entidades.value;

	if(entidade == '00'){
		alert('Escolha a entidade antes de continuar.');
		return false;
	}
	return true;
}

function limpa_string(s){

	var digitos = "0123456789";
	var temp = "";
	var digito = "";

	for (var i=0; i<s.length; i++){
      digito = s.charAt(i);
      if (digitos.indexOf(digito)>=0){
		  temp = temp + digito;
	  }
    }
    return temp
}

function valida_cnpj(s){

	var i;
	s = limpa_string(s);
	var c = s.substr(0,12);
	var dv = s.substr(12,2);
	var d1 = 0;
	for (i = 0; i < 12; i++){
		d1 += c.charAt(11-i)*(2+(i % 8));
	}
	if (d1 == 0){
		return false;
	}
    d1 = 11 - (d1 % 11);
	if (d1 > 9) {
		d1 = 0;
	}
	if (dv.charAt(0) != d1){
		return false;
	}

	d1 *= 2;
	for (i = 0; i < 12; i++){
		d1 += c.charAt(11-i)*(2+((i+1) % 8));
	}

	d1 = 11 - (d1 % 11);
	if (d1 > 9) {
		d1 = 0;
	}
	if (dv.charAt(1) != d1)	{
		return false;
	}
    return true;
}

function valida_nao_filiadas(){
	nome_entidade = document.cadastro_nao_filiadas.nome_entidade.value;
	cnpj = document.cadastro_nao_filiadas.cnpj.value;
	endereco = document.cadastro_nao_filiadas.endereco.value;
	cidade = document.cadastro_nao_filiadas.cidade.value;
	estado = document.cadastro_nao_filiadas.estado.value;
	cep = document.cadastro_nao_filiadas.cep.value;

	if(trim(nome_entidade) == ''){
		alert('Preencha o nome da entidade.');
		return false;
	}
	if(trim(cnpj) == ''){
		alert('Preencha o CNPJ da entidade.');
		return false;
	}
	if(cnpj.length < 14){
		alert('O CNPJ digitado é inválido.');
		return false;
	}
	if(!valida_cnpj(cnpj)){
		alert('O CNPJ digitado é inválido.');
		return false;
	}
	if(trim(endereco) == ''){
		alert('Preencha o endereço da entidade.');
		return false;
	}
	if(trim(cidade) == ''){
		alert('Preencha a cidade da entidade.');
		return false;
	}
	if(trim(estado) == 'XX'){
		alert('Escolha o estado da entidade.');
		return false;
	}
	if(trim(cep) == ''){
		alert('Preencha o CEP da entidade.');
		return false;
	}
	cep = limpa_string(cep);
	if(cep.length < 8){
		alert('O CEP digitado é inválido.');
		return false;
	}
	return true;
}

function valida_pf_nao_filiadas(){
	nome = document.cadastro_pf_nao_filiadas.nome.value;
	cpf = document.cadastro_pf_nao_filiadas.cpf.value;
	endereco = document.cadastro_pf_nao_filiadas.endereco.value;
	cidade = document.cadastro_pf_nao_filiadas.cidade.value;
	estado = document.cadastro_pf_nao_filiadas.estado.value;
	cep = document.cadastro_pf_nao_filiadas.cep.value;

	if(trim(nome) == ''){
		alert('Preencha o nome.');
		return false;
	}
	if(trim(cpf) == ''){
		alert('Preencha o CPF.');
		return false;
	}
	if(cpf.length < 11){
		alert('O CPF digitado é inválido.');
		return false;
	}
	if(!valida_cpf(cpf)){
		alert('O CPF digitado é inválido.');
		return false;
	}
	if(trim(endereco) == ''){
		alert('Preencha o endereço.');
		return false;
	}
	if(trim(cidade) == ''){
		alert('Preencha a cidade.');
		return false;
	}
	if(trim(estado) == 'XX'){
		alert('Escolha o estado.');
		return false;
	}
	if(trim(cep) == ''){
		alert('Preencha o CEP.');
		return false;
	}
	cep = limpa_string(cep);
	if(cep.length < 8){
		alert('O CEP digitado é inválido.');
		return false;
	}
	return true;
}

function valida_filtro_data(){

	dtinicial = trim(document.getElementById('data_inicial').value);
	dtfinal   = trim(document.getElementById('data_final').value);
	if(!valida_data(dtinicial)){
		alert('Data inicial é inválida.');
		return false;
	}
	if(!valida_data(dtfinal)){
		alert('Data final é inválida.');
		return false;
	}
	if((dtinicial.length == 0 && dtfinal.length != 0) || (dtinicial.length != 0 && dtfinal.length == 0)){
		alert('Em caso de filtro de datas, as duas datas devem estar preenchidas.');
		return false;
	}

	if(dtinicial.length != 0 && dtfinal.length != 0){
		dt1 = dtinicial.substring(6,10) + dtinicial.substring(3,5) + dtinicial.substring(0,2);
		dt2 = dtfinal.substring(6,10) + dtfinal.substring(3,5) + dtfinal.substring(0,2);
		if(dt1 > dt2){		
			alert('Data final deve ser posterior à data inicial.');
			return false;
		}
	}
	return true;
}

function valida_filtro_anuidade(){

	dtinicial = trim(document.getElementById('data_inicial').value);
	dtfinal   = trim(document.getElementById('data_final').value);
	if(!valida_data(dtinicial)){
		alert('Data inicial é inválida.');
		return false;
	}
	if(!valida_data(dtfinal)){
		alert('Data final é inválida.');
		return false;
	}
	if((dtinicial.length == 0 && dtfinal.length != 0) || (dtinicial.length != 0 && dtfinal.length == 0)){
		alert('Em caso de filtro de datas, as duas datas devem estar preenchidas.');
		return false;
	}

	if(dtinicial.length != 0 && dtfinal.length != 0){
		dt1 = dtinicial.substring(6,10) + dtinicial.substring(3,5) + dtinicial.substring(0,2);
		dt2 = dtfinal.substring(6,10) + dtfinal.substring(3,5) + dtfinal.substring(0,2);
		if(dt1 > dt2){		
			alert('Data final deve ser posterior à data inicial.');
			return false;
		}
	}
	return true;
}

function valida_filtro_taxa21(){

	dtinicial = trim(document.getElementById('data_inicial').value);
	dtfinal   = trim(document.getElementById('data_final').value);
	if(!valida_data(dtinicial)){
		alert('Data inicial é inválida.');
		return false;
	}
	if(!valida_data(dtfinal)){
		alert('Data final é inválida.');
		return false;
	}
	if((dtinicial.length == 0 && dtfinal.length != 0) || (dtinicial.length != 0 && dtfinal.length == 0)){
		alert('Em caso de filtro de datas, as duas datas devem estar preenchidas.');
		return false;
	}

	if(dtinicial.length != 0 && dtfinal.length != 0){
		dt1 = dtinicial.substring(6,10) + dtinicial.substring(3,5) + dtinicial.substring(0,2);
		dt2 = dtfinal.substring(6,10) + dtfinal.substring(3,5) + dtfinal.substring(0,2);
		if(dt1 > dt2){		
			alert('Data final deve ser posterior à data inicial.');
			return false;
		}
	}
	return true;
}

function valida_filtro_outros(){

	dtinicial = trim(document.getElementById('data_inicial').value);
	dtfinal   = trim(document.getElementById('data_final').value);
	if(!valida_data(dtinicial)){
		alert('Data inicial é inválida.');
		return false;
	}
	if(!valida_data(dtfinal)){
		alert('Data final é inválida.');
		return false;
	}
	if((dtinicial.length == 0 && dtfinal.length != 0) || (dtinicial.length != 0 && dtfinal.length == 0)){
		alert('Em caso de filtro de datas, as duas datas devem estar preenchidas.');
		return false;
	}

	if(dtinicial.length != 0 && dtfinal.length != 0){
		dt1 = dtinicial.substring(6,10) + dtinicial.substring(3,5) + dtinicial.substring(0,2);
		dt2 = dtfinal.substring(6,10) + dtfinal.substring(3,5) + dtfinal.substring(0,2);
		if(dt1 > dt2){		
			alert('Data final deve ser posterior à data inicial.');
			return false;
		}
	}
	return true;
}

function trim(t){

	var ch = t.substring(0, 1);

	while (ch == " ") { // Check for spaces at the beginning of the string
		t = t.substring(1, t.length);
		ch = t.substring(0, 1);
	}
	ch = t.substring(t.length-1, t.length);

	while (ch == " ") { // Check for spaces at the end of the string
		t = t.substring(0, t.length-1);
		ch = t.substring(t.length-1, t.length);
	}
	return t;
}

function mascara_data(input){

	c_data = input.value;

	if (c_data.length == 2 || c_data.length == 5){
		c_data+='/';
	}
	input.value = c_data;
}

function valida_data(d_data){

	var chars_data = "0123456789/";

	//if (d_data.length < 8){
	//	return false;
	//}

	if(trim(d_data).length > 0){
		for(k=0;k<d_data.length;k++){
			if(chars_data.indexOf(d_data.substring(k,k+1)) < 0){
				return false;
			}
		}

		if(d_data.substring(0,2) < '01'||d_data.substring(0,2) > '31'){
			return false;
		}

		if (d_data.substring(0,2) > '30' && (d_data.substring(3,5) == '02' || d_data.substring(3,5) == '04' || d_data.substring(3,5) == '06' || d_data.substring(3,5) == '09')){
			return false;
		}

		if(d_data.substring(0,2) > '29' && d_data.substring(3,5) == '02'){
			return false;
		}

		if(d_data.substring(3,5) < '01'||d_data.substring(3,5) > '12'){
			return false;
		}
	}
	return true;
}

function toggle_level(rowid){
	for(k=0;k<=10;k++){
		if(document.getElementById(rowid + k)){
			if (document.getElementById(rowid + k).style.display == 'none'){
				document.getElementById(rowid + k).style.display = '';
				if(k == 1){
					img_old = document.getElementById('S_'+rowid);
					img_new = new Image();
					img_new.src = 'imagens/seta_down.png';
					img_old.src = img_new.src;
				}
			}else{
				document.getElementById(rowid + k).style.display = 'none';
				if(k == 1){
					img_old = document.getElementById('S_'+rowid);
					img_new = new Image();
					img_new.src = 'imagens/seta_up.png';
					img_old.src = img_new.src;
				}
			}
		}
	}
}

function go_home(){
	document.location.href='http://www.fpx.com.br/fpxsys';
}

function carregabrw(){
	window.open('', 'SHOPLINE', 'toolbar=yes,menubar=yes,resizable=yes,status=no,scrollbars=yes,width=675,height=485');
}	

function valida_torneio(){
	combo = document.confirma_torneio.lista_torneios;
	comboIndex = document.confirma_torneio.lista_torneios.selectedIndex;
	if(combo[comboIndex].value == '00'){
		alert('Escolha o torneio antes de avançar.');
		return false;
	}
	return true;
}

function valida_pagamento(){
	combo = document.confirma_pagamento.lista_pagamentos;
	comboIndex = document.confirma_pagamento.lista_pagamentos.selectedIndex;
	if(combo[comboIndex].value == '00'){
		alert('Escolha o pagamento antes de confirmar.');
		return false;
	}
	return true;
}

function valida_pagamento_acompanhante(){
	combo = document.confirma_pagamento.lista_pagamentos;
	comboIndex = document.confirma_pagamento.lista_pagamentos.selectedIndex;
	nome = document.confirma_pagamento.nome_acompanhante.value;
	if(combo[comboIndex].value == '00'){
		alert('Escolha o pagamento antes de confirmar.');
		return false;
	}
	if(trim(nome) == ''){
		alert('Preencha o nome antes de confirmar.');
		return false;
	}
	return true;
}

function linha_vazia(linha){
	reserva = document.getElementById('reserva'+linha).value;	
	idfpx_reserva = document.getElementById('idfpx_reserva'+linha).value;	
	nasc_reserva = document.getElementById('nasc_reserva'+linha).value;	
	tabuleiro_reserva = document.getElementById('tabuleiro_reserva'+linha).value;	

	if(trim(reserva) == '' && trim(idfpx_reserva) == '' && trim(nasc_reserva) == '' && tabuleiro_reserva == '0'){
		return true;
	}
	return false;
}

function linha_cheia(linha){
	reserva = document.getElementById('reserva'+linha).value;	
	idfpx_reserva = document.getElementById('idfpx_reserva'+linha).value;	
	nasc_reserva = document.getElementById('nasc_reserva'+linha).value;	
	tabuleiro_reserva = document.getElementById('tabuleiro_reserva'+linha).value;	

	if(trim(reserva) != '' && trim(idfpx_reserva) != '' && trim(nasc_reserva) != '' && tabuleiro_reserva != '0'){
		return true;
	}
	return false;
}

function linha_ok(linha){
	reserva = document.getElementById('reserva'+linha).value;	
	idfpx_reserva = document.getElementById('idfpx_reserva'+linha).value;	
	nasc_reserva = document.getElementById('nasc_reserva'+linha).value;	
	tabuleiro_reserva = document.getElementById('tabuleiro_reserva'+linha).value;	

	if(trim(reserva) != '' && trim(idfpx_reserva) != '' && trim(nasc_reserva) != '' && tabuleiro_reserva != '0' && valida_data(nasc_reserva) && isNumeric(idfpx_reserva)){
		return true;
	}
	return false;
}

function linha_incompleta(linha){
	if(!linha_vazia(linha) && !linha_cheia(linha)){
		return true;
	}
	return false;
}

function valida_pagamento_equipes21(){
	combo = document.confirma_pagamento.lista_pagamentos;
	comboIndex = document.confirma_pagamento.lista_pagamentos.selectedIndex;
	nome_equipe = document.confirma_pagamento.nome_equipe.value;
	numero_titulares = document.confirma_pagamento.num_titulares.value;	
	numero_reservas = document.confirma_pagamento.num_reservas.value;	
	total_tabuleiros = Number(numero_titulares)+Number(numero_reservas);	
	entidade_filiada = document.confirma_pagamento.entidade_filiada.value;	
	entidade_nao_filiada = document.confirma_pagamento.entidade_nao_filiada.value;	
	pessoa_fisica = document.confirma_pagamento.pf_nao_filiada.value;	

	if(combo[comboIndex].value == '00'){
		alert('Escolha o pagamento antes de confirmar.');
		return false;
	}

	if(trim(nome_equipe) == ''){
		alert('Preencha o nome da equipe antes de confirmar.');
		return false;
	}

	if(entidade_filiada == '0' && entidade_nao_filiada == '0' && pessoa_fisica == '0'){
		alert('Escolha ao menos uma entidade, filiada ou não-filiada, ou uma pessoa física.');
		return false;
	}

	var opcoes_selecionadas = 0;
	if(entidade_filiada != '0'){
		opcoes_selecionadas++;
	}
	if(entidade_nao_filiada != '0'){
		opcoes_selecionadas++;
	}
	if(pessoa_fisica != '0'){
		opcoes_selecionadas++;
	}
	if(opcoes_selecionadas > 1){
		alert('Escolha somente uma entidade, filiada ou não-filiada, ou uma pessoa física.');
		return false;
	}

	//VALIDAÇÃO TITULARES
	for(linha=1;linha<=numero_titulares;linha++){
		jogador = document.getElementById('jogador'+linha).value;	
		idfpx = document.getElementById('idfpx'+linha).value;	
		nasc_jogador = document.getElementById('nasc_jogador'+linha).value;	
		tabuleiro_jogador = document.getElementById('tabuleiro_jogador'+linha).value;	

		if(trim(jogador) == ''){
			alert('Preencha o nome do jogador '+linha+'.');
			return false;
		}
		if(trim(idfpx) == ''){
			alert('Preencha o IDFPX do jogador '+linha+'.');
			return false;
		}
		if(!isNumeric(idfpx)){
			alert('O IDFPX do jogador '+linha+' é inválido.');
			return false;
		}
		if(trim(nasc_jogador) == ''){
			alert('Preencha o a data de nascimento do jogador '+linha+'.');
			return false;
		}
		if(!valida_data(nasc_jogador)){
			alert('A data de nascimento digitada para o jogador '+linha+' é inválida.');
			return false;
		}
		if(tabuleiro_jogador == '0'){
			alert('Escolha o tabuleiro do jogador '+linha+'.');
			return false;
		}
	}

	//VALIDAÇÃO RESERVAS
	for(t=1;t<=numero_reservas;t++){
		if(t > 1){
			if(linha_ok(t)){
				for(v=1;v<=t-1;v++){
					if(linha_vazia(v)){
						c_msg = 'Existe um ou mais erros no preenchimento do quadro de reservas.\n';
						c_msg+= 'As linhas devem ser completadas de cima para baixo, uma a uma.\n';
						c_msg+= 'Não pode haver linhas preenchidas precedidas por linhas vazias.\n';
						c_msg+= 'Caso não haja jogadores reservas, deixe o quadro completamente vazio.';
						alert(c_msg);
						return false;
					}
				}
			}
		}
		if(linha_incompleta(t) || (linha_cheia(t) && !linha_ok(t))){
			alert('A linha do jogador reserva '+ t +' está incompleta ou preenchida incorretamente.');
			return false;
		}
	}	

	//VALIDAÇÃO TABULEIROS
	array_tabuleiros = new Array(total_tabuleiros);
	for(p=0;p<array_tabuleiros.length;p++){
		array_tabuleiros[p] = 0;
	}
	
	for(linha=1;linha<=numero_titulares;linha++){
		tabuleiro_jogador = document.getElementById('tabuleiro_jogador'+linha).value;	
		if(tabuleiro_jogador != 0){
			array_tabuleiros[tabuleiro_jogador-1]++;
		}
	}

	for(linha=1;linha<=numero_reservas;linha++){
		tabuleiro_reserva = document.getElementById('tabuleiro_reserva'+linha).value;	
		if(tabuleiro_reserva != 0){
			array_tabuleiros[tabuleiro_reserva-1]++;
		}
	}

	for(q=0;q<array_tabuleiros.length;q++){
		if(array_tabuleiros[q] > 1){
			alert('Não pode haver tabuleiros repetidos.');
			return false;
		}
	}

	n_maior = 0;
	n_preenchidos = 0;
	for(linha=1;linha<=numero_titulares;linha++){
		tabuleiro_jogador = document.getElementById('tabuleiro_jogador'+linha).value;	
		if(tabuleiro_jogador != 0){
			n_preenchidos++;
			if(Number(tabuleiro_jogador) > n_maior){
				n_maior = Number(tabuleiro_jogador);
			}
		}
	}

	for(linha=1;linha<=numero_reservas;linha++){
		tabuleiro_reserva = document.getElementById('tabuleiro_reserva'+linha).value;	
		if(tabuleiro_reserva != 0){
			n_preenchidos++;
			if(Number(tabuleiro_reserva) > n_maior){
				n_maior = Number(tabuleiro_reserva);
			}
		}
	}

	if(n_maior > n_preenchidos){
		c_msg = 'Há algum erro no preenchimento dos tabuleiros.\n';
		c_msg+= 'Os tabuleiros devem ser preenchidos a partir do tabuleiro 1, \n';
		c_msg+= 'e sempre em sequência, sem intervalos.';
		alert(c_msg);
		return false;
	}
	return true;
}

function valida_pagamento_equipes(){
	combo = document.confirma_pagamento.lista_pagamentos;
	comboIndex = document.confirma_pagamento.lista_pagamentos.selectedIndex;
	nome_equipe = document.confirma_pagamento.nome_equipe.value;
	numero_titulares = document.confirma_pagamento.num_titulares.value;	
	numero_reservas = document.confirma_pagamento.num_reservas.value;	
	total_tabuleiros = Number(numero_titulares)+Number(numero_reservas);	
	entidade_filiada = document.confirma_pagamento.entidade_filiada.value;	

	if(combo[comboIndex].value == '00'){
		alert('Escolha o pagamento antes de confirmar.');
		return false;
	}

	if(trim(nome_equipe) == ''){
		alert('Preencha o nome da equipe antes de confirmar.');
		return false;
	}

	if(entidade_filiada == '0'){
		alert('Escolha uma entidade.');
		return false;
	}

	//VALIDAÇÃO TITULARES
	for(linha=1;linha<=numero_titulares;linha++){
		jogador = document.getElementById('jogador'+linha).value;	
		idfpx = document.getElementById('idfpx'+linha).value;	
		nasc_jogador = document.getElementById('nasc_jogador'+linha).value;	
		tabuleiro_jogador = document.getElementById('tabuleiro_jogador'+linha).value;	

		if(trim(jogador) == ''){
			alert('Preencha o nome do jogador '+linha+'.');
			return false;
		}
		if(trim(idfpx) == ''){
			alert('Preencha o IDFPX do jogador '+linha+'.');
			return false;
		}
		if(!isNumeric(idfpx)){
			alert('O IDFPX do jogador '+linha+' é inválido.');
			return false;
		}
		if(trim(nasc_jogador) == ''){
			alert('Preencha o a data de nascimento do jogador '+linha+'.');
			return false;
		}
		if(!valida_data(nasc_jogador)){
			alert('A data de nascimento digitada para o jogador '+linha+' é inválida.');
			return false;
		}
		if(tabuleiro_jogador == '0'){
			alert('Escolha o tabuleiro do jogador '+linha+'.');
			return false;
		}
	}

	//VALIDAÇÃO RESERVAS
	for(t=1;t<=numero_reservas;t++){
		if(t > 1){
			if(linha_ok(t)){
				for(v=1;v<=t-1;v++){
					if(linha_vazia(v)){
						c_msg = 'Existe um ou mais erros no preenchimento do quadro de reservas.\n';
						c_msg+= 'As linhas devem ser completadas de cima para baixo, uma a uma.\n';
						c_msg+= 'Não pode haver linhas preenchidas precedidas por linhas vazias.\n';
						c_msg+= 'Caso não haja jogadores reservas, deixe o quadro completamente vazio.';
						alert(c_msg);
						return false;
					}
				}
			}
		}
		if(linha_incompleta(t) || (linha_cheia(t) && !linha_ok(t))){
			alert('A linha do jogador reserva '+ t +' está incompleta ou preenchida incorretamente.');
			return false;
		}
	}	

	//VALIDAÇÃO TABULEIROS
	array_tabuleiros = new Array(total_tabuleiros);
	for(p=0;p<array_tabuleiros.length;p++){
		array_tabuleiros[p] = 0;
	}
	
	for(linha=1;linha<=numero_titulares;linha++){
		tabuleiro_jogador = document.getElementById('tabuleiro_jogador'+linha).value;	
		if(tabuleiro_jogador != 0){
			array_tabuleiros[tabuleiro_jogador-1]++;
		}
	}

	for(linha=1;linha<=numero_reservas;linha++){
		tabuleiro_reserva = document.getElementById('tabuleiro_reserva'+linha).value;	
		if(tabuleiro_reserva != 0){
			array_tabuleiros[tabuleiro_reserva-1]++;
		}
	}

	for(q=0;q<array_tabuleiros.length;q++){
		if(array_tabuleiros[q] > 1){
			alert('Não pode haver tabuleiros repetidos.');
			return false;
		}
	}

	n_maior = 0;
	n_preenchidos = 0;
	for(linha=1;linha<=numero_titulares;linha++){
		tabuleiro_jogador = document.getElementById('tabuleiro_jogador'+linha).value;	
		if(tabuleiro_jogador != 0){
			n_preenchidos++;
			if(Number(tabuleiro_jogador) > n_maior){
				n_maior = Number(tabuleiro_jogador);
			}
		}
	}

	for(linha=1;linha<=numero_reservas;linha++){
		tabuleiro_reserva = document.getElementById('tabuleiro_reserva'+linha).value;	
		if(tabuleiro_reserva != 0){
			n_preenchidos++;
			if(Number(tabuleiro_reserva) > n_maior){
				n_maior = Number(tabuleiro_reserva);
			}
		}
	}

	if(n_maior > n_preenchidos){
		c_msg = 'Há algum erro no preenchimento dos tabuleiros.\n';
		c_msg+= 'Os tabuleiros devem ser preenchidos a partir do tabuleiro 1, \n';
		c_msg+= 'e sempre em sequência, sem intervalos.';
		alert(c_msg);
		return false;
	}
	return true;
}

function valida_login(){

	texto = document.form_login.input_usuario.value;

	var ch = texto.substring(0, 1);

	while (ch == " ") { // Check for spaces at the beginning of the string
		texto = texto.substring(1, texto.length);
		ch = texto.substring(0, 1);
    }
    ch = texto.substring(texto.length-1, texto.length);
   
    while (ch == " ") { // Check for spaces at the end of the string
		texto = texto.substring(0, texto.length-1);
		ch = texto.substring(texto.length-1, texto.length);
    }

	if (texto.length == 0){
		alert("Preencha o nome do usuário.");
		return false;
	}

	texto = document.form_login.input_senha.value;

	var ch = texto.substring(0, 1);

	while (ch == " ") { // Check for spaces at the beginning of the string
		texto = texto.substring(1, texto.length);
		ch = texto.substring(0, 1);
    }
    ch = texto.substring(texto.length-1, texto.length);
   
    while (ch == " ") { // Check for spaces at the end of the string
		texto = texto.substring(0, texto.length-1);
		ch = texto.substring(texto.length-1, texto.length);
    }

	if (texto.length == 0){
		alert("Preencha a senha.");
		return false;
	}

	return true;
}
