﻿function ClearSearchBox(id, value) {
	var i = document.getElementById(id);
	if (i.value == value) {
		i.value = "";
	}
}

function sitesRedirect(globalSites) {
	if (globalSites.value != "") {
		window.open(globalSites.value);
	}
}

function onSelectToolChange(globalSites) {
	if (globalSites.value != "") {
		window.location = globalSites.value;
	}
}

function getIndexGraph(index) {
	var indexImg = document.getElementById("indexGraph");
	
}

function open_glossary(aURL, aWinName) {
	window.location = aURL;
}

function textBoxOnFocus(elementRef, def) {
	if (elementRef.value == def) {
		elementRef.value = "";
	}
	elementRef.select();
}

function textBoxOnBlur(elementRef, def) {
	if (elementRef.value == "") {
		elementRef.value = def;
	}
}

function MM_findObj(n, d) { //v4.01
	var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
		d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
	}
	if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
	for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
	if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
	var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
		if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}

function MM_swapImgRestore() { //v3.0
	var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}


/*
File: Dictionary.js
Version: 1.0
Last modified: 09.17.2002
Author: Alexandar Minkovsky (a_minkovsky@hotmail.com ; URL: http://www24.brinkster.com/alekz)
Copyright: Left
*/
function Dictionary() {
	//Properties
	//~Public
	this.count = 0;
	//~Private
	this.Obj = new Object();
	//Methods
	//~Public
	this.exists = Dictionary_exists;
	this.add = Dictionary_add;
	this.remove = Dictionary_remove;
	this.removeAll = Dictionary_removeAll;
	this.values = Dictionary_values;
	this.keys = Dictionary_keys;
	this.items = Dictionary_items;
	this.getVal = Dictionary_getVal;
	this.setVal = Dictionary_setVal;
	this.setKey = Dictionary_setKey;
}

function Dictionary_exists(sKey) {
	return (this.Obj[sKey]) ? true : false;
}

function Dictionary_add(sKey, aVal) {
	var K = String(sKey);
	if (this.exists(K)) return false;
	this.Obj[K] = aVal;
	this.count++;
	return true;
}

function Dictionary_remove(sKey) {
	var K = String(sKey);
	if (!this.exists(K)) return false;
	delete this.Obj[K];
	this.count--;
	return true;
}

function Dictionary_removeAll() {
	for (var key in this.Obj) delete this.Obj[key];
	this.count = 0;
}

function Dictionary_values() {
	var Arr = new Array();
	for (var key in this.Obj) Arr[Arr.length] = this.Obj[key];
	return Arr;
}

function Dictionary_keys() {
	var Arr = new Array();
	for (var key in this.Obj) Arr[Arr.length] = key;
	return Arr;
}

function Dictionary_items() {
	var Arr = new Array();
	for (var key in this.Obj) {
		var A = new Array(key, this.Obj[key]);
		Arr[Arr.length] = A;
	}
	return Arr;
}

function Dictionary_getVal(sKey) {
	var K = String(sKey);
	return this.Obj[K];
}

function Dictionary_setVal(sKey, aVal) {
	var K = String(sKey);
	if (this.exists(K))
		this.Obj[K] = aVal;
	else
		this.add(K, aVal);
}

function Dictionary_setKey(sKey, sNewKey) {
	var K = String(sKey);
	var Nk = String(sNewKey);
	if (this.exists(K)) {
		if (!this.exists(Nk)) {
			this.add(Nk, this.getVal(K));
			this.remove(K);
		}
	} else if (!this.exists(Nk)) this.add(Nk, null);
}

function addHolding(idName, valueName, idTypeWithId, valueTypeWithId) {
	try {
		var watermark_SecName = $find(currentIdName);
		var hf_TypeWithId = document.getElementById(currentIdTypeWithId);

		if (hf_TypeWithId != null) {
			hf_TypeWithId.value = valueTypeWithId;
			if (watermark_SecName != null) {
				watermark_SecName.set_Text(valueName);
			}
			onPopupValueReturned(currentIdTypeWithId);
		}
	}
	catch (e)
	{}
}

var originaliframeHeight;

function resizeSecurityLookUpIFrame() {
	if (originaliframeHeight) {
		//restore to original height
		var currentlocation = window.location;
		var parenturl = window.parent.location;
		if (currentlocation != parenturl) {//the page is running within a frame, we need to do some position changes to fit into iframe
			var iframechart = window.parent.document.getElementById("iframechart");
			if (iframechart) {
				iframechart.height = originaliframeHeight;
				originaliframeHeight = null;
			}
		}
	}
}

//be used to init LookUpPopup when page load to make showLookUpPopup quick
function initLookUpPopup(source, universeId, moduleId) {
	var popupframe = getElementById("popup");
	if (popupframe == null) popupframe = getElementById(popupId);
	var lookUpUrl;
	lookUpUrl = AppPath + "/" + UrlKey+ "/securitylookup/default.aspx?source=" + source;
	if (universeId != null) lookUpUrl += "&UniverseId=" + universeId;
	if (moduleId != null) lookUpUrl += "&ModuleId=" + moduleId;
	if (LanguageId != null) lookUpUrl += "&LanguageId=" + LanguageId;

	var securitySearch = getElementById("securitySearch");
	if (securitySearch.src.substring(securitySearch.src.indexOf("?")) != lookUpUrl.substring(lookUpUrl.indexOf("?"))) {
		securitySearch.src = lookUpUrl;
	}
	popupframe.style.display = 'none';
}

