function Menu()
{
	this.fade = 0;
	this.opacity = 0;
}

Menu.prototype.name;
Menu.prototype.file;
Menu.prototype.image;
Menu.prototype.fade;
Menu.prototype.opacity;

var change = 10;
var duration = 25;

var menus = new Array();

var filename = getFileName();
if (filename == "")
{
	filename = "home";
}
 
var animating = false;

var frame = 0;
var element = 1;

function load()
{
	var parent = document.getElementById("menus");
	
	for (var i = 0; i < parent.childNodes.length; i++)
	{		
		var item = parent.childNodes[i];	
		
		if (item.nodeType != element)
			continue;			
		
		var link = item.firstChild;	
		var image = link.firstChild;
		
		//var visibility = getStyle(image, "visibility");
		
		//if (visibility == "hidden")
		
		if (image.id == filename)
		{
			setOpacity(image, 100);	
		    
		}
		else
		{		
			var menu = new Menu();
			menu.image = image;
			menus.push(menu);
			addMouseEvents(item, menus.length - 1);			
			setOpacity(image, 0);		
		}	
		
		image.style.visibility = "visible";
	}
	
	window.setTimeout("animate()", duration);
}

function addMouseEvents(item, index)
{
	item.onmouseover = function()
	{
		menus[index].fade = change;
		startAnimation();
	}
	
	item.onmouseout = function()
	{
		menus[index].fade = -change;
		startAnimation();
	}
}

function animate()
{
	var fading = 0;
	for (i in menus)
	{
		var menu = menus[i];
		if (menu.fade != 0)
		{
			menu.opacity += menu.fade;
			setOpacity(menu.image, menu.opacity);
			if ((menu.fade == change && menu.opacity >= 100) || (menu.fade == -change && menu.opacity <= 0))
				menu.fade = 0;
			if (menu.fade != 0)
				fading++;
		}
	}
	if (fading > 0)
		window.setTimeout("animate()", duration);
	else
		animating = false;
}

function startAnimation()
{
	if (animating)
	{
		return;
	}
	
	window.setTimeout("animate()", duration);
	animating = true;
}

function getFileName() 
{
	var file = document.location.href;

	var start = file.lastIndexOf("/");

	if (start == -1) 
	{
		return "";
	}

	finish = file.indexOf(".", start + 1);

	return file.slice(start + 1, finish);
}

function fadeIn(index,opacity) 
{
	if (document.getElementById) 
	{
		obj = document.getElementById(menus[index].name);
		if (opacity <= 100)
		{
			setOpacity(obj, opacity);
			opacity += 10;
			window.setTimeout("fadeIn('"+index+"',"+opacity+")", 16);
		}
	}
}

function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}

function setOpacity(obj, opacity) 
{
	opacity = (opacity == 100)?99.999:opacity;

	// IE/Win
	obj.style.filter = "alpha(opacity:"+opacity+")";

	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;

	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;

	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}

function checkInteger(integer)
{
	var characters = "0123456789";
 
	for (i = 0; i < integer.length; i++) 
	{ 
		var character = integer.charAt(i); 
		
		if (characters.indexOf(character) == -1) 
			return false;
	}

	return true;
}

function checkAddress(address)
{
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	return filter.test(address);
}

function checkPhone(text)
{
	var phone = "";
	var i;
	for (var i = 0; i < text.length; i++)
	{
		var char = text.substr(i, 1);
		if (char >= "0" && char <= "9")
		phone += char;
	}
	return phone.length == 10;
}

function validatePassword()
{
	if (document.form.password.value == "" )
	{
		alert("Password cannot be empty.");
		return false;
	}
	if (document.form.password.value != document.form.confirm.value)
	{
		alert("Passwords do not match.");
		return false;
	}
	return true;
}

function validateEmail(email)
{
	if (email == undefined)
		email = document.form.email.value;
	return checkAddress(email);
}

function validateInfo()
{
	if (!validateEmail())
	{
		alert("Please provide a valid Email Address.");
		return false;
	}
	if (document.form.first.value == "")
	{
		alert("First name cannot be empty.");
		return false;
	}
	if (document.form.last.value == "")
	{
		alert("Last name cannot be empty.");
		return false;
	}
	if (document.form.phone.value != "" && !checkPhone(document.form.phone.value))
	{
		alert("Please enter a valid 10 digit phone number.");
		return false;
	}
	return true;
}

function validateAccount()
{		
	return validatePassword() && validateInfo();
}

function validateTeam()
{
	if (document.form.name.value == "")
	{
		alert("Team name cannot be empty.");
		return false;
	}
	if (document.form.city.value == "")
	{
		alert("City name cannot be empty.");
		return false;
	}
	if (document.form.state.selectedIndex == 0)
	{
		alert("Please select your state.");
		return false;
	}
}

function validateMessage()
{
	if (document.form.text.value == "")
	{
		alert("Message text cannot be empty.");
		return false;
	}
}

function updateRemainingCharacters(textBox)
{
	var label = document.getElementById("remaining");
	var text = textBox.value;
	if (textBox.value.length > 160)
		textBox.value = textBox.value.substr(0, 160);
	var remaining = 160 - textBox.value.length;
	label.firstChild.nodeValue = remaining + " characters remaining";
}