function build_autocompleter(id,table,id_field,desc_field,filterkey,filterval,distinct,afterFunc){
	new Ajax.Autocompleter(id+'_tx',id+'_ac','/intranet/ajax.php?op=autocompleter&table='+table+'&id_field='+id_field+'&desc_field='+desc_field+'&distinct='+distinct+'&filterkey='+filterkey+'&filterval='+filterval+'&html_id='+id, { 
		paramName: 'value', 
		minChars: 2,
		afterUpdateElement : eval(afterFunc)
	});
}

function build_multicompleter(id,table,id_field,desc_field){
	var old = $(id+'_old');
	old.parentNode.removeChild(old);
	tlist2 = new FacebookList(id+'_tx', id+'_ac');
	// fetch and feed
	new Ajax.Request('/intranet/ajax.php?op=multicompleter&table='+table+'&id_field='+id_field+'&desc_field='+desc_field, {
	    onSuccess: function(transport) {
	        transport.responseText.evalJSON(true).each(function(t){
	        	tlist2.autoFeed(t)
	        });
	    }
	});
}
function getSelectionId(text, li) {
	id  = li.id.split("|")[0];
	val = li.id.split("|")[1];
	$(id).value = val
}
function Noop(text, li){
	return true;
}
function proto_fill_art(text,li){
	var id  = li.id.split("|")[0];
	var val = li.id.split("|")[1];
	var idx = id.split("_")[(id.split("_").length-1)];
	if (li.innerHTML=="Nuovo..."){
		window.open(val, 'NuovoArticolo', 'width=1024,height=768,scrollbars=yes,resizable=yes');
	}else{
		if ((val!=0) && (idx>=0)) {
			var tofill = document.getElementById('res_'+idx);
			if (tofill!=undefined) tofill.value = val;
		}
		
		var query = "select a.alpha_cod,a.descrizione,a.cod_fornitore from mag_articoli as a where id_articoli="+val;
		new Ajax.Request('/intranet/ajax.php?op=query&query='+query, {
			onSuccess: function(transport){
				eval(transport.responseText);
				if ($('note_'+idx+'_tx') != undefined)
					$('note_'+idx+'_tx').value = result['1']['descrizione'];
				if ($('codfor_'+idx+'_tx') != undefined)
					$('codfor_'+idx+'_tx').value = result['1']['cod_fornitore'];
				if ($('cod_fornitore_id_'+idx+'_tx') != undefined)
					$('cod_fornitore_id_'+idx+'_tx').value = result['1']['cod_fornitore'];
				if ($('alpha_'+idx+'_tx') != undefined)
					$('alpha_'+idx+'_tx').value = result['1']['alpha_cod'];
				if ($('alpha_cod_'+idx+'_tx') != undefined)
					$('alpha_cod_'+idx+'_tx').value = result['1']['alpha_cod'];

			}
		});
	}
	return false;
}
function proto_calcola_prezzo(text,li){
	var id  = li.id.split("|")[0];
	var val = li.id.split("|")[1];
	var idx = id.split("_")[1];
	if (li.innerHTML=="Nuovo..."){
		window.open(val, 'NuovoArticolo', 'width=1024,height=768,scrollbars=yes,resizable=yes');
	}else{
		if ((val!=0) && (idx>=0)) {
			var tofill = document.getElementById('res_'+idx);
			if (tofill!=undefined) tofill.value = val;
		}
		
		if (document.getElementById('fornitore_'+idx) != undefined)
			var fornitore = document.getElementById('fornitore_'+idx).value;
		else if (document.getElementById('fornitore') != undefined)
			var fornitore = document.getElementById('clifor_ls_tx').value;
		else var fornitore = 0;
			
		if (document.getElementById('qta_'+idx) != undefined)
			var qta = document.getElementById('qta_'+idx).value;
		else var qta = 1;
		
		if (document.getElementById('gm_'+idx) != undefined)
			var gm = document.getElementById('gm_'+idx).value;
		else var gm = 0;
		
		if (document.getElementById('sconto_'+idx) != undefined)
			var sconto_acquisto = document.getElementById('sconto_'+idx);
		else var sconto_acquisto = 0;
		
		if (document.getElementById('sconto_val_'+idx) != undefined)
			var sconto_acquisto_val = document.getElementById('sconto_val_'+idx);
		else var sconto_acquisto_val = 0;
	
		if (document.getElementById('prezzo_'+idx) != undefined)
			var prezzo_vendita = document.getElementById('prezzo_'+idx);
		else var prezzo_vendita = 0;
	
		if (document.getElementById('listino') != undefined)
			var listino = document.getElementById('listino').value;
		else var listino = 'aq';
		
		if (document.getElementById('clifor') != undefined)
			var clifor = document.getElementById('clifor').value;
		else var clifor = 0;
	
		
		if (document.getElementById('deposito_0') != undefined)
			var deposito = document.getElementById('deposito_0').value;
		else if (document.getElementById('deposito') != undefined)
			var deposito = document.getElementById('deposito').value;
		else var deposito = 0;	
		
		var sconto_q = "(select sconto from mag_sconti_anagrafica where anagrafica_id=ang.id_anagrafica and famiglia_id IN (0,a.famiglia_id) ORDER BY famiglia_id DESC limit 1) as sconto";
		var sconto_vq = "(select sconto_val from mag_sconti_anagrafica where anagrafica_id=ang.id_anagrafica and famiglia_id IN (0,a.famiglia_id) ORDER BY famiglia_id DESC limit 1) as sconto_val";
			
		if (listino!='aq') {
			var query = "select a.id_articoli,a.alpha_cod,a.descrizione,a.cod_fornitore,IF(ang.tipo_azienda='E',1, IF(ang.tipo_azienda='X',2,a.cod_iva_id)) as cod_iva_id,a.conto_id,a.conto_costo_id,m.prezzo_vendita as prezzo,"+sconto_q+","+sconto_vq+" from mag_articoli as a left join mag_prezzo as m on (a.id_articoli=m.articolo_id and m.listino_id='"+listino+"') left join ang_anagrafica as ang on (ang.id_anagrafica='"+clifor+"') where id_articoli="+val;
		} else {
			var query = "select a.id_articoli,a.alpha_cod,a.descrizione,a.cod_fornitore,IF(ang.tipo_azienda='E',1, IF(ang.tipo_azienda='X',2,a.cod_iva_id)) as cod_iva_id,a.conto_id,a.conto_costo_id,m.prezzo_listino as prezzo,sconto_for as sconto,sconto_val from mag_articoli as a left join mag_lista_for as m on (a.id_articoli=m.articolo_id and m.fornitore_id='"+clifor+"') left join ang_anagrafica as ang on (ang.id_anagrafica='"+clifor+"') where id_articoli="+val;
		}
		alert(query);
		new Ajax.Request('/intranet/ajax.php?op=query&query='+query, {
			onSuccess: function(transport){
				eval(transport.responseText);
				if (document.getElementById('res_'+idx) != undefined)
					document.getElementById('res_'+idx).value = result['1']['id_articoli'];
				if (document.getElementById('note_'+idx+'_tx') != undefined)
					document.getElementById('note_'+idx+'_tx').value = result['1']['descrizione'];
				if (document.getElementById('codfor_'+idx+'_tx') != undefined)
					document.getElementById('codfor_'+idx+'_tx').value = result['1']['cod_fornitore'];
				if (document.getElementById('alpha_'+idx+'_tx') != undefined)
					document.getElementById('alpha_'+idx+'_tx').value = result['1']['alpha_cod'];
				if (document.getElementById('iva_'+idx) != undefined)
					document.getElementById('iva_'+idx).value = result['1']['cod_iva_id'];
				if (document.getElementById('conto_fatturazione_'+idx) != undefined)
					document.getElementById('conto_fatturazione_'+idx).value = result['1']['conto_id'];
				if (document.getElementById('conto_costo_'+idx) != undefined)
					document.getElementById('conto_costo_'+idx).value = result['1']['conto_costo_id'];
				if (document.getElementById('prezzo_'+idx) != undefined)
					document.getElementById('prezzo_'+idx).value = result['1']['prezzo'];
				if (document.getElementById('sconto_'+idx) != undefined)
					document.getElementById('sconto_'+idx).value = result['1']['sconto'];
				if (document.getElementById('sconto_val_'+idx) != undefined)
					document.getElementById('sconto_val_'+idx).value = result['1']['sconto_val'];
			}
		});
		dump_tot_row(idx);
	}
	
}

