function comissionObj_toString()
{
   if (this.perc > 0.00001)
       s = '&nbsp;-' + Math.round(this.perc * 100 * 10000)/10000 + '%';
   else if (this.perc < -0.000001)
       s = '&nbsp;+' + Math.round((-this.perc) * 100 * 10000)/10000 + '%';
   else
       s = '';

   if (this.fix > 0)
       s = s + '&nbsp;-' + this.fix + '&nbsp;'+ this.code;

   if (this.min > 0)
s = s + ' не&nbsp;менее&nbsp;' + this.min + '&nbsp;' + this.code;

   if (this.max > 0)
s = s + ' не&nbsp;более&nbsp;' + this.max + '&nbsp;' + this.code;

   if (s == '') s = msg_no_comission;

   return '<nobr>'+s+'</nobr>';
}

// apply comission to input value, return how much you will gain
// (case: any input system where receiver pays, ex: e-gold)
function comissionObj_afterComission(inval, last_comm)
{
    if (this.calc_comm_diff) {
        percinval = inval - this.from_amount;
        ru_post_coeff = this.from_amount * this.perc;
    } else {
        percinval = inval;
        ru_post_coeff = 0;
    }
    
    
    if ((last_comm != undefined) && ((true == last_comm) || (1 == last_comm))) {
        amount = (inval - this.fix + ru_post_coeff) / (1 + this.perc);
        commission = inval - amount;
        if (commission < this.min && this.min > 0) {
            commission = this.min;
        }
        if (commission > this.max && this.max > 0) {
            commission = this.max;
        }
        amount = inval - commission;
        
        if (amount < 0) {
            amount = 0;
        }
        
        return amount;
    } else {
       // no matter who pays, the output value is less.

       // subtract comission
       if (inval == 0) return 0;

       if (inval < this.fix) return 0;

       if (inval < this.min) return 0;
       
       if ((percinval * (1 - this.perc)) < this.fix) commsize = (percinval * (this.perc) );
       else commsize = (percinval * (this.perc) ) + this.fix;
       outval = inval - commsize;

       if (inval < outval) return outval; // special case for 'negative' percents, no min/max limits for comission.

       if (inval - outval >= this.min)
       {
           if (this.max > 0 && (inval - outval > this.max))
               return inval - this.max;
           return outval;
       } else {
           return inval - this.min;
       }
   }
}

// returns how much you must pay to get this output value
// (case: any output system where sender pays, ex: WM)
function comissionObj_reverseComission(outval, last_comm)
{
   if (this.perc >= 1) return 0;
   
   if ((last_comm != undefined) && ((true == last_comm) || (1 == last_comm))) {
           if (this.calc_comm_diff) {
            amount = outval * (1+this.perc) - this.from_amount * this.perc + this.fix;
        } else {
            amount = outval * (1+this.perc) + this.fix;   
        }
        commission = amount - outval;
        if (commission < this.min && this.min > 0) {
            commission = this.min;
        }
        if (commission > this.max && this.max>0) {
            commission = this.max;
        }
        amount = commission + outval;
        if (amount < 0) {
            amount = 0;
        }
        return amount;            
   } else {
       if (this.calc_comm_diff) {
           res = (outval - (this.perc) * this.from_amount + this.fix) / (1 - this.perc);
       } else {
           res = (outval + this.fix) / (1 - this.perc);
       }
       
       if (this.min > 0 && res - outval < this.min) return outval + this.min;
       if (this.max > 0 && res - outval > this.max) return outval + this.max;
       return res;
   }
}

function comissionObj_hasError()
{
   return this.error != '';
}

function comissionObj_getError()
{
   return this.error;
}

function comissionObj(code, from_amount, to_amount, perc, fix, min, max, calc_comm_diff)
{

   this.code        = code; 
   this.from_amount = from_amount;
   this.to_amount   = to_amount;
   this.perc        = perc;
   this.fix         = fix;
   this.min         = min;
   this.max         = max;
   this.calc_comm_diff = calc_comm_diff;

   this.error       = '';

   //this.toString    = comissionObj_toString;
   this.afterComission = comissionObj_afterComission;
   this.reverseComission = comissionObj_reverseComission;
   this.hasError    = comissionObj_hasError;
   this.getError    = comissionObj_getError;
   return this;
}
var nullComission = new comissionObj('',0,0,0,0,0,0);
