//Specify highlight behavior. "TD" to highlight table cells, "TR" to highlight the entire row:
var highlightbehavior="TR"

var ns6=document.getElementById&&!document.all
var ie=document.all
var selcolor = 'red';
var highlightcolor = 'lightgreen';
var lastclicked = new Array();
var clicksource = new Array('A', 'IMG');
/*
Adds an empty colum to a table passed
@param tblId (String) Table name
*/
function addColumn(tblId)
{
	var tblBodyObj = document.getElementById(tblId).tBodies[0];
	for (var i=0; i<tblBodyObj.rows.length; i++) {
		var newCell = tblBodyObj.rows[i].insertCell(-1);
		newCell.innerHTML = 'new column' + (i + 1);
	}
}
/*
Deletes last column
*/
function deletelastColumn(tblId)
{
	var tblBodyObj = document.getElementById(tblId).tBodies[0];
	for (var i=0; i<tblBodyObj.rows.length; i++) {
		if (tblBodyObj.rows[i].cells.length > 1) {
			tblBodyObj.rows[i].deleteCell(-1);
		}
	}
}
function emptyrow(row)
{
	if (row){
	for (var i=0; i<row.cells.length; i++)
	{
		row.deleteCell(-1);
	}
	}
}
/*
Deletes column colIndex from table: tblId
*/
function deleteColumn(tblId, colindex)
{
	var tblBodyObj = document.getElementById(tblId).tBodies[0];
	for (var i=0; i<tblBodyObj.rows.length; i++) {
		if (tblBodyObj.rows[i].cells.length > colindex-1) {
			tblBodyObj.rows[i].deleteCell(colindex);
		}
	}
}
function appendRow(tblId)
{
	var tbl = document.getElementById(tblId);
	var newRow = tbl.insertRow(tbl.rows.length);
	var newCell = newRow.insertCell(0);
}

/*
Appends a row to tbl
*/
  // @todo: add Row to table dyn, delete rows, selectRows
function addRow(tbl, row)
{
  var tbody = tbl.getElementsByTagName("TBODY")[0];
  tbody.appendChild(row);

}
/* Sample of table manipulation
  function addRow(id){
    var tbody = document.getElementById
(id).getElementsByTagName("TBODY")[0];
    var row = document.createElement("TR")
    var td1 = document.createElement("TD")
    td1.appendChild(document.createTextNode("column 1"))
    var td2 = document.createElement("TD")
    td2.appendChild (document.createTextNode("column 2"))
    row.appendChild(td1);
    row.appendChild(td2);
    tbody.appendChild(row);
  }
*/


function deletelastRow(tbl)
{
    //var tbl = document.getElementById(tblId);
    if (tbl.rows.length > 2) tbl.deleteRow(tbl.rows.length - 1);
}
function insertRow(tblId, rowIndex)
{
	var tbl = document.getElementById(tblId);
	if (tbl.rows.length > rowindex)
		rowindex = tbl.rows.length;
	try {
		var newRow = tbl.insertRow(rowIndex);
		var newCell = newRow.insertCell(0);
		newCell.innerHTML = 'Inserted..';
	} catch (ex) {
		return false;
	}
}

function deleterow(tbl, rowIndex)
{
	//var tbl = document.getElementById(tblId);
	if (tbl.rows.length > rowIndex)
		tbl.deleteRow(rowIndex);
	
}
/*
Returns a table
*/
function gettable(tblid)
{
	return document.getElementById(tblid);//.tBodies[0];
}
/*
Returns a raw
tbl: table object
rowindex: int, position in collection 0 based
*/
function getrow(tbl, rowindex)
{
	if (rowindex == 'last')
		rowindex = tbl.rows.length;
	if (!rowindex)
		rowindex = tbl.rows.length;
	return tbl.rows[rowindex-1];
}
/*
Return a cell 
@param tbl: a table
@param rowIndex (int) 
@param colIndex (int)
*/
function getCell(tbl, rowIndex, colIndex)
{
	return tbl.rows[rowIndex].cells[colindex];
}


/***********************************************
Modified script by itzco based on code by : 
* Highlight Table Cells Script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* Visit http://www.dynamicDrive.com for hundreds of DHTML scripts
* This notice must stay intact for legal use

to work with standarista-table-sorting
***********************************************/


function changeto(e, idxtbl){
source=ie? event.srcElement : e.target
if (source.tagName=="TABLE")
return
while(source.tagName!=highlightbehavior && source.tagName!="HTML")
source=ns6? source.parentNode : source.parentElement
if (source.style.backgroundColor!=highlightcolor&&source.id!="ignore"&&source!=lastclicked[idxtbl])
source.style.backgroundColor=highlightcolor
}

function contains_ns6(master, slave) { //check if slave is contained by master
while (slave.parentNode)
if ((slave = slave.parentNode) == master)
return true;
return false;
}

