function Handler()
{
	Calculatevalue()
	if(document.calc.fromamount.value == "")
	{
		alert("Please fill in the amount to convert.")
		return false
	}
	
}
function getCompanyID()
{
	if(document.calc.CompanyID.value == "")
	{
		document.calc.CompanyID.value = CompanyID
		document.calc.WebUserID.value = UserID
		document.calc.Commission.value = Cmmsn
	}

}
function Calculatevalue()
{
  // missing relevant digits
  var fromR, toR, resultV, nVal, commR;

  
	if(document.calc.Convert.options[document.calc.Convert.selectedIndex].value != "")
	{
		
		Cmmsn = document.calc.Commission.value
		//if(document.calc.Convert.options[document.calc.Convert.selectedIndex].value != 1){
		//	 document.getElementById("buy").src = "http://www.ezforex.com/images/buy_foreigncurrencygray.gif"
		//	document.getElementById("buy").disabled = true
		//  }else{
		 // 	 document.getElementById("buy").src = "http://www.ezforex.com/images/buy_foreigncurrency.gif"
		//	document.getElementById("buy").disabled = false
		 // }
  
  		fromR = rate[document.calc.Convert.selectedIndex];
		toR = 1;
		if(document.calc.toamount.value == "" && document.calc.fromamount.value == "")
		{
			alert("Enter a foreign or US Dollar amount of currency")
			return
		}
		if(document.calc.fromamount.value != "")
		{
			nVal = document.calc.fromamount.value;

			if ( IsNumeric(nVal) == false ) {
			    alert("The amount to multiply is not a number\n\nyou can only use\n\n1234567890 and . (dot)");
				return
			}
			commR =  fromR;
		 	commR = commR + (commR * (Cmmsn/100))
  			resultV = format(nVal * commR, 2);
			document.calc.toamount.value = resultV 
		}else{
		
			nVal = document.calc.toamount.value;

			if ( IsNumeric(nVal) == false ) {
			    alert("The amount to multiply is not a number\n\nyou can only use\n\n1234567890 and . (dot)");
				return
			}
			commR =  fromR;
		 	commR = commR + (commR * (Cmmsn/100))
  			resultV = format(nVal / commR, 2);
			document.calc.fromamount.value = resultV 
		}
	}else{
		alert("Select a country")
		return
	}
}

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

function invertValue()
{
  var i;
  i = document.calc.Convert.selectedIndex;
  document.calc.Convert.selectedIndex = document.calc.To.selectedIndex;
  document.calc.To.selectedIndex = i;
  Calculatevalue();
}

function format(expr, decplaces)
{
	str = "" + Math.round(eval(expr) * Math.pow(10, decplaces))
	while(str.length <= decplaces)
	{
		str = "0" + str
	}
	decpoint = str.length - decplaces
	return str.substring(0, decpoint) + "." + str.substring(decpoint, str.length)
}
