/**
 * cercaInstallatori
 * @return
 */
var tot_installatori = 0;
var tot_installatori_def = 0;
function cercaInstallatori ( pagina_da, tot_record_start )
{
	/* variabili per la ricerca */
	var tipo_ricerca = 'instal';
	var provincia    = $F('provincia');
	var comune		 = $F('comune');
	tot_record = (tot_record_start == 0) ? 0 : tot_installatori;
	def_tot_record = (tot_record_start == 0) ? 0 : tot_installatori_def;
	
	/* variabili di gestione request/response ajax */
	var currentFunction = "cercaInstallatori";
	var target 			= "corpo_centrale";	
	var submit_button	= "submit_search_inst";
	var loadingName		= "loading";
	var codice_agente	= ($('codice_agente') && $('codice_agente').checked) ? $F('codice_agente') : "";
	var params 			= "tot_record=" + tot_record + "&def_tot_record=" + def_tot_record + "&pagina_da=" + pagina_da + "&tipo_ricerca=" + tipo_ricerca + "&provincia=" + provincia + "&comune=" + comune + "&currentFunction=" + currentFunction;
	if ( codice_agente != "" ) params += "&codice_agente=" + codice_agente;
	
	$(target).update();
	$(loadingName).show();
	$(submit_button).disable();
	//executeAjaxRequest ( urlAjax, 'get', params, true, target, loadingName, submit_button );
	new Ajax.Request(
			urlAjax, 
			{ 
				method: 'get', 
				parameters: params, 
				evalJS: true, 
				onSuccess: function (transport) 
				{ 
					/**
					alert(transport.responseText);
					$(submit_button).enable();
					/**/
					
					eval(transport.responseText);
					$(loadingName).hide();
					$(submit_button).enable();
					
					$(target).update ( string );
					tot_installatori = tot_record;
					tot_installatori_def = def_tot_record;
				}, 
				onFailure: function (transport) 
				{ 
					alert("oops path non trovato: " + urlAjax + " o errore di sintassi nel tpl!!" );
				}
			});
} 

/**
 * cercaInstallatori
 * @return
 */
function cercaAllegati ( categoria, azienda, isLoggato, pagina)
{
	
	var currentFunction = "cercaAllegati";
	var target 			= "lista_documenti";
	var loadingName		= "loading";
	    isLoggato		= ( isLoggato == 1 ) ? "Y" : "N";
	var params 			= "categoria=" + categoria + "&azienda=" + azienda + "&isLoggato=" + isLoggato + "&currentFunction=" + currentFunction + "&pagina=" + pagina;
	$(target).update();
	$(loadingName).show();
	executeAjaxRequest ( urlAjax, 'get', params, true, target, loadingName, "" );
}

/**
 * FUNZIONI RELATIVE ALLA PAGINA UNLINKED_USER_DETAIL
 */
function retrieveAziendaDetail ( id_azienda )
{
	var currentFunction = "retrieveAziendaDetail";
	var complemFunction = "retrieveAziendaID";
	var target 			= "result_azienda";	
	var loadingName		= "loading_azienda";
	var params1			= "id_azienda=" + id_azienda + "&currentFunction=" + currentFunction;
	var params2			= "id_azienda=" + id_azienda + "&currentFunction=" + complemFunction;
	var codice_azienda  = "";
	
	if ( id_azienda != "" )
	{
		$(target).update();
		$(loadingName).show();
		executeAjaxRequest ( urlAjax, 'get', params1, true, target, loadingName, "" );

		new Ajax.Request(
				urlAjax, 
				{ 
					method: 'get', 
					parameters: params2, 
					evalJS: true, 
					onSuccess: function (transport) 
					{  
						if (transport.responseText != "" )
							$('codice_agente').value = eval(transport.responseText);
					}, 
					onFailure: function (transport) 
					{ 
						alert("oops path non trovato: " + urlAjax + " o errore di sintassi nel tpl!!" );
					}
				});
		
		return true;
	}
	else
	{
		$('codice_agente').value = "";
		$(target).update( '<i><span style="font-size:9px; margin-left:40px;">Nessun MasterGID di riferimento selezionato</span></i>' );
		return true;
	}
}

