/* The following function creates a new input field and then calls datePickerController.create();
   to dynamically create a new date-picker widgit for it */
function newline() {
        var total = document.getElementById("newline-wrapper").getElementsByTagName("div").length;

        // No more than ten new inputs
        if(!total) {
                document.getElementById("newline").style.display = "none";
                return false;
        }

        total++;
        
        // Clone the first div in the series
        var div = document.getElementById("newline-wrapper").getElementsByTagName("div")[0].cloneNode(true);

        // Reset the cloned labels "for" attributes
        var labels = div.getElementsByTagName('label');
        
        for(var i = 0, lbl; lbl = labels[i]; i++) {
                // Set the new labels "for" attribute
                if(lbl["htmlFor"]) {
                        lbl["htmlFor"] = lbl["htmlFor"].replace(/[0-9]+/g, total);
                } else if(lbl.getAttribute("for")) {
                        lbl.setAttribute("for", lbl.getAttribute("for").replace(/[0-9]+/, total));
                }
        }
        
        // Reset the inputs name and id attributes
        var inputs = div.getElementsByTagName('input');
        for(var i = 0, inp; inp = inputs[i]; i++) {
                // Set the new inputs id and name attribute
                inp.id = inp.name = inp.id.replace(/[0-9]+/g, total);
                if(inp.type == "text") inp.value = "";
        }

        // Reset the button name and id attributes
        var inputs = div.getElementsByTagName('button');
        for(var i = 0, inp; inp = inputs[i]; i++) {
                inp.id = inp.name = inp.id.replace(/[0-9]+/g, total);
        }

        // Create a delete button
        var deleteBut = document.createElement('button');
        deleteBut.className = "delete-button";
        deleteBut.appendChild(document.createTextNode('X'));
        deleteBut.onclick = destroyLine;
        
        // Add the delete button to the wrapper div
        div.appendChild(deleteBut);

        // DOM inject the wrapper div
        document.getElementById("newline-wrapper").appendChild(div);

        // Call the create method to create and associate a new date-picker widgit with the new input
        datePickerController.create();

        // Stop the event
        return false;
}

function resetLineNumbers() {
        var divs = document.getElementById("newline-wrapper").getElementsByTagName("div");
        for(var i = 0, div; div = divs[i]; i++) {
                var inps = div.getElementsByTagName('input');
                for(var j = 0, inp; inp = inps[j]; j++) {
                        inp.id = inp.name = inp.id.replace(/[0-9]+/g, i+1);
                }
                var buts = div.getElementsByTagName('button');
                for(var j = 0, but; but = buts[j]; j++) {
                        but.id = but.name = but.id.replace(/[0-9]+/g, i+1);
                }
        }
}

function destroyLine() {
        this.parentNode.parentNode.removeChild(this.parentNode);
        resetLineNumbers();
        datePickerController.create();
}