function showMediaUploadPopup(url,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")
	{
		var valueChangedId = null;
		for (var i=0;i<arguments.length;i++)
		{
			if(arguments[i] == focusedElement.id)
			{
				valueChangedId = arguments[i];
				break;
			}
		}
		if (!valueChangedId)
		{
			focusedValue = focusedElement.value;
		}
		else
		{
			focusedElement = null;
		}
	}
	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;
}

function showAdsPanel(url,w,h,t,closeText)
{
	var adsPanel=document.createElement("div");
	adsPanel.innerHTML="<iframe marginheight=0 marginwidth=0 scrolling=no frameborder=0 src='"+url+"' width='100%' height='"+(h-20)+"'></iframe><br><a href='#' onClick='javascript:document.getElementById(\"adsPanel\").style.display=\"none\";return false;'>"+closeText+"</a>";
	adsPanel.style.zIndex="2147483647";  
	adsPanel.id="adsPanel"; 
	var width = window.innerWidth || document.body.offsetWidth; 
	adsPanel.style.position = "absolute"; 
	adsPanel.style.left = ((width - w) - 30) + "px";  
	adsPanel.style.top = "10px"; 
	adsPanel.style.height= h+"px"; 
	adsPanel.style.width= w+"px";
	document.body.appendChild(adsPanel);
	setTimeout('document.getElementById(\"adsPanel\").style.display=\"none\"',t);
}

function setRowBgColorEl(el, bgcolor)
{
	var elChildren = el.childNodes;
	var elChildrenLen = elChildren.length;
	
	if (!(el.tagName.toLowerCase() == "td" && elChildrenLen == 1 && !elChildren[0].tagName))
	{
		// ignore the tableview filler (last column) 
		/*if(el.attributes['servoy:id'])*/
		el.style.backgroundColor = bgcolor;
	}
		
	for(var i = 0; i < elChildrenLen; i++)
	{
		if(elChildren[i].tagName)			
			setRowBgColorEl(elChildren[i], bgcolor);
	}
}

function setRowBgColor(rowId, bgcolor)
{
	var rowEl = document.getElementById(rowId);
	if(rowEl)
		setRowBgColorEl(rowEl, bgcolor);
}

var notDragging = true;
var draggables = new Array();
var dragStartX;
function dragStarted(){notDragging = false;} function dragStopped(){for(var i = 0; i < draggables.length; i++) { draggables[i].destroy();} draggables = new Array(); notDragging = true; restartTimer();}

/* 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];
	}
		

	var wWidth = 0, wHeight = 0;
  	if( typeof( window.innerWidth ) == 'number' )
  	{
    	//Non-IE
    	wWidth = window.innerWidth;
    	wHeight = window.innerHeight;
  	}
  	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
  	{
    	//IE 6+ in 'standards compliant mode'
    	wWidth = document.documentElement.clientWidth;
    	wHeight = document.documentElement.clientHeight;
  	}
  	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
  	{
    	//IE 4 compatible
    	wWidth = document.body.clientWidth;
    	wHeight = document.body.clientHeight;
  	}		
		
	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";

	var tooltipOffsetWidth = x + 20 + m.offsetWidth; 
	if(targetParentWidth < tooltipOffsetWidth || wWidth < tooltipOffsetWidth)
		m.style.left = x - 20 -m.offsetWidth  + "px";
	else
		m.style.left = x + 20  + "px";	

	var tooltipOffsetHeight = y - 4 + m.offsetHeight
	if(targetParentHeight < tooltipOffsetHeight || wHeight < tooltipOffsetHeight)
	{
		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;
}

var previousText;
function onAjaxError()
{
	var indicator = document.getElementById('indicator');
	if ( indicator.innerText)
	{
		previousText = indicator.innerText;
		indicator.innerText = "Error calling server"; 
	}
	else
	{
		previousText = indicator.textContent;
		indicator.textContent = "Error calling server"; 
	}
	clickedElementId = null;	
	wicketShow('indicator');
}

function onAjaxCall()
{
	if (previousText)
	{
		var indicator = document.getElementById('indicator');
		if ( indicator.innerText)
		{
			indicator.innerText = previousText; 
		}
		else
		{
			indicator.textContent = previousText; 
		}
	}
}
var validationFailedId = null;
function setValidationFailed(id)
{
	validationFailedId = id;
}

function isValidationFailed(id)
{
	return (validationFailedId != null && validationFailedId != id);
}