/*
	Java script for the French version of the currency converter.
*/

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");
/*
	This fameScript snippet is replaced by a separate JavaScript file...
	
<famescript group=$null>
"\	var textDate = """ "%today2" "\"";"
</famescript>

*/	
	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 = "Le " + textDate + ", " + textAmount + " " + textCurrency + "(s) = " + textAnswer + " dollar(s) Canadien(s) selon un taux de change de " + textRate + " ";
    }
    
if (toFrom=="from" && textCurrency!="Pick currency") {
   fill = "Le " + textDate + ", " + textAmount + " dollar(s) Canadien(s) = " + textAnswer + " " + textCurrency + "(s) selon un taux de change de " + textRate + " ";
   }
   
if (cash=="yes") {
   cashText = " (taux argent comptant, 4%)";
   } else {
   cashText = " (taux nominal)";
   }
   
   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;
}
