function onSymbolChange(object)
{
	// Selected symbol changed, so redirect page
	if (!object)  return;
	// Try to get the current chart period too
	var periodSelect = document.getElementById('period-select');
	var period = '';
	if (periodSelect)  period = periodSelect.options[periodSelect.selectedIndex].value;
	var sym = object.options[object.selectedIndex].value;
	if (sym == 'ALLQUOTES')  self.location.href = '/forex/real-time-forex-quotes.php';
	else if (sym)  self.location.href = '/forex/free-forex-charts.php?sym=' + sym + '&period=' + period;
}

function onPeriodChange(object)
{
	// Chart Period changed, so redirect page
	var symbolSelect = document.getElementById('symbol-select1');
	if (!object || !symbolSelect)  return;

	// Try to find the selected symbol
	var sym = symbolSelect.options[symbolSelect.selectedIndex].value;
	var period = object.options[object.selectedIndex].value;
	if (sym && period)  self.location.href = '/forex/free-forex-charts.php?sym=' + sym + '&period=' + period;
}

function onPortfolioChange()
{
	// Selected portfolio changed, so change the Amount drop-down options
	var object = document.getElementById('portfolio-select');
	if (!object)  return;
	var portidLotsize = object.options[object.selectedIndex].value;
	var amountSelect = document.getElementById('amount-select');
	if (!amountSelect)  return;

	var i, amtTmpText, amtTmpValue, lotSize = 10000;  // default Lot Size
	if (portidLotsize != 0) {
		// Try to extract the Lot Size like 10000 from 103247_10000
		var x = portidLotsize.indexOf("_");
		if (x > 0)
			lotSize = portidLotsize.substr(x + 1);
	}

	// Update the AMOUNT drop-down box
	deleteOptions(amountSelect);
	for (i=1; i<=20; i++) {  // 20 sets of amounts
		amtTmpValue = i * lotSize;
		amtTmpText = (amtTmpValue / 1000) + 'K';  // like 10K instead of 10000
		insertNewOption(amountSelect, amtTmpText, amtTmpValue)
	}
}

function reloadPage()
{
	// Get chart options and reload page
	var sym = '', period = '';
	var periodSelect = document.getElementById('period-select');
	var symSelect = document.getElementById('symbol-select1');
	if (periodSelect)  period = periodSelect.options[periodSelect.selectedIndex].value;
	if (symSelect)  sym = symSelect.options[symSelect.selectedIndex].value;
	if (sym && period)  self.location.href = '/forex/free-forex-charts.php?sym=' + sym + '&period=' + period;
	else self.location.href = '/forex/free-forex-charts.php';
}

function refreshChart()
{
	var tmp = findSWF("chart");
	try { // this may fail the first time on IE6
		var x = tmp.reload(false, false);  // reload the chart data and options from an external file; do NOT show the loading message
	} catch (errMsg) {}

	setTimeout("refreshChart()", 5000);
}

/* Fixed function to work with IE7: http://www.cyberdyne.org/~icebrkr/2008/01/03/open-flash-charts/ */
function findSWF(movieName) {
	if (navigator.appName.indexOf("Microsoft")!= -1) {
		if (window[movieName])  return window[movieName];  // IE6-
		else return document.getElementById("ie_"+movieName);  // IE7+
		//return window["ie_" + movieName];
	} else {
		//return document[movieName];
		return document.getElementById(movieName);
	}
}

function insertNewOption(object, newText, newValue)
{
	myOption=new Option();
	myOption.text=newText;
	myOption.value=newValue;
	var insertIndex;
	if (object.selectedIndex>0)
		insertIndex=object.selectedIndex;
	else insertIndex=object.options.length;
	object.options[insertIndex]=myOption;	
}

function deleteOptions(object)
{ 
	var deleteIndex;
	while (object.options.length>0)
	{
		deleteIndex=object.options.length-1;
		object.options[deleteIndex]=null;
	}
}
