window.withdraw =
  {
      //MATH: BEGIN
      round_up : function(amount, precision)
        {
          if (isNaN(amount)) return false;

          pow = Math.pow(10, precision);
          amount *= pow;

          word = amount.toString();
          position = word.indexOf('.');

          if (position > 0)
            {
              amount = (((word.charAt(position + 1) >= 5))?Math.ceil(amount):Math.floor(amount));
            }

          amount /= pow;

          return amount;
        },

      round_down : function(amount, precision)
        {
          if (isNaN(amount)) return false;

          pow = Math.pow(10, precision);
          amount *= pow;

          word = amount.toString();
          position = word.indexOf('.');

          if (word.charAt(position + 1) == 5)
            {
              equal = true;
              for (i = position + 2; i < word.length; i++)
                {
                  if ((word.charAt(i)) && (word.charAt(i) > 0)) equal = false;
                }
              amount = ((equal)?Math.floor(amount):Math.ceil(amount));
            }
          else
            {
              amount = ((word.charAt(position + 1) <= 5)?Math.floor(amount):Math.ceil(amount));
            }

          amount /= pow;

          return amount;
        },

      ceil : function(amount, precision)
        {
          if (isNaN(amount)) return false;

          word = amount.toString();
          position = word.indexOf('.');

          equal = true;
          for (i = position + 1; i < Math.min(word.length, precision); i++)
            {
              if ((word.charAt(i)) && (word.charAt(i) > 0)) equal = false;
            }

          amount = ((equal)?Math.floor(amount):Math.ceil(amount));

          return amount;
        },
      //MATH: END

    accounting_amount : function()
      {
        document.getElementById('accounting_fee').innerHTML = '';
        document.getElementById('paysystem_fee').innerHTML = '';
        document.getElementById('paysystem_amount').value = '';

        var regexp_amount = /-?^\d+(\.\d{1,2})?$/;
        if ((document.getElementById('accounting_amount').value.length == 0) || (!regexp_amount.test(document.getElementById('accounting_amount').value)) || (!(document.getElementById('accounting_amount').value > 0)) || (document.getElementById('accounting_amount').value < 1)) return false;

        var amount = parseFloat(document.getElementById('accounting_amount').value);
        var tax_free = parseFloat(document.getElementById('tax_free').value);
        var tax_percent = parseFloat(document.getElementById('tax_percent').value);
        var balance = document.getElementById('balance_amount').value;

        if (balance < amount) return false;

        if (tax_free >= amount)
          {
            var accounting_fee = 0;
          }
        else
          {
            var accounting_fee = window.withdraw.round_up((amount - tax_free) * (tax_percent / 100), 2);
          }

        amount = amount - accounting_fee;

        if (document.getElementById('paysystem_name').value == 'click2pay')
          {
            var paysystem_fee = window.withdraw.round_up(amount * 0.009, 2);
            var paysystem_amount = amount - paysystem_fee;
          }
        else if  (document.getElementById('paysystem_name').value == 'ecocard')
          {
            var paysystem_fee = window.withdraw.round_up(amount * 0.035, 2) + 0.15;
            var paysystem_amount = amount - paysystem_fee;
          }
        /*
        else if  (document.getElementById('paysystem_name').value == 'moneybookers')
          {
            var paysystem_fee = window.withdraw.round_down(amount / 1.01 * 0.01, 2);
            if (paysystem_fee < 0.01)
              {
                paysystem_fee = 0.01;
              }
            else if (paysystem_fee > 0.5)
              {
                paysystem_fee = 0.5;
              }

            var paysystem_amount = amount - paysystem_fee;
          }
        */
        /*
        else if  (document.getElementById('paysystem_name').value == 'neteller')
          {
            var paysystem_fee = ((window.withdraw.round_up(amount * 0.02, 2) <= 078)?0.78:window.withdraw.round_up(amount * 0.02, 2));
            var paysystem_amount = amount - paysystem_fee;
          }
        */
        else if  (document.getElementById('paysystem_name').value == 'webmoney')
          {
            var paysystem_fee = window.withdraw.ceil(window.withdraw.ceil(amount / 1.008 * 100, 10) / 100 * 0.008 * 100, 10) / 100;
            if (paysystem_fee <= 0.01)
              {
                paysystem_fee = 0.01;
              }
            else if (paysystem_fee >= 50)
              {
                paysystem_fee = 50;
              }

            var paysystem_amount = amount - paysystem_fee;
          }
        else
          {
            var paysystem_fee = 0;
            var paysystem_amount = amount;
          }

        document.getElementById('accounting_fee').innerHTML = '&nbsp;- house fee <b>&euro;' + window.withdraw.round_up(accounting_fee, 2) + '</b>';
        document.getElementById('paysystem_amount').value = window.withdraw.round_up(paysystem_amount, 2);
        document.getElementById('paysystem_fee').innerHTML = '&nbsp;- paysystem fee <b>&euro;' + window.withdraw.round_up(paysystem_fee, 2) + '</b>';
      },

    paysystem_amount : function()
      {
        document.getElementById('accounting_amount').value = '';
        document.getElementById('accounting_fee').innerHTML = '';
        document.getElementById('paysystem_fee').innerHTML = '';

        var regexp_amount = /-?^\d+(\.\d{1,2})?$/;
        if ((document.getElementById('paysystem_amount').value.length == 0) || (!regexp_amount.test(document.getElementById('paysystem_amount').value)) || (!(document.getElementById('paysystem_amount').value > 0)) || (document.getElementById('paysystem_amount').value < 1)) return false;

        var amount = parseFloat(document.getElementById('paysystem_amount').value);
        var tax_free = parseFloat(document.getElementById('tax_free').value);
        var tax_percent = parseFloat(document.getElementById('tax_percent').value);
        var balance = document.getElementById('balance_amount').value;

        if (balance < amount) return false;

        if (document.getElementById('paysystem_name').value == 'click2pay')
          {
            var paysystem_fee = window.withdraw.round_up(amount / 0.991 * 0.009, 2);
            amount = amount + paysystem_fee; //window.withdraw.round_up(amount / 0.991, 2);

          }
        else if (document.getElementById('paysystem_name').value == 'ecocard')
          {
            var paysystem_fee = window.withdraw.round_up((amount + 0.15)/0.965 * 0.035, 2) + 0.15;
            amount = amount + paysystem_fee; //window.withdraw.round_up((amount + 0.15)/0.965, 2);
          }
        /*
        else if (document.getElementById('paysystem_name').value == 'moneybookers')
          {
            var paysystem_fee = window.withdraw.round_down((amount / 0.99 * 0.01), 2);
            if (paysystem_fee <= 0.01)
              {
                paysystem_fee = 0.01;
              }
            else if (paysystem_fee >= 0.5)
              {
                paysystem_fee = 0.5;
              }

            amount = amount + paysystem_fee;
          }
        */
        /*
        else if (document.getElementById('paysystem_name').value == 'neteller')
          {
            var paysystem_fee = ((window.withdraw.round_up(amount * 0.02, 2) <= 078)?0.78:window.withdraw.round_up(amount * 0.02, 2));
            amount = amount + paysystem_fee;
          }
        */
        else if (document.getElementById('paysystem_name').value == 'webmoney')
          {
            var paysystem_fee = window.withdraw.ceil(amount * 0.008 * 100, 10) / 100;
            if (paysystem_fee <= 0.01)
              {
                paysystem_fee = 0.01;
              }
            else if (paysystem_fee >= 50)
              {
                paysystem_fee = 50;
              }

            amount = amount + paysystem_fee;
          }
        else
          {
            paysystem_fee = 0;
          }

        if (tax_free >= amount)
          {
            var accounting_fee = 0;
          }
        else
          {
            var accounting_fee = window.withdraw.round_up((amount - tax_free) * (tax_percent / 100) / ((100 - tax_percent) / 100), 2);
          }

        var accounting_amount = amount + accounting_fee;

        document.getElementById('accounting_amount').value = window.withdraw.round_up(accounting_amount, 2);
        document.getElementById('accounting_fee').innerHTML = '&nbsp;- house fee <b>&euro;' + window.withdraw.round_up(accounting_fee, 2) + '</b>';
        document.getElementById('paysystem_fee').innerHTML = '&nbsp;- paysystem fee <b>&euro;' + window.withdraw.round_up(paysystem_fee, 2) + '</b>';
      }
  }