﻿function THTMLHelper()
{
	this.createTable = function(document)
	{
		var table	= document.createElement('table');
		var tbody	= document.createElement('tbody');
		var trow	= document.createElement('tr');
		var tcell	= document.createElement('td');
		tcell.innerText = 'Tabellentext';

		table.appendChild(tbody);
		tbody.appendChild(trow);
		trow.appendChild(tcell);

		return new Array(table,tbody,trow,tcell);
	}	

	//adds cell next to existing TD object and returns array of td objects created on success
	this.addTableCell = function(objTD,position)
	{ 
		var result = null;

		if (objTD)
		{	
			td = objTD;

			tr = this.getParent(td,new Array('TR'));
			if (tr)
			{
				table = this.getParent(tr,new Array('TABLE','TBODY'));
				if (table)
				{			
					if (position == 'left')
					{
						tdIndex = td.cellIndex;
					}		
					else
					{
						tdIndex = td.cellIndex+1;
						if (tdIndex > tr.cells.length) { tdIndex = tr.cells.length; }		
					}

					trList = table.rows;

					result = new Array();
				
					c = 0;
					while (c <= trList.length-1)
					{
						td = trList.item(c).insertCell(tdIndex);
						td.text = '';	

						result.push(td);
										
						c++;
					}
				}
			}	
		}

		return result;
	}

	//adds row next to existing TD object and returns array of td objects created on success
	this.addTableRow = function(objTD,position) 
	{ 
		var result = null;

		if (objTD)
		{
			tr = this.getParent(objTD,new Array('TR'));
			if (tr)
			{
				table = this.getParent(tr,new Array('TABLE','TBODY'));
				if (table)
				{
					if (position == 'top')
					{
						trIndex = tr.rowIndex;
					}
					else
					{
						trIndex = tr.rowIndex+1;
						if (trIndex > table.rows.length) { trIndex = table.rows.length; }		
					}

					trNew = table.insertRow(trIndex);

					result = new Array();
				
					tdList = tr.cells;
					c = 0;
					while (c <= tdList.length-1)
					{
						td = tdList.item(c).cloneNode(false);
						td.innerText = '';
			
						result.push(td);
								
						trNew.appendChild(td);
						c++;
					}
				}
			}
		}

		return result;
	}

	//returns the next table cell object selected after deleting
	this.deleteTableCell = function(objTD,index,errorDescription)
	{
		var result = null;

		if (objTD)
		{	
			td = objTD;
			
			tr = this.getParent(td,new Array('TR'));
			if (tr)
			{
				table = this.getParent(tr,new Array('TABLE','TBODY'));
				if (table)
				{
					//loop trough all cells to determine if controls are inside, then abort action
					abort = false;
					r = 0;

					while (r <= table.rows.length-1)
					{
						if (table.rows.item(r).cells.length >= 0 && index <= table.rows.item(r).cells.length-1)
						{
							i = 0;
							while (i <= table.rows.item(r).cells.item(index).childNodes.length-1)
							{
								abort = abort || this.containsControl(table.rows.item(r).cells.item(index));
						
								i++;
							}
						}
	
						r++;
					}

					if (!abort)
					{
						if (tr.cells.length > 1) //to avoid that invisible table is created
						{
							if (td.previousSibling) { result = td.previousSibling; } else 
									{ result = td.nextSibling; }
		
							trList = table.rows;
								
							c = 0;
							while (c <= trList.length-1)
							{
								if (index >= 0 && index <= trList.item(c).cells.length-1)
								{
									trList.item(c).deleteCell(index);
								}
		
								c++;
							}
						}
					}
				}
			}
		}

		return result;	
	}

	//returns the next table row object selected after deleting
	this.deleteTableRow = function(objTD,index)
	{
		var result = null;

		if (objTD)
		{
			tr = this.getParent(objTD,new Array('TR'));
			if (tr)
			{
				//loop trough all cells to determine if controls are inside, then abort action
				abort = false;
				c = 0;
				while (c <= tr.cells.length-1)
				{
					i = 0
					while (i <= tr.cells.item(c).childNodes.length-1)
					{
						abort = abort || this.containsControl(tr.cells.item(c));
			
						i++;
					}

					c++;
				}

				if (!abort)
				{
					table = this.getParent(tr,new Array('TABLE','TBODY'));
					if (table)
					{
						if (table.rows.length > 1) //to avoid that invisible table is created
						{
							if (tr.previousSibling) { result = tr.previousSibling; } else 
										{ result = tr.nextSibling; }
								if (index >= 0 && index <= table.rows.length-1)
							{
								table.deleteRow(index);
							}
						}
					}
				}
			}
		}

		return result;	
	}

	//returns true if controls are detected in childen collection
	this.containsControl = function(obj)
	{
		var result = false;

		if (obj)
		{
			if (obj.childNodes)
			{
				var i = 0;
				while (i <= obj.childNodes.length-1)
				{
					if (obj.childNodes.item(i))
					{
						if (
							obj.childNodes.item(i).tagName == 'INPUT'  || 
							obj.childNodes.item(i).tagName == 'SELECT' ||
							obj.childNodes.item(i).tagName == 'TEXTAREA'
						)
						{
							result = true;
						}
						else
						{
							result = result || this.containsControl(obj.childNodes.item(i));
						}
					}
		
					i++;
				}


			}
		}
	
		return result;
	}
	
	//returns specific parent (equal to given tag name) from the parent queue
	this.getParent = function(obj, parentTagNameArray)
	{
		var result = null;
		var abort = true;
		var p = obj.parentElement;

		if (p)
		{ 
			abort = false; 
			c = 0;
			while (c <= parentTagNameArray.length-1 && !abort)
			{
				if (parentTagNameArray[c] == p.tagName)
				{
					abort = true; 
					result = p;	
				}

				c++;
			}

		} 
		else
		{
			abort = true;
		} 

		while (!abort)	
		{
			if (p.parentElement)
			{
				p = p.parentElement;
			
				abort = false; 
				c = 0;
				while (c <= parentTagNameArray.length-1 && !abort)
				{
					if (parentTagNameArray[c] == p.tagName)
					{
						abort = true; 
						result = p;	
					}

					c++;
				}
			}
			else
			{ 
				abort = true; 
			}
		}

		return result;
	}
	
	this.addOption = function(objSelect,value,text)
	{
		var result = null;
		
		if (objSelect)
		{
			var option = objSelect.ownerDocument.createElement('OPTION');
			option.value = value;
			option.innerText = text;	

			objSelect.appendChild(option);
			
			var c = 0;
			var i = -1;
			while (c <= objSelect.options.length-1 && i == -1)
			{
				if (objSelect.options(c) == option) { i = c; }
				c++;
			}

			if (i != -1) { objSelect.selectedIndex = i; }
			
			result = option;
		}
		
		return result;
	}

	this.deleteOption = function(objSelect)
	{
		var result = false;

		if (objSelect.selectedIndex >= 0)
		{
			var i = objSelect.selectedIndex;
			objSelect.options.remove(i);
			this.setSelectedIndex(objSelect,i);

			result = true;	
		}

		return result;
	}

	this.updateOption = function(objSelect,index,value,text)
	{
		var result = null;
		
		if (index >= 0 && index <= objSelect.options.length-1)
		{
			objSelect.options(index).value = value;
			objSelect.options(index).innerText = text;
			objSelect.selectedIndex = index;

			result = true;	
		}

		return result;
	}

	this.moveOption = function(objSelect,index,offset)
	{
		var result = null;
		var i = index+offset;

		if (i < 0) { i = 0; }
		if (i > objSelect.options.length-1) { i = objSelect.options.length-1; }

		if (i >= 0 && i <= objSelect.options.length-1 && index >= 0 && index <= objSelect.options.length-1)
		{
			var value = objSelect.options(index).value;
			var text = objSelect.options(index).innerText;

			objSelect.options(index).value = objSelect.options(i).value;
			objSelect.options(index).innerText = objSelect.options(i).innerText;

			objSelect.options(i).value = value;
			objSelect.options(i).innerText = text;

			objSelect.selectedIndex = i;

			result = true;	
		}

		return result;	
	}

	this.setSelectedIndex = function(objSelect,index)
	{
		var i = index;
		if (i > objSelect.options.length-1) { i = objSelect.options.length-1; }

		objSelect.selectedIndex = i;
	}
}