﻿function eXcell_math(cell)
{

   if(cell)
   {
      this.cell = cell;
      this.grid = this.cell.parentNode.grid
   }
   ;
   this.edit = function()
   {
      this.grid.editor = new eXcell_ed(this.cell);
      this.grid.editor.getValue = function()
      {
         return this.cell.eN
      }
      ;

      this.grid.editor.setValue = this.setValue;
      this.grid.editor.edit()
   }
   ;
   this.isDisabled = function()
   {
      return ! this.grid.DI
   }
   ;
   this.setValue = function(val)
   {
      this.cell.eN = val;
      val = this.grid.MB(val, this.cell);
      if(this.grid.ga[this.cell.A])this.grid.adH(this.cell, this.grid.ga[this.cell.A]).setValue(val);
      else this.dq(val)
   }
   ;
   this.getValue = function()
   {
      if(this.grid.ga[this.cell.A])return this.grid.adH(this.cell, this.grid.ga[this.cell.A]).getValue();

      return this.cell.innerHTML
   }
}
;
eXcell_math.prototype = new gD;
D.prototype._init_point_bm = D.prototype.Yz;
D.prototype.Yz = function()
{
   this.yh =
   {
   }
   ;
   this._aggregators = [];
   this.attachEvent("onClearAll", function()
   {
      this.yh =
      {
      }
      ;
      this._aggregators = []
   }
   );
   this.attachEvent("onCellChanged", function(id, ind)
   {
      if(this.yh[id])
      {
         var cell = this.yh[id][ind];
         if(cell)
         {
            for(var i = 0; i < cell.length; i ++ )this.adH(cell[i]).setValue(this.rO(cell[i]))
         }
      }
      ;
      if( ! this.rX && this._aggregators[ind])
      {
         var qc = this._h2.get[id].parent.id;
         if(qc != 0)
         {
            var ed = this.cells(qc, ind);
            ed.setValue(this.rO(ed.cell))
         }
      }
   }
   );
   this.attachEvent("onAfterRowDeleted", function(id, qc)
   {
      if(qc != 0)if( ! this.rX && this._aggregators.length)
      {
         for(var ind = 0; ind < this._aggregators.length;
         ind ++ )
         {
            if(this._aggregators[ind])
            {
               var ed = this.cells(qc, ind);
               ed.setValue(this.rO(ed.cell))
            }
         }
      }
      ;
      return true
   }
   );
   this.attachEvent("onXLE", function()
   {
      for(var i = 0; i < this._aggregators.length; i ++ )
      {
         if(this._aggregators[i])this._h2.so(0, function(el)
         {
            if(el.bu.length != 0)
            {
               var ed = this.cells(el.id, i);
               ed.setValue(this.rO(ed.cell))
            }
         }
         , this)
      }
   }
   );
   this.Yz = this._init_point_bm;
   if(this.Yz)this.Yz()
}
;
D.prototype.XD = function(status)
{
   this.oX = K(status)
}
;
D.prototype.setMathRound = function(Ki)
{
   this.FR = Ki;
   this.HO = Math.pow(10, Ki)
}
;
D.prototype.enableMathEditing = function(status)
{
   this.DI = K(status)
}
;
D.prototype.rO = function(cell)
{
   if( ! cell.Lq)return cell.innerHTML;
   try
   {
      var agrid = this;
      var z = eval(cell.Lq)
   }
   catch(e)
   {
      return("#SCL")
   }
   ;
   if(this.HO)
   {
      var pre = Math.abs(z) < 1 ? "0" : "";
      z = Math.round(z * this.HO).toString();
      if(z == 0)return 0;
      if(this.FR > 0)
      {
         var n = z.length - this.FR;
         if(n < 0)
         {
            z = ("000000000" + z).substring(9 + n);
            n = 0
         }
         ;
         return(pre + z.substring(0, n) + "." + z.substring(n, z.length))
      }
   }
   ;
 
   return z
}
;
D.prototype._countTotal = function(row, cell)
{
   var b = 0;
   var z = this._h2.get[row];
   for(var i = 0; i < z.bu.length; i ++ )
   {
      if( ! z.bu[i].buff)return b;
      if(z.bu[i].buff.sQ)
      {
         this._h2.so(row, function(el)
         {
            if(el.bu.length == 0)b += this._get_cell_value(el.buff, cell) * 1
         }
         , this);
         return b
      }
      ;
      b += this._get_cell_value(z.bu[i].buff, cell) * 1
   }
   ;
   return b
}
;
D.prototype.MB = function(code, cell)
{

   if( ! code)return code;
   code = code.toString();
   if(code.indexOf("=") != 0)
   {
      this.CL([], cell);
      return code
   }
   ;
   var qL = null;
   code = code.replace("=", "");
   if(code.indexOf("sum") != - 1)
   {
      code = code.replace("sum", "(agrid._countTotal('"+cell.parentNode.idd+"'," + cell.A + "))");
      if( ! this._aggregators)this._aggregators = [];
      this._aggregators[cell.A] = "sum";
      cell.Lq = code;
     
      return this.rO(cell)
   }
   ;
   if(code.indexOf("[[") != - 1)
   {
      var test = /(\[\[([^\,]*)\,([^\]]*)]\])/g;
      var agrid = this;
      qL = qL || (new Array());
      code = code.replace(test, function($0, $1, $2, $3)
      {
         if($2 == "-")$2 = cell.parentNode.idd;
         if($2.indexOf("#") == 0)$2 = agrid.py($2.replace("#", ""));
         qL[linked.length] = [$2, $3];
         return "(agrid.cells(\""+$2+"\"," + $3 + ").getValue()*1)"
      }
      )
   }
   ;
   if(code.indexOf(":") != - 1)
   {
      var test = /:(\w+)/g;
      var agrid = this;
      var id = cell.parentNode.idd;
      qL = qL || (new Array());
      code = code.replace(test, function($0, $1, $2, $3)
      {
         qL[linked.length] = [id, agrid.getColIndexById($1)];
         return '(agrid.cells("'+id+'",agrid.getColIndexById("'+$1+'")).getValue()*1)'
      }
      )
   }
   else
   {
     
      var test = /c([0-9]+)/g;

      var agrid = this;
      var id = cell.parentNode.idd;
      qL = qL || (new Array());
      code = code.replace(test, function($0, $1, $2, $3)
      {
         qL[linked.length] = [id, $1];
         return "(agrid.cells(\""+id+"\"," + $1 + ").getValue()*1)"
      }
      )
   }
   ;
   this.CL(qL, cell);
   cell.Lq = code;
   return this.rO(cell)
}
;
D.prototype.CL = function(ar, cell)
{
   if( ! ar.length)return;
   for(var i = 0; i < ar.length; i ++ )
   {
      if( ! this.yh[ar[i][0]])this.yh[ar[i][0]] =
      {
      }
      ;
      var t = this.yh[ar[i][0]];
      if( ! t[ar[i][1]])t[ar[i][1]] = [];
      t[ar[i][1]].push(cell)
   }
}
;

if(cn)
{
   (function()
   {
      var wi = String.prototype.replace;
      String.prototype.replace = function(search, replace)
      {
         if(typeof replace != "function")
         {
            return wi.apply(this, arguments)
         }
         ;
         var str = "" + this;
         var Eq = replace;
         if( ! (search instanceof RegExp))
         {
            var idx = str.indexOf(search);
            return(idx == - 1 ? str : wi.apply(str, [search, Eq(search, idx, str)]))
         }
         ;
         var reg = search;
         var result = [];
         var yS = reg.lastIndex;
         var re;
         while((re = reg.exec(str)) != null)
         {
            var idx = re.index;
            var CF = re.concat(idx, str);
            result.push(str.slice(yS, idx), Eq.apply(null, CF).toString());
            if( ! reg.global)
            {
               yS += RegExp.lastMatch.length;
               break
            }
            else
            {
               yS = reg.lastIndex
            }
         }
         ;
         result.push(str.slice(yS));

         return result.join("")
      }
   }
   )()
}
;
