//{{{ FulQuery ()
function FulQuery ()
{
	this.mode = '';
	this.db_name = '';
	this.resource = '';
	this.order_by = '';
	this.group_by = '';
	this.highlight = false;
	this.opera = '';
	this.operasez = '';

	// <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_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 = [];
}
//}}}
//{{{ fulquery_clear ()
function fulquery_clear ()
{
	this.mode = '';
	this.db_name = '';
	this.order_by = '';
	this.group_by = '';
	this.resource = '';
	this.highlight = false;
	this.page = 0;
	this.lines = 10;

	this._conds  = [];
	this._fields = [];
	this._doc_text = '';
	this._id1 = '';
	this._id2 = '';
}
//}}}
//{{{ 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 ();
	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 = '';

	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;

	for ( t = 0; t < l; t ++ )
	{
		cond = this._conds [ t ];
		
		dst [ prefix + cond [ 0 ] ] = cond [ 2 ];
		dst [ prefix + "_M_" + cond [ 0 ] ] = cond [ 1 ];

		if ( cond [ 3 ] ) dst [ prefix + "_F_" + cond [ 0 ] ] = cond [ 3 ];
		if ( cond [ 4 ] ) dst [ prefix + "_P_" + cond [ 0 ] ] = cond [ 4 ];
		if ( cond [ 5 ] ) dst [ prefix + "_A_" + cond [ 0 ] ] = cond [ 5 ];
		if ( cond [ 6 ] ) dst [ prefix + "_B_" + cond [ 0 ] ] = 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.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.order_by ) dst [ prefix + '_X_ORDER_BY' ] = this.order_by;
	if ( this.group_by ) dst [ prefix + '_X_GROUP_BY' ] = this.group_by;
	if ( this.highlight && this._doc_text ) dst [ prefix + '_X_HIGHLIGHT' ] = this._doc_text;

	// 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 );
}
