/********************************************************
This script will run a function after the DOM has been
Loaded in the browsers that support it. Else functions
won't run until document has completely loaded
(i.e. objects, images, content, ect)
*********************************************************/
//clear case test
/********************************************************
Used to make sure function General only runs first time.
IE 5+ PC, Moz1+, and Opera will run after DOM is Parsed
all other browsers will run after the Document is
completely loaded.
********************************************************/
var g_blnGeneralRun=true;
var g_blnGo=true;

/********************************************************
Statement checks if Browser is Opera/IE or Geck/other
DOM compliant Browser. If it's Opera/IE then it will set
the proprietary event listener onreadystatechange to run
the function opieLoad. Else if document a DOM Complient
browser it will set the proprietary Gecko event listener
DOMContentLoaded to run function general. Finally the
script will set window.onload to run function general as a
fallback in case previous proprietary methods fail to run.
**********************************************************/
if (document.attachEvent && document.readyState) {
	document.onreadystatechange=opieLoad;
} else if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", general, false);
}
window.onload=onloadGeneral;

/********************************************************
opieLoad will only run in Browsers that support IE PC's
proprietary methods attachEvent and readyState, and if
document.readyState = "interactive".
********************************************************/
function opieLoad(e) {
	if (!e) e=window.event;
	switch (document.readyState) {
		case "interactive":general();break;
	}
}
function onloadGeneral(e) {
	if (!e) e = window.event;
	general();
	/*****************************************
	add any functions that have to run after
	entire document has loaded bellow here.
	*****************************************/

}

//This is where all functions that need to run after
//document has loaded are to be added.
function general(e) {
	if (g_blnGeneralRun) {
		g_blnGeneralRun=false;
		getAs();
		setTabs("nm");
		//add any addtional functions that need to run
		//after document has loaded below here...

		/* if you need to have function run after dom
		has been parsed but function only exists in
		an another js file run it here as shown bellow. */
		if (window.details) {
			details();
		}
		if (window.ie) {
			ie();
		}
	}
}


// Generic popup window function
function popUp(url,name,features) {
	var popUp = window.open(url,name,features);
	popUp.focus();
	return false;
}

// PDF popup
var pdfPluginInstalled = (detectAcrobatPlugin() || detectAcrobatActiveX());
function pdfPopup(url,query) {
	if (pdfPluginInstalled) {
		return popUp('/literature/viewPdf.htm?' + query,'pdfPopup','width=600,height=580,resizable=yes,scrollbars=yes');
	}
	else {
		document.location.href = url;
	}
}


// movie popup
function moviePopup(url) {
	return popUp(url,'moviePopup','width=400,height=400,menubar=yes,resizable=yes,scrollbars=yes,status=yes,toolbar=yes');
}

// movie2 popup
function movie2Popup(url) {
	return popUp(url,'movie2Popup','width=700,height=480,menubar=yes,resizable=yes,scrollbars=yes,status=yes,toolbar=yes');
}

// movie3 popup
function movie3Popup(url) {
	return popUp(url,'movie3Popup','width=740,height=500,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no');
}

// tour popup
function tourPopup(url) {
	return popUp(url,'tourPopup','width=740,height=600,menubar=no,location=no,resizable=yes,scrollbars=yes,status=no,toolbar=no');
}


function directBack(url) {
	var sourceWin = window.open(url, "af_source");
	sourceWin.focus();
	return false;
}

function directBackToTop(url) {
	var sourceWin = window.open(url, "af_source");
	sourceWin.focus();
	return false;
}

