function ConvertCurrency()
{
  // missing relevant digits
  
  var currency = new Array("USD","AUD","BRL","GBP","CAD","CNY","DKK","EUR","HKD","INR","JPY","MYR","MXN","NZD","NOK","SGD","ZAR","KRW","LKR","SEK","CHF","TWD","THB","VEB");
  var country = new Array("US","AU","BR","GB","CA","CN","DK","EU","HK","IN","JP","MY","MX","NZ","NO","SG","ZA","KR","LK","SE","CH","TW","TH","VE");
  var rate = new Array("1","1.12905","1.7583","0.487948","1.0107","7.374","5.0743","0.68018","7.7984","39.29","112.36","3.3085","10.8105","1.2663","5.4079","1.4412","6.695","926.1","108.9","6.4069","1.1355","32.35","30.14","2144.6");
  var euroLakonianBottle500Cost = new Array(0,0.01,0.02,0.03,0.04);
  var euroOreinoBottle500Cost = new Array(0,0.01,0.02,0.03,0.04);
  var bottle500shippingCost = 22;

  var fromFlag = new Array(0,3,1,7,15,10);
  var toFlag   = new Array(7,3,10,9,1,15);
  
  var fromCR, toCR, currencyStr, euProductCost, euShippingCost, nProductCost, nShippingCost, nTotalCost;
  
  fromCR = rate[7]; // we are always converting from euros (EUR)
  toCR = rate[document.order_cost.currency.selectedIndex];
  
  euProductCost = euroLakonianBottle500Cost[document.order_cost.lakonian_500_quantity.selectedIndex]+euroOreinoBottle500Cost[document.order_cost.oreino_500_quantity.selectedIndex];
  euShippingCost = bottle500shippingCost*(document.order_cost.lakonian_500_quantity.selectedIndex + document.order_cost.oreino_500_quantity.selectedIndex);
    
  nProductCost = euProductCost * ( toCR / fromCR );	  	
  nShippingCost = euShippingCost * ( toCR / fromCR );
  nTotalCost = nProductCost +  nShippingCost;
  
  currencyStr = currency[document.order_cost.currency.selectedIndex];
  
  nProductCost = formatCost(nProductCost);
  document.order_cost.product_cost.value = comma(nProductCost) + " " + currencyStr;
  nShippingCost = formatCost(nShippingCost);
  document.order_cost.shipping_cost.value = comma(nShippingCost) + " " + currencyStr;
  nTotalCost = formatCost(nTotalCost);
  document.order_cost.total_cost.value = comma(nTotalCost) + " " + currencyStr;
}

function formatCost(resultV)
{
  // 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);
    }
  }
  
  return resultV;
}

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;
}
