/****************************************************
*					  functions.js					
*****************************************************
* Description: A Library of functions	  
* Created: 09 Dec 04		By: Scott O'Dell	
* Last Updated: 15 Sep 07	By: Scott O'Dell	
*****************************************************/


/* from: http://www.quirksmode.org/js/dhtmloptions.html
 * .obj gives access to the actual HTML element
 * .style gives access to the styles of the HTML element
 */
function getObj(name)
{
  if (document.getElementById)
  {
	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers)
  {
	this.obj = document.layers[name];
	this.style = document.layers[name];
  }
}

/* given a string, check that it is a number.
 * returns true if it is, false if it's not.
 */
function isNumeric(str)
{
	var vaild = "0123456789.-";
	var character = '';
	var result = true;

	if (str.length == 0) {return false;}

	for (i=0; i<str.length && result==true; i++)
	{
		character = str.charAt(i);
		if (vaild.indexOf(character) == -1) {result = false;}
	}
	return result;
}

//pass an array of element ids to this function
//returns true is everything in the array is not equal to ''
//returns false as soon as it finds an empty element
//sets focus to that element and alerts.
function checkBlank(ids)
{
	for (var i=0; i<ids.length; i++)
	{
		var elm = document.getElementById(ids[i]);
		if (elm.value == '')
		{
			alert('Please do not leave this blank!');
			elm.focus();
			return false;
		}
	}
	return true;
}

//pass an array of element ids to this function
//uses isNumeric to alert and set focus is the argument is not a number
function checkNum(ids)
{
	for (var i=0; i<ids.length; i++)
	{
		var elm = document.getElementById(ids[i]);
		if (!isNumeric(elm.value))
		{
			alert('Please only enter a number!');
			elm.focus();
			elm.select();
			return false;
		}
	}
	return true;
}

//check that the registration form is valid
function errorReport(obj, msg)
{
	alert(msg);
	obj.focus();
	obj.select();
	return false;
}
function checkRegValid()
{
	//get variables
	var user = document.getElementById("username");
	var pass = document.getElementById("password");
	var pass2 = document.getElementById("password2");
	var fname = document.getElementById("fname");
	var email = document.getElementById("email");
	var email2 = document.getElementById("email2");
	
	//check username
	var regex = /^\w{3,25}$/;
	if (!regex.test(user.value)) return errorReport(user, 'Please check that you have entered a valid username.');
	
	//check password
	if (pass.value.length < 6) return errorReport(pass, 'Password is too short.');
	if (pass.value != pass2.value) return errorReport(pass, 'Passwords do not match.');
	
	//check name
	if (fname.value.length < 1) return errorReport(fname, 'Please enter your first name.');
	
	//check e-mail
	if (email.value.length < 1) return errorReport(email, 'Please enter your e-mail address.');
	var regex = /^[a-zA-Z](\w*\.?)+@(\w+\.?)+\.[a-zA-Z]{2,3}$/;
	if (!regex.test(email.value)) return errorReport(email, 'E-mail address is not properly formatted.');
	if (email.value != email2.value) return errorReport(email, 'E-mail Addresses do not match.');
	
	//all's well
	return true;
}

//check that the edit info forms are valid
function checkInfoValid()
{
	//check name
	if (document.getElementById("fname").value.length < 1) return errorReport(document.getElementById("fname"), 'Please enter your first name.');
	
	//all's well
	return true;
}
function checkPassValid()
{
	var pass = document.getElementById("password");
	var pass2 = document.getElementById("password2");

	//check password
	if (pass.value.length < 6) return errorReport(document.getElementById("password"), 'Password is too short.');
	if (pass.value != pass2.value) return errorReport(document.getElementById("password"), 'Passwords do not match.');

	//all's well
	return true;
}
function checkEmailValid()
{
	//get variables
	var email = document.getElementById("email");
	var email2 = document.getElementById("email2");
	
	//check e-mail
	if (email.value.length < 1) return errorReport(document.getElementById("email"), 'Please enter your e-mail address.');
	var regex = /^[a-zA-Z](\w*\.?)+@(\w+\.?)+\.[a-zA-Z]{2,3}$/;
	if (!regex.test(email.value)) return errorReport(document.getElementById("email"), 'E-mail address is not properly formatted.');
	if (email.value != email2.value) return errorReport(document.getElementById("email"), 'E-mail Addresses do not match.');
	
	//all's well
	return true;
}

//simulate :hover behavoir for IE. Taken from A List Apart
// http://www.alistapart.com/articles/dropdowns/
//modified to work with spans for a collapsible menu
startList = function()
{
	if (document.all&&document.getElementById)
	{
		var navRoot = document.getElementById("navigation");
		for (i=0; i<navRoot.childNodes.length; i++)
		{
			node = navRoot.childNodes[i];
			if (node.firstChild.nodeName=="LI")
			{
				node.firstChild.onmouseover=function()
				{
					this.className+=" over";
				}
				node.firstChild.onmouseout=function()
				{
					this.className=this.className.replace(" over", "");
				}				
			}
		}
	}
}

navigation = function()
{	
	//get tabs
	var tablist = document.getElementById('navigation').getElementsByTagName("div");
		
	//get the display area
	var display_area = document.getElementById('second');
	
	//initialize display area with home submenu
	//display_area.appendChild(document.getElementById('section0_sub'));
		
	//loop through the tabs and assign mousover events
	for (i=0; i<tablist.length; i++)
	{
		//get the ul.
		ul = document.getElementById('section' + i + '_sub');
		
		//add it to the display area and hide it		
		display_area.appendChild(ul);
		ul.style.display = 'none';
	
		//register event listeners
		tablist[i].onmouseover = function(e)
		{			
			//get the id and the number of the mouseover'd element
			var current;
			if (e) //all browers
			{
				current = e.currentTarget.id;
			}
			else //ie
			{
				current = window.event.srcElement.id;
			}
						
			var num = current.substr(7, 1);
			
			//get the numeber of tabs
			ul_list = document.getElementById('navigation').getElementsByTagName("div");

			//hide all the submenus, then display the one we want
			for(i=0; i<ul_list.length; i++)
			{
				document.getElementById('section' + i + '_sub').style.display = 'none';
				document.getElementById('section' + i + 'img').src = '/images/section' + i + 'small.png';
			}
			document.getElementById('section' + num + '_sub').style.display = 'inline';
			document.getElementById('section' + num + 'img').src = '/images/section' + num + 'small_active.png';
			
		}
	}
}

//ajax creation fun
function createRequestObject()
{
	var req;

	if(window.XMLHttpRequest)
	{
		//For Firefox, Safari, Opera
		req = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		//For IE 5+
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		return false;
	}
	
	return req;
}

//this should always be last.
window.onload = function(){
	navigation();
};

