<%--
   asp code Generated by Basic Processor
   Original Page Name: C:\gmTestBed\FMStocks\src\FMSWeb\ASP\ChartPortfolio.asp
--%>

<%@ Page language="VB" explicit="true" CodeBehind="ChartPortfolio.aspx.vb" Inherits="FMSWeb._ChartPortfolio" %>

<%
   HttpContext.Current.Response.Buffer = True
%>
<html>
<head>
<% _t_head1.Author_Markup() %>

	<title>Chart Portfolio</title>
	<style>
		.normal {font-family: Arial; font-size: 10pt;}
		.heading {font-family:Verdena; font-size: medium; font-weight:bold; color:midnightblue}
		.date {font-family:Arial; font-size:9pt; font-weight:bold; color:gray}
	</style>
	<script language="vbscript">
	<!--
	' global vars
	dim gPrevSelection, gLastRow
	' constants
	const HDR_COLOR = "#ffcc00"
	const ROW_COLOR1 = "#ffffcc"
	const ROW_COLOR2 = "#d8d8d8"
	' window loading
	sub Window_OnLoad()
		InitSheet
		InitChart
		ChartViewChanged
		ChartTypeChanged
	end sub
	' init the sheet
	sub InitSheet
		' spreadsheet attributes
		Sheet.DisplayGridlines = False
		with Sheet.ActiveSheet
			' sheet attributes
			.Columns.Font.Name = "Arial"
			.Columns.Font.Size = 8
			' format header
			.Rows(1).Font.Bold = true
			'.Rows(1).HAlignment = Sheet.Constants.ssHAlignRight
			.Rows.RowHeight = 20
			' column attibutes
			.Columns.ColumnWidth = 65
			.Columns(2).ColumnWidth = 80
			.Columns(3).ColumnWidth = 30
			.Columns(4).NumberFormat = "Currency"
			.Columns(5).NumberFormat = "Currency"
			.Columns(6).NumberFormat = "Currency"
			.Columns(7).NumberFormat = "Currency"
			'.Columns(1).HAlignment = Sheet.Constants.ssHAlignLeft
			'.Columns(2).HAlignment = Sheet.Constants.ssHAlignLeft
			' header labels
			.Cells(1, 1).Value = "Symbol"
			.Cells(1, 2).Value = "Name"
			.Cells(1, 3).Value = "Qty"
			.Cells(1, 4).Value = "Last price"
			.Cells(1, 5).Value = "Total inv"
			.Cells(1, 6).Value = "Avg price"
			.Cells(1, 7).Value = "Cur value"
		end with
		' hold recordset value
		dim symbol, name, qty, last_price, avg_price
		' current row
		dim row
		row = 2
		' loop through the xml data
		xmlid.recordset.moveFirst()
		if xmlid.recordset.Fields.Count > 1 then
			' if the user has no purchased shares, xmlid.recordset would
			' just return a 1 blank field
			do while not xmlid.recordset.eof
				' get values
				symbol = xmlid.recordset("symbol")
				name = xmlid.recordset("name")
				qty = xmlid.recordset("qty")
				last_price = xmlid.recordset("last_price")
				avg_price = xmlid.recordset("avg_price")
				' update cells
				with Sheet.ActiveSheet
					.Cells(row, 1) = symbol
					.Cells(row, 2) = name
					.Cells(row, 3) = qty
					.Cells(row, 4) = last_price
					.Cells(row, 5) = "=C" & row & "*F" & row
					.Cells(row, 6) = avg_price
					.Cells(row, 7) = "=C" & row & "*D" & row
					' row color
					if row mod 2 = 0 then
						.Range("A" & row & ":G" & row).Interior.Color = ROW_COLOR1
					else
						.Range("A" & row & ":G" & row).Interior.Color = ROW_COLOR2
					end if
				end with
				' move to the next record
				row = row + 1
				xmlid.recordset.moveNext()
			loop
		end if
		' setup the totals section
		gLastRow = row - 1
		with Sheet.ActiveSheet
			.Rows(row).RowHeight = 10
			row = row + 1
			' subtotals
			.Cells(row, 1) = "Subtotals:"
			.Cells(row, 5) = "=sum(E2:E" & gLastRow & ")"
			.Cells(row, 7) = "=sum(G2:G" & gLastRow & ")"
			' net gain / loss
			.Cells(row+1, 1) = "=if(G" & row & "-E" & row & "<0,""Net loss"", ""Net gain"")"
			.Cells(row+1, 2) = "=G" & row & "-E" & row
			.Range("A" & row & ":G" & row+1).Interior.Color = HDR_COLOR
			.Range("A" & row+1 & ":B" & row+1).Font.Bold = true
		end with
	end sub
	' init the chart
	sub InitChart
		dim series
		dim labels
		' add chart
	 	Chart.Clear
		Chart.Charts.Add
		' set attributes
		Chart.DataSource = Sheet
		'Chart.Border.Color = "white"
		Chart.Charts(0).GapWidth = 75
		set series = Chart.Charts(0).SeriesCollection.Add()
		Set labels = series.DataLabelsCollection.Add()
		with labels
			.HasValue = true
			.Position = Chart.Constants.chLabelPositionAutomatic
			.Font.Name = "Tahoma"
			.Font.Size = 8
			.Font.Color = "#555500"
		end with
	end sub
	sub chkBar_OnClick
		ChartTypeChanged
	end sub
	sub ChartTypeChanged
		if (chkBar.checked = true) then
			Chart.Charts(0).Type = Chart.Constants.chChartTypeBarClustered
			Chart.Charts(0).SeriesCollection(0).DataLabelsCollection(0).HasCategoryName = false
		else
			Chart.Charts(0).Type = Chart.Constants.chChartTypePie
			Chart.Charts(0).SeriesCollection(0).DataLabelsCollection(0).HasCategoryName = true
		end if
	end sub
	' chart view clicked
	sub selView_OnClick
		ChartViewChanged
	end sub
	' update the chart
	sub ChartViewChanged
		' determine the column to chart and the title
		dim col, title
		select case selView.selectedIndex
			case 0
				col = "G"
				title = "Value of portfolio"
			case 1
				col = "E"
				title = "Total amount invested"
			case 2
				col = "C"
				title = "Number of shares"
			case 3
				col = "D"
				title = "Latest stock price"
			case 4
				col = "F"
				title = "Average purchase price"
		end select
		' set the title
		lblChartTitle.innerText = title
		' update the chart
		Chart.Charts(0).SeriesCollection(0).SetData _
			Chart.Constants.chDimCategories, 0, "B2:B" & gLastRow
		Chart.Charts(0).SeriesCollection(0).SetData _
			Chart.Constants.chDimValues, 0, col & "2" & ":" & col & gLastRow
	end sub
	' mouse over the chart
	sub Chart_MouseMove(lngMousePosition)
		on error resume next
		' return if the ueser does not want to track
		if chkTrack.checked = false then
			exit sub
		end if
		' determine the selected chart item
		dim objSelection
		set objSelection = Chart.RangeFromPoint(lngMousePosition.x, lngMousePosition.y)
		' return if a bar is not selected
		if TypeName(objSelection) <> "WCPoint" then
			if not (gPrevSelection is nothing) then
				gPrevSelection.Border.Weight = 1
				gPrevSelection.Border.Color = "black"
			end if
			exit sub
		end if
		' restore the previous bar
		if not (gPrevSelection is nothing) then
			gPrevSelection.Border.Weight = 1
			gPrevSelection.Border.Color = "black"
		end if
		' highlight the current bar
	 	objSelection.Border.Weight = 3
	  	objSelection.Border.Color = "yellow"
		' select the stock row in the spreadsheet
		if gPrevSelection.Index <> objSelection.Index then
			dim row
			row = objSelection.Index+2
			Sheet.Range("A" & row & ":G" & row).Select
		end if
		' remember what bar is selected
	  	set gPrevSelection = objSelection
		set objSelection = nothing
	end sub
	-->
	</script>
