//
// Javascript function library for use in Adm/ CMS system
// Copyright (c) 1999-2006 - Online-Art Corp.
// Created: 2005-1-29 - Lars de Vries
// Modified: 2006-6-26 - Lars de Vries
//

// GEHACKTE VERSIE! VOORAL NIET HERGEBRUIKEN

/////////////////////////////////////////////////////////////////
//
// Table object
// Copyright (c) 2005 - Online-Art Corp.
// Created: 2005-1-29 - Lars de Vries
// Modified: 2006-2-22 - Lars de Vries

//
// selection operations
//

function tSelectToggle(row)
{
	var id = row.id.replace(this.obj,'');

	var s = explode(",",this.params);	
	var key = array_search(id,s);
	
	if(key==-1)
		this.selectAdd(row);
	else
		this.selectRemove(row);	
}

function tSelectAdd(row)
{
	this.currentRow = row;
	
	var id = row.id.replace(this.obj,'');
	
	var s = explode(",",this.params);	
	var key = array_search(id,s);
		
	if(key==-1)
	{
		s[s.length] = id;
			
		row.style.backgroundColor = '#0A246A';
		row.style.color = 'white';
	}

	this.params = implode(",",s);
}

function tSelectRemove(row)
{
	this.currentRow = row;
	
	var id = row.id.replace(this.obj,'');

	var s = explode(",",this.params);
	var key = array_search(id,s);

	if(key!=-1)
	{
		s = array_remove(key,s);
		row.style.backgroundColor = '';
		row.style.color = '';
	}
		
	this.params = implode(",",s);
}

function tSelectAll()
{
	var tableId = this.obj.substring(0,this.obj.length-1);
	var table = document.getElementById(tableId);
	var rows = table.rows;
	var row;
	
	for(var i=2; i<rows.length; i++)
	{
		row = rows[i];
		this.selectAdd(row);	
	}
}

function tSelectClear()
{
	var s = explode(",",this.params);
	var row;
	for(var i=0; i<s.length; i++)
	{
		
		row = document.getElementById(this.obj + s[i]);
		this.selectRemove(row);
	}
}

function tSelectNext()
{
	var tableId = this.obj.substring(0,this.obj.length-1);
	var table = document.getElementById(tableId);
	var rows = table.rows;
	var row;

	row = this.currentRow;
	if(!row)
	{
		row = rows[2];
	} else if(row.nextSibling != null)
	{
		row = row.nextSibling;
	}
	this.selectAdd(row);
	
	if(this.editCell!=false)
	{
		index = this.getCellIndex(this.editCell);		
		this.setEditMode(row.childNodes[index],true,true);
	}
}

function tSelectPrevious()
{
	var tableId = this.obj.substring(0,this.obj.length-1);
	var table = document.getElementById(tableId);
	var rows = table.rows;
	var row;

	row = this.currentRow;
	if(!row || row.previousSibling.id == '')
	{
		row = rows[2];
	} else
	{
		row = row.previousSibling;
	}
		
	this.selectAdd(row);
	
	if(this.editCell!=false)
	{
		index = this.getCellIndex(this.editCell);		
		this.setEditMode(row.childNodes[index],true,true);
	}
	
}

function tCopySelection()
{
	
	var i,s,rowId,row,cell,copyData,copyFormat;
	
	format = getSetting('copyFormat',this.settings);
	
	if(format=='csv')
	{
		syntax = new Object({
						dataBegin:'',
						dataEnd:'',
						rowBegin:'',
						rowEnd:'\n',
						cellBegin:'"',
						cellEnd:'"',
						cellSeparator:','
					});
	} else if(format=='html')
	{
		syntax = new Object({
						dataBegin:'<table>',
						dataEnd:'</table>',
						rowBegin:'<tr>',
						rowEnd:'</tr>',
						cellBegin:'<td>',
						cellEnd:'</td>',
						cellSeparator:''
					});
	}
	
	data = syntax.dataBegin;
	
	var s = explode(',',this.params);
		
	for(i=0; i<s.length; i++)
	{
		row = document.getElementById(this.obj + s[i]).getElementsByTagName('td');
		
		data += syntax.rowBegin;
		
		for(j=0; j<row.length; j++)
		{
			cell = row[j].innerHTML;
			
			data += syntax.cellBegin + cell + syntax.cellEnd;
			
			if(j+1<row.length)
				data += syntax.cellSeparator;		
		}
		
		data += syntax.rowEnd;
		
	}
	
	data += syntax.dataEnd;
	
	window.clipboardData.setData('Text', data);
	
}

//
// keyboard functions
//

