	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(form);
		}
	}

	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(form);
		}
	}

	function ConvertTo (amount)
	{
		return Math.round((exchangeRate * amount)*100)/100;
	}

	function ConvertFrom (amount)
	{
		return Math.round((invExchangeRate * amount)*100)/100;
	}

	function ShowNaNAlert(form) 
	{
		alert('The value entered is not a number, please enter only numeric values. e.g. 34.53');
		form.start.focus();
		form.start.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;
		}

		var fromCell = document.createElement("TD");
		fromCell.innerHTML = fromSymbol + from;

		var middleCell = document.createElement("TD");
		middleCell.innerHTML = '=';
		
		var toCell = document.createElement("TD");
		toCell.innerHTML = toSymbol + to;

		var url = getPermaLink(currency, toCurrency, from);

		var linkCell = document.createElement("TD");
		linkCell.innerHTML += '<a href="' + encodeURI(url) + '" class="PopupLink"><img src="./images/32x32/link.png" title="permalink"/></a>&nbsp;';
		linkCell.innerHTML += '<a href="' + encodeURI(url) + '" class="PopupLink" target="_blank"><img src="./images/32x32/link_new.png" title="Open permalink in a new window"/></a>';

		var row = document.createElement("TR");
		row.appendChild(fromCell);
		row.appendChild(middleCell);
		row.appendChild(toCell);
		row.appendChild(linkCell);

		var tbody = document.getElementById("historyTable").getElementsByTagName("tbody")[0];
		
		// Append at the bottom
		tbody.appendChild(row); 
    }

    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);
		}
		
	}

	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);
		}
	}

	function SelectHistoryRow() 
	{
		//alert("hello");
		var sourceElement = event.srcElement;
		sourceElement.ClassName="Selected";
	}