</head>
<body>
<% _t_begin1.Author_Markup() %>

<span class="heading">Your portfolio</span><br>
<span class="date"><%=Strings.FormatDateTime(Now,DateFormat.LongDate)%></span>
<p></p>
<!-- spreadsheet control
CLSID:0002E510-0000-0000-C000-000000000046
-->
<object classid="CLSID:0002E551-0000-0000-C000-000000000046" id="Sheet" style="HEIGHT: 250px; WIDTH: 100%"></object>
<p>&nbsp</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
  <tr>
    <td width="60%"><span id="lblChartTitle" class="heading"></span></td>
    <td width="5%"></td>
    <td width="35%"><span class="heading">Options</span></td>
  </tr>
  <tr>
    <td>
      <object classid="CLSID:0002E556-0000-0000-C000-000000000046" id="Chart" style="HEIGHT: 250px; WIDTH: 100%"></object>
	</td>
    <td></td>
    <td valign="top">
		<br><span class="normal"><b>Chart view</b></span>
		<select id="selView" name="selView" style="WIDTH: 130px">
			<option selected>Current value</option>
			<option>Total investment</option>
			<option>Quantity</option>
			<option>Last price</option>
			<option>Average price</option>
			</select>
        <p>
		<input id="chkTrack" name="chkTrack" type="checkbox" checked>
		<span class="normal">Track mouseover</span>
		<br>
		<input id="chkBar" name="chkBar" type="checkbox" checked>
		<span class="normal">Display bar chart</span>
		<p></p>
		<span class="normal">To download your portfolio, save this
			page to your local system. Then you can open the page
			at a later time to view your portfolio offline.</span>
    </td>
	</tr>
</table>
</body>
<!-- portfolio data -->
<xml id="xmlid">
<portfolio>

<%
   rs = objAccount.ListPositions(CInt(_t_head1.g_AccountID))
   Do Until rs.EOF
   %>
     <stock>
       <symbol><%=HttpContext.Current.Server.HtmlEncode(Strings.Trim(rs.Fields("ticker").Value))%></symbol>
       <name><%=HttpContext.Current.Server.HtmlEncode(Strings.Trim(rs.Fields("company").Value))%></name>
       <qty><%=HttpContext.Current.Server.HtmlEncode(Strings.Trim(rs.Fields("SharesOwned").Value))%></qty>
       <last_price><%=HttpContext.Current.Server.HtmlEncode(Strings.Trim(rs.Fields("LastPrice").Value))%></last_price>
       <avg_price><%=HttpContext.Current.Server.HtmlEncode(Strings.Trim(rs.Fields("AvgPricePaid").Value))%></avg_price>
     </stock>
   <%
      rs.MoveNext()
   Loop
   rs = Nothing
%>
</portfolio>
</xml>
</html>
