



  var reg = /[\s]/g;



  function findPos(obj) {

  	var curleft = curtop = 0;

  	if (obj.offsetParent)

  	do {

  			curleft += obj.offsetLeft;

  			curtop  += obj.offsetTop;

  	} while (obj = obj.offsetParent);

  	return [curleft,curtop];

  }



  function generateSelectOptions(arr, selected) {

    var tmp = '';

    for(x in arr) {

      tmp += '<option value="'+x+'" '+((arr[x]===selected)?'selected':'')+'>'+arr[x]+'</option>';

    }

    return tmp;

  }



  

  function NumberToUserFormatNumber(number,add, len, zeros) {
	

	    if(!number) number = 0; 

		if (!len) {len=2; zeros=2;}
    
		number = parseFloat(number);
		
    //var tmp = number.toFixed(OPT['dec_len']);
	    var tmp = number.toFixed(len);

    //var s1 = tmp.substring(0,tmp.length-1-OPT['dec_len']);
	var s1 = tmp.substring(0,tmp.length-1-len);
	
	if (zeros==true) return s1;
	

    //var s2 = tmp.substring(tmp.length-OPT['dec_len'],tmp.length);
	var s2 = tmp.substring(tmp.length-len,tmp.length);
		
    var tmp = '';

    for(var i=s1.length;i>0;i-=3) {

     // tmp = ((i<=3)?"":OPT['sep_1000'])+s1.substring(i-3,i)+tmp;
	  tmp=s1.substring(i-3,i)+tmp;
	  if (i>3) tmp = '.' + tmp;
    }
    s1 = tmp;
	
	if (s2.length>zeros) {
		var cut = s2.length;
		for (var i = s2.length-1; i>zeros-1;i--) {
			if (s2[i]=='0') cut--; else  break;
		}
		s2 = s2.substring(0,cut);
	}
	//console.log(s2);
    //return (s1+OPT['dec_sep']+s2).toString()+((add)?add:'');
	return (s1+','+s2).toString()+((add)?add:'');

  } 





  function UserFormatNumberToNumber(ufn,add) {

    if(add) {

      var match = /add/g;

      ufn = ufn.replace(match,'');

    }

    var match = /Err/g;

    ufn = ufn.replace(match,'');

    if(!ufn) return parseFloat(0);

    var pos = ufn.indexOf(OPT['dec_sep']);

    var s1='', s2='';

    if(pos==-1) { s1 = ufn; s2 = ''; }

      else { s1 = ufn.substring(0,pos); s2 = ufn.substring(pos+1,ufn.length); }


/*
    if(OPT['sep_1000'] != "")

      for(var i=s1.length-4;i>=0;i-=4)

        if(s1.charAt(i) != OPT['sep_1000'])  { return -1; }

    if(s1.charAt(0) == OPT['sep_1000']) return -1;
*/


    var pos = -1;

    while((pos = s1.indexOf(OPT['sep_1000'])) != -1)

      s1 = s1.substring(0,pos)+s1.substring(pos+1,s1.length);



    //alert(s1);

    return parseFloat(s1+"."+s2);

  }





  function keyPressedNumber(e) {

    var keynum;

    if(window.event) //IE 

      keynum = e.keyCode;

    else

      keynum = e.which;

    return keynum;    

  }



  function isEnterOrTabPressed(e) {

    var keynum = keyPressedNumber(e);

    if(keynum == 9 || keynum == 13) return true; else return false;

  }

  

  function setSelectionRange(obj) {

    if(obj && typeof(obj) == "object" && (obj.type == "text" || obj.type == "textarea")) {

      if(obj.createTextRange) {

        var range = obj.createTextRange();

        range.moveStart("character", 0);

        range.moveEnd("character", obj.value.lengh-1);

        range.select();

      } else { 

        if(obj.setSelectionRange) {

          obj.setSelectionRange(0,obj.value.length);

        }

      }

      obj.focus();

    }

    if(obj && typeof(obj) == "object" && obj.options) { obj.focus(); }

  }

