// JavaScript Document
<!-- // JavaScript Code - uirc.js
/*
* This javascript file controls the navigation bar functionality for the
* UIRC web site.
*
* This code is a modification of code found on the following web site:
* http://www.rgagnon.com/jsdetails/js-0086.html
*
* Please read documentation to properly use this file (Navigation Bar)
*/

//-----------------------------------------------------------------< INITIALIZATION >

//arrow images
ARROW_CLOSED_SRC = "images/design/arrow_closed_[TYPE].gif";
ARROW_OPENED_SRC = "images/design/arrow_opened_[TYPE].gif";

//open menu for the page.
var openMenu;

var overImage;
var outImage;

/*
* assign what browser type
*/
var browserType;
var browserName;
var browserVersion;

if (document.layers) {browserType = "nn4"}
if (document.all) {browserType = "ie"}
if (window.navigator.userAgent.toLowerCase().match("gecko")) {
   browserType= "gecko"
}

if(document.images)
{
	overImage = new Image();
	overImage.src = "../images/design/bg_navbarFull.png";
	
	outImage = new Image();
	outImage.src = "../images/design/bg_menuFull.png";

	submenuoverImage = new Image();
	submenuoverImage.src = "../images/design/bg_submenuhoverFull.png";
	
	submenuoutImage = new Image();
	submenuoutImage.src = "../images/design/bg_submenuFull.png";
}

/*
* Initialization function called upon page load for index.html
*/
function initialize()
{
	assignMenu();
	assignMenuArrows('menu');
	openCurrentMenu('menu');
}

//-----------------------------------------------------------------< PUBLIC METHODS >

function detectBrowser()
{
	browserName = navigator.appName;
	var b_version = navigator.appVersion;
	
	browserVersion = parseFloat(b_version);
	
}

/*
* Retrieve a page element by its ID
* Accounts for different browsers handling
* @param id value for the id attribute of the element
*/
function getElement(id)
{
if (browserType == "gecko" )
     return eval('document.getElementById(id)');
  else if (browserType == "ie")
     return eval('document.getElementById(id)');
  else
     return eval('document.layers[id]');
}

/*
* Toggle visibility of divs.  Works recursively
* @param id Id of element to toggle
* @param forceClose Boolean variable used for recursive call
*/
function toggle(id, forceClose)
{
	var elmt; //current element for iteration
    var i=1; //iteration counter
	var arrow; //arrow image
	var prefix; //url prefix based on arrow source relative path
	var toggleElmt = id.lastIndexOf("_") > 0 ? id.substring(0, id.lastIndexOf("_")) : id+i;
	if(traceEnabled)
		alert("toggleElmt: "+toggleElmt);
	//go thru all items found and toggle
	while(elmt = getElement(toggleElmt+"_"+i))
	{
		if(traceEnabled)
		alert("toggleElmt: "+toggleElmt+"_"+i);
		toggle_rec(toggleElmt+"_"+i, true);		
		i++; //step up
	}
	
	toggle_rec(id, forceClose);
}

/*
* Toggle visibility of divs.  Works recursively
* @param id Id of element to toggle
* @param forceClose Boolean variable used for recursive call
*/
function toggle_rec(id, forceClose)
{
	var elmt; //current element for iteration
    var i=1; //iteration counter
	var arrow; //arrow image
	var prefix; //url prefix based on arrow source relative path
	
	//go thru all items found and toggle
	while(elmt = getElement(id+"_"+i))
	{
		var classname = getElement(id).className.replace("Selected", "");;
		if(classname == "menuTop") classname = "menu";
		
		//if element is visible or being forced closed by a parent menu
		if(elmt.style.display == "block" || forceClose)
		{
			if(traceEnabled)
				alert("close: "+elmt.id);
				
			elmt.style.display = "none";			
			elmt.disabled = true;
			
			//assign proper close arrow based on menu class
			if(arrow = getElement(id+"_arrow"))
			{
				prefix = arrow.src.substring(0, arrow.src.indexOf('images')); //assign prefix based on images folder
				arrow.src = prefix + ARROW_CLOSED_SRC.replace("[TYPE]", classname);
			}
			//recursive - close all sub items
			toggle_rec(id+"_"+i, true);
		}
		else //element is invisible
		{
			if(traceEnabled)
				alert("open: "+elmt.id);
				
			elmt.style.display = "block";
			elmt.disabled = false;
						
			//assign proper open arrow based on menu class
			if(arrow = getElement(id+"_arrow"))
			{
				prefix = arrow.src.substring(0, arrow.src.indexOf('images')); //assign prefix based on images folder
				arrow.src = prefix + ARROW_OPENED_SRC.replace("[TYPE]", classname);
			}
		}
		
		i++; //step up
	}
}

