

var GrandTotal = 0;
var ProfitTotal = 0;
var ProfitPoints = 0;
var TotalTrades = 0;
var CommissionRate = 40;


function market()
{
	var numparams = market.arguments.length;
	this.month = market.arguments[0]; //month;
	this.name = market.arguments[1]; //name;
	this.direction = market.arguments[2]; //direction;
	this.startprice = market.arguments[3]; //startprice;
	this.stopprice = market.arguments[4]; //stopprice;
	this.reverse = market.arguments[5]; //reverse;
	this.cumulative = market.arguments[6]; //cumulative;
	this.pointvalue = market.arguments[7]; //pointvalue;
	this.numtrades = 0;
	if (numparams > 8) { this.numtrades = market.arguments[8]; }
	this.monthtotal = 0;
	this.monthtrades = 0;
	if (numparams > 9) 
		{ this.monthtotal = market.arguments[9]; 
		  this.monthtrades = market.arguments[10];
		}
}

function menu(page)
{
	document.numbers.request.value = page;
	document.numbers.submit();
}


function PrevDay(page)
{
	document.numbers.request.value = page;
	document.numbers.datefile.value = Prevfile;
	document.numbers.submit();
}

function NextDay(page)
{
	document.numbers.request.value = page;
	document.numbers.datefile.value = Nextfile;
	document.numbers.submit();
}

function PrevMonth(page)
{
	document.numbers.request.value = page;
	document.numbers.datefile.value = PrevMonthFile;
	document.numbers.submit();
}

function NextMonth(page)
{
	document.numbers.request.value = page;
	document.numbers.datefile.value = NextMonthFile;
	document.numbers.submit();
}


function CurrDay(page)
{
	document.numbers.request.value = page;
	document.numbers.datefile.value = "defs.js";
	document.numbers.submit();
}
function month_name( symbol )
{

var monthletters = "FGHJKMNQUVXZ";
var c = monthletters.indexOf( symbol )

var months = new Array(12);
months[0] = "Jan";
months[1] = "Feb";
months[2] = "Mar";
months[3] = "Apr";
months[4] = "May";
months[5] = "Jun";
months[6] = "Jul";
months[7] = "Aug";
months[8] = "Sep";
months[9] = "Oct";
months[10]= "Nov";
months[11]= "Dec";

var answer = months[c];
return answer;
}

/* Call function as setCookie("cookiename" , cookievalue, lifetime, cookiepath)
with the lifetime required in days, -1 to delete a cookie or zero
for a temporary cookie. The Cookie Path is optional.*/

function setCookie(cookie_name, cookie_value, cookie_life, cookie_path) {
  var today = new Date()
  var expiry = new Date(today.getTime() + cookie_life * 24*60*60*1000)
  if (cookie_value != null && cookie_value != ""){
    var cookie_string =cookie_name + "=" + escape(cookie_value)
    if(cookie_life){ cookie_string += "; expires=" + expiry.toGMTString()}
    if(cookie_path){ cookie_string += "; path=" + cookie_path}
 	document.cookie = cookie_string
  }
} // Based on JavaScript provided by Peter Curtis at www.pcurtis.com -->

/* Call function as getCookie("cookiename") It returns the value of a cookie
if set or null. Beware of potential ambiguities in names of cookies -
getCookie is simple and will match the end of a string so xyname 
will also be matched by yname and ame. */
 
function getCookie(name) {
  var index = document.cookie.indexOf(name + "=")
  if (index == -1) { return null}
  index = document.cookie.indexOf("=", index) + 1
  var end_string = document.cookie.indexOf(";", index)
  if (end_string == -1) { end_string = document.cookie.length }
  return unescape(document.cookie.substring(index, end_string))
} // Based on JavaScript provided by Peter Curtis at www.pcurtis.com 

function blanktotal()
{
	GrandTotal = 0;
}

function figurebonds( x, seconds)
{
	var c = x.indexOf("-");
	if (c == -1) return x;

	var whole_points = x.substring(0, c); 
	var fraction = x.substring(c+1, x.length); 

	var Total = 0;
	Total = parseInt(whole_points);
	Total = Total * seconds; //alert("Total without fraction " + Total);
	Total += parseInt(fraction);
	//alert( Total );
	return Total;
}

// changes the global value GrandTotal which is a string
function DoCumulative(which)
{
	var dollar_value =  which.pointvalue;
	var GrandProfit = 0;
	GrandProfit = which.cumulative;
	GrandProfit = GrandProfit * dollar_value;
	GrandTotal = GrandProfit.toFixed(2);
	return GrandProfit;
}

