/*
auto table contains 3 arrays - 
keys - ie the db table field names, (which we require to index the data arrays)
master - data as sent from server
local - data which has been filtered from master via search criteria

in order to fill these 3 arrays the php must respond to a request with ClassToJSON (return keys followed by data) followed by a data complete message.
on receipt of this response the js must call
for the keys - fill_keys_array(obj);
for each db record - add_record(obj);
on the list complete message - display_html_table();
display_html_table must also be called after each change of filtering

mousedown events must be directed to the relevant table_mousedown handler.
mousedown_display will automatically highlight the selected row, set the selected_index and set selectedID to ar_filtered[selected_index]

you can click on a record in the table to fill & display the details panel.
the auto_table.ar_detail_titles is what appears in bold alongside the record details - 
ie NAME: marc lacome



*/

var hilite_colour = '#ffcc66';


auto_table.prototype = new auto_table;
auto_table.prototype.init = auto_table_init;
auto_table.prototype.display_html_table = auto_table_display_html_table;
auto_table.prototype.fill_keys_array = auto_table_fill_keys_array;
auto_table.prototype.add_record = auto_table_add_record;
auto_table.prototype.mousedown = auto_table_mousedown;
auto_table.prototype.mousedown_display = auto_table_mousedown_display;
auto_table.prototype.deselect = auto_table_deselect;
auto_table.prototype.add_header = auto_table_add_header;
auto_table.prototype.add_header_sortable = auto_table_add_header_sortable;
auto_table.prototype.add_visible_field = auto_table_add_visible_field;
auto_table.prototype.add_detail_title = auto_table_add_detail_title;
auto_table.prototype.new_td = auto_table_new_td;
auto_table.prototype.select_last = select_last;

auto_table.prototype.clear = auto_table_clear;
auto_table.prototype.display_all_records = auto_table_display_all_records;
auto_table.prototype.not_selected = auto_table_not_selected;

function auto_table()
{
	this.ar_unfiltered = Array();
	this.ar_filtered = Array();
	this.ar_keys = Array();
	this.ar_hidden = Array();
	this.ar_detail_titles = Array();
	this.ar_visible_fields = Array();
	this.ar_headers = Array();
	this.ar_sortable_headers = Array();

	this.max_rows = 0;
	this.current_rows = 0;
	this.selected_record = Array();
	this.selected_index = -1;
	this.selectedID = -1;
	this.headers_created = false;
	this.clickable = true;
	this.border = true;
}



function auto_table_init(table_name)
{
	if (table_name == undefined)
		return;

	this.table_name = table_name;
	this.table = gbi(this.table_name);
	this.tableBody = document.createElement("tbody");
	this.table.appendChild(this.tableBody);
/*
    this.table_name = table_name;
    	this.table = gbi(this.table_name);
    	$(this.table).append("<tr id='tr1'></tr>");

    $('#tr1').append("<td>arse</td>");
    */
 }

function select_last()
{
	var elem = gbi(this.table_name + "_panel");
	if (elem != undefined)
		elem.scrollTop = elem.scrollHeight;
	td_elem = this.table_name + "_" + (this.ar_filtered.length-1);
	if (td_elem != undefined)
		this.mousedown(td_elem);
}

function auto_table_add_header( header)
{
	this.ar_headers.push(header);
}

function auto_table_add_header_sortable( header)
{
	this.ar_headers.push(header);
	this.ar_sortable_headers.push(header);
}


function auto_table_add_detail_title( header)
{
	this.ar_detail_titles.push(header);
}


function auto_table_add_visible_field(db_name)
{
	this.ar_visible_fields.push(db_name);
}


function auto_table_new_td(text, index)
{

	var td = document.createElement("td");
	if (text != undefined)
	{
		if (text.indexOf("SQL_DATE") == 0 )
		{
			var sbd = new SB_Date();
			sbd.fromSQL(text.substr(8));
			text =  sbd.get_date_string();
		}

		text = text.replace(/\|n/g, " ");
	}
	else
		text = "";

	td.innerHTML = text;
	td.id = this.table_name + "_" + index;

	var my_class = "auto_table_td";
	if (this.clickable == false)
		my_class = "auto_table_td_no_hover";
	if (this.border == false)
	    my_class = "auto_table_td_no_border";
	setClassAttribute(td, my_class);

	return td;
}

/* copy all records from filtered to unfiltered */
function auto_table_display_all_records()
{
	this.ar_filtered.length = 0;
	var i = 0;
	for (i=0; i < this.ar_unfiltered.length; i++)
	{
		this.ar_filtered.push(this.ar_unfiltered[i]);
	}
	this.display_html_table();
}

/*display filtered records - filtering can be done outside of the auto_table class*/
function auto_table_display_filtered_records()
{
	this.display_html_table();
}




