// javaScript
//-- Dalen

//recupera l'oggetto rappresentante un entita HTML presa per id
function getElement(id_elemento) 
{
    var elemento;
 
   //controllo browser
   if(document.getElementById)
      elemento = document.getElementById(id_elemento);
   else
      elemento = document.all[id_elemento];
      
   return elemento;
}

//restituisce il valore di un elemento
function gv(id)
{
	//alert($("#"+id).val());
	return $("#"+id).val();
}

//assegna un valore ad un elemento
function setValue(id_elemento, valore)
{
	getElement(id_elemento).value=valore;
}

//setta il testo di un elemento
function setText(id_elemento, valore)
{
	$("#"+id_elemento).text(valore);
}

//cambia il valore di un checkbox
function changeCheckbox(id_c)
{
	var cb = getElement(id_c);
	if(cb.checked)
		cb.checked=false;
	else
		cb.checked=true;
}

//spegne tutti i checkbox di gare e bus
function notCompete(id_u)
{
	if(getElement('no_race'+id_u).checked)
	{
		for(var i=1; i<6; i++)
		{
			if(getElement('stage'+i+id_u).checked)
				changeCheckbox('stage'+i+id_u);
		}
		if(getElement('rentSC'+id_u).checked)
			changeCheckbox('rentSC'+id_u);
		setValue('SC'+id_u,'');
	}
}

//controlla che se si partecipa ad almeno una gara il checkbox 'no partecipo sia disattivato'
function controlRaces(id,id_u)
{
	if(getElement('stage'+id+id_u).checked)
	{
		if(getElement('no_race'+id_u).checked)
			changeCheckbox('no_race'+id_u);
	}
}

//controlla se la pagina è aperta o no
function linkToNothing(id)
{
	if(id<200)
	{
		$("#"+id).replaceWith('<span id="'+id+'">' + $("#"+id).text() + "</span>");
		for(var i=0; i<4; i++)
		{
			if((i+130)!=id)
			{
				var id_n=130+i;
				var id_p=30+i;
				var onc="onclick=\" linkToNothing('"+id_n+"'); changePage('"+id_p+"');\"";
				$("#"+id_n).replaceWith('<a id="'+id_n+'"'+onc+' >' + $("#"+id_n).text() + "</a>");
			}
		}
			
	}
}

//sfoglia pagine
function changePage(id)
{
	for(var i=0;i<5;i++)
	{
		if((30+i)!=id)
		{
			var id_n=30+i;
			if(!$("#"+id_n).is(":hidden"))
				hideElement(id_n);
		}
	}
	showElement(id);	
}

//cambia testo linkper visualizzare il dettaglio spese
function editLinkDetails(id)
{
	if($('#'+id).text()=="show details")
		$('#'+id).text("hide details");
	else
		$('#'+id).text("show details");
}

//funzioni per il rollover sponsors l'altra funzione si trova nello header di index.php


/**********************************************
 * funzioni per mostrare e nascondere elementi
 **********************************************/

//mostra oggetti nascosti
function showElement(id_elem)
{
	$("#"+id_elem).slideDown("normal");
   	//getElement(id_elem).style.display='';
}

//nasconda oggetti
function hideElement(id_elem)
{
	$("#"+id_elem).slideUp("normal");
	//getElement(id_elem).style.display='none';
}

//mostra o nasconde a seconda dello stato dell'oggetto	
function openAndClose(id_elem)
{

	if($("#"+id_elem).is(":hidden"))
	   showElement(id_elem);
	else
	   hideElement(id_elem);
}

/**********************************************
 * funzioni varie di controllo
 **********************************************/
