/* Element Functions
   ----------------------------------------------- */
function getElement(elementId) {
	return document.getElementById(elementId);
}
/* ----------------------------------------------- */

/* Position Functions
   ----------------------------------------------- */
function getPositionTop(elementId){
	var element = getElement(elementId);
	var position = 0;
	while (element) {
		position += element.offsetTop;
		element = element.offsetParent;
	}
	return position;
}

function getPositionLeft(elementId){
	var element = getElement(elementId);
	var position = 0;
	while (element) {
		position += element.offsetLeft;
		element = element.offsetParent;
	}
	return position;
}

function getPositionRight(elementId){
	var element = getElement(elementId);
	var position = 0;
	if (element != null)
		position = getPositionLeft(elementId) + element.offsetWidth;
	return position;
}

function getPositionBottom(elementId){
	var element = getElement(elementId);
	var position = 0;
	if (element != null)
		position = getPositionTop(elementId) + element.offsetHeight;
	return position;
}
/* ----------------------------------------------- */

/* Visuality Functions
   ----------------------------------------------- */
function showElement(elementId) {
	setClassOfElement(elementId, '');
}
function hideElement(elementId) {
	setClassOfElement(elementId, 'hidden');
}
function toggleElement(elementId) {
	if (getClassOfElement(elementId) == 'hidden')
		showElement(elementId)
	else
		hideElement(elementId);
}

function setClassOfElement(elementId, className) {
	var element = getElement(elementId);
	if (element != null)
		element.className = className;
}

function getClassOfElement(elementId) {
	var element = getElement(elementId);
	if (element == null)
		return null
	else
		return element.className;
}

function toggleClass(elementId, firstClassName, secondClassName) {
	if (getClassOfElement(elementId) == firstClassName)
		setClassOfElement(elementId, secondClassName)
	else
		setClassOfElement(elementId, firstClassName);
}
/* ----------------------------------------------- */

/* Disclaimer Functions
   ----------------------------------------------- */
var disclaimerUrl = "";
function checkDisclaimer(cookieName, url) {
	var cookieData = getCookie(cookieName);
	if (cookieData != 'yes') {
		showElement('disclaimer');
		disclaimerUrl = url;
		if (document.all) {
			document.all.pagefooterleft.style.marginTop=0;
			document.all.pagefooterright.style.marginTop=0;
			document.all.pagefooter.style.height = 155;
		}
		else {
			document.getElementById("pagefooterleft").style.marginTop = "0px";
			document.getElementById("pagefooterright").style.marginTop = "0px";
			document.getElementById("pagefooter").style.height = "155px";
		}
	}
	else {
		if (document.all) {
			document.all.pagefooterleft.style.marginTop=0;
			document.all.pagefooterright.style.marginTop=0;
			document.all.pagefooter.style.height = 45;
		}
		else {
			document.getElementById("pagefooterleft").style.marginTop = "0px";
			document.getElementById("pagefooterright").style.marginTop = "0px";
			document.getElementById("pagefooter").style.height = "45px";
		}
	}
}

/* ----------------------------------------------- */

/* Detail Page Functions
   ----------------------------------------------- */
function toggleList(elementId) {
	toggleClass(elementId, 'collapsible', 'collapsed');
	toggleClass(elementId + '_content', 'line', 'hidden');
	window.scrollTo(getPositionRight(elementId), getPositionBottom(elementId));
	window.scrollTo(getPositionLeft(elementId), getPositionTop(elementId));
}
/* ----------------------------------------------- */

/* Cookie Functions
   ----------------------------------------------- */
function getCookie(name) {
	name = name.replace(/-/g,'%2D');
	var start = document.cookie.indexOf(name + "=");
	var len = start + name.length + 1;
	if ((!start) && (name != document.cookie.substring(0, name.length)))
		return null;
	if (start == -1)
		return null;
	var end = document.cookie.indexOf( ";", len);
	if (end == -1)
		end = document.cookie.length;
	return unescape(document.cookie.substring(len, end));
}

function setCookie(name, value, expires, path, domain, secure) {
	var today = new Date();
	today.setTime(today.getTime());
	if (expires)
		expires = expires * 1000 * 60 * 60 * 24;
	var expires_date = new Date(today.getTime() + (expires));
	name = name.replace(/-/g,'%2D');
	document.cookie = name + "=" + escape(value) +
		((expires) ? ";expires=" + expires_date.toGMTString() : "") +
		((path)    ? ";path=" + path : "") +
		((domain)  ? ";domain=" + domain : "") +
		((secure)  ? ";secure" : "");
}

