/**
 * Returns the layer (or: element) corresponding the given ID
 * @param String layer
 * @return Layer
 */
function getLayer( layer ) 
{
	if( document.getElementById ) 
	{
		return document.getElementById( layer )
	} 
	else if( document.all ) 
	{
		return document.all[layer]
	} 
	else 
	{
		return document[layer]
	}
}

function getLayerFromObject( layer, obj ) 
{
	obj = getLayer(obj);
	
	if( obj.getElementById ) 
	{
		return obj.getElementById( layer )
	} 
	else if( obj.all ) 
	{
		return obj.all[layer]
	} 
	else 
	{
		return obj[layer]
	}
}

/** 
 * Displays a new image in the image-element
 * @param String imageElementID the ID from the image element witch image must be swapped
 * @param newImageName the name of the new image, including the relative path (e.g. '/test.jpg')
 */
function swapImage( imageElementID, newImageName )
{
	getLayer( imageElementID ).src = newImageName;
}

function setVisibility( elementID, visibleType )
{
	getLayer( elementID ).style.visibility = visibleType;
}

function setDisplay( elementID, displayType )
{
	getLayer( elementID ).style.display = displayType;
}

function followmouse( mouseEvent )
{
	var xcoord = offsetFromMouse[0];
	var ycoord = offsetFromMouse[1];

	var docwidth  = document.all ? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth - 15;
	var docheight = document.all ? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight);

	//if (document.all){
	//	gettrailobjnostyle().innerHTML = 'A = ' + truebody().scrollHeight + '<br/>B = ' + truebody().clientHeight;
	//} else {
	//	gettrailobjnostyle().innerHTML = 'C = ' + document.body.offsetHeight + '<br/>D = ' + window.innerHeight;
	//}

	if (typeof mouseEvent != "undefined"){
		if (docwidth - mouseEvent.pageX < 380){
			xcoord = mouseEvent.pageX - xcoord - 400; // Move to the left side of the cursor
		} else {
			xcoord += mouseEvent.pageX;
		}
		if (docheight - mouseEvent.pageY < (currentimageheight + 110)){
			ycoord += mouseEvent.pageY - Math.max(0,(110 + currentimageheight + mouseEvent.pageY - docheight - truebody().scrollTop));
		} else {
			ycoord += mouseEvent.pageY;
		}

	} else if (typeof window.event != "undefined"){
		if (docwidth - event.clientX < 380){
			xcoord = event.clientX + truebody().scrollLeft - xcoord - 400; // Move to the left side of the cursor
		} else {
			xcoord += truebody().scrollLeft+event.clientX
		}
		if (docheight - event.clientY < (currentimageheight + 110)){
			ycoord += event.clientY + truebody().scrollTop - Math.max(0,(110 + currentimageheight + event.clientY - docheight));
		} else {
			ycoord += truebody().scrollTop + event.clientY;
		}
	}

	if(ycoord < 0) { ycoord = ycoord*-1; }
	gettrailobj().left=xcoord+"px"
	gettrailobj().top=ycoord+"px"
}


var offsetFromMouse 	= [15,15]; //image x,y offsets from cursor position in pixels. Enter 0,0 for no offset
var displayDuration 	= 0; //duration in seconds image should remain visible. 0 for always.
var currentimageheight 	= 270;	// maximum image size.

/**
 * Sets the position of the popup (usualy an DIV element) so, that it is totaly visible in the page
 * and that you don't have to scroll to see the whole popup
 */
function setPopupPosition( popupID )
{
	var xcoord = offsetFromMouse[0];
	var ycoord = offsetFromMouse[1];

	var docwidth  = document.all ? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth - 15;
	var docheight = document.all ? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight);

	if (typeof mouseEvent != "undefined")
	{
		if (docwidth - mouseEvent.pageX < 380){
			xcoord = mouseEvent.pageX - xcoord - 400; // Move to the left side of the cursor
		} else {
			xcoord += mouseEvent.pageX;
		}
		if (docheight - mouseEvent.pageY < (currentimageheight + 110)){
			ycoord += mouseEvent.pageY - Math.max(0,(110 + currentimageheight + mouseEvent.pageY - docheight - truebody().scrollTop));
		} else {
			ycoord += mouseEvent.pageY;
		}
	} 
	else if (typeof window.event != "undefined")
	{
		if (docwidth - event.clientX < 380){
			xcoord = event.clientX + truebody().scrollLeft - xcoord - 400; // Move to the left side of the cursor
		} else {
			xcoord += truebody().scrollLeft+event.clientX
		}
		if (docheight - event.clientY < (currentimageheight + 110)){
			ycoord += event.clientY + truebody().scrollTop - Math.max(0,(110 + currentimageheight + event.clientY - docheight));
		} else {
			ycoord += truebody().scrollTop + event.clientY;
		}
	}

	if(ycoord < 0) { ycoord = ycoord*-1; }
	
	getLayer( popupID ).left= xcoord+"px"
	getLayer( popupID ).top = ycoord+"px"
}

