if (window.navigator.userAgent.indexOf("MSIE") <= 0) {
	HTMLElement.prototype.__defineGetter__("innerText", function () { return(this.textContent); });
	HTMLElement.prototype.__defineSetter__("innerText", function (txt) { this.textContent = txt; });
}

var derivat_tr, derivat_tr_color, isin, etrade_market, shortinfo_orderid, nordnet_marketid, shortinfo_issuer, shortinfo_shortname, shortinfo_marketplace, shortinfo_countryid;

function menuoption_over(obj) {
	obj.style.backgroundColor='#FFFFFF';
	obj.style.color='#FF6600';
}
function menuoption_out(obj) {
	obj.style.backgroundColor='#F2F2F2';
	obj.style.color='#365772';
}

function existInWatchlist(localIsin, localProductType, localCountryId){
	watchlistSelectElement = document.getElementById('watchlist_select');
	exists = 0;
	for(i=1; i < watchlistSelectElement.options.length; i++){
		watchlistOption = watchlistSelectElement.options[i];
		watchlistOptionValues = watchlistOption.value.split("!");
		trailIsin = watchlistOptionValues[0];
		trailProductType = watchlistOptionValues[1];
		trailCountryId = watchlistOptionValues[2];
		
		if(trailIsin == localIsin){
			if(trailProductType == localProductType){
				if(trailCountryId == localCountryId){
					exists = 1;
					break;
				}
			}
		}
	}
	return exists;
}

/*
	removed
		derivatid
		derivativetype_long
*/
function derivatmenu(listnr, derivatname, tr_color, isin, market, orderid, derivativetype, issuer, countryid, subtype) {
	document.getElementById("derivat_menu_name").innerText = derivatname;

	nordnet_marketid = getPartnerMarketId(market, "NORDNET")
	shortinfo_orderid = getNordnetOrderId(orderid, derivatname, "DERIVATIVE", derivativetype);

	derivat_tr = 'tr'+listnr;
	derivat_tr_color = tr_color;
	shortinfo_isin = isin;
	shortinfo_derivativetype = derivativetype;
	shortinfo_issuer = issuer;
	shortinfo_shortname = derivatname;
	shortinfo_marketplace = market;
	shortinfo_countryid = countryid;
	shortinfo_subtype = subtype;
	var xPos = findPosX(document.getElementById('arrow_'+listnr));
	var yPos = findPosY(document.getElementById('arrow_'+listnr));
	
	if(derivativetype != "W"){
		var openProspectElement = document.getElementById("openprospect");
		openProspectElement.style.display = 'none';
	}
	else{
		hasProspectUrl = hasProspect(shortinfo_isin, shortinfo_issuer, shortinfo_shortname, shortinfo_marketplace, shortinfo_countryid);
		if(!hasProspectUrl){
			var openProspectElement = document.getElementById("openprospect");
			openProspectElement.style.display = 'none';
		}
	}
	
	if(user_logged_in){
		// Watchlist code
		// - Change the visibility of the watchlist menu item depending on if it already exist in the watchlist
		if(existInWatchlist(shortinfo_isin, 'warrant', shortinfo_countryid) || existInWatchlist(shortinfo_isin, 'option', shortinfo_countryid) || existInWatchlist(shortinfo_isin, 'term', shortinfo_countryid)){
			divWatchlistRemove = document.getElementById('div_derivative_watchlist_remove');
			divWatchlistRemove.style.display = 'inline';
			hideElement('div_derivative_watchlist_add');
		}
		else{
			hideElement('div_derivative_watchlist_remove');
			divWatchlistAdd = document.getElementById('div_derivative_watchlist_add');
			divWatchlistAdd.style.display = 'inline';		
		}
	}
	else{
		hideElement('derivat_menu_item_watchlist');
		hideElement('derivat_menu_item_portfolio');
		/*trWatchlistElement = document.getElementById('derivat_menu_item_watchlist');
		trWatchlistElement.style.display = 'none';*/
	}
	
	document.getElementById('derivat_menu').style.left = xPos-2;
	document.getElementById('derivat_menu').style.top = yPos-2;
	document.getElementById('derivat_menu').style.visibility = 'visible';
}