//needs to be deleted after it is running on all tools
var currentIdName;
var currentIdTypeWithId;

function showLookUpPopup(idImage, idName, idTypeWithId, source, universeId, moduleId) {
	currentIdName = idName;
	currentIdTypeWithId = idTypeWithId;
	
	var i = getAbsolutePosition(getElementById(idImage));
	var w = getWindowSize();
	var popupframe = getElementById("popup");
	if (popupframe == null) popupframe = getElementById(popupId);

	var lookUpUrl;
	lookUpUrl = AppPath + "/" + UrlKey+ "/securitylookup/default.aspx?source=" + source;
	if (universeId != null) lookUpUrl += "&UniverseId=" + universeId;
	if (moduleId != null) lookUpUrl += "&ModuleId=" + moduleId;
	if (LanguageId != null) lookUpUrl += "&LanguageId=" + LanguageId;

	var securitySearch = getElementById("securitySearch");
	if (securitySearch.src.substring(securitySearch.src.indexOf("?")) != lookUpUrl.substring(lookUpUrl.indexOf("?"))) {
		securitySearch.src = lookUpUrl;
	}
	var securitySearchContainerWidth = securitySearch.parentNode.style.width;
	securitySearchContainerWidth = parseFloat(trim(securitySearchContainerWidth.replace(/px/i, '')));

	var left = i.x + 5, top = i.y + 5;
	if (left + securitySearchContainerWidth >= w.w) left -= securitySearchContainerWidth;

	popupframe.style.left = left + "px";
	popupframe.style.top = top + "px";
	popupframe.style.display = 'block';
}

//new version with better position support
function showLookUpPopupV2(idImage, idName, idTypeWithId, source, universeId, moduleId) {
	currentIdName = idName;
	currentIdTypeWithId = idTypeWithId;

	var position = jQuery("#" + idImage).offset();
	var popupframe = document.getElementById("popup");
	if (popupframe == null) popupframe = document.getElementById(popupId);

	var currentlocation = window.location;
	var parenturl = window.parent.location;

	if (currentlocation != parenturl) {		//the page is running within a frame, we need to do some position changes to fit into iframe
		var frameWidth = jQuery(popupframe).width();
		var iframeWidth = jQuery('body').width();
		var iframechart = window.parent.document.getElementById("iframechart");

		if (frameWidth + position.left > iframeWidth - 15) {
			position.left = iframeWidth - frameWidth - 15;
		}

		if (iframechart) {
			var frameHeight = jQuery(popupframe).height();
			var iframeHeight = parseInt(iframechart.height.replace("px", ""));

			if (frameHeight + position.top > iframeHeight - 15) {
				originaliframeHeight = iframeHeight;
				iframechart.height = frameHeight + position.top + 15;
			}
		}
	}
	
	jQuery(popupframe).css({
		position: "absolute",
		marginLeft: 0, marginTop: 0,
		top: position.top + 8 + "px",
		left: position.left + 8 + "px",
		display: "block"
	});

	var lookUpUrl = AppPath + "/" + UrlKey + "/securitylookup/default.aspx?source=" + source + "&idName=" + idName + "&idTypeWithId=" + idTypeWithId;
	if (universeId != null) lookUpUrl += "&UniverseId=" + universeId;
	if (moduleId != null) lookUpUrl += "&ModuleId=" + moduleId;
	if (LanguageId != null) lookUpUrl += "&LanguageId=" + LanguageId;

	var securitySearch = document.getElementById("securitySearch");
	securitySearch.src = "";
	securitySearch.src = lookUpUrl;
}

function getAbsolutePosition(el) {
	var iTop = 0, iLeft = 0;

	while (el != null) {
		iTop += el.offsetTop;
		iLeft += el.offsetLeft;
		el = el.offsetParent;
	}
	return { x: iLeft, y: iTop };
}

function getWindowSize(){
	var iWidth = 0, iHeight = 0;
	if (typeof(window.innerWidth) == 'number'){
		//Non-IE
		iWidth = window.innerWidth;
		iHeight = window.innerHeight;
	}else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)){
		//IE 6+ in 'standards compliant mode'
		iWidth = document.documentElement.clientWidth;
		iHeight = document.documentElement.clientHeight;
	}else if (document.body && (document.body.clientWidth || document.body.clientHeight)){
		//IE 4 compatible
		iWidth = document.body.clientWidth;
		iHeight = document.body.clientHeight;
	}
	return { w: iWidth, h: iHeight };
}

//Layer function for Quickrank
var IE4 = (document.all && !document.getElementById) ? true : false;
var W3C = (document.getElementById) ? true : false;

function getObj(obj) {
	if (IE4) { var o = document.all[obj] }
	else if (W3C) { var o = document.getElementById(obj) }
	return o;
}

function showHideObj(obj, action) {
	var o = getObj(obj);
	o.style.visibility = action;
}

function delay(obj, action) {
	var milliseconds = 1000; //1 sec
	setTimeout(function() { showHideObj(obj,action) }, milliseconds);
}

// Taken from urchin.js as is used in various places
function _uES(s) {
	if (typeof (encodeURIComponent) == 'function') {
		return encodeURIComponent(s);
	} else {
		return escape(s);
	}
}