function tKDown(event)
{
	event = this.getEvent(event);
	
	keyCode = event.keyCode;
	
	//alert(keyCode);
		
	switch (keyCode)
	{	
	case 13: // return
		if(this.editCell==false)
		{
			this.selectClear();
			this.selectAdd(this.currentRow);
			
			if(this.actionScript!=false)
			{
				script = this.actionScript.replace('{PARAMS}',this.params);			
				eval(script);			
			}
			
			if(this.actionUrl!=false)
			{
				url = this.actionUrl.replace('{PARAMS}',this.params);
				document.location = url;
			}
			
		} else
		{
			this.setEditMode(null,false,true);
		}
			
		break
		
	case 113: // F2
	
		if(this.editCell==false)
		{
			index = 0;
			while(this.rename[index]!=1 && index < this.rename.length)
				index++;
				
			if(this.rename[index] == 1)
			{
				row = this.currentRow;
				this.setEditMode(row.childNodes[index],true);
			}			
		}
	
		break;
		
	case 27: // escape
		if(this.editCell!=false)
		{
			this.editCell.firstChild.setAttribute('value',this.editCell.firstChild.getAttribute('pValue'));			
			this.setEditMode(null,false,false);
		}
	
		break;
		
	case 9: // tab
		if(this.editCell!=false)
		{			
			index = this.getCellIndex(this.editCell);
			_index = index;
			
			if(!event.shiftKey)
			{			
				index++;
				while(this.rename[index]!=1 && index < this.rename.length)
					index++;
			} else
			{
				index--;
				while(this.rename[index]!=1 && index > 0)
					index--;				
			}
				
			if(this.rename[index]!=1)
			{
				var tableId = this.obj.substring(0,this.obj.length-1);
				var table = document.getElementById(tableId);
				var rows = table.rows;

				if(!event.shiftKey)
				{
					index = 0;
					while(this.rename[index]!=1) index++;
					
				} else if(event.shiftKey)
				{
					index = this.rename.length;
					while(this.rename[index]!=1) index--;
				}
				
				this.setEditMode(row.childNodes[index],true,true);
				if(!event.shiftKey && this.currentRow.nextSibling != null)
				{
					this.selectClear();
					this.selectNext();
				} else if(event.shiftKey && this.currentRow.previousSibling.id != '')
				{
					this.selectClear();
					this.selectPrevious();
				} else
				{					
					this.setEditMode(row.childNodes[_index],true,true);
				}
					
				
			} else
			{
				this.setEditMode(row.childNodes[index],true,true);
			}			
		}	
	
		break;
			
	case 38: // up
		if(!event.shiftKey)
			this.selectClear();
		this.selectPrevious();
		break
		
	case 40: // down
		if(!event.shiftKey)
			this.selectClear();
		this.selectNext();
		break
		
	case 65: // ctrl + a
		if(event.ctrlKey)
			this.selectAll();
		break
		
	case 67: // ctrl + c
		if(event.ctrlKey)
			this.copySelection();
		break		
	}
	
}



//
// mouse functions
//

function tMRowDown(event)
{
	if(event.button == 0 || event.button == 1)
	{
		this.mDown = true;
		var row = this.mGetEventRow(event);
		table = this.getParentElementByName(row,'TABLE');
		//table.focus();
		//row.focus();
					
		if(!event.ctrlKey)
		{
			this.renameCell(event);
			this.selectClear();		
			this.selectAdd(row);			
		
		} else
		{
			this.selectToggle(row);
		}
		
		
		
		this.cMContainer.hideMenu();
	}
}

function tRenameCell(event)
{
	var row = this.mGetEventRow(event);
	var id = row.id.replace(this.obj,'');
	var s = explode(",",this.params);	
	var key = array_search(id,s);
	
	if(key==0)
	{
		cell = this.mGetEventCell(event);
		index = this.getCellIndex(cell);
		if(cell != this.editCell && 
			this.rename[index]==1)
			this.setEditMode(cell,true);
		
	} else
	{
		this.setEditMode(null,false,true);
	}
}

function tSetEditMode(cell,mode,update)
{
	if(this.editCell!=false)
	{	
		if(update==true)
			this.xmlUpdate(this.editCell);
			
		row = this.getParentElementByName(this.editCell,'TR');
		row.setAttribute('ondblclick',row.getAttribute('_ondblclick'));
		row.setAttribute('_ondblclick',null);
			
		value = this.editCell.firstChild.getAttribute('value');
		this.editCell.innerHTML = value;	
		this.editCell = false;
			
	}

	if(mode==true)
	{
		this.editCell = cell;
		value = cell.innerHTML;
		
		cell.innerHTML = '<input class="FormText" style="position: absolute; z-index: 5; width: ' + (cell.clientWidth-6) + 'px;" type="text" value="' + value + '"/></div>';
		input = this.editCell.firstChild;
		input.setAttribute('pValue',value);
		input.select();
	
		row = this.getParentElementByName(this.editCell,'TR');
		row.setAttribute('_ondblclick',row.getAttribute('ondblclick'));
		row.setAttribute('ondblclick',null);
	}
	
}

function tXmlUpdate(cell)
{
	////
	// create url
	
	// get value
	value = cell.firstChild.getAttribute('value');
	
	// get id
	row = this.getParentElementByName(cell,'TR');
	id = row.id.replace(this.obj,'');
	
	// get cell index
	index = this.getCellIndex(cell);
		
	// table id
	tableId = this.obj;

	// url
	url = 'actions.rename' +
			'&tableId=' + tableId +
			'&index=' + index +
			'&id=' + id +
			'&value=' + value;
			
	// code for Mozilla, etc.
	if (window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
		xmlhttp.onreadystatechange=this.xmlHttpChange;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);

	// code for IE
	} else if (window.ActiveXObject)
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		if (xmlhttp)
		{
			xmlhttp.onreadystatechange=this.xmlHttpChange;
			xmlhttp.open("GET",url,true);
			xmlhttp.send();
		}

	}	

}

