// JavaScript Document

function emailValido(mail){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if(typeof(mail) == "string"){
        if(er.test(mail)){ return true; }
    }else if(typeof(mail) == "object"){
        if(er.test(mail.value)){
                    return true;
                }
    }else{
        return false;
        }
}


function isEmpty(aTextField) // Verifica se um campo de texto se encontra vazio
{
	 if ((aTextField.value.length==0) || (aTextField.value==null)) {return true;}
	 else { return false; }
}

function AvaliaLogin(formulario)
{
	var valida=true;
	var alerta = "";

	with (formulario)
	{
		if (isEmpty(clube)) { alerta += "Por favor, coloque o ID do clube.\n";  formulario.clube.focus(); valida=false; }
		if (isEmpty(password)) { alerta += "Por favor, coloque a palavra passe.\n";  formulario.password.focus(); valida=false; }
	}
	if (alerta != "") { alert (alerta); }
	return valida;		
}	

function AvaliaSeleccaoProva(formulario)
{
	var valida=true;
	var alerta = "";

	with (formulario)
	{
		if (isEmpty(prova)) { alerta += "Por favor, seleccione uma prova.\n";  valida=false; }
	}
	if (alerta != "") { alert (alerta); }
	return valida;		
}	

function AvaliaSeleccaoAdmin(formulario)
{
	var valida=true;
	var alerta = "";

	with (formulario)
	{
		if (isEmpty(admin)) { alerta += "Por favor, seleccione uma opção.\n";  valida=false; }
	}
	if (alerta != "") { alert (alerta); }
	return valida;		
}	

function AvaliaSeleccaoEscalao(formulario)
{
	var valida=true;
	var alerta = "";

	with (formulario)
	{
		if (isEmpty(escalao)) { alerta += "Por favor, seleccione um escalão.\n";  valida=false; }
	}
	if (alerta != "") { alert (alerta); }
	return valida;		
}	
function AvaliaSeleccaoAtleta(formulario)
{
	var valida=true;
	var alerta = "";

	with (formulario)
	{
		if (isEmpty(atleta)) { alerta += "Por favor, seleccione um atleta.\n";  valida=false; }
	}
	if (alerta != "") { alert (alerta); }
	return valida;		
}

function AvaliaSeleccaoEdicaoAtleta(formulario)
{
	var valida=true;
	var alerta = "";

	with (formulario)
	{
		if (isEmpty(nome)) { alerta += "Por favor, seleccione um atleta.\n";  valida=false; }
	}
	if (alerta != "") { alert (alerta); }
	return valida;		
}

var xmlHttp

function VisualizaFichaAtleta(str)
{ 
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="procura_atleta.php"
url=url+"?atleta="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function VisualizaFichaProva(str)
{ 
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="procura_prova.php"
url=url+"?prova="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function ValidarDesconto(str)
{ 
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="aplicar_desconto.php"
url=url+"?valor="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function AdicionarPar(str)
{ 
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="adicionar_par.php"
url=url+"?atleta1="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("txtHint").innerHTML=xmlHttp.responseText 
 } 
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
function AvaliaSeleccaoNomeProva(formulario)
{
	var valida=true;
	var alerta = "";

	with (formulario)
	{
		if (isEmpty(nome)) { alerta += "Por favor, escreva o nome da prova.\n";  valida=false; }
		if (isEmpty(tipo)) { alerta += "Por favor, seleccione o tipo de prova.\n";  valida=false; }		
	}
	if (alerta != "") { alert (alerta); }
	return valida;		
}	
function AvaliaPreenchimentoCamposAtleta(formulario)
{
	var valida=true;
	var alerta = "";

	with (formulario)
	{
		if (isEmpty(id)) { alerta += "Por favor, escreva o ID da FPB.\n";  valida=false; }
		if (isEmpty(nome)) { alerta += "Por favor, escreva o nome do atleta.\n";  valida=false; }		
		if (isEmpty(clube)) { alerta += "Por favor, seleccione o clube.\n";  valida=false; }
		if (isEmpty(escalao)) { alerta += "Por favor, seleccione o tipo de escalão.\n";  valida=false; }		
		if (isEmpty(categoria_s)) { alerta += "Por favor, seleccione a categoria singulares.\n";  valida=false; }
		if (isEmpty(categoria_p)) { alerta += "Por favor, seleccione a categoria pares.\n";  valida=false; }		
		if (isEmpty(categoria_pm)) { alerta += "Por favor, selecciona a categoria pares mistos.\n";  valida=false; }
		if (isEmpty(sexo)) { alerta += "Por favor, seleccione o sexo.\n";  valida=false; }		
	}
	if (alerta != "") { alert (alerta); }
	return valida;		
}	

function VerificarPares(formulario)
{
	var valida=true;
	var alerta = "";

	with (formulario)
	{
		if (isEmpty(valida)) { alerta += "Por favor, seleccione pelo menos 1 atleta.\n";  valida=false; }
	}
	if (alerta != "") { alert (alerta); }
	return valida;			
}	


 var gotBodyFocus=0; // used in gotBFocus() function.

function escBackSpace() // called on document.body keydown event
 {
    if(event.keyCode == 8)
       {
          if  (gotBodyFocus == 1 | document.activeElement.getAttribute("type") != "text") // to avoid the backspace event except the Textbox
           {
             //event.keyCode = 0;
             event.returnValue=false
           }
           
       }    
 }

function gotBfocus() //called on document.body onfocus event
{
  gotBodyFocus = 1;
 }

function lostBfocus() // called on document.body onblur event
{
  gotBodyFocus = 0;
}


function lookup(inputString) {
	if(inputString.length == 0) {
		// Hide the suggestion box.
		$('#suggestions').hide();
	} else {
		$.post("rpc.php", {queryString: ""+inputString+""}, function(data){
			if(data.length >0) {
				$('#suggestions').show();
				$('#autoSuggestionsList').html(data);
			}
		});
	}
} // lookup

function fill(thisValue) {
	$('#inputString').val(thisValue);
	setTimeout("$('#suggestions').hide();", 200);
}

function AvaliaSeleccaoPagamento(formulario)
{
// set var radio_choice to false
var radio_choice = false;
var button = document.getElementById("submit");

// Loop from zero to the one minus the number of radio button selections
for (counter = 0; counter < formulario.radio.length; counter++)
{
// If a radio button has been selected it will return true
// (If not it will return false)
if (formulario.radio[counter].checked) {
radio_choice = true; 
button.disabled = true;}
}

if (!radio_choice)
{
// If there were no selections made display an alert box 
alert("Por favor, seleccione um tipo de pagamento.\n")


return (false);
}
return (true);
}


function ConfirmaEnvioInscricao(prova) {
var answer = confirm("Ao clicar em OK as inscrições tornam-se definitivamente validadas e será enviado um mail para os clubes com respectivas confirmações.\n\n Para cancelar a confirmação definitiva clique em CANCELAR.")
if (answer){
	window.location = "mostrar_validacoes_inscricao.php";
}
else{
	window.location = "admin.php";	
}
} 

