Javascript Dynamic Fieldset and sum
<p>I am using a script that adds a fieldset to my form but I am having one glitch that I can not figure out and would appreciate any insight you can provide. 
<br>
<br>Here is the trouble:<br>
The duplication of the fieldset works great.</p>

<p>I have another script which is creating a sum of the fields named "unitPrice" in the original fieldset. When I add a new fieldset with the new unitPrice fields do not get calculated into the sum. I am sure they are being renamed but what name would I use and how do I write it into the calculation so that I have a total of all of the unitPrice fields?<br><br>
Thanks for your help.
<br><br></p>

<p>Here is the Fieldset script:</p>

<pre><code>&lt;script&gt;

function insertAfter(newElement, targetElement)

{
var parent = targetElement.parentNode;
if (parent.lastChild == targetElement)
{
    parent.appendChild(newElement);
}
else
{
    parent.insertBefore(newElement, targetElement.nextSibling);
}
}


// Suffix + Counter
var suffix = ':';
var counter = 1;


// Clone nearest parent fieldset
function cloneMe(a)
{
// Increment counter
counter++;

// Find nearest parent fieldset
var original = a.parentNode;
while (original.nodeName.toLowerCase() != 'fieldset')
{
    original = original.parentNode;
}
var duplicate = original.cloneNode(true);

// Label - For and ID
var newLabel = duplicate.getElementsByTagName('label');
for (var i = 0; i &lt; newLabel.length; i++)
{
    var labelFor = newLabel[i].htmlFor
    if (labelFor)
    {
        oldFor = labelFor.indexOf(suffix) == -1 ? labelFor : labelFor.substring(0, labelFor.indexOf(suffix));
        newLabel[i].htmlFor = oldFor + suffix + counter;
    }
    var labelId = newLabel[i].id
    if (labelId)
    {
        oldId = labelId.indexOf(suffix) == -1 ? labelId : labelId.substring(0, labelId.indexOf(suffix));
        newLabel[i].id = oldId + suffix + counter;
    }
}

// Input - Name + ID
var newInput = duplicate.getElementsByTagName('input');
for (var i = 0; i &lt; newInput.length; i++)
{
    var inputName = newInput[i].name
    if (inputName)
    {
        oldName = inputName.indexOf(suffix) == -1 ? inputName : inputName.substring(0, inputName.indexOf(suffix));
        newInput[i].name = oldName + suffix + counter;
    }
    var inputId = newInput[i].id
    if (inputId)
    {
        oldId = inputId.indexOf(suffix) == -1 ? inputId : inputId.substring(0, inputId.indexOf(suffix));
        newInput[i].id = oldId + suffix + counter;
    }
}

// Select - Name + ID
var newSelect = duplicate.getElementsByTagName('select');
for (var i = 0; i &lt; newSelect.length; i++)
{
    var selectName = newSelect[i].name
    if (selectName)
    {
        oldName = selectName.indexOf(suffix) == -1 ? selectName : selectName.substring(0, selectName.indexOf(suffix));
        newSelect[i].name = oldName + suffix + counter;
    }
    var selectId = newSelect[i].id
    if (selectId)
    {
        oldId = selectId.indexOf(suffix) == -1 ? selectId : selectId.substring(0, selectId.indexOf(suffix));
        newSelect[i].id = oldId + suffix + counter;
    }
}

// Textarea - Name + ID
var newTextarea = duplicate.getElementsByTagName('textarea');
for (var i = 0; i &lt; newTextarea.length; i++)
{
    var textareaName = newTextarea[i].name
    if (textareaName)
    {
        oldName = textareaName.indexOf(suffix) == -1 ? textareaName : textareaName.substring(0, textareaName.indexOf(suffix));
        newTextarea[i].name = oldName + suffix + counter;
    }
    var textareaId = newTextarea[i].id
    if (textareaId)
    {
        oldId = textareaId.indexOf(suffix) == -1 ? textareaId : textareaId.substring(0, textareaId.indexOf(suffix));
        newTextarea[i].id = oldId + suffix + counter;
    }
}

duplicate.className = 'duplicate';
insertAfter(duplicate, original);
}


// Delete nearest parent fieldset
function deleteMe(a)
{
var duplicate = a.parentNode;
while (duplicate.nodeName.toLowerCase() != 'fieldset')
{
    duplicate = duplicate.parentNode;
}
duplicate.parentNode.removeChild(duplicate);
}
 &lt;/script&gt;
</code></pre>

<p>Here is the script I am using for the calculation:</p>

<pre><code>&lt;script type="text/javascript"&gt;
function myFunction(){
var the_fields = document.getElementsByName("unitPrice");
var the_sum = 0;
for (var i=0; i&lt;the_fields.length; i++){
    if (the_fields[i].value != ""
    &amp;&amp; !isNaN(the_fields[i].value))
    {
        the_sum += Number(the_fields[i].value);
    }
}
document.repairform.sum.value = (the_sum.toFixed(2));

}
&lt;/script&gt;
</code></pre>
