/****************************************************************************************
Author:	
	- Mett, Rinse Stellingwerf
	
Description:
	- Mett Javascript functions.
	
Date created:
	- 09/06/09
	
Update log:
	- 10/06/09: Added resizeHeader().
	- 11/06/09: Added menu toggle options.
	- 03/07/09: Perfected the menu toggle function.
	- 08/07/09: Added toggle support for inline/block display.
	- 05/08/09: Added loadHeader() function.
	- 06/08/09: Added extra checks to loadHeader() function.
	- 25/08/09: Added fix for Firefox bug.
	- 31/08/09: Added template support to toggleState() function.
	- 21/09/09: Added getProjectInformation() function.
	- 22/09/09: Added default header width & height to loadHeader() & resizeHeader().
	- 23/09/09: Removed default header width & height ;). Added initialize() function.
	- 05/10/09: Added getPaging() function.
	- 08/10/09: Reposition footer after header load (IE6 fix).
	- 09/10/09: Added toggleMenuBullets() function.
	- 12/10/09: Improved toggleMenuBullets(), added getElementsByClassName() function.
	- 12/10/09: Added showError() function;
	- 15/10/09: Improved showError() function, so it's able to show multiple errors.
	- 16/10/09: Added Preferences(), submitFormOnEnter() & resetError() functions.
	- 19/10/09: Added clickButton() function.
	- 26/10/09: Disabled loadHeader() & repositionFooter().
	- 12/11/09: Added getTabs(), initializeTab() & openTab() functions.
	- 13/11/09: Added some extra checks to tabs functions.
	- 23/12/09: Added toggleClass() & clickLink() functions.
	- 21/01/10: Added extra checks to initialize() function.
	- 22/01/10:	Added extra checks to getPaging() function.
	- 08/03/10: Added clickCell() function.
	- 11/03/10: Added getIcon() function.
	- 01/04/10: Added getContent() function.
	- 26/04/10: Rewrote clickButton, clickLink & clickCell() functions for FireFox.
	- 12/05/10: Added getLinks() function.
	- 14/05/10: Added createSelectList() function.
	- 31/05/10: Added copyContent() function.
	- 02/06/10: Added extra checks to copyContent() and createSelectList() functions.
	- 02/06/10: Added showIfNotEmpty() function.
	- 14/06/10: Added extra option to copyContent() function.
	- 21/06/10: Added toggleFiles() function.
	- 01/07/10: Kick-ass rewrite of toggleFiles() function.
	- 05/07/10: Added functionality to getElementsByClassName() function.
	- 12/07/10: Added extra margin to paging container.
	- 17/08/10: Added return value to setFormPreferences().
	- 19/08/10: Added some extra checks to setFormPreferences() & submitFormOnEnter().
	- 07/09/10: Added ID's to getProjectInformation() function.
	- 09/09/10: Added button support and extra checks to setFormPreferences() function.
	- 20/10/10: Made some changes to toggleFiles() function.
	- 21/10/10: Added listPickerMoveItem(), listPickerMoveAllItems(), listPickerGetSelectedItems().
	- 10/11/10: Added countCharacters() function.
	- 11/11/10: Changed countCharacters() function, so it also works when you edit a page.
	- 02/12/10: Added some extra checks to setFormPreferences().
	- 22/12/10: Added copyToClipboard() function.
	- 04/01/10: Added checkMobileDevice() & getCookie() functions.
	- 18/01/10: Added linkNumber to clickLink() function.
	- 18/01/10: Added toggleElementsClass() function.
	- 02/02/11: Added global variables.
	- 03/02/11: Added getCheckedRadioButtonValue() & toggleTab() function.
	- 15/02/11: Added toggleFormOptions() function.
	- 16/02/11: Added open by text label to initializeTab() function.
	- 24/02/11: Renamed getCheckedRadioButtonValue() to getCheckedRadioButton().
	- 01/03/11: Added querystring support to initializeTab() function.
	- 02/03/11: Added clickTab() link to getTabs() funciton.
	- 08/03/11: Enabled checkMobileDevice() function.
	- 14/03/11: Added changeInputFieldValue() function.
	- 14/03/11: Added facebookShareLink & twitterShareLink.
	- 04/04/11: Added empty & null property to toggleClass() function.
	- 12/04/11: Added "before" to copyContent() function.
	- 28/03/11: Added inputError class to showError() and resetError() functions.
	- 08/06/11: Added checkCondition() fuction.
	- 08/06/11: Added toggleObjectClass() function.
	- 14/06/11: Added extra check to setFormPreferences() function.
	- 08/07/11: Added toggleClassWithFade() function.
	- 15/07/11: Added loadBackground() function.
	- 13/09/11: Added trim(), applyFilter() & clearFilter() functions.
	- 22/09/11: Added alterLinks() function.
	- 27/09/11: Added menuItemOpen & menuItemCurrent class to toggleMenuBullets() function.
	- 06/10/11: Added moveWebbox() function.
	- 21/10/11: Added fancyBox initialization.
	- 21/10/11: Removed getElementsByClassname() function (obsolete, now using jQuery).
	- 06/12/11: Added URL encode to share links.
	- 30/01/12: Added checks to setFormPreferences() & submitFormOnEnter().
	- 31/01/12: Added getQuerystring();
****************************************************************************************/

