// JavaScript Document

function addepc() {
	tb = document.getElementById('epcparts');
	if (!tb.added) { tb.raw = tb.innerHTML; tb.added=true; tb.count=1; }
	//updateAllDOMFields(document.getElementById('epcForm'));
	tb.count++;
	//var rawnew = new String(cleanTab);
	//tn = tb.cloneNode(true);
	tn = cleanNode.cloneNode(true);
	insertAfter(tn,tb);
	tn.innerHTML = tn.innerHTML.replace(/\[epc1\]/g,'[epc'+tb.count+']');
}

function insertAfter(newElement, referenceElement){
  //referenceElement.parentNode.insertBefore(newElement, referenceElement.nextSibling);
  referenceElement.parentNode.appendChild(newElement);
} 

function debug(m) {
alert(m);
}

function updateAllDOMFields(theForm){
var inputNodes = getAllFormFields(theForm);
for(x=0; x < inputNodes.length; x++){
var theNode = inputNodes[x];
updateDOMField(theNode)
}
}

function getAllFormFields(theForm){
try{
var inputFields = theForm.getElementsByTagName("input");
var selectFields = theForm.getElementsByTagName("select");
var textFields = theForm.getElementsByTagName("textarea");
var array = new Array(inputFields + selectFields + textFields);
for(i=0; i < array.length; i++){
for(x=0; x < inputFields.length; x++){
array[i] = inputFields[x];
i++
}
for(a=0; a < selectFields.length; a++){
array[i] = selectFields[a];
i++
}
for(t=0; t < textFields.length; t++){
array[i] = textFields[t];
i++
}
}
}
catch(e){alert("Error when evoking getAllFormFields(): \nSomething is probably wrong with the form you passed in\n\n"+e.message)}
return array;
}
function updateDOMField(inputField) {
    // if the inputField ID string has been passed in, get the inputField object
    if (typeof inputField == "string") {
        inputField = document.getElementById(inputField);
    }

    if (inputField.type == "select-one") {
        for (var i=0; i<inputField.options.length; i++) {
            if (i == inputField.selectedIndex) {
                inputField.options[inputField.selectedIndex].setAttribute("selected","selected");
            }
        }
    } else if (inputField.type == "text") {
        inputField.setAttribute("value",inputField.value);
    } else if (inputField.type == "textarea") {
        inputField.setAttribute("value",inputField.value);
		inputField.innerHTML=inputField.value;
    } else if ((inputField.type == "checkbox") || (inputField.type == "radio")) {
        if (inputField.checked) {
            inputField.setAttribute("checked","checked");
        } else {
            inputField.removeAttribute("checked");
        }
    }
}

function toggleAccess(control,toToggle) {
	tog = document.getElementById(toToggle);
	if (control.selectedIndex>=1) {
		tog.style.display='';
	} else {
		tog.style.display='none';
	}
 }




