function ToggleHelper(pHelperId, pImgId)
{
	var ImgElement = document.getElementById(pImgId);
	var HelperElement = document.getElementById(pHelperId);

	if ('none' == HelperElement.style.display)
	{
		HelperElement.style.display = 'block';
		ImgElement.title = 'click to hide help';
	}
	else
	{
		HelperElement.style.display = 'none';
		ImgElement.title = 'click to show help';
	}
}

function GetRootElementByTagName(pElement, pTagName)
{
	var parent = pElement.parentNode;

	if (pTagName.toUpperCase() == parent.tagName)
	{
		return parent;
	}
	else
	{
		var new_parent = GetRootElementByTagName(parent, pTagName);
		return new_parent;
	}
}


function GetAbsPosition(pElement)
{
	var position = new Object;

	position.x = 0;
	position.y = 0;

	if (null != pElement)
	{
		position.x = pElement.offsetLeft;
		position.y = pElement.offsetTop;

		if (null != pElement.offsetParent)
		{
			var parentpos = GetAbsPosition(pElement.offsetParent);
			position.x += parentpos.x;
			position.y += parentpos.y;
		}
	}

	position.cx = pElement.offsetWidth;
	position.cy = pElement.offsetHeight;

	return position;
}


function ToggleSection(pSectionId, pToggleImageId)
{
	var e = document.getElementById(pSectionId);
	var p = document.getElementById(pToggleImageId);

	if (null != e && null != p)
	{
		if ('none' == e.style.display)
		{
			e.style.display = 'block';
			p.src = 'img/section_up.gif';
			p.title = 'click to hide section';
		}
		else
		{
			e.style.display = 'none';
			p.src = 'img/section_down.gif';
			p.title = 'click to show section';
		}
	}
}

function ShowSection(pSectionId)
{
	var e = document.getElementById(pSectionId);

	if (null != e)
	{
		// show KTML if exists
		e_ktml = GetKTML(pSectionId);

		if (null != e_ktml)
		{
			ShowKTML(e_ktml);
		}

		e.style.display = 'block';
	}
}

function HideSection(pSectionId)
{
	var e = document.getElementById(pSectionId);

	if (null != e)
	{
		// hide KTML if exists
		e_ktml = GetKTML(pSectionId);

		if (null != e_ktml)
		{
			HideKTML(e_ktml);
		}

		e.style.display = 'none';
	}
}

/**
 * @return bool true if section is visible
 * @param string pSectionId
 */
function SectionVisible(pSectionId)
{
	var e = document.getElementById(pSectionId);

	if (null != e)
	{
		if ('none' == e.style.display)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}

function AddSubmit(name, id)
{
	el = document.createElement('input');
	el.setAttribute('type', 'hidden');
	el.setAttribute('name', name);

	frm = document.getElementById(id);
	frm.appendChild(el);
}

function DisableSubmits()
{
	var btn = document.body.getElementsByTagName('input');

	for(i = 0; i < btn.length; i++)
	{
		att = btn[i].attributes;
		type = att.getNamedItem('type');

		if (type.nodeValue == 'submit')
		{
			btn[i].setAttribute('disabled', 'yes');
		}
	}
}

/**
 * @return mixed iframe with KTML editor or null
 * @param string pSectionId
 */
function GetKTML(pSectionId)
{
	var section = document.getElementById(pSectionId);

	var e_ul = section.getElementsByTagName('ul');

	var e_li = e_ul[0].getElementsByTagName('li');

	for (var i = 0; i < e_li.length; i++)
	{
		// get all div elements
		var e_div = e_li[i].getElementsByTagName('div');

		for (var j = 0; j < e_div.length; j++)
		{
			if ('XInput' == e_div[j].className)
			{
				var xinput_kids = e_div[j].getElementsByTagName('iframe');

				if (0 < xinput_kids.length)
				{
					return xinput_kids[0];
				}
			}
		}
	}

	return null;
}

function HideKTML(pElement)
{
	pElement.style.visible = 'hidden';
	pElement.style.height = '0px';
	HtmlBoxSubmit();
}

function ShowKTML(pElement)
{
	pElement.style.visible = '';
	pElement.style.height = '400px';

	var value = pElement.src;
	content_edit.location.href = encodeURI(value);

	SetContent();

}