function isASiCard(number, id_u)
{
	var string = ""+number;
	if(isNaN(number) || string.length<4 || string.length>7)
	{ 
		alert(number+' is not a valid number ');
		setValue('SC'+id_u,'');
		getElement('SC'+id_u).focus();
	}

}
/*
function saveRunner(id, sicard, cat, name, surname, no_races, nat, id_club, flag, town, address, year, pren, tipo_a)
{
	if((sicard=='siCard' || sicard=='') && (!getElement('rentSC'+id).checked && !no_races))
	{
		alert('siCard number is not valid');
	} 
	else if(name=="" || surname=="" )
	{
		alert('name or surname is not valid');
	}
	else if(town!="n" && address=="")
	{
		alert('if you know where you are living in Primiero you must write down the address');
	}
	else 
	{ 
		if(getElement('stage5'+id).checked)
		{
			if(!getElement('bus5d'+id).checked)
				getElement('bus1d'+id).checked=true;
		}
		openAndClose('runner_data'+id); 
		openAndClose('small'+id); 
		var cat_n;
		if(no_races)
			cat_n=99;
		else
			cat_n=cat;
		insertRunnerData(id, sicard, cat_n, name, surname, nat, id_club, flag, town, address, year, pren, tipo_a);
	}
}
*/
function saveRunner(id, town, address, pren, tipo_a)
{

	if(town!="n" && address=="")
	{
		alert('if you know where you are living in Primiero you must write down the address');
	}
	else 
	{ 
		openAndClose('runner_data'+id); 
		openAndClose('small'+id); 
		insertRunnerData(id, town, address, pren, tipo_a);
	}
}
function resetForm(id)
{
	setValue('name_r'+id,'');
	setValue('surname_r'+id,'');
	setValue('SC'+id,'');
	getElement('rentSC'+id).checked=false;
	for(i=1;i<6;i++)
		getElement('stage'+i+id).checked=true;
	getElement('bus5d'+id).checked=true;
	getElement('bus1d'+id).checked=false;
	getElement('no_race'+id).checked=false;
}

function controlEmail(email)
{
	var r = new RegExp("^[a-z]");
}

function controlPasswords(old, new1, new2)
{
	if(old=="")
		return false;
	if(new1=="")
		return false;
	if(new1==new2)
		return true;
}

/**********************************************
 * ajax
 **********************************************/
//riempo alcuni campi con dati FISO.
function fiso(id, tessera)
{
	if(tessera!="")
	{
		$.post("pages/base/ajax.php?action=fiso", { code : tessera }, 
												function(data)
												{
													if(data.response==1)
													{
														setValue('name_r'+id,data.name);
														setValue('surname_r'+id,data.surname);
														setValue('SC'+id,data.sicard);
														getElement('year'+id).selectedIndex=parseInt(data.year)-1900;
													}
													else
													{
														alert('perkele');
													}
												}
   			, "json");
   	}	
}
//inserimento nome club nel database
function insertClubName(club_name, id)
{
	$.post("pages/base/ajax.php?action=iclubname", { cn : club_name, id : id }, 
												function(data)
												{
													if(data.response==1)
													{
														$("#club_name_o").slideUp("normal");
														setText('resp', "now your club's name is "+club_name);
														setTimeout('setText("resp", "")', 3000);

													}
													else
													{
														$("#resp").removeClass("ok");
														$("#resp").addClass("error");
														setText('resp', "there was a problem "+data.detail);
														setTimeout('setText("resp", "")', 3000);
													}
												}
   			, "json");
}

//inserimento dati gare e trasporti di un runner
/*
function insertRunnerData(id, sicard, _class, name, surname, nat, id_club, flag, town, address, year, pren, tipo_a)
{
	var sicard; 
	if(!getElement('rentSC'+id).checked) 
		sicard=gv('SC'+id); 
	else 
		sicard=1; 

	var races='';
	if(getElement('stage1'+id).checked) races+='1'; else races+='0';
	if(getElement('stage2'+id).checked) races+='1'; else races+='0';
	if(getElement('stage3'+id).checked) races+='1'; else races+='0';
	if(getElement('stage4'+id).checked) races+='1'; else races+='0';
	if(getElement('stage5'+id).checked) races+='1'; else races+='0';
	
	var bus='';
	if(getElement('bus5d'+id).checked) 
		bus='11111';
	else 
		bus='00001';
	
	//alert(sicard+' - '+_class+' - '+races+' - '+bus+' - '+not_r);
	
	$.post("pages/base/ajax.php?action=irunnerdata", { id : id, sicard : sicard, _class : _class, races : races, bus : bus, name : name, surname : surname, nat : nat, id_club : id_club, town : town, address : address, y : year, pre : pren, tipo_a : tipo_a }, 
												function(data)
												{
													if(data.response==1)
													{
														//$("#runner_data"+id).slideUp("normal");
														//alert(data.detail);
														flag=parseInt(flag);
														if(flag==0)
														{
															setSession('entry');
															var html = '<div class="entry" id="small<?=$id?>"><p class="left">'+name+' '+surname+'</p><p class="right"><a onclick="location.reload();">edit</a></p></div>';
															$("#small0").after(html);
															resetForm('0');
														}
														setText('resp', data.detail);
														setTimeout('setText("resp", "")', 3000);
													}
													else
													{
														$("#resp").removeClass("ok");
														$("#resp").addClass("error");
														setText('resp', data.detail);
														setTimeout('setText("resp", "")', 3000);
													}
												}
   			, "json");	
}
*/
function insertRunnerData(id, town, address, pren, tipo_a)
{
	
	$.post("pages/base/ajax.php?action=irunnerdata", { id : id, town : town, address : address, pre : pren, tipo_a : tipo_a }, 
												function(data)
												{
													if(data.response==1)
													{
														//$("#runner_data"+id).slideUp("normal");
														//alert(data.detail);
														setText('resp', data.detail);
														setTimeout('setText("resp", "")', 3000);
													}
													else
													{
														$("#resp").removeClass("ok");
														$("#resp").addClass("error");
														setText('resp', data.detail);
														setTimeout('setText("resp", "")', 3000);
													}
												}
   			, "json");	
}
//richiesta dati per modifiche
function dataForUpdate(id)
{
	$.post("pages/base/ajax.php?action=dataforupdate", { id : id }, 
												function(data)
												{
												setValue('SC'+id, data.sicard);
												openAndClose('header'+id);
												openAndClose('runner_datas'+id);
												openAndClose('small'+id);
												}
   			, "json");
}

