
//var rate = new Array("1","1.35906","4.37445","2.94","0.579039","1.3045","8.2769","6.121","0.822571","7.7635","222.39","45.66","107.78","3.8","11.205","1.55087","6.6515","1.7145","6.31","1191","96.5","7.3635","1.2755","34.11","39.92","1600");
//var currency = new Array("USD","AUD","BWP","BRL","GBP","CAD","CNY","DKK","EUR","HKD","HUF","INR","JPY","MYR","MXN","NZD","NOK","SGD","ZAR","KRW","LKR","SEK","CHF","TWD","THB","VEB");

function Cvalue()
{
  
  var fromR, toR, resultV, nVal;
  
  fromR = rate[document.forms[0].from.selectedIndex];
  toR = rate[document.forms[0].to.selectedIndex];
  nVal = document.forms[0].inV.value;
  
  if ( IsNumeric(nVal) == false ) {
    alert("amount to multiply is not a number\n\nyou can only use\n\n1234567890 and . (dot)");
  }
  
  resultV = nVal * ( toR / fromR );

  // 6 relevant digits only, or integer 
  if ( (resultV == parseInt(resultV)) || (resultV > 99999) )
  {
    // mostly integer
    resultV = parseInt( resultV );
  }
  else
  {
    if (resultV > 1)
    {
	resultV = resultV.toString();
	resultV = resultV.substring(0,7);
    } else {
	resultV = resultV.toString();
	resultV = resultV.substring(0,8);
    }
  }
result.innerHTML = "   " + comma(resultV) + " " + currency[document.forms[0].to.selectedIndex]; 
}

function invertCurr()
{
  var i;
  i = document.forms[0].from.selectedIndex;
  document.forms[0].from.selectedIndex = document.forms[0].to.selectedIndex;
  document.forms[0].to.selectedIndex = i;
  Cvalue();
}

function comma(num)
{
 var n = Math.floor(num);
 var myNum = num + "";
 var myDec = ""
 
 if (myNum.indexOf('.',0) > -1){
  myDec = myNum.substring(myNum.indexOf('.',0),myNum.length);
 }
 var arr=new Array('0'), i=0; 
 while (n>0) 
   {arr[i]=''+n%1000; n=Math.floor(n/1000); i++;}
 arr=arr.reverse();
 for (var i in arr) if (i>0) //padding zeros
   while (arr[i].length<3) arr[i]='0'+arr[i];
 return arr.join() + myDec;
}

function IsNumeric(strString)
{
   var strValidChars = "0123456789.";
   var strChar;
   var blnResult = true;

   for (i = 0; i < strString.length && blnResult == true; i++)
   {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
      {
         blnResult = false;
      }
   }
   return blnResult;
}