// Detect Acrobat Plugin (Moz, Opera, Safari, etc.)
function detectAcrobatPlugin() {
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		for (i = 0; i < navigator.plugins.length; i++) {
			var plugin = navigator.plugins[i];
			if (plugin.name.indexOf("Adobe Acrobat") > -1) return true;
		}
	}
}
// Detect Acrobat ActiveX control (IE)
function detectAcrobatActiveX() {
	acro4 = false;
	acro5 = false;
	document.write('<scr' + 'ipt language=VBScript>\n on error resume next \n acro5 = IsObject(CreateObject(\"PDF.PdfCtrl.5\"))</scr' + 'ipt>\n');
	document.write('<scr' + 'ipt language=VBScript>\n on error resume next \n acro4 = IsObject(CreateObject(\"PDF.PdfCtrl.1\"))</scr' + 'ipt>\n');
	if (acro5 || acro4) return true;
	else return false;
}
// function used to get specified parent. requires obj and tagname of parent you wish to stop at.
// example getParent(document.getElementById('foo'),"div")
function getParent(objT,stopAt) {
	while (objT.parentNode.tagName.toLowerCase().indexOf(stopAt) == -1) {
		if (objT.tagName.match(/^(html:)?body$/gi) != null) {
			return objT
		}
		if (objT.parentNode) {
			objT=objT.parentNode;
		}
	}
	objT=objT.parentNode;
	return objT;
}
//used to preload image
var clickTrack = new Array();
var blnGo = true;
function getAs() {
	var parent;
	if (document.getElementsByTagName) {
		var a = document.getElementsByTagName('a');
		duffs(a,'onclick');
}
//sets either an a's onclick attribute, or relation attribute
//based on if the argument prop == 'rel' or 'onclick'.
function sp(objT,prop,intI) {
	switch(prop) {
		//Modify
		case "onclick":objT.onclick=function() { return goTo(this.href,this.rel);  };break;
		case "rel":if (objT.rel=="wasClicked") { objT.rel=intI; };break;
	}
}
function duffs(objT,prop) {
		var j=0;
		var i=objT.length;
		var n=i % 8;
		if (n>0) {
			do {
				sp(objT[j],prop,j);j++;
			} while (--n); // n must be greater than 0 here
		}
		n = parseInt(i / 8);
		if (n>0) {
			do {
				sp(objT[j],prop,j);j++;
				sp(objT[j],prop,j);j++;
				sp(objT[j],prop,j);j++;
				sp(objT[j],prop,j);j++;
				sp(objT[j],prop,j);j++;
				sp(objT[j],prop,j);j++;
				sp(objT[j],prop,j);j++;
				sp(objT[j],prop,j);j++;
			} while (--n);
		}
	}
}
function goTo(strHref,strRel) {
	if (!g_blnGo) {
		g_blnGo=true;
		return false;
	}
	var strUrlarray;
	switch(strRel) {
		case "pdf":
			strUrlarray = strHref.split('?');
			pdfPopup(strHref,strUrlarray[1]);
			return false;
			break;
		case "movie":moviePopup(strHref);return false;break;			
		case "movie2":movie2Popup(strHref);return false;break;
		case "movie3":movie3Popup(strHref);return false;break;
		case "movie4":movie4Popup(strHref);return false;break;
		case "movie5":movie5Popup(strHref);return false;break;
		case "movie6":movie6Popup(strHref);return false;break;
		case "movie7":movie7Popup(strHref);return false;break;
	}
	return true;
}

function setTabs() {
	var args = setTabs.arguments;
	var r = args.length;
	var tmpObj,objAs,objLI;
	for (var i=r-1;i>=0;i--) {
		tmpObj=document.getElementById(args[i]);
		if (tmpObj) { 
			objAs=tmpObj.getElementsByTagName("a");
			for (var j=objAs.length-1;j>=0;j--) {
				objLI = getParent(objAs[j],'li');
				objAs[j].onfocus=function() { show(this);showHideSelectBoxes('hide'); };
				if (!document.addEventListener && document.attachEvent) {
					objLI.onmouseover=function() { show(this.firstChild);showHideSelectBoxes('hide'); };
					objLI.onmouseout=function() { hide();showHideSelectBoxes('show'); };
				}
			}
			objAs[objAs.length-1].onblur=function() { hide();showHideSelectBoxes('show'); };
		}
	}

}
function showHideSelectBoxes(strState) {
	if (window.fixSelectBoxes) {
		fixSelectBoxes(strState);
	}
}
function show(objA) {
	var pLi=getParent(objA,"li");
	var cUl=pLi.getElementsByTagName("ul");
	var pUl=getParent(pLi,"ul");
	if (cUl[0] && pUl.id=="nm") {
		hide();
		//alert(cUl[0].style.left);
		switch(cUl[0].style.left) {
			case "auto":cUl[0].style.left="-99999px";break;
			default : cUl[0].style.left="auto";
		}
	}
}
function hide() {
	var tmpObj=document.getElementById("nm").getElementsByTagName("ul");
	for (var i=tmpObj.length-1;i>=0;i--) {
		tmpObj[i].style.left="-99999px";
	}
}

