/* Apply a target to a link */

function applyLinkTarget(id, target){
	var oLink = getElementById(id);
	if (oLink) oLink.target = target;
}


/* External link functionality */

addLoadEvent('MasterNavigationBottom', function(){
	var links = getElementsByTagName(document, 'a');
	if (links){
		for (var i=0; i<links.length; i++){
			var link = links[i];

			if (elementHasClass(link, 'external')){
				link.target = "_blank";
				if (link.title == '') link.title = 'opens in a new window';
				else link.title += ' (opens in a new window)';
			}
		}
	}

	var forms = getElementsByTagName(document, 'form');
	if (forms){
		for (var i=0; i<forms.length; i++){
			var form = forms[i];

			if (elementHasClass(form, 'external')){
				form.target = "_blank";
				if (form.title == '') form.title = 'opens in a new window';
				else form.title += ' (opens in a new window)';
			}
		}
	}
});


/* Generic Show/Hide function
	e = object
	id = id of object
	action = show/hide
*/

function showHide(e, id, action){
	if (typeof(e) == "undefined")
		e = getElementById(id);

	if (e){
		if (action == "hide") applyClass(e, "hidden");
		else if (action == "show") removeClass(e, "hidden");
	}
}


/* Show/Hide items by class */

function showHideByClassInContainer(container, tagName, className, action){
	var elements = getElementsByClassName(getElementById(container), tagName, className);

	if (elements){
		for (var i=0; i<elements.length; i++){
			showHide(elements[i], "", action);
		}
	}
}


/* Open Window functions */

function openWindow(url, windowName, options){
	var default_args = {
		'height':				500,
		'left':					20,
		'top':					20,
		'width':				500,
		'directories':	"no",
		'location':			"no",
		'menubar':			"yes",
		'resizable':		"yes",
		'scrollbars':		"yes",
		'status':				"no",
		'titlebar':			"yes",
		'toolbar':			"no"
	}

	var sOptions = "";
	for (var index in default_args){
		if (typeof options[index] == 'undefined') options[index] = default_args[index];
		sOptions = sOptions + index + "=" + options[index] + ",";
	}
	sOptions = sOptions.substr(0, sOptions.length - 1);

	newWindow = window.open(url, windowName, sOptions);
	if (newWindow){
		if (window.focus) newWindow.focus();
		return false;
	}else{
		alert('Your browser does not allow pop up windows! Please change your browser settings.');
		return true;
	}
}

/* Need to confirm use of some of the following... some aren't in code but could be in the database */
// Not seen in code
function print(theURL, theWindow){
	var options = {
		'height':			500,
		'width':			690,
		'resizable':	"no"
	};
	openWindow(theURL, theWindow, options);
}

// Not seen in code
function openPDF(theURL,theWindow){
	var options = {
		'height':	500,
		'width':	690
	};
	openWindow(theURL, theWindow, options);
}

function openHelp(theURL, theWindow){
	var options = {
		'height':			250,
		'width':			260,
		'menubar':		"no",
		'scrollbars':	"no"
	};
	openWindow(theURL, theWindow, options);
}

// Not seen in code
function openCustomerScreen(theURL, theWindow){
	var options = {
		'height':			250,
		'width':			260,
		'menubar':		"no",
		'scrollbars':	"no"
	};
	openWindow(theURL, theWindow, options);
}

function openQTR(theURL, theWindow){
	var options = {
		'height':		550,
		'width':		750,
		'menubar':	"no"
	};
	openWindow(theURL, theWindow, options);
}

function openHelpFullScreen(theURL, theWindow){
	var options = {
		'height':		600,
		'width':		800,
		'menubar':	"no"
	};
	openWindow(theURL, theWindow, options);
}

// Legacy: necessary until *all* labels that use it have been updated
function disclaimerOpen(pageName){
	openDisclaimer(pageName);
}

function openDisclaimer(pageName){
	pageName = pageName.toLowerCase();
	var height, width;
	var options = {
		'menubar':	"no"
	};
	var url = "../util/disclaimer.aspx";

	switch (pageName){
		case "privacypolicy":
			url = url + "?type=privacy";
			options['height'] = 400;
			options['width'] = 530;
			break;
		case "termsofuse":
			url = url + "?type=terms";
			options['height'] = 400;
			options['width'] = 500;
			break;
		default:
			url = url + "?type=copyright";
			options['height'] = 300;
			options['width'] = 400;
			break;
	}
	if (typeof(LanguageId) != 'undefined')
		url = url + "&LanguageId=" + LanguageId;

	return openWindow(url, 'msDisclaimerWindow', options);
}


/* Titlebar functions */

/* STILL NEED TO CHECK THESE
function toolsTitleBarChangeCurrency(selectedcurrencyobject){
	var SelectedCurrencyInput = getElementById('<%= SelectedCurrency.ClientID %>');
	if (SelectedCurrencyInput != null) SelectedCurrencyInput.value = selectedcurrencyobject.value;
	__doPostBack('ToolsTitleBarChangeCurrency', '');
	return false;
}

function toolsTitleBarChangeLanguage(selectedlanguageobject){
	var SelectedLanguageInput = document.getElementById('ctl00_ContentPlaceHolder1_aFundQuickrankControl_SelectedLanguage');
	if (SelectedLanguageInput != null){
		SelectedLanguageInput.value = selectedlanguageobject.value;
	}

	var SearchKey = document.getElementById('ctl00_ContentPlaceHolder1_aFundQuickrankControl_SearchKey');
	if (SearchKey != null && SearchKey.value == DefaultSearchText) SearchKey.value = '';
	__doPostBack('ToolsTitleBarChangeLanguage', '');
	return false;
}
*/


