var mediafieldid;
function showMediaUploadPopup(url,imgid)
{
	mediafieldid = imgid;
	var x = 0, y = 0;
	
	if (document.all) {
		x = window.screenTop + 100;
		y = window.screenLeft + 100;
	}
	else if (document.layers) {
		x = window.screenX + 100;
		y = window.screenY + 100;
	}else {// firefox, need to switch the x and y?
		y = window.screenX + 100;
		x = window.screenY + 100;
	}
	window.open(url,'upload', 'top=' + x + ',left=' + y + ',screenX=' + x + ',screenY=' + y + ',location=no,toolbar=no,menubar=no,width=380,height=140')
}

function showMediaDownloadPopup(url)
{
	var x = 0, y = 0;
	
	if (document.all) {
		x = window.screenTop + 100;
		y = window.screenLeft + 100;
	}
	else if (document.layers) {
		x = window.screenX + 100;
		y = window.screenY + 100;
	}else {// firefox, need to switch the x and y?
		y = window.screenX + 100;
		x = window.screenY + 100;
	}
	window.open(url,'download','top=' + x + ',left=' + y + ',screenX=' + x + ',screenY=' + y + ',location=no,toolbar=no,menubar=no,width=310,height=140,resizable=yes')
}

function setAjaxIndicatorLocation(indicatorMarkupId, x ,y)
{
	var indicator = document.getElementById(indicatorMarkupId);
	indicator.style.top = x +"px";
	indicator.style.left = y +"px";
}

function requestFocus(focusId)
{
	if (focusId != "" && focusId != null)
	{
		var toFocus = document.getElementById(focusId);
		if (toFocus != null && toFocus.focus) {
			try {
				toFocus.focus();
			} catch (ignore) {
			}
		}
	}
}

var focusingOnInvalidComponent = false;
function scheduleRequestFocusOnInvalid(focusId, value)
{
	if (focusingOnInvalidComponent) return;
	focusingOnInvalidComponent = true;
	window.setTimeout('focusIfUnchanged(\"' + focusId + '\", ' + value + ');' ,1);
}

function focusIfUnchanged(focusId, value)
{
	if (focusId != "" && focusId != null)
	{
		var toFocus = document.getElementById(focusId);
		if (toFocus != null && toFocus.focus && (toFocus.checked == value || toFocus.value == value)) {
			try {
				toFocus.focus();
			} catch (ignore) {
			}
		}
	}
	focusingOnInvalidComponent = false;
}

var clickedElementId = null;
function testDoubleClickId(elementId)
{
	if (elementId == clickedElementId)
	{
		Wicket.Log.info("element id already clicked: " + clickedElementId);
		return false;
	} 
	clickedElementId = elementId;
	return Wicket.$(elementId) != null;
}

function clearDoubleClickId(elementId)
{
	if (elementId == clickedElementId)
	{
		clickedElementId = null;
	}
	else
	{
		Wicket.Log.info("element id was already another: " + clickedElementId + ", " + elementId + " tried to clear it");
	}
}
	
var focusedValue = null;
var focusedElement = null;
function storeValueBeforeUpdate()
{
	focusedElement = Wicket.Focus.getFocusedElement();
	if (typeof(focusedElement) != "undefined" && focusedElement != null
	 && focusedElement.type != "button" && focusedElement.type != "submit")
	{
		focusedValue = focusedElement.value;
	}
	else
	{
		focusedElement = null;
	}
}

function restoreValueAfterUpdate()
{
	var element = Wicket.Focus.getFocusedElement();
	if (focusedElement != null && element != null  
		&& element.id == focusedElement.id 
		&& typeof(element) != "undefined"
		&& element.value != focusedValue)
	{
		element.value = focusedValue;
	}
	focusedElement = null;
}

function testEnterKey(e, script) 
{
     var code;
     
     if (!e) e = window.event;
     if (!e) return true;
     if (e.keyCode) code = e.keyCode;
     else if (e.which) code = e.which;
     
     if(code==13)
     {
        if (script) script();
	    return false;
     }
     return true;
}

function filterBackKey(e) 
{
     var code;
     
     if (!e) e = window.event;
     if (!e) return true;
     if (e.keyCode) code = e.keyCode;
     else if (e.which) code = e.which;
     
     if(code==8)
     {
	    return false;
     }
     return true;
}

