
 function getTarget(e){
	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3){
		targ = targ.parentNode;
	}
	return targ;
 }

 function openPlayerScore(obj){
	var tables = obj.getElementsByTagName('DIV');
	var app;
	for(var i=0;i<tables.length;i++){
		if(tables[i].className=='pTables'){
			app = tables[i];
			break;
		}
	}
	if(app.style.display=='none'){
		app.style.display='block';
	}else{
		app.style.display='none';
	}
 }
 
 function openMap(){
	var mapd = document.getElementById('divMap');
	mapd.style.display = 'none';
	mapd.style.zIndex = 40;
	mapd.style.visibility = 'visible';
	openDiv('divMap');
 }
 
 function closeMap(){
 	var mapd = document.getElementById('divMap');
	mapd.style.visibility = 'hidden';
	mapd.style.zIndex = 1;
	closeDiv();
 }
 
//---------------------------------------------------------
//	GESTIONE UTENTI
//---------------------------------------------------------
 
 //apre la finestra per l'aggiunta di un utente
 function openNewUser(){
	//creo il nuovo utente nel db e prendo l'id
	var risp = ajaxRequestPost('core.php','mode=129');
	var app = parseInt(risp,10);
	//carico la finestra di edit
	var risp2 = ajaxRequestPost('core.php','mode=130&user='+app);
	document.getElementById('divUserData').innerHTML = risp2;
 }
 
 //apre il dettaglio del utente selezionato
 function changeUser(idUser){
	var risp = ajaxRequestPost('core.php','mode=130&user='+idUser);
	document.getElementById('divUserData').innerHTML = risp;
 }
 
 //svuoto la machera di modifica di un utente
 function clearUserData(){
	document.getElementById('divUserData').innerHTML = "";
 }
 
 //modifica i dati dell'utente selezionato
 function modifyUser(){
	var idUser = document.getElementById('hddUserId').value;
	var nome = escape(document.getElementById('txtUNome').value);
	var cog = escape(document.getElementById('txtUCog').value);
	var username = escape(document.getElementById('txtUUsername').value);
	var password = document.getElementById('txtUPassword').value;
	var admin = document.getElementById('chkAdmin').checked;
	if(idUser&&nome&&cog&&username&&password){
		var appStr = 'mode=131&user='+idUser+'&name='+nome+'&surname='+cog+'&username='+username+'&password='+password;
		if(admin)appStr = appStr + '&admin=1';
		var risp = ajaxRequestPost('core.php',appStr);
		//aggiorno la lista dei giocatori
		document.getElementById('divUsers').innerHTML = risp;
		alert('Dati utente aggiornati.');
	}else{
		alert("Attenzione! Alcuni dati sono stati lasciati vuoti.");
	}
 }
 
 //eliminazione utente
 function deleteUser(){
	if(document.getElementById('hddUserId').value!=""){
		if(confirm("Si vuole veramente eliminare questo utente?")){
			var risp = ajaxRequestPost('core.php','mode=132&user='+document.getElementById('hddUserId').value);
			//aggiorno l'interfaccia
			clearUserData();
			document.getElementById('divUsers').innerHTML = risp;
		}
	}
 }
 