function proto_add_clifor(text,li){
	var id   = li.id.split("|")[0];
	var val  = li.id.split("|")[1];
	$('clifor').value = val;
	if (li.innerHTML=="Nuovo..."){
		window.open(val, 'NuovoCliFor', 'width=1024,height=768,scrollbars=yes,resizable=yes');
	}else{
		if ((gup('file')=='fatturazione') || (gup('file')=='movimento')){ 
			var spec = document.getElementById('spec').value;
			if (spec=='C'){
				var query = 'SELECT id_anagrafica,listino_id,mod_pagamento_id,agente_id,conto_id FROM ang_anagrafica WHERE id_anagrafica = ' + val + ' and status = 1'
			}else{
				var query = 'SELECT id_anagrafica,mod_pagamento_id,conto_id,CONCAT(IFNULL(giorni_consegna,0)," giorni") as scadenze FROM ang_anagrafica WHERE id_anagrafica = ' + val + ' and status = 1'
			}
			new Ajax.Request('/intranet/ajax.php?op=query&query='+query, {
				onSuccess: function(transport){
					eval(transport.responseText);
					if ((result['1']['listino_id']!=null) && ($('listino')!=undefined)){
						$('listino').value = result['1']['listino_id'];
					}
					if ((result['1']['mod_pagamento_id']!=null) && ($('pagamento')!=undefined)){
						$('pagamento').value = result['1']['mod_pagamento_id'];
						open_banche(result['1']['mod_pagamento_id'], result['1']['conto_id']);
					}
					if ((result['1']['agente_id']!=null) && ($('agente_id')!=undefined)){
						$('agente_id').value = result['1']['agente_id'];
					}
					if ((result['1']['scadenze']!=null) && ($('scadenze')!=undefined)){
						$('scadenze').value = result['1']['scadenze'];
					}
					if ((result['1']['id_anagrafica']!=null) && ($('dest')!=undefined)){
						new Ajax.Updater('altdest', 'index.php?module='+gup('module')+'&file='+gup('file')+'&act=destinazioni&id='+result['1']['id_anagrafica']);
					}
				}
			});
		}
	}
}