// JavaScript Document
//*********** XML HTTP OBJECT
function getHTTPObject()
{
	var xmlhttp;
	// Internet Explorer
	
	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(oc)
		{
			xmlhttp = null;
		}
	}
	// Mozilla/Safari
	if (!xmlhttp && typeof xmlhttp != "undefined")
	{
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}
//Pega valor do elemento
function pegaValor(idElemento)
{
	if(document.getElementById(idElemento) == null)
	{
		var valor = document.getElementsByName(idElemento)[0].value
	}
	else
	{
		var valor = document.getElementById(idElemento).value;
	}
	return valor;
}
//Abre sub Menus
var atualMenu = 'home';
function abreSub(menu){
	if(atualMenu!='home'){
		document.getElementById(atualMenu).style.display = 'none';
	}
	if(menu=='home' && atualMenu!='home'){
		document.getElementById(atualMenu).style.display = 'none';
		atualMenu = menu;
	}
	else
	{
		document.getElementById(menu+'Sub').style.display = 'block';	
		atualMenu = menu+'Sub';
	}
	//alert(atualMenu);
}
//*********** FUNÇÕES DE INTERFACE
//Abre links na div
function abreLink(pagina, destino)
{

	var obj = getHTTPObject();
	var url = pagina;
	document.getElementById(destino).innerHTML ='<p class="carregando"><img src="imgs/carregando.gif">&nbsp;&nbsp;&nbsp;Carregando conteúdo.. Aguarde!</p>';
	obj.open("GET", url, true);
	obj.onreadystatechange = function(){
		if(obj.readyState == 4){
			document.getElementById(destino).innerHTML = obj.responseText;
		}
	}
	obj.send("");
}
/** 
* @desc  Função de chamada para listagem de atletas 
* @return Retorna a resposta do script em elemento html definido na função
*/
function listaAtleta(tipo)
{
	var ini = pegaValor('ini');
	var fim = pegaValor('fim');
	if(ini > fim)
	{
		alert('Range de caracteres invalido!');
	}
	else
	{
		var obj = getHTTPObject();
		if(tipo=='adm')
		{
			var url = 'gerAtleta.php?acao=listar&ini='+ini+'&fim='+fim;
		}
		else
		{
			var url = 'Atletas.php?acao=listar&ini='+ini+'&fim='+fim;	
		}
		var destino = 'atletaLista';
		document.getElementById(destino).innerHTML='<p style="text-align:center;">Carregando dados.. Aguarde!</p>';
		obj.open("GET",url,true);
		obj.onreadystatechange = function()
		{
			if(obj.readyState==4)
			{
				document.getElementById(destino).innerHTML = obj.responseText;
			}
		}
		obj.send("");
	}
}
/**
* Envia o formulário de contato
*/
function enviaContato()
{
	var email = pegaValor('emailContato');
	var nome = pegaValor('nomeContato');
	var mensagem = pegaValor('mensagemContato');
	var parametros = 'nome='+escape(nome)+'&email='+email+'&mensagem='+escape(mensagem);
	var url = 'contato.php?acao=enviar';
	var destino = 'conteudoPrincipal';
	var obj = getHTTPObject();
	
	document.getElementById(destino).innerHTML='<p style="text-align:center;">Enviando dados.. Aguarde!</p>';
	obj.open("POST",url,true);
	obj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	obj.onreadystatechange = function()
	{
		if(obj.readyState==4)
		{
			document.getElementById(destino).innerHTML = obj.responseText;
		}
	}
	obj.send(parametros);
}
/************************************************************************
  			VERIFICA QUAL RADIO BUTTON QUE ESTA MARCADO
************************************************************************/

function pegaRadioChecked(vetorRadio)
{
	for (var i=0;i<vetorRadio.length;i++)
	{
		if (vetorRadio[i].checked == true)
		{
			return vetorRadio[i].value;
		}
	}
}
/************************************************************************
		FAZ A VALIDACAO DA DATA (dateString FORMATO = ANO,MES,DIA) 
************************************************************************/

function isDate(dateString, separador) 
{
    dateArray = dateString.split(separador); 			// separada data pelo separador
    
	if (dateArray.length < 3) 
	{
		return false;   
	}
	else 
	{
		if (isNaN(dateArray[0]) || dateArray[0] < 1900 || dateArray[0] > 9999)
		{			
			return false;
		}      
		else 
		{
			if (isNaN(dateArray[1]) || dateArray[1] < 1 || dateArray[1] > 12)
			{				
				return false;
			}
			else 
			{
				if (isNaN(dateArray[2]) || dateArray[2] < 1 || dateArray[2] > daysInMonth(dateArray[1], dateArray[0]))
				{					
					return false;
				}
				else
				{
					return true;
				}
			}
		}
	}
}
/************************************************************************
		RETORNA O NUMERO DE DIAS DE UM MÊS DE ACORDO COM O ANO
************************************************************************/
   
function daysInMonth(month, year) 
{
	// meses com 31 dias
	if (month=='1' || month=='01' || month=='3' || month=='03' || month=='5' || month=='05' || month=='7' || month =='07' || month =='8' || month =='08' ||month =='10'|| month =='12' )
	{
		return 31;
	}
	else 
	{	// meses com 30 dias
		if (month =='4' || month =='04' || month =='6' || month =='06' || month =='9' || month =='09' || month =='11')
		{
			return 30;
		}
		else  
		{	// mes de 28 ou 29 dias (fevereiro)
			if(year % 4 == 0)
			{
				return 29;
			}
			else 
			{
				return 28;
			}
		} // fim else
	}  // fim else
}
