/*----------------------------------------------------------------------------------
Copyright © 2004 Ian Davis & James Carlyle
This software is provided 'as-is', without any express or implied warranty. In no 
event will the author(s) be held liable for any damages arising from the use of this 
software.
Permission is granted to anyone to use this software for any purpose, including 
commercial applications, and to alter it and redistribute it freely, subject to the 
following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that 
you wrote the original software. If you use this software in a product, an 
acknowledgment (see the following) in the product documentation is required.
Portions Copyright © 2004 Ian Davis & James Carlyle
2. Altered source versions must be plainly marked as such, and must not be 
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
----------------------------------------------------------------------------------*/
function FormMgr (model, resMgr, resTable, resList, clsList, propList, resTitle, resRemove, propAdd, scriptBlock, editDiv, viewDiv, serialiseDiv) {
  this.model = model;
  this.resMgr = resMgr;
  this.resTable = resTable;
  this.resList = resList;
  this.clsList = clsList;
  this.propList = propList;
  this.resTitle = resTitle;
  this.resRemove = resRemove;
  this.propAdd = propAdd;
  this.scriptBlock = scriptBlock;
  this.editDiv = editDiv;
  this.viewDiv = viewDiv;
  this.serialiseDiv = serialiseDiv;
  this.currentRes = null;
}

function clearNode(node) {
  while (node.firstChild != null) {
      node.removeChild(node.firstChild);
  }
}
FormMgr.prototype.clearNode = clearNode;

function displayRes() {
  this.clearNode(this.resTable);
  if (this.currentRes != null) {
    var table = document.createElement("table");
    this.resTable.appendChild(table);
    this.resRemove.disabled = false;
    this.propAdd.disabled = false;
    var classRow = this.appendRow();
    var classLabel = this.appendCell(classRow);
    classLabel.appendChild(document.createTextNode("class"));
    var classValue = this.appendCell(classRow);
    var classValueInput = document.createElement("input");
    classValue.appendChild(classValueInput);
    classValueInput.value = this.currentRes.cls.toString();
    classValueInput.disabled = true;
    for (propName in this.currentRes.prop) {
      var resPropArray = this.currentRes.getPropByName(propName);
      for (i = 0; i < resPropArray.length; i++) {
        this.addPropToTable(resPropArray[i]);
      }
    }
  } else {
    this.resRemove.disabled = true;
    this.propAdd.disabled = true;
  }
  return false;
}
FormMgr.prototype.displayRes = displayRes;

function addPropToTable(resProp) {
  if (resProp != undefined) {
    var prop = resProp.prop;
    var row = this.appendRow();
    var label = this.appendCell(row);
    label.appendChild(document.createTextNode(prop.toString()));
    var value = this.appendCell(row);
    var valueField;
    if (prop.rangeClsIsLiteral) {
      valueField = document.createElement("input");
      valueField.value = resProp.value;
      valueField.onchange = new Function("formMgr.saveProp(this.name, this.value)");
    } else {
      valueField = document.createElement("select");
      var resArray = resMgr.getResByCls(prop.rangeCls);
      for (var j = 0; j < resArray.length; j++) {
        var res = resArray[j];
        if (res != undefined) {
          var option = new Option();
          option.text = res.toString();
          option.value =  res.getId();
          if (resProp.value == res) {
            option.selected = true;
          }
          valueField.options.add(option, 0);
        }
      }
      valueField.onchange = new Function("formMgr.saveRef(this.name, this.value)");
    }
    valueField.name = prop.toString() + "#" + resProp.index;
    value.appendChild(valueField);
    if (!prop.rangeClsIsLiteral) {
      var navButton = document.createElement("button");
      navButton.appendChild(document.createTextNode("view"));
      navButton.onclick = new Function("formMgr.navigateRes(this.previousSibling.value)");
      value.appendChild(navButton);
      if (resProp.value == null) {
        valueField.onchange();
      }
    }
    var removeCell = this.appendCell(row);
    if (!resProp.readOnly) {
      var removeButton = document.createElement("button");
      removeButton.appendChild(document.createTextNode("remove"));
      removeButton.name = prop.toString() + "#" + resProp.index;
      removeButton.onclick = new Function("formMgr.removeProp(this)");
      removeCell.appendChild(removeButton);
    }
  }
}
FormMgr.prototype.addPropToTable = addPropToTable;

function removeRowFromTable(rowNode) {
  if (document.all) {
    this.resTable.firstChild.deleteRow(rowNode.rowIndex);
  } else {
    this.resTable.firstChild.removeChild(rowNode);
  }
}
FormMgr.prototype.removeRowFromTable = removeRowFromTable;

function appendRow() {
  if (document.all) {
    return this.resTable.firstChild.insertRow();
  } else {
    var rowNode = document.createElement("tr");
    this.resTable.firstChild.appendChild(rowNode);
    return rowNode;
  }
}
FormMgr.prototype.appendRow = appendRow;

function appendCell(rowNode) {
  if (document.all) {
    return rowNode.insertCell();
  } else {
    var cellNode = document.createElement("td");
    rowNode.appendChild(cellNode);
    return cellNode;
  }
}
FormMgr.prototype.appendCell = appendCell;

