/*
recordTable.js

definition: extends dynamic table with the addition of record rows for which changes can be made and tracked.


Methods:
  constructor ({id: "testTable", border: "0px", cellpadding: "0px", cellspacing: "0px"})
    sets up the table and puts it in place

  createRecModel([{field: "testName", editable: "true", width: "120px", colSpan: "1"}, {field: "actions", editable: "false", colSpan: "1"}]);

  addNewRec([{contents: "Nihongo"}, {contents: "<a href='edit.html?id="+ testID + "'>edit</a>"}]);

deleteRec
editRec

*/


extend = function(subClass, parentClass) {
  function inheritance() {}
  inheritance.prototype = parentClass.prototype;

  subClass.prototype = new inheritance();
  subClass.prototype.constructor = subClass;
  subClass.parentConstructor = parentClass;
  subClass.superClass = parentClass.prototype;
}

extend(recordTable, dynamicTable);

function recordTable(attributes, objName) {
  this.insertedRec = [];
  this.objName = objName;
  recordTable.parentConstructor.call(this, attributes);
  this.numRecs = 0;
  this.numNewRecs = 0;
}

recordTable.prototype.createRecModel = function(recModel){
  this.recModel = recModel;
}

recordTable.prototype.createDeleteModel = function(deleteModel){
  this.deleteModel = deleteModel;
}

recordTable.prototype.setRecLimit = function(recLimit, numRecs){
  this.recLimit = recLimit;
  this.numRecs = numRecs;
}

recordTable.prototype.addNewRec = function(){
  if (!this.recLimit || this.numRecs < this.recLimit){ 
    this.numNewRecs++;
    this.numRecs++;
    var attributes = {};
    this.addTr({id: "newRecRow" + this.numNewRecs});
    this.insertedRec[this.numNewRecs] = false;
    for (var a = 0; a < this.recModel.length; a++){
      if(this.recModel[a].width !="") attributes.width = this.recModel[a].width;
      if(this.recModel[a].colSpan !="") attributes.colSpan = this.recModel[a].colSpan;
      attributes.id = "newRecCellID" + this.numNewRecs + "x" + a;
      if (this.recModel[a].field){
        var maxlength = "";
        var inputClass = "";
        if (this.recModel[a].maxlength) maxlength = ' maxlength="' + this.recModel[a].maxlength + '"';
        if (this.recModel[a].inputClass) inputClass = ' class = "' + this.recModel[a].inputClass + '"';
        this.addTd(attributes, '<input id="newRecInput' + this.numNewRecs + 'x' + a + '" onkeydown="' + this.objName + '.recChange('+this.numNewRecs+');" type="text"' + inputClass + maxlength + ' value=""></input>');
      }else{
        this.addTd(attributes, '');
      }
    }
  }
}

recordTable.prototype.recChange = function(recID){
  this.insertedRec[recID] = true;
  if (!document.getElementById("newRecRow" + (recID+1))){
    this.addNewRec();
  }
}

recordTable.prototype.recRowID = function(recID){
  return "recRowID"+recID;
}

recordTable.prototype.addExistingRec = function(recID, rec){
  this.updatedRec = [];
  this.deletedRec = [];
  this.updatedRec[recID] = false; //boolean for updated status
  var recRowID = this.recRowID(recID);
  this.addTr({id: recRowID});
  for (var a = 0; a < this.recModel.length; a++){
    var attributes = {};

    if(this.recModel[a].width !="") attributes.width = this.recModel[a].width;
    if(this.recModel[a].colSpan !="") attributes.colSpan = this.recModel[a].colSpan;

    attributes.id = "recDatum" + recID + "x" + a;
    attributes.onclick = this.objName+".editRecData("+recID+", "+ a +");";

    //if this is an editable field sanatize the content
    if(this.recModel[a].field){
      this.addTd(attributes, this.HTMLSanitize(rec[a].contents));
    }else if(this.recModel[a].deleteButton == "true"){
      this.addTd(attributes, '<div id="deleteButton' + recID + '" class="' + this.deleteModel.button.buttonClass + '" onclick="' + this.objName + '.deleteRec('+recID+');">' + this.deleteModel.button.caption + '</div>');
    }else{
      this.addTd(attributes, rec[a].contents);
    }
  }
  return recRowID;
}