function CheckStops(which)
{
	var mkt_direction_long = which.direction;
	var bond_stopprice, bond_reverse;
	if (mkt_direction_long) //first num must be < 2nd num
	{
		if (which.name == "Bonds" || which.name == "TNotes")
		{	//can use 32 for notes and bonds because doesn't matter
			bond_stopprice = figurebonds(which.stopprice, 32);
			bond_reverse = figurebonds(which.reverse, 32);
			if (bond_stopprice > bond_reverse)
				alert(which.name + " prices are reversed");
		}
		else
		{
			if (which.stopprice > which.reverse)
				alert(which.name + " prices are reversed");
		}
	}
	else
	{
		if (which.name == "Bonds" || which.name == "TNotes")
		{
			bond_stopprice = figurebonds(which.stopprice, 32);
			bond_reverse = figurebonds(which.reverse, 32);
			if (bond_stopprice < bond_reverse)
				alert(which.name + " prices are reversed");
		}
		else
		{
			if (which.stopprice < which.reverse)
				alert(which.name + " prices are reversed");
		}
	}
}


// changes the global profittotal which is a string
function DoCalc(which, price)
{
	var dollar_value = which.pointvalue; 
	var mkt_direction_long = which.direction; 
	CheckStops(which);
	
	var x, y;
	var sellprice = 0;
	var buyprice = 0;
	
	if (mkt_direction_long) //we're long
	{
		x = which.startprice;
		y = price;
	}
	else
	{
		x = price;
		y = which.startprice;
	}

	// fix in case of bonds or Tnotes
	if (which.name == "Bonds" || which.name == "TNotes")
	{ 
		if (which.name == "Bonds") 
			{buyprice = figurebonds( x, 32); sellprice = figurebonds( y, 32);} 
		else
			{buyprice = figurebonds( x, 32); sellprice = figurebonds( y, 32);}
	}
	else
	{
		buyprice = x;
		sellprice = y;
	}
	
	var Profit = 0.0;
	Profit = sellprice - buyprice;
	ProfitPoints = parseInt(Profit);
	
	Profit = Profit * dollar_value;
	ProfitTotal = Profit.toFixed(2);
	
	return Profit;
}


function profile_profits( which )
{
	var TotalProfits = 0;
	var dollar_value = 0;
	var profiletrades = 0;

	for( i = 0; which[i] != null; i++)
		{
			dollar_value = which[i].pointvalue;
			TotalProfits += which[i].cumulative * dollar_value;
			profiletrades += which[i].numtrades;
		}
	var Commissions = profiletrades * CommissionRate;
	TotalProfits -= Commissions;
	TotalTrades += profiletrades;
	return TotalProfits;
}

function profile_pending_profits( which )
{
	var TotalProfits = 0;
	var profiletrades = 0;

	for( i = 0; which[i] != null; i++)
		{
			TotalProfits += DoCalc( which[i], which[i].stopprice);
			profiletrades++; // because only one trade is open at a time
//			TotalProfits += DoCumulative( which[i] );
		}
	var Commissions = profiletrades * CommissionRate;
	TotalProfits -= Commissions;
	return TotalProfits;
}


function printtrade(which, tradeprice)
{
var tradedata = "";
var direction = "";
var Profit = 0;
var tempc = 0;
var x, y;
var price = 0;

var numparams = printtrade.arguments.length;
if (numparams == 2)
  price = tradeprice
else
  price = 0;
var month = month_name(which.month);
which.numtrades++;
which.monthtrades++;

//price = tradeprice;
if (price == 0)
	price = which.stopprice; 

// to display buy/sell, do opposite...if we are long, we sell
if (which.direction) {direction = "Sell"} else {direction = "Buy"};

Profit = DoCalc( which, price );
tradedata += "<tr><td width='20%'>";
tradedata += month;
tradedata += " ";
tradedata += which.name;
tradedata += "</td><td width='10%'>";
tradedata += direction;
tradedata += "</td><td width='15%'>";
if (which.direction) //we're long
	{
		x = which.startprice;
		y = price;
	}
	else
	{
		x = price;
		y = which.startprice;
	}

tradedata += x;
tradedata += "</td><td width='15%'>";
tradedata += y;
tradedata += "</td><td width='15%'><p align='right'>";
tradedata += which.numtrades;
tradedata += " / ";
tradedata += ProfitPoints;
tempc = ProfitPoints + which.cumulative;
tradedata += " / ";
tradedata += tempc;
tradedata += "</td><td width='10%'><p align='right'>$";
tradedata += which.pointvalue;
tradedata += "</td><td width='15%'><p align='right'>$";
Profit = ProfitTotal - CommissionRate;
tradedata += Profit.toFixed(2);
tradedata += "</td></tr>";
document.write(tradedata);

//Now that we're done printing, update the in-memory stats

if (which.direction) {which.direction = 0;} else {which.direction =1;}
which.cumulative = tempc;
if (tradeprice == 0) 
{which.startprice = which.stopprice;}
else
{which.startprice = tradeprice;}
price = which.stopprice;  // reuse the price variable;
which.stopprice = which.reverse; which.reverse = price;
return Profit;
}

