/*
	***** Table Routines *****
	
	A set of functions for creating tables and adding functionality
	such as sorting, show/hide regions and internal scrolling
*/

/**** Rollover routines for automatic tables - class rollover preferred *****/
function initialiseTableRollover(TableID, bgcolor, rocolor, textcolor, showheader) {
	var table = document.getElementById(TableID);
	var trs = table.getElementsByTagName('tr');
	var isdark = false;
	for (i=showheader?1:0;i<trs.length;i++) {
		if(trs[i].tagName=='TR') {
			trs[i].onmouseover=(textcolor!='') 
				? function() { this.style.backgroundColor=rocolor; this.style.color=textcolor; }
				: function() { this.style.backgroundColor=rocolor; }
			var origcolor = trs[i].style.color;
			if (isdark && bgcolor!='') {
				trs[i].style.backgroundColor=bgcolor;
				trs[i].onmouseout=function() { this.style.backgroundColor=bgcolor; this.style.color= origcolor; }
			} else {
				trs[i].style.backgroundColor='';
				trs[i].onmouseout=function() { this.style.backgroundColor=''; ; this.style.color= origcolor; }
			}
			isdark = !isdark;
		}
	}
}

function initialiseTableClassRollover(TableID, roclass, usealternates, altclass, showheader) {
	var table = document.getElementById(TableID);
	var trs = table.getElementsByTagName('tr');
	var isalt = false;
	for (i=showheader?1:0;i<trs.length;i++) {
		if(trs[i].tagName=='TR') {
			trs[i].onmouseover=  function() { this.className+=' '+roclass; }
			trs[i].onmouseout=function() { this.className=this.className.replace(roclass,'') }
			if (isalt && usealternates) { trs[i].className+=' '+altclass; }
			isalt=!isalt;
		}
	}
}
/**** End Rollover routines for automatic tables *****/

/***** vertical tabbing ****/
function setupTabbing(tableid, tabindex, startrow) {
	var tbl=document.getElementById(tableid);
	var trs=tbl.getElementsByTagName('tr');
	for (var i=1; i<trs[1].cells.length;i++) {
		trs[1].cells[i].firstChild.tabIndex=tabindex++;
		for(var j=startrow; j<trs.length;j++) {
			trs[j].cells[i].firstChild.tabIndex=tabindex++;
		}
	}
}

/***** DOM-based table sorting ****/
/* v1.00 - 19/1/6 */
// Setup column headers to handle clicks
function setupTableSorting(tableid) {
	return(true);
	var table=document.getElementById(tableid);
	if(!table) {
		//If we can't find the table it could be a GroupedTable i.e. tableid1, tableid2...
		var tables = document.getElementsByTagName('table');
		for(var i=0; i<tables.length;i++) {
			if(tables[i].id.slice(0,tableid.length)==tableid) {
				setupTableSorting(tables[i].id);
			}
		}
	} else {
		var startrow=table.getAttribute('startrow')!=null?table.getAttribute('startrow'):0;
		var image_nosort='<img src="'+config_BaseDir+'images/nosort.gif" hspace=3 vspace=0 class="tableSortIcon"/>';
		
		var colheaders=table.getElementsByTagName('th');
		if(colheaders) {
			for(var i=0;i<colheaders.length;i++) {
				var th=colheaders[i];
				th.style.whiteSpace='nowrap';
				
				//Get data type for comparison
				var datatype=th.getAttribute('datatype')!=null?th.getAttribute('datatype'):'string';
				eval('th.onclick=function() { sortTable(\''+tableid+'\', '+i+', \''+datatype+'\','+startrow+'); }');
				th.innerHTML+=th.innerHTML!='&nbsp;'?image_nosort:'';
				try {
					th.style.cursor='pointer';
				} catch (e) {
					th.style.cursor='hand';
				}
			}
		}
	}
}
// Get arguments for sorting table
function sortTable(tableid, colindex, datatype) {
	var startrow=arguments.length>3?arguments[3]:0;
	var image_nosort='<img src="'+config_BaseDir+'images/nosort.gif" hspace=3 vspace=0/>';
	var image_asc='<img src="'+config_BaseDir+'images/asc.gif" hspace=3 vspace=0/>';
	var image_desc='<img src="'+config_BaseDir+'images/desc.gif" hspace=3 vspace=0/>';
	
	var table=document.getElementById(tableid);

	var sortorder = (table.getAttribute('sortorder')!=null)?table.getAttribute('sortorder'):'desc';
	sortorder=sortorder=='asc'?'desc':'asc';
	table.setAttribute('sortorder',sortorder);

	//Add image to indicate sort order
	var colheaders=table.getElementsByTagName('th');
	if(colheaders) {
		for(var i=0;i<colheaders.length;i++) {
			var th=colheaders[i];
			if(th.innerHTML.toLowerCase().indexOf('<img')!=-1) { th.innerHTML=th.innerHTML.slice(0, th.innerHTML.toLowerCase().indexOf('<img')); }
			if(colindex==i) { 
				th.innerHTML+=th.innerHTML!='&nbsp;'?(sortorder=='asc'?image_asc:image_desc):'';
			} else {
				th.innerHTML+=th.innerHTML!='&nbsp;'?image_nosort:'';
			}
		}
	}
	
	var trs=table.tBodies[0].rows;
	for(var i=startrow; i<trs.length; i++) {
		var rowvalue=getCellValue(trs[i].cells[colindex], datatype);
		for(var j=startrow; j<trs.length; j++) {
			if(i!=j) {
				var comparisonvalue=getCellValue(trs[j].cells[colindex], datatype);
				if((sortorder=='asc' && rowvalue<comparisonvalue) || (sortorder=='desc' && rowvalue>comparisonvalue)) {
					trs[j].parentNode.insertBefore(trs[i], trs[j]);
					break;
				}
			}
		}
	}
}