recordTable.prototype.editRecData = function(recID, column){
  if(!this.updatedRec[recID] && this.recModel[column].field && !this.deletedRec[recID]){
    for (var a = 0; a < this.recModel.length; a++){
      if (this.recModel[a].field){
        this.updatedRec[recID] = true;
        var maxlength = "";
        var inputClass= "";
        if (this.recModel[a].maxlength){
          maxlength = ' maxlength="' + this.recModel[a].maxlength + '"';
        }
        if (this.recModel[a].inputClass) inputClass = ' class = "' + this.recModel[a].inputClass + '"';
        originalText = document.getElementById("recDatum" + recID + "x" + a).innerHTML;
        document.getElementById("recDatum" + recID + "x" + a).innerHTML = '<input id="recInput' + recID + 'x' + a + '" type="text"' + inputClass + maxlength +' value="' + this.textSanitize(originalText) + '"></input>';
      }
    }
  }
}

recordTable.prototype.deleteRec = function(recID){
  for (var a = 0; a < this.recModel.length; a++){
    if (this.recModel[a].field){
      if (this.updatedRec[recID]){
        document.getElementById("recDatum" + recID + "x" + a).innerHTML = this.HTMLSanitize(document.getElementById("recInput" + recID + "x" + a).value);
      }
    }else{//not a field
      document.getElementById("recDatum" + recID + "x" + a).innerHTML = "";
    }
  }
  this.updatedRec[recID] = false;

//  document.getElementById("deleteButton"+recID).style.display = "none";
  this.setAttribute(document.getElementById("recRowID"+recID),"class",this.deleteModel.recordClass);
  this.numRecs--;
  this.deletedRec[recID] = true;
  if (this.numNewRecs == 0)
    this.addNewRec();
}

/*
//returns object
recordTable.prototype.getChanges = function(){
  var changes = {updates:{}};
  var record = {};
  var records = {};
  for (var recID in this.updatedRec){  
    record = {};
    for (var column = 0; column < this.recModel.length; column++){
      if (this.updatedRec[recID] == true && this.recModel[column].field){
        record[this.recModel[column].field] = document.getElementById("recInput" + recID + "x" + column).value;
      }
    }
    records[recID] = record;
  }
  changes.updates = records;
return changes;
}
*/

recordTable.prototype.HTMLSanitize = function(data){
  data = data.replace(/&/g, "&amp;");
  data = data.replace(/</g, "&lt;");
  data = data.replace(/>/g, "&gt;");
  data = data.replace(/"/g, "&quot;");
  return data;
}

recordTable.prototype.jsonSanitize = function(data){
  data = data.replace(/\\/g, "\\\\\\\\");
  data = data.replace(/'/g, "''");
  data = data.replace(/\"/g, "\\\"");
  return data;
}

recordTable.prototype.textSanitize = function(data){
  data = data.replace(/\"/g, "&quot;");
  return data;
}

//returns json string
recordTable.prototype.getChanges = function(){

// inserts

  var changes = '{"inserts":{';
  firstRec = true;
  for (var recID in this.insertedRec){  
    if (this.insertedRec[recID] == true){
      firstRec == true? firstRec = false:changes += ",";
      changes += '"' + recID + '":{';
      firstColumn = true;
      for (var column = 0; column < this.recModel.length; column++){
        if (this.recModel[column].field){
          firstColumn == true? firstColumn = false:changes += ",";
          changes += '"' + this.recModel[column].field + '":"' + this.jsonSanitize(document.getElementById("newRecInput" + recID + "x" + column).value) +'"';
        }
      }
      changes += "}";
    }
  }
  changes += "}";


// updates

  changes += ', "updates":{';
  firstRec = true;
  for (var recID in this.updatedRec){  
    if (this.updatedRec[recID] == true){
      firstRec == true? firstRec = false:changes += ",";
      changes += '"' + recID + '":{';
      firstColumn = true;
      for (var column = 0; column < this.recModel.length; column++){
        if (this.recModel[column].field){
          firstColumn == true? firstColumn = false:changes += ",";
          changes += '"' + this.recModel[column].field + '":"' + this.jsonSanitize(document.getElementById("recInput" + recID + "x" + column).value) +'"';
        }
      }
      changes += "}";
    }
  }
  changes += "}";

//end updates
  changes += ', "deletes":[';
  firstRec = true;
  for(recID in this.deletedRec){
    if (this.deletedRec[recID] == true){
      firstRec == true? firstRec = false:changes += ",";
      changes += '"' + recID + '"';
    }
  }
  changes += "]}";

//  alert(changes);

  return changes;
}
