/**
 * DOMGen : Script de manipulation simple du dom
 *
 * @package    Libs
 * @subpackage Scripts
 * @copyright  Cediti
 * @version    0.5
 * @origin     UCM_Booster
 * @used_in    UCM_Booster, GaleriaInno
 */

function createInput(type, name, value, change)
{
	var input = document.createElement('input');
	input.setAttribute('name',name);
	input.id = name;
	input.setAttribute('type',type);
	
	
	if (value != null)
	{
		input.setAttribute('value',value);
	}
	
	if (change != null)
	{
		setOnChangeEvent(input,change);
	}
	
	return input;
}

function setOnChangeEvent(element,onchange)
{
	element.setAttribute('onchange',function() { eval(onchange) }); //version tordue IE
	element.setAttribute('onChange',onchange); //version FF, Opera & 
}

function createSelect(name, value, optionsValue, optionsLabel, change)
{
	var select = document.createElement('select');
	select.setAttribute('name',name);
	select.id = name;
	
	var opt;
	
	for (i=0;i<optionsValue.length;i++)
	{
		opt = document.createElement('option');
		opt.setAttribute('value',optionsValue[i]);
		opt.appendChild(document.createTextNode((optionsLabel != null ? optionsLabel[i] : optionsValue[i])));
		
		if(value == optionsValue[i]) opt.setAttribute('selected','selected');
		
		select.appendChild(opt);
	}
	
	if (change != null)
	{
		setOnChangeEvent(select,change);
	}
	
	return select;
}

function getParent(node, element)
{
	node = node.parentNode;
	while (element != null && node.tagName.toLowerCase() != element) {
		node = node.parentNode;
	}
	return node;
}

function removeRow(node)
{
	node = getParent(node, 'tr');
	node.parentNode.removeChild(node);
}

function removeTable(node)
{
	node = getParent(node, 'table');
	node.parentNode.removeChild(node);
}

function hideRow(node)
{
	node = getParent(node, 'tr');
	node.style.display = 'none';
}

function highlightFields(fields)
{
	var x = 0;
	for(x = 0; x < fields.length; x++)
	{	
		var elem = document.getElementById(fields[x]);
		
		if (elem != null)
		{
			if (elem.className != '')
			{
				elem.className = elem.className + ' field_highlight';
			}
			else
			{
				elem.className = 'field_highlight';
			}
		}
	}
}

function resetFields(fields)
{
	var x = 0;
	
	for(x = 0; x < fields.length; x++)
	{
		var elem = document.getElementById(fields[x]);
		
		if(elem != null)
		{	
			clearField(elem);
		}
	}
	
	return false;
}

function clearField(elem, ignoreHidden)
{
	if (elem.tagName.toLowerCase() == 'input')
	{
		if(elem.className.indexOf('js_exemple') < 0 && elem.className.indexOf('noreset') < 0)
		{
			if (elem.type == 'text' || elem.type == 'password')
			{
				elem.value = '';
			}
			else if (elem.type == 'checkbox')
			{
				elem.checked = false;
			}
			else if (elem.type == 'radio')
			{
				elem.checked = false;
			}
			else if (ignoreHidden != true && elem.type == 'hidden')
			{
				elem.value = '';
			}
			
			var exemple = document.getElementById(elem.id + '_exemple');
			if(exemple != null)
			{
				elem.style.display = 'none';
				exemple.style.display = 'inline';
			}
		}
	}
	else if (elem.tagName.toLowerCase() == 'select')
	{
		elem.selectedIndex = 0;
	}
	else if (elem.tagName.toLowerCase() == 'form')
	{
		var x = 0;
		for(x = 0; x < elem.elements.length; x++)
		{
			clearField(elem.elements[x], true);
		}
	}
	else if (elem.tagName.toLowerCase() == 'textarea')
	{
		elem.value = '';
	}
}

var originalDisplayCollection = new Array();

function switchDisplay(id, state, newDisplay)
{
	var elem = document.getElementById(id);
	
	if (elem != null)
	{
		if (originalDisplayCollection[id] == null) originalDisplayCollection[id] = elem.style.display;
		if (newDisplay == null) 
		{
			newDisplay = 'none';
		}
		
		if (state == null)
		{
			state = (elem.style.display == newDisplay);
		}
		
		if(state == true)
		{
			elem.style.display = originalDisplayCollection[id];
		}
		else
		{
			elem.style.display = newDisplay;
		}
		
		return state;
	}
	else
	{
		return false;
	}
}

function TList(tbodyId, beforeFetch, afterFetch)
{
	this.elements = new Array();
	this.tbodyId = tbodyId;
	this.beforeFetch = (beforeFetch ? beforeFetch : null);
	this.afterFetch = (afterFetch ? afterFetch : null);
}

TList.prototype.tbodyId;
TList.prototype.elements;
TList.prototype.beforeFetch;
TList.prototype.afterFetch;

TList.prototype.Clear = function()
{
	this.elements = new Array();
	this.Fetch();
}

TList.prototype.SetData = function(x,label,value)
{
	if(x >= 0 && x < this.elements.length)
	{
		this.elements[x].data[label] = value; 
	}
}

TList.prototype.Add = function(element, notFetch)
{
	
	this.elements.push(element);
	if (! notFetch)
	{
		this.Fetch();
	}
}

TList.prototype.Insert = function(x, element)
{
	if (x >= 0 && x < this.elements.length)
	{
		this.elements.splice(x,0,element);
	}
	else if (x < 0)
	{
		this.elements.unshift(element);
	}
	else
	{
		this.elements.push(element);
	}
	this.Fetch();
}

TList.prototype.Remove = function(x)
{
	if(x >= 0 && x < this.elements.length)
	{
		this.elements.splice(x,1);
		this.Fetch();
	}
}

TList.prototype.MoveUp = function(x)
{
	if(x > 0 && x < this.elements.length)
	{
		var cache = this.elements[x];
		this.elements[x] = this.elements[x-1];
		this.elements[x-1] = cache;
		this.Fetch();
	}
}

TList.prototype.MoveDown = function(x)
{
	if(x >= 0 && x < this.elements.length - 1)
	{
		var cache = this.elements[x];
		this.elements[x] = this.elements[x+1];
		this.elements[x+1] = cache;
		this.Fetch();
	}
}

TList.prototype.Fetch = function()
{
	if(this.beforeFetch) this.beforeFetch();

	var tbodyO = document.getElementById(this.tbodyId);
	if (tbodyO != null)
	{
		var tbody = tbodyO.cloneNode(false)
		tbodyO.parentNode.replaceChild(tbody, tbodyO); //vide le tbody
		
		var num = this.elements.length;
		for(var x = 0; x < num; x++)
		{
			var row = this.elements[x].createRow(x);
			tbody.appendChild(row);
		}
	}
	
	if(this.afterFetch) this.afterFetch();
}
