// (c) Copyright TTT Moneycorp Limited
	function disable_right_click(e)
  {
      var browser = navigator.appName.substring ( 0, 9 );
      var event_number = 0;
	
      if (browser=="Microsoft")
          event_number = event.button;
      else if (browser=="Netscape")
          event_number = e.which;

      if ( event_number==2 || event_number==3 )
          {
          alert ("The source and images on this page are protected by (c) Copyright TTT MoneyCorp Limited. Do not save, download or use any of the source contents of this page.");
          return (false);
          }

      return (true);
  }

  function check_mousekey ()
  {
      var mouse_key = 93;
      var keycode = event.keyCode;

      if ( keycode == mouse_key )
          alert ( "Mouse Key Is Disabled" );
  }

  function trap_page_mouse_key_events ()
  {
      var browser = navigator.appName.substring ( 0, 9 );

      document.onmousedown = disable_right_click;

      if ( browser == "Microsoft" )
          document.onkeydown = check_mousekey;
      else if ( browser == "Netscape" )
          document.captureEvents( Event.MOUSEDOWN );
  }
  
function roundToPennies(n)
{
   pennies = n * 100;
   
   pennies = Math.round(pennies);
   
   strPennies = "" + pennies;

   len = strPennies.length;

   first = strPennies.substring(0, len - 2) + ".";

   last = strPennies.substring(len - 2, len);

   if(first == ".")
   {
      first = "0."
   }

   if(last.length == 1)
   {
      last = "0" + last;
   }
   
   return first + last;
}

function result_onchange() {
	document.MoneyForm.result.value = Comma(document.MoneyForm.result.value);
}

function Comma(number) {
var roundingValue;
var toRound;
var iOriDec;
var idecimal;

	idecimal = 2;
		
   if (idecimal == "undefined") {
	idecimal = 2;
   }
   
   iOriDec = idecimal;
   
   //Extra digit to handle a calculation rounding bug
   idecimal = idecimal + 1
   
   //convert the number into a string
   strNumber = "" + number;
   
   //get the resulting string's length
   len = strNumber.length;

	//Format to the relevant number of decimal places
	bDecimal = ""
	
	for (i = 0; i < len; i++)
	{
		//Look for the dot (.) to get the existing decimal places
		if (strNumber.substr(i, 1) == ".") {
						
			//The rest of the string is the decimal figure
			strDecimal = "" + strNumber.substring(i + 1, len);
			
			lenDec = strDecimal.length;
			
			//Check if the decimal string is too long, and round
			//to the requested number of decimal places
			roundingValue = 1
	
			for (k = 0; k < idecimal; k++){	//idecimal
				roundingValue = roundingValue * 10
			}
			toRound = "0.1" + strDecimal;
			
			strDecimal = Math.round(toRound * roundingValue);
			strDecimal = "" + strDecimal;
			
			ivalue1 = ((strDecimal*1) / 10);
			ivalue2 = (iOriDec*10);
			if (ivalue1==ivalue2) {
				//We have rounded a full number
				bAddOne="true";
			}
			else
			{
				bAddOne="false";
			}
			
			//Get the rounded figure
			strDecimal = strDecimal.substr(1, idecimal);
			
			//Now that we handled the rouding bug, remove the 
			//extra digit
			idecimal = idecimal - 1;
			
			lenDec = strDecimal.length;
						
			//Check if the length of the decimal string is up to the requested
			//no of decimal placess; add zeros if not
			for (g = lenDec; g < idecimal; g++)
			{
				strDecimal = strDecimal + "" + "0";
			}
						
			strCore = strNumber.substring(0, i);
			if (bAddOne=="true") {
				strCore = (strCore*1) + 1;
			}
			bDecimal = "true";
			
		}
		
	}

	if (iOriDec!=idecimal) {
		//Now that we handled the rouding bug, remove the 
		//extra digit
		idecimal = idecimal - 1;
	}
	
	
	//Check if decimal places where found and if not, add zeros to the
	//end of the number
	if (bDecimal != "true") {
		
		strDecimal = "";
			
			for (r = 0; r < idecimal; r++)
			{
				strDecimal = strDecimal + "0";
			}
		
		if (strNumber == 0 || strNumber == null) {
			strNumber = 1;
		}
		
		strNumber = strNumber + "." + strDecimal;
	}
	else
	{
		strNumber = strCore + "." + strDecimal;
	}
	
	//DOUBLE CHECK THE DECIMAL PLACES FORMATING
	//get the number of decimal characters returned into a new string
	len = strNumber.length;

	strDecimal = strNumber.substring(len - (idecimal + 1), len - idecimal);
		
	if (strDecimal == ".") {
	 strDecimal = strNumber.substring(len - (idecimal + 1), len);
	 strNumber = strNumber.substring(0, len - (idecimal + 1));
	}
	else
	{
     strNumber = "" + number;
	 strDecimal = "";
	}
		
	if (strNumber.length > 3) {
		var mod = strNumber.length % 3;
		var output = (mod > 0 ? (strNumber.substring(0,mod)) : '');
		for (i=0 ; i < Math.floor(strNumber.length / 3); i++) {
		if ((mod == 0) && (i == 0))
		output += strNumber.substring(mod+ 3 * i, mod + 3 * i + 3);
		else
		output+= ',' + strNumber.substring(mod + 3 * i, mod + 3 * i + 3);
		}
		 output = "" + output;
		 strNumber = "" + output + strDecimal;
		 
	return strNumber;
	}
	else return (strNumber + strDecimal);

}

