/*	20070412d-probabilities.js		3-1-2009		JavaScript
	Copyright (C)2008-09 Steven Whitney.
	Initially published by http://25yearsofprogramming.com.

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License (GPL)
	Version 3 as published by the Free Software Foundation.
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

JavaScript functions for the stock price movement probabilities calculator.

*/
//----------------------------------------------------------------------------------------------
function SetAllDates()
{
var tdate = new Date();
var dxdays = new Array(0,7,14,0,0,0,0,0,0,0);	// days to add for each interval
var dxmonths = new Array(0,0,0,1,2,3,4,6,9,12);	// months to add for each interval
var d;
for(i = 0 ; i <= 9 ; i++)
{
	d = new Date((tdate.getMonth() + 1 + dxmonths[i]) + "/" + (tdate.getDate() + dxdays[i]) + "/" + tdate.getFullYear());
	d = (d.getMonth() + 1) + "/" + d.getDate() + "/" + d.getFullYear();
	document.getElementById("TargetDateA" + i).value = d;
	document.getElementById("TargetDateB" + i).value = d;
}
}
//----------------------------------------------------------------------------------------------
function CalcAll()
{
var Current, TargetPxA, TargetPxB, Volatility, FractionalYear;
var StartDate = new Date();		// today
var TargetDate;					// various dates in the future
var AllIsOk = true;				// set to false if there are any input or other errors
var i, j;						// loop counters and misc

// Retrieve and validate these values that don't change.
if(isNaN(Current = parseFloat(document.getElementById("StockPriceNow").value.replace(/[^0-9.]+/gi," "))))
{
	AllIsOk = false;
	alert("Error: Current stock price is invalid.");
}

if(isNaN(Volatility = parseFloat(document.getElementById("Volatility").value.replace(/[^0-9.]+/gi," "))))
{
	AllIsOk = false;
	alert("Error: Volatility is invalid.");
}
else 
{
	// The program expects volatility to be entered as a percent. If it doesn't seem to be a %, get user confirmation.
	if(Volatility < 1)	
	{
		j = "The program interprets volatility as a percentage.\n" + 
				Volatility + "% is extremely low.\nClick...\nOK to change it to " + 
				(100 * Volatility) + "%, or\nCancel to leave it as " + Volatility + "%.";
		if(confirm(j))
		{
			Volatility *= 100;
			document.getElementById("Volatility").value = Volatility;
		}
	}
}
Volatility /= 100;
document.getElementById("DailyVolatilityTrading").innerHTML = (100 * Volatility * Math.sqrt(1/252)) + "%";	
document.getElementById("DailyVolatilityCalendar").innerHTML = (100 * Volatility * Math.sqrt(1/365.25)) + "%";	

// Set (to defaults) any required values that the user didn't enter.
if(isNaN(TargetPxA = parseFloat(document.getElementById("TargetPxA").value.replace(/[^0-9.]+/gi," "))))
{
	TargetPxA = Current;
	document.getElementById("TargetPxA").value = TargetPxA;
}
if(isNaN(TargetPxB = parseFloat(document.getElementById("TargetPxB").value.replace(/[^0-9.]+/gi," "))))
{
	TargetPxB = Current;
	document.getElementById("TargetPxB").value = TargetPxB;
}

// Loop through the Above/Below table rows.
// OK - matches Excel
for(i = 0 ; i <= 9 ; i++)
{
	TargetDate = new Date(document.getElementById("TargetDateA" + i).value.replace(/[^0-9/]+/gi," "));
	FractionalYear = CalendarDaysBetween(StartDate,TargetDate) / 365.25;
	if(FractionalYear >= 0)
	{
		document.getElementById("PBelow" + i).innerHTML = (100 * ProbabilityStockPriceBelow(Current, TargetPxA, Volatility, FractionalYear)).toFixed(2);
		document.getElementById("PAbove" + i).innerHTML = (100 * ProbabilityStockPriceAbove(Current, TargetPxA, Volatility, FractionalYear)).toFixed(2);
	}
	else
	{
		document.getElementById("PBelow" + i).innerHTML = "&nbsp;";
		document.getElementById("PAbove" + i).innerHTML = "&nbsp;";
	}
}

// Time to move (days to reach) calculation
// OK - calendar days matches Excel worksheet
i = CalendarDaysToReachPrice16(Current, TargetPxB, Volatility);
document.getElementById("TradingDays1").innerHTML = TradingDaysIn(i).toFixed(2);
document.getElementById("CalendarDays1").innerHTML = i.toFixed(2);

// Price boundaries calculations
// OK - matches Excel. 
for(i = 0 ; i <= 9 ; i++)
{
	TargetDate = new Date(document.getElementById("TargetDateB" + i).value.replace(/[^0-9/]+/gi," "));
	FractionalYear = CalendarDaysBetween(StartDate,TargetDate) / 365.25;
	if(FractionalYear >= 0)
	{
		document.getElementById("LowerBound" + i).innerHTML = LowerLimitPrice84(Current, Volatility, FractionalYear).toFixed(2);
		document.getElementById("UpperBound" + i).innerHTML = UpperLimitPrice84(Current, Volatility, FractionalYear).toFixed(2);
	}
	else
	{
		document.getElementById("LowerBound" + i).innerHTML = "&nbsp;";
		document.getElementById("UpperBound" + i).innerHTML = "&nbsp;";
	}
}

if(!AllIsOk)
	alert("Calculated values may be invalid due to input data errors.");
}
//----------------------------------------------------------------------------------------------

