function modifyCode(codeId) {

  codeId = parseInt(str_filter(codeId));
  var resultPane = document.getElementById("result"+codeId);
  var find = document.getElementById("find"+codeId).value;
  var final = document.getElementById("final"+codeId).value;
  var func = document.getElementById("function"+codeId).value;
  if(func == "1") {
    resultPane.innerHTML = "<s>"+find+"</s><br />"+final;
  } else if(func == "2") {
    resultPane.innerHTML = find+"<br />"+final;
  } else if(func == "3") {
    resultPane.innerHTML = final+"<br />"+find;
  }

}

function addOptionBox(codeId) {

  if(codeId == "0") {
    newCodeId = 1;
  } else {
    codeId = parseInt(str_filter(codeId));
    newCodeId = codeId+1;
    var click = document.getElementById("click"+codeId);
    click.style.display = 'none';
  }

  document.getElementById("modifications").value = newCodeId;
  var modTable = document.getElementById('modTable');
  var newtr = document.createElement('tr');
  var newtdFirst = document.createElement('td');
  var newtdSecond = document.createElement('td');

  newtdFirst.innerHTML = 'Find this code in <input type="text" name="file'+newCodeId+'" value="client.java" />:<br /><textarea name="find'+newCodeId+'" id="find'+newCodeId+'" style="width: 400px;" onkeyup="modifyCode(this.id)"></textarea><br />and <select id="function'+newCodeId+'" name="function'+newCodeId+'" style="width: 200px;" onchange="modifyCode(this.id)"><option value="1">replace it with</option><option value="2">add after</option><option value="3">add before</option></select> this<br /><textarea name="final'+newCodeId+'" id="final'+newCodeId+'" style="width: 400px;" onkeyup="modifyCode(this.id)"></textarea><br /><a href="javascript:;" id="click'+newCodeId+'" onclick="addOptionBox(this.id)"><img src="static/images/add.png" style="vertical-align: middle;" /> <span style="vertical-align: middle;">Add another option box...</span></a><br />';
  newtdFirst.style.width = '400px';
  newtdSecond.innerHTML = '<div id="result'+newCodeId+'" style="text-align: center;">&nbsp;</div>';
  newtdSecond.style.width = '300px';

  newtr.appendChild(newtdFirst);
  newtr.appendChild(newtdSecond);
  modTable.appendChild(newtr);

  scrollDown();

}


function str_filter(s) {
  filtered = "abcdefghijklmnopqrstuvwxyz";
  var i;
  var rs = "";
  for (i = 0; i < s.length; i++) {
    var c = s.charAt(i);
    if (filtered.indexOf(c) == -1)
      rs += c;
  }
  return rs;
}
function scrollDown(){
  dh=document.body.scrollHeight
  ch=document.body.clientHeight
  if(dh>ch) {
    moveme=dh-ch
    window.scrollTo(0,moveme)
  }
}