function getCellValue(cell, datatype) {
	var rowvalue=(cell.textContent)?cell.textContent:cell.innerText;
	if(cell.innerHTML.toLowerCase().indexOf('<input')==0) {
		switch(cell.firstChild.type) {
			case 'checkbox' :
				rowvalue=cell.firstChild.checked;
				break;
			default:
				rowvalue=cell.firstChild.value;
				break;
		}
	}
	if(cell.innerHTML.toLowerCase().indexOf('<select')==0) {
		rowvalue=(datatype=='number')
			?cell.firstChild.options[cell.firstChild.selectedIndex].value
			:cell.firstChild.options[cell.firstChild.selectedIndex].text;
	}
	switch(datatype) {
		case 'number':
			rowvalue=isNaN(rowvalue)?0.0:parseFloat(rowvalue);
			break;
		case 'date':
			if(rowvalue.indexOf('/')>0) {
				var dateparts = rowvalue.split('/');
				rowvalue=new Date(dateparts[2], dateparts[1], dateparts[0]);
			} else {
				rowvalue=new Date(rowvalue);
			}
			break;
	}
	return (rowvalue);
} // End getCellValue
/*	Old comparison without datatype attribute
	if(!isNaN(rowvalue) && !isNaN(comparisonvalue)) {
		rowvalue=parseFloat(rowvalue);
		comparisonvalue=parseFloat(comparisonvalue);
	} else {
		if(isDate(rowvalue).length==0 && isDate(comparisonvalue).length==0) {
			rowvalue=new Date(rowvalue);
			comparisonvalue=new Date(comparisonvalue);
		}
	} 
*/
/***** End table sorting ****/


/**** js Tables ****/

// Column object - corresponds to a field in an rs
function tableColumn(name, datatype) { //Optional args: value, link, onclick, formatFunction, class, style
	// Set properties based on constructor args
	this.name=name;
	this.title=name;
	this.dataType=datatype;
	this.value=arguments.length>=3?arguments[2]:'';
	this.link=arguments.length>=4?arguments[3]:'';
	this.onClick=arguments.length>=5?arguments[4]:'';
	this.formatFunction=(arguments.length>=6 && typeof arguments[5]=='function')?arguments[5]:null;
	this.classname=arguments.length>=7?arguments[6]:'';
	this.style=arguments.length>=8?arguments[7]:'';
	this.currency='$'
	// Set defaults based on type
	switch(this.datatype) {
		case 'number':
			this.defaultValue='0';
			this.alignment='center';
			break;
		case 'date':
			this.defaultValue='';
			this.alignment='center';
			break;
		case 'currency':
			this.defaultValue='0.00';
			this.alignment='center';
			this.formatFunction=formatPrice;
			break;	
	}
	//Print header cell
	this.printHeader=function() {
		return('<th>'+(this.title.length!=0?this.title:this.name)+'</th>');
	}
	//Print cell, with all the options
	this.print=function() {
		var tempvalue=this.value.length!=0?this.value:this.defaultValue;
		return('<td align="'+this.alignment+'" '
			+(this.classname.length!=0?' class="'+this.classname+'"':'')
			+(this.style.length!=0?' style="'+this.style+'"':'') 
			+(this.onclick.length!=0?' onclick="'+this.onclick+'"':'')
			+ '>'
			+(this.link.length!=0?'<a href="'+this.link+tempvalue+'">':'')
			+(typeof this.formatFunction=='function'?this.formatFunction(tempvalue):tempvalue)
			+(this.link.length!=0?'</a>':'')
			+'</td>');
	}
}
