// Funzioni Javascript for GDM � 2008
// Developed by Davide Gullo  -->>> gullo at m4ss dot net

// /////////////////////////////////////////////////////////////
// /////////////////////// FUNZIONI GENERALI ///////////////////
// /////////////////////////////////////////////////////////////

	// 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 = '';
			}
		}
	}
	
// 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(jfunc, txt_confirm)
	{
		var risposta;
		risposta = MsgConferma(txt_confirm, 1);
		if( risposta )
		{
			eval(jfunc);
			return true;
		} else {
			return false;
		} 
	}

// 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);
	}

	
	
	
	function StartLinkLoading(id)
	{
		$(id).addClass('loading_smaller');
	}
	function StopLinkLoading(id)
	{
		$(id).removeClass('loading_smaller');
	}

	function StartLoading(id, size)
	{
		$(id).html('<span class="loading_' + size + '">Attendere...</span>').show();
	}
	function StopLoading(id)
	{
		$(id).html('').hide();
	}
	
	function DisableSubmit()
	{
		$('#submit').attr('disabled', true);
	}
	function EnableSubmit()
	{
		$('#submit').attr('disabled', false);
	}
	
	function StartLoadingCombo(idcombo)
	{
		$('#'+idcombo).attr('disabled', true).html('');
		addOption(idcombo,'Loading...','');
	}
	
	function LoadComboOptions(idcombo, arVal, emptyVal)
	{
		$('#'+idcombo).attr('disabled', false).html('');
		addOption(idcombo, emptyVal, '');
		for (opt in arVal)
		{
			addOption(idcombo, arVal[opt], opt);
		}
	}
	
	function convertBoolToString(b)
	{
		return b ? "S" : "N";
	}

    function initOverlayWrapContent(idRef)
    {
        $('#'+idRef)
            .overlay({  effect: 'apple',
                        onBeforeLoad: function() {
                            var wrap = this.getOverlay().find(".contentWrap");
                            wrap.load(this.getOverlay().attr('rel'));
                        }
                });
    }