function checkForDecimals( elemID, elemValue, foutmeldingstekst )
{
	checkPassed = false;
	var reverseValue = elemValue.split('').reverse().join('');

	if (reverseValue.substring(1, 2) == ",") 
	{
		getLayer(elemID).value = elemValue + '0';
		checkPassed = true;		
	}
	else if (reverseValue.substring(1,2) == ".")
	{
		getLayer(elemID).value = elemValue + '0';
		checkPassed = true;		
	}
	else if ( (reverseValue.indexOf(',', 0) < 0) && 
	     (reverseValue.indexOf('.', 0) < 0) )
	{
		getLayer(elemID).value = elemValue + ',00';
		checkPassed = true;
	}
	else if ( reverseValue.indexOf(',', 0) == 2 ) 
	  checkPassed = true;
	else if ( reverseValue.indexOf('.', 0) == 2 )
	  checkPassed = true;
	
	if (checkPassed != true)
	{
		alert( foutmeldingstekst );
	}
	
	getLayer(elemID).value = getLayer(elemID).value.replace('.', ',');
	
	return checkPassed;
}

/*
 * ----------------- SWAP-ITEMS part ---------------------------------------------
 */
var categorieVisible 	= 1;
var categorieCollapsed 	= 0;

var categorieArray		= new Array(20);
categorieArray['categorie1']	= categorieVisible;
categorieArray['categorie2'] 	= categorieVisible;
categorieArray['categorie3'] 	= categorieVisible;
categorieArray['categorie4'] 	= categorieVisible;
categorieArray['categorie5'] 	= categorieVisible;
categorieArray['categorie6'] 	= categorieVisible;
categorieArray['categorie7'] 	= categorieVisible;
categorieArray['categorie8'] 	= categorieVisible;
categorieArray['categorie9'] 	= categorieVisible;
categorieArray['categorie10']	= categorieVisible;
categorieArray['categorie11']	= categorieVisible;
categorieArray['categorie12'] 	= categorieVisible;
categorieArray['categorie13'] 	= categorieVisible;
categorieArray['categorie14'] 	= categorieVisible;
categorieArray['categorie15'] 	= categorieVisible;
categorieArray['categorie16'] 	= categorieVisible;
categorieArray['categorie17'] 	= categorieVisible;
categorieArray['categorie18'] 	= categorieVisible;
categorieArray['categorie19'] 	= categorieVisible;
categorieArray['categorie20']	= categorieVisible;

function swapCategorie( categorieItem, updateViaAJAX )
{
	if (updateViaAJAX == null)
	  updateViaAJAX = true;
	  
	if ( getLayer( categorieItem + '_header' ) != null)
	{
		if (categorieArray[categorieItem] == categorieVisible)
		{
			getLayer( categorieItem + '_header' ).className = 'categorie_header_collapsed';
			setDisplay( categorieItem + '_body',   'none' );
			//setDisplay( categorieItem + '_footer', 'none' );
			
			categorieArray[categorieItem] = categorieCollapsed;
		}
		else
		{
			getLayer( categorieItem + '_header' ).className = 'categorie_header_visible';
			setDisplay( categorieItem + '_body',   'block' );
			//setDisplay( categorieItem + '_footer', 'block' );
					
			categorieArray[categorieItem] = categorieVisible;
		}	
	}
	
	if (updateViaAJAX)
	  saveVisibilityInPHPSession( categorieItem, categorieArray[categorieItem] );
}

function saveVisibilityInPHPSession( menuItem, itemState )
{
	var xmlhttp;
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	  	xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	//xmlhttp.onreadystatechange=function()
	//{
	//	if (xmlhttp.readyState==4 && xmlhttp.status==200)
	//    {
	//    	document.getElementById("saveResult").innerHTML=xmlhttp.responseText;
	//    }
	//}
	var URL = "ajax/menuStatusFrontEnd.php?menuItem=" + menuItem + "&itemState=" + itemState;
	xmlhttp.open("GET", URL ,true);
	xmlhttp.send();
}
