function CurrencyFormatted(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function formatCurrency(amount, currencySymbol)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + currencySymbol + s;
	return s;
}

function WriteLayer(ID,parentID,sText) {
 if (document.layers) {
   var oLayer;
   if(parentID){
     oLayer = eval('document.' + parentID + '.document.' + ID + '.document');
   }else{
     oLayer = document.layers[ID].document;
   }
   oLayer.open();
   oLayer.write(sText);
   oLayer.close();
 }
 else if (parseInt(navigator.appVersion)>=5&&navigator.appName=="Netscape") {
   document.getElementById(ID).innerHTML = sText;
 }
 else if (document.all) document.all[ID].innerHTML = sText
} 

function UpdatePricing(country)
{
	var currencySymbol = '$';
	if (country == 'UK') {
		currencySymbol = '&pound;';
	}
	if (document.forms.length > 0)
	{		
		 var QtyElement = document.forms[0].elements['products_pricingtool_count'];		 
		 if (QtyElement) 
		 {
			 var Price;
			 var PriceElement;
		 	 var TotalPrice = 0;
			 var QtyElementName;
			 var PriceElementName;
		 	 var Count = QtyElement.value;
			 for (var LoopVar = 0; LoopVar < Count; LoopVar++)
			 {
			 	 QtyElementName = "products_qty_" + LoopVar;
			 	 QtyElement = document.forms[0].elements[QtyElementName];
				 if (QtyElement) 
				 {
				 	 Qty = QtyElement.value;
				 	 PriceElementName = "products_price_" + LoopVar + "_" + QtyElement.selectedIndex;
				 	 PriceElement = document.forms[0].elements[PriceElementName];
					 if (PriceElement) 
					 {
					 	//Price = PriceElement.value;					 	
			 			//WriteLayer('single_price_' + LoopVar, null, formatCurrency(Price));			 			
				 	 	PriceElementName = "products_price_total_" + LoopVar + "_" + QtyElement.selectedIndex;
					 	PriceElement = document.forms[0].elements[PriceElementName];
					 	if (PriceElement)
					 	{
						 	//Price = Price * Qty;
					 		Price = PriceElement.value;					 	
				 			WriteLayer('price_' + LoopVar, null, formatCurrency(Price, currencySymbol));
				 			Price = parseFloat(Price);
						 	TotalPrice = TotalPrice + Price;
						 	PriceSingle = Price;
						 	if (Qty > 0)
						 	{
						 		PriceSingle = PriceSingle / Qty;
						 	}
			 				WriteLayer('single_price_' + LoopVar, null, formatCurrency(PriceSingle, currencySymbol));			 			
					 	}
					 }
				 }
			 }
			 WriteLayer('TotalPrice', null, formatCurrency(TotalPrice, currencySymbol));
		 }
    }
}