/****************************************************************************************
Initialize
****************************************************************************************/

// Initialize.
window.onload = initialize;

// Globals.
var currentPage = document.location.href.toLowerCase();
var linkedInShareLink = "https://www.linkedin.com/cws/share?url=" + encodeURIComponent(currentPage);
var facebookShareLink = "https://facebook.com/sharer.php?u=" + encodeURIComponent(currentPage);
var googlePlusShareLink = "https://plusone.google.com/_/+1/confirm?url=" + encodeURIComponent(currentPage);
var twitterShareLink = "http://twitter.com/share";

// Run the following functions on load.
function initialize()
{	
	// Load (random) background.
	if (backgroundCount > 1)
	{
		loadBackground(backgroundLocation, backgroundCount);
	}
	
	// Load (random) header.
	if (headerCount > 1)
	{
		loadHeader(headerLocation, headerContainer, headerCount, menuTopContainer, menuTopHeight);
	}
	
	// Toggle (sub)menu bullets.
	if (typeof(menuLeftContainer) == "string" && typeof(menuLeftItemClassName) == "string")
	{
		toggleMenuBullets(menuLeftContainer, menuLeftItemClassName);
	}
	
	// Fancybox.
	$(document).ready(function()
	{	
		$("a[rel=photos]").fancybox(
		{
			"overlayOpacity": 	0.7,
			"overlayColor": "	#000000",
			"cyclic": 			"true",
			"transitionIn": 	"none",
			"transitionOut": 	"none",
			"titlePosition": 	"over",			
			"titleFormat": 		function(title, currentArray, currentIndex, currentOpts)
			{
				return '<span id="fancybox-title-over">Afbeelding ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
			},
			"onComplete": 		function()
			{
				clearTimeout($.fancybox.slider);
				$.fancybox.slider = setTimeout("$.fancybox.next()", 5000); 
			}
		}
		);
	});
	
	// Check for iOS device.
	checkMobileDevice();
}

/****************************************************************************************
Functions
****************************************************************************************/

// Load background.
function loadBackground(backgroundLocation, backgroundCount)
{
	if (backgroundLocation != "")
	{
		// Get (random) background number.
		var backgroundNumber = Math.floor(Math.random() * backgroundCount) + 1;
		
		// Set background.
		document.body.style.backgroundImage = "url('" + backgroundLocation + "background_body_" + backgroundNumber + ".jpg')";
	}
}

// Load header.
function loadHeader(headerLocation, headerContainer, headerCount, menuContainer, menuHeight)
{	
	if (headerLocation != "")
	{
		// Get (random) header number.
		var headerNumber = Math.floor(Math.random() * headerCount) + 1;
		
		// Place header in container.
		document.getElementById(headerContainer).style.backgroundImage = "url('" + headerLocation + headerContainer + "_" + headerNumber + ".jpg')";		
	}
}

// Check condition (used for conditional toggles).
function checkCondition(conditionString, conditionOperator, conditionText)
{
	var returnValue = false;
	
	if (conditionText == "empty" || conditionText == "null")
	{
		// Remove white spaces.
		conditionString = conditionString.replace(/^\s+$/, "");
	}
		
	if (conditionOperator == "==")
	{		
		if ((conditionText == "empty" || conditionText == "null") && (conditionString.length == 0))
		{
			// Container is empty, condition is true.
			returnValue = true;
		}		
		else if (conditionString.indexOf(conditionText.toLowerCase()) != -1)
		{
			// Condition has been met, condition is true.
			returnValue = true;
		}		
	}	
	else if (conditionOperator == "!=")
	{
		if ((conditionText == "empty" || conditionText == "null") && (conditionString.length != 0))
		{
			// Container is not empty, condition is true.
			returnValue = true;
		}
		else if ((conditionText != "empty" && conditionText != "null") && (conditionString.indexOf(conditionText.toLowerCase()) == -1))
		{
			// Condition has been met, condition is true.
			returnValue = true;
		}
	}
	
	// Return result of condition check.
	return returnValue;
}

// Toggle (menu or webbox) state.
function toggleState(toggler, containerID, classOpen, classClosed)
{	
	if(document.getElementById(containerID))
	{	
		var display = document.getElementById(containerID).style.display;
		
		if (display == "none")
		{			
			// Menu should have display "inline", all other elements "block".
			if (document.getElementById(containerID).id.indexOf("menu") > -1)
			{
				display = "inline";
			}
			else
			{
				display = "block";
			}
			
			// Open.
			document.getElementById(containerID).style.display = display;
			
			// Change class.		
			toggler.className = classOpen;
		}
		else
		{
			// Close.
			document.getElementById(containerID).style.display = "none";
			
			// Change class.
			toggler.className = classClosed;
		}		
	}	
}

