//{{{ FulQuery ()
function FulQuery ()
{
	this.mode = '';
	this.db_name = '';
	this.resource = '';
	this.order_by = '';
	this.group_by = '';
	this.highlight = false;
	this.opera = '';
	this.operasez = '';
	this.tail_where = '';  // Aggiunge una stringa alla fine delle condizioni WHERE
	this.distinct = 0;
	this.skip_text_status = true; // Falg T/F. Se T, setta _X_SKIP_TEXT_STATUS

	this.key_overwrite = true;  // Flag T/F. Se T, sovrascrive i campi di fq.add()

	// <VerQuery specific>
	this.bucket = '';
	this.buckets = '';
	// </VerQuery specific>

	this.lines = 10;
	this.page  = 0;

	this.clear 	= fulquery_clear;
	this.add	= fulquery_add;
	this.query	= fulquery_query;
	this.add_date	= fulquery_add_date;
	this.del	= fulquery_del;
	this.fill 	= fulquery_fill;
	this.set_fields = fulquery_set_fields;
	this.set_fields_by_list = fulquery_set_fields_by_list;
	this.set_biblio = fulquery_set_biblio;
	this.corr_search = fulquery_corr_search;
	this.doc_corr	 = fulquery_doc_corr;

	this.biblio = {
		ckey  : null,
		user  : null,
		field : null,
		mode  : null		
	};

	this.corr = {
		key : null,
		dbname : null,
		tipo : null,
		art : null
	};

	this.set_id 	= fulquery_set_id;

	this._conds = [];
	this._fields = null;
	this._doc_text = '';
	this._id1 = '';
	this._id2 = '';
	this._query_full = '';

	// Variabili di supporto per il Multi Query
	this._multi = [];
}
//}}}


function fulquery_set_biblio ( ckey, dbuser, dbfield, mode )
{
	this.biblio.ckey  = ckey;
	this.biblio.user  = dbuser;
	this.biblio.field = dbfield;
	this.biblio.mode  = mode;
}

function fulquery_corr_search ( key, tipo, dbname )
{
	this.mode = "SEARCH_CORR";
	
	this.corr.key = key;
	this.corr.tipo = tipo;
	this.corr.dbname = dbname;
}

function fulquery_doc_corr ( key, art )
{
	this.mode = "DOC_CORR";
	
	this.corr.key = key;
	this.corr.art = art;
}



//{{{ fulquery_clear ()
function fulquery_clear ()
{
	this.mode = '';
	this.db_name = '';
	this.order_by = '';
	this.group_by = '';
	this.distinct = 0;
	this.resource = '';
	this.highlight = false;
	this.page = 0;
	this.lines = 10;

	this._conds  = [];
	this._fields = [];
	this._doc_text = '';
	this._id1 = '';
	this._id2 = '';

	this.biblio.ckey  = null;
	this.biblio.user  = null;
	this.biblio.field = null;
	this.biblio.mode  = null;

	this.corr.key = null;
	this.corr.dbname = null;
	this.corr.tipo = null;
}
//}}}
//{{{ fulquery_add ( field, mode, value, db_field, prepend, append, base )
/*
	field		-> nome del campo (es. "NATURA")
	mode		-> modalita' di ricerca (es. "IN_STR")
	value		-> valore da ricercare ( es. 'L.|D.Lgs.|DM.' )
	db_field	-> campo del db associato al field (di default == field, es. "DATAGU")
	prepend		-> stringa da prependere al valore 
	append		-> stringa da appendere al valore
	base		-> base dei campi data [NOTA: solo per campi data] (es. "GU" per "GUGIORNO1" ... )
*/
function fulquery_add ( field, mode, value, db_field, prepend, append, base )
{
	mode = mode.toUpperCase ();

	if ( this.key_overwrite ) this.del ( field );

	this._conds.push ( [ field, mode, value, db_field, prepend, append, base ] );

	if ( field.toUpperCase () == 'DOCUMENT_TEXT' )
		this._doc_text = value;
}
//}}}

function fulquery_query ( sql )
{
	this._query_full = sql;
}

