var ie = document.all;
var ns6 = document.getElementById && !document.all;

function AddLoadEvent(fct)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
		window.onload = fct;
	}
	else
	{
		window.onload = function()
		{
			if (oldonload)
			{
				oldonload();
			}
			fct();
		}
	}
}

function HLRow(row, highlight)
{
	if(highlight)
	{
		row.oldClassName = row.className;
		row.className += 'over';
	}
	else 
		row.className = row.oldClassName;
}

function SearchGoogleKeyPress(e, myfield)
{
	var code;
	if(e.keyCode)
		// IE
		code = e.keyCode;
	if(e.which)
		// Other browser
		code = e.which;

	// Enter key
	if(code == 13)
	{
		window.location = 'Search.aspx?q=' + myfield.value;
	}
}

function SearchGoogleClick(textfieldid)
{
	window.location = 'Search.aspx?q=' +  window.document.getElementById(textfieldid).value;
}

function KeyDownHandler(e, btn)
{
	var code;
	if(e.keyCode)
		// IE
		code = e.keyCode;
	if(e.which)
		// Other browser
		code = e.which;

	// Enter key
	if(code == 13)
	{
		// Cancel the default submit
		if(ie)
		{
			e.returnValue = false;
			e.cancel = true;
		}
		if(ns6)
		{
			//e.which = 0;
			e.preventDefault();
			e.stopPropagation();
		}
		// Submit the form by programmatically clicking the specified button
		btn.click();
	}
}

function PicRotatorItem(filename, url)
{
	this.item = new Object();
	this.item.src = filename;
	this.item.url = url;
}

function PicRotatorNext(picRotatorItemsVar, rotationMode, picRotatorSeqVar)
{
	var picRotatorItems = window[picRotatorItemsVar];
	var t = picRotatorItems.length;
	if (t == 0)
		return;

	if (rotationMode == 'Sequential')
	{
		var picRotatorSeq = window[picRotatorSeqVar];
		if (picRotatorSeq < t)
		{
			picRotatorSeq++;
		}
		else
		{
			picRotatorSeq = 1;
		}
		window[picRotatorSeqVar] = picRotatorSeq;
		return picRotatorItems[picRotatorSeq - 1];
	}
	else
	{
		// TODO: var to remember the last "i" (like iRan) avoiding displaying twice the same picture - FAP
		var i = Math.floor(Math.random() * t)
		return picRotatorItems[i];
	}
}

function PicRotatorRotate(picRotatorID, picRotatorItemsVar, interval, rotationMode, picRotatorSeqVar)
{
	var nextPic = PicRotatorNext(picRotatorItemsVar, rotationMode, picRotatorSeqVar);
	var picRotator = document.getElementById(picRotatorID);
	if (picRotator && nextPic)
	{
		var pic = picRotator.childNodes[0];
		if (pic)
			pic.src = nextPic.item.src;

		picRotator.href = nextPic.item.url;

		var recursiveCall = "PicRotatorRotate('" + picRotatorID + "', '" + picRotatorItemsVar + "', " + interval + ", '" + rotationMode + "', '" + picRotatorSeqVar + "')";
		setTimeout(recursiveCall, interval);
	}
}
