
function addLoadEvent(func) {
 var oldonload = window.onload;
 if (typeof window.onload != 'function') {
 window.onload = func;
 } else {
 window.onload = function() {
 oldonload();
 func();
 }
 }
}

addLoadEvent(addNewClone);




/*

// kick things off when user clicks button #add-clone
function addClone(){
 var addCloneButton = document.getElementById('add-clone');
 addCloneButton.onclick = addNewClone;
}
*/


// set fieldcount to be used in incrementing names, below
fieldcount = 1;

function addNewClone() {

 // grab panel & fieldset that contain form fields to clone
// var addClonedPanel = document.getElementById('add-clone-panel');
 var originalFieldset = document.getElementById('copyme');

 //clone #new-user
 var newClonedFieldset = originalFieldset.cloneNode(true);

//var newClonedFieldset = document.getElementById('copyme').cloneNode(true);

 //append number to each id and for
 newClonedFieldset.id = 'clone' + fieldcount;
//	alert(newClonedFieldset.id);
 addNumberToAttribute('label',newClonedFieldset);
 addNumberToAttribute('input',newClonedFieldset);
 addNumberToAttribute('select',newClonedFieldset);
 // note: if you're cloning other form field types like select, add them here

newClonedFieldset.style.display = 'block';
 //insert new cloned, incremented elements into page
 ////addClonedPanel.appendChild(newClonedFieldset);
 	var insertHere = document.getElementById('writehere');			// and insert it onto the page
	insertHere.parentNode.insertBefore(newClonedFieldset, insertHere);


 fieldcount++; //increment field count for next time
 return false; // block default behavior of submit button
}


function addNumberToAttribute(element, newClonedFieldset){

 elementList = newClonedFieldset.getElementsByTagName(element);
 for (i = 0; i < elementList.length; i++){
 numberAttribute(elementList[i], fieldcount);
 }
}

//add number to each new form field, to keep IDs unique
function numberAttribute(currentNode, numToUse) {

 if(currentNode.nodeType == 1 && currentNode.getAttribute('for')){
 var currentFor = currentNode.getAttribute('for');
 currentNode.setAttribute('for', (currentFor + '-' + numToUse)) ;
 }

 if((currentNode.nodeType == 1) && currentNode.getAttribute('id')){
 var currentId = currentNode.getAttribute('id');
 currentNode.setAttribute('id', (currentId + '-' + numToUse )) ;
 }
 
 if((currentNode.nodeType == 1) && currentNode.getAttribute('name')){
 var currentName = currentNode.getAttribute('name');
 currentNode.setAttribute('name', (currentName + '-' + numToUse )) ;
 }

 // note: if you added other form field types like select above, repeat the argument for these

}
