

function switchHeaderSearchType( newType, nodeToBeReplaced ) {
	jQuery.ajax({
        url: "/AloeView/ajax/switchHeaderSearchType",
        type: 'get',  // get is the default type
        data: {
			newType: newType
		},
		success: function( data, textStatus, jqXHR ) {
			var headerForm = document.getElementById( 'headerSearchForm' );
			if ( headerForm != null ) {
				headerForm.setAttribute( 'action', local_getActionFromType( newType ) );
			}
			changeAppearance( newType, nodeToBeReplaced );
		},
		error: function( jqXHR, textStatus, errorThrown ) {
			// do nothing
		}
	});
}    


function local_getActionFromType( newType, nodeToBeReplaced ) {
	var toReturn = "";
	if ( 'resources' == newType ) {
		toReturn = 'searchResources';
	}
	else if ( 'groups' == newType ) {
		toReturn = 'searchGroups';
	}
	else if ( 'members' == newType ) {
		toReturn = 'searchUsers';
	}
	return( toReturn );
}

function changeAppearance( newType, nodeToBeReplaced ) {
	var possibleTypes = new Array( 'resources', 'groups', 'members' );
	var newHeaderSearchString = "";
	for( var i = 0; i < possibleTypes.length; i++ ) {
		var currentType = possibleTypes[i];
		if ( currentType == newType ) {
			newHeaderSearchString += getActiveElement( currentType );
		}
		else {
			newHeaderSearchString += getPassiveElement( currentType );
		}
	}
	nodeToBeReplaced.innerHTML = newHeaderSearchString;
}


function getActiveElement( givenType ) {
	var printString = global_aloeTranslations[givenType];
	return( "<li class=\"in\"><span>" + printString + "</span></li>" );
}
function getPassiveElement( givenType ) {
	var printString = global_aloeTranslations[givenType];
	return( "<li class=\"out\">"
			+ "<a href=\"javascriptDisabled\" onclick=\""
			+ "switchHeaderSearchType('" + givenType + "',this.parentNode.parentNode);return(false);\">"
			+ printString + "</a></li>" );
}

/* Works perfect for Firefox and Opera and is (in theory) the correct way
 * to manipulate HTML pages using the DOM interface.
 * Does not work in IE, since it does not correctly create the attributes
 * given via setAttribute!
 */
function popupAtMouseCorrect( givenText, event ) {
	var geometry = getGeometry();
	alert( "horizontal scroll: " + geometry.getHorizontalScroll() + ", vertical scroll: " + geometry.getVerticalScroll() );

	var newNode = document.createElement( "div" );
	//var xPos = event.screenX - geometry.getHorizontalScroll();
	//var yPos = event.screenY - geometry.getVerticalScroll();
	var xPos = event.clientX + geometry.getHorizontalScroll();
	var yPos = event.clientY + geometry.getVerticalScroll();
	alert( "x position: " + xPos + ", y position: " + yPos );
	newNode.setAttribute( 'style', 'z-index:100;position:absolute;top:' + yPos + "px;left:"
			+ xPos + "px;background-color:white;padding:20px;" );
	newNode.appendChild( document.createTextNode( givenText ) );

	var formNode = document.createElement( "form" );
	formNode.setAttribute( 'action', 'javascriptDisabled' );

	var okayButton = document.createElement( "input" );
	okayButton.setAttribute( 'class', 'button' );
	okayButton.setAttribute( 'type', 'button' );
	okayButton.setAttribute( 'value', 'Okay' );
	okayButton.setAttribute( 'onclick', 'alert("ready");document.body.removeChild(document.body.firstChild);return(false);' );

	formNode.appendChild( okayButton );
	newNode.appendChild( formNode );

	document.body.insertBefore( newNode, document.body.firstChild );

}

/* Function must return false for technical reasons! */
function userAnonymousWarning( event, offsetX, offsetY ) {
	popupAtMouse( global_aloeTranslations.anonymousClickNotAllowed, event, offsetX, offsetY);
	return( false );
}

/* Workaround using innerHTML 
 */
function popupAtMouse( givenText, event, offsetX, offsetY ) {
	if ( ! offsetX ) offsetX = 0;
	if ( ! offsetY ) offsetY = 0;
	var geometry = getGeometry();
	var xPos = event.clientX + geometry.getHorizontalScroll() + offsetX;
	var yPos = event.clientY + geometry.getVerticalScroll() + offsetY;
	//alert( "horizontal scroll: " + geometry.getHorizontalScroll() + ", vertical scroll: " + geometry.getVerticalScroll() );

	var form = "<form accept-charset=\"utf-8\" action=\"javascriptDisabled\">"
		+ "<div><input class=\"button\" type=\"button\" value=\"Okay\" "
		+ "onclick=\"document.body.removeChild(document.body.firstChild);return(false);\" /></div>"
		+ "</form>";
	var toPrint = "<div class=\"aloePopupWindow\" style=\"top:" + yPos + "px;left:" + xPos 
	+ "px;\">"+ givenText + form + "</div>\n";

	var newNode = document.createElement( "div" );
	var newDocumentNode = document.body.insertBefore( newNode, document.body.firstChild );

	newDocumentNode.innerHTML = toPrint;

	return( false );
}

