///-------------------------------------------------------------------------
//  @project    core
//  @version    1.9
//  @author     Niel Astle
//  @company    Liquid Edge Solutions
//	@copyright  Copyright Liquid Edge Solutions. All rights reserved.
//  @desc       Client side user interface for list component
///-------------------------------------------------------------------------
// classes
///-------------------------------------------------------------------------
var com_list = function(the_cid, the_count, the_url, the_navigation) { // com_list constructor
	/// init
	this.cid = the_cid; // unique component id
	this.navigation = the_navigation; // navigation type: page, alpha
	this.block = $(this.cid + '_block'); // container
	this.requests = $('r' + this.cid); // request parameters
	this.url = the_url; // the base url to prepend
	this.count = the_count; // the amount of items in the list
	this.checkboxes = new Array(); // array with checkbox elements
  	for (var i = 0; i < this.count; i++) this.checkboxes[i] = $(this.cid + '_chk[' + i + ']');

  	// block ids
  	this.id_navtop = $(this.cid + '_navtop');
  	//this.id_navbottom = $(this.cid + '_navbottom');
  	this.id_list = $(this.cid + '_list');
  	this.id_addnew = $(this.cid + '_addnew');
}

///-------------------------------------------------------------------------
// prototypes
///-------------------------------------------------------------------------
com_list.prototype = { // com_list prototype
	///-------------------------------------------------------------------------
	set_request: function(the_nr, the_value) { // sets a request parameter
	  	var request = this.requests.value.split('.');
	  	request[the_nr] = the_value;
	  	request = request.join('.');
	  	$$getvalue(this.requests, request);
	  	return ('&z=' + request);
	},

	///-------------------------------------------------------------------------
	send: function(the_post, the_clear) { // send ajax query
	  	if (the_post == null) the_post = '';
	  	if (!the_clear) {
		  	if (document.forms[this.cid + '_advfilter_form']) {
			    temp_post = core.form.toUrl(this.cid + '_advfilter_form');
			 	if (temp_post) the_post += '&' + temp_post;
			}
		}
	  	core.ajax.requestUpdate(this.url + the_post, this.block);
	},

	///-------------------------------------------------------------------------
	refresh: function() {
	  	core.ajax.requestUpdate(this.url + '&z=' + this.requests.value, this.block);
	},

	///-------------------------------------------------------------------------
	reset: function() { // reset list
	  	this.send('&z=reset', true);
	},

	///-------------------------------------------------------------------------
	quickfind: function(the_nr, the_reset) { // submit quickfind request
		var the_name = this.cid + '_quickfind' + the_nr;
		if (the_reset) $$getvalue(the_name, '');
		if (this.navigation == 'page') this.set_request(2, 0);
		this.send(this.set_request(6, encodeURIComponent($$getvalue(the_name))));
	},

	///-------------------------------------------------------------------------
	filter_date: function(the_reset) { // submit date filter request
		var e_datefrom = $(this.cid + '_datefrom');
		var e_dateto = $(this.cid + '_dateto');

		if (the_reset) {
			$$getvalue(e_datefrom, '');
			$$getvalue(e_dateto, '');
		}

		if (this.navigation == 'page') this.set_request(2, 0); // reset page offset
		this.set_request(4, encodeURIComponent($$getvalue(e_datefrom)));
		var request = this.set_request(5, encodeURIComponent($$getvalue(e_dateto)));
		this.send(request);
	},

	///-------------------------------------------------------------------------
	page: function(the_offset) { // submit page change request
		this.send(this.set_request(2, the_offset));
	},

	///-------------------------------------------------------------------------
	order: function(the_field, the_type) { // submit order change request
		this.set_request(0, the_field)
		this.send(this.set_request(1, the_type));
	},

	///-------------------------------------------------------------------------
	check_all: function() { // checks all checkboxes
	  	for (var i = 0; i < this.count; i++) this.checkboxes[i].checked = true;

		// check all "check-all" boxes
	  	var i = 0;
		var the_element = $(this.cid + '_chkall' + i);
		while (the_element) {
		  	the_element.checked = true;
		  	i++;
		  	the_element = $(this.cid + '_chkall' + i);
		}
	},

	///-------------------------------------------------------------------------
	uncheck_all: function() { // unchecks all checkboxes
	  	for (var i = 0; i < this.count; i++) this.checkboxes[i].checked = false;

		// uncheck all "check-all" boxes
	  	var i = 0;
		var the_element = $(this.cid + '_chkall' + i);
		while (the_element) {
		  	the_element.checked = false;
		  	i++;
		  	the_element = $(this.cid + '_chkall' + i);
		}
	},

	///-------------------------------------------------------------------------
	toggle: function(header_nr) { // toogle checkboxes checked or unchecked
		if (header_nr == null) header_nr = 0;
	  	var the_element = $(this.cid + '_chkall' + header_nr);
	  	if (the_element.checked) this.check_all();
	  	else this.uncheck_all();
	},

	///-------------------------------------------------------------------------
	advanced_filter: function(the_clear) {
	  	if (the_clear) core.ajax.requestUpdate(this.url + '&z=' + this.requests.value + '&clear' + this.cid + '=1', this.block);
	  	else core.ajax.requestUpdate(this.url + '&z=' + this.requests.value, this.block, { form: this.cid + '_advfilter_form' });
	},

	///-------------------------------------------------------------------------
	show_addnew: function() {
		cr.element.hide(this.id_navtop);
		//cr.element.hide(this.id_navbottom);
		cr.element.hide(this.id_list);
		cr.element.show(this.id_addnew);
	},

	///-------------------------------------------------------------------------
	hide_addnew: function() {
		cr.element.hide(this.id_addnew);
		cr.element.show(this.id_navtop);
		//cr.element.show(this.id_navbottom);
		cr.element.show(this.id_list);
	}

	///-------------------------------------------------------------------------
}

///-------------------------------------------------------------------------
