/* $Id: sbiplgsel.js 21220 2010-12-16 10:07:34Z dve $ */
function sbiplgsel(plgFrame, selFrame, wrkFrame) {

  // attributes:
  this.changeRecord  = true;

  this.plgFrame = plgFrame;
  this.selFrame = selFrame;
  this.wrkFrame = wrkFrame;

  this.selForm = null;
  this.table = null;
  this.counter = "";
  this.firstIndex = "";
  this.order = "";
  this.prefix = "";
  this.INDEX = 0;

  this.wrkForm = null;
  this.attributes = null;
  this.properties = null;

  this.rowCSSTitScroll = "tab_title_scroll";
  this.rowCSSTit = "tab_title";
  this.rowCSSData = "tab_data";

  // Public methods:
  this.init = sbiplgsel_init;
  this.addTable = sbiplgsel_addTable;
  this.clearControls = sbiplgsel_clearControls;
  this.setReadOnly = sbiplgsel_setReadOnly;
  this.setCurRecord = sbiplgsel_setCurRecord;
  this.changeData = sbiplgsel_changeData;
  this.setOrder = sbiplgsel_setOrder;
  this.getAttrFullIndex = sbiplgsel_getAttrFullIndex;
  this.getPropFullIndex = sbiplgsel_getPropFullIndex;
  // event:
  this.addRow = sbiplgsel_addRow;
  this.deleteRow = sbiplgsel_deleteRow;
  this.changeRow = sbiplgsel_changeRow;
  this.saveRow = sbiplgsel_saveRow;
  this.cancelRow = sbiplgsel_cancelRow;
  this.setDisabled = sbiplgsel_setDisabled;
  // Private methods:
  this.getPrefix = sbiplgsel_getPrefix;
  this.getAttributeOhneIndex = sbiplgsel_getAttributeOhneIndex;
  this.compute = sbiplgsel_compute;
  this.setCount = sbiplgsel_setCount;
  this.getCount = sbiplgsel_getCount;

}

function sbiplgsel_init(firstIndex, counterAttributeName, order) {

  if (!this.selFrame) return;
  this.selForm = this.selFrame.document.forms(0);
  if (!this.selForm) return;

  this.counter = counterAttributeName;
  if (this.counter == "") return;

  this.prefix = this.getPrefix(this.counter);
  if (this.prefix == "") return;

  if (this.wrkFrame.document.forms(0)) {
    this.wrkForm = this.wrkFrame.document.forms(0);
    this.attributes = this.wrkFrame.attributes;
    this.properties = this.wrkFrame.properties;
    this.setReadOnly(true);
  }

  this.firstIndex = firstIndex;
  this.order = order;
}

function sbiplgsel_addTable(tableID, selCol, tableTitleID) {
  this.table = new sbiplgsel_Table(this, tableID, selCol, tableTitleID);
  return this.table;
}

function sbiplgsel_clearControls() {
  if (!this.wrkForm) return;
  for(var i = 0; i < this.wrkForm.elements.length; i++) {
    var control = this.wrkForm.elements[i];
    if (control.type.toLowerCase() == "text" || control.type.toLowerCase() == "textarea" || control.type.toLowerCase() == "select-one") {
      control.value = "";
    }
    if (control.type.toLowerCase() == "radio" || control.type.toLowerCase() == "checkbox") {
      control.checked = false;
    }
  }
}

function sbiplgsel_setReadOnly(bool) {
  if (!this.wrkForm) return;
  for(var i = 0; i < this.wrkForm.elements.length; i++) {
    var control = this.wrkForm.elements[i];
    control.readOnly = bool;
    control.disabled = bool;
  }
  if(this.wrkFrame.setReadonly != null)
      this.wrkFrame.setReadonly();
      
  if(this.wrkFrame.setReadOnly) this.wrkFrame.setReadOnly(bool);
}