function displayTitle() {
  this.clearNode(this.resTitle);
  var text;
  if (this.currentRes != null) {
    text = this.currentRes.toString();
  } else {
    text = "No resource selected";
  }
  this.resTitle.appendChild(document.createTextNode(text));
}
FormMgr.prototype.displayTitle = displayTitle;

function listCls() {
  for (clsName in this.model.cls) {
    var cls = this.model.getCls(clsName); 
    if (cls.instantiable) {
      var option = new Option();
      option.text = cls.toString();
      option.value =  cls.toString();
      this.clsList.options.add(option, 0);
    }
  }
}
FormMgr.prototype.listCls = listCls;

function listProp() {
  this.clearNode(this.propList);
  if (this.currentRes != null) {
    var resProp = this.currentRes.cls.getDomainProp();
    for (var i = resProp.length - 1; i > -1; i--) {
      var option = new Option();
      option.text = resProp[i].toString();
      option.value =  resProp[i].toString();
      this.propList.options.add(option, 0);
    }
  }
  return false;
}
FormMgr.prototype.listProp = listProp;

function listRes() {
  this.clearNode(this.resList);
  var resArray = this.resMgr.getAllRes();
  for (i = 0; i < resArray.length; i++) {
    var res = resArray[i];
    if (res != undefined && res != this.currentRes) {
      var option = new Option();
      option.text = res.toString();
      option.value =  res.getId();
      this.resList.options.add(option, 0);
    }
  }
  return false;
}
FormMgr.prototype.listRes = listRes;

function displaySelectedRes() {
  this.setCurrentRes(this.getRes(this.resList.value));
}
FormMgr.prototype.displaySelectedRes = displaySelectedRes;

function navigateRes(ref) {
  this.setCurrentRes(this.getRes(ref));
}
FormMgr.prototype.navigateRes = navigateRes;

function removeRes() {
  this.resMgr.removeRes(this.currentRes);
  this.setCurrentRes(null);
}
FormMgr.prototype.removeRes = removeRes;

function setCurrentRes(res) {
  this.currentRes = res;
  this.displayTitle();
  this.displayRes();
  this.listRes();
  this.listProp();
  return false;
}
FormMgr.prototype.setCurrentRes = setCurrentRes;

function addRes() {
  this.setCurrentRes(this.resMgr.createRes(this.model.getCls(this.clsList.value), true));
}
FormMgr.prototype.addRes = addRes;

function addProp() {
  var prop = this.model.getProp(this.propList.value);
  var resProp;
  if (prop.rangeClsIsLiteral) {
    resProp= this.currentRes.addProp(prop, "", false);
  } else {
    resProp = this.currentRes.addProp(prop, null, false);
  }
  this.addPropToTable(resProp);
  return false;
}
FormMgr.prototype.addProp = addProp;

function saveProp(propFieldName, value) {
  var nameArray = propFieldName.split("#");
  this.currentRes.setProp(this.model.getProp(nameArray[0]), nameArray[1] - 1, value);
  return true;
}
FormMgr.prototype.saveProp = saveProp;

function saveRef(propFieldName, ref) {
  var nameArray = propFieldName.split("#");
  var res = this.getRes(ref);
  this.currentRes.setProp(this.model.getProp(nameArray[0]), nameArray[1] - 1, res);
  return true;
}
FormMgr.prototype.saveRef = saveRef;

function removeProp(propField) {
  var nameArray = propField.name.split("#");
  this.currentRes.removeProp(this.model.getProp(nameArray[0]), nameArray[1] - 1);
  this.removeRowFromTable(propField.parentNode.parentNode);
}
FormMgr.prototype.removeProp = removeProp;

function clear() {
  if (confirm("are you sure you want to reset the model?")) {
    resMgr.clearRes();
    this.setCurrentRes(null);
    return true;
  }
  return false;
}
FormMgr.prototype.clear = clear;

function getRes(ref) {
  var refArray = ref.split("#");
  return this.resMgr.getRes(refArray[0], refArray[1]);
}
FormMgr.prototype.getRes = getRes;

function loadScript(scriptName) {
  if (this.clear()) {
    this.clearNode(this.scriptBlock);
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = scriptName;
    this.scriptBlock.appendChild(script);
  }
}
FormMgr.prototype.loadScript = loadScript;

function showView() {
  this.clearNode(this.serialiseDiv);
  this.serialiseDiv.appendChild(document.createTextNode(model.serialiseRdfXml()));
  this.editDiv.style.display="none";
  this.viewDiv.style.display = "block";
}
FormMgr.prototype.showView = showView;

function showEdit() {
  this.viewDiv.style.display = "none";
  this.editDiv.style.display="block";
}
FormMgr.prototype.showEdit = showEdit;

function saveCookie() {
  var content = escape(resMgr.serialiseJs());
  if (confirm("save " + content.length + " bytes to cookie?  some browsers have a limit of 4000 bytes.")) {
    document.cookie = "";
    document.cookie = content;
  }
}
FormMgr.prototype.saveCookie = saveCookie;

function loadCookie() {
  if (this.clear()) {
    eval(unescape(document.cookie));
    this.setCurrentRes(null);
  }
}
FormMgr.prototype.loadCookie = loadCookie;
