/*
 * $Id: CssHelper.js 84386 2009-10-01 11:47:48Z k.reimer $
 * Copyright (C) 2007 IP Labs GmbH <http://www.iplabs.de/>
 * All rights reserved.
 */

 
/**
 * helper css methods
 * 
 * @author Vladimir Sakhovski
 * @version $Revision: 84386 $
 */

function CssHelper() {
}

/** returns whether an element has a class */
CssHelper.hasClass = function(element, className) {
	if (!element.className) return false;
   	return (" " + element.className + " ").indexOf(" " + className + " ") != -1;
};

/** adds a class to an element, maybe a second time */
CssHelper.addClass = function(element, className) {
	if (CssHelper.hasClass(element, className))  return;
	var old = element.className ? element.className : "";
	element.className = old + " " + className;
};

/** removes a class from an element */
CssHelper.removeClass = function(element, className) {
	var old = element.className ? element.className : "";
	element.className = old.replace(className, "");
};

/** 
 * sets style.display for specified element, 
 * if element does not exists, silently ignore. 
 */
CssHelper.setDisplay = function(elId, display)
{
	var el = document.getElementById(elId);
	if (!el) { return; }
	el.style.display = display;
};


/** 
 * Removes all children from specified element. 
 */
CssHelper.removeAllChildren = function(el)
{
	while (el.hasChildNodes()) {
		el.removeChild(el.firstChild);
	}
};

/** 
 * Append an element to the parent,
 * fill it with the optional text.
 *
 * @parameter parentEl parent element to which newly created element should belong
 * @parameter newEl an Object with the settings for the element to be created
 * 					the following attributes can be set:
 *		elType - required, type of the new element like TR, INPUT, etc.
 *		common elemnts attrs : className, id, onclick, onmouseover, onmouseout
 *		INPUT elements attrs : type, value, checked, disabled
 *		IMG elements attrs : src
 *					
 *		special attr :	aspHelpFieldId - optional element attr with id for aspHelpContent 
 * 		text : if present this will be the text content of the newly created element
 * 
 */
CssHelper.appendElement = function(parentEl, newEl)
{
	var elType = newEl.elType.toUpperCase();
	
	var el;
	if ('TR' == elType) {
		// parentEl can only be a table
		el = parentEl.insertRow(-1);
	} 
	else if ('INPUT' == elType) {

		if (typeof newEl.type == 'undefined') {
			alert('type attribute is required for the input elements');
			return null;  
		}
		
		try {
   			el = parentEl.appendChild(document.createElement('<input type="'+newEl.type+'">'));
	    }
	    catch(err) {
	    	el = parentEl.appendChild(document.createElement('input'));
	     	el.type = newEl.type;
	    }
		
		if (typeof newEl.checked != 'undefined') { el.checked = newEl.checked; }
		if (typeof newEl.disabled != 'undefined') { el.disabled = newEl.disabled; }
		if (typeof newEl.value != 'undefined') { el.value = newEl.value; }
	} 
	else 
	{
		el = parentEl.appendChild(document.createElement(elType));
	}

	// element-specific attributes
	if ('IMG' == elType) {
		if (typeof newEl.src != 'undefined') { el.src = newEl.src; }
	}

	if (typeof newEl.className != 'undefined') { el.className = newEl.className; }
	if (typeof newEl.id != 'undefined') { el.id = newEl.id; }
	
	if (typeof newEl.onclick != 'undefined') { el.onclick = newEl.onclick; }
	if (typeof newEl.onmouseover != 'undefined') { el.onmouseover = newEl.onmouseover; }
	if (typeof newEl.onmouseout != 'undefined') { el.onmouseout = newEl.onmouseout; }

	if (typeof newEl.aspHelpFieldId != 'undefined') { el.aspHelpFieldId = newEl.aspHelpFieldId; }
	
	if (typeof newEl.text != 'undefined') { el.appendChild(document.createTextNode(newEl.text)); }
	
	return el;
};

/** 
 * De-activate specified control element. 
 */
CssHelper.deactivateControlEl = function(controlElId)
{
	var controlEl = document.getElementById(controlElId);
	if (!controlEl) return;
	
	var isTxtControlBtn = CssHelper.hasClass(controlEl, 'txtControlBtn');
	
	var deactEl = controlEl.cloneNode(true);
	deactEl.id = controlEl.id + '_deactivated';
	CssHelper.addClass(deactEl, 'deactivated');
	deactEl.onclick = null;
	deactEl.onmouseover = null;
	deactEl.onmouseout = null;
	
	if (!isTxtControlBtn)
	{
		var innerDiv = deactEl.appendChild(document.createElement("DIV"));
		innerDiv.style.position = 'absolute';
		innerDiv.style.width = '100%';
		innerDiv.style.height = '100%';
		innerDiv.style.top = '0px';
		innerDiv.style.left = '0px';
		innerDiv.style.background = '#FFF';
		innerDiv.style.opacity = '.5';
		innerDiv.style.color = '#FFF';
		innerDiv.style.fontSize = 'xx-small';	
		innerDiv.appendChild(document.createTextNode('x'));
	}
	controlEl.style.display = 'none';
	controlEl.parentNode.insertBefore(deactEl, controlEl.nextSibling);
};

/** 
 * Reactivate specified control element. 
 */
CssHelper.reactivateControlEl = function(controlElId)
{
	var deactEl = document.getElementById(controlElId+'_deactivated');
    if (deactEl)
    {
    	deactEl.parentNode.removeChild(deactEl);
    }

    var controlEl = document.getElementById(controlElId);
    if (controlEl.type == 'DIV')
    {
    	CssHelper.setDisplay(controlElId,'block');
    }
    else
    {
    	CssHelper.setDisplay(controlElId,'inline');
    }
};