var g_bSubmitButtonClicked = false;

function handleFormSubmit()
{
	var b = g_bSubmitButtonClicked;
	
	g_bSubmitButtonClicked = false;
	
	return b;
}

function handleSubmitButtonClick(pButton)
{
	g_bSubmitButtonClicked = true;
}

function submitFormViaLink(pLink)
{
	var pform = document.getElementById("cms_form");
	if(pform) {
		pform.action = pLink.href;
		pform.submit();
	}
	return false;
}


function imagemenu_switchImage(pImg, sFN)
{
	pImg.src_old = pImg.src;
	pImg.src = sFN;
}

function imagemenu_restoreImage(pImg)
{
	pImg.src = pImg.src_old;
}

function formattedTB_bold(sName)
{
	var tb = document.getElementById(sName+".textbox");
	encloseSelection(tb, "[b]", "[/b]");
}

function formattedTB_heading(sName, iLevel)
{
	var tb = document.getElementById(sName+".textbox");
	encloseSelection(tb, "[h"+iLevel+"]", "[/h"+iLevel+"]");
}

function formattedTB_image(sName)
{
	var tb = document.getElementById(sName+".textbox");
	var chooser = document.getElementById(sName+".mediachooser");
	insertTextAtSelection(tb, "[image:"+chooser.value+"]");
}

function formattedTB_download(sName)
{
	var tb = document.getElementById(sName+".textbox");
	var chooser = document.getElementById(sName+".mediachooser");
	insertTextAtSelection(tb, "[download:"+chooser.value+"]");
}

function formattedTB_ilink(sName)
{
	var tb = document.getElementById(sName+".textbox");
	var chooser = document.getElementById(sName+".webpagechooser");
	var stext = prompt("Text?", "");
	if(stext != "") {
		insertTextAtSelection(tb, "[ilink:"+chooser.value+"]"+stext+"[/ilink]");
	}
	//encloseSelection(tb, "[ilink:"+chooser.value+"]", "[/ilink]");
}

function formattedTB_elink(sName)
{
	var tb = document.getElementById(sName+".textbox");
	var url = prompt("URL?", "");
	var stext = prompt("Text?", "");
	if(url != "" && stext != "") {
		insertTextAtSelection(tb, "[elink:"+url+"]"+stext+"[/elink]");
	}
	//encloseSelection(tb, "[elink:"+url+"]", "[/elink]");
}

function encloseSelection(pControl, sPrefix, sSuffix)
{
	pControl.focus();
	
	if(document.selection) {
		var sel = document.selection.createRange();
		
		if(sel.text != "") {
			sel.text = sPrefix + sel.text + sSuffix;
		}
	} else {
		var selstart = pControl.selectionStart;
		var selend = pControl.selectionEnd;
		var text = pControl.value.substring(selstart, selend);
		if(selstart != selend) {
			pControl.value = pControl.value.substring(0, selstart) + sPrefix + text + sSuffix + pControl.value.substring(selend);
		}
	}
}

function insertTextAtSelection(pControl, sText)
{
	pControl.focus();
	
	if(document.selection) {
		var range = document.selection.createRange();
		range.text = sText;
	} else {
		var selstart = pControl.selectionStart;
		var selend = pControl.selectionEnd;
		var text = pControl.value.substring(selstart, selend);
		pControl.value = pControl.value.substring(0, selstart) + sText + pControl.value.substring(selend);
	}
}
function nospam(sUser, sDomain)
{
	document.write("<a href=\"mailto:"+sUser+"@"+sDomain+"\">"+sUser+"@"+sDomain+"</a>");
}

function moveDiv(pDiv, x, y)
{
	pDiv.style.left = x+"px";
	pDiv.style.top = y+"px";
}
function hideDiv(pDiv)
{
	pDiv.style.visibility = "hidden";
}
function showDiv(pDiv)
{
	pDiv.style.visibility = "";
}
function setDivOpacity(pDiv, iOpacity)
{
	if(pDiv.filters) {
		pDiv.filters.alpha.opacity = iOpacity;
	} else if(typeof pDiv.style.MozOpacity == "string") {
		pDiv.style.MozOpacity = iOpacity/100.0;
	}
}