//{{{ fulquery_add_date ( anno1, mese1, giorno1, anno2, mese2, giorno2, db_field, prepend, append, base )
function fulquery_add_date ( anno1, mese1, giorno1, anno2, mese2, giorno2, db_field, prepend, append, base )
{
	if ( ! base ) base = "";
	this._conds.push ( [ base + "ANNO1", "DATA", anno1, db_field, prepend, append, base ] );
	this._conds.push ( [ base + "MESE1", "DATA", mese1, db_field, prepend, append, base ] );
	this._conds.push ( [ base + "GIORNO1", "DATA", giorno1, db_field, prepend, append, base ] );
	this._conds.push ( [ base + "ANNO2", "DATA", anno2, db_field, prepend, append, base ] );
	this._conds.push ( [ base + "MESE2", "DATA", mese2, db_field, prepend, append, base ] );
	this._conds.push ( [ base + "GIORNO2", "DATA", giorno2, db_field, prepend, append, base ] );
}
//}}}
//{{{ fulquery_del
function fulquery_del ( field )
{
	var i, l = this._conds.length;
	for ( i = 0; i < l; i ++ )
	{
		if ( this._conds [ i ] [ 0 ] == field )
		{
			this._conds.splice ( i, 1 );
			break;
		}
	}
}
//}}}
//{{{ fulquery_fill ( dst )
function fulquery_fill ( dst, prefix )
{
	var t, l;
	var cond;
	var s = '';
	var name;

	if ( ! prefix ) 
		prefix = "";
	else
		this._multi.push ( prefix );


	if ( ! this.opera )
	{
		console.error ( "[FulQuery]: manca l'opera !!!" );
		alert ( "[FulQuery]: manca l'opera !!!" );
		return;
	}

	// FIX: add prefix in already added fields
	/*
	if ( prefix )
	{
		var dst2 = dst.clone ();
		dst2.iterate ( function ( v, k ) { dst [ prefix + k ] = v; delete dst [ k ]; } );
	}
	*/
	// END FIX

	l = this._conds.length;

	// NEW: Contatore per campi duplicati
	var counters = {};	

	for ( t = 0; t < l; t ++ )
	{
		cond = this._conds [ t ];

		if ( typeof ( cond [ 2 ] ) == 'undefined' ) continue;

		// FIX: skippiamo anche i campi che hanno valore a ''
		//      forse questo ha un impatto su altri siti
		if ( cond [ 2 ] == '' ) continue;

		name = cond [ 0 ];

		count = counters.get ( name, 0 );
		counters [ name ] = count + 1;

		if ( count != 0 )
		{
			dst [ prefix + "_D_" + count + "_" + name ] = cond [ 2 ];
			dst [ prefix + "_M_" + count + "_" + name ] = cond [ 1 ];

			if ( cond [ 4 ] ) dst [ prefix + "_P_" + count + "_" + name ] = cond [ 4 ];
			if ( cond [ 5 ] ) dst [ prefix + "_A_" + count + "_" + name ] = cond [ 5 ];
		} else {
			// Condizione iniziale (count == 0), funziona come prima
			dst [ prefix + name ] = cond [ 2 ];

			dst [ prefix + "_M_" + name ] = cond [ 1 ];

			if ( cond [ 3 ] ) dst [ prefix + "_F_" + name ] = cond [ 3 ];
			if ( cond [ 4 ] ) dst [ prefix + "_P_" + name ] = cond [ 4 ];
			if ( cond [ 5 ] ) dst [ prefix + "_A_" + name ] = cond [ 5 ];
			if ( cond [ 6 ] ) dst [ prefix + "_B_" + name ] = cond [ 6 ];
		}
	}

	if ( ! this._fields ) this._fields = [];
	l = this._fields.length;
	for ( t = 0; t < l; t ++ )
	{
		if ( ! this._fields [ t ] ) continue;
		s += this._fields [ t ] + ":";
	}

	s = s.substr ( 0, s.length -1 );

	dst [ prefix + '_X_SEARCH_FIELDS' ] = s;
	dst [ prefix + '_X_MODE' ] = this.mode;
	dst [ prefix + '_X_LINES' ] = this.lines;
	dst [ prefix + '_X_PAGE' ] = this.page;
	dst [ prefix + '_X_DBNAME' ] = this.db_name;
	dst [ prefix + '_X_RESOURCE' ] = this.resource;
	dst [ prefix + '_X_FULL' ] = this._query_full;
	dst [ prefix + '_X_OPERA' ] = this.opera;

	if ( this.biblio.ckey )
	{
		dst [ prefix + '_X_BIBLIO' ] = this.biblio.ckey;
		dst [ prefix + '_X_BIBLIO_DBUSER' ] = this.biblio.user;
		dst [ prefix + '_X_BIBLIO_DBFIELD' ] = this.biblio.field;
		dst [ prefix + '_X_BIBLIO_MODE' ] = this.biblio.mode;
	}

	if ( this.mode == 'SEARCH_CORR' )
	{
		dst [ prefix + '_X_CORR_KEY' ] = this.corr.key;
		dst [ prefix + '_X_CORR_DBNAME' ] = this.corr.dbname;
		dst [ prefix + '_X_CORR_TIPO' ] = this.corr.tipo;
	}

	if ( this.mode == 'DOC_CORR' )
	{
		dst [ prefix + 'KEY_DOC' ] = this.corr.key;
		dst [ prefix + 'ART' ] = this.corr.art;
	}

	if ( this.skip_text_status ) dst [ prefix + '_X_SKIP_TEXT_STATUS' ] = 1;

	if ( this.operasez ) dst [ prefix + '_X_OPERASEZ' ] = this.operasez;

	if ( this.bucket ) dst [ prefix + '_X_BUCKET' ] = this.bucket;
	if ( this.buckets ) dst [ prefix + '_X_BUCKETS' ] = this.buckets;

	if ( this._id1 )
	{
		dst [ prefix + '_X_ID1' ] = this._id1;
		dst [ prefix + '_X_ID2' ] = this._id2;
	}

	if ( this.tail_where ) dst [ prefix + '_X_TAIL_WHERE' ] = this.tail_where;
	if ( this.group_by ) dst [ prefix + '_X_GROUP_BY' ] = this.group_by;
	if ( this.order_by ) dst [ prefix + '_X_ORDER_BY' ] = this.order_by;
	if ( this.highlight && this._doc_text ) dst [ prefix + '_X_HIGHLIGHT' ] = this._doc_text;
	if ( this.distinct ) dst [ prefix + '_X_DISTINCT' ] = "1";

	// Se multi e' settato, aggiungo _X_MULTI
	if ( this._multi.length ) dst [ '_X_MULTI' ] = ':'.join ( this._multi );

	// Cancello i dati in memoria
	if ( prefix ) this.clear ();
}
//}}}
//{{{ fulquery_set_fields ()
function fulquery_set_fields ()
{
	this._fields = arguments;
}
//}}}
// {{{ fulquery_set_fields_by_list ( lst )
function fulquery_set_fields_by_list ( lst )
{
	this._fields = lst; //.clone ();
}
// }}}

function fulquery_set_id ()
{
	var d = new Date ();
	
	this._id1 = d.getTime ().toString ();
	this._id2 = MD5.hex_md5 ( this._id1 );
}

