var selectableTchegbeTableTags;
var selectionTchegbeTableTags;
var resetTchegbeTableTags;
var formObjTchegbeTableTags;

function setFormTchegbeTableTags(obj, inConfigKey){
	formObjTchegbeTableTags = obj;
	configKeyTchegbeTableTags = inConfigKey;
	selectableTchegbeTableTags = formObjTchegbeTableTags.selectable;
	selectionTchegbeTableTags = formObjTchegbeTableTags.selection;
	resetTchegbeTableTags = formObjTchegbeTableTags.reset;
}

function addColumnTchegbeTableTags() {
  var fromOption = selectableTchegbeTableTags;
  var toOption = selectionTchegbeTableTags;
  
  var index = fromOption.options.length - 1;
  
  if (fromOption.selectedIndex >= 0) {
    for (var i = 0; i <= index; --index) {
      if (fromOption.options[index].selected) {
        toOption.options.length = toOption.options.length + 1;
        toOption.options[toOption.options.length-1].value = fromOption.options[index].value;
        toOption.options[toOption.options.length-1].text = fromOption.options[index].text;
        fromOption.options[index] = null;
      }
    }
  }
  else {
    alert("please selected a value");
  }
}

function removeColumnTchegbeTableTags(){
  var fromOption = selectableTchegbeTableTags;
  var toOption = selectionTchegbeTableTags;
  var index = toOption.options.length - 1;

  if (toOption.selectedIndex >= 0) {
    for (var i = 0; i <= index; index--) {
      if (toOption.options[index].selected) {
          fromOption.options.length = fromOption.options.length + 1;
          fromOption.options[fromOption.options.length - 1].text = toOption.options[index].text;
          fromOption.options[fromOption.options.length - 1].value = toOption.options[index].value;
          toOption.options[index] = null;
      }
    }
  }
  else {
    alert("please selected a value");
  }
}



function orderUpTchegbeTableTags() {
  var toOption = selectionTchegbeTableTags;
  
  if (toOption.selectedIndex > 0) {
    var index = toOption.selectedIndex;
    
    var currItem = toOption.options[index].text;
    var prevItem = toOption.options[index-1].text;
    var currItemVal = toOption.options[index].value;
    var prevItemVal = toOption.options[index-1].value;
            
    toOption.options[index].text = prevItem;
    toOption.options[index].value = prevItemVal;    
    toOption.options[index-1].text = currItem;
    toOption.options[index-1].value = currItemVal;
      
    toOption.options[index].selected = false;
    toOption.options[index-1].selected = true;
    
  }
  else {
    if (toOption.selectedIndex < 0) alert("please selected a value");
  }
}

function orderDownTchegbeTableTags() {
  var toOption = selectionTchegbeTableTags;

  if (toOption.selectedIndex >= 0) {
    if (toOption.selectedIndex != toOption.options.length - 1) {
        var index = toOption.selectedIndex;
        
        var currItem = toOption.options[index].text;
        var nextItem = toOption.options[index+1].text;
        var currItemVal = toOption.options[index].value;
        var nextItemVal = toOption.options[index+1].value;      
        
        toOption.options[index].text = nextItem;
        toOption.options[index].value = nextItemVal;      
        toOption.options[index+1].text = currItem;
        toOption.options[index+1].value = currItemVal;      
        
        toOption.options[index].selected = false;
        toOption.options[index+1].selected = true;
    }
  }
  else {
    alert("please selected a value");
  }
}

function restoreTchegbeTableTags(){
  if(isAjaxTableSelectionTchegbeTableTags(configKeyTchegbeTableTags)){
  	updateTableTchegbeTableTags(formObjTchegbeTableTags.action + '&' + resetTchegbeTableTags.name + '=true', configKeyTchegbeTableTags);
  	hideSelectorTchegbeTableTags();
  	updateTableSelectionTchegbeTableTags(formObjTchegbeTableTags.action, configKeyTchegbeTableTags);
  }
  else {
  	resetTchegbeTableTags.value = 'true';
  	formObjTchegbeTableTags.submit();
  }
}

function saveTchegbeTableTags() {
  var fromOption = selectableTchegbeTableTags;
  var toOption = selectionTchegbeTableTags;
  
  if(isAjaxTableSelectionTchegbeTableTags(configKeyTchegbeTableTags)){
  	var postBody = resetTchegbeTableTags.name + '=false';
	for (var i = 0; i < fromOption.length; i++) {
		postBody += "&" + fromOption.name + "=" + escape(fromOption.options[i].value);
	}
	  
	for (var i = 0; i < toOption.length; i++) {
		postBody += "&" + toOption.name + "=" + escape(toOption.options[i].value);
	}
	
	updateTablePostTchegbeTableTags(formObjTchegbeTableTags.action, postBody, configKeyTchegbeTableTags);
	
  	hideSelectorTchegbeTableTags();
  }
  else {   
		for (var i = 0; i < fromOption.length; i++) {
	    	fromOption.options[i].selected = true;
	  	}
	  
	  	for (var i = 0; i < toOption.length; i++) {
	    	toOption.options[i].selected = true;
	  	}
	  
	  	formObjTchegbeTableTags.submit();
  }
}

function hideSelectorTchegbeTableTags(){
  hideSectionTchegbe(formObjTchegbeTableTags.tableSelectionHideId.value);
  hideSectionTchegbe(formObjTchegbeTableTags.tableSelectionId.value);
  showSectionTchegbe(formObjTchegbeTableTags.tableSelectionShowId.value);
}

function showSelectorTchegbeTableTags(){
  hideSectionTchegbe(formObjTchegbeTableTags.tableSelectionShowId.value);
  showSectionTchegbe(formObjTchegbeTableTags.tableSelectionId.value);
  showSectionTchegbe(formObjTchegbeTableTags.tableSelectionHideId.value);
}