function printpositions(which)
{
var tradedata = "";
var direction = "";
var Profit = 0;
var profittrades = 0;
var CloseProfit = 0;
var month = month_name(which.month);
if (which.direction) {direction = "L"} else {direction = "S"};
Profit = DoCalc( which, which.stopprice );
Profit -= CommissionRate; // open trades. There is only one
tradedata += "<tr><td width='20%'>";
tradedata += month;
tradedata += " ";
tradedata += which.name;
tradedata += "</td><td width='5%'>";
tradedata += direction;
tradedata += "</td><td width='10%'>";
tradedata += which.startprice;
tradedata += "</td><td width='10%'>";
tradedata += which.stopprice;
tradedata += "</td><td width='10%'>";
tradedata += which.reverse;
tradedata += "</td><td width='10%' align='right'> ";
tradedata += which.cumulative;  //pointvalue;
tradedata += "</td><td width='15%' align='right'>$ ";
ProfitTotal -= CommissionRate;
var printprofit = ProfitTotal.toFixed(2);
tradedata += printprofit;
tradedata += "</td><td width='15%' align='right'>$ ";
DoCumulative(which);
var positioncomm = which.numtrades * CommissionRate;
GrandTotal -= positioncomm;
printprofit = GrandTotal.toFixed(2);
tradedata += printprofit;
tradedata += "</td></tr>";
document.write(tradedata);
return Profit;
}



function print_profile(which)
{
	var TotalProfits = 0;
	var dollar_value = 0;
	var Profits      = 0;
	var ProfileTrades = 0;
	var PrintTotal;
	var Name = which.name;

	
	for( i = 0; which[i] != null; i++)
		{
			dollar_value = which[i].pointvalue;
			TotalProfits += which[i].cumulative * dollar_value;
			ProfileTrades = which[i].numtrades * CommissionRate;
			TotalProfits -= ProfileTrades;
			Profits += printpositions(which[i]);
		}
	PrintTotal = Profits.toFixed(2);
	document.write( "<tr><td colspan='6' align='right'>Totals ");
	document.write("</td><td width='15%' align='right'>$ ");
	document.write(PrintTotal);
	document.write("</td><td width='15%' align='right'>$ ");
	PrintTotal = TotalProfits.toFixed(2);
	document.write(PrintTotal);
	document.write("</td></tr>");
	document.write( "<tr><td colspan='7' align='right'>Total for Both ");
	document.write("</td><td width='15%' align='right'>$ ");
	TotalProfits += Profits;
	PrintTotal = TotalProfits.toFixed(2);
	document.write(PrintTotal);
	document.write("</td></tr>");
	return TotalProfits;
}

function prtdaily(which)
{
var tradedata = "";
var direction;
var month = month_name(which.month);


if (which.direction) {direction = "L"} else {direction = "S"};
tradedata += "<tr><td width='5%'>";
tradedata += direction;
tradedata += "</td><td width='15%'>";
tradedata += month;
tradedata += "</td><td width='30%'>"
tradedata += which.name;
tradedata += "</td><td width='25%'>";
tradedata += which.stopprice;
tradedata += "</td><td width='25%'>";
tradedata += which.reverse;
tradedata += "</td></tr>";
document.write(tradedata);
}

var TeaserTotal = 0;
function teaserline( which, name, margin )
{
   document.write("<tr>");
   document.write("<td width='40%'>");
   document.write(name);
   document.write("</td><td width='28%' align='right'>$");
   var temp = profile_profits(which);
   var display = temp.toFixed(0);
   document.write(display);
   document.write("</td>");
   document.write("<td width='32%' align='right'>$");
   document.write(margin);
   document.write("</td></tr>");
   TeaserTotal += temp;
   return temp;
}

function market_profits(which)
{
	var TotalProfits = 0;
	var dollar_value = 0;
	var ProfileTrades = 0;

	dollar_value =  which.pointvalue;
	TotalProfits += which.cumulative * dollar_value;
	ProfileTrades = which.numtrades * CommissionRate;
	TotalProfits -= ProfileTrades;

	return TotalProfits;
}

function teaserprofile( which )
{
	var ProfileTotal = 0;
	var display = "";

	for (i=0; which[i] != null; i++)
	{
		document.write("<tr>");
		document.write("<td width='40%'>");
		document.write(which[i].name);
		document.write("</td><td width='50%' align='right'>$ ");
		var temp = market_profits(which[i]);
		display = temp.toFixed(0);
		document.write(display);
		document.write("</td></tr>");
		ProfileTotal += temp;
	}
	display = ProfileTotal.toFixed(0);
	document.write("<tr><td width='40%'>");
	document.write("</td><td width='50%' align='right'>");
	document.write("=====");
	document.write("</td></tr>");
	document.write("<tr>");
	document.write("<td width='40%'>");
	document.write("Total");
	document.write("</td><td width='50%' align='right'>$ ");
	document.write(display);
	document.write("</td></tr>");
}

var rack = new Array();

