// Funzioni Javascript for GDM © 2008
// Developed by Davide Gullo  -->>> gullo at m4ss dot net

// /////////////////////////////////////////////////////////////
// /////////////////////// FUNZIONI GENERALI ///////////////////
// /////////////////////////////////////////////////////////////

	// Apre una nuova finestra
	function ApriFinestra( nome, urlpagina, w, h )
	{
		window.open( urlpagina, nome, 'left=50,top=50,width=' + w + ',height=' + h + ', menubar=no, scrollbars=yes, toolbar=no, screenX=0, screenY=0, status=1');
	}

	// Conferma messaggio delle Finestre Javascript
	function MsgConferma(txt, conferma)
	{
		// Exp Reg per cercare  . o ?
		var regX = /\.|\?|!/gi ;
		var _ret="";
		var counter = 0;
		var _item = txt.split(" ");
		for(var i=0;i<_item.length;i++)
		{
			counter++;
			if( _item[i].match(regX) )
			{
				_item[i]=_item[i] + '\n';
				_ret+=_item[i];
				counter = 0;
			} else {
				if( counter == 6 )
				{
					_item[i]=_item[i] + '\n';
					_ret+=_item[i];
					counter = 0;
				} else {
					_ret+=_item[i] + ' ';
				}
			}
		}
		if(conferma == 1)
		{
			return confirm(_ret);
		} else {
			alert(_ret);
			return true;
		}
	}
// Blocca submit per tasto invio
	function handleEnter(field, event) 
	{
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) 
		{
			// field.blur();
			// alert(field.name);
			return false;
		} else {
			return true;
		}
	}

// Aggiunge oggetti Option ad una select
	function addOption(selectId, txt, val)
	{
	        var objOption = new Option(txt,val);
	
	        document.getElementById(selectId).options.add(objOption);
	}

// Riempie una combo in base all'oggetto passato
	function populate(cacheobj, x, selezionato)
	{
		for (m=cacheobj.options.length-1;m>0;m--)
		{
			cacheobj.options[m]=null
		}

		selectedarray=eval(x);
		for (i=0;i<selectedarray.length;i++)
		{
			cacheobj.options[i]=new Option(selectedarray[i].text,selectedarray[i].value)
		}
		eval('cacheobj.options[' + selezionato + '].selected=true;');
	}

// Expandable content script
	function toggleDisplay(id){
		
		if(this.document.getElementById(id).style.display=='none'){
			this.document.getElementById(id).style.display='block'
			this.document.getElementById(id+"link").style.display='none';
		}else{
			this.document.getElementById(id).style.display='none'
			this.document.getElementById(id+"link").style.display='block';
		}
	}
// Come sopra ma switch solo su un elemento (id)
	function Simple_toggleDisplay(id){
		
		if(this.document.getElementById(id).style.display=='none'){
			this.document.getElementById(id).style.display='block';
		}else{
			this.document.getElementById(id).style.display='none';
		}
	}
	
	// Visualizzo contenuto per gestione tabcontentcontainer
	function DisplayContent(section, id)
	{
		var tabobj = document.getElementById("tablist");
		var tabobjlinks = tabobj.getElementsByTagName("A");
	
		for (i=1; i<tabobjlinks.length+1; i++)
		{
			var my_section = "sc" + i;
			if(my_section == section)
			{
				document.getElementById( "sc" + i + "_" + id ).style.display='block';
				document.getElementById( i + "_" + id ).className = 'current';
			} else {
				document.getElementById( "sc" + i + "_" + id ).style.display='none';
				document.getElementById( i + "_" + id ).className = '';
			}
		}
	}
	
	// Visualizzo contenuto per gestione tabcontentcontainer by Name (a differenza di quella sopra che considera solo i numeri!)
	function DisplayContentByName(section, arID)
	{
		for (i=0; i < arID.length; i++)
		{
			var myID = arID[i];
			var my_section = "sc_" + myID;
			if(my_section == section)
			{
				document.getElementById( "sc_" + myID + "_tab_f" ).style.display='block';
				document.getElementById( myID + "_tab_f" ).className = 'current';
			} else {
				document.getElementById( "sc_" + myID + "_tab_f" ).style.display='none';
				document.getElementById( myID + "_tab_f" ).className = '';
			}
		}
	}
	
// Verifico che invito al tavolo ci sia email
// Da sistemare con controllo indirizzo email valido
	function VerifyInvito(my_form)
	{
		var my_field_nome = my_form.email.value;
		if( my_field_nome != "")
		{
			return true;
		} else {
			alert("Devi inserire un indirizzo email valido prima di poter spedire un invito.");
			return false;
		}
	}

// Controlla che sia stato inserito un titolo per i post del forum
	function CheckPost(my_form)
	{
		if( my_form.titolo.value == "" )
		{
			alert('Il titolo è obbligatorio!!');
			return false;
		} else {
			return true;
		}
	}
	
// Rimuove un element HTML dalla pagina
	function RemoveElement(id)
	{
		var elem = document.getElementById(id);
		var old = (elem.parentNode).removeChild(elem);
	}	
	
// Richiesta conferma di Azione + funzione AJAX
	function xajaxConfermaAzione(ajax_func, txt_confirm)
	{
		var risposta;
		risposta = MsgConferma(txt_confirm, 1);
		if( risposta )
		{
			eval(ajax_func);
			return true;
		} else {
			return false;
		} 
	}

// Loading per combo + funzione AJAX
	function My_Xajax_AddCombo(ajax_func, idcombo)
	{
		document.getElementById(idcombo).disabled = true;
		document.getElementById(idcombo).options.length = 0;
		addOption(idcombo,'Loading...','');
		eval(ajax_func);
	}

// Disabilita pulsante submit + funzione AJAX
	function My_Xajax_Loading(id, size)
	{
		var div = "r_" + id;
		document.getElementById(div).style.display='block';
		document.getElementById(div).innerHTML = '<span class="loading_' + size + '">Attendere...</span>';
	}
	
// Attiva Loading + funzione AJAX per submit form (Pager)
	function My_Xajax_SubmitFormWithP(ajax_func, idform, params)
	{
		My_Xajax_Loading(idform, 'small');
		// eseguo funzione xajax 
		var func = ajax_func + "( xajax.getFormValues('" + idform + "') , '" + params + "' )";
		eval(func);
	}

// Disabilita pulsante submit + funzione AJAX
	function My_Xajax_SubmitForm(ajax_func, idform)
	{
		// disabilito pulsante submit
		document.getElementById('submit').disabled = true;
		My_Xajax_Loading(idform, 'small');
		// eseguo funzione xajax 
		var func = ajax_func + "( xajax.getFormValues('" + idform + "') )";
		eval(func);
	}
	
// Attiva Loading + funzione AJAX per link (Pager)
	function My_Xajax_LinkLoadingWithP(ajax_func, id)
	{
		document.getElementById(id).innerHTML = '';
		document.getElementById(id).className='loading_smaller';
		// eseguo funzione xajax 
		eval(ajax_func);
	}

// Attiva Loading + funzione AJAX per link
	function My_Xajax_LinkLoading(ajax_func, id)
	{
		document.getElementById(id).className='loading_smaller';
		// eseguo funzione xajax 
		eval(ajax_func);
	}

