//categories
function saveItems4(){
	document.getElementById('position_list4').value = ItemsList4(true);
	
}
function doRequest(where,post,doFunction,error) {
  this.Display = function(result) { doFunction(result.responseText); }
  this.Fail = function(result){ if(error) alert(error);}
  YAHOO.util.Connect.asyncRequest('POST',where,{success:this.Display,failure:this.Fail},post);       
}



function addEvent(object,eventName,do_function) {
  if(typeof(object) == "string") object = document.getElementById(object);
  if(!object) { alert('No object in function addEvent!'); return; }
  if(object.addEventListener) {
    object.addEventListener(eventName, do_function, false);
	} else {
		object.attachEvent('on'+eventName, do_function);
	} 
}


var tbody_;
var ItemListSave4;
var ItemListClear4;
var ItemListFil4;
var FillText;
		
addEvent(
  window,
  'load',
  function() {
	var paramsTable4 = new paramsMT('itemsTable4');
	paramsTable4.onCreateRow = function(row) {
		row.newPos = false;
		row.noAddNew = true;
		row.ondblclick = function() {}
		row.onSelect = function() {
		}
		row.onDeselect = function() {		
		}
	}
	
	refreshPositionIndex = function() {
		for(var i=0; i<paramsTable4.rowCount(); i++) {
			var data = paramsTable4.row(i).getData();
			data.position = i+1;
			paramsTable4.row(i).setData(data);
		}
	}

	paramsTable4.onCreateCell = function(cell) {

		var i = cell.index;
		if(i == 0) {
		
			cell.setData = function(data) {	
				if(data.pricebook_name) {
					cell.firstChild.value = data.pricebook_name;
					
					cell.getElementsByTagName('input')[1].value = data.pricebook_id;
				}
			};
			cell.getData = function(data) {
				data.ecmpricebook_name = cell.firstChild.value;
				data.ecmpricebook_id = cell.getElementsByTagName('input')[1].value;
			}
			
		 		    
			//cell.select = function() { this.selectNext(); }
			//cell.selectNext = function() {  var row = this.parentNode.selectNext(); row.select(); row.cells.item(i).select(); };
			var edit = document.createElement('input');
			edit.setAttribute('type','text');
			edit.setAttribute('readonly','readonly');
			edit.setAttribute('tabIndex',1);
			edit.className = 'inputs';
			cell.appendChild(edit);
			var edit = document.createElement('input');
			edit.setAttribute('type','hidden');
			edit.setAttribute('readonly','readonly');
			edit.setAttribute('tabIndex',1);
			edit.className = 'inputs';
			cell.appendChild(edit);
		}
		
	      if(i == 1) {
	          cell.getData = function(data) {
	            var cn = this.getElementsByTagName('input');
	            data.price = UserFormatNumberToNumber(cn[0].value);
	          }
	          cell.setData = function(data) {
	            var cn = this.getElementsByTagName('input');
	            cn[0].value = ((data.price)?NumberToUserFormatNumber(data.price):NumberToUserFormatNumber(0));
	  		}
	          cell.onDeselect = function() {
	            ERROR = false;
	            var data = new Object();
	            this.getData(data);
	            if(!ERROR) {
	              data.price = data.price;
	              this.setData(data);
	            }
	          }

	          var edit = document.createElement('input');
	          edit.setAttribute('type','text');
	          edit.setAttribute('tabIndex',1);
	          edit.className = 'inputs';
	          cell.appendChild(edit);
	        } 
	      if(i == 2) {
	          cell.getData = function(data) {
	            var cn = this.getElementsByTagName('input');
	            data.currency = cn[0].value;
	          }
	          cell.setData = function(data) {
	            var cn = this.getElementsByTagName('input');
	            cn[0].value = data.currency;
	  		}
	          cell.onDeselect = function() {
	            ERROR = false;
	            var data = new Object();
	            this.getData(data);
	            if(!ERROR) {
	              data.currency = data.currency;
	              this.setData(data);
	            }
	          }

	          var edit = document.createElement('input');
	          edit.setAttribute('type','text');
	          edit.setAttribute('tabIndex',1);
	          edit.className = 'inputs';
	          cell.appendChild(edit);
	        } 
	
	}
  
	ItemsList4 = function(json) { 
      var data = '';
	  for(var i=0; i<paramsTable4.rowCount(); i++) {
        data = data + '||||' + JSON.stringifyNoSecurity(paramsTable4.row(i).getData());
      }	  
      return data;
    } 
	//if(paramsTable4.rowCount() == 0) {paramsTable4.addRow();};
    paramsTable4.onSetCellData = function(row,cell,data) {
		if(cell.innerHTML == '') cell.innerHTML = '&nbsp;';
    }

    ItemListFill4 = function() {
      var pl;
	  pl = document.getElementById('position_list4').value;
      if(pl && pl != '') {
        try {
          pl = eval(pl);     
          for(x in pl) { var pl_row = pl[x];if (pl_row.pricebook_name=='') continue; paramsTable4.addRow().setData(pl_row); }
        } catch(err) { pl = null; };
	  }
	}
	
	ItemListClear4 = function(noNew,save) {
		if(typeof(save)=="string") ItemListSave4(null,save);
		while(paramsTable4.rowCount()>0) paramsTable4.row(0).deleteRow(noNew);
	}

	ItemListClear4();
	ItemListFill4();
  }
);