// Toggle menu bullets.
function toggleMenuBullets(menuContainer, menuItemClassName)
{
	if (document.getElementById(menuContainer))
	{
		// Configuration.
		var menuItemTagName = "a";	
		var menuItemParentTagName = "li";
		
		var menuItemClosedClassName = menuItemClassName + "Closed";
		var menuItemOpenClassName = menuItemClassName + "Open";
		var menuItemCurrentClassName = menuItemClassName + "Current";
		
		var menuItemClassNameBulletClosed = menuItemClassName + "BulletClosed";
		var menuItemClassNameBulletOpen = menuItemClassName + "BulletOpen";
		
		// Get all currently opened menu items.		
		var menuItemObjects = $("#" + menuContainer + " " + menuItemTagName + "." + menuItemClassName + "Selected");
		
		if (menuItemObjects.length > 0)
		{
			var i = 1;
			var parentObject = menuItemObjects[0].parentNode;
			
			// Change current menu item class.
			parentObject.parentNode.className = menuItemClassName + " " + menuItemCurrentClassName;
			
			// Loop through menu items untill the menu container is reached.
			while(parentObject.id != menuContainer)
			{  
				if (parentObject.tagName.toLowerCase() == menuItemParentTagName)
				{
					if (i > 1)
					{
						var parsedMenuItemContent = parentObject.innerHTML;
						
						// Replace closed class for open class.
						parsedMenuItemContent = parsedMenuItemContent.replace(menuItemClassNameBulletClosed, menuItemClassNameBulletOpen);
						parsedMenuItemContent = parsedMenuItemContent.replace(menuItemClosedClassName, menuItemOpenClassName);
						
						// Replace menu item content.
						parentObject.innerHTML = parsedMenuItemContent;
					}
					
					i++;
				}
				
				// Go to next parent.
				parentObject = parentObject.parentNode;
			}
		}
	}
}

// Conditional class toggler for a container.
function toggleClass(containerID, newClassName, conditionContainerID, conditionOperator, conditionText)
{	
	if (document.getElementById(containerID))
	{	
		var toggleContainerClass = false;
		
		if (conditionContainerID != "" && conditionText != "")
		{
			// Conditional toggle.
			if (document.getElementById(conditionContainerID))
			{
				var conditionContainerHTML = document.getElementById(conditionContainerID).innerHTML.toLowerCase();
				
				// Check condition.
				toggleContainerClass = checkCondition(conditionContainerHTML, conditionOperator, conditionText);
			}
			else
			{
				// Always toggle if the condition container does not exist.
				toggleContainerClass = true;
			}
		}
		else
		{
			// Always toggle (no condition).
			toggleContainerClass = true;
		}
		
		if (toggleContainerClass)
		{
			// Toggle the class of the container.
			document.getElementById(containerID).className = newClassName;
		}
	}
}

// Conditional class toggler for elements.
function toggleElementsClass(containerID, elementName, newClassName, conditionOperator, conditionText)
{	
	if (document.getElementById(containerID))
	{		
		var container = document.getElementById(containerID);
		var elements = container.getElementsByTagName(elementName);
		
		var conditionalToggle = false;
		
		if (elements.length > 0)
		{			
			if (conditionOperator != "" && conditionText != "")
			{			
				// Conditional toggle.
				conditionalToggle = true;
			}
			
			// Toggle elements.
			for(i = 0; i < elements.length; i++)
			{
				var toggleElementClass = false;
				
				if (conditionalToggle)
				{
					var elementHTML = elements[i].innerHTML.toLowerCase();
					
					// Check condition.
					toggleElementClass = checkCondition(elementHTML, conditionOperator, conditionText);
				}
				else
				{
					// Always toggle (no condition).
					toggleElementClass = true;
				}
				
				if (toggleElementClass)
				{
					// Toggle class for this element.					
					elements[i].className = newClassName;
				}
			}
		}	
	}
}

// Conditional class toggler for an object.
function toggleObjectClass(object, newClassName, conditionOperator, conditionText)
{	
	if (typeof(object) == "object")
	{
		var toggleObjectClass = false;
		
		if (conditionOperator != "" && conditionText != "")
		{
			// Conditional toggle.
			var objectHTML = object.innerHTML.toLowerCase();
			
			// Check condition.			
			toggleObjectClass = checkCondition(objectHTML, conditionOperator, conditionText);			
		}
		else
		{
			// Always toggle (no condition).
			toggleObjectClass = true;
		}
		
		if (toggleObjectClass)
		{
			// Toggle the object class.		
			object.className = newClassName;
		}		
	}
}

// Toggle class with a jQuery fade in fade out effect.
function toggleClassWithFade(fromContainerID, toContainerID, fromClassName, toClassName)
{
    if (document.getElementById(fromContainerID))
	{		
		var containerFrom = document.getElementById(fromContainerID);
		var containerTo = document.getElementById(toContainerID);
		
		if (containerTo.className != toClassName)
		{			
			// jQuery fade toggle.
			$("#" + fromContainerID).fadeToggle("fast", function()
			{
				containerTo.className = toClassName;
				containerFrom.className = fromClassName;
			});
		}
		else
		{			
			// jQuery fade toggle.
			$("#" + toContainerID).fadeToggle("fast", function()
			{
				containerFrom.className = toClassName;
				containerTo.className = fromClassName;
			});
		}
	}
}