/* Utility Functions */

function trim(text){
	//trim whitespace from a string
	text = text.replace(/^\s+/g,''); //leading space
	return text.replace(/\s+$/g,''); //trailing space
}

function elementHasClass(element, className){
	//return true if the element has the given class name
	className = className.replace(/\-/g, '\\-');
	var re = new RegExp('(^|\\s)' + className + '(\\s|$)');
	return re.test(element.className);
}

function applyClass(element, className){
	//add a class to an element
	if (!elementHasClass(element, className)) element.className += ' ' + className;
}

function removeClass(element, className){
	//remove a class from an element
	className = className.replace(/\-/g, '\\-');
	var re = new RegExp('(^|\\s)' + className + '(\\s|$)');
	element.className = element.className.replace(re,'');
}

function getElementById(id){
	//get element with a given id
	return (document.all)? document.all[id] : document.getElementById(id);
}

function getElementsByTagName(container, tagName){
	//get all html nodes of a given type within a given container
	if (container) return container.getElementsByTagName(tagName);
	else return new Array();
}

function getElementsByClassName(container, tagName, className){
	//get all html nodes of a given type and class within a given container
	var elements = getElementsByTagName(container, tagName);
	var result = new Array();
	for(var i=0;i < elements.length;i++){
		var e = elements[i];
		if (elementHasClass(e,className)) result.push(e);
	}
	return result;   
}

function addEvent(element, event, func){
	//add an event to the specified element, preserving any existing events that are attached
	if (!element) return;
	if (element.addEventListener){
		element.addEventListener(event, func, false);
		return true;  
	}else{
		var oldFn = element['on' + event];
		var fn;
		if (oldFn){
			fn = function(){
				return oldFn();
				return func();
			}
		}else{
			fn = func;
		}
		if (element.attachEvent) return element.attachEvent('on' + event, fn);
		else element['on' + event] = fn;
	}
}

function addLoadEvent(id,func){
	//add an onload event which will fire once the given id is located within the document
	//this is more reliable than window.onload and will happen exactly when needed

	function loadEvent(){
		var element = getElementById(id);
		if (element) func(); else window.setTimeout(loadEvent,2000);
	}

	loadEvent();

}

function createElement(tagName){
	//create an html node of the specified type
	return document.createElement(tagName);
}

function createTextNode(text){
	//create an html text node
	return document.createTextNode(text);
}

function replaceElement(element,replacement){
	//replace an html node with the specified alternative
	if (element) element.parentNode.replaceChild(replacement,element);
}

function insertElement(parent,child){
	//insert an html node within the specified parent, after the last child
	if (parent) parent.appendChild(child);
}

function removeElement(element) {
	//remove the specified node from the dom tree
	if (element && element.parentNode) element.parentNode.removeChild(element);
}

function insertElementBefore(element,before) {
	//insert an html node before the specified element
	if (before && before.parentNode) before.parentNode.insertBefore(element,before);
}

function insertElementAfter(element,after) {
	//insert an html node after the specified element
	if (after && after.parentNode) {
		if (after.nextSibling) insertElementBefore(element,after.nextSibling);
		else insertElement(after.parentNode,element);
	}
}

function setElementPosition(element,x,y) {
	//set the absolute (document relative) position of an element in pixels
	if (element.offsetParent) { // using ie's relative positioning model
		x -= getOffsetLeft(element.parentNode);
		y -= getOffsetTop(element.parentNode);
	}
	element.style.left = x + 'px';
	element.style.top = y + 'px';
}

function getOffsetTop(element) {
	//get the absolute (document relative) position of an element in pixels
	var o = element.offsetTop;
	while (element.offsetParent) {
		element = element.offsetParent
		o += element.offsetTop;
	}
	return o;
}

function getOffsetLeft(element) {
	//get the absolute (document relative) position of an element in pixels
	var o = element.offsetLeft;
	while (element.offsetParent) {
		element = element.offsetParent
		o += element.offsetLeft;
	}
	return o;
}

// NB: In the following function cssprop must be the javascript syntax rather than css, e.g. backgroundColor not background-color
function getStyle(el, cssprop){
	if (el.currentStyle) //IE
		return el.currentStyle[cssprop];
	else if (document.defaultView && document.defaultView.getComputedStyle) //Firefox etc
		return document.defaultView.getComputedStyle(el, "")[cssprop];
	else //try and get inline style
		return el.style[cssprop];
}

function getQueryParams(query) {
	//decode query parameters into an associative array
	var params = new Object();
	if (!query) return params;
	var start = query.indexOf('?');
	if (start > -1) query = query.substr(start + 1);
	var paramArray = query.split("&");
	for (var i in paramArray) {
		var param = paramArray[i];
		var splitPos = param.indexOf("=");
		var name = unescape(param.substring(0,splitPos));
		var value = unescape(param.substring(splitPos+1));
		params[name] = value;
	}
	return params;
}