// Create an array of exchange 'objects'.
function setXrates(theSelect) {
	// Re-init the select's option
	theSelect.options.length = 0;
	for ( var i = 0 ; i< xRates.length ; i++ ) {
		theSelect.options[i] =
			new Option(xRates[i].country, xRates[i].rate);
	}
	return false;
}

// Also, set the legend with the date
function setLegend(theLegend) {
	theLegend.innerHTML =
		"Using rates for: <span style='color:green'>"+textDate+"</span>";
	return false;
}

// Wrap the setXrates & setLegend functions into one function
// called via window.onload.
function startup() {
	setXrates(document.theForm.fxrate);
	setLegend(document.getElementById('dateForRate'));
}

function validated(string) {
     for (var i=0, output='', valid="1234567890."; i<string.length; i++)
        if (valid.indexOf(string.charAt(i)) != -1)
           output += string.charAt(i);
     return output;
}

function formatDecimal(argvalue, addzero, decimaln) {
  var numOfDecimal = (decimaln == null) ? 2 : decimaln;
  var number = 1;
  number = Math.pow(10, numOfDecimal);
  argvalue = Math.round(parseFloat(argvalue) * number) / number;
  // If you're using IE3.x, you will get error with the following line.
  // argvalue = argvalue.toString();
  // It works fine in IE4.
  argvalue = "" + argvalue;
  if (argvalue.indexOf(".") == 0)
    argvalue = "0" + argvalue;
  if (addzero == true) {
    if (argvalue.indexOf(".") == -1)
      argvalue = argvalue + ".";
    while ((argvalue.indexOf(".") + 1) > (argvalue.length - numOfDecimal))
      argvalue = argvalue + "0";
  }
  return argvalue;
}

function formatValue(argvalue, format) {
// use this format: formatValue(1223.434, "$##,###.##")  will return "$1,223.43"
  var numOfDecimal = 0;
  if (format.indexOf(".") != -1) {
    numOfDecimal = format.substring(format.indexOf(".") + 1, format.length).length;
  }
  argvalue = formatDecimal(argvalue, true, numOfDecimal);
  argvalueBeforeDot = argvalue.substring(0, argvalue.indexOf("."));
  retValue = argvalue.substring(argvalue.indexOf("."), argvalue.length);
  strBeforeDot = format.substring(0, format.indexOf("."));
  for (var n = strBeforeDot.length - 1; n >= 0; n--) {
    oneformatchar = strBeforeDot.substring(n, n + 1);
    if (oneformatchar == "#") {
      if (argvalueBeforeDot.length > 0) {
        argvalueonechar = argvalueBeforeDot.substring(argvalueBeforeDot.length - 1, argvalueBeforeDot.length);
        retValue = argvalueonechar + retValue;
        argvalueBeforeDot = argvalueBeforeDot.substring(0, argvalueBeforeDot.length - 1);
      }
    }
    else {
      if (argvalueBeforeDot.length > 0 || n == 0)
        retValue = oneformatchar + retValue;
    }
  }
  return retValue;
}

function getCheckedValue( radio ) {
	for( var button = 0; button < radio.length; button++ )
		{
			if ( radio[button].checked )
				return radio[button].value;
			}
			alert( "No Radio Button checked");
			return "No Radio Button checked";

}

function calcAnswer() {

	var cash = getCheckedValue(document.theForm.cashRate);

	var answer = document.theForm.theRate.value * document.theForm.theAmount.value;
	if (cash=="yes") {

		answer = answer - (4/100 * answer);

	}

	document.theForm.answer.value = formatValue(parseFloat(answer), "###,###,###,###,###.##");
}

 
function changetheRate() {
	var toFromWhich = "";
	var toFrom = getCheckedValue(document.theForm.toFrom);

	var theAmount = document.theForm.fxrate.options[document.theForm.fxrate.selectedIndex].value;
	var dammit = (1/validated(theAmount));

	if (toFrom=="to") {
    	toFromWhich = theAmount;
    } else {
    	toFromWhich = formatValue(dammit, "######################.####");
    }
    document.theForm.theRate.value = toFromWhich;
}

function ChangeText() {

	var stuff = document.getElementById("stuff");
	var summ = document.getElementById("summ");
	var nominal = document.getElementById("nominal");
	var cash = getCheckedValue(document.theForm.cashRate);
	
	var toFromWhich = "";
	var toFrom = getCheckedValue(document.theForm.toFrom);

   var textAmount = document.theForm.theAmount.value;
   var textAnswer = document.theForm.answer.value;
   var textRate = document.theForm.theRate.value;
   var textCurrency = document.theForm.fxrate.options[document.theForm.fxrate.selectedIndex].text;

   if (toFrom=="to" && textCurrency!="Pick currency") {
   		fill = "On " + textDate + ", " + textAmount + " " + textCurrency + "(s) = " + textAnswer + " Canadian dollar(s), at an exchange rate of " + textRate + " ";
   }
    
   if (toFrom=="from" && textCurrency!="Pick currency") {
   		fill = "On " + textDate + ", " + textAmount + " Canadian dollar(s) = " + textAnswer + " " + textCurrency + "(s), at an exchange rate of " + textRate + " ";
   }
   
   if (cash=="yes") {
      cashText = " (using cash rate of 4%.)";
   } else {
      cashText = " (using nominal rate.)";
   }
   
   stuff.firstChild.nodeValue=fill;
   // stuff.style.background="#f7f5f3";
   stuff.style.padding="0.6em";
   stuff.style.color="#444";   
   summ.style.visibility="visible";
   nominal.firstChild.nodeValue=cashText;
}

window.onload=startup;
