// JavaScript Document

// Return document element with id.
function getId(id){ return document.getElementById(id); }

// Set a cookie.
function setCookie(name, value, nDays)
{
	var today	= new Date();
	var expire	= new Date();

	if (nDays==null || nDays==0)
		nDays=1;

	expire.setTime(today.getTime() + 3600000 * 24 * nDays);
	document.cookie = name +
		"=" + escape(value) +
		"; expires=" + expire.toGMTString() +
		"; path=/";
}

function fFindNode( aParentNode, aNodeName, aIndex )
{
	var i = 0;
	var vResultNode = null;

	if( aIndex == null )
	{
		aIndex = 0;
	}

	var vNodeList = aParentNode.getElementsByTagName( aNodeName );

	if( vResultNode = vNodeList[aIndex] )
	{
		return vResultNode;
	}

	for( var vNode in vNodeList )
	{
		if( vResultNode = fFindNode( vNodeList[vNode], aNodeName, aIndex ) )
		{
			return vResultNode;
		}
	}
}


function fMenuHover( aMenuRow )
{
	var vMenuImg = fFindNode( aMenuRow, "img", 0 );
	var vMenuCell = fFindNode( aMenuRow, "td", 1 );
	var vMenuCellType = vMenuCell.getAttribute('class');

	if( vMenuImg.src.indexOf( "menu_block.gif" ) >= 0 ||
			vMenuCellType == "menu_sub_selected" )
	{
		aMenuRow.style.cursor = "default";
		return;
	}

	aMenuRow.style.cursor = "pointer";

	if( vMenuCell.style.backgroundColor != vMenuCell.bgColor )
	{
		vMenuCell.style.backgroundColor = vMenuCell.bgColor;
		vMenuCell.style.color = vMenuCell.txtColor;

		if( vMenuCellType == "menu_item" )
		{
			vMenuImg.src = vMenuImg.imgSrc;
		}

		return;
	}

	vMenuCell.bgColor = vMenuCell.style.backgroundColor;
	vMenuCell.txtColor = vMenuCell.style.color;
	vMenuImg.imgSrc = vMenuImg.src;

	vMenuCell.style.backgroundColor = '#eff7fd';
	vMenuCell.style.color = '#ffffff';

	if( vMenuCellType == "menu_item" )
	{
		vMenuImg.src = '/images/menu_block_over.gif';
	}
}

function fSwapFontSize()
{
	var vBody = document.getElementsByTagName('body')[0];
	var vListContainer = document.getElementById('service_container');
	var vListNode = document.getElementById('service_list');

	if( vBody.className != "large" ) {
		vBody.className = "large";
	} else {
		vBody.className = "small";
	}

	vListContainer.removeChild(vListNode).cloneNode(true).appendChild(vListNode);
	vListContainer.appendChild(vListNode);

	setCookie('fontSize', vBody.className, 365);
}

function setCookie(name, value, nDays)
{
	var today	= new Date();
	var expire	= new Date();

	if (nDays==null || nDays==0)
		nDays=1;

	expire.setTime(today.getTime() + 3600000 * 24 * nDays);
	document.cookie = name +
		"=" + escape(value) +
		"; expires=" + expire.toGMTString() +
		"; path=/";
}

function displayCategory( cat )
{
	if( cat == 'all' )
	{
		document.location.replace( 'index.jsp' );
	}
	else {
		document.location.replace( 'index.jsp?cat=' + cat );
	}
}

function displayDiagram( mmbaseNodeNumber )
{
	if( mmbaseNodeNumber == 'actual' )
	{
		document.location.replace( 'index.jsp' );
	}
	else {
		document.location.replace( 'index.jsp?id=' + mmbaseNodeNumber );
	}
}

function fNotaFeedback(aHTML)
{
	var vDiv = document.getElementById("nota_feedback");
	if (vDiv == null)
	{
		alert("Error: couldn't find div with id 'nota_feedback'.");
		return;
	}
	vDiv.innerHTML = unescape(aHTML);
}

function fClearForm(aForm) {

	var vForm = document.forms[aForm];

	for (var i = 0; i < vForm.elements.length; i++) {

		// Input fields
		var elements = vForm.getElementsByTagName('input');
		for(var i = 0; i < elements.length; i++ ) {
			var elm = elements[i];
			if (elm.getAttribute('type') == "text") {
				elm.value = '';
			}
			else if (elm.getAttribute('type') == "radio" || elm.getAttribute('type') == "checkbox") {
				elm.checked = false;
			}
		}

		// Textareas
		var textareas = vForm.getElementsByTagName('textarea');
		for(var j = 0; j < textareas.length; j++) {
			textareas[j].value = '';
		}
	}
}

function icstatsWrapper(pageId) {

	icstats('http://www.icstats.nl/asp/icstats.asp?siteID=260497&p=' + pageId);
}