function changeback(e, idxtbl){
if (ie&&(event.fromElement.contains(event.toElement)||source.contains(event.toElement)||source.id=="ignore")||source.tagName=="TABLE")
return
else if (ns6&&(contains_ns6(source, e.relatedTarget)||source.id=="ignore"))
return
else if (source == lastclicked[idxtbl])
return

if (ie&&event.toElement!=source||ns6&&e.relatedTarget!=source)
source.style.backgroundColor=''
}

/*
Function to set the row to a certain color on click, can be limited to certain element types A/TD
Doesn't work properly combined with change changeback & limit tag simultaneusly, need some debug
*/
function clicked(e,idxtbl, hb)
{
source=ie? event.srcElement : e.target
if (source.tagName=="TABLE"){
return
}
if (!inarray(clicksource,source.tagName))
{
//alert (source.tagName); clicksource.inArray(source.tagName))
return
}
if (!hb) hb = highlightbehavior;
//alert ("looking for " + hb + ", default:" + highlightbehavior );
while(source.tagName!=hb && source.tagName!="HTML")
{
source=ns6? source.parentNode : source.parentElement
}
if (source.id!="ignore")
{
//alert ("looking for " + hb + ", default:" + highlightbehavior + " setting on: "+ source.tagName);
if (lastclicked[idxtbl])
  lastclicked[idxtbl].style.backgroundColor = ''
source.style.backgroundColor=selcolor
lastclicked[idxtbl] = source;
}
}
/*
Array.prototype.inArray = function (value)
// Returns true if the passed value is found in the
// array.  Returns false if it is not.
{
    var i;
    for (i=0; i < this.length; i++) {
        // Matches identical (===), not just similar (==).
        if (this[i] === value) {
            return true;
        }
    }
    return false;
}
*/
/*


*/

  function dorow(arrcells)
  {
	var row = document.createElement("TR");
	for(q=0; q<arrcells.length; q++)
	{
		row.appendChild(arrcells[q]);
	}
	return row;
  }
function addCell(row, cell)
{
	row.appendChild(cell);
	return row;
}

  // careful this function gets txt no objects!!
  function docell(id, text, link, image)
  {
	var td = document.createElement("TD");
	if (id)
	{
		td.id = id;
	}
	if (text)
	{
		td.appendChild(text)
	}
	if (image)
	{
		td.appendChild(image);
	}
	if (link)
	{
		td.appendChild(link);
	}
	return td;
  }
  function add2cell(td, something)
  {
	td.appendChild(something);
	return td;
  }
/**
*Creates a link element 
* @param image = image node, can create with getimage
*/
  function dolink (href, text, image, h, w)
{
	//create the link
	var link = document.createElement('a');
	link.href = href;
	if (text)
	{
		var linkText=document.createTextNode(text);
		link.appendChild(linkText);
	}
 	if (image)
	{
		var img = doimage(image, "", h, w);
		link.appendChild(img);
	}
	return link;
}
function doimage(image, alt, h, w)
{
		var img = document.createElement('img');
		img.src = image;
		img.width = w;
		img.height = h;
		img.alt = alt;
		img.border = "0";
		
		return img;
}
function doinput(id,type,value,size)
{
	//create the input
	var elHiddenInput1 = document.createElement('input');
	elHiddenInput1.name = id;
	elHiddenInput1.id = id;		
	elHiddenInput1.type = type;
	if (size)
	{
		elHiddenInput1.size = size;
	}
	elHiddenInput1.value = value;
	
	return elHiddenInput1;
}	
function dotextarea(id,value)
{
	var elta = document.createElement('textarea');
	elta.name = id;
	elta.id = id;
	elta.value = value;
	return elta;
}
function doButton(id,label,action)
{
	var myb = document.createElement('button');
	myb.name = id;
	myb.id = id;
	
	var buttext = document.createTextNode(label);
	myb.appendChild(buttext);
	
	myb.onclick = action;
	
	return myb;

}
function dospan(id, value)
{
	//create the  span
	var elSpan = document.createElement('span');	
	elSpan.name = id;
	elSpan.id = id;
	elSpan.innerHTML = value;
	
	return elSpan;
}		
function dotext(txt)
{
	return document.createTextNode(txt);
}
// return the number of columns
function colcount(tbl)
{
	if (tbl.rows.length>1)
	return tbl.rows[1].length;
}

function findparent(myobj, lookingfor)
{
 var source = myobj.parentNode;

while(source.tagName!=lookingfor && source.tagName!="HTML")
{
source=ns6? source.parentNode : source.parentElement
}
	return source;
}

function removeNode(node)
{
    for(var i = (node.childNodes.length - 1); i >= 0; i--)
        removeNode(node.childNodes[i]);

    node.parentNode.removeChild(node);
}

function deleteRow(rowID)
{
    var rowNode = document.getElementById(rowID);
    removeNode(rowNode);
}

