//categories
function saveItems(){
	document.getElementById('position_list').value = ItemsList(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 ItemListSave;
var ItemListClear;
var ItemListFil;
var FillText;
		
addEvent(
  window,
  'load',
  function() {
	var paramsTable = new paramsMT('itemsTable');
	paramsTable.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<paramsTable.rowCount(); i++) {
			var data = paramsTable.row(i).getData();
			data.position = i+1;
			paramsTable.row(i).setData(data);
		}
	}

	paramsTable.onCreateCell = function(cell) {

		var i = cell.index;
		if(i == 0) {
		
			cell.setData = function(data) {			
				console.log(data);
				if(data.feature_name) {
					cell.firstChild.value = data.feature_name;					
					cell.firstChild.value = data.feature_name_eng;					
				}
			};
			cell.getData = function(data) {
				data.feature_name = cell.firstChild.value;
				data.ecmproductcategory_id = cell.getElementsByTagName('input')[1].value;
				data.id = cell.getElementsByTagName('input')[2].value;
			}
			
        cell.onDeselect = function() {
          ERROR = false;
          var data = new Object();
          this.getData(data);
          if(!ERROR) {
            this.setData(data);
          }
	  
        }  
			var edit = document.createElement('input');
			edit.setAttribute('type','text');
			edit.setAttribute('tabIndex',1);
			edit.className = 'inputs';
			cell.appendChild(edit);
		}
		if(i == 1) {
			
			cell.setData = function(data) {			
				if(data.feature_options) {
					cell.firstChild.value = data.feature_options;					
				}
			};
			cell.getData = function(data) {
				data.feature_options = cell.firstChild.value;
			}
			
        cell.onDeselect = function() {
          ERROR = false;
          var data = new Object();
          this.getData(data);
          if(!ERROR) {
            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) {
        	var img;
			
			cell.style.padding="auto auto auto auto";
        	//insert
        	img = document.createElement('img');
        	img.setAttribute('alt',MOD['LBL_INSERT_NEW_ROW']);
        	img.setAttribute('src','modules/EcmQuotes/images/insertrow.gif');
        	img.style.cursor = 'pointer';
       		img.onclick = function() {		
				this.parentNode.myTable.addRow(this.parentNode.parentNode.index+1);
				refreshPositionIndex();
        	};

        	//delete
			cell.appendChild(img);
        	cell.appendChild(document.createTextNode(" "));
        	img = document.createElement('img');
        	img.setAttribute('alt',MOD['LBL_DELETE_ROW']);
        	img.setAttribute('src','modules/EcmQuotes/images/deleterow.gif');
        	img.style.cursor = 'pointer';
        	img.onclick = function() { this.parentNode.parentNode.deleteRow(); refreshPositionIndex();};
			cell.appendChild(img);
        	cell.appendChild(document.createTextNode(" "));
			
        cell.appendChild(document.createElement('br'));
        //move up
        img = document.createElement('img');
        img.setAttribute('alt',MOD['LBL_MOVE_ROW_UP']);
        img.setAttribute('src','modules/EcmQuotes/images/moverowup.gif');
        img.style.cursor = 'pointer';
        img.onclick = function() { this.parentNode.parentNode.moveUp(); refreshPositionIndex();};
        cell.appendChild(img);
        cell.appendChild(document.createTextNode(" "));
        //move down
        img = document.createElement('img');
        img.setAttribute('alt',MOD['LBL_MOVE_ROW_DOWN']);
        img.setAttribute('src','modules/EcmQuotes/images/moverowdown.gif');
        img.style.cursor = 'pointer';
        img.onclick = function() { this.parentNode.parentNode.moveDown();refreshPositionIndex(); }
        cell.appendChild(img);
		}
	}
	ItemsList = function(json) { 
      var data = '';
	  for(var i=0; i<paramsTable.rowCount(); i++) {
        data = data + '||||' + JSON.stringifyNoSecurity(paramsTable.row(i).getData());
      }	  
      return data;
    } 
	if(paramsTable.rowCount() == 0) {paramsTable.addRow();};
    paramsTable.onSetCellData = function(row,cell,data) {
		if(cell.innerHTML == '') cell.innerHTML = '&nbsp;';
    }

    ItemListFill = function() {
      var pl;
	  pl = document.getElementById('position_list').value;
      if(pl && pl != '') {
        try {
          pl = eval(pl);     
          for(x in pl) { var pl_row = pl[x]; if (pl_row.feature_name=='') continue; paramsTable.addRow().setData(pl_row); }
        } catch(err) { pl = null; };
	  }
	}
	
	ItemListClear = function(noNew,save) {
		if(typeof(save)=="string") ItemListSave(null,save);
		while(paramsTable.rowCount()>0) paramsTable.row(0).deleteRow(noNew);
	}

	ItemListClear();
	ItemListFill();
	
	
	  var check_form_ = check_form;
	  check_form = function(formname) {
		  saveItems();
		  return check_form_(formname);
	  }
  }
);