// Determines if current URL is to the index page
// Note:  This was written for checking the fund view preference but is no longer used.
function isIndexPage() {
	// Note:  I didn't use the Location object to do this
	// because the Location object is not W3C compliant

	var INDEX_PAGE = "/index.htm";

	var url;
	var index = document.URL.indexOf("?");
	if ( index == -1)
		url = document.URL;
	else
		url = document.URL.substring(0, index);

	var pageName = url.substr(url.lastIndexOf("/"));
	return INDEX_PAGE == pageName;
}

/********************************************************************
 * String utilities
 ********************************************************************/
// trim whitespace from left and right of string
function trim(aString) {
    return aString.replace(/^\s*|\s*$/g,'');
}

// trim whitespace from left of string
function ltrim(aString) {
    return aString.replace(/^\s*/g,'');
}

// trim whitespace from right of string
function rtrim(aString) {
    return aString.replace(/\s*$/g,'');
}

/********************************************************************
 * Cookie utilities -- Functions to get, set, and delete cookies
 ********************************************************************/
// Get cookie value
function getCookie(cookieName) {
	var cookies = document.cookie.split(";");
	for (var i=0; i<cookies.length; i++) {
		// get cookie name
		var index = cookies[i].indexOf("=");
		if ((index != -1) && (index + 1 < cookies[i].length)) {
			var name = trim(cookies[i].substring(0,index));
			if (name == cookieName) {
				// return cookie value (after cookie name + "=")
				var value = trim(cookies[i].substr(index+1));
				return value;
			}
		}
	}

	// if cookie not found, return null
	return null;
}

// Set a session cookie.  This cookie will expire when the browser window is closed.
function setSessionCookie(cookieName, cookieValue) {
	document.cookie = cookieName + "=" + cookieValue + "; domain=" + DOMAIN + "; path=/;";
}

// Set a persistent cookie.  This cookie will expire in 5 years.
function setPersistentCookie(cookieName, cookieValue) {
	var date = new Date();
	date.setFullYear(date.getFullYear() + 5);

	document.cookie = cookieName + "=" + cookieValue + "; domain=" + DOMAIN + "; path=/; expires=" + date.toGMTString();
}

// Delete a cookie.  This cookie will expire right away.
function deleteCookie(cookieName) {
	var date = new Date();
	date.setFullYear(1999);	// pick any year in the past

	document.cookie = cookieName + "=invalid; domain=" + DOMAIN + "; path=/; expires=" + date.toGMTString();
}

/********************************************************************
 * Personalization utilities
 ********************************************************************/
 // Object to store user's personal information
function personalInfo() {
	this.isGenericUser = "true";
	this.firm = "";
	this.firstName = "";
	this.midInit= "";
	this.lastName = "";
	this.showOnlinePanel="true";
	this.showEmailUpdates="true";
	this.loaded=false;
}

function loadPersonalInfo() {
	if (! personalInfo.loaded)
		parseCookieValue(COOKIE_PERSONAL, COOKIE_DELIMITER, "personalInfo");

	personalInfo.loaded = true;
}

function getIdentifier() {
	var identifier = "";
	if (personalInfo.isGenericUser == "false") {
		var fullName = personalInfo.firstName;
		if (personalInfo.midInit != "") {
			if (fullName != "")
				fullName += " ";
			fullName += personalInfo.midInit + ".";
		}
		if (personalInfo.lastName != "") {
			if (fullName != "")
				fullName += " ";
			fullName += personalInfo.lastName;
		}
		if (fullName != "")
			identifier = fullName + ", ";
	}
	identifier += personalInfo.firm;
	return identifier;
}

function isBobLoggedIn() {
	this.isLoggedIn = "false";
	var visionsessionidCookie = getCookie("visionsessionid");
	var activevisionsessionCookie = getCookie("activevisionsession");
	if (visionsessionidCookie != null && activevisionsessionCookie != null) {
		isLoggedIn = "true";
	}

	return isLoggedIn;
}

// Parse a cookie containing delimited name/value pairs.
// Stores the values in the specified object name.
function parseCookieValue(cookieName, delimiter, objectName) {
	var cookieValue = getCookie(cookieName);
	if (cookieValue != null) {
		var values = cookieValue.split(delimiter);
		var name;
		var value;
		var index;
		for (var i=0; i<values.length; i++) {
			index = values[i].indexOf("=");
			if (index != -1) {
				name = trim(values[i].substring(0,index));
				if (name.length > 0) {
					if (index < values[i].length -1) {
						value = trim(values[i].substr(index+1));
						if (value == "null")
							value = "";
					}
					else {
						value = "";
					}
					eval(objectName + "." + name + " = '" + value + "';");
				}
			}
		}
	}
}

