var fading;
var imageIndex = 0;

function swapImage(id,src)
{
	document.getElementById(id).src = src;
	return;
}

function addLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
			window.onload = func;
	} else {
		window.onload = function()
		{
			if (oldonload) {
				oldonload();
			}
		func();
		}
	}
}

function activateLinks()
{
	document.getElementById('previousImageLink').href="javascript:;";
	document.getElementById('previousImageLink').onclick=function()
	{
		return detailImageFade(0);
	}
	document.getElementById('nextImageLink').href="javascript:;";
	document.getElementById('nextImageLink').onclick=function()
	{
		return detailImageFade(1);
	}
}

function detailImageFade(direction)
{
	if (!fading) {
		new Effect.Fade(document.getElementById('bigImg'));
		if (direction == 0) // back
		{
			if (imageIndex == 0)
			{
				imageIndex = imageArray.length - 1;
			} else {
				imageIndex--;
			}
		} 
		else if (direction == 1) // forward 
		{
			if (imageIndex == imageArray.length - 1)
			{
				imageIndex = 0;
			} else {
				imageIndex++;
			}
		}
		var txtDiv = document.getElementById('currentImage');
		txtDiv.innerHTML = imageIndex + 1;
		imgsrc = imageArray[imageIndex];
		this.count = 0;
		s1 = setInterval("fadeImgIn(imgsrc)",10);
	}
}

function fadeImgIn(src)
{
	if (src != undefined) {
		this.count = this.count + 1;
		fading=true;
		if (this.count > 120) {
			fading=false;
			clearInterval(s1);
			document.getElementById('bigImg').src=src;
			new Effect.Appear("bigImg");
		}
	}
}

function displayError(formNode, validators)
{
	var listNode = document.createElement("ul");
	for(var i=0;i<validators.length;i++)
	{
		if (validators[i].message != '')
		{
			var itemNode = document.createElement("li");
			itemNode.appendChild(document.createTextNode(validators[i].message));
			listNode.appendChild(itemNode);
		}
	}
	var displayNode = document.getElementById("errorList"); 
	if (document.getElementById('error1') != null)
	{
		document.getElementById('error1').style.display = 'none';
	}
	if (document.getElementById('notification') != null)
	{
		document.getElementById('notification').style.display = 'none';
	}
	document.getElementById('error2').style.display = 'block';
	if (document.getElementById('top'))
	{
		document.getElementById('top').focus();
	}
}

function popupSpecific(address, width, height, scrollbar, resizable) 
{
	var screenX = screen.width
	var screenY = screen.height
	if (resizable == undefined){
		resizable = 0;
	}
	var x = (screenX - width)/2;
	var y = (screenY - height)/3;
	var random_num = (Math.round((Math.random()*9)+1));
	
	var popup = window.open(address,"popup"+random_num,"resizable="+resizable+" ,scrollbars="+scrollbar+",width=" + width + ",height=" + height + ", top=" + y + ", left=" + x);
	popup.focus();
}

function formatAsDollars(amt)
{
	// return a 0 dollar value if amount is not valid
	// (you may optionally want to return an empty string)
	if (isNaN(amt))
	{
		return "$0.00";
	}
	// round the amount to the nearest 100th 
	amt = Math.round(amt * 100) / 100;
	// convert the number to a string
	var amount_str = String(amt);
	// split the string by the decimal point, separating the
	// whole dollar value from the cents. Dollars are in
	// amount_array[0], cents in amount_array[1]
	var amount_array = amount_str.split(".");
	// if there are no cents, add them using "00"
	if (amount_array[1] == undefined)
	{
		amount_array[1] = "00";
	}
	// if the cents are too short, add necessary "0" 
	if (amount_array[1].length == 1)
	{
		amount_array[1] += "0";
	}
	// add the dollars portion of the amount to an 
	// array in sections of 3 to separate with commas
	var dollar_array = new Array();
	var start;
	var end = amount_array[0].length;
	while (end > 0)
	{
		start = Math.max(end - 3, 0);
		dollar_array.unshift(amount_array[0].slice(start, end));
		end = start;
	}
	// assign dollar value back in amount_array with
	// the a comma delimited value from dollar_array
	amount_array[0] = dollar_array.join(",");
	// finally construct the return string joining
	// dollars with cents in amount_array
	return ("$" + amount_array.join("."));
}