//aggiornamento dati club
function updateClubData(id_c, club_name, nation)
{
	$.post("pages/base/ajax.php?action=updateclubdata", { club_id : id_c, club_n : club_name, nat: nation }, 
												function(data)
												{
													if(data.response==1)
													{
														//$("#club_data").slideUp("normal");
														setText('resp', data.detail);
														setTimeout('setText("resp", "")', 3000);
														location.reload(true);
														//alert(data.detail);

													}
													else
													{
														$("#resp").removeClass("ok");
														$("#resp").addClass("error");
														setText('resp', data.detail);
														setTimeout('setText("resp", "")', 3000);
													}
												}
   			, "json");
}

//conferma pagamento
function confirm_pay(id, jwoc)
{
	$.post("pages/base/ajax.php?action=confirmpayment", { id : id, j_tot : jwoc}, 
												function(data)
												{
													if(data.response==1)
													{
														$("#economy_detail").hide("normal");
													}
													else
													{
														$("#resp").removeClass("ok");
														$("#resp").addClass("error");
														setText('resp', data.detail);
														$("#economy_detail").hide("normal");
														setTimeout('setText("resp", "")', 3000);
													}
												}
   			, "json");
}

//controllo disponibilità username
function checkUsername(un)
{
	$.post("pages/base/ajax.php?action=checkusername", { un : un }, 
												function(data)
												{
													if(data.response==1)
													{
														//$("#club_data").slideUp("normal");
														$("#response").val("username available");
														if($("#response").hasClass("error"))
															$("#response").removeClass("error");
														$("#response").addClass("ok");
													}
													else
													{
														getElement('usernameO').value="";
	      												getElement('response').value="Username non disponibile";
	      												getElement('usernameO').focus();
														if($("#response").hasClass("ok"))
															$("#response").removeClass("ok");														
														$("#response").addClass("error");
													}
												}
   			, "json");
}

//
function setSession(what)
{
		$.post("pages/base/ajax.php?action=setsession", { w : what }, 
												function(data)
												{
												/*	if(data.response==1)
													{
														//$("#club_data").slideUp("normal");
														$("#response").val("username available");
														if($("#response").hasClass("error"))
															$("#response").removeClass("error");
														$("#response").addClass("ok");
													}
													else
													{
														getElement('usernameO').value="";
	      												getElement('response').value="Username non disponibile";
	      												getElement('usernameO').focus();
														if($("#response").hasClass("ok"))
															$("#response").removeClass("ok");														
														$("#response").addClass("error");
													}*/
												}
   			, "json");
}

function changePassword(olpPass,newPass)
{
	$.post("pages/base/ajax.php?action=changepassword", { op : olpPass, np : newPass}, 
												function(data)
												{
													if(data.response==1)
													{
														setText('resp', data.detail);
														$("#economy_detail").hide("normal");
													}
													else
													{
														$("#resp").removeClass("ok");
														$("#resp").addClass("error");
														setText('resp', data.detail);
													}
												}
   			, "json");
}

function newPassword(un)
{
	$.post("pages/base/ajax.php?action=newpassword", {username:un}, 
												function(data)
												{
													if(data.response==1)
													{
														openAndClose('user_for_pass');
														openAndClose('login_form');
														openAndClose('new_club');
														openAndClose('fg_pw');
														setText('resp', data.detail+" "+un);
													}
													else
													{
														$("#resp").removeClass("ok");
														$("#resp").addClass("error");
														setText('resp', data.detail);
													}
												}
   			, "json");
}