/* Workaround using innerHTML 
 */
function popupGlobal( givenText, offsetX, offsetY ) {
	var wrapper = document.getElementById('wrapper');

	if ( wrapper != null ) {
		var positionBase = getPosition( wrapper );
		var geometry = getGeometry();
		var posX = positionBase.x + geometry.getHorizontalScroll() + + offsetX;
		var posY = positionBase.y + geometry.getVerticalScroll() + offsetY;
		var inputElement = "<input class=\"button\" type=\"button\" value=\"Okay\" "
			+ "onclick=\"document.body.removeChild(document.body.firstChild);return(false);\" />";
		var toPrint = "<div class=\"aloePopupWindow\" style=\"top:" + posY + "px;left:" + posX 
		+ "px;\">"+ givenText + "<br />" + inputElement + "</div>\n";

		var newNode = document.createElement( "div" );
		var newDocumentNode = document.body.insertBefore( newNode, document.body.firstChild );

		newDocumentNode.innerHTML = toPrint;
	}

	return( false );
}

function popupGlobalAbsolutePosition( givenText, positionX, positionY ) {
	var wrapper = document.getElementById('wrapper');

	if ( wrapper != null ) {
		var posX = positionX;
		var posY = positionY;
		var inputElement = "<input class=\"button\" type=\"button\" value=\"Okay\" "
			+ "onclick=\"document.body.removeChild(document.body.firstChild);return(false);\" />";
		var toPrint = "<div class=\"aloePopupWindow\" style=\"top:" + posY + "px;left:" + posX 
		+ "px;\">"+ givenText + "<br />" + inputElement + "</div>\n";

		var newNode = document.createElement( "div" );
		var newDocumentNode = document.body.insertBefore( newNode, document.body.firstChild );

		newDocumentNode.innerHTML = toPrint;
	}

	return( false );
}

//Extend String class
String.prototype.trim = function () {
	var toReturn = this;
	while ( toReturn.substring(0,1) == ' ' ) {
		toReturn = toReturn.substring(1, toReturn.length);
	}
	while ( toReturn.substring(toReturn.length-1, toReturn.length) == ' ')
	{
		toReturn = toReturn.substring(0,toReturn.length-1);
	}
	return toReturn;
} 


//Borrowed from
//http://kkaefer.com/blog/javascript-position-bestimmen
//Get the position of a given element
function getPosition(obj) {
	var pos = { x:0, y:0 };

	do {
		pos.x += obj.offsetLeft;
		pos.y += obj.offsetTop;
	} while (obj = obj.offsetParent);

	return pos;
} 

/* From the examples of the o'reilly book 'JavaScript - The Definitive Guide' from David Flanagan,
                         downloaded at http://examples.oreilly.com/jscript5/js5examples.tar.gz
 */
function getGeometry() {

	/**
	 * Geometry.js: portable functions for querying window and document geometry
	 *
	 * This module defines functions for querying window and document geometry.
	 * 
	 * getWindowX/Y(): return the position of the window on the screen
	 * getViewportWidth/Height(): return the size of the browser viewport area
	 * getDocumentWidth/Height(): return the size of the document.
	 * getHorizontalScroll(): return the position of the horizontal scrollbar
	 * getVerticalScroll(): return the position of the vertical scrollbar
	 *
	 * Note that there is no portable way to query the overall size of the 
	 * browser window, so there are no getWindowWidth/Height() functions.
	 * 
	 * IMPORTANT: This module must be included in the <body> of a document
	 *            instead of the <head> of the document.
	 */
	var Geometry = {};

	if (window.screenLeft) { // IE and others
		Geometry.getWindowX = function() { return window.screenLeft; };
		Geometry.getWindowY = function() { return window.screenTop; };
	}
	else if (window.screenX) { // Firefox and others
		Geometry.getWindowX = function() { return window.screenX; };
		Geometry.getWindowY = function() { return window.screenY; };
	}

	if (window.innerWidth) { // All browsers but IE
		Geometry.getViewportWidth = function() { return window.innerWidth; };
		Geometry.getViewportHeight = function() { return window.innerHeight; };
		Geometry.getHorizontalScroll = function() { return window.pageXOffset; };
		Geometry.getVerticalScroll = function() { return window.pageYOffset; };
	}
	else if (document.documentElement && document.documentElement.clientWidth) {
		// These functions are for IE6 when there is a DOCTYPE
		Geometry.getViewportWidth =
			function() { return document.documentElement.clientWidth; };
			Geometry.getViewportHeight = 
				function() { return document.documentElement.clientHeight; };
				Geometry.getHorizontalScroll = 
					function() { return document.documentElement.scrollLeft; };
					Geometry.getVerticalScroll = 
						function() { return document.documentElement.scrollTop; };
	}
	else if (document.body.clientWidth) {
		// These are for IE4, IE5, and IE6 without a DOCTYPE
		Geometry.getViewportWidth =
			function() { return document.body.clientWidth; };
			Geometry.getViewportHeight =
				function() { return document.body.clientHeight; };
				Geometry.getHorizontalScroll =
					function() { return document.body.scrollLeft; };
					Geometry.getVerticalScroll = 
						function() { return document.body.scrollTop; };
	}

	// These functions return the size of the document.  They are not window 
	// related, but they are useful to have here anyway.
	if (document.documentElement && document.documentElement.scrollWidth) {
		Geometry.getDocumentWidth =
			function() { return document.documentElement.scrollWidth; };
			Geometry.getDocumentHeight =
				function() { return document.documentElement.scrollHeight; };
	}
	else if (document.body.scrollWidth) {
		Geometry.getDocumentWidth =
			function() { return document.body.scrollWidth; };
			Geometry.getDocumentHeight =
				function() { return document.body.scrollHeight; };
	}
	return( Geometry );
}