/*
* Assign global variable value based on get variable passed in url.
* NOTE: This works in conjunction with the url passing the GET variable "s"
*       which contains the menu hierachy without the keyword 'menu'
*       i.e. for menu3_1_1, [url]?s=3_1_1
*/
function assignMenu()
{
	var menu_id = getVar('s');
	if(menu_id == "")
	{
		menu_id = "10";
	}
	openMenu = 'menu'+menu_id; //get menu item
}

/*
* Assign arrow visibility to menu items that have at least one child
* NOTE: This works in conjunction with the default css value of visibility
*       set to 'hidden' for all arrow images in the corresponding css file.
*/
function assignMenuArrows(id)
{
	var elmt; //current element for iteration
    var i=1; //iteration counter
	
	//go thru all items found and toggle
	while(elmt = getElement(id+i))
	{
		//if there's one child element with suffix "_1"
		if(getElement(id+i+"_1"))
		{
			getElement(id+i+"_arrow").style.visibility = "visible";
			//recurse thru sub menus
			assignMenuArrows(id+i+"_");
		}
		
		i++; //step up
	}
}

/*
* Opens the current menu item according to value
* set for openMenu variable in html file
* If no value is set, return
* @param id current menu id being checked
*/
function openCurrentMenu(id)
{
	var openMenuElement = getElement(openMenu);
	var openMenuElementLink = getElement(openMenu+"_link");
	var breadCrumbsCell = getElement('breadcrumbsCell');
	
	if(!openMenuElement)
	{
		return;
	}
	
	
	
	if(breadCrumbsCell)
	{
		breadCrumbsCell.innerHTML = '<a href="'+openMenuElementLink.href+'">' + 
			(document.all ? openMenuElement.innerText : openMenuElement.textContent)+'</a>';
	}
	var elmt; //current element for iteration
    var i=1; //iteration counter
	//alert('id: '+id);
	//go thru all items found and toggle
	while(elmt = getElement(id+i))
	{
		
		//if element id is the one set
		if(elmt.id == openMenu)
		{
			//toggle both parents.
			var firstIndex = id.indexOf("_");
			var lastIndex = id.lastIndexOf("_");
			
			var toggleElmt;
			var toggleElmtParent;
			
			var toggleElmtInstance;
			var toggleElmtParentInstance;
			
			var toggleElmtInstanceLink;
			var toggleElmtParentInstanceLink;
						
			// last index is greater than 0, it is a submenu or submenuitem
			if(lastIndex > 0)
			{
				//if first and last index are the same, element is a submenu
				if(firstIndex != lastIndex)
				{
					//so it's a submenuitem, hence open submenu
					toggleElmt = id.substring(0, id.lastIndexOf("_"));
				}
				else //so its a submenu, hence assign to open itself
				{
					toggleElmt = elmt.id;
				}
			}
			else //it's a menu... assign to open it
			{
				toggleElmt = id+i;
			}
			
			toggleElmtParent = toggleElmt.substring(0, toggleElmt.lastIndexOf("_"));
				
			toggle(toggleElmtParent);
			toggle(toggleElmt);
			
			toggleElmtInstance = getElement(toggleElmt);
			toggleElmtInstanceLink = getElement(toggleElmt+"_link");
			
			toggleElmtParentInstance = getElement(toggleElmtParent);
			toggleElmtParentInstanceLink = getElement(toggleElmtParent+"_link");
	
			if(breadCrumbsCell)
			{
				if(toggleElmtInstance)
				if(openMenuElement != toggleElmtInstance)
				{
					if(breadCrumbsCell.innerHTML != '')
						breadCrumbsCell.innerHTML = ' <span class="breadcrumbSeparator"> &gt; </span>' + breadCrumbsCell.innerHTML;
					breadCrumbsCell.innerHTML = '<a href="'+toggleElmtInstanceLink.href+'">' + 
						(document.all ? toggleElmtInstance.innerText : toggleElmtInstance.textContent)+'</a>' + ' ' + 
						breadCrumbsCell.innerHTML;
				}
		
				if(toggleElmtParentInstance)
				if(openMenuElement != toggleElmtParentInstance)
				{
					if(breadCrumbsCell.innerHTML != '')
						breadCrumbsCell.innerHTML = ' <span class="breadcrumbSeparator"> &gt; </span>' + breadCrumbsCell.innerHTML;
					breadCrumbsCell.innerHTML = '<a href="'+toggleElmtParentInstanceLink.href+'">' + 
					(document.all ? toggleElmtParentInstance.innerText : toggleElmtParentInstance.textContent)+'</a>' + ' ' + 
					breadCrumbsCell.innerHTML;			}
			}
			//toggleElmtParentInstance.innerHTML+'<span class="breadcrumbSeparator"> &gt; </span>'+toggleElmtInstance.innerHTML;
			
			//assign proper styles
			//@TODO: Not working, please revise.
			//if(toggleElmtInstance) toggleElmtInstance.className = toggleElmtInstance.className + "Selected";
			//if(toggleElmtParentInstance) toggleElmtParentInstance.className = toggleElmtParentInstance.className + "Selected";
			
			elmt.className = elmt.className+"Selected";
			
			openMenu = null; //stops following iterations
			
		}
		else
		{
			openCurrentMenu(id+i+"_");
		}
		
		i++; //step up
	}
}

