
function openURL(selected_row)
{
    window.location = selected_row;
    return true;
}


/* returns a function that prompts the user with the given message,
and if it gets "OK" it moves to the URL. Set the row_activate parameter 
to be the result of calling this function, i.e., 

your_specific_format.row_activate = makeConfirmMessage("Delete this record?");
*/
function makeConfirmFunction(message)
{
	return function (selected_row) 
	{
		var result = window.confirm(message);
		if (result)
		{
			window.location = selected_row;
			return true;
		}
		else
		{
			return false;
		}
	}
}	

function makePopupFunction(name, config)
{
	return function openURLInPopup(selected_row)
	{
		openPopup(selected_row, name, config);
	    return false;
	}
}


function statExceptionCount(table, el)
{
	el.innerHTML = table.GetTotalSize() + " " + table.GetRowTerm(table.GetTotalSize()) + " (" + table.accumulators['exception'].count + " in exception)";
}

/* Returns an array of functions, one for each status bar panel. */
function SimpleStatus(table)
{
	return [ statTotalRecordCount, statFilteredRecordCount ];
}

/* Returns an array of functions, one for each status bar panel. */
function SampleStatus(table)
{
	return [ statExceptionCount, statFilteredRecordCount];
}

/* Returns an array of functions, one for each status bar panel. */
function MaintCenterStatus(table)
{
	return [ statTotalRecordCount, statFilteredRecordCount ];
}


var base_format = 
{ 
	'sorted_column' : 0,  // the column which is sorted initially
	'ascending' : true,  // true if the initial order is ascending
	'unique_row_styles' : 2, // the number of rows that get a unique style (2 means alternating stripes)
	'rows_per_page' : 10,   // the number of rows to show at one time
	'row_activate' : openURL,
	'table_classes' : 'data-table',   // the class(es) assigned to the table
	'sortable' : true,         // indicates the column headers should be sortable
	'show_filters' : false,         // show filters?
	'show_scrollbar' : true,   // show the scroll buttons for moving to different pages?
	'show_status_bar' : true,      // show the status bar at the bottom?
	'status_function': SimpleStatus, // should return an array of functions, each of which produces a status-bar item

	// indicates the color of this list.  The classes COLOR-list is added
	'color' : 'blue',
	
	// if true, items on the list can be selected and the classes selectable and COLOR-list-selectable are added
	'selectable' : true,

	// if true and selectable is true, this list supports multiple selection
	'multiselect' : false,

	// true if the current row should not be affected by user selections 
	// (if false, deselecting the row that is current will make it no longer current)
	'fixed_current_row' : false, 

	// a mapping of objects that gather information about the data 
	// (with a required accumulate(row) and reset() function)
	'accumulators' : {}, 
	
	// the element that is the highest element that should be resized
	// horizontally.  In other words, all elements between the datatable and this element (inclusive)
	// will be resized horizontally.
	'horizontal_sized_element_id' : 'window_div',  
	// the element whose vertical size should be a bound on the vertical size of this class.
	// this element is not resized (like horizontal_sized_element_id).  Instead, its existing
	// size is how we determine how big our datatable can be.  This should be the element that is
	// sized vertically with CSS to to a certain size (or a certain percentage), since normally block-level
	// elements only expand as big as their contents.  This table will then constrain itself to the size
	// of this element.
	'vertical_bound_element_id' : 'mainpanel',
	'min_width' : 500,
	'row_term_singular' : 'item',
	'row_term_plural' : 'items',
	// indicates that the state for this table should be saved and restored between runs
	'preserve_state' : true
}


var simple_format = cloneObject(base_format);

var full_format = cloneObject(base_format);
full_format.show_filters = true;

var edit_format = cloneObject(base_format);
edit_format.show_filters = true;
edit_format.editing = true;
edit_format.sortable = false;
edit_format.show_filters = false;
edit_format.preserve_state = false;

// ********** specific  formats below **********

var support_users_format = cloneObject(full_format);
support_users_format.color = "yellow";
support_users_format.layout = "support_user";

var accounts_format = cloneObject(base_format);
accounts_format.show_filters = true;
accounts_format.color = "violet";
accounts_format.layout = "account";

var account_users_format = cloneObject(base_format);
account_users_format.show_filters = true;
account_users_format.color = "magenta";
account_users_format.layout = "account_user";

var mail_templates_format = cloneObject(base_format);
mail_templates_format.color = "cyan";
mail_templates_format.layout = "mail_template";

var equipment_format = cloneObject(full_format);
equipment_format.layout = "equipment";

var audited_accounts_format = cloneObject(full_format);
audited_accounts_format.color = "violet";
audited_accounts_format.layout = "audited_account";

var audit_entries_format = cloneObject(full_format);
audit_entries_format.color = "gray";
audit_entries_format.layout = "audit_entry";


var samples_format = cloneObject(full_format);
samples_format.status_function = SampleStatus;
samples_format.accumulators['exception'] = new ColumnCounter("exception", "E1");
samples_format.multiselect = true;
samples_format.color = "blue";
samples_format.layout = "sample";

var sample_points_format = cloneObject(samples_format);
sample_points_format.color = "green";
sample_points_format.layout = "sample_point";


var batch_format = cloneObject(full_format);
batch_format.color = "orange";
batch_format.layout = "batch";

var batch_support_format = cloneObject(full_format);
batch_support_format.color = 'orange';
batch_support_format.layout = "batch_support";

var month_format = cloneObject(full_format);
month_format.color = "yellow";
month_format.layout = "sampled_month";

var graph_format = cloneObject(base_format);
graph_format.color = "yellow";
graph_format.layout = "graph";

var messages_format = cloneObject(full_format);
messages_format.color = "gray";
messages_format.layout = "message";
messages_format.row_activate = makePopupFunction('detail', "width=400, height=200, scrollbars, resizable");

var alarm_distribution_format = cloneObject(full_format);
alarm_distribution_format.color = "cyan";
alarm_distribution_format.layout = "alarm_distribution";
alarm_distribution_format.multiselect = true;
alarm_distribution_format.row_activate = returnFalse;
alarm_distribution_format.fixed_current_row = true;
// these are different since it is in a popup
alarm_distribution_format.horizontal_sized_element_id = 'horizontal_container';
alarm_distribution_format.vertical_bound_element_id ='popup_container';

var maintcenters_format = cloneObject(full_format);
maintcenters_format.color = "red";
maintcenters_format.layout = "maintcenter";

var maintcenters_edit_format = cloneObject(edit_format);
maintcenters_edit_format.status_function = MaintCenterStatus;
maintcenters_edit_format.color = "red";
maintcenters_format.layout = "maintcenter";
// these are different since it is in a popup
maintcenters_edit_format.horizontal_sized_element_id = 'horizontal_container';
maintcenters_edit_format.vertical_bound_element_id ='popup_container';
maintcenters_edit_format.multiselect = true;