function toggleVisibility( elementId ) {
	var element = document.getElementById( elementId );
	if ( element != null ) {
		var nextVisibility = element.style.display == "none" ? "block" : "none";
		element.style.display = nextVisibility;
	}
}
function toggleImage( imageId, image1, image2 ) {
	var element = document.getElementById( imageId );
	if ( element != null ) {
		// current image is absolute, image1 and image2 are relative. Since
		// image1 and image2 are starting with '..' we cut the first two
		// characters
		var currentImage = element.src;
		var suffix = image1.substring( 2, image1.length );
		var nextImage = currentImage.indexOf(suffix) >= 0 ? image2 : image1;
		element.src = nextImage;
	}
}

function setVisibility( elementId, newValue ) {
	jQuery('#' + elementId).css('display', newValue);
}

function disableSubmitButtons() {
	var inputElements = document.getElementsByTagName("input");
	for( var i = 0; i < inputElements.length; i++ ) {
		var current = inputElements[i];
		if ( current.type == "submit" ) {
			//alert( "Will disable " + current.name );
			current.disabled = true;
			document.body.style.cursor = "wait";
			current.style.cursor = "wait";
		}
	}
}

function truncateToMaxLength( toTruncate, maxLength ) {
	var toReturn = toTruncate;
	if ( toTruncate.length > maxLength && maxLength >= 3 ) {
		toReturn = toTruncate.substr( 0, maxLength-3 ) + "...";
	}
	return( toReturn );
}

function getTextBelowNode( referenceNode ) {
	var toReturn = "";
	if ( referenceNode != null ) {
		var children = referenceNode.childNodes;
		var numberOfChildren = children.length;
		for ( var index = 0; index < numberOfChildren; index++ ) {
			var current = children[index];
			// better would be Node.TEXT_NODE but the node constants are not supported by - guess - IE
			if ( current.nodeType == 3 ) {
				toReturn += current.data;
			}
		}
	}
	return( toReturn );
}

function addStarIfNeeded( inputElement, inputString ) {
	var toReturn = inputString;
	if ( inputString.length > 0 ) {
		if ( inputString.charAt(inputString.length-1) != '*' ) {
			toReturn += '*';
		}
	}
	return( toReturn );
}

function acceptApertureProposal( destinationId, sourceId ) {
	document.getElementById(destinationId).value += getTextBelowNode( document.getElementById(sourceId) );
}

function submitFirstFormBelowReferenceElement( referenceId ) {
	var referenceElement = document.getElementById( referenceId );
	if ( referenceElement != null ) {
		var formsBelow = referenceElement.getElementsByTagName( 'form' );
		if ( formsBelow != null && formsBelow.length > 0 ) {
			formsBelow[0].submit();
		}
	}
}

function getSelectedButtonValue(buttonGroup){
	for (var i = 0; i < buttonGroup.length; i++) {
		if (buttonGroup[i].checked) {
			return buttonGroup[i].value;
		}
	}
	return null;
}

function getObjectsFromHtmlAsJsonArray( elementId ){

	var objectDiv = document.getElementById( elementId );

	if( objectDiv != null ){

		var objectDivs = objectDiv.getElementsByTagName("div");
		if( objectDivs != null ){
			var objectsFound = new Array();
			for (var i = 0; i < objectDivs.length; i++) {
				objectsFound.push( objectDivs[i].innerHTML );
			}
			return( JSON.stringify( objectsFound ) );
		}
	}
}