function ImageAnnotator() {
	var FADESPEED = 100;
	var XOFFSET = 9;
	var YOFFSET = 9;
	
	setInterval(doFade, FADESPEED);
	
	var m_pAnnotations=new Array();
	var m_pLayer;
	var m_iFadePercent;
	var m_iCurrentAnnotation = -1;
	var m_bMouseInFlag = false;
	
	this.addAnnotation = addAnnotation;
	this.showAnnotation = showAnnotation;
	this.hideAnnotation = hideAnnotation;
	this.setLayer = setLayer;
	this.doFade = doFade;
	
	function doFade()
	{
		if(m_iFadePercent < 100) {
			m_iFadePercent += 20;
			setDivOpacity(m_pLayer, m_iFadePercent);
			/*m_pLayer.innerHTML = m_iFadePercent;*/
		}
	}
	
	function addAnnotation(x, y, text)
	{
		var a = new Object();
		a.x = x;
		a.y = y;
		a.text = text;
		m_pAnnotations[m_pAnnotations.length] = a;
	}
	
	function showAnnotation(id)
	{
		if(m_iCurrentAnnotation == id) {
			return;
		}
		
		var annotation = m_pAnnotations[id];
		m_iCurrentAnnotation = id;
		
		/* hide */
		hideDiv(m_pLayer);
		
		/* opacity=0 */
		m_iFadePercent = 0;
		setDivOpacity(m_pLayer, 0);
		
		/* move */
		moveDiv(m_pLayer, annotation.x+XOFFSET, annotation.y+YOFFSET);
		
		/* set new text */
		m_pLayer.innerHTML = annotation.text;
		
		/* show */
		showDiv(m_pLayer);
	}
	
	function hideAnnotation()
	{
		hideDiv(m_pLayer);
		m_iCurrentAnnotation = -1;
		m_bMouseInFlag = false;
	}
	
	function setLayer(pLayer)
	{
		m_pLayer = pLayer;
	}
}

function really(pLink)
{
	return confirm("Sind Sie sicher?");
}

function getPosition(obj) {
	var pos = { x:0, y:0 };
	
	do {
		pos.x += obj.offsetLeft;
		pos.y += obj.offsetTop;
	} while(obj = obj.offsetParent);
	
	return pos;
}

/* pass event-Variable in handler: onClick="getClickPosition(this, event)" */
function getClickPosition(pControl, pEvent)
{
	var clickpos = { x:0, y:0 };
	var xPage;
	var yPage;
	
	if(pEvent.pageX) {
		xPage = pEvent.pageX;
		yPage = pEvent.pageY;
	} else if(document.documentElement && event.clientX) {
		xPage = pEvent.clientX + document.documentElement.scrollLeft;
		yPage = pEvent.clientY + document.documentElement.scrollTop;
	}
	
	var ctlpos = getPosition(pControl);
	
	clickpos.x = xPage - ctlpos.x;
	clickpos.y = yPage - ctlpos.y;

	return clickpos;
}

function coordchooser_click(sName, pImage, pEvent)
{
	var ctl_kx = document.getElementById(sName+".xcoord");
	var ctl_ky = document.getElementById(sName+".ycoord");
	
	var clickpos = getClickPosition(pImage, pEvent)
	
	ctl_kx.value = clickpos.x;
	ctl_ky.value = clickpos.y;
}

function showHint(pControl, sHint)
{
	var phint = document.getElementById("hint");
	if(!phint) {
		phint = document.createElement("div");
		phint.id = "hint";
		document.body.appendChild(phint);
	}
	
	phint.style.visibility = "visible";
	phint.innerHTML = sHint;
	phint.style.left = (getPosition(pControl).x+10)+"px";
	phint.style.top = (getPosition(pControl).y+pControl.offsetHeight)+3+"px";
}

function hideHint(pControl)
{
	var phint = document.getElementById("hint");
	if(phint) {
		phint.style.visibility = "hidden";
	}
}

function changeCheckBox(pCheckBox, sHFName)
{
	var hf = document.getElementById(sHFName);
	if(hf) {
		hf.value = (pCheckBox.checked ? "1" : "0");
	}
}
