function formatDecimal(val, n) {
	n = n || 2;
	var str = "" + Math.round ( parseFloat(0+val) * Math.pow(10, n) );
	while (str.length <= n)	{
  		str = "0" + str;
	}
	var pt = str.length - n;
	return str.slice(0,pt); // + "." + str.slice(pt);
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	num = num.toString().replace(/^\s+|\s+$/g,'');
	if(isNaN(num))
		num = "0";

	return num;
}

function cal(con, id) {
	var price = "";
	var cost = "";
	var a = "price" + id;
	var b = "cost" + id;
	var aed = "aed" + id;
	var usd = "usd" + id;
	var gbp = "gbp" + id;
	var eur = "eur" + id;

	if(con=="AED") {
		cost = formatCurrency(document.getElementById(a).value);
		price = formatDecimal(cost, 2);
		document.getElementById(b).innerHTML = "<b>AED " + price + "</b>";
		document.getElementById(aed).src="/dssimg/aed1.gif";
		document.getElementById(usd).src="/dssimg/usd.gif";
		document.getElementById(gbp).src="/dssimg/gbp.gif";
		document.getElementById(eur).src="/dssimg/eur.gif";
	}
	if(con=="USD") {
		cost = formatCurrency(document.getElementById(a).value);
		price = formatDecimal(cost * usd1, 2);
		document.getElementById(b).innerHTML = "<b>USD " + price + "</b>";
		document.getElementById(aed).src="/dssimg/aed.gif";
		document.getElementById(usd).src="/dssimg/usd1.gif";
		document.getElementById(gbp).src="/dssimg/gbp.gif";
		document.getElementById(eur).src="/dssimg/eur.gif";

		return;
	}
	if(con=="GBP") {
		cost = formatCurrency(document.getElementById(a).value);
		price = formatDecimal(cost * gpb1, 2);
		document.getElementById(b).innerHTML = "<b>GBP " + price + "</b>";
		document.getElementById(aed).src="/dssimg/aed.gif";
		document.getElementById(usd).src="/dssimg/usd.gif";
		document.getElementById(gbp).src="/dssimg/gbp1.gif";
		document.getElementById(eur).src="/dssimg/eur.gif";

		return;
	}
	if(con=="EUR") {
		cost = formatCurrency(document.getElementById(a).value);
		price = formatDecimal(cost * eur1, 2);
		document.getElementById(b).innerHTML = "<b>EUR " + price + "</b>";
		document.getElementById(aed).src="/dssimg/aed.gif";
		document.getElementById(usd).src="/dssimg/usd.gif";
		document.getElementById(gbp).src="/dssimg/gbp.gif";
		document.getElementById(eur).src="/dssimg/eur1.gif";

		return;
	}
	return;
}

function convertRates(con, count) {
	var price = "";
	var cost = "";
	var id = 0;
	if(count > 0)
	{
		var aed = "aedRate";
		var usd = "usdRate";
		var gbp = "gbpRate";
		var eur = "eurRate";

		if(con=="AED") {
			document.getElementById(aed).src="/dssimg/aed1.gif";
			document.getElementById(usd).src="/dssimg/usd.gif";
			document.getElementById(gbp).src="/dssimg/gbp.gif";
			document.getElementById(eur).src="/dssimg/eur.gif";
			
			document.getElementById("selectedRate").innerHTML="<strong>(AED)</strong>";
		}

		if(con=="USD") {
			document.getElementById(aed).src="/dssimg/aed.gif";
			document.getElementById(usd).src="/dssimg/usd1.gif";
			document.getElementById(gbp).src="/dssimg/gbp.gif";
			document.getElementById(eur).src="/dssimg/eur.gif";

			document.getElementById("selectedRate").innerHTML="<strong>(USD)</strong>";
		}

		if(con=="GBP") {
			document.getElementById(aed).src="/dssimg/aed.gif";
			document.getElementById(usd).src="/dssimg/usd.gif";
			document.getElementById(gbp).src="/dssimg/gbp1.gif";
			document.getElementById(eur).src="/dssimg/eur.gif";

			document.getElementById("selectedRate").innerHTML="<strong>(GBP)</strong>";
		}

		if(con=="EUR") {
			document.getElementById(aed).src="/dssimg/aed.gif";
			document.getElementById(usd).src="/dssimg/usd.gif";
			document.getElementById(gbp).src="/dssimg/gbp.gif";
			document.getElementById(eur).src="/dssimg/eur1.gif";

			document.getElementById("selectedRate").innerHTML="<strong>(EUR)</strong>";
		}

		for(id = 1; id <= count; id++)
		{
			var a = "priceRate" + id;
			var b = "costRate" + id;
		
			if(con=="AED") {
				cost = formatCurrency(document.getElementById(a).value);
				price = formatDecimal(cost, 2);
				document.getElementById(b).innerHTML = price ;
			}
			if(con=="USD") {
				cost = formatCurrency(document.getElementById(a).value);
				price = formatDecimal(cost * 0.272274, 2);
				document.getElementById(b).innerHTML = price;
			}
			if(con=="GBP") {
				cost = formatCurrency(document.getElementById(a).value);
				price = formatDecimal(cost * 0.136262, 2);
				document.getElementById(b).innerHTML = price;
			}
			if(con=="EUR") {
				cost = formatCurrency(document.getElementById(a).value);
				price = formatDecimal(cost * 0.202520, 2);
				document.getElementById(b).innerHTML = price;
			}
		}
	}
	return;
}