function sbiplgsel_changeData() {
  if (!this.wrkForm) return;

  if (this.attributes) {
    var ai = this.getAttrFullIndex();
    for(var i = 0; i < this.attributes.items.length; i++) {
      var ax = this.attributes.items[i];
      var attrName = this.getAttributeOhneIndex(ax.attributeName,"[");
  
      var checkProp = ax.checkProperties;
      var pos = checkProp.indexOf(".");
      if (pos > 0) checkProp = this.getAttributeOhneIndex(checkProp.substring(0, pos),"[") + ai + checkProp.substring(pos);
      this.attributes.modifyItem(ax.controlName, attrName+ai, ax.checkValue, ax.checkOtherAvailable, ax.checkSelfAvailable, checkProp);
    }
    this.attributes.updateAll();
  }

  if (this.properties) {
    var ai = this.getPropFullIndex();
    for(var i = 0; i < this.properties.items.length; i++) {
      var px = this.properties.items[i];
      var propName = this.getAttributeOhneIndex(px.propertyName,"(");
      this.properties.modifyItem(px.controlName, propName+ai);
    }
    this.properties.updateAll();
  }
}

function sbiplgsel_setCurRecord(index, check) {
  if(typeof(check) == "undefined") check = true;
  if(check) if(!this.changeRecord) return false;
  var out = false;
  if (index != null && index >= 0 && index < this.getCount()) {
    out = true;
  } else {
    index = this.table.getRecord(0);
    if (index != null && index >= 0 && index < this.getCount()) {
      out = true;
    }
  }
  if (out) {
    this.INDEX = index;
    if(!this.wrkFrame.submain){
      this.changeData();
      this.setReadOnly(true);
    }
    this.table.changeClass();
    if(this.selFrame.setStatusRecord) this.selFrame.setStatusRecord();
  }
  return out;
}

function sbiplgsel_setOrder() {
  if(this.order == "") return;

  var col = new Array();
  var code = this.plgFrame.sbiplg.choice("", this.order);
  while(code == 1) {
      col.push(this.plgFrame.sbiplg.message);
      code = this.plgFrame.sbiplg.choice("", this.order);
  }
  for (var i=0; i<col.length; i++) {
    var arrAttr = col[i].split("#");
    for (var j=0; j<arrAttr.length; j++) {
      var arrPaar = arrAttr[j].split("=");
      if (arrPaar.length == 2) this.plgFrame.sbiplg_setvar(arrPaar[0], arrPaar[1]);
      delete arrPaar;
    }
    delete arrAttr;
  }
  delete col;
}

function sbiplgsel_getAttrFullIndex(secondIndex) {
  if (secondIndex == null) secondIndex = this.INDEX;
  return "["+(this.firstIndex==""?"":this.firstIndex+";")+String(secondIndex)+"]";
}

function sbiplgsel_getPropFullIndex(secondIndex) {
  if (secondIndex == null) secondIndex = this.INDEX;
  return "("+(this.firstIndex==""?"":this.firstIndex+";")+String(secondIndex)+")";
}

// *********************************** Private Functions ***************************************************

function sbiplgsel_getPrefix(attr) {
  var out = "";
  var p = attr.indexOf("i");
  if (p >= 5) out = attr.substring(0,p)+"_";
  return out;
}

function sbiplgsel_getAttributeOhneIndex(attr, syn) {
  var pos = attr.indexOf(syn);
  if (pos >= 0) return attr.substring(0,pos);
  return attr;
}

function sbiplgsel_compute(prop, err) {
  if (err == null) err = "";
  if(this.plgFrame.sbiplg_compute(prop) != 0) return(err);
  return(this.plgFrame.sbiplg.result);
}

function sbiplgsel_setCount(iVal) {
  this.plgFrame.sbiplg_setvar(this.counter, iVal);
}

function sbiplgsel_getCount() {
  return parseInt(this.compute(this.counter, "0"));
}

// *********************************** end Private Functions ***********************************************

// *********************************************************************************************************
// ************************************* Event Functions ***************************************************
// *********************************************************************************************************

// *********************************** Neuer Eintrag *******************************************************

function sbiplgsel_addRow() {
  
  if(!this.changeRecord) return false;
  this.plgFrame.sbiplg_clone();

  var count = this.getCount();
  this.setCount(count + 1);
  this.table.paint();
  this.setCurRecord(count);
  var div = this.table.getHTMLDiv();
  if (div) div.scrollTop = div.scrollHeight;
  this.setReadOnly(false);
  this.attributes.updateAll();
  if (this.properties) this.properties.updateAll();
  this.changeRecord = false;
  return true;

}

// *********************************** Löschen *************************************************************