function auto_table_display_html_table()
{
	if (this.headers_created == false)
	{
		if (this.ar_headers.length > 0 )
		{	
			var tr = document.createElement("tr");
			
			for(i=0; i < this.ar_headers.length; i++)
			{				
				var th = document.createElement("th");
				var th_style = "auto_table_th";
				if (in_array(this.ar_sortable_headers, this.ar_headers[i]))
				{
				    th_style = "auto_table_sortable_th";
				}
				setClassAttribute(th, th_style);

				th.innerHTML = this.ar_headers[i];
				tr.appendChild(th);
			}
			this.tableBody.appendChild(tr);	
		}
		
		this.headers_created = true;
	}


	for (i=0; i < this.ar_filtered.length; i++)
	{
		if (i >= this.max_rows)
		{
			var tr = document.createElement("tr");
			setClassAttribute(tr, "auto_table_tr");

			tr.id = this.table_name + "_" + i;
			/*start looping from index = 1 so we skip db_ID*/
			for (var t = 1; t < this.ar_keys.length; t++)
			{
				if (in_array(this.ar_visible_fields, this.ar_keys[t]) == true)
				{
					var text_value = eval('this.ar_filtered[' + i + '].' + this.ar_keys[t]);
					if (this.ar_keys[t] == "db_Title")
						text_value = TitleDropdown[text_value];
					if (text_value != undefined)
					{
						if (text_value.indexOf("SQL_DATE") == 0 )
						{
							var sbd = new SB_Date();

							sbd.fromSQL(text_value.substr(8));
							text_value =  sbd.get_date_string();
						}
						text_value = text_value.replace(/\|n/g, " ");
					}


					var td = this.new_td(text_value, i);
					tr.appendChild(td);
				}
			}	
			this.tableBody.appendChild(tr);	
		}
		else
		{
			var tr = gbi(this.table_name + "_" + i);
			var elem = tr.firstChild;
			for(var t = 1; t < this.ar_keys.length; t++)
			{
				if (in_array(this.ar_visible_fields, this.ar_keys[t]) == true)
				{
					var stat_str = 'this.ar_filtered[' + i + '].' + this.ar_keys[t];
					var text_value = eval(stat_str);
					if (this.ar_keys[t] == "db_Title")
						text_value = TitleDropdown[text_value];

					if (text_value != undefined)
					{
						if (text_value.indexOf("SQL_DATE") == 0 )
						{
							var sbd = new SB_Date();

							sbd.fromSQL(text_value.substr(8));
							text_value =  sbd.get_date_string();
						}
						text_value = text_value.replace(/\|n/g, " ");
					}
					else
						text_value = "";
					elem.innerHTML = text_value;
					elem = elem.nextSibling;
				}
			}
		}
	}

	
	this.current_rows = this.ar_filtered.length;
		
	for (i=0; i < this.current_rows; i++)
	{
	    setTableDisplay(this.table_name + "_" + i);
	}
	for (i=this.current_rows; i < this.max_rows; i++)
		gbi(this.table_name + "_" + i).style.display = "none";

	if (this.max_rows < this.current_rows)
		this.max_rows = this.current_rows;

	for (index=0; index < this.ar_filtered.length; index++)
		gbi(this.table_name + "_" + index).style.backgroundColor = '#ffffff';

}


/* we need the keys from the server so we can access the json parsed object by field names - 
ie obj.FirstName, obj.Country etc
*/
function auto_table_fill_keys_array(list)
{
	var i=0; 
	for (i=1; i <= list.count_keys; i++) 
	{
		list_str = "list.k" + i;
		this.ar_keys.push(eval(list_str));
	}
	for (i=1; i <= list.count_hidden; i++)
	{
		hidden_str = "list.h" + i;
		this.ar_hidden.push(eval(hidden_str));
	}
}

function auto_table_add_record(obj)
{
	this.ar_unfiltered.push(obj);
}

function auto_table_mousedown(elem_id)
{
	this.mousedown_display(elem_id);
}

function auto_table_mousedown_display(elem_id)
{
	var index = 0;
    if(elem_id.lastIndexOf("panel") != -1) 
	  return;
	for (index=0; index < this.ar_filtered.length; index++)
		gbi(this.table_name + "_" + index).style.backgroundColor = '#ffffff';
	var underscore = elem_id.lastIndexOf("_");
	this.selected_index = elem_id.substr(underscore+1, elem_id.length-(underscore-1));

	this.selectedID = this.ar_filtered[this.selected_index].db_ID;

	gbi(elem_id).style.backgroundColor = hilite_colour;
	gbi(elem_id).style.borderColor = '#00ff00';
}

function auto_table_deselect()
{
	var index = 0;
	for (index=0; index < this.ar_filtered.length; index++)
		gbi(this.table_name + "_" + index).style.backgroundColor = '#ffffff';
	this.selected_index = -1;
	this.selectedID = -1;
}
	

function auto_table_clear()
{
	this.ar_unfiltered.length = 0;
	this.ar_filtered.length = 0;
	this.selected_index = -1;
	this.selectedID = -1;
}

function auto_table_not_selected()
{
	if (this.ar_filtered.length == 0 )
		return true;
	if (this.selected_index < 0 || this.selected_index >= this.ar_filtered.length)
		return true;
	return false;
}