function certificatemenu(listnr, derivatname, tr_color, isin, market, orderid, derivativetype, issuer, countryid) {
	document.getElementById("certificate_menu_name").innerText = derivatname;

	nordnet_marketid = getPartnerMarketId(market, "NORDNET")
	shortinfo_orderid = getNordnetOrderId(orderid, derivatname, "DERIVATIVE", derivativetype);

	derivat_tr = 'tr'+listnr;
	derivat_tr_color = tr_color;
	shortinfo_isin = isin;
	shortinfo_derivativetype = derivativetype;
	shortinfo_marketplace = market;
	shortinfo_shortname = derivatname;	
	shortinfo_issuer = issuer;	
	shortinfo_countryid = countryid;
	var xPos = findPosX(document.getElementById('arrow_'+listnr));
	var yPos = findPosY(document.getElementById('arrow_'+listnr));
	document.getElementById('certificate_menu').style.left = xPos;
	document.getElementById('certificate_menu').style.top = yPos;
	document.getElementById('certificate_menu').style.visibility = 'visible';

	if(user_logged_in){
		// Watchlist code	
		// - Change the visibility of the watchlist menu item depending on if it already exist in the watchlist
		if(existInWatchlist(shortinfo_isin, 'warrant', shortinfo_countryid)){
			divWatchlistRemove = document.getElementById('div_certificate_watchlist_remove');
			divWatchlistAdd = document.getElementById('div_certificate_watchlist_add');
			divWatchlistRemove.style.display = 'inline';
			divWatchlistAdd.style.display = 'none';
		}
		else{
			divWatchlistRemove = document.getElementById('div_certificate_watchlist_remove');
			divWatchlistAdd = document.getElementById('div_certificate_watchlist_add');
			divWatchlistRemove.style.display = 'none';
			divWatchlistAdd.style.display = 'inline';		
		}
	}
	else{
		hideElement('certificate_menu_item_portfolio');
		hideElement('certificate_menu_item_watchlist');
	}

	// Hide menu item
	if(issuer != "RBS")
	{
		var detailedInfoDiv1 = document.getElementById("certificate_menu_1");
		detailedInfoDiv1.style.display = 'none';
		var detailedInfoDiv2 = document.getElementById("certificate_menu_2");
		detailedInfoDiv2.style.display = 'none';
		var detailedInfoDiv3 = document.getElementById("certificate_menu_3");
		detailedInfoDiv3.style.display = 'none';
	}
	else{
		var detailedInfoDiv1 = document.getElementById("certificate_menu_1");
		detailedInfoDiv1.style.display = '';
		var detailedInfoDiv2 = document.getElementById("certificate_menu_2");
		detailedInfoDiv2.style.display = '';
		var detailedInfoDiv3 = document.getElementById("certificate_menu_3");
		detailedInfoDiv3.style.display = '';
	}
	
	if(!hasInfo(isin, issuer, countryid)){
		var detailedInfoDiv = document.getElementById("certificate_menu_1");
		detailedInfoDiv.style.display = 'none';
	}
	if(!hasProspect(isin, issuer, shortinfo_shortname, shortinfo_marketplace, countryid)){
		var detailedInfoDiv = document.getElementById("certificate_menu_2");
		detailedInfoDiv.style.display = 'none';		
	}
	if(!hasConditions(isin, issuer, countryid)){
		var detailedInfoDiv = document.getElementById("certificate_menu_3");
		detailedInfoDiv.style.display = 'none';		
	}
}

function maxcertificatemenu(listnr, derivatname, tr_color, isin, market, orderid, derivativetype, issuer, countryid) {
	document.getElementById("maxcertificate_menu_name").innerText = derivatname;

	nordnet_marketid = getPartnerMarketId(market, "NORDNET")
	shortinfo_orderid = getNordnetOrderId(orderid, derivatname, "DERIVATIVE", derivativetype);

	derivat_tr = 'tr'+listnr;
	derivat_tr_color = tr_color;
	shortinfo_isin = isin;
	shortinfo_derivativetype = derivativetype;
	shortinfo_marketplace = market;
	shortinfo_shortname = derivatname;	
	shortinfo_issuer = issuer;	
	shortinfo_countryid = countryid;
	var xPos = findPosX(document.getElementById('arrow_'+listnr));
	var yPos = findPosY(document.getElementById('arrow_'+listnr));
	document.getElementById('maxcertificate_menu').style.left = xPos;
	document.getElementById('maxcertificate_menu').style.top = yPos;
	document.getElementById('maxcertificate_menu').style.visibility = 'visible';

	if(user_logged_in){
		// Watchlist code
		// - Change the visibility of the watchlist menu item depending on if it already exist in the watchlist
		if(existInWatchlist(shortinfo_isin, 'warrant', shortinfo_countryid)){
			showElement('div_maxcertificate_watchlist_remove');
			hideElement('div_maxcertificate_watchlist_add');
		}
		else{
			showElement('div_maxcertificate_watchlist_add');
			hideElement('div_maxcertificate_watchlist_remove');
		}
	}
	else{
		hideElement('maxcertificate_menu_item_portfolio');
		hideElement('maxcertificate_menu_item_watchlist');
	}
}