// Toggle files.
function toggleFiles(toggler, togglerParent, className, classOpen, classClosed)
{	
	if (togglerParent)
	{		
		// Get the ID of the containers that match the specified tag.
		var toggleContainers = $(togglerParent).find("span" + "." + className);
		
		if (toggleContainers.length > 1)
		{
			// Toggle multiple containers.
			for(i = 0; i < toggleContainers.length; i++)
			{
				// Get containerID.
				var containerID = toggleContainers[i].id;
				
				// Show or hide files.
				if (toggler.className == classOpen)
				{
					// Hide container.
					document.getElementById(containerID).style.display = "none";
				}
				else
				{
					// Show container.
					document.getElementById(containerID).style.display = "block";
				}
			}
			
			// Change toggler icon.
			if (toggler.className == classOpen)
			{				
				toggler.className = classClosed;
			}
			else
			{
				toggler.className = classOpen;
			}
			
			// Change container icons (the toggleIcons array comes from "downloads_downloadsmsection.ascx").
			for(j = 0; j < toggleIcons.length; j++)
			{
				if ((toggleIcons[j].className == classOpen) || (toggleIcons[j].className == classClosed))
				{
					// Change container icon into the same icon as the "show all" toggler.
					toggleIcons[j].className = toggler.className;
				}
			}
		}
		else if (toggleContainers.length == 1)
		{			
			// Get containerID.
			var containerID = toggleContainers[0].id;
			
			// Prepare container for toggle.
			if (document.getElementById(containerID).style.display == "" || document.getElementById(containerID).style.display == "none")
			{
				document.getElementById(containerID).style.display = "none";
			}
			
			// Toggle container with toggleState function.
			toggleState(toggler, containerID, classOpen, classClosed);
			
			// Count the number of toggle icons.
			var iconCount = 0;
			var iconID = 0
			
			for(j = 0; j < toggleIcons.length; j++)
			{
				if ((toggleIcons[j].className == classOpen) || (toggleIcons[j].className == classClosed))
				{
					iconCount = iconCount + 1;
					iconID = j;
				}
			}
			
			// If there is only 1 toggler (besides the "show all" toggler), also toggle the "show all" icon when this toggler is clicked.
			if (iconCount == 1)
			{
				toggleIcons[iconID].className = toggler.className;
				
				// Change icon for the "show all" toggler.
				if (document.getElementById("iconShowAll").className != toggler.className)
				{
					document.getElementById("iconShowAll").className = toggler.className;
				}
			}			
		}
	}
}

// Toggle form field from an array.
function toggleFormOptions(formFields, toggleOptions)
{
	for(i = 0; i < formFields.length; i++)
	{
		if (document.getElementById(formFields[i]))
		{
			// Check or uncheck checkboxes.
			document.getElementById(formFields[i]).checked = toggleOptions[i];
		}		
	}
}

// Show error.
function showError(containerIDs)
{	
	for(i = 0; i < containerIDs.length; i++)
	{		
		if (document.getElementById(containerIDs[i]))
		{		
			// Get the input field that has an error.
			var errorContainer = document.getElementById(containerIDs[i]);
			var errorInputField = errorContainer.getElementsByTagName("input");
			
			// Get the (hidden) div, to show the error sign.
			var errorDiv = errorContainer.getElementsByTagName("div");
			
			// Add error class to the input field.
			errorInputField[0].className = errorInputField[0].className + " inputError";
			
			// Clear input field with error.
			errorInputField[0].value = "";
			
			// Focus on input field with error.
			errorInputField[0].focus();
			
			// Show the error sign + message.
			errorDiv[0].style.display = "block";
			
			// Reset error message after unfocus.
			errorInputField[0].onblur = resetError;
		}
	}
}

// Reset error.
function resetError()
{
	// Get error objects.
	var errorObjects = $("#contentCenter div.error");
	var errorFields = $("#contentCenter input.inputError");
	
	for(i = 0; i < errorObjects.length; i++)
	{
		// Hide error objects.
		errorObjects[i].style.display = "none";
	}
	
	for(j = 0; j < errorFields.length; j++)
	{
		// Hide error class.		
		errorFields[j].className = errorFields[j].className.replace(" inputError", "");		
	}
}

// Set various form preferences.
function setFormPreferences(focusContainerID, submitContainerID)
{	
	// Set focus.
	if (focusContainerID)
	{
		if (document.getElementById(focusContainerID))
		{		
			var focusContainer = document.getElementById(focusContainerID);
			var focusInputField = focusContainer.getElementsByTagName("input");
			
			if (focusInputField.length > 0)
			{			
				if (! focusInputField[0].disabled)
				{
					try
					{
						// Set focus on the input field.
						focusInputField[0].focus();
					}
					catch(e)
					{
						// Field is not available, do nothing.
					}
				}
			}
			else
			{
				if (! focusContainer.disabled)
				{
					// Set focus directly on the input field.
					try
					{
						focusContainer.focus();
					}
					catch(e)
					{
						// Field is not available, do nothing.
					}
				}
			}
		}
	}
	
	// Set on <enter> submit action.
	if (submitContainerID)
	{		
		if (document.getElementById(submitContainerID))
		{		
			var submitContainer = document.getElementById(submitContainerID);
			var submitLink = submitContainer.getElementsByTagName("a");
			var submitButton = submitContainer.getElementsByTagName("input");
			
			if (submitLink.length > 0)
			{			
				// Get href.
				submitLink = submitLink[0].href.toLowerCase();
				submitLink = submitLink.replace(/%22/gi, "\"");
			}
			else
			{			
				// No href? Get button onClick.
				submitLink = submitButton[0].attributes["onclick"].value.toLowerCase();
				submitLink = submitLink.replace(/%22/gi, "\"");
			}
			
			// Postback without options.
			if (submitLink.indexOf("__dopostback") != -1)
			{			
				var beginPosition = submitLink.indexOf("'");
				var endPosition = submitLink.indexOf("'", beginPosition + 1);	
			}
			
			// Postback with options.
			if (submitLink.indexOf("_dopostbackwithoptions") != -1)
			{
				var beginPosition = submitLink.indexOf("\"");
				var endPosition = submitLink.indexOf("\"", beginPosition + 1);
			}
			
			if (beginPosition > -1)
			{
				var eventTarget = submitLink.substring(beginPosition + 1, endPosition);
				
				if(typeof(eventTarget) == "string")
				{
					// Set form event target.
					theForm.__EVENTTARGET.value = eventTarget;
					
					var checkForKeysPressed = true;
					
					if (submitContainerID == "searchField" && (submitButton[0].value == "" || submitButton[0].value == "Zoeken" || submitButton[0].value == "Search"))
					{
						// Don't check pressed keys when the searchfield still has it's default value.
						checkForKeysPressed = false;
					}
					
					if (checkForKeysPressed)
					{
						// Check pressed keys.
						document.onkeypress = submitFormOnEnter;
					}
				}
			}
		}
	}
	
	return true;
}