/**
 * retrieves get variable from url
 * < http://scripts.franciscocharrua.com/javascript-get-variables.php >
 */
function getVar(name)
 {
 get_string = document.location.search;         
 return_value = '';
 
 do { //This loop is made to catch all instances of any get variable.
	name_index = get_string.indexOf(name + '=');
	
	if(name_index != -1)
	  {
	  get_string = get_string.substr(name_index + name.length + 1, get_string.length - name_index);
	  
	  end_of_value = get_string.indexOf('&');
	  if(end_of_value != -1)                
		value = get_string.substr(0, end_of_value);                
	  else                
		value = get_string;                
		
	  if(return_value == '' || value == '')
		 return_value += value;
	  else
		 return_value += ', ' + value;
	  }
	} while(name_index != -1)
	
 //Restores all the blank spaces.
 space = return_value.indexOf('+');
 while(space != -1)
	  { 
	  return_value = return_value.substr(0, space) + ' ' + 
	  return_value.substr(space + 1, return_value.length);
					 
	  space = return_value.indexOf('+');
	  }
  
 return(return_value);        
 }

////////////////////////////////////////////////
// Form validation functions
////////////////////////////////////////////////

/*
 * Checks the form fields if they are valid.
 */
function Validate(formID)	{

	var form = document.getElementById(formID);
	var isValid = true;
	var errorHolder = document.getElementById('errorMsg');
	var errorMsg = "<em>Sorry there has been an error</em> <br />";
	
	for(var i=0;i<form.elements.length;i++)	{
	
		var element = form.elements[i];
		
		if(element.className.indexOf('required') != -1)	{
			if(!isFilled(element))	{
				errorMsg+="Please fill in the "+element.name+" field.<br/>";
				element.style.backgroundColor = '#FFCCCC';
				isValid = false;
			}
			else //reset field background.
			{
				element.style.backgroundColor = '#FFFFFF';
			}
			
			if(element.className.indexOf("email") != -1)	{
			
				if(isEmail(element))	{
					errorMsg+="The "+element.name+" field must be a valid email address.<br/>";
					isValid = false;
				}
				
			}
		
			
		}
		
	}
		
	if(isValid == false)	{
		errorHolder.innerHTML = errorMsg;
		errorHolder.style.display = 'block';
		scrollTo(0,0);
	} else {
          form.submit();
	}

	return isValid;

}

function isFilled(field)	{
	
	//check for select object
	if(!isNaN(field.selectedIndex))
	{
		if(field.selectedIndex == 0)	{
			return false;
		}	
		else	{
			return true;	
		}
	}
	else //check for input fields
	{
		if(field.value.length < 1)	{
			return false;
		}	
		else	{
			return true;	
		}
	}
}

function isEmail(email)	{

	if(field.value.indexOf('@') == -1 || field.value.indexOf(".") == -1)	{
		return false;	
	}	else	{
		return true;	
	}

}

var traceEnabled = false;

function toggleMenu(id)
{
	var menuLink = getElement(id+'_link');
	var arrow = getElement(id+"_arrow");
	if(document.location == menuLink.href)
	{
		//alert('toggle('+id+')');
		//traceEnabled = true;
		toggle(id, arrow.src.indexOf('arrow_opened') > 0);
		return false;
	}
	
	return true;
}

//////////////////////////////////
// MENU ROLLOVER
//////////////////////////////////

function mouseOverImage(id)
{
	if(browserName == "Microsoft Internet Explorer" && browserVersion > 6)
	{
		return false;
	}
	
	if(id)
	{
		getElement(id).style.background = "url"+"('"+submenuoverImage.src+"')";
		return true;
	}
	else
	{
		return false;
	}
}

function mouseOutImage(id)
{
	if(browserName == "Microsoft Internet Explorer" && browserVersion > 6)
	{
		return false;
	}
	
	if(id)
	{
		getElement(id).style.background = "url"+"('"+submenuoutImage.src+"')";
		return true;
	}
	else
	{
		return false;
	}
}

function mailToCurrent()
{
	link = document.location

	// %0A is a newline which we use for formatting the body of the email
	document.location = 'mailto:?subject=Saw this on the UI Resource Center&body=I just saw this on the UI Resource Center and thought you would be interested.%0A%0A' + link
}








// END OF JAVASCRIPT CODE -->