//---------------------------------------------------------
//	GESTIONE GIOCATORI
//---------------------------------------------------------

 //termina l'upload dei file
 function uploadFile(filename){
	document.getElementById('hddImage').value=filename;
	document.getElementById('imgUp').src=filename;
 }
 
 //cambia la squadra attualmente selezionata
 function changeSquad(){
	var squad = document.getElementById('selSquad').value;
	var risp = ajaxRequestPost('core.php','mode=101&squad='+squad);
	document.getElementById('divPlayers').innerHTML = risp;
	clearPlayerData();
 }
 
 //apre il dettaglio del giocatore selezionato
 function changePlayer(idPlayer){
	var risp = ajaxRequestPost('core.php','mode=102&player='+idPlayer);
	document.getElementById('divPlayerData').innerHTML = risp;
 }
 
 //svuoto la machera di modifica di un giocatore
 function clearPlayerData(){
	document.getElementById('divPlayerData').innerHTML = "";
 }
 
 //apre la finestra per l'aggiunta di un giocatore alla squadra
 function openNewPlayer(){
	var squad = document.getElementById('selSquad').value;
	//creo il nuovo giocatore nel db e prendo l'id
	var risp = ajaxRequestPost('core.php','mode=103&squad='+squad);
	var app = parseInt(risp,10);
	//carico la finestra di edit
	var risp2 = ajaxRequestPost('core.php','mode=102&player='+app);
	document.getElementById('divPlayerData').innerHTML = risp2;
 }

 //modifica i dati del giocatore selezionato
 function modifyPlayer(){
	var idPlayer = document.getElementById('hddPlayerId').value;
	var squad = escape(document.getElementById('selSquad').value);       
	var nome = escape(document.getElementById('txtPNome').value);
	var cog = escape(document.getElementById('txtPCog').value);
	var ruolo = escape(document.getElementById('txtPRole').value);
	var goal = document.getElementById('txtPGoal').value;
	var plays = document.getElementById('txtPPlays').value;
	var image = document.getElementById('hddImage').value;
	var giocatore = document.getElementById('chkAdmin').checked;
	if(idPlayer&&nome&&cog&&ruolo&&goal&&plays){
		var appStr = 'mode=105&player='+idPlayer+'&squad='+squad+'&name='+nome+'&surname='+cog+'&role='+ruolo+'&goal='+goal+'&plays='+plays+'&image='+image;
		if(!giocatore)appStr = appStr + '&admin=1';
		var risp = ajaxRequestPost('core.php',appStr);
		//aggiorno la lista dei giocatori
		changeSquad();
		alert('Dati giocatore aggiornati.');
	}else{
		alert("Attenzione! Alcuni dati sono stati lasciati vuoti.");
	}
 }
 
 //eliminazione giocatore
 function deletePlayer(){
	if(document.getElementById('hddPlayerId').value!=""){
		if(confirm("Si vuole veramente eliminare questo giocatore?")){
			var risp = ajaxRequestPost('core.php','mode=104&player='+document.getElementById('hddPlayerId').value);
			//aggiorno l'interfaccia
			clearPlayerData();
			changeSquad();
		}
	}
 }

//---------------------------------------------------------
//	GESTIONE POST
//---------------------------------------------------------
 
 //termina l'upload dei file
 function uploadPostImage(filename){
	document.getElementById('imgUp').src=filename;
	document.getElementById('hddImgPost').value=filename;
 }
 
 //inserimento di un nuovo post
 function openNewPost(){
	//creo il nuovo post nel db e prendo l'id
	var risp = ajaxRequestPost('core.php','mode=112');
	var app = parseInt(risp,10);
	//carico la finestra di edit
	var risp2 = ajaxRequestPost('core.php','mode=120&post='+app);
	document.getElementById('divPostEdit').innerHTML = risp2;
	//carico il contenuto nell'editor
	var risp3 = ajaxRequestPost('core.php','mode=111&post='+app);
	openEditor(risp3);
 }
 
 //carica il post selezionato
 function loadCurrentPost(obj){
	var id = obj.id.split('p_')[1];
	//carico la finestra di edit
	var risp2 = ajaxRequestPost('core.php','mode=120&post='+id);
	document.getElementById('divPostEdit').innerHTML = risp2;
	//carico il contenuto nell'editor
	var risp3 = ajaxRequestPost('core.php','mode=111&post='+id);
	openEditor(risp3);
 }
 
 //seleziona il layout per il post
 function selectLayout(obj){
	if(obj.className=='layout_sel')return false;
	var lay = document.getElementById('divLayout').getElementsByTagName('DIV');
	for(var i=0;i<lay.length;i++){
		if(lay[i].className=='layout_sel'){
			lay[i].className = 'layout';
			break;
		}
	}
	obj.className = 'layout_sel';
	if(obj.id=='layout1'){
		document.getElementById('divSource').style.display = "none";
		document.getElementById('divImage').style.display = "none";
	}else if(obj.id=='layout2'){
		document.getElementById('divSource').style.display = "none";
		document.getElementById('divImage').style.display = "block";
	}else if(obj.id=='layout3'){
		document.getElementById('divSource').style.display = "block";
		document.getElementById('divImage').style.display = "none";
	}
 }
 

 //salva il post 
 function savePost(){
	var idPost = document.getElementById('hddPostId').value;
	if(document.getElementById('layout1').className=='layout_sel'){
		//salvataggio di un post titolo + testo
		var titolo = escape(document.getElementById('txtPostTitle').value);
		var testo = escape(CKEDITOR.instances.txtPostEditor.getData());
		if(titolo!=""&&testo!=''){
			var risp = ajaxRequestPost('core.php','mode=122&title='+titolo+'&content='+testo+'&layout=layout1&post='+idPost);
			//aggiorno l'interfaccia
			document.getElementById('divPostEdit').innerHTML = "";
			document.getElementById('divPosts').innerHTML = risp;
		}else{
			alert('Attenzione. Alcuni campi sono stati lasciati vuoti.')
		}
	}else if(document.getElementById('layout2').className=='layout_sel'){
		//salvataggio di un post titolo + testo + immagine
		var titolo = escape(document.getElementById('txtPostTitle').value);
		var testo = escape(CKEDITOR.instances.txtPostEditor.getData());
		var source = escape(document.getElementById('hddImgPost').value);
		source = source.replace("_mini","");
		if(titolo!=""&&testo!=''){
			if(document.getElementById('hddImgPost').value.length>3)var risp = ajaxRequestPost('core.php','mode=122&title='+titolo+'&content='+testo+'&layout=layout2&source='+source+'&post='+idPost);
			else var risp = ajaxRequestPost('core.php','mode=122&title='+titolo+'&content='+testo+'&layout=layout2&post='+idPost);
			//aggiorno l'interfaccia
			document.getElementById('divPostEdit').innerHTML = "";
			document.getElementById('divPosts').innerHTML = risp;
		}else{
			alert('Attenzione. Alcuni campi sono stati lasciati vuoti.')
		}
	}else if(document.getElementById('layout3').className=='layout_sel'){
		//salvataggio di un post titolo + testo + video
		var titolo = escape(document.getElementById('txtPostTitle').value);
		var testo = escape(CKEDITOR.instances.txtPostEditor.getData());
		var source = escape(document.getElementById('txtPostVideo').value);
		if(titolo!=""&&testo!=''&&source!=""){
			var risp = ajaxRequestPost('core.php','mode=122&title='+titolo+'&content='+testo+'&layout=layout3&source='+source+'&post='+idPost);
			//aggiorno l'interfaccia
			document.getElementById('divPostEdit').innerHTML = "";
			document.getElementById('divPosts').innerHTML = risp;
		}else{
			alert('Attenzione. Alcuni campi sono stati lasciati vuoti.')
		}
	}
 }
 
 //elimina il post selezionato
 function deletePost(){
	if(document.getElementById('hddPostId').value!=0){
		if(confirm("Si vuole procedere alla cancellazione di questa notizia?")){
			var risp = ajaxRequestPost('core.php','mode=114&post='+document.getElementById('hddPostId').value);
			//aggiorno l'interfaccia
			document.getElementById('divPostEdit').innerHTML = "";
			document.getElementById('divPosts').innerHTML = risp;
		}
	}else{
		alert('Attenzione. Nessuna notizia selezionata.');
	}
 }
 