function minifuturemenu(listnr, derivatname, tr_color, isin, market, orderid, derivativetype, issuer, countryid) {
	document.getElementById("minifuture_menu_name").innerText = derivatname;

	nordnet_marketid = getPartnerMarketId(market, "NORDNET");
	shortinfo_orderid = getNordnetOrderId(orderid, derivatname, "DERIVATIVE", derivativetype);

	derivat_tr = 'tr'+listnr;
	derivat_tr_color = tr_color;
	shortinfo_isin = isin;
	shortinfo_derivativetype = derivativetype;
	shortinfo_marketplace = market;
	shortinfo_shortname = derivatname;	
	shortinfo_issuer = issuer;	
	shortinfo_countryid = countryid;
	var xPos = findPosX(document.getElementById('arrow_'+listnr));
	var yPos = findPosY(document.getElementById('arrow_'+listnr));
	document.getElementById('minifuture_menu').style.left = xPos;
	document.getElementById('minifuture_menu').style.top = yPos;
	document.getElementById('minifuture_menu').style.visibility = 'visible';

	if(user_logged_in){
		// Watchlist code
		// - Change the visibility of the watchlist menu item depending on if it already exist in the watchlist
		if(existInWatchlist(shortinfo_isin, 'warrant', shortinfo_countryid)){
			showElement('div_minifuture_watchlist_remove');
			hideElement('div_minifuture_watchlist_add');
		}
		else{
			showElement('div_minifuture_watchlist_add');
			hideElement('div_minifuture_watchlist_remove');
		}
	}
	else{
		hideElement('minifuture_menu_item_portfolio');
		hideElement('minifuture_menu_item_watchlist');
	}
}

function sharemenu(listnr, shareisin, sharename, tr_color, market, currency, orderid, countryid) {
	document.getElementById("share_menu_name").innerText = sharename;

	nordnet_marketid = getPartnerMarketId(market, "NORDNET")
	shortinfo_orderid = getNordnetOrderId(orderid, sharename, "SHARE", "");

	share_tr = 'tr'+listnr;
	share_tr_color = tr_color;
	shortinfo_isin = shareisin;
	share_currency = currency;
	shortinfo_shortname = sharename;
	shortinfo_marketplace = market;
	shortinfo_countryid = countryid;

	if(user_logged_in){
		// Watchlist code
		// - Change the visibility of the watchlist menu item depending on if it already exist in the watchlist
		if(existInWatchlist(shortinfo_isin, 'share', shortinfo_countryid)){
			hideElement('div_share_watchlist_add');
			showElement('div_share_watchlist_remove');
		}
		else{
			hideElement('div_share_watchlist_remove');
			showElement('div_share_watchlist_add');
		}
	}
	else{
		hideElement('share_menu_item_portfolio');
		hideElement('share_menu_item_watchlist');
	}
	
	var xPos = findPosX(document.getElementById('arrow_'+listnr));
	var yPos = findPosY(document.getElementById('arrow_'+listnr));
	document.getElementById('share_menu').style.left = xPos;
	document.getElementById('share_menu').style.top = yPos;
	document.getElementById('share_menu').style.visibility = 'visible';
}

function watchlist_add_remove(idDivWatchlistAdd, idDivWatchlistRemove){
	divWatchlistRemove = document.getElementById(idDivWatchlistRemove);
	divWatchlistAdd = document.getElementById(idDivWatchlistAdd);
	if(divWatchlistRemove.style.display == 'none' && divWatchlistAdd.style.display == 'inline'){
		watchlist_addfunc();
	}
	else{
		watchlist_removeFunc();
	}
}

function watchlist_addfunc() {
	var country_id_url_param = '&watchlist_country_id='+shortinfo_countryid;
	var url = parseQueryString('watchlist_add', shortinfo_isin, 'watchlist_remove');
	document.location=(location.pathname+url+country_id_url_param);
}

