// AS 30/01/2001 - Functions for Favourite Funds
var openWindows = new Object();

function showFavFunds(section, button)
{
	//var arrff = null;
	var arrff = load_favfunds();
	
	if (arrff != null && arrff != "")
	{
		//url for opening factSheet via fundperf.cgi
		//var factSheetUrl = 'http://dynabase.fidelity.co.uk:8383/cgi-bin/fundperf.cgi?mark=uk/pi&lang=e&id=';
		//var factSheetUrl = 'http://www.fidelity.co.uk/cgi-bin/fundperf.cgi?mark=uk/pi&lang=e&id=';
		var factSheetUrl = 'http://www.fidelity.co.uk/cgi-bin/fundchart.cgi?id=';
		var ffHtmlStr = '<DIV ID="favFunds" CLASS="sdMenu">';
	
		ffHtmlStr += '<BR><BR>';
		ffHtmlStr += '<IMG SRC="/images/favfunds/favfunds.gif" WIDTH="200" HEIGHT="18" BORDER="0" ALT="your favourite funds">';

		/* AS 05/03/2001 - Commented for the current release; as required by John		
		ffHtmlStr += '<BR><BR>';
		ffHtmlStr += '<A HREF="#"><IMG SRC="/images/fsm/i_info.gif" WIDTH="16" HEIGHT="14" BORDER="0" ALT="view info online"></A><IMG SRC="/images/shim.gif" WIDTH="16" HEIGHT="10"><FONT CLASS="sdSelected">Fund Name</FONT>';
		ffHtmlStr += '<BR>';
		ffHtmlStr += '<IMG SRC="/images/shim.gif" WIDTH="32" HEIGHT="10"><FONT CLASS="sdSelected">Price</FONT><IMG SRC="/images/shim.gif" WIDTH="26" HEIGHT="10"><FONT CLASS="sdSelected">Charge</FONT>';
		*/
		
		for(var i=0; i<arrff.length; i++)
		{
			if(arrff[i][5] == "e")
				continue;

			/* AS 05/03/2001 - Commented for the current release; as required by John				
			if(arrff[i][1] == "Not Available")
			{
				arrff[i][1] += " (" + arrff[i][0] + ")";
				arrff[i][2] = "";
				arrff[i][3] = "";
			}
			
			if(arrff[i][2] == "999")
				arrff[i][2] = "N/A";

			if(arrff[i][3] == "999")
				arrff[i][3] = "N/A";
			ffHtmlStr += '<BR><BR>';
			ffHtmlStr += '<A HREF="javascript: fidOpenNewWindow(\'ff' + arrff[i][0].substring(0, 6) + '\', \'' + factSheetUrl + arrff[i][0].substring(0, 6) + '\');"><IMG SRC="/images/fsm/i_info.gif" WIDTH="16" HEIGHT="14" BORDER="0" ALT="view info online"></A><IMG SRC="/images/shim.gif" WIDTH="16" HEIGHT="10">' + arrff[i][1];
			ffHtmlStr += '<BR>';
			ffHtmlStr += '<IMG SRC="/images/shim.gif" WIDTH="32" HEIGHT="10">' + arrff[i][2] + '<IMG SRC="/images/shim.gif" WIDTH="26" HEIGHT="10">' + arrff[i][3];
			*/

			if(arrff[i][1] == "Not Available")
				arrff[i][1] += " (" + arrff[i][0] + ")";

			ffHtmlStr += '<BR>';
			ffHtmlStr += '<A CLASS="sdMenu" HREF="javascript: fidOpenNewWindow(\'ff' + arrff[i][0].substring(0, 6) + '\', \'' + factSheetUrl + arrff[i][0].substring(0, 6) + '\');"><IMG ALIGN="absbottom" SRC="/images/favfunds/i_info.gif" WIDTH="16" HEIGHT="14" BORDER="0" ALT="view info online"><IMG SRC="/images/favfunds/shim.gif" WIDTH="5" HEIGHT="10" BORDER="0">' + arrff[i][1] + '</A>';
		}

		ffHtmlStr += '<BR><BR>';		
		ffHtmlStr += '<A CLASS="favfund" HREF="/tools/prestelchart.html">Chart</A><IMG SRC="/images/favfunds/shim.gif" WIDTH="110" HEIGHT="10" BORDER="0"><A CLASS="favfund" HREF="/accountopening/index.html">Buy Now</A>';
		ffHtmlStr += '</DIV>';
		
		document.write(ffHtmlStr);
	}
}