// Submit form on <enter>.
function submitFormOnEnter(e) 
{
	var keyCode;
	var targetType = "";
	
	if (window.event)
	{
		keyCode = window.event.keyCode;
		
		if (event.srcElement.type)
		{
			targetType = event.srcElement.type;
		}
	}
	else if (e)
	{
		keyCode = e.which;
		
		if (e.target.type)
		{
			targetType = e.target.type;
		}
	}
	
	// Only submit when the <enter> key is pressed outside a textarea.
	if (keyCode == 13 && targetType != "textarea")
	{
		// Submit the page form.
		theForm.submit();
	}
}

// Click specified button.
function clickButton(buttonContainer)
{
	if (document.getElementById(buttonContainer))
	{		
		var button = document.getElementById(buttonContainer).getElementsByTagName("input")[0];
		
		if (button)
		{			
			// Click button.
			button.click();
		}
	}
}

// Click specified link.
function clickLink(linkContainer, linkNumber)
{	
	if (document.getElementById(linkContainer))
	{		
		if (!linkNumber)
		{
			// Linknumber is optional, default = 0.
			linkNumber = 0;
		}
		
		if (linkNumber == 999)
		{
			// Get the last link.
			linkNumber = document.getElementById(linkContainer).getElementsByTagName("a").length - 1;
		}
		
		// Get link.
		var link = document.getElementById(linkContainer).getElementsByTagName("a")[linkNumber];
		
		if (link)
		{			
			// Redirect to link.
			document.location.href = link;
		}		
	}
}

// Change form input field value.
function changeInputFieldValue(inputField, inputAction)
{		
	// On focus.
	if (inputAction == "clear")
	{		
		// Check if current value matches the title of the input field.
		if (inputField.value == inputField.title)
		{
			// Clear input field.
			inputField.value = "";			
		}
	}
	
	// On blur.
	if (inputAction == "restore")
	{		
		// Check if current value is empty.
		if (inputField.value == "" || inputField.value == " ")
		{
			// Restore default value.
			inputField.value = inputField.title;
		}
	}
}

// Restore form input field value.
function restoreFormInput(inputField)
{
	// Check if current value is empty.
	if (inputField.value == "")
	{
		// Restore original value.
		inputField.value = inputField.title;
	}
}

// Click link in list cell.
function clickCell(rowID, linkID)
{	
	var link = rowID.getElementsByTagName("a")[linkID];
	
	if (link)
	{
		// Redirect to link.
		document.location.href = link;
	}
}

// Get project title and domain.
function getProjectInformation()
{
	var returnValue = "";
	
	// Get document title and domain.
	var projectTitle = document.title.substring(0, document.title.indexOf(" - ") + 1);
	var projectDomain = document.domain;
	
	projectTitle = "<h1 id=\"projectTitle\">" + projectTitle + "</h1>";
	projectDomain = "<a href=\"http://" + projectDomain + "\" id=\"projectURL\">" + projectDomain + "</a>";
	
	returnValue = projectTitle + projectDomain;
	
	// Return project information.
	return returnValue;
}

// Get generic paging.
function getPaging(pagingContainerSource)
{
	var returnValue = "";
	
	if (document.getElementById(pagingContainerSource))
	{		
		// Parse paging content.
		var pagingContent = document.getElementById(pagingContainerSource);
		pagingContent = pagingContent.getElementsByTagName("table");
		
		// Get the innerHTML of the last <table>.		
		if (pagingContent.length > 0)
		{			
			// Get paging destination container.
			var pagingContainerDestination = $("#contentCenter div.paging");
			
			if (pagingContainerDestination.length > 0)
			{
				// Add extra margin to paging container.
				pagingContainerDestination[0].style.marginBottom = "50px";
			}
			
			// Parse paging content.
			var parsedPagingContent = pagingContent[pagingContent.length - 1].innerHTML;
			
			parsedPagingContent = parsedPagingContent.replace(/<tr>/gi, "<ul>");
			parsedPagingContent = parsedPagingContent.replace(/<\/tr>/gi, "</ul>");
			parsedPagingContent = parsedPagingContent.replace(/<td>/gi, "<li>");
			parsedPagingContent = parsedPagingContent.replace(/<\/td>/gi, "</li>");
			parsedPagingContent = parsedPagingContent.replace("[", "<strong>");
			parsedPagingContent = parsedPagingContent.replace("]", "</strong>");
			parsedPagingContent = parsedPagingContent.replace("<span>", "");
			parsedPagingContent = parsedPagingContent.replace("<TBODY>", "");
			parsedPagingContent = parsedPagingContent.replace("</TBODY>", "");
			parsedPagingContent = parsedPagingContent.replace("<tbody>", "");
			parsedPagingContent = parsedPagingContent.replace("</tbody>", "");
			
			returnValue = parsedPagingContent;			
		}		
	}
	
	// Return parsed paging.
	return returnValue;	
}