function sbiplgsel_deleteRow() {

  if(!this.changeRecord) return false;
  if (this.table.getCount() <= 0) return false;

  var ai = this.getAttrFullIndex();
  this.plgFrame.sbiplg_removevar(this.prefix+"*"+ai);

  this.setCount(this.getCount() - 1);
  this.table.paint();
  this.table.setFirstRecord();
  this.setReadOnly(true);
  var div = this.table.getHTMLDiv();
  if (div) div.scrollTop = 0;
  return true;
}

// *********************************** Ändern **************************************************************

function sbiplgsel_changeRow() {
  if(!this.changeRecord) return false;
  if (this.table.getCount() <= 0) return false;

  this.plgFrame.sbiplg_clone();

  this.setReadOnly(false);
  this.attributes.updateAll();
  if (this.properties) this.properties.updateAll();
  this.changeRecord = false;
  return true;
}

// *********************************** Speichern ***********************************************************

function sbiplgsel_saveRow() {
  if(this.changeRecord) return false;
  if (this.table.getCount() <= 0) return false;

  this.changeRecord = true;
  this.attributes.synchronizeAll();
  this.setOrder();
  this.table.paint();
  this.setCurRecord(this.INDEX);
  this.setReadOnly(true);

  this.plgFrame.sbiplg_commit();

  return true;
}


// *********************************** Rückgängig **********************************************************

function sbiplgsel_cancelRow() {
  if(this.changeRecord) return false;
  if (this.table.getCount() <= 0) return false;

  this.plgFrame.sbiplg_closeclones();

  this.changeRecord = true;
  this.table.paint();
  this.setCurRecord(this.INDEX)
  this.setReadOnly(true);
  return true;
}

// *********************************** Alle Eingabefelder Disablen ***********************************************************

function sbiplgsel_setDisabled() {  
  this.setReadOnly(true);

  return true;
}

// *********************************************************************************************************
// **************************************** Table Object ***************************************************
// *********************************************************************************************************

function sbiplgsel_Table(set, tableID, selCol, tableTitleID) {
  this.set = set;
  this.id = tableID;
  this.selCol = selCol;
  this.titleID = this.id + "Title";
  if (tableTitleID != null) this.titleID = tableTitleID;

  this.paint = sbiplgsel_TablePaint;
  this.paintTitle = sbiplgsel_TablePaintTitle;
  this.setStyle = sbiplgsel_TableSetStyle;
  this.getCount = sbiplgsel_TableCount;
  this.setFirstRecord = sbiplgsel_TableSetFirstRecord;
  this.getHTMLTable = sbiplgsel_TableGetHTMLTable;
  this.getRecord = sbiplgsel_TableGetRecord;
  this.changeClass = sbiplgsel_TableChangeClass;
  this.getHTMLDiv = sbiplgsel_TableGetHTMLDiv;
}

function sbiplgsel_TablePaint() {
  var plgtab = new this.set.plgFrame.sbiplg_Table(this.set.selForm, this.id, this.set.counter);
  var ai = (this.set.firstIndex == "" ? "" : this.set.firstIndex+";x");
  for(var i = 0; i < this.selCol.length; i++) {
    if (this.selCol[i][1] == "") this.selCol[i][1] = this.set.rowCSSTit;
    if (this.selCol[i][3] == "") this.selCol[i][3] = this.set.rowCSSData;

 var type;
    var pmsName = this.selCol[i][2].toUpperCase();
    if(pmsName.indexOf("A")== 0){
      if (pmsName.indexOf(".") > 0) type = "AP";
      else type = "A";
    }
    else if(pmsName.indexOf("P")== 0 ||this.selCol[i][2].indexOf("F")== 0) type = "P";

    // (header, headerStyle, expression, type, style, width, format, rule)
    //plgtab.addColumn("<span class='"+this.selCol[i][1]+"'>"+this.selCol[i][0]+"</span>", this.selCol[i][1], this.selCol[i][2], type, this.selCol[i][3], "1", this.selCol[i][4], ai);
    var transHeader = this.selCol[i][0];
    if(this.set.plgFrame.translate) transHeader = this.set.plgFrame.translate(this.set.selFrame, transHeader);
    plgtab.addColumn("<span class='"+this.selCol[i][1]+"' originText='"+this.selCol[i][0]+"'>"+transHeader+"</span>", this.selCol[i][1], this.selCol[i][2], type, this.selCol[i][3], "1", this.selCol[i][4], ai);
  }
  plgtab.update();
  if (plgtab) delete plgtab;
  this.paintTitle();
  this.set.clearControls();
  if(this.set.selFrame.setEvent) this.set.selFrame.setEvent();
  if(this.set.selFrame.modifyTable) this.set.selFrame.modifyTable();
}