function unComma(number) {

   strNumber = "" + number;
   len = strNumber.length;

	strDecimal = strNumber.substring(len - 3, len - 2);
	
	if (strDecimal == ".") {
	 strDecimal = strNumber.substring(len - 3, len);
	 strNumber = strNumber.substring(0, len - 3);
	}
	else
	{
     strNumber = "" + number;
	 strDecimal = "";
	}
   
    strOutput = "";
    
	for (i=0; i < len; i++){
	  if (strNumber.substring(i, i + 1) == ","){
	   strOutput = strOutput + ""
	  }
	  else
	  {
	   strOutput = strOutput + strNumber.substring(i, i + 1);
	  }
	}  
	
 return (strOutput + strDecimal);	
}


function CalcFxRate (rate1, rate2)
{
     if ((rate1 == -1) || (rate2 == -1)) {
            return (-1);
        }
        else {
            return (rate1 / rate2);
        }
}

function Convert()
{
    document.MoneyForm.text1.value = unComma(document.MoneyForm.text1.value);
	if (document.MoneyForm.text1.value == "") {
  			MoneyValue = "1.00";      
	}
	else if (document.MoneyForm.text1.value == "0") {
  			MoneyValue = "1.00";      
	}
	else if (!check(field("text1"),test("allow","0123456789.")).v) {
  			MoneyValue = "1.00";      
	}
	else
	{
			MoneyValue = document.MoneyForm.text1.value;
	}
			UnitPlace = document.MoneyForm.From.selectedIndex;
			Unit2Place = document.MoneyForm.To.selectedIndex;
			UnitValue = document.MoneyForm.From.options[UnitPlace].value;
			Unit2Value = document.MoneyForm.To.options[Unit2Place].value;
			UnitName = document.MoneyForm.From.options[UnitPlace].text;
			Unit2Name = document.MoneyForm.To.options[Unit2Place].text;

			Money = eval(MoneyValue);

			if(UnitName == Unit2Name)
				  {
			   document.MoneyForm.Result.value = roundToPennies(Money);
				  }
			else
			   {
				  FxRate = CalcFxRate(Unit2Value,UnitValue);

			   Money = Money * FxRate;

			   document.MoneyForm.Result.value = roundToPennies(Money);
			   }
			document.MoneyForm.Result.value = Comma(document.MoneyForm.Result.value);
			document.MoneyForm.text1.value = Comma(MoneyValue);
	
}

function text1_onkeypress() {
  var browser = navigator.appName.substring ( 0, 9 );

		if ( browser == "Microsoft" )
		{
		  //CheckKey()
	  }	  
	  	
}


function Reset_onclick() {
 document.MoneyForm.text1.focus()
}