var myTableCounter = 0;
var myInputCounter =  0;

function createInput(aRow, aName) {
  myInputCounter ++;
  var theInput =  document.createElement("input");
  var theText = document.createTextNode(aName+":");
  theInput.style.width =  "100px";
  theInput.setAttribute("type", "text");
  theInput.setAttribute("name", "INPUT_TEXT_" + aName + "NUMERO" + myInputCounter);


  td1  =  document.createElement("td");
  td1.appendChild(theText);

  td2  =  document.createElement("td"); 
  td2.appendChild(theInput);


  aRow.appendChild(td1);
  aRow.appendChild(td2);

}

function createCheckbox(aRow, aName) {
  myInputCounter ++;
  var theInput =  document.createElement("input");
  var theText = document.createTextNode(aName+":");
  theInput.style.width =  "100px";
  theInput.setAttribute("type", "checkbox");
  theInput.setAttribute("name", "INPUT_CHECK_" + aName + "NUMERO" + myInputCounter);


  td1  =  document.createElement("td");
  td1.appendChild(theText);

  td2  =  document.createElement("td");
  td2.appendChild(theInput);


  aRow.appendChild(td1);
  aRow.appendChild(td2);

}

function deleteTable(aPart, aTable){
    var theTable = document.getElementById(aTable);

    if (theTable != null){
       document.getElementById(aPart).removeChild(theTable);
    }


}

function newAdress(aPart) {
    var theTableNumber;
    
    myTableCounter++;
    theTableNumber = myTableCounter;
    
    var theTable = document.createElement("table");
    theTable.setAttribute("id", "table" + theTableNumber);
   
    var theTBody = document.createElement("tbody");
    var theRow1 = document.createElement("tr");
    var theRow2 = document.createElement("tr");
    var theRow3 = document.createElement("tr");
    var theRow4 = document.createElement("tr");
    var theRow5 = document.createElement("tr");
    var theRow6 = document.createElement("tr");
    var theRow7 = document.createElement("tr");
    var theBR = document.createElement("br");


    theTable.appendChild(theTBody);
    createInput(theRow1, "Vorname");
    createInput(theRow1, "Nachname");

    createInput(theRow2, "Straße");
    createInput(theRow2, "Nummer");

    createInput(theRow3, "Postleizahl");
    createInput(theRow3, "Ort");

    createInput(theRow4, "Land");

    createInput(theRow5, "Etage");
    createCheckbox(theRow5, "Fahrstuhl");

    var theTdA = document.createElement("td");
    var theTdB = document.createElement("td");
    theRow6.appendChild(theTdA);
    theRow6.appendChild(theTdB);
    createInput(theRow6, "Fahrstuhlpersonenanzahl");

    createInput(theRow7, "Tragestrecke");

    theTBody.appendChild(theRow1);
    theTBody.appendChild(theRow2);
    theTBody.appendChild(theRow3);
    theTBody.appendChild(theRow4);
    theTBody.appendChild(theBR);
    theTBody.appendChild(theRow5);
    theTBody.appendChild(theRow6);
    theTBody.appendChild(theRow7);

    document.getElementById(aPart).appendChild(theTable);
    var theDelete = document.createElement("input");
    theDelete.setAttribute("type", "button");
    theDelete.setAttribute("name", "delete" + theTableNumber);
    theDelete.setAttribute("value", "Löschen");

    theDelete.onclick = function(){deleteTable(aPart, "table" + theTableNumber )};
    var theTr1 = document.createElement("tr");
    var theTd1 = document.createElement("td");


    theTd1.appendChild(theDelete);
    theTr1.appendChild(theTd1);
    theTBody.appendChild(theTr1);


    var theSeperatorRow = document.createElement("tr");
    var theSeperatorCell = document.createElement("td");
    theSeperatorCell.setAttribute("colspan", "4");

    var theHr = document.createElement("hr");
    theHr.setAttribute("size", "1");
    theHr.setAttribute("width", "300");

    theSeperatorCell.appendChild(theHr);
    theSeperatorRow.appendChild(theSeperatorCell)
    theTBody.appendChild(theSeperatorRow);

    var theSeperator = document.createElement("input");
    theSeperator.setAttribute("type", "hidden");
    theSeperator.setAttribute("name", "SEPERATOR" + theTableNumber);
    theSeperator.setAttribute("value", "SEPERATOR");

    theTBody.appendChild(theSeperator);

}