function calculate() 
{
     var select1 = document.currcalc.from_tkc;
     var select2 = document.currcalc.to_tkc;
     var fromval = Number(select1.options[select1.selectedIndex].value);
     var toval = Number(select2.options[select2.selectedIndex].value);
     if (fromval == "" || toval == "") 
     {
		alert('Please select a pair of currencies to calculate exchange rate');
		return false;
     }
     var amount = document.currcalc.amount.value;
     var pattern = /,/g;
     amount = amount.replace(pattern, "");

     if (navigator.appName.indexOf("Netscape") != -1) 
     {
		paintNSCP(fromval, toval, amount);
     } 
       else if (navigator.appName.indexOf("Microsoft") != -1) 
     	{
			paintMSFT(fromval, toval, amount);
     	} 
     	else 
     	{
			alert("This calculator can only be used in Netscape Navigator or Microsoft Internet Explorer, version 4 or greater");
     	}
}

function paintNSCP(fromval, toval, amount) 
{
     var totval = amount * toval / fromval;
     var select1 = document.currcalc.from_tkc;
     var select2 = document.currcalc.to_tkc;

     document.layers['exchrate'].document.write('<NOBR><SPAN CLASS="erate">' + toCurrency(toval / fromval) + ' ' + select2.options[select2.selectedIndex].text + '</SPAN></NOBR>');
     document.layers['exchrate'].document.close();
     document.layers['conversion'].document.write('<NOBR><SPAN CLASS="erate">' + amount + " " + select1.options[select1.selectedIndex].text + " is equal to " + toCurrency(totval) + " "  + select2.options[select2.selectedIndex].text + '</SPAN></NOBR>');
     document.layers['conversion'].document.close();
}

function paintMSFT(fromval, toval, amount) 
{
     var totval = amount * toval / fromval;
     var select1 = document.currcalc.from_tkc;
     var select2 = document.currcalc.to_tkc;

     document.all['exchrate_ie'].innerHTML = '<SPAN CLASS="erate">' + toCurrency(toval / fromval) + ' ' + select2.options[select2.selectedIndex].text + '</SPAN>';
     document.all['conversion_ie'].innerHTML = '<SPAN CLASS="erate">' + amount + " " + select1.options[select1.selectedIndex].text + " is equal to " + toCurrency(totval) + " " + select2.options[select2.selectedIndex].text + '</SPAN>';
}

function changeFromTk() 
{
	var select = document.currcalc.from_tkc;
	if (navigator.appName.indexOf("Netscape") != -1) 
	{
		if (select.options[select.selectedIndex].value != "") 
		{
			document.layers['amountcurr'].document.write('<NOBR><SPAN CLASS="titles3">' + select.options[select.selectedIndex].text + '</SPAN></NOBR>');
		} 
		else 
		{
			document.layers['amountcurr'].document.write('');
		}
		document.layers['amountcurr'].document.close();
	} 
	else 
	{
		document.all['amountcurr_ie'].innerHTML = '<SPAN CLASS="titles3">' + select.options[select.selectedIndex].text + '</SPAN>';
	}
}

function round(num) 
{
	var x = num * 100;
	return (Math.round(x) / 100);
}

function toCurrency(num) 
{
	num = round(num);
	var currstring = num.toString();
	if (currstring.match(/\./)) 
	{
		var curr = currstring.split('.');
	} 
	else 
	{
		var curr = [currstring, "00"];
	}
	curr[1] += "00";
	curr[2] = "";
	var returnval = "";
	var length = curr[0].length;

	// add 0 to decimal if necessary
	for (var i = 0; i < 2; i++)
		curr[2] += curr[1].substr(i, 1);

	// insert commas for readability
	for (i = length; (i - 3) > 0; i = i - 3) 
	{
		returnval = "," + curr[0].substr(i - 3, 3) + returnval;		
	}
	returnval = curr[0].substr(0, i) + returnval + "." + curr[1].substr(0, 4);
	return (returnval);
}