//---------------------------------------------------------
//	GESTIONE RISULTATI
//---------------------------------------------------------

 //mostra le giornate di un campionato
 function changeChamp(){
	var champ = document.getElementById('selChamp').value;
	var risp = ajaxRequestPost('core.php','mode=106&champ='+champ);
	document.getElementById('divTurnList').innerHTML = risp;
	changeTurn();
	changeScorer();
 }
 
 //mostra gli incontri di una giornata
 function changeTurn(){
	var turn = document.getElementById('selTurn').value;
	var risp = ajaxRequestPost('core.php','mode=107&turn='+turn);
	document.getElementById('divFightList').innerHTML = risp;
 }
 
 //imposta la giornata come attuale
 function setActualTurn(){
	var champ = document.getElementById('selChamp').value;
	var turn = document.getElementById('selTurn').value;
	var risp = ajaxRequestPost('core.php','mode=109&champ='+champ+'&turn='+turn);
	alert("Giornata impostata come attuale");
 }

 //visualizza la classifica aggiornata
 function changeScorer(){
	var champ = document.getElementById('selChamp').value;
	var risp = ajaxRequestPost('core.php','mode=110&champ='+champ);
	document.getElementById('divScores').innerHTML = risp;
 }
 
 //aggiorna i risultati di una giornata
 function updateResults(){
	var champ = document.getElementById('selChamp').value;
	var results = document.getElementById('tblMatch').getElementsByTagName('INPUT');
	var string = "";
	for(var i=0;i<results.length-1;i=i+2){
		var id = results[i].id.split('txtG1_')[1];
		var app1 = results[i].value;
		var app2 = results[i+1].value;
		if(app1!=""&&app2!="")string = string + id + '_' + app1 + '_' + app2 + '|';
	}
	var risp = ajaxRequestPost('core.php','mode=108&champ='+champ+'&results='+string);
	document.getElementById('divScores').innerHTML = risp;
 }

