//
// This file contains all kind of utility Javascript functions
//




//
// Function returns the dimensions of the current browser window.
// Return width and height in an array.
//
function getScreenDimension() {

	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || window.document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return [myWidth, myHeight];
}


//
//
//
function getPageEventCoords(evt) {
	var coords = {left:0, top:0};
	if (evt.pageX) {
		coords.left = evt.pageX;
		coords.top = evt.pageY;
	} else if (evt.clientX) {
		coords.left =
		evt.clientX + document.body.scrollLeft - document.body.clientLeft;
		coords.top =
		evt.clientY + document.body.scrollTop - document.body.clientTop;
		// include html element space, if applicable
		if (document.body.parentElement && document.body.parentElement.clientLeft) {
			var bodParent = document.body.parentElement;
			coords.left += bodParent.scrollLeft - bodParent.clientLeft;
			coords.top += bodParent.scrollTop - bodParent.clientTop;
		}
	}

	return coords;
}


//
// Function returns the scroll coordinates. When user did not scroll (0,0) is returned. When used scrolled down to bottom of page. The function
// returns for instance ( 450, 0).
//
function getScrollXY() {


	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [scrOfX, scrOfY];
}

/**
 * Returns the scroll coordinates relative to a given element.
 *
 * Note:	This function is added since the 'getScrollXY' does not
 * 			work within TTS. This is caused by some weird html/body
 * 			overflow properties (css). I'm not sure if we can just
 * 			remove those, so I implemented this work-around.
 */
function getScrollXYRelativeTo( elementId )
{
	var scrollLeft = 0;
	var scrollTop  = 0;

	var element = document.getElementById( elementId );
	if ( element != null )
	{
		scrollLeft = element.scrollLeft;
		scrollTop = element.scrollTop;
	}

	return [ scrollLeft , scrollTop ];
}


function getScrollOffsetY()
{
	var scrollTop = document.body.scrollTop;

	if (scrollTop == 0)
	{
	    if (window.pageYOffset)
	        scrollTop = window.pageYOffset;
	    else
	        scrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}
	return scrollTop;
}


function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function findPos( obj )
{
	try {
		var curleft = 0;
		var curtop = 0;
		if (obj.offsetParent) {
			do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
		}
		return [ curleft, curtop ];
	} catch (e) {
		//Robert: 'Unspecified error'
		//Well this is a catch for an IE8 JS error
		//which i don't know how to handle, but
		//does not seem to corrupt the application.
	}
	return [0, 0];
}

/**
 * Float div width the given id below another div also specified by id.
 *
 * Usage:	'floatDivBelowOther( "targetId" , "otherId" ).floatIt();' -> Please note the call to 'floatIt()'!
 *
 * Example:
 *     <html>
 *       <body>
 *         <div id="floatingOne">
 *           The floating content goed here.
 *         </div>
 *         <div id="someOther">
 *           Some other content here.
 *         </div>
 *         <sscript type="text/javascript">
 *           floatDivBelowOther("floatingOne", "someOther").floatIt();
 *         </sscript>
 *       </body>
 *     </html>
 *
 * Note:	This function is based on the "Floating Div" example from http://www.javascript-fx.com
 *
 * @param	targetId			Id of target div to float.
 * @param	otherId				Id of div to float below.
 * @param	extOffTop			Extra top offset to be added.
 *
 * @param	scrollRelativeToId	Id of element to get scroll positions relative to.
 * 								Note:	See the 'getScrollXYRelativeTo' function for more info.
 */
function floatDivBelowOther( targetId , belowId , extOffTop , scrollRelativeToId )
{
	var d  = document;
	var px = d.layers ? "" : "px";
	var ns = (navigator.appName.indexOf("Netscape") != -1);

	var target = d.getElementById ? d.getElementById( targetId ) : d.all ? d.all[ targetId ] : d.layers[ targetId ];
	var other  = d.getElementById ? d.getElementById( belowId  ) : d.all ? d.all[ belowId  ] : d.layers[ belowId  ];

	window[ targetId + "_obj" ] = target;

	if ( d.layers )
		target.style = target;

	target.cx = target.sx = 0;
	target.cy = target.sy = 0;

	/*
	 * Add function to target div to set its floating position based on the given scroll position(s).
	 *
	 * @param	x	Scroll position on X-axis.
	 * @param	y	Scroll position on Y-axis.
	 */
	target.setPosition = function( x , y )
	{
		var viewPortDimensions = getScreenDimension();
		var yPosBelowOther     = findPos(other)[ 1 ] + other.offsetHeight; // Use real (top) position and add the height.

		if ( target.offsetHeight < viewPortDimensions[ 1 ] ) // Element must not scroll when this causes a part of it to stay out of sight.
		{
			if ( y > yPosBelowOther )
			{
				this.style.top= y + extOffTop + px;
			}
			else
			{
				this.style.top= yPosBelowOther + extOffTop + px; // Reset to initial position when scrolled back to the top.
			}
		}
		else
		{
			this.style.top= yPosBelowOther + extOffTop + px; // If browser is resized (gets to small) when already scrolled down -> reset.
		}
	};

	/*
	 * Add function to target div to make it float. This is the one that needs to be invoked from the page.
	 */
	target.floatIt = function()
	{
		var scrollXY = getScrollXYRelativeTo( scrollRelativeToId );
		var pX = scrollXY[0];
		var pY = scrollXY[1];

		if( this.sy < 0 )
		{
			pY += ns ? innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
		}

		this.cx += ( pX + this.sx - this.cx ) / 8;
		this.cy += ( pY + this.sy - this.cy ) / 8;

		this.setPosition( this.cx , this.cy );

		setTimeout( this.id + "_obj.floatIt()" , 40 );
	}

	return target;
}

//
//getPageSize()
//Returns array with page width, height and window width, height

function getPageSize(){

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}

function runApplicationWithActiveX(path) {

	//activeX can only be used under Internet Explorer
	if(!Prototype.Browser.IE){
		alert('Deze applicatie kan alleen opgestart worden in Internet Explorer');
		return false;
	}

	try{
		WSH=new ActiveXObject("WScript.Shell");
		WSH.run(path);
	}catch(err){
		return false;
	}
}

/**
 * Start blocking  "Enter" key events for the given document.
 *
 * @param   doc Document to ignore "Enter" key events for.
 */
function blockEnterKeyForTextInputs( doc )
{
	doc.onkeypress = ignoreEnterKeyForTextNodes;
}

function ignoreEnterKeyForTextNodes(evt)
{
	var evt = (evt) ? evt : ((event) ? event : null);
	var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
	if ((evt.keyCode == 13) && (node.type == "text")) {
		return false;
	}
}