// Conditional get icon function.
function getIcon(valueToCheck, conditionValue, iconLocation, iconNew, iconDefault)
{	
	var returnValue = "";
	var icon;
	
	// Check for condition value.	
	if (valueToCheck.indexOf(conditionValue) != -1)
	{
		icon = iconNew;
	}
	else
	{
		icon = iconDefault;
	}
	
	// Create icon string.
	returnValue = "<img src=\"" + iconLocation + icon + "\" width=\"16\" height=\"16\" />";
	
	// Return icon.
	return returnValue;
}

// Get generic tabs.
function getTabs(tabsContainer)
{
	var returnValue = "";
	
	if (document.getElementById(tabsContainer))
	{
		// Parse tabs.
		var parsedTabs = "";
		var tabLink = "";
		var tabCustomLink = false;
		
		var tabs = document.getElementById(tabsContainer);		
		tabs = tabs.getElementsByTagName("a");
		
		// Add custom link to tabs (if function exists).
		if (typeof (clickTab) == 'function')
		{			
			tabCustomLink = true;
		}
		
		// Get tab links.
		for(i = 0; i < tabs.length; i++)
		{		
			tabLink = tabs[i];
			
			if (tabCustomLink)
			{
				// Add custom link.
				tabLink = tabLink + ";clickTab('tab" + i + "', '" + tabs[i].innerHTML.replace("'", "") + "');"
			}
			
			// Create new tab.
			parsedTabs = parsedTabs + "<a href=\"" + tabLink + "\" id=\"tab" + i + "\" class=\"tab\">" + tabs[i].innerHTML + "</a>";
		}
		
		// Replace showPanel() function with openTab() function.
		parsedTabs = parsedTabs.replace(/showPanel/gi, "openTab");
		
		returnValue = parsedTabs;
	}
	
	// Return parsed tabs.
	return returnValue;
}

// Get content from ID.
function getContent(containerID)
{
	var returnValue = "";
	
	if (document.getElementById(containerID))
	{
		returnValue = document.getElementById(containerID).innerHTML;
	}
	
	// Return innerHTML.
	return returnValue;
}

// Get links from ID.
function getLinks(containerID)
{	
	var returnValue = new Array();
	
	if (document.getElementById(containerID))
	{		
		var foundObjects = document.getElementById(containerID).getElementsByTagName("a");
		
		// Check if links exist, if so place in array.
		if (foundObjects.length > 0)
		{
			returnValue = foundObjects;
		}
	}
	
	// Return links.
	return returnValue;
}

// Get cookie.
function getCookie(cookieID) 
{ 
	var returnValue = "";
	
	var	cookie = "" + document.cookie;	
	var beginPosition = cookie.indexOf(cookieID);
	var endPosition = cookie.indexOf(';', beginPosition);
	
	if (beginPosition > -1 && cookieID != "") 
	{		
		if (endPosition == -1)
		{
			endPosition = cookie.length;
		}
		
		returnValue = unescape(cookie.substring(beginPosition + cookieID.length + 1, endPosition));
	}
	
	// Return cookie.
	return returnValue;	
}

// Get checked radio button from radio list.
function getCheckedRadioButton(radioButtonListID)
{
	var returnValue = "";
	
	var radioButtonList = document.getElementById(radioButtonListID);
	
	if (radioButtonList)
	{
		// Get the radio buttons.
		var radioButtons = radioButtonList.getElementsByTagName("input");
		
		for (var i = 0; i < radioButtons.length; i++)
		{
			if (radioButtons[i].checked)
			{
				// Return checked radio button.
				returnValue = radioButtons[i];
			}
		}
	}
	
	// Return value.
	return returnValue;
}

// Get query string.
function getQueryString(key)
{
	var returnValue = "";
	var queryString;
	
	key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regularExpression = new RegExp("[\\?&]" + key + "=([^&#]*)");
	
	queryString = regularExpression.exec(document.location.href);
	
	if (queryString != null)
	{
		returnValue = queryString[1];		
	}
	
	return returnValue;
}