function watchlist_removeFunc() {
	var country_id_url_param = '&watchlist_country_id='+shortinfo_countryid;
	var url = parseQueryString('watchlist_remove', shortinfo_isin, 'watchlist_add');
	document.location=(location.pathname+url+country_id_url_param);
}

function compare_list(listtype) {
	var compare_link, counter = 1;
	compare_link = 'compare.php?listtype='+listtype;
	els = document.getElementById("compareform").getElementsByTagName('input');
	for(vCnt = 0; vCnt < els.length; vCnt++) {
		if(els[vCnt].checked == true) {
			compare_link = compare_link + '&' + counter + '=' + els[vCnt].value;
			counter++;
		}
	}
	if(counter > 2) document.location=(compare_link);
	else alert('Du måste välja två eller fler derivat att jämföra');
}
function parseQueryString(variable, value, variableToRemove) {
  var new_query = '?', query_found = 0;
  var str = location.search;
  var query = str.charAt(0) == '?' ? str.substring(1) : str;
  var args = new Object();
  if (query) {
    var fields = query.split('&');
    for (var f = 0; f < fields.length; f++) {
      var field = fields[f].split('=');
      if(field[0] == variable) {
      	args[field[0]] = value;
      	query_found = 1;
      }
      else {
      	if(field[0] != 'watchlist_add')	args[field[0]] = field[1];
      	if(field[0] != 'watchlist_remove')	args[field[0]] = field[1];
      }
    }
  }
  for (var arg in args) {
  	if(arg != variableToRemove){
  		new_query += arg+'='+args[arg]+'&';
  	}
  }
  if(query_found == 0) { new_query += variable+'='+value+'&'; }
  return new_query.substring(0, new_query.length-1);
}

function helpPopup(id) {
	if(id == null) id = 1;
	window.open ("dictionary_popup.php?id="+id, "dictionary_popup", "width=580,height=361,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1");
}

function getPartnerMarketId(market, partner){
	var etrade_market = 'etrade_market';
	var avanza_market = 'avanza_market';
	var nordnet_marketid = 'nordnet_marketid';
	if(market == 'OM'){
		// Optionsmarknad
		avanza_market = 'OM1';
		etrade_market = 'OM';
		nordnet_marketid = '12';
	}
	else if (market == 'NDX' || market == 'NGM') {
		// NDX/NGM
		etrade_market = 'NGM';
		avanza_market = 'SBI';
		nordnet_marketid = '13';
	}
	else if (market == 'SSE') {
		// Stockholmsbörsen
		avanza_market = 'SX1';
		etrade_market = 'SSE';
		nordnet_marketid = '11';
	}

	if(partner == 'NORDNET'){
		return nordnet_marketid;
	}
	else if(partner == 'ETRADE'){
		return etrade_market;
	}
	else if(partner == 'AVANZE'){
		return avanza_market;
	}
	
	return nordnet_marketid;
}

/* ### NORDNET TRADE INTEGRATION ### */
function getNordnetOrderId(orderId, shortname, productType, derivativeType){
	var nordnetOrderId = orderId;

	if(productType == "DERIVATIVE"){
		if(derivativeType == "O" || derivativeType == "T"){
			nordnetOrderId = shortname;
		}
	}

	return nordnetOrderId;
}

function tradeViaPartner(product_isin, product_order_id, product_shortname, product_type, derivativetype, marketplace, partner, buy_or_sell, origin, country_id, currency) {
	window_name = "QUICKTRADE";
	w = 545;
	h = 550;
	scrollbars = "yes";		
	leftPosition=(screen.width)?(screen.width-w)/2:100;
	topPosition=(screen.height)?(screen.height-h)/2:100;

	settings='width='+w+',height='+h+',top='+topPosition+',left='+leftPosition+',scrollbars='+scrollbars+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes';

	window_url = "partners_trade.php?isin=" + product_isin + "&orderid=" + product_order_id + "&shortname=" + product_shortname + "&producttype=" + product_type + "&derivativetype=" + derivativetype + "&marketplace=" + marketplace + "&partner=" + partner + "&buysell=" + buy_or_sell + "&origin=" + origin + "&country_id=" + country_id + "&currency=" + currency;
	/*alert("Window URL: " + window_url); return;*/
	win = window.open(window_url, window_name, settings);
	win.focus();
}

function hideElement(elementId){
	var element = document.getElementById(elementId);
	element.style.display = 'none';
}

function showElement(elementId){
	var element = document.getElementById(elementId);
	element.style.display = 'inline';
}