function deleteCookie(name, path, domain) {
	if (getCookie(name)) {
		name = name.replace(/-/g,'%2D');
		document.cookie = name + "=" +
			((path)   ? ";path=" + path : "") +
			((domain) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
}
/* ----------------------------------------------- */

var active_charts = new Array();

/* Chart Functions
   ----------------------------------------------- */
var unchecked = false;
function changeChartTyp(interval){
	if (interval == 'd1') {
		if(document.getElementsByName('blproduct')[0].checked && document.getElementsByName('blunderlying')[0].checked){
			if (selectedChart == "blunderlying") {
				document.getElementsByName('blproduct')[0].checked = false;
			}else if(selectedChart == "blproduct"){
				document.getElementsByName('blunderlying')[0].checked = false;
			}
		}
	} else {
		document.getElementsByName('blunderlying')[0].disabled = false;
		if (unchecked) {
			document.getElementsByName('blunderlying')[0].checked = true;
			unchecked = false;
		}
	}
}
function change_chart(interval, uniqueId, url) {
	if (url != null) {
		var element = getElement('chart_' + uniqueId);
		if (element != null) {
			active_charts['chart_' + uniqueId] = interval;
			element.src = url;
			if (browser.isIE || browser.isOpera) {
				var elements = document.getElementsByTagName('li');
				for (var i = 0; i < elements.length; i++) {
					if (elements[i].id.indexOf(uniqueId) != -1 && elements[i].id.indexOf('chart') != -1) {
						setClassOfElement(elements[i].id, (elements[i].id == 'chart_' + interval + '_' + uniqueId) ? 'active' : '');
					}
				}
			} else {
				var elements = document.getElementsByName('chart_link_' + uniqueId);
				for (var i = 0; i < elements.length; i++) {
					setClassOfElement(elements[i].id, (elements[i].id == 'chart_' + interval + '_' + uniqueId) ? 'active' : '');
				}
			}
		}
	}
}
/* ----------------------------------------------- */

/* Profile Search Functions
   ----------------------------------------------- */
function profile_search_starter() {
	if (profile_search_data['start'] != null) {
		if (profile_search_data['start'][1] != null) {
			for (var i = 0; i < profile_search_data['start'][1].length; i++) {
				setClassOfElement(profile_search_data['start'][1][i], 'clickable');
			}
		}
	}
}

function profile_search_builder(element) {
	if (element != null) {
		var elementId = element.id;
		
		// Is it clickable?
		if (getClassOfElement(elementId).indexOf('clickable') != -1) {
			var siblings = profile_search_get_siblings(elementId, 'start');
			
			// Disable all siblings of the current element.
			for (var i = 0; i < siblings.length; i++) {
				if (siblings[i] != elementId) {
					if ((getClassOfElement(siblings[i]).indexOf('clickable') != -1) || (getClassOfElement(siblings[i]).indexOf('active') != -1)) {
						setClassOfElement(siblings[i], 'disabled clickable');
					} else {
						setClassOfElement(siblings[i], 'disabled');
					}
				}
			}
			
			// Highlight the current element.
			setClassOfElement(elementId, 'active');
			
			// Build the selection path (including the current element / stopping at the current element).
			var path = profile_search_path_builder(elementId, 'start');
			
			// Remove all further selections.
			profile_search_cleaner(path);
			
			var countElement = getElement('profile_search_count');
			if (countElement != null) {
				var count = parseInt(profile_search_data[path][0]);
				if (count == 1) {
					countElement.innerHTML = profile_search_count1;
				} else {
					countElement.innerHTML = profile_search_count.replace(/@@COUNT@@/, profile_search_data[path][0]);
				}
			}
			
			// Activate possible next elements.
			if (profile_search_data[path][1] != null) {
				var possibleCount = 0;
				for (var i = 0; i < profile_search_data[path][1].length; i++) {
					elementId = path + '_' + profile_search_data[path][1][i];
					if (profile_search_data[elementId] != null) {
						possibleCount++;
						setClassOfElement(profile_search_data[path][1][i], 'clickable');
					}
				}
				if (possibleCount == 1) {
					for (var i = 0; i < profile_search_data[path][1].length; i++) {
						elementId = path + '_' + profile_search_data[path][1][i];
						if (profile_search_data[elementId] != null) {
							profile_search_builder(getElement(profile_search_data[path][1][i]));
							break;
						}
					}
				}
			}
		}
	}
}

function profile_search_get_siblings(selectedId, parentId) {
	if (profile_search_data[parentId] != null) {
		if (profile_search_data[parentId][1] != null) {
			for (var i = 0; i < profile_search_data[parentId][1].length; i++) {
				var elementId = (parentId == 'start' ? '' : parentId + '_') + profile_search_data[parentId][1][i];
				if (profile_search_data[parentId][1][i] == selectedId) {
					return profile_search_data[parentId][1];
				} else {
					var siblings = profile_search_get_siblings(selectedId, elementId);
					if (siblings != null) {
						return siblings;
					}
				}
			}
		}
	}
	return null;
}

function profile_search_path_builder(selectedId, parentId) {
	if (profile_search_data[parentId] != null) {
		if (profile_search_data[parentId][1] != null) {
			for (var i = 0; i < profile_search_data[parentId][1].length; i++) {
				var elementId = (parentId == 'start' ? '' : parentId + '_') + profile_search_data[parentId][1][i];
				if (profile_search_data[elementId] != null) {
					if (profile_search_data[parentId][1][i] == selectedId) {
						return elementId;
					} else if (getClassOfElement(profile_search_data[parentId][1][i]) == 'active') {
						return profile_search_path_builder(selectedId, elementId);
					}
				}
			}
		}
	}
	return parentId;
}

function profile_search_cleaner(elementId) {
	if (profile_search_data[elementId] != null) {
		if (profile_search_data[elementId][1] != null) {
			for (var i = 0; i < profile_search_data[elementId][1].length; i++) {
				setClassOfElement(profile_search_data[elementId][1][i], 'disabled');
				profile_search_cleaner(elementId + '_' + profile_search_data[elementId][1][i]);
			}
		}
	}
}

function profile_search_url_builder(path) {
	var elements = path.split(/_/);
	var querystring = '';
	for (var i = 0; i < elements.length; i++) {
		var elementId = ''
		for (var j = 0; j < i; j++) {
			elementId += elements[j] + '_';
		}
		elementId += elements[i];
		if (profile_search_data[elementId][3] != null) {
			querystring += '&' + profile_search_data[elementId][2] + '=' + profile_search_data[elementId][3];
		}
	}
	return profile_search_action + querystring;
}
/* ----------------------------------------------- */

/* Newsletter Functions
   ----------------------------------------------- */
function toggle_subscription(elementId, newsletterId, defaultState) {
	var element = getElement(elementId);
	if (element != null) {
		if (element.value != '') {
			element.value = '';
			element = getElement(elementId + '_caption');
			if (defaultState) {
				element.innerHTML = myaccount_newsletter_caption_unsubscribe;
			} else {
				element.innerHTML = myaccount_newsletter_caption_unsubscribed;
			}
		} else {
			element.value = newsletterId;
			element = getElement(elementId + '_caption');
			if (defaultState) {
				element.innerHTML = myaccount_newsletter_caption_subscribed;
			} else {
				element.innerHTML = myaccount_newsletter_caption_subscribe;
			}
		}
	}
}
/* ----------------------------------------------- */

/* Window Functions
   ----------------------------------------------- */
function open_window(url, name, width, height, center, scrollbar,resizable) {
	var features = 'location=no,menubar=no,status=no,toolbar=no';
	
	if (scrollbar == 0) {
		features += ',scrollbars=no'
	} else {
		features += ',scrollbars=yes'
	}
	if (resizable == 0) {
		features += ',resizable=no'
	} else {
		features += ',resizable=yes'
	}
	if (center == 1) {
		var left = (screen.width) ? (screen.width - width) / 2 : 100;
		var top = (screen.height) ? (screen.height - height) / 2 : 100;
		features += ',left=' + left + ',top=' + top;
	}
	features += ',width=' + width + ',height=' + height;
	window.open(url, name.replace(/ /, ""), features);
}
/* ----------------------------------------------- */

/* Other Functions
   ----------------------------------------------- */
// Returns true if the passed value is found in the
// array. Returns false if it is not.
Array.prototype.inArray = function (value) {
	for (var i = 0; i < this.length; i++) {
		// Matches identical (===), not just similar (==).
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};
/* ----------------------------------------------- */