function sbiplgsel_TablePaintTitle() {
  var tab = this.getHTMLTable();
  if (!tab) return;

  var origTitRow = tab.rows(0);
  var imgWidth = new Array();
  for (var i=0; i<origTitRow.cells.length; i++) imgWidth.push(origTitRow.cells(i).scrollWidth);
  this.setStyle(tab);

  var tabTit = this.set.selFrame.document.all.item(this.titleID);
  if (!tabTit) return;
  this.setStyle(tabTit);
  while (tabTit.rows.length > 0) { tabTit.deleteRow(); }

  var nc = origTitRow.insertCell(origTitRow.cells.length);
  nc.className = this.set.rowCSSTitScroll;
  nc.style.width = "0px";

  var origTitRowCloned = origTitRow.cloneNode(true);
  tabTit.tBodies[0].appendChild(origTitRowCloned);

  for (var i=0; i<origTitRowCloned.cells.length-1; i++) {
    origTitRowCloned.cells(i).style.setExpression("width",this.id+".rows(0).cells("+i+").offsetWidth");
  }
  var div = this.getHTMLDiv();
  if (div) origTitRowCloned.cells(origTitRowCloned.cells.length-1).style.setExpression("width",div.id+".offsetWidth-"+this.id+".offsetWidth");

  for (var i=0; i<origTitRow.cells.length; i++) {
    var dc = origTitRow.cells(i);
    if (imgWidth[i]) dc.innerHTML = "<img src='img/space.gif' width='" + imgWidth[i] + "' height='1' border='0'>";
    dc.style.height = "1px";
    dc.className = this.set.rowCSSTitScroll;
  }
  if (imgWidth) delete imgWidth;
}

function sbiplgsel_TableSetStyle(tab) {
  if (!tab) return;
  tab.style.width = "100%";
  tab.cellPadding = "2";
  tab.cellSpacing = "1";
}

function sbiplgsel_TableCount() {
  var tab = this.getHTMLTable();
  if (!tab) return 0;
  return parseInt(tab.rows.length - 1);
}

function sbiplgsel_TableSetFirstRecord() {
  if (this.getCount() <= 0) return;
  this.set.setCurRecord(this.getRecord(0));
}

function sbiplgsel_TableGetRecord(tableIndex) {
  var out = null;
  tableIndex = tableIndex + 1;
  var tab = this.getHTMLTable();
  if (tab && tableIndex < tab.rows.length) {
    var row = tab.rows[tableIndex];
    if ( !isNaN(parseInt(row.id)) ){
      out = parseInt(row.id);
    }
  }
  return out;
}

function sbiplgsel_TableChangeClass() {
  var tab = this.getHTMLTable();
  if (!tab) return;
  var rowToSel = this.set.INDEX + 1;
  for (var i = 1; i < tab.rows.length; i++) {
    var row = tab.rows[i];
    for (var j = 0; j < row.childNodes.length; j++) {
      row.childNodes[j].className = (row.childNodes[j].className).replace("_selected","");
      if (i == rowToSel && (row.childNodes[j].className).indexOf("_selected") < 0) {
        row.childNodes[j].className = (row.childNodes[j].className).replace(this.selCol[j][3],this.selCol[j][3]+"_selected");
        //row.childNodes[j].className = row.childNodes[j].className + "_selected";
      }
    }
  }
}

function sbiplgsel_TableGetHTMLTable() {
  if (!this.set.selFrame) return null;
  var out = this.set.selFrame.document.all.item(this.id);
  if (!out) return null;
  return out;
}

function sbiplgsel_TableGetHTMLDiv() {
  if (!this.set.selFrame) return null;
  var div = null;
  var tab = this.getHTMLTable();
  if (tab) {
    div = (tab.parentElement.tagName.toLowerCase() == "div" ? tab.parentElement : null);
  }
  return div;
}

