/**
 * @author Arnaud Métral <arnaud.metral@evonux.com>
 * @version 1.0
 * @copyright Evonux 2009
 *
 * Evonux.List.js
 *
 */

Evonux.List = {}

/**
 * Set selected style to line if checkbox is checked in line
 */
Evonux.List.checkItem = function (item, form)
{
    cell = item.getParent ();
    row = cell.getParent ();
    if (!item.checked)
    row.removeClass ("selected");
    else
    row.addClass ("selected");
    Evonux.List.checkIds (form);
}


/**
 * Invert all items selection
 */
Evonux.List.checkItems = function (form)
{    
    var         t1 = $$ ("input.chk");

    for (var i = 0; i < t1.length; i++)
    {
        var item = t1[i];

        if (item.id != "chk-all")
            {
		item.checked = $('chk-all').checked && !item.checked;
		Evonux.List.checkItem (item, form);
	    }
    }
    Evonux.List.checkIds (form);
}

/**
 * Update hidden oids input with real check state of checkboxes
 */
Evonux.List.checkIds = function (form)
{
    var         t1 = $$ ("input.chk");
    var         ids = "";

    for (var i = 0; i < t1.length; i++)
    {
        var item = t1[i];

        if (item.id != "chk-all")
            {
                if (item.checked)
                    {
			var id = item.id;
                        ids += id.substring (4, id.length) + ",";
                    }
            }
    }
    $(form).oids.value = ids.substring (0, ids.length - 1);
}

/**
 * Change module action value (in order to post)
 */
Evonux.List.changeState = function (select, prefix, label)
{
    var		frm = $('frm-' + prefix);

    if (select.selectedIndex && frm.oids.value)
    {
	if (confirm (label))
	{
	    Evonux.Page.get ("module=" + frm.module.value + "&action=" + select.value + "&oid=" + frm.oids.value, "", function () { Evonux.List.reloadList (prefix); });
	}
    }
    select.selectedIndex = 0;
}

/**
 * Reload a sorted list (grid)
 */
Evonux.List.reloadList = function (prefix, params)
{
    var		frm = $('frm-' + prefix);

    Evonux.Page.get ('module=' + frm.module.value + '&action=getlist&update=1' + (params ? params : ''), 'list-' + prefix);
}

