var byu__onLoadHandlers = new Array();
var byu__onLoadCompleteHandlers = new Array();
var byu__baseHref = "";

function byu__registerOnLoadHandler(f) {
	if( f == null ) return;
	byu__onLoadHandlers[byu__onLoadHandlers.length] = f; // push not supported on IE5
};
var byu__onLoadStarted = false;
function byu__onLoadChain() {
	if( byu__onLoadStarted ) return; // sometimes this gets called more than once
	byu__onLoadStarted = true;
	for(var i = 0; i < byu__onLoadHandlers.length; i++)
	{
		try
		{
			byu__onLoadHandlers[i]();
		}
		catch(error){}
	}
	
	if(!byu__IsDefined('byu__byuBarLoaded'))//calls to make if common-web.js is not included
	{
		byu__unhideElements();//unhides bars
		byu__onLoadCompleteChain();
	}
};
if(window.onload) byu__registerOnLoadHandler(window.onload);
window.onload = byu__onLoadChain;

function byu__registerOnLoadCompleteHandler(f) {
	if( f == null ) return;
	byu__onLoadCompleteHandlers[byu__onLoadCompleteHandlers.length] = f; // push not supported on IE5
};
var byu__onLoadCompleteStarted = false;
function byu__onLoadCompleteChain() {
	if( byu__onLoadCompleteStarted ) return; // sometimes this gets called more than once
	byu__onLoadCompleteStarted = true;
	for(var i = 0; i < byu__onLoadCompleteHandlers.length; i++)
	{
		try
		{
			byu__onLoadCompleteHandlers[i]();
		}
		catch(error){}
	}
};

function byu__isIE() {
	// Opera DOES implement document.all when in IE-masquerading mode, and reports as MSIE,
	// so we explicitly check for Opera as well as check for IE.
	return navigator.appVersion.indexOf('MSIE') != -1 && navigator.userAgent.indexOf('Opera') == -1;
};

function byu__getBaseHREF()//get's base path of stylesheets to be used for image swapping
{
	if(document.styleSheets.length > 0)
	{
		var cssUrl = document.styleSheets[0].href;
		byu__baseHref = cssUrl.substring(0,cssUrl.indexOf("collage/"));
	}
	else
	{
		byu__baseHref = "";
	}
};
byu__registerOnLoadHandler(byu__getBaseHREF);

/***********************************************************************
 * Common utility functions
 ***********************************************************************/

// getElementsByTagName returns all descendant nodes,
// but this method returns only those nodes that are immediate children.
function byu__getImmediateChildrenByTagName(element, tagName)
{
	if( !element ) return new Array(); // quit without error if null is sent in.
	var list = element.getElementsByTagName(tagName);
	// Keep elements that are immediate children 
	// of supplied element.
	var finalList = new Array();
	var finalIdx = 0;
	for( var i = 0; i < list.length; i++ )
		if( list[i].parentNode == element )
			finalList[finalIdx++] = list[i];
	return finalList;
};

function byu__getElementsByClassName(className, container, tagName) {
	var outArray = new Array();
	var all = container.getElementsByTagName(tagName ? tagName : '*');
	for( var i = 0; i < all.length; i++ )
	{
		if( all[i].className.substr() == className ) outArray[outArray.length] = all[i];
	}
	return outArray;
};

// function useful because Safari gets confused when there is more than one element with the same ID 
// even if only one of the elements is currently being displayed
function byu__getChildElementById(parent, id)
{
	for(var i=0; i<parent.childNodes.length; ++i)
	{
		if(parent.childNodes[i].id == id)
		{
			return parent.childNodes[i];
		}
	}
};

function byu__IsDefined(variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
};

