//////////////////////////
// Exchange Rate Helpers
//////////////////////////

function Convert ()
{
	// Text entry boxs must be called start and finish
	var start = getObjectById('start');
	var finish = getObjectById('finish');

	if (!isNaN(start.value)) {
		finish.value = ConvertTo(start.value);

		getObjectById('FromCurrencySymbol').innerHTML = '&nbsp;' + C2Symbol;
		getObjectById('ToCurrencySymbol').innerHTML = '&nbsp;' + C1Symbol;
	}
	else 
	{
	    ShowNaNAlert(start);
	}
}

function InvConvert ()
{
	// Text entry boxs must be called start and finish
	var start = getObjectById('start');
	var finish = getObjectById('finish');

	if (!isNaN(start.value)) 
	{
		finish.value = ConvertFrom(start.value);

		getObjectById('FromCurrencySymbol').innerHTML = '&nbsp;' + C1Symbol;
		getObjectById('ToCurrencySymbol').innerHTML = '&nbsp;' + C2Symbol;
	}
	else 
	{
	    ShowNaNAlert(start);
	}
}

function ConvertTo (amount)
{
	return Math.round((exchangeRate * amount)*100)/100;
}

function ConvertFrom (amount)
{
	return Math.round((invExchangeRate * amount)*100)/100;
}

function ShowNaNAlert(startTextBox) 
{
	alert('The value entered is not a number, please enter only numeric values. e.g. 34.53');
	startTextBox.focus();
	startTextBox.select();
}

// Calculator History functions.
function getObjectById( id )
{
	if (document.getElementById) 
	{
		var returnVar = document.getElementById(id);
	}
	else if (document.all) 
	{
		var returnVar = document.all[id];
	}
	else if (document.layers) 
	{
		var returnVar = document.layers[id];
	}
	return returnVar;
}

function validateInput(textBox) 
{
	var errorBox = getObjectById("ErrorMessage1");

	if (isNaN(textBox.value)) {
		//alert('The value entered is not a number, please enter only numeric values. e.g. 34.53');
		textBox.style.backgroundColor = "#FF0000";
		errorBox.style.visibility = "visible";
		errorBox.style.display = "block"; 
		errorBox.innerHTML = "The value entered is not a number, please enter only numeric values. e.g. 1234.56";
	} 
	else 
	{
		textBox.style.backgroundColor = "#FFFFFF";
		errorBox.style.display = "none";
		errorBox.innerHTML = "";
	}

	var outputBox = getObjectById("finish");
	outputBox.value="";
}

function setupPermaLink(fromCurrency, toCurrency, fromSymbol, from, toSymbol, to, converterUrl) 
{
    var permaLink = document.getElementById("permalink")

    permaLink.title = "Permalink for the conversion of " + fromCurrency + " " + from + " to " + toCurrency;
    permaLink.href = getPermaLink(fromCurrency, toCurrency, from);

    var permalinkSpan = document.getElementById("permalinkSpan");
    permalinkSpan.style.display = "";
}

function AddConverstionToTable(currency, toCurrency, fromSymbol, from, toSymbol, to, converterUrl) 
{
    if (isNaN(to) || isNaN(from)) 
    {
        return false;
    }

    // Get the perma link to use.
    var url = getPermaLink(currency, toCurrency, from);

	var fromCell = document.createElement("TD");
	fromCell.innerHTML += '<a href="' + encodeURI(url) + '" class="PopupLink" target="_blank">' + fromSymbol + from + '</a>';

	var middleCell = document.createElement("TD");
	middleCell.innerHTML = '=';
	
	var toCell = document.createElement("TD");
	toCell.innerHTML = toSymbol + to;

	var notes = document.createElement("TD");
	notes.innerHTML = '<input type="text" value="Add notes here." onclick="clearIfDefault(this);return false;" />';

	var row = document.createElement("TR");
	row.appendChild(fromCell);
	row.appendChild(middleCell);
	row.appendChild(toCell);
	row.appendChild(notes);

	var tbody = document.getElementById("historyTable").getElementsByTagName("tbody")[0];
	
	// Append at the bottom
	tbody.appendChild(row);
}

function clearIfDefault(textBox) {
    if (textBox.value == 'Add notes here.') {
        textBox.value = "" 
    }
}

function getPopupLink(converterUrl, fromCurrency, amount) {
    return converterUrl + '?Base=' + fromCurrency + '&amp;Amount=' + amount + '&amp;Source=' + siteName;
}

function getPermaLink(fromCurrency, toCurrency, amount) {
    return 'ConvertAmount.aspx?FromCurrency=' + fromCurrency + '&ToCurrency=' + toCurrency + '&Amount=' + amount;
}

function ConvertC1toC2() 
{
    try 
	{
	    InvConvert(); 
		
		var startValue = getObjectById('start').value;
		var amount = ConvertFrom(startValue);

		setupPermaLink(C1Currency, C2Currency, C1Symbol, startValue, C2Symbol, amount, ConverterUrl);
		AddConverstionToTable(C1Currency, C2Currency, C1Symbol, startValue, C2Symbol, amount, ConverterUrl);
		
		return false;
	}
	catch (ex)
	{
		alert(ex.description);
	}
	
}

function ConvertC2toC1() 
{
    try
	{
	    Convert(); 
		
		var startValue = getObjectById('start').value;
		var amount = ConvertTo(startValue);
		
		setupPermaLink(C2Currency, C1Currency, C2Symbol, startValue, C1Symbol, amount, ConverterUrl);
		AddConverstionToTable(C2Currency, C1Currency, C2Symbol, startValue, C1Symbol, amount, ConverterUrl);
		
		return false;
	}
	catch (ex)
	{
	    alert(ex.description);
	}
}

function SelectHistoryRow() 
{
	//alert("hello");
	var sourceElement = event.srcElement;
	sourceElement.ClassName="Selected";
}

function UpdateChartByFile(chartName, file) {
    if (document[chartName]) {
        FCObject = document[chartName];
    } else {
        FCObject = window[chartName];
    }

    FCObject.SetVariable('_root.xml_file', file);
}

//////////////////////////
// Charting helpers
//////////////////////////

function UpdateChart(chartName) {
    var DataSource = 'GetRatesForChart.aspx?Base=' + C1Currency + '&To=' + C2Currency + '&Days=' + Days + '&Height=' + ChartHeight + '&Width=' + ChartWidth;

    UpdateChartByFile(chartName, DataSource);
}

function SetCurrencies(chartName) {
    C1Currency = GetSelectedCurrency('C1');
    C2Currency = GetSelectedCurrency('C2');
    UpdateChart(chartName);
}

function GetSelectedCurrency(combo) {
    var currencySelector = document.getElementById(combo);
    return currencySelector.options[currencySelector.selectedIndex].value;
}

function SetDays(chartName, daysHistory) {
    Days = daysHistory;
    UpdateChart(chartName);
}