
//All of this Javascript will be place in the WrapPlatform.js file when the work has been signed off
function sortByName(a, b) 
{//Sort method to alphabetically sort array list full of names
    var x = a.Name.toLowerCase();
    var y = b.Name.toLowerCase();
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
	
	function loadTimeValues(val,ctrl)
{
    //Function overwritten from chartingtoolv10 file to show custom time values
	var options = null, selectedIndex = 0;
	
	var menu = GetElementById(ctrl);
	menu.options.length = 0;

	options = new Array("60:5 Years", "36:3 Years", "12:1 Year", "6:6 Months");
	selectedIndex = 2;	

	for (var i = 0; i < options.length; i++)
	{
		var option = options[i];
		var optionArr = option.split(":");
			
		var optValue = optionArr[0];
		var optText = optionArr[1];
			
		menu.options.add(new Option(optText, optValue), 0);		
	}
	
	menu.selectedIndex = selectedIndex;
}	
	
function UpdatePerfTablesSub(r, isCumulative, showFiveYearPerf)
{//Overwritten to obtain colour of different funds on chart and key
	var perfTableShow = (isCumulative ? PerfTableCumulativeShow : PerfTableDiscreteShow);
	var pfText = (isCumulative ? 'Cumulative' : 'Discrete');
	var addRowFunc = (isCumulative ? addCumulativePerformanceRow : addDiscretePerformanceRow);
	
	// if we can't find PerfTables division, then don't do anything.
	if (GetElementById('PerfTables') == null)
	{
		return;
	}
	
	var dataArr = GetElementById('hdFundList').value.split(";");
    var options;	
    var optioncategory = new Array();
    var currentTitle;

		
	var coreFund = '';
	var coreSector = '';
	var dataNames = new Array();	
	var data = r.PerfItems;
	
    //Sorts the data by NAME Ascending 
	data.sort(sortByName); //major sort

	var performanceTable = GetElementById("tbl" + pfText + "Performance");
	if (performanceTable == null)
	{
		return;
	}

	var performanceHeader = GetElementById("theadCumulativePerformance");
	performanceTable.removeChild(performanceHeader);
	CreateHeader(showFiveYearPerf);
	
	var performanceBody = GetElementById("tbdy" + pfText + "Performance");
	if (performanceBody) performanceTable.removeChild(performanceBody);
	
	var performanceBody = document.createElement("tbody");
	
	performanceBody.id = "tbdy" + pfText + "Performance";
	performanceTable.appendChild(performanceBody);
	
	var img = GetElementById("ChartImg");
	var currSrc = img.src;	
	var allCodes = parseURL(currSrc, "codes");
	var sectorCode = parseURL(currSrc, "sector");
	var arrAllCodes = new Array();
	var numCodes = 0;
	var numCoreRows = 0;
	var numPerfItems = data.length;
	
	if(sectorCode != "")
	{
	    allCodes += (allCodes != "") ? "," + sectorCode : sectorCode;
	}
	if (allCodes != "")
	{
		arrAllCodes = allCodes.split(",");
		numCodes = arrAllCodes.length;							
	}
	
	var lastItem = numCodes - 1;

	// Load core fund & sector performance, quartiles & rankings
	var x = 0;
	for (var i = 0; i < numCodes; i++)
	{
		if(data[i] == null)
		{
			return;
		}

		if (i < lastItem && data[i].Code == coreSector)
		{
			arrAllCodes.splice(i, 1); // remove it
			arrAllCodes[lastItem] = coreSector; // stick it at the end 
			
			var tmp = data[i];
			data.splice(i, 1);
			data[lastItem] = tmp;
			
			i--; // backtrack
			
			continue;
		}
					
		if (data[i].Code == coreFund)
		{
			// Core Fund Performance rows
			var fundRow = performanceBody.insertRow(0);
			addRowFunc(fundRow, data, i, data[i].Name, "P");
			numCoreRows++;
			
			// Core Fund Rankings
			if (perfTableShow.indexOf('R') >= 0)
			{
				var fundRankRow = performanceBody.insertRow(1);
				addRowFunc(fundRankRow, data, i, "Rank in sector", "R");
				numCoreRows++;
			}
			
			// Core Fund Quartiles
			if (perfTableShow.indexOf('Q') >= 0)
			{
				var fundQuartileRow = performanceBody.insertRow(2); 
				addRowFunc(fundQuartileRow, data, i, "Quartile", "Q");
				numCoreRows++;
			}
			
			x = 1;
		}
		
		if (perfTableShow.indexOf('S') >= 0)
		{
			if (i == lastItem && data[i].Code == coreSector)
			{
				// Core Sector erformance rows
				var sectorRow = performanceBody.insertRow(x);
				addRowFunc(sectorRow, data, i, data[i].Name, "P");
				numCoreRows++;
			}
		}
	}

	var posToAddNewRow = numCoreRows;

	// Add seperator 
	if (posToAddNewRow > 0 && perfTableShow.indexOf('B') >= 0)
	{
		var blankRow = performanceBody.insertRow(posToAddNewRow);
		addRowFunc(blankRow, data, 0, "", "B");
		posToAddNewRow++;
	}

	// Load user selected instruments

	var tmpArr = new Array();
	tmpArr = arrAllCodes;
	
	for (var i = 0; i < tmpArr.length; i++)
	{
		if (tmpArr[i] == coreFund)
		{
			tmpArr.splice(i, 1);
		}
		if (tmpArr[i] == coreSector)
		{
			tmpArr.splice(i, 1);
		}
	}
	
	var navRow = null;
	for (var i = 0; i < performanceBody.childNodes.length; i++)
	{
		var oRow = performanceBody.childNodes[i];		
		if (performanceBody.flaggedRows == null)
		{
			oRow.NotData = true;
		}
		else if (oRow.NotData == null)
		{
			performanceBody.removeChild(oRow);
			i--; // to counter the removal.
		}
		if (navRow == null && oRow.NotData == true)
		{
			navRow = oRow;
		}
	}
	performanceBody.flaggedRows = true;
    
	// Output Ordered data

	if (tmpArr.length > 0)
	{
	//for (var i = 0; i < tmpArr.length; i++)
		//{
		   // var codeValueArr = tmpArr[i].split("@");
		    		var codeValueStr = "";		
		
		    //Builds string of Typecodes and colour codes
		    for (var i=0; i<tmpArr.length; i++)
		    {
		        var codeValueArr  = tmpArr[i].split("@");
		        
		            if (i == 0)
		            {
    		            codeValueStr = codeValueArr[0] + "," + codeValueArr[1]	
		            }
		            else
		            {
		                codeValueStr += "," + codeValueArr[0] + "," + codeValueArr[1]	
		            }      	    
		    }
		    
		    //This array contains the correct colour codes that correspond to the appropriate typecode
		    var codeColourArray = codeValueStr.split(",");
		    nameOrderedCodes = ""
		    
			for (var x = 0; x < data.length; x++)
			{
			    var chartColour = "";
			    
			    //This loop checks through the array of colour codes from the chart to get the appropriate colour for the newly
			    //sorted list of type codes
			    for (var k = 0; k<codeColourArray.length; k++)
			    {
			        if (data[x].Code == codeColourArray[k])
			        {
			            k++;			            
			            chartColour = codeColourArray[k];
			            break;
			        }
			    }
			    for (z=0;z<dataArr.length; z++) 
	            {
		            optionCollection = dataArr[z].split("!");		    
		            optionCollection[0]= optionCollection[0].split("@")[0];
		            for (y=0; y<optionCollection.length; y++) 
		            {
		                if(optionCollection[y].indexOf(data[x].Code) >= 0)
		                {
		                    currentTitle = optionCollection[y].split(",")[0];
		                    break;
		                }
		            }
	            }
	    
				    var fundRow = document.createElement("tr");
					fundRow.id = "i" + (x + 1);
					performanceBody.insertBefore(fundRow, navRow);

					if(i % 2 == 0)
			        {
				        classname = "rw";
			        }
			        else
			        {
				        classname = "rwa";
			        }		       	
			       		        
			        if(data[x].Name.indexOf("IMA") < 0)			       
			        {		           
					    addRowFunc(fundRow, data, x, data[x].Name, "P", classname, showFiveYearPerf, chartColour,currentTitle);
                        
                        //This code list is created for the perf chart show/hide to retain colours to the correct funds
                        if (x ==0)
                            {nameOrderedCodes += data[x].Code + "@" + chartColour;}
                        else
                            {nameOrderedCodes += "," + data[x].Code + "@" + chartColour;}			
					}
					else
					{
					    addRowFunc(fundRow, data, x, data[x].Name, "P", classname, showFiveYearPerf, "",currentTitle);
					}
					posToAddNewRow++;

			}

	}		
}


function LoadFundValues(isLoadSector)
{
    var AllFunds = GetElementById('hdFundList').value;
	var menuFunds = GetElementById('Unit');
	var menuSectors = GetElementById('Sector');
	menuSectors.options.length = 0;

	var slctIndex = 0, i, fundcount = 0;
	var dataArr = new Array();
	var optioncategory = new Array();
	var options;
	
	dataArr = AllFunds.split(";");
	
	for (i=0; i<dataArr.length; i++) 
	{
		options = dataArr[i];
		optionCollection = options.split("!");
		
		var fundOption = optionCollection[0].split(",");
        var fundName = fundOption[0];
        var fundValue = fundOption[1];
        
        if(!isLoadSector)
        {
            var chkflag = true;
	        for(var opt=0; opt<menuFunds.options.length; opt++)
	        {
		        if(menuFunds.options[opt].value == fundValue)
		        {
			        chkflag = false;
		        }
	        }
	        if(chkflag == true && fundName != '' && fundValue != '')
	        {
		        menuFunds.options.add(new Option(fundName, fundValue.replace('#','')), 0);
	        }
	    }
	    else if(fundValue != undefined)
	    {
	        var img = GetElementById("ChartImg");
	        var currSrc = img.src;
	        var allCodes = parseURL(currSrc, "codes");
	        var unitValue = getValue("Unit");
	        unitValue = unitValue.replace('#','');
	        var codeArray = allCodes.split(",");
	        
	        var isCodeFound = false;
	        for(var l=0; l<codeArray.length; l++)
	        {
	            if(codeArray[l] == unitValue)
	            {
	                isCodeFound = true;
	            }
	        }

	        if(!isCodeFound && codeArray[0] != "")
	        {
	            GetElementById("Sector").disabled = true;
	        }
	        else if(isCodeFound && codeArray.length == 1)
	        {
	            GetElementById("Sector").disabled = false;
	        }
	        
	        if(fundValue.replace('#','') == getValue('Unit') && getValue('Unit') != "" && optionCollection[1] != null)
	        {
	            var sectorOption = optionCollection[1].split(",");
                var sectorName = sectorOption[0];
                var sectorValue = sectorOption[1];
                var chkflag = true;
                
	            for(var opt=0; opt<menuSectors.options.length; opt++)
	            {
		            if(menuSectors.options[opt].value == sectorValue)
		            {
			            chkflag = false;
		            }
	            }
	            if(chkflag == true && sectorName != '' && sectorValue != '')
	            {
	                var img = GetElementById("ChartImg");
	                var currSrc = img.src;
	                var sectorCode = parseURL(currSrc, "sector");
	                if(sectorCode.indexOf(sectorValue) >= 0)
	                {
	                    unitCodeChangeable = true;
	                }
		            menuSectors.options.add(new Option(sectorName, sectorValue.replace('#','')), 0);
	            }
	        }
	    }
	}
	if(!isLoadSector)
	{
	    menuFunds.options.add(new Option("Choose a Fund", ""), 0);
        menuFunds.selectedIndex = 0;
    }
    if(menuSectors.options.length == 0)
    {
        menuSectors.options.add(new Option("Choose a Sector", ""), 0);
        menuSectors.selectedIndex = 0;
    }
}


function CreateHeader(showFiveYearPerf)
{//Overwritten due to header re-shuffling
    /* Header */
    
    var performanceTable = GetElementById("tblCumulativePerformance");
    var performanceHeader = document.createElement("thead");
	performanceHeader.id = "theadCumulativePerformance";
	performanceTable.appendChild(performanceHeader);
	
    var headRow = document.createElement("tr");
    headRow.id = "perfHeader";
    headRow.className = "head";
    performanceHeader.insertBefore(headRow, null);
    
    var noOfColumns = 7;
   
    if(!showFiveYearPerf)
    {
        not5yrPerf = true;
    }
    else
    {
        has5yrPerf = true;    
    }
    
    if (not5yrPerf == true)
    {
        ++noOfColumns;
    }
    
    if (has5yrPerf == true)
    {
        ++noOfColumns;
    }       
    
    var cells = LegendRowAddCells(headRow, noOfColumns);
    
    var txtNode= document.createTextNode("Key");
	cells[0].className = "charHeadPad";
	cells[0].appendChild(txtNode);	
	
	txtNode= document.createTextNode("Rathbone Funds/Sector");
	cells[1].className = "charHeadPad";
	cells[1].appendChild(txtNode);
	
	txtNode= document.createTextNode("6 months");
	cells[2].className = "charHeadPad";
	cells[2].appendChild(txtNode);
	
	txtNode= document.createTextNode("1 year");
	cells[3].className = "charHeadPad widex";
	cells[3].appendChild(txtNode);
	
	txtNode= document.createTextNode("3 years");
	cells[4].className = "charHeadPad widex";
	cells[4].appendChild(txtNode);
	
	if(has5yrPerf == true && not5yrPerf == true )
	{
	    txtNode= document.createTextNode("5 years");
	    cells[5].className = "charHeadPad widex";
	    cells[5].appendChild(txtNode);
	    
	    txtNode= document.createTextNode("Since Launch");
	    cells[6].className = "charHeadPad";
	    cells[6].appendChild(txtNode);
	}
	else if (has5yrPerf == true && not5yrPerf == false )
	{
	    txtNode= document.createTextNode("5 years");
	    cells[5].className = "charHeadPad widex";
        cells[5].appendChild(txtNode);
    }
	else if (has5yrPerf == false && not5yrPerf == true )
	{
	    txtNode= document.createTextNode("Since Launch");
	    cells[5].className = "charHeadPad";
	    cells[5].appendChild(txtNode);
    }
   
	txtNode= document.createTextNode("Show");
	cells[noOfColumns - 2].className = "charHeadPad sml";
	cells[noOfColumns - 2].appendChild(txtNode);
	
	 //Remove column removed but code left in incase client changes mind
	txtNode= document.createTextNode("Remove");
	cells[noOfColumns - 1].className = "charHeadPad sml";
	cells[noOfColumns - 1].appendChild(txtNode);
	
	//not5yrPerf = false;
    //has5yrPerf = false;
	
	/* End Header */
}

function addCumulativePerformanceRow(row, data, m, title, type, name, showFiveYearPerf, chartingColor,currentTitle)
{//Overwritten to vary number of columns
    var noOfColumns = 7;
   
    if(!showFiveYearPerf)
    {
        not5yrPerf == true;
    }
    else
    {
        has5yrPerf = true;    
    }
    
    if (not5yrPerf == true)
    {
        ++noOfColumns;
    }
    
    if (has5yrPerf == true)
    {
        ++noOfColumns;
    }  
    
    //ammend the title to put brackets around part of the name, string formatting is done
    //as this comes from the ws performance function and its easier to format it here rather than change it earlier on
    //as its a generic function taking data from the instruments view
    var IncAccIdx = (title.lastIndexOf(' Acc') == -1) ? title.lastIndexOf('Inc'):title.lastIndexOf('Acc');
    var CosIdx = title.lastIndexOf('Cos')
    
    if (CosIdx != -1)
    {
        title = currentTitle.replace("(","Companies (");
    }
    else
    {    
        title = currentTitle;
    }
    
	var tdCells = createCells(row, noOfColumns, 'titleCell', 'numberData');

	SetInnerText(tdCells[1], title);
	tdCells[0].className = classname + " ac";
	if(chartingColor != "")
	{
	    tdCells[0].innerHTML = '<Center><div class="key" style="background-color:#' +  chartingColor + '" ></Center>';
	}
	else
	{
	    tdCells[0].innerHTML = '<Center><img src="Images/dash.gif" /></Center>';
	}
	tdCells[1].className = classname;
	tdCells[2].className = classname + " ac";
	tdCells[3].className = classname + " ac";
	tdCells[4].className = classname + " ac";
	//tdCells[5].className = classname + " ac"; 
    
    if(has5yrPerf == true && not5yrPerf == true)
	{
	    tdCells[5].className = classname + " ac";
	    tdCells[6].className = classname + " ac";
	}
	else (has5yrPerf == true && not5yrPerf == false)
	{
	    tdCells[5].className = classname + " ac";	    
	}
		
	//Here the appropriate figures are filled in for the table
	switch (type)
	{
		case "P":	// Performance
		    if(has5yrPerf == true && not5yrPerf == true)
		    {
			    SetCellsToPropVals(
				    tdCells, 2, data[m],
				    ['P6', 'P12', 'P36', 'P60','PBase'],
				    roundPercentage
			    );
			}
			else if(has5yrPerf == true && not5yrPerf == false)
			{
			    SetCellsToPropVals(
				    tdCells, 2, data[m],
				    ['P6', 'P12', 'P36', 'P60'],
				    roundPercentage
			    );
			}
			else if(has5yrPerf == false && not5yrPerf == true)
			{
			    SetCellsToPropVals(
				    tdCells, 2, data[m],
				    ['P6', 'P12', 'P36', 'PBase'],
				    roundPercentage
			    );
			}			
			break;
		case "R":	// Ranking
			SetCellsToPropVals(
				tdCells, 1, data[m], 
				['R3', 'R6', 'R12', 'R36', 'R60'], 
				['T3', 'T6', 'T12', 'T36', 'T60'],
				getRank
			);
			break;
		case "Q":
			SetCellsToPropVals(
				tdCells, 1, data[m],
				['Q3', 'Q6', 'Q12', 'Q36', 'Q60'],
				getQuartile
			);
			break;
		case "B":
			for (var i = 0; i < tdCells.length; i++)
			{
				SetInnerText(tdCells[i], ' ');
				tdCells[i].className = 'emptyCell';
			}
			break;
	}
	
	tdCells[noOfColumns - 2].className = classname + " ac";
	var img = GetElementById("ChartImg");
    var currSrc = img.src;
    var hiddenCodes = parseURL(currSrc, "hide");
    var aHideCodeArr = hiddenCodes.split(",");
    var isToShow = true;
    for(var i=0; i<aHideCodeArr.length; i++)
    {
        if(aHideCodeArr[i] == data[m].Code)
        {
            isToShow = false;
        }
    }    
    
    if(isToShow)
    {   
	    tdCells[noOfColumns - 2].innerHTML = '<input type="checkbox" name="cbLegend" checked id="cb' + data[m].Code + '" value="' + data[m].Code + '" onclick=ChangeChart("'+ data[m].Code +'") />';
	}
	else
	{	   
	    tdCells[noOfColumns - 2].innerHTML = '<input type="checkbox" name="cbLegend" id="cb' + data[m].Code + '" value="' + data[m].Code + '" onclick=ChangeChart("'+ data[m].Code +'") />';
	}
	    //Remove column removed but code left in incase client changes mind
	tdCells[noOfColumns - 1].className = classname + " ac";
	tdCells[noOfColumns - 1].innerHTML = '<a href="#" onclick="RemoveCode(\''+ data[m].Code +'\')"><img src="Images/cross.gif" alt="delete" width="9"  height="9" /></a>';

}	

/* Removing instrument from the chart and performance table */
function RemoveCode(code)
{//Overrided due to varying number of columns in table and this functionality has to be incorporated into remove
    has5yrPerf = false;
    not5yrPerf = false;
    
    var img = GetElementById("ChartImg");
    var currSrc = img.src;
    var allCodes = parseURL(currSrc, "codes");
    var hiddenCodes = parseURL(currSrc, "hide");
    var sectorCode = parseURL(currSrc, "sector");
    var span = parseURL(currSrc, "span");
    var aCodeArr = allCodes.split(",");
    var aHCodeArr = hiddenCodes.split(",");
    
    for(var i=0; i<aCodeArr.length; i++)
    {
        var codeTempArr = aCodeArr[i].split("@");
        if(codeTempArr[0] == code)
        {
            var aColors = ChartingColors.split(",");
            for(var j=0; j<aColors.length; j++)
            {
                if(aColors[j] == codeTempArr[1])
                {
                    aColors.splice(j, 1);
                    GetElementById("Unit").disabled = false;
                    if(sectorCode != "")
                    {
                        unitCodeChangeable = false;
                    }
                }
            }
            ChartingColors = aColors.join();
            aCodeArr.splice(i, 1);
        }
    }
    for(var i=0; i<aHCodeArr.length; i++)
    {
        if(aHCodeArr[i].indexOf(code + "@") >= 0)
        {
            aHCodeArr.splice(i, 1);
        }
    }
    if(sectorCode.indexOf(code) >= 0)
    {
        if(aCodeArr.length > 1)
        {
            GetElementById("Sector").disabled = true;
        }
        else
        {
            GetElementById("Sector").disabled = false;
        }
        GetElementById("Unit").disabled = false;
        sectorCode = "";
    }
    if(aCodeArr.length == 1 && sectorCode == "")
    {
        RetainSelection("Unit", aCodeArr[0]);
        LoadFundValues(true);
        GetElementById("Sector").disabled = false;
    }
    if(allCodes == "" && sectorCode == "")
    {
        GetElementById("Unit").disabled = false;
        GetElementById("Sector").disabled = false;
        unitCodeChangeable = true;
    }
    if(sectorCode != "" && aCodeArr.length == 0)
    {
        GetElementById("Unit").disabled = false;
    }
    
    // Verify instrument age for 5yr perf show/hide
    var instrAgeArr = instrumentAges.split(",");
    var isToHide5yrs = false;
    for(var k=0; k<instrAgeArr.length; k++)
    {
        var ageCodeArr = instrAgeArr[k].split("~");
        if(ageCodeArr[0].indexOf(code+"@") >= 0)
        {
            instrAgeArr.splice(k, 1);
        }
    }
    instrumentAges = instrAgeArr.join();
    instrAgeArr = instrumentAges.split(",");
    for(var k=0; k<instrAgeArr.length; k++)
    {
        var ageCodeArr = instrAgeArr[k].split("~");
        if(ageCodeArr[1] == "hide5yrs")
        {
            isToHide5yrs = true;
            not5yrPerf = true;
        }
        else
        {            
            has5yrPerf = true;
        }
    }
    ageStatus = (isToHide5yrs) ? "hide5yrs" : "show5yrs";
    // End instrument age 
   
    allCodes = aCodeArr.join();
    hiddenCodes = aHCodeArr.join();
    ChartUpdate(allCodes, hiddenCodes, span, sectorCode);
    UpdatePerfTable(allCodes, sectorCode);
}
	
function SetCellsToPropVals(tdCells, startAt, data, props1) // optional parameters: (props2, valueFixFunc)
{//This method was overridden to not show the since launch data if 5 years data exists
	var value1, value2 = null, props2 = null, propIdx;
	var paramIdx = arguments.length;
	var bln5yrs = false;
	Debug(paramIdx < 5 || paramIdx > 6, "Incorrect number of parameters supplied to SetCellsToPropVals().");

	paramIdx--;
	if (paramIdx == 5)
	{
		props2 = arguments[paramIdx - 1];
	}
	var valueFixFunc = arguments[paramIdx];

	propIdx = 0;
	for (var i = startAt; i < tdCells.length; i++)
	{   
		value1 = GetPropertyFrom(data, props1[propIdx]);	    
	    		
		if (props2 != null)
		{
			value2 = GetPropertyFrom(data, props2[propIdx]);
		}
		if (valueFixFunc != null)
		{
			value1 = valueFixFunc(value1, value2);
			
			//Here a check is done if the 5 year value contains data
			if (props1[propIdx] == 'P60' && value1 != '-' )
	        {
	            bln5yrs = true;
	        }
	        //If the 5 year value contains data then don't show data from the base date
	        if (props1[propIdx] == 'PBase' && bln5yrs == true )
	        {         	         
	           value1 = valueFixFunc('99999.99999', value2);          
	        }	    
		}
		SetPropertyTo(tdCells[i], 'innerText', value1);
		propIdx++;
	}
}

function ChangeChart(code)
{//Overwritten to used the ordered list of codes to pass into the chart with the correct colour codes assigned  
    var img = GetElementById("ChartImg");
    var currSrc = img.src;
    
    var allCodes = nameOrderedCodes;
    var hiddenCodes = parseURL(currSrc, "hide");
    var sectorCode = parseURL(currSrc, "sector");
    var span = parseURL(currSrc, "span");
    
    var ctrl = GetElementById("cb" + code);
    var aHideCodes = hiddenCodes.split(",");
   
    var alreadyHidden = false;
    for(var i=0; i<aHideCodes.length; i++)
    {
        if(aHideCodes[i] == code)
        {
            alreadyHidden = true;
        }
    }
    if(!alreadyHidden && ctrl.checked == false) //if show box unchecked then code is added to hidden list
    {  
        hiddenCodes += (hiddenCodes != "") ? "," + code : code;
    }
    
    else if(ctrl.checked) //If show box is checked then code is removed from hidden list
    {
        var hCodeArr = hiddenCodes.split(",");
        for(var i=0; i<hCodeArr.length; i++)
        {
            if(hCodeArr[i] == code)
            {
                hCodeArr.splice(i, 1);
            }
        }
        hiddenCodes = hCodeArr.join();
    }    
    ChartUpdate(allCodes, hiddenCodes, span, sectorCode);
}

function roundPercentage(number) 
{//Overwritten to apply 2DP to numbers

    var roundedVal;
    var numNoDP;
    var numOneDP;
          
	if (number > 99999) return "-";
	
	numNoDP = Math.round(number*1)/1;
	numOneDP = Math.round(number*10)/10;
    roundedVal = Math.round(number*100)/100;	
    
    //Check done to see if we need to append zero's to make up 2 dp by checking if number comming in is 0 or 1 dp
    if (numNoDP == roundedVal)
        return numNoDP + ".00%";
    else if (numOneDP == roundedVal)
        return numOneDP + "0%";    
	else
	    return roundedVal + "%";
}



function ViewPDF()
{
	var img = GetElementById("ChartImg");
	var currSrc = img.src;

	var aCodes = parseURL(currSrc, "codes");
	var color = parseURL(currSrc,"color");

	var span = parseURL(currSrc, "span");
	var hide = parseURL(currSrc, "hide");

    var Link = "ChartPdf.aspx?"+"codes=" + aCodes + "&color=" + color + "&span=" + span + "&hide=" + hide;

	window.open(Link);
}