function byu__clearCookies()
{
	var expDate = new Date("01/01/1980").toGMTString();
	document.cookie = 'FORMCRED=0;domain=.byu.edu;path=/;expires='+expDate;
	document.cookie = 'SMTRYNO=0;domain=.byu.edu;path=/;expires='+expDate;
	document.cookie = 'SECURE_COOKIE=anakincookieookie; domain=.byu.edu; path=/; expires='+expDate;
	document.cookie = 'usr_byu_edu=anakincookieookie; domain=.byu.edu; path=/; expires='+expDate;
	document.cookie = 'role_byu_edu=anakincookieookie; domain=.byu.edu; path=/; expires='+expDate;
	document.cookie = 'byu_ry_po_session=anakincookieookie; domain=.byu.edu; path=/; expires='+expDate;
	document.cookie = 'SmSESSION=anakincookieookie; domain=.byu.edu; path=/; expires='+expDate;
	document.cookie = 'SMIDENTITY=anakincookieookie; domain=.byu.edu; path=/; expires='+expDate;
	document.cookie = 'SMSESSION=anakincookieookie; domain=.byu.edu; path=/; expires='+expDate;
	document.cookie = 'https%3a%2f%2fhrms.byu.edu%2fpsp%2fhrprd2%2femployee%2fhrms%2frefresh=0;domain=.byu.edu;path=/;expires='+expDate;
	document.cookie = 'PS_TOKENEXPIRE=0;domain=.byu.edu;path=/;expires='+expDate;
	document.cookie = 'PS_TOKEN=0;domain=.byu.edu;path=/;expires='+expDate;
	document.cookie = 'PS_LOGINLIST=0;domain=.byu.edu;path=/;expires='+expDate;
	document.cookie = 'ExpirePage=0;domain=.byu.edu;path=/;expires='+expDate;
	document.cookie = 'SignOnDefault=0;domain=.byu.edu;path=/;expires='+expDate;
	document.cookie = 'HPTabName=0;domain=.byu.edu;path=/;expires='+expDate;
	document.cookie = 'queens-byu-edu-9080-PORTAL-PSJSESSIONID=0;domain=hrms.byu.edu;path=/;expires='+expDate;
	document.cookie = 'dauphin-byu-edu-9080-PORTAL-PSJSESSIONID=0;domain=hrms.byu.edu;path=/;expires='+expDate;
}

function byu__doRYLogin()
{
	byu__clearCookies();
	return true;
};

function byu__doRYLogout() {
	byu__clearCookies();
	var logoutString = "https://login.byu.edu/login/html/logout.html";
	if(byu__IsDefined('byu__targetLogoutURL'))
	{
		logoutString += "?" + byu__targetLogoutURL;
	}
	window.location = logoutString;
};

function byu__checkEnter(e) {
  if(!byu__isIE()) {
    if(e.which == 13) {
      return true;
    }
  }

  if(byu__isIE()) {
    if(event.keyCode == 13) {
      return true;
    }
  }
  return false;
};

function byu__setCookie(cValue, cookieID) {
	//alert('set cookie'+cookieID);
	var NameOfCookie = cookieID;
	//document.cookie = NameOfCookie + "=" + escape(cValue);
	document.cookie = NameOfCookie + "=" + escape(cValue)+"; path='/';";

};

function byu__unsetCookie(cookieID) {
	byu__setCookie (null, cookieID);
};

function byu__getCookie(cookieID) {
	var dc = document.cookie, prefix = cookieID + "=", begin = dc.indexOf("; " + prefix);
	if (begin == -1) { 
		begin = dc.indexOf(prefix); 
		if (begin != 0) 
			return null; 
	}
	else 
		begin += 2;
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) 
		end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
};


function byu__unhideElements()
{
	try
	{
		var ByuBarLogin = document.getElementById("ByuBarLogin");
		var ByuBarLoginForm = byu__getImmediateChildrenByTagName(ByuBarLogin, 'FORM');
		ByuBarLoginForm[0].style.display = 'block';
		document.forms.routeYLoginByuBar.USER.focus();
	}
	catch(error){}
	
	try
	{
		byu__generateLinksBar();
		var LinksBar = document.getElementById("LinksBar");
		LinksBar.className = 'show';
	}
	catch(error){}
};

function byu__closePageElements(evt) {
	try
	{
		byu__closeMenu();
	}
	catch(error){}
	try
	{
		byu__closeQuickFinder();
	}
	catch(error){}
};
document.onclick = byu__closePageElements;