//---------------------------------------------------------
//	GESTIONE IMMAGINI
//---------------------------------------------------------
 
 //completa l'upload di un'immagine su un album
 function addAlbumImage(imgSrc,id){
	document.getElementById('imgLoading').style.visibility = "hidden";
	var album = document.getElementById('hddAlbum').value;
	var photo = document.createElement('SPAN');
	photo.id = 'sp_'+id;
	photo.className = 'albumImage';
	var photoImg = document.createElement('IMG');
	photoImg.src = imgSrc;
	photoImg.id = 'img_'+id;
	photo.appendChild(photoImg);
	document.getElementById('divImageList').appendChild(photo);
 }
 
 //crea un nuovo album
 function addAlbum(){
	var risp = ajaxRequestPost('core.php','mode=115');
	document.getElementById('divCurAlbum').innerHTML = risp;
 }
 
 //edita l'album attuale e ricarica la lista
 function editAlbum(){
	var id = document.getElementById('hddAlbum').value;
	var name = escape(document.getElementById('txtAName').value);
	var risp = ajaxRequestPost('core.php','mode=116&album='+id+'&name='+name);
	document.getElementById('divAlbumGrid').innerHTML = risp;
 }
 
 //carica l'album selezionato
 function loadAlbum(id){
	var risp = ajaxRequestPost('core.php','mode=117&album='+id);
	document.getElementById('divCurAlbum').innerHTML = risp;
 }
 
 //elimina l'album selezionato e tutte le sue foto
 function deleteAlbum(id){
 	if(confirm("Si vuole veramente eliminare quest'album?")){
 		var risp = ajaxRequestPost('core.php','mode=118&album='+id);
 		document.getElementById('divCurAlbum').innerHTML = "";
		//ricarico la lista degli album
 		document.getElementById('divAlbumGrid').innerHTML = risp;
 	}
 }
 
 //elimina l'immagine selezionata
 function deleteImage(evt){
	var tar = getTarget(evt);
	if(tar.tagName=='IMG'){
		var id = tar.id.split('_')[1];
		var risp = ajaxRequestPost('core.php','mode=119&image='+id);
		document.getElementById('divImageList').removeChild(document.getElementById('sp_'+id));
	}
 }
 
 //---------------------------------------------------------
 //	GESTIONE COACH
 //---------------------------------------------------------
 
 //mostra le giornate di un campionato
 function changeChampCoach(){
	var champ = document.getElementById('selChamp').value;
	var risp = ajaxRequestPost('core.php','mode=124&champ='+champ); 	
	document.getElementById('divTurnList').innerHTML = risp;	
	changeTurnCoach();	
 }
 
 //mostra gli incontri di una giornata
 function changeTurnCoach(){
	var turn = document.getElementById('selTurn').value;
	var risp = ajaxRequestPost('core.php','mode=123&turn='+turn);
	document.getElementById('divFightName').innerHTML = risp;	
	changeCommentCoach();
 }
 
 //cambia i commenti ed i tabellini di un incontro
 function changeCommentCoach(){
	//controllo se il commento esiste e in caso lo carico
	var fight = document.getElementById('hddFightId').value;	
	var risp = ajaxRequestPost('core.php','mode=125&fight='+fight);
	openEditor(risp);
	//stampo i tabellini di una partita
	var risp2 = ajaxRequestPost('core.php','mode=126&fight='+fight);
	document.getElementById('divTables').innerHTML = risp2;
 }
 
 //salva il commento di una partita ed i tabellini
 function saveComment(){
	var fight = document.getElementById('hddFightId').value;	
	var testo = escape(CKEDITOR.instances.txtPostEditor.getData());
	var inputs = document.getElementById('divTabellini').getElementsByTagName('INPUT');
	var str = "";
	for(var i=0;i<inputs.length;i++){
		if(inputs[i].type=='text'){
			var app = inputs[i].id.split('txt_')[1];
			str = str + '|' + app + '_' + inputs[i].value;
		}
	}
	var richiesta = 'mode=127&fight='+fight+'&content='+testo+'&votes='+str;
	var risp = ajaxRequestPost('core.php',richiesta);
	alert('Modifiche salvate');
 }
 
 //elimina il commento ed i tabellini del coach
 function deleteComment(){
	var fight = document.getElementById('hddFightId').value;
	var risp = 	ajaxRequestPost('core.php','mode=128&fight='+fight);
	CKEDITOR.instances.txtPostEditor.setData("");
	var inputs = document.getElementById('divTabellini').getElementsByTagName('INPUT');
	for(var i=0;i<inputs.length;i++){
		if(inputs[i].type=='text'){
			inputs[i].value = "";
		}
	}
	alert("Commento e tabellini eliminati.");
 }
 