// Initialize tab.
function initializeTab(tabID, overrulesQuerystring)
{	
	var beginPosition;
	var endPosition;
	var selectedTabContentID;
	
	if (!overrulesQuerystring)
	{
		// Tab initialization by querystring overrules normal tab initialization.	
		if (document.location.href.indexOf("tab=") > -1)
		{		
			beginPosition = currentPage.indexOf("tab=");
			endPosition = currentPage.length;
			
			tabID = "tab" + currentPage.substring(beginPosition + 4, endPosition);
		}		
	}
	
	if (document.getElementById(tabID))
	{
		var selectedTab = document.getElementById(tabID);
		
		// Get selected tab content container.	
		beginPosition = selectedTab.href.indexOf("'");
		endPosition = selectedTab.href.indexOf("'", beginPosition + 1);	
		selectedTabContentID = selectedTab.href.substring(beginPosition + 1, endPosition);
		
		// Select & open tab.
		openTab(selectedTabContentID);
	}
	else
	{
		// Failover, search for tabID.
		var tabs = document.getElementById("tabs");
		tabs = tabs.getElementsByTagName("a");
		
		// Get tab titles.
		for(i = 0; i < tabs.length; i++)
		{		
			var tabContent = tabs[i].innerHTML.toLowerCase();
			tabID = tabID.toLowerCase();
			tabID = tabID.replace("tab", "");
			
			if (tabContent.indexOf(tabID) != -1)
			{
				tabID = "tab" + i;
				
				// Tab found, exit loop.
				break;
			}
		}
		
		if (!document.getElementById(tabID))
		{
			// If all else fails, initialize the first tab.
			tabID = "tab0";
		}
		
		// Initialize tab.
		initializeTab(tabID, true);		
	}	
}

// Open tab content.
function openTab(tabContentID)
{	
	if (document.getElementById(tabContentID))
	{
		// Get all the tab content panels.
		var tabContentPanels = $("#contentCenter div.tabContentPanel");
		
		if (tabContentPanels.length > 0)
		{		
			for(i = 0; i < tabContentPanels.length; i++)
			{				
				if (tabContentPanels[i].id == tabContentID)
				{					
					var selectedTab = document.getElementById("tab" + i);
					
					// Check if tab isn't already selected.
					if (selectedTab.className != "tabSelected")
					{					
						// Set selected class on tab.
						document.getElementById("tab" + i).className = selectedTab.className + "Selected";
						
						// Show content.
						tabContentPanels[i].style.display = "block";
					}
				}
				else
				{
					var unSelectedTab = document.getElementById("tab" + i);
					
					// Set default class on tab.
					if (document.getElementById("tab" + i))
					{
						document.getElementById("tab" + i).className = "tab"
					}
					
					// Hide content.
					tabContentPanels[i].style.display = "none";
				}				
			}
		}
	}
}

// Show or hide tab.
function toggleTab(tabID, currentPageTypeID)
{	
	if (document.getElementById(tabID))
	{
		var tabDisplay = "none";
		
		for(i = 0; i < pageTypesWithExtraTab.length; i++)
		{			
			if (parseInt(pageTypesWithExtraTab[i]) == parseInt(currentPageTypeID))
			{
				// Only show tab for certain page types (declared in config.js).
				tabDisplay = "block";
			}
		}
		
		// Show or hide tab.
		document.getElementById(tabID).style.display = tabDisplay;
	}
}

// Create select list from array.
function createSelectList(optionValues, optionFirstText)
{	
	var returnValue = "";
	var options = new Array();
	
	var selectList = document.createElement("select");	
	var optionFirst = document.createElement("option");
	
	optionFirst.text = optionFirstText;
	optionFirst.value = document.location.href;
	
	// Add first option to array.
	options[0] = optionFirst;
	
	// Create options.	
	for(i = 0; i < optionValues.length; i++)
	{		
		// Create new option.
		var option = document.createElement("option");
		
		option.text = optionValues[i].innerHTML;
		option.value = optionValues[i];
		
		// Add option to array.
		options[i + 1] = option;		
	}
	
	// Add options to the select list.
	for (j = 0; j < options.length; j++)
	{
		try
		{
			// All browsers except IE.
			selectList.add(options[j], selectList.options[j]);
		}
	
		catch(ex)
		{
			// IE only.
			selectList.add(options[j], selectList.options.length);
		}
	}
	
	returnValue = selectList;
	
	// Return select list.
	return returnValue;
}

// Copy content from one container to another.
function copyContent(sourceContainerID, destinationContainerID, copyMode)
{	
	if (document.getElementById(sourceContainerID) && document.getElementById(destinationContainerID))
	{		
		var sourceContainer = document.getElementById(sourceContainerID);
		var destinationContainer = document.getElementById(destinationContainerID);
		
		// Check if there is content.
		if (sourceContainer.innerHTML.length > 0)
		{		
			if (copyMode == "add")
			{
				// Add content to existing content.				
				destinationContainer.innerHTML = destinationContainer.innerHTML + sourceContainer.innerHTML;
			}
			else if (copyMode == "before")
			{
				// Add content before existing content.
				destinationContainer.innerHTML = sourceContainer.innerHTML + destinationContainer.innerHTML;
			}
			else
			{				
				// Replace existing content.
				destinationContainer.innerHTML = sourceContainer.innerHTML;
			}
			
			// Empty source container.
			sourceContainer.innerHTML = "";
			
			// Show destination container.
			destinationContainer.style.display = "block";
		}
	}
}

// Copy data to clipboard.
function copyToClipboard(containerID)
{    
    if (window.clipboardData && clipboardData.setData)
	{
        // Internet Explorer.
		clipboardData.setData("text", document.getElementById(containerID).innerHTML);
    }    
    else 
	{
        // All other browsers.
		alert("Het exporteren van gebruikers werkt alleen in Internet Explorer.");
    }
}