function savFavFundsFact(arrFundsSgl)
{
	var arrFavFunds = new Array(1);
	arrFavFunds[0] = new Array(3);
	arrFavFunds[0][0] = arrFundsSgl;
	arrFavFunds[0][1] = "y";
	arrFavFunds[0][2] = "f";

	//call to savFavFunds for adding/appending a fund, into the favFunds list, from the factsheets
	savFavFunds(arrFavFunds, "addFavourite")
}

function savFavFundsChart(arrFundsMult)
{
	//call to savFavFunds for replacing the old favFunds list, from the charting tool
	savFavFunds(arrFundsMult, "saveAllFavourites")
}

function savFavFunds(arrFunds, noOfFunds)
{
	var loadPage = "http://www.fidelity.co.uk/tools/favfundsload.html";
	var ffQryStr = "";		//favourite funds query string having all the required info
	if(arrFunds != "" && arrFunds != null)
	{
		var sedol = "";			//Alphanumeric without semicolon ";"
		var display = "";		//y/n
		var equityFunds = ""; 	//e/f
		if (arrFunds[0][0]=="")
		{
			alert("Sorry, but the funds you have chosen are not available to be saved.  Please select again.");
			return;
		}
		else
		{
			sedol += arrFunds[0][0];
			display += arrFunds[0][1];
			equityFunds += arrFunds[0][2];
			if(noOfFunds == "saveAllFavourites" && arrFunds.length>1)
				for(var i=1; i<arrFunds.length; i++)
				{
					if (arrFunds[i][0]=="")
					{
						alert("Sorry, but the funds you have chosen are not available to be saved.  Please select again.");
						return;
					}
					else
					{
						sedol += ":" + arrFunds[i][0];
						display += ":" + arrFunds[i][1];
						equityFunds += ":" + arrFunds[i][2];
					}
				}
			ffQryStr += "?METHOD=" + noOfFunds + "%26SEDOL=" + sedol + "%26DISP=" + display + "%26EQUITY=" + equityFunds;
		}
	}
	else
		ffQryStr += "?METHOD=" + noOfFunds + "%26SEDOL=%26DISP=%26EQUITY=";

		
	loadPage += ffQryStr;

	fidOpenNewWindow('favFundsLoading', loadPage, 'favFundsLoading', 800, 600);
}

function load_favfunds()
{
	if (document.cookie != null && document.cookie != "")
	{
		var cookie = document.cookie;
		var arrCookie = cookie.split(";");
		var arrFavFunds = new Array();
		var return_str = "";

		if ( cookie.indexOf("loggedIntoAccountManagement=NULL") < 0)
		{
			return_str = "notlogged";
			return return_str;
			//window.location.href = 'https://www.accountmanagement.fidelity.co.uk/FundFavourites?METHOD=checkLogin&redirect=http://fidelity.co.uk/direct/newsroom/charttool/charttool.html';
		}
		for(var j=0; j<arrCookie.length; j++)
			if (arrCookie[j].indexOf("favFunds=") != -1)
			{
				
				var ffSt = arrCookie[j].indexOf("favFunds=") + 9;
				var ffCookie = arrCookie[j].substring(ffSt);
				
				if (ffCookie != "" && ffCookie.indexOf("_") != -1)
				{				
					var arrffCookie = ffCookie.split("::");
					//
						
					for(var i=0; i<arrffCookie.length; i++)
						arrFavFunds[i] = arrffCookie[i].split("_");
						
					return arrFavFunds;
				}
				else
				{
					return_str = "nofunds";
					break;
				}
			} 
	}
	return return_str;
}

function buyFavFundsFact(sedolNo)
{
	window.opener.document.location = "/tools/buyfromfavs_1.html";
}

function fidOpenNewWindow(winReference, winUrl, winName, width, height)
{
	if (!winName)
		winName = winReference + "";
	if (!width)
		width = 810;
	if (!height)
		height = 580;
		
	var bV = ( navigator.appName == "Netscape" ) ? "nn" : "ie";
	
	if (!openWindows[winReference] || (openWindows[winReference] && openWindows[winReference].closed))
		if (bV == "ie")
			openWindows[winReference] = window.open(winUrl, winName,  'resizable=yes,scrollbars=yes,menubar=no,location=no,toolbar=no,status=yes,width='+width+',height='+height+',left=1,top=115');
		else 
			openWindows[winReference] = window.open(winUrl, winName,  'resizable=yes,scrollbars=yes,menubar=no,location=no,toolbar=no,status=yes,width='+width+',height='+height+',screenX=1,screenY=115');
	else
		openWindows[winReference].focus();
}