// xml request handler
function tXmlHttpChange()
{
	/*// if xmlhttp shows "loaded"
	if (xmlhttp.readyState==4)
	{
		// if "OK"
		if (xmlhttp.status==200)
		{
			//alert(xmlhttp.responseText);
		} else
			alert("error");
	}*/
	
}

function tMRowContext(event)
{

	var row = this.mGetEventRow(event);
	var id = row.id.replace(this.obj,'');
	
	var s = explode(",",this.params);	
	var key = array_search(id,s);
	
	if(key==-1)
	{	
		this.selectClear();		
		this.selectAdd(row);
	}
	
	var scrollTop = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
	var scrollLeft = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft;
	
	this.cMContainer.showMenu(event.clientX + scrollLeft,event.clientY + scrollTop,this.params);
}

function tMRowDblClick(event)
{
	var row = this.mGetEventRow(event);

	var id = row.id.replace(this.obj,'');
		
	if(this.actionScript!=false)
	{
		script = this.actionScript.replace('{PARAMS}',id);
		eval(script);			
	}
	
	//alert(this.actionUrl);
	
	if(this.actionUrl!=false)
	{
		
		url = this.actionUrl.replace('{PARAMS}',id);
		document.location = url;
	}
		
}

function tMRowUp(event)
{
	this.mDown = false;
}

function tMRowOver(event)
{
	var row = this.mGetEventRow(event);
	if(this.mDown)
		this.selectAdd(row);
}

function tMGetEventRow(event)
{
	element = this.mGetEventElement(event);
	return this.getParentElementByName(element,'TR');
}

function tMGetEventCell(event)
{
	element = this.mGetEventElement(event);
	return this.getParentElementByName(element,'TD');
}

function tMGetEventElement(event)
{
	event = this.getEvent(event); 
	
	if(window.navigator.appName == 'Microsoft Internet Explorer')
	{
		element = event.srcElement;
	} else
	{
		element = event.target;
	}
	
	return element;
}

function tGetEvent(event)
{
	return (event) ? event:(window.event);
}

//
// DOM functions
//

function tGetParentElementByName(element,name)
{	
	while(element.nodeName!=name)
		element = element.parentNode;	
	return element;
}

function tGetCellIndex(cell)
{
	row = this.getParentElementByName(cell,'TR');
		
	index = 0;
	nodeList = row.childNodes;
	while(cell!=row.childNodes[index] && 
			index < row.childNodes.length) 
		{ index++; }
	return index;
	
}



function tFinalize()
{	
	//this.cMContainer.draw();
	
	var url = getSetting("actionUrl",this.settings);
	var action = getSetting("action",this.settings);
	url = url.replace('{ACTION}',action);
	
	var script = getSetting("actionScript",this.settings);
	
	if(url!='')
		this.actionUrl = url;
	
	if(script!='')
		this.actionScript = script;
}

//
// constructor
//

function Table(obj,settings)
{

	//
	// vars
	//
	
	this.params = false;
	this.currentRow = false;
	this.obj = obj;
	this.settings = setDefaultSettings('uid=id,actionUrl={ACTION}&id={PARAMS}&,action=,actionScript=,copyFormat=html,rename=',settings);
	this.uid = getSetting('uid',this.settings);
	//this.cMContainer = new OAContextMenu(obj+'.cMContainer',settings);
	//this.rowMenu = this.cMContainer.addMenu();
	this.mDown = false;
	this.actionUrl = false;
	this.actionScript = false;
	this.rename = explode('|',getSetting('rename',this.settings));
	this.editCell = false;
	
	//
	// functions
	//
	
	this.selectToggle = tSelectToggle;
	this.selectAdd = tSelectAdd;
	this.selectRemove = tSelectRemove;
	this.selectAll = tSelectAll;
	this.selectClear = tSelectClear;
	this.selectNext = tSelectNext;
	this.selectPrevious = tSelectPrevious;
	this.renameCell = tRenameCell;
	this.setEditMode = tSetEditMode;
	this.copySelection = tCopySelection;
	
	this.kDown = tKDown;
		
	this.mRowContext = tMRowContext;
	this.mRowDown = tMRowDown;
	this.mRowDblClick = tMRowDblClick;
	this.mRowUp = tMRowUp;
	this.mRowOver = tMRowOver;

	this.mGetEventRow = tMGetEventRow;
	this.mGetEventCell = tMGetEventCell;
	this.mGetEventElement = tMGetEventElement;
	
	this.getEvent = tGetEvent;
	
	this.getCellIndex = tGetCellIndex;
	this.getParentElementByName = tGetParentElementByName;
	
	this.xmlUpdate = tXmlUpdate;
	this.xmlHttpChange = tXmlHttpChange;

	this.finalize = tFinalize;
		
}
