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;

  	getCompanyID()
  
	if(document.calc.To.options[document.calc.To.selectedIndex].value != "" && document.calc.Convert.options[document.calc.Convert.selectedIndex].value != "")
	{
		if(document.calc.To.options[document.calc.To.selectedIndex].value != 1 && document.calc.Convert.options[document.calc.Convert.selectedIndex].value != 1 || document.calc.To.options[document.calc.To.selectedIndex].value == 1 && 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 = rate[document.calc.To.selectedIndex];
		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)");
		  }
		  commR = ( fromR / toR );
  
  		if(document.calc.To.options[document.calc.To.selectedIndex].value == 1 || document.calc.Convert.options[document.calc.Convert.selectedIndex].value == 1)
		{
			if(document.calc.Convert.selectedIndex != document.calc.To.selectedIndex)
		  	{
	  		 	if(document.calc.Convert.selectedIndex == 0)
				{
				  	commR = commR - (commR * (Cmmsn/100))
				}else{
				 	commR = commR + (commR * (Cmmsn/100))
				}
		  	}
		}
  
  		resultV = format(nVal * commR, 2);

  
		if(typeof(currency) == "undefined")
		{
			document.calc.toamount.value = resultV 
		}else{
			document.calc.toamount.value = resultV + " " + currency[document.calc.To.selectedIndex];
 		 }
	}
}

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)
}