function rearrageTabsInTabPanel(tabPanelId)
{
	var tabPanel = document.getElementById(tabPanelId);
	if (tabPanel)
	{
		var ul = tabPanel.getElementsByTagName("ul")[0];
		var tabs = ul.getElementsByTagName("li");
		var selectedRowIndex = -1;
	
		// Split the tabs into rows. The tabs in a row will be stored into an array,
		// and all the arrays representing the rows will be stored into another array
		// (a matrix-like structure, but with possibly different sized rows).
		var rows = new Array();
		var currentRow;
		var prevTop;
		for (var i=0; i<tabs.length; i++)
		{
			var tab = tabs[i];
			// The coordinates of the <li> element seems to be wrongly reported in various browsers 
			// (in firefox they appear all on the same line, in IE the first <li> is reported wrong,
			// in Opera they work ok). So we'll just use the coordinates of the inner <a> element,
			// which seem to be reported ok in all browsers.
			var anch = tab.getElementsByTagName("a")[0];
			var currentTop = anch.offsetTop;
			// If this is the first tab, we don't have a previous tab to compare with.
			// We just create a new row-array.
			if (i==0)
			{
				currentRow = new Array();
			}
			// If not the first tab, compare with previous element. If the vertical coordinate is
			// different, then start a new row-array.
			else if (prevTop != currentTop)
			{
				rows.push(currentRow);
				currentRow = new Array();
			}
			// If we get the selected tab, remember its index.
			if ((tab.className == "selected_tab") || (tab.className == "disabled_selected_tab"))
				selectedRowIndex = rows.length;
			currentRow.push(tab);
			prevTop = currentTop;
		}
		rows.push(currentRow);
		
		// If we have no selected row, then just take the first row.
		if (selectedRowIndex == -1)
			selectedRowIndex = 0;
		
		if (rows.length > 1)
		{		
			// This will be the index of the new top row (based on circular
			// shift of rows).	
			var newFirstRow = (selectedRowIndex - 1 + rows.length) % rows.length;

			// Now create separate <ul> elements for each row and move
			// the tabs into these new elements. We'll make some adjustment
			// to the style of the <ul>s, in order to have correct spacing 
			// between rows.
			var domRows = new Array();
			for (var i=0; i<rows.length; i++)
			{
				var domRow = document.createElement("ul");
				domRow.className = "tabs";
				if (i == selectedRowIndex)
				{
					domRow.style.marginTop = "0px";
					domRow.style.paddingTop = "0px";
				}
				else if (i == newFirstRow)
				{
					domRow.style.borderBottom = "0px";
					domRow.style.marginBottom = "0px";
					domRow.style.paddingBottom = "0px";
				}
				else
				{
					domRow.style.marginTop = "0px";
					domRow.style.marginBottom = "0px";
					domRow.style.borderBottom = "0px";
					domRow.style.paddingTop = "0px";
					domRow.style.paddingBottom = "0px";
				}
				for (var j=0; j<rows[i].length; j++)
				{
					var tab = rows[i][j];
					ul.removeChild(tab);
					domRow.appendChild(tab);
				}
				domRows.push(domRow);
			}

			
			// Now remove the initial <ul> and add the newly created ones.
			// Add them in the correct order, so that the selected row
			// will be placed at the bottom.
			var firstControl = ul;
			for (var i=selectedRowIndex; i<rows.length; i++)
			{
				tabPanel.insertBefore(domRows[i], firstControl);
				firstControl = domRows[i];
			}
			for (var i=0; i<selectedRowIndex; i++)
			{
				tabPanel.insertBefore(domRows[i], firstControl);
				firstControl = domRows[i];
			}
			tabPanel.removeChild(ul);	
		}
	}
}

function testStyleSheets()
{
	if(document.styleSheets.length >= 29)
	{
		window.location.reload();
	}
}

function setStatusText(str)
{
	window.status = str;
}


/* Tooltip fctions */

var tipTimeout;

function showtip(e,message)
{
	var x = 0;
	var y = 0;
	var m;

	if(!e)
		var e = window.event;

	var targetParentWidth = 0;
	var targetParentHeight = 0;
	var src = e.target;	// get the target element
	
	// for IE
	if(!src)
		src = e.srcElement;
		
	if(src.parentNode)
	{
		var positionXY = getXY(src.parentNode);
		var sizeWH = getRootElementSize(src.parentNode);
		targetParentWidth = positionXY[0] + sizeWH[0];
		targetParentHeight = positionXY[1] + sizeWH[1];
	}
		
	if(e.pageX || e.pageY)
	{
		x = e.pageX;
		y = e.pageY;
	}
	else
		if(e.clientX || e.clientY)
		{
			x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
	m = document.getElementById('mktipmsg');

	
	m.innerHTML = message;
	m.style.display = "block";

	if(targetParentWidth < x + 20 + m.offsetWidth)
		m.style.left = x - 20 -m.offsetWidth  + "px";
	else
		m.style.left = x + 20  + "px";	

	if(targetParentHeight < y - 4 + m.offsetHeight)
	{
		m.style.top = y - 4 - m.offsetHeight  + "px";
	}
	else
		m.style.top = y - 4 + "px";		
	
	m.style.zIndex = 203;
	
	tipTimeout = setTimeout("hidetip();", 5000);
}

function hidetip()
{
	clearTimeout(tipTimeout);
	var m;
	m = document.getElementById('mktipmsg');
	m.style.display = "none";
}

function getXY(oElement)
{
	var iReturnValue = new Array();
	iReturnValue[0] = 0;
	iReturnValue[1] = 0;
	while( oElement != null )
	{
		iReturnValue[0] += oElement.offsetLeft;
		iReturnValue[1] += oElement.offsetTop;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}

function getRootElementSize(oElement)
{
	var iReturnValue = new Array();
	iReturnValue[0] = 0;
	iReturnValue[1] = 0;
	while( oElement != null )
	{
		iReturnValue[0] = oElement.offsetWidth;
		iReturnValue[1] = oElement.offsetHeight;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}