// Show container if not empty.
function showIfNotEmpty(containerID)
{
	if (document.getElementById(containerID))
	{
		var container = document.getElementById(containerID);
		
		// Remove white spaces (needed for Firefox).
		var containerContent = container.innerHTML.replace(/^\s+$/, "");
		
		// Check if there is content.
		if (containerContent.length > 0)
		{			
			// Show container.
			container.style.display = "block";
		}
	}
}

// Move selected listpicker items from source list to destination list.
function listPickerMoveItem(sourceID, destinationID)
{ 		
	$("#" + sourceID + " option:selected").appendTo("#" + destinationID);
}            

// Move all selected listpicker items from source list to destination list.
function listPickerMoveAllItems(sourceID, destinationID)
{
	$("#" + sourceID + " option").appendTo("#" + destinationID);
}

// Move selected listpicker items to hidden field.
function listPickerGetSelectedItems(selectedItemsListID, hiddenFieldID)
{	
	var selectedItems = document.getElementById(selectedItemsListID);
	var hiddenField = document.getElementById(hiddenFieldID);
	
	if (selectedItems && hiddenField)
	{	
		// Clear previous items.
		hiddenField.value = "";	
		
		// Add selected items to hidden field.
		for (var i = 0; i < selectedItems.options.length; i++)
		{
			hiddenField.value += ';' + selectedItems.options[i].value;			
		}
	}
}

// Count characters in a form field.
function countCharacters(inputField, containerID, maximumCharacters)
{	
	var counterContainer = document.getElementById(containerID);
	
	if (counterContainer)
	{
		// Check if input object exists. If not, search for it.
		if (typeof(inputField) != "object")
		{		
			if (document.getElementById(inputField).getElementsByTagName("textarea")[0])
			{
				// Textarea.
				inputField = document.getElementById(inputField).getElementsByTagName("textarea")[0];
			}
			else
			{
				// Text field.
				inputField = document.getElementById(inputField).getElementsByTagName("input")[0];
			}
		}
		
		if (inputField.value.length > maximumCharacters)
		{
			// Trim characters in form field if maximum is reached.
			inputField.value = inputField.value.substring(0, maximumCharacters);
		}	
		else
		{
			// Write number of remaining characters in a field.
			counterContainer.innerHTML = maximumCharacters - inputField.value.length;
		}
	}
}

// Check mobile device.
function checkMobileDevice()
{
	//Check if mobile device is an iOS device.
	if((navigator.userAgent.match(/iPhone/i))||(navigator.userAgent.match(/iPod/i))||(navigator.userAgent.match(/iPad/i)))
	{	  
		// Check if cookie exists, if not, show message.
		if(getCookie("mettMobileCookie") == "")
		{
			var expires = new Date();			
			
			if (confirm('Gratis de Mett iPhone/iPad app downloaden?'))
			{ 
				// Cookie expires after 1 year.				
				expires.setDate(expires.getDate() + 365);
				
				// Go to Mett app in iTunes.
				window.location = "http://appstore.mett.nl";
			} 
			else
			{				
				// Cookie expires after 1 week.				
				expires.setDate(expires.getDate() + 7);
			}
			
			document.cookie = "mettMobileCookie=true;expires=" + expires.toGMTString() + ";path=/;domain=" + document.domain;			
		}
	}
}

// Trim value.
function trim(value) 
{
	value = value.replace(/^\s+/, '');
	value = value.replace(/\s+$/, '');

	return value;
}

// Apply filter to a list of items.
function applyFilter(filterContainer, filterSelected, listItemContainer, listItemClassName) 
{
	// Show filter.
	document.getElementById(filterContainer + "Content").innerHTML = filterSelected;
	document.getElementById(filterContainer).style.display = "block";
}


// Alters the destination of links.
function alterLinks(linkContainer, linkDestination)
{
	if (document.getElementById(linkContainer))
	{
		// Get all links.
		var links = document.getElementById(linkContainer).getElementsByTagName("a");
		
		// Change the destination url of the link.
		for(var i = 0; i < links.length; i++) 
		{
			links[i].href = links[i].href.replace(document.domain, document.domain + linkDestination);
		}
	}
}

// Move webbox(es) to one or more containers
function moveWebbox(sourceContainerID, firstDestinationContainerID, secondDestinationContainerID)
{
	if (document.getElementById(sourceContainerID))
	{
		if(document.getElementById(firstDestinationContainerID) && document.getElementById(secondDestinationContainerID))
		{
			var destinationContainerID;
			var webboxes = $("#" + sourceContainerID + " div" + ".webboxTitle");
			
			if (webboxes.length > 0)
			{
				// Hide source container.
				document.getElementById(sourceContainerID).style.display = "none";
		
				for(var i = 0; i < webboxes.length; i++) 
				{
					if(i % 2)
					{
						// Second destination container.
						destinationContainerID = secondDestinationContainerID;
					}
					else
					{
						// First destination container.
						destinationContainerID = firstDestinationContainerID;
					}
					
					// Move webbox.
					document.getElementById(destinationContainerID).innerHTML = document.getElementById(destinationContainerID).innerHTML + "<div class=\"webbox\">" + webboxes[i].parentNode.innerHTML + "</div>";
				}
			}	
		}
	}
}