/*

  function keyPressed = function(e, method) {

      var keynumkey = PressedNumber(e);

      if((keynum == 9) || (keynum == 13)) {

        //this.calculateTotal();           

        //if(this.selectedCell.lp == 1) setTimeout( function() { N.selectNextCell(); } , 100); 

        //this.selectNextCell(); 

        return false;

      }

      //if(keynum == 40) this.selectNextRow();

      //if(keynum == 38) this.selectPreviousRow();



      if(e.shiftKey && (method == "decimalNumber" || method == "onlyNumber")) return false; 

      if(method == "decimalNumber") return this.OnlyNumbers(keynum);

      if(method == "onlyNumber") return this.OnlyNumbers(keynum, true);

      return true;

  } 

  

    this.OnlyNumbers = function(e, noQuote) {



      var keynum = e, keychar, numcheck;



      keychar = String.fromCharCode(keynum);



      numcheck = /\d/;



      return numcheck.test(keychar) 



             || ((!noQuote)?(keynum == 190):false)



             || (keynum == 8) //backspace



             || (keynum == 13) //enter



             || (keynum == 0) //special keys with FF



             || (keynum == 37) //left arrow



             || (keynum == 39) //right arrow



             || (keynum == 188) //,
			 || (keynum >= 95 && keynum <= 105) //numeric keyboard
			 || (keynum == 110);



    } 



*/



 

  function MyTable(name) {

  

    this.myTableName = name;

    this.table = document.getElementById(this.myTableName);

    this.thead = this.table.tHead;

    this.tbody = this.table.tBodies.item(0);

    this.cellSelectedClass = 'selectedCell';

    this.rowSelectedClass = 'selectedRow';

    this.selectedRow;

    this.selectedCell;

	this.divParent;


    this.rowCount = function() {

      return this.tbody.rows.length;

    }



    this.colCount = function() {

      return this.thead.rows.item(0).cells.length;

    };



    this.colWidth = function(i) {

      return this.thead.rows.item(0).cells.item(i).width;

    };



    this.moveUpRow = function() {

      if(this.selectedRow) this.selectedRow.moveUp(); 

    };

    this.moveDownRow = function() {

      if(this.selectedRow) this.selectedRow.moveDown(); 

    };



    this.insertRow = function(row, newRow) {

      if(!row)

        if(this.rowCount())

        if(typeof(row) == "number") 

          row = this.tbody.rows.item(row);

        else 

          row = this.tbody.rows.item(this.tbody.rows.length-1);

          

      var row_tmp;

      if((newRow) && (row)) row_tmp = newRow; else { row_tmp = this.createRow(); this.fillWithDefaultData(row_tmp); }  

      if(this.rowCount() > 0 && row.nextSibling) 

        this.tbody.insertBefore(row_tmp, row.nextSibling);

      else 

        this.tbody.appendChild(row_tmp); 

      return row_tmp;     

    };



/*

    this.deleteRow = function(row) {



      if(!row)



        if(this.rowCount())



        if(typeof(row) == "number") 



          row = this.tbody.rows.item(row);



        else 



          row = this.tbody.rows.item(this.tbody.rows.length-1);



      if(!row) return;



      if(row.nextSibling) this.selectNextRow();



      else if(row.previousSibling) this.selectPreviousRow();



      



      this.tbody.removeChild(row);



      this.deselectRow();



      this.deselectCell();



      



      if(this.rowCount() == 0)



        setTimeout( function() { N.insertRow(); } , 1000); 



    }

    this.changeRowMode = function(row) {



      row.isnew = !row.isnew;



      this.setEditNames(row,!row.isnew);



      var img = row.firstChild.nextSibling.lastChild.previousSibling.previousSibling;



      if(row.isnew)



        img.setAttribute('src','modules/EcmInvoiceOuts/images/newproductset.gif');



      else



        img.setAttribute('src','modules/EcmInvoiceOuts/images/newproduct.gif');



    }

*/

    



    this.refreshRowIndex = function() {

      for(var i=0; i<this.rowCount(); i++) {

        this.tbody.rows.item(i).index = i;

        if(this.onRefreshRowIndex) this.onRefreshRowIndex(this.tbody.rows.item(i));

      }

    }

    this.onRefreshRowIndex;



    this.addRow = function(i,data) {

	  if(this.rowCount() > 0) {
		try {
			if(typeof(i) != "undefined") {
				var r = i;
				if(typeof(i) == "object") {}
				else if(typeof(i) == "number") r = this.tbody.rows.item(i - 1);
				if(this.isProductLoaded(r.cells.item(0)) == false) { return 0; }
			}
		} catch(e) { return 0; }
	  }
	  
      var row = this.createRow();

      if(this.selectedRow) this.selectedRow.deselect();

      if(this.selectedCell) this.selectedCell.deselect();

      row.myTable = this;

      if(typeof(i) == "number")

        this.tbody.insertBefore(row,this.tbody.rows.item(i));

      else

        this.tbody.appendChild(row);

      this.refreshRowIndex();

      this.setRowData(row, data);

      for(var i=0; i<this.colCount(); i++) row.cells.item(i).afterCreate();

      return row;

    }



    this.createRow = function(row) {

      var row = document.createElement('tr');

      row.myTable = this;

      row.isnew = false;

      row.onclick = function() { this.select(); }

      row.select = function() { 

        if(!this.myTable.selectedRow || this.myTable.selectedRow !== this) {

          if(this.myTable.selectedRow) this.myTable.selectedRow.deselect();

          this.myTable.selectedRow = this; 

          this.className = this.myTable.rowSelectedClass; 

          if(row.onSelect) row.onSelect();
		  
		  if(this.myTable.divParent)
		  if(((this.offsetTop+this.offsetHeight-this.myTable.divParent.scrollTop) > (this.myTable.divParent.offsetHeight+5)) || ((this.offsetTop-this.myTable.divParent.scrollTop) < (-10))) this.myTable.divParent.scrollTop = this.offsetTop;

        }

      }

      row.deselect = function() {

        if(this.myTable.selectedRow === this) {

          this.className = '';

          this.myTable.selectedRow = '';

          if(row.onDeselect) row.onDeselect();

        }

      };

      row.calculateTotal = function() {};

      row.selectNext = function() {

        if(this.index < this.myTable.rowCount()-1) { this.deselect(); this.nextSibling.select(); return this.nextSibling; }
        else {
			if(this.noAddNew) return this;
			   
			this.deselect();
           	var row = this.myTable.addRow(); return row; 
        }

      }

      row.selectPrevious = function() {

        this.deselect();

        if(this.previousSibling) { this.previousSibling.select(); return this.previousSibling; }else return this;

      }

      row.deleteRow = function(noNew) {

        if(this.myTable.selectedCell) this.myTable.selectedCell.deselect();

        if(this.myTable.selectedRow) this.myTable.selectedRow.deselect();

        if(this.myTable.rowCount() == 1 && !noNew) {

          var MyTaBlE = this.myTable;   

          setTimeout( function() { MyTaBlE.addRow(); } , 1000);     

        }

        this.myTable.tbody.removeChild(this);

        this.myTable.refreshRowIndex();
		
		calculateTotal();

      }

      row.moveUp = function() {

        if(!this.previousSibling) return;

        this.myTable.tbody.insertBefore(this,this.previousSibling);

        this.myTable.refreshRowIndex();

      }

      row.moveDown = function() {

        if(!this.nextSibling) this.myTable.addRow(row);

        this.myTable.tbody.insertBefore(this.nextSibling,this);

        this.myTable.refreshRowIndex();

      }

      row.setData = function(data) {

        if(!data || typeof(data) != "object") { return; };

        for(var i=0; i<this.myTable.colCount(); i++) {

          this.cells.item(i).setData(data);

        }        

		//this.calculateTotal();

      }

      row.getData = function() {

        var data = new Object();

        for(var i=0; i<this.myTable.colCount(); i++) {

          if(this.cells.item(i).getData) this.cells.item(i).getData(data,true);

        }    

        return data;    

      }

      for(var i=0; i<this.colCount(); i++) {

        var cell = this.createCell(i);

        row.appendChild(cell);

      }

      if(this.onCreateRow) this.onCreateRow(row);

      return row;

    };



    this.onCreateRow; //function(row) {}

	this.cellCodeIndex = 1;
	this.isProductLoaded = function(cell) { 
		if(this.selectedRow && typeof(this.selectedRow) != "undefined" && this.selectedRow === cell.parentNode) {
			var data = this.selectedRow.getData();
			if(data.id && data.id != "" && data.code && data.code != "") {
			} else {
				if(this.selectedCell !== cell && cell === this.selectedRow.cells.item(1))  {
					return true;
				} else {}
				if(cell.parentNode.cells.item(1).firstChild.focus && !cell.parentNode.cells.item(1).noSelect) setSelectionRange(cell.parentNode.cells.item(1).firstChild);
				return false;
			}
		} else {
			if(typeof(cell) != "undefined" && cell.index != this.cellCodeIndex) {
				cell.parentNode.select();
				cell.parentNode.cells.item(1).select();
				return false;
			}
		}
		return true;
	}

    this.createCell = function(i) {

      var cell = document.createElement('td');

      cell.index = i;

      cell.myTable = this;

      cell.onclick = function() { this.select(); }

      cell.select = function() { 

        if(this.myTable.isProductLoaded(this) == false) return;

        if(!this.myTable.selectedCell || this.myTable.selectedCell !== this) {

          if(this.myTable.selectedCell) this.myTable.selectedCell.deselect();

          this.myTable.selectedCell = this; 

          if(this.firstChild.focus && !this.noSelect) setSelectionRange(this.firstChild);

          if(this.onSelect) this.onSelect();

          this.className = this.myTable.cellSelectedClass; 

        }

      }

      cell.deselect = function() {

        if(this.myTable.selectedCell === this) {

          if(this.onDeselect) cell.onDeselect();
		  if(this.onDeselect2) cell.onDeselect2();

          this.className = '';

          this.selected = false;

          this.myTable.selectedCell = '';

          this.parentNode.calculateTotal();

        }

      };

      cell.selectNext = function() {

        this.deselect();

        if(this.nextSibling) this.nextSibling.select();

        else {

          if(!this.parentNode.nextSibling) this.myTable.addRow();

          this.parentNode.nextSibling.select();

          this.parentNode.nextSibling.firstChild.select();

        }

      }

      cell.afterCreate = function() {}

      cell.setData = function(data) {}

      cell.getData = function(data) {}

      if(this.onCreateCell) this.onCreateCell(cell);

      return cell;

    };

    this.onCreateCell; //function(cell) {}

    

    this.setRowData = function(row,data) {

      for(var i=0; i<this.colCount(); i++) {

        this.setCellData(row,row.cells.item(i),data);

      }

    }



    this.setCellData = function(row,cell,data) {

      if(typeof(row) == "number") 

        if(this.tbody.rows.item(row)) row = this.tbody.rows.item(row);

      if(typeof(cell) != "object")

        if(typeof(cell) == "number" && typeof(row) == "object") {

          if(row.cells.item(cell)) 

            cell = row.cells.item(cell);

          else return;

        }

        else return;

      if(this.onSetCellData) this.onSetCellData(row,cell,data);

    }

    this.onSetCellData; //function(row,cell,data) {}



    

    this.setEditNames = function(row,isset) {



      if(isset)



        for(var i=0; i<row.cells.length; i++) {



          if(i!=0 && i!=6 && i!=7) {



            var c = row.cells.item(i).firstChild;



            if(c.parentNode.lp == 1) { 



              c.name = 'parent_wi'; c.id = 'parent_wi'; 



              c=c.nextSibling.nextSibling.nextSibling.nextSibling;



              c.name = 'parent_name_wi'; c.id = 'parent_name_wi'; c.className = 'sqsEnabled';



              //alert(c);



              c=c.nextSibling;



              c.name = 'parent_id_wi'; c.id = 'parent_id_wi';



              /*



              c=c.nextSibling;



              c.name = 'vat_id_p'; c.id = 'vat_id_p';



              c=c.nextSibling;



              c.name = 'category_id_p'; c.id = 'category_id_p';



              */



            }



            /*



            if(c.parentNode.lp == 2) { c.name = 'name_p'; c.id = 'name_p'; c.className = 'sqsEnabled'; }



            if(c.parentNode.lp == 3) { c.name = 'quantity_p'; c.id = 'quantity_p'; }



            if(c.parentNode.lp == 4) { c.name = 'price_p'; c.id = 'price_p'; }



            if(c.parentNode.lp == 5) { c.name = 'discount_p'; c.id = 'discount_p'; }



            */



          }



        }                      



      else



        for(var i=0; i<row.cells.length; i++) {



          if(i!=0 && i!=5) {



            var c = row.cells.item(i).firstChild;



            c.name = ''; c.id = '';



            if(i == 1) {



              c=c.nextSibling.nextSibling.nextSibling.nextSibling;



              c.name = ''; c.id = '';



              c=c.nextSibling;



              c.name = ''; c.id = '';



              /*



              c=c.nextSibling;



              c.name = ''; c.id = '';



              c=c.nextSibling;



              c.name = ''; c.id = ''; */



            }



          }



        }



    }



    



    this.deselectRow = function() {



      if(!this.selectedRow) return; 



      



      this.setEditNames(this.selectedRow,false);



      



      this.selectedRow.className = '';



      this.deselectCell();



      this.selectedRow = null;          



    }



    



    this.selectRow = function(row) {



      if(this.selectedRow === row) return;



      if(this.selectedRow) this.deselectRow();



      this.selectedRow = row;



      this.selectedRow.className = this.rowSelectedClass;



      this.setEditNames(this.selectedRow,!this.selectedRow.isnew);          



    }







    this.selectNextRow = function() {



      if(!this.selectedRow) return;



      if(!this.selectedRow.nextSibling) this.insertRow();



      var cell_id = this.selectedCell.lp;



      this.selectRow(this.selectedRow.nextSibling);



      this.selectCell(this.selectedRow.cells.item(cell_id));



    }







    this.selectPreviousRow = function() {



      if(!this.selectedRow) return;



      if(!this.selectedRow.previousSibling) return;



      if(this.selectedRow === this.tbody.rows.item(0)) return;



      var cell_id = this.selectedCell.lp;



      this.selectRow(this.selectedRow.previousSibling);



      this.selectCell(this.selectedRow.cells.item(cell_id));



    }







    this.calculateSubtotal = function(row,sub) {



      if(!row) if(this.selectedRow) row = this.selectedRow; else return 0;



      if(row.cells.item(4).err || row.cells.item(5).err) {



        row.cells.item(6).firstChild.value = NumberToUserFormatNumber(0);



        row.cells.item(6).firstChild.style.color = 'red';



        row.title = 'Bad number format';



      } else if(row.cells.item(2).firstChild.value == '' && UserFormatNumberToNumber(row.cells.item(4).firstChild.value) != 0 && !row.cells.item(4).err) {



        row.cells.item(6).firstChild.style.color = 'red';



        row.title = 'Position have price, but didin\'t have a description';          



      } else {



        var quantity = parseInt(row.cells.item(3).firstChild.value);



        if(row.cells.item(1).firstChild.nextSibling.nextSibling.value == 3)



          quantity = 1;



        var price = UserFormatNumberToNumber(tmp = row.cells.item(4).firstChild.value);



        var discount = UserFormatNumberToNumber(row.cells.item(5).firstChild.value,"%");



        var total = NumberToUserFormatNumber(quantity*price-quantity*price*(discount/100));



        row.cells.item(6).firstChild.value = total;



        row.cells.item(6).firstChild.style.color = 'black';



        row.title = '';



      }



      //if(!sub) this.calculateTotal();



    }



    



    this.calculateTotal = function() {



    



      var vats = new Object();                  



      var subtotal = 0;



      var total = 0;



      



      for(var i=0; i<this.rowCount(); i++) {



        if(!OPT['to_is_vat_free']) {



          var v_tmp = this.tbody.rows.item(i).cells.item(1).firstChild.nextSibling.nextSibling.nextSibling.value;



          if(v_tmp && this.tbody.rows.item(i).cells.item(2).firstChild.value != '') { 



            if(typeof(vats[v_tmp]) != "object") { vats[v_tmp] = new Object(); vats[v_tmp]['vat'] = 0; }



            vats[v_tmp]['vat'] += UserFormatNumberToNumber(this.tbody.rows.item(i).cells.item(6).firstChild.value);



          }



        } else if(typeof(vats['0.00']) != "object") { vats['0.00'] = new Object(); vats['0.00']['vat'] = 0; }



        subtotal += UserFormatNumberToNumber(this.tbody.rows.item(i).cells.item(6).firstChild.value);



      }     



      total = subtotal;



      var rt = document.getElementById('result_table');



      for(var i=1; i<rt.rows.length-1; i++) { if(!vats[rt.rows.item(i).id]) { rt.deleteRow(i); --i; } else vats[rt.rows.item(i).id]['node'] = rt.rows.item(i); }          



      for(x in vats) {



        vats[x]['vat'] = vats[x]['vat']*(parseFloat(x)/100);



        total += vats[x]['vat'];                  



        var txL = 'VAT'+' ('+NumberToUserFormatNumber(parseFloat(x),'%')+')';



        var txF = NumberToUserFormatNumber(vats[x]['vat']);



        if(vats[x]['node']) {



          vats[x]['node'].id = x;



          vats[x]['node'].cells.item(0).innerHTML = txL;



          vats[x]['node'].cells.item(1).innerHTML = txF; 



        } else {



          rt.insertRow(1);



          rt.rows.item(1).id = x;



          rt.rows.item(1).insertCell(0);



          rt.rows.item(1).cells.item(0).className = 'positionsLabel';



          rt.rows.item(1).cells.item(0).innerHTML = txL;



          rt.rows.item(1).insertCell(1);



          rt.rows.item(1).cells.item(1).className = 'positionsField';



          rt.rows.item(1).cells.item(1).innerHTML = txF;



        }



      } 







      document.getElementById('subtotal_span').innerHTML = NumberToUserFormatNumber(subtotal);



      document.getElementById('subtotal').value = NumberToUserFormatNumber(subtotal);



      total = ((iType == "correct")?'-':'')+NumberToUserFormatNumber(total).toString();



      document.getElementById('total_span').innerHTML = total;



      document.getElementById('total').value = total;



    }







    this.deselectCell = function() {



      if(!this.selectedCell) return;



      



      var tmp = 0;



      var c = this.selectedCell;



      /*



      if(c.lp == 4)



        if(c.firstChild.value != "") {



          tmp = UserFormatNumberToNumber(this.selectedCell.firstChild.value);



          if(tmp == -1) {



            alert('Error with format number: '+c.firstChild.value);



            c.err = true;



            c.firstChild.style.color = 'red';



          }



          else {



            c.firstChild.value = NumberToUserFormatNumber(tmp);



            c.firstChild.style.color = 'black';



            c.err = false;



          }



        }







      if(c.lp == 5)



        if(c.firstChild.value != "") {



          tmp = UserFormatNumberToNumber(this.selectedCell.firstChild.value,"%");



          if(tmp == -1) {



            alert('Error with format number: '+c.firstChild.value);



            c.err = true;



            c.firstChild.style.color = 'red';



          }



          else {



            c.firstChild.value = NumberToUserFormatNumber(tmp,"%");



            c.firstChild.style.color = 'black';



            c.err = false;



          }



      }



      */



      //this.calculateSubtotal(c.parentNode);







      this.selectedCell.className = '';



      this.selectedCell = null;



    }



    



    this.selectCell = function(cell) {



      if(this.selectedCell === cell) return;



      if(this.selectedCell) this.deselectCell();



      this.selectedCell = cell;  



      if(!cell.noSelect) {



        this.selectedCell.className = this.cellSelectedClass;



        this.setCellFocus(this.selectedCell);



      }   



    }



    



    this.selectNextCell = function() {



      if(!this.selectedCell) return;



      if(this.selectedCell.lp == 4) {



        this.selectNextRow();



        this.selectCell(this.selectedRow.firstChild.nextSibling);



      }



      else



        this.selectCell(this.selectedCell.nextSibling);



    }



 



    this.setCellFocus = function(cell,input) {

        if(typeof(cell) == 'number') {

          cell = this.selectedRow.cells.item(cell);

        }

        var edit = ((input)?input:cell.firstChild);

        if(edit && typeof(edit) == "object" && (edit.type == "text" || edit.type == "textarea")) {

          if(edit.createTextRange) {

            var range = edit.createTextRange();

            range.moveStart("character", 0);

            range.moveEnd("character", edit.value.lengh-1);

            range.select();

          } else { 

            if(edit.setSelectionRange) {

              edit.setSelectionRange(0,edit.value.length);

            }

          }

          edit.focus();

        }

        if(edit && typeof(edit) == "object" && edit.options) { edit.focus(); }

    }



    



    this.fillWithDefaultData = function(row) {



      //this.setCellData(3,1,row);



      //this.setCellData(5,0,row);



      //this.setCellData(6,0,row);



    }



    



    this.refreshNumeration = function() {



      for(var i=0; i<this.tbody.rows.length; i++)



        this.tbody.rows.item(i).cells.item(0).firstChild.value = i+1;



    }



    /*        



    this.setCellData = function(i,data,row) {



      if(!row) if(this.selectedRow) row = this.selectedRow; else return null;



      //if(i == 3) { row.firstChild.nextSibling.nextSibling.nextSibling.firstChild.value = data; }



      //if(i == 5) { row.firstChild.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.firstChild.value = NumberToUserFormatNumber(data,"%"); }



      //if(i == 6) { row.firstChild.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.firstChild.value = NumberToUserFormatNumber(data); }



    }



    */







    



    this.insertRowFromArray = function(arr) {



      var row = this.insertRow();



      row.cells.item(1).firstChild.value = arr['code'];



      row.cells.item(1).firstChild.nextSibling.value = arr['id'];



      row.cells.item(1).firstChild.nextSibling.nextSibling.value = arr['unit_id'];



      row.cells.item(1).firstChild.nextSibling.nextSibling.nextSibling.value = arr['vat_id'];



      row.cells.item(1).firstChild.nextSibling.nextSibling.nextSibling.nextSibling.value = arr['category_id'];



      row.cells.item(2).firstChild.value = arr['name'];



      row.cells.item(3).firstChild.value = arr['quantity'];



      row.cells.item(4).firstChild.value = NumberToUserFormatNumber(arr['price']);



      row.cells.item(5).firstChild.value = NumberToUserFormatNumber(arr['discount'],'%');



      row.cells.item(6).firstChild.value = NumberToUserFormatNumber(arr['total']);



    }







    this.getRowData = function(row) {



      if(typeof(row) == "number") row = this.tbody.rows.item(row);



      var rData = new Object();



      rData['id'] = row.cells.item(1).firstChild.nextSibling.value;



      rData['unit_id'] = row.cells.item(1).firstChild.nextSibling.nextSibling.value;



        if(!rData['unit_id']) rData['unit_id'] = OPT['default_unit']; //szt



      rData['vat_id'] = row.cells.item(1).firstChild.nextSibling.nextSibling.nextSibling.value;



        if(!rData['vat_id']) rData['vat_id'] = OPT['default_vat']; //standard



      rData['category_id'] = row.cells.item(1).firstChild.nextSibling.nextSibling.nextSibling.nextSibling.value;



        if(!rData['category_id']) rData['category_id'] = OPT['default_category']; //no category



      



      rData['code'] = row.cells.item(1).firstChild.value;



      rData['name'] = row.cells.item(2).firstChild.value;



      rData['quantity'] = row.cells.item(3).firstChild.value;



      rData['price'] = UserFormatNumberToNumber(row.cells.item(4).firstChild.value);



      rData['discount'] = UserFormatNumberToNumber(row.cells.item(5).firstChild.value,'%');



      rData['total'] = UserFormatNumberToNumber(row.cells.item(6).firstChild.value);



      



      if(rData['code'] == '' && rData['name'] == '') return ''; else return rData;



    }



    



    this.prepareToSend = function(json) {



      var rData = new Object();



      var l = 0, tmp;



      for(var i=0; i<this.rowCount(); i++) {



        //this.calculateSubtotal(this.tbody.rows.item(i),true);



        tmp = this.getRowData(i); 



        if(tmp != '') { rData[l.toString()] = tmp; l++; }



      }



      //this.calculateTotal();



      if(!json)



        return JSON.stringifyNoSecurity(rData);



      else



        return rData;



    }



    



    this.KeyPressedNumber = function(e) {



      var keynum;



      if(window.event) //IE 



        keynum = e.keyCode;



      else



        keynum = e.which;



      return keynum;    



    }



    



    this.KeyPressed = function(e, cell, method, noArrow) {

      var keynum;

      if(window.event) //IE 

        keynum = e.keyCode;

      else

        keynum = e.which;

      if((keynum == 9) || (keynum == 13 && cell.index != 2)) {


        //this.calculateTotal();           

        //if(this.selectedCell.lp == 1) setTimeout( function() { N.selectNextCell(); } , 100); 

        //this.selectNextCell();
        if(cell.index == 1 || cell.index == 2) cell.parentNode.calculateTotal(); 
		cell.selectNext();


        return false;

      }
	  
	  if(!noArrow) {
      	if(keynum == 40) { var id = cell.index; var row = cell.parentNode.selectNext(); if(row) { row.select(); row.cells.item(id).select(); } }
      	if(keynum == 38) { var id = cell.index; var row = cell.parentNode.selectPrevious(); if(row) { row.select(); row.cells.item(id).select(); } }
	  } else return true;

      if(e.shiftKey && (method == "decimalNumber" || method == "onlyNumber")) return false; 

      if(method == "decimalNumber") return this.OnlyNumbers(keynum);

      if(method == "onlyNumber") return this.OnlyNumbers(keynum, true);

      return true;

    } 



    this.OnlyNumbers = function(e, noQuote) {

      var keynum = e, keychar, numcheck;

      keychar = String.fromCharCode(keynum);

      numcheck = /\d/;

      return numcheck.test(keychar) 

             || ((!noQuote)?(keynum == 190):false)

             || (keynum == 8) //backspace

             || (keynum == 46) //delete

             || (keynum == 13) //enter

             || (keynum == 0) //special keys with FF

             || (keynum == 37) //left arrow

             || (keynum == 39) //right arrow

             || (keynum == 188) //,
			 || (keynum >= 95 && keynum <= 105) //numeric keyboard
			 || (keynum == 110);

    } 



    this.row = function(i) { if(this.tbody.rows.item(i)) return this.tbody.rows.item(i); }

    this.cells = function(i,j) { if(this.tbody.rows.item(i).cells.item(i)) return this.tbody.rows.item(i).cells.item(i); }



  }



  



//  var N;



  var parent_options;



  

/*

  function ItemListSave(json) {



    if(N.selectedCell) N.selectedCell.deselect();



    if(N.selectedRow) N.selectedRow.deselect();



    var data = new Object();



    for(var i=0; i<N.rowCount(); i++) {



      data[i.toString()] = N.row(i).getData();



      }



    return json ? JSON.stringifyNoSecurity(data) : data;



  } 



  



  function ItemListClear() {



    while(N.rowCount()>0) N.row(0).deleteRow();



  }

*/

  



  function ItemListFill(list) {



/*



    var start_list_count = 6;



    var count = 0;



    var arr = '';



    



    if(list)



      arr = list;



    else



    if(document.getElementById('position_list').value!="")



      arr = eval(document.getElementById('position_list').value);



  



    if(arr != '')



      for(x in arr) {



        N.insertRowFromArray(arr[x]);



        count++;



      }



        



    for(var i=count; i<start_list_count; i++) N.insertRow();  



    N.calculateTotal();  



*/



  }