function listaProdotti ( isLoggato, codice_gruppo )
{
	var currentFunction = "listaProdotti";
	var target 			= "lista_prodotti";
	var loadingName		= "loading";
	    isLoggato		= ( isLoggato == 1 ) ? "Y" : "N";
	var params 			= "codice_gruppo=" + codice_gruppo + "&isLoggato=" + isLoggato + "&currentFunction=" + currentFunction;
	
	$(target).update();
	$(loadingName).show();
	executeAjaxRequest ( urlAjax, 'get', params, true, target, loadingName, "" );
}

function filtraProdotti ( isLoggato, prodotto, marchio )
{
	var currentFunction = "filtraProdotti";
	var target 			= "lista_prodotti";
	var loadingName		= "loading";
	      isLoggato		=  (isLoggato == 1) ? "Y" : "N";
	var params			= "currentFunction=" + currentFunction + "&isLoggato=" + isLoggato;
	if ( prodotto != "" ) 		  params += "&prodotto=" + prodotto;
	if ( parseInt(marchio) != 0 ) params += "&marchio=" + marchio;
	//alert(params); 
	$(target).update();
	$(loadingName).show();
	executeAjaxRequest ( urlAjax, 'get', params, true, target, loadingName, "" );
}

function getAnniEsercizio (codice_azienda, tipo_documento, selected)
{
	var currentFunction = "getAnniEsercizio";
	var target		    = "select_anno_esercizio";
	
	$(target).update();
	var newSelectOption = new Option('Caricamento in corso...');
	$(target).appendChild(newSelectOption);
		
	var params = "codice_azienda=" + codice_azienda + "&tipo_documento="+tipo_documento+"&selected="+selected+"&currentFunction="+currentFunction;
	
	new Ajax.Request(
			urlAjax, 
			{ 
				method: 'get', 
				parameters: params, 
				evalJS: true, 
				onSuccess: function (transport) 
				{ 
					//alert(transport.responseText);
					var tmp = transport.responseText.split("--");
					var selected = tmp[0];
					var key_val	 = tmp[1].split("||");
					$(target).update();
					
					for(i = 0; i < key_val.length; i++) 
					{
						var curr_key_val = key_val[i].split(",");		
						var newSelectOption = new Option(curr_key_val[1], curr_key_val[0]);
						//$(target).appendChild(newSelectOption); // appendChild non funziona su IE
						if (curr_key_val[1] != "")
							$(target).options.add(newSelectOption);
					}
	
					$(target).value = selected;
					
					if (selected != "")
						listaODL('');
				}, 
				onFailure: function (transport) 
				{ 
					alert("oops problemi di recupero dati dal server ajax");
				}
			});
}

function listaODL( codice_odl )
{
	var codice_azienda_wt = $F('codice_cliente');
	var anno_esercizio = $F('select_anno_esercizio');
	
	var formName    	= "documentsView";
	var currentFunction = "listaODL";
	var target 			= "list_odl";	
	var loadingName		= "loading_odl";
	var params 			= "codice_azienda_wt=" + codice_azienda_wt + "&anno_esercizio=" + anno_esercizio + "&currentFunction=" + currentFunction;
		
	$(formName).disable();
	$(target).update();
	$(loadingName).show();
	
	new Ajax.Request(
			urlAjax, 
			{ 
				method: 'get', parameters: params, evalJS: true, 
				onSuccess: function (transport) 
				{ 
					if ($(formName)) $(formName).enable();
					if ($(loadingName)) $(loadingName).hide();
					$(target).update( transport.responseText );
					if (codice_odl != "") getODL( codice_odl );
				}, 
				onFailure: function (transport) 
				{ 
					alert("oops problemi di recupero dati dal server ajax");
				}
			});	
}

/**
 * Estrae i dettagli di un ODL
 * @param codice_azienda_madre 
 * @param anno_esercizio 
 * @param codice_tipo_documento 
 * @param codice_documento 
 * @param codice_versione_documento
 * @return
 */
function getODL( codice_odl )
{
	// svuota tutti i div "dettaglio_contratto_*" prima di ripopolare quello giusto
	var allDivs = $$('div');	
	allDivs.each(function(i, index) 
	{
		if (i.id.indexOf("dettaglio_odl_") >= 0)
			i.update();
	});
	
	var target 			= "dettaglio_odl_" + codice_odl;
	var loadingName 	= "loading_" + codice_odl;
	var formName		= "documentsView";
	var currentFunction = "getODL";
	var params = "codice_odl=" + codice_odl + "&currentFunction=" + currentFunction;

	if ($(loadingName)) $(loadingName).show();
	if ($(formName))	$(formName).disable();
	
	executeAjaxRequest ( urlAjax, "get", params, true, target, loadingName, formName, "" );	
}

function salvaODL( codice_odl, stato_odl )
{
	var formName    	= "documentsView";
	var loadingName 	= "loading_" + codice_odl;
	var currentFunction = "salvaODL";
	var nota			= "nota_" + codice_odl;
	var params = "codice_odl=" + codice_odl + "&stato_odl=" + stato_odl + "&currentFunction=" + currentFunction;
	
	if ($(nota)) params += "&nota=" + $F(nota);
	if ($(formName)) $(formName).disable();
	if ($(loadingName)) $(loadingName).show();
	
	new Ajax.Request(
			urlAjax, 
			{ 
				method: 'get', parameters: params, evalJS: true, 
				onSuccess: function (transport) 
				{ 
					alert(transport.responseText);
					if ($(formName)) $(formName).enable();
					if ($(loadingName)) $(loadingName).hide();
					listaODL( codice_odl );
				}, 
				onFailure: function (transport) 
				{ 
					alert("oops problemi di recupero dati dal server ajax");
				}
			});	
}

function executeAjaxRequest ( urlAjax, method, params, evalJs, target, loadingName, formName )
{
	new Ajax.Request(
			urlAjax, 
			{ 
				method: method, 
				parameters: params, 
				evalJS: evalJs, 
				onSuccess: function (transport) 
				{ 
					if ($(loadingName)) $(loadingName).hide();
					if (target == "alert")
						alert(transport.responseText);
					else
						$(target).update ( transport.responseText );
					if ($(formName)) $(formName).enable();
					if (functionOnSuccess != "") 			
					{
						functionOnSuccess = functionOnSuccess.split("||");
						for (var i=0; i<=functionOnSuccess.length; i++)
						{
							eval( functionOnSuccess[i] + "('" + transport.responseText + "')" );	
						}											
					}
				}, 
				onFailure: function (transport) 
				{ 
					alert("oops path non trovato: " + urlAjax + " o errore di sintassi nel tpl!!" );
				}
			});
}

/**
 * ESEGUE IL TRACCIAMENTO VIA AJAX DELLE ATTIVITA' SUL SITO WEB
 */
function tracciaAttivita( attivita, cod_obj, desc_obj )
{

	 if (!urlAjax) 
		return false;
	 
	 var currentFunction = "tracciaAttivita";
	 var sParameters     = "currentFunction=" + currentFunction + "&cod_obj=" + cod_obj + "&attivita=" + attivita + "&desc_obj=" + desc_obj;
	 //alert(sParameters);
	 new Ajax.Request(
			 urlAjax, 
				{ 
					method: 'get', 
					parameters: sParameters, 
					onSuccess: function (transport) 
					{ 
						//alert ( transport.responseText );
					}
				});
}