//coded 03-05-09 By Chris Anderson.
//For use with the Faculty Network Share form to allow for the addition and removal of Accessors
///in the from of text box inputs in real time.
var numBoxes = 1;

	function addBox(frmForm)
	{
	
		
	
		if (document.getElementById && document.appendChild && document.removeChild)
  		{
			//make sure that the user is not attempting to exceed the allowed limit of text boxes.
			//if so, change nothing and output an alert to the user.
			//otherwise, create new text box set as normal.
			
			if(numBoxes < 10)
			{
		
				numBoxes++;
			
				var copy_target;
				
				var destination;
				
				var zaclone;
				
				var breakClone;
				
				var buttonClone;
			
				//alert("Creating label for new text box.");
				
				destination = document.getElementById("accessors_label");
				
				zaclone = destination.cloneNode(false);
				
				zaclone.id = "accessors_label" + numBoxes;
				
				//alert("Creating Destination");
				
				copy_target = "accessors" + ((numBoxes-1)+"");
				
				//alert("Copy Target is " + copy_target);
				
				destination = document.getElementById(copy_target);
				
				//add label
				destination.parentNode.appendChild(zaclone);
				
				//alert("Cloning node");
				
				zaclone = destination.cloneNode(false);
				
				//.nodeName returns the name of the tag, while .getNodeName() returns the actual name attribute of the element
				//alert("Clone's tag is: " + zaclone.nodeName);
				
				//alert("Clone's name is: " + zaclone.getNodeName);
				
				//be sure to set text value to the empty string or else it will have the same value as the copied box
				zaclone.value = "";
				
				zaclone.name = "accessors" + numBoxes;
				
				zaclone.id = "accessors" + numBoxes;
				
				//alert("appending to destination");
				
				destination.parentNode.appendChild(zaclone);
				
				//alert("Appenditure successful.");
				
				//Now, for this form we need to add a portion of text.
				
				var textDiv = document.createElement('strong');
				
				textDiv.id = "accessorsBold" + numBoxes;
				
				textDiv.innerHTML = ' Class Year: ';
				
				zaclone.parentNode.appendChild(textDiv);
				
				//add secondary field for Class Year of Accessor.
				//Note that this portion of the code uses a different technique, as it was 
				//coded by myself whilst learning more and cleaner techniques of dynamic page alteration through javascript.
				//I am also told that this method is 100% guaranteed to not leak memory. I am unsure about the old method.
				
				classBox = document.createElement("input");
				
				classBox.type = "text";
				
				classBox.name="accessorsYear" + numBoxes;
				classBox.id="accessorsYear" + numBoxes;
				classBox.value="";
				classBox.title="Class Year of Individual Needing Access";
				//classBox.maxlength="4";
				//for some reason, maxlength is the only one which must be set with 'setAttribute'.
				//.maxlength = 4 has no effect.
				classBox.setAttribute("maxlength", 4);
				classBox.size = "5";
				
				zaclone.parentNode.appendChild(classBox);
				
				//put in buffer of text before the button
				
				textBuffer = document.createElement("font");
				
				textBuffer.id = "accessorsBuffer" + numBoxes;
				
				textBuffer.innerHTML = " ";
				
				zaclone.parentNode.appendChild(textBuffer);
				
				//add button for self-removal
				
				destination = document.getElementById("newBox");
				
				buttonClone = destination.cloneNode(false);
				
				buttonClone.name = "removeBox" + numBoxes;
				
				buttonClone.id= numBoxes;
				
				buttonClone.value = "Remove";
				
				//alert("Event was " + buttonClone.onclick);
				
				buttonClone.onclick = function onclick(event) {
		removeBox(this.id);
	};
	
				//alert("Event Now: " + buttonClone.onclick);
				
				zaclone.parentNode.appendChild(buttonClone);
				
				
				
				//add break
				
				destination = document.getElementById("accessors_break");
				
				breakClone = destination.cloneNode(false);
				
				breakClone.id = "accessors_break" + numBoxes;
				
				zaclone.parentNode.appendChild(breakClone);
				
				
				//modify form hidden variable to reflect new number of total accessors for processing purposes
				
				destination = document.getElementById("total_accessors");
				
				destination.value = numBoxes;
				
				copy_target = null;
				
				destination = null;
				
				zaclone = null;
				
				buttonClone = null;
				
				breakClone = null;
			
			}
			
			else
			{
				alert("You may only request access for up to a maximum of 10 individuals. If you should require more accessors, please describe your situation in the comments section and you will be contacted after your request has been processed.");
			}
			
			
		}
		
		return true;
		
	}
	
	function removeBox(target_number)
	{
	
		var target_number = (target_number*1);
	
		//alert("Removing Box " + target_number);
	
		if (document.getElementById && document.appendChild && document.removeChild)
  		{
		
			if(numBoxes > 1)
			{
				
				
				var target;
				var target_name;
				
				
		
				//remove label
				target_name = "accessors_label" + target_number;
		
				target = document.getElementById(target_name);
				
				target.parentNode.removeChild(target);
				
				
				//remove actual text box
				target_name = "accessors" + target_number;
		
				target = document.getElementById(target_name);
				
				target.parentNode.removeChild(target);
				
				//remove boldness after it
				target_name = "accessorsBold" + target_number;
		
				target = document.getElementById(target_name);
				
				target.parentNode.removeChild(target);
				
				//remove textbox asking for year after it
				target_name = "accessorsYear" + target_number;
		
				target = document.getElementById(target_name);
				
				target.parentNode.removeChild(target);
				
				//remove label
				target_name = "accessorsBuffer" + target_number;
		
				target = document.getElementById(target_name);
				
				target.parentNode.removeChild(target);
				
				//remove removal button
				
				target_name = target_number;
				
				target = document.getElementById(target_name);
				
				target.parentNode.removeChild(target);
				
				//remove break after line of inputs
				target_name = "accessors_break" + target_number;
		
				target = document.getElementById(target_name);
				
				target.parentNode.removeChild(target);
				
				i = (0 + target_number + 1); 
				
				//alert("i is now " + i);
				
				//alert("numBoxes is now " + numBoxes);
				
				if(i <= numBoxes)
				{
					//alert("This should run");
				}
				
				while(i <= numBoxes)
				{
				
					//alert("Renaming Box " + i + " attributes to be " + (i-1));
					
					//rename initial labels
					target_name = "accessors_label" + i;
					
					//alert("Target name is " + target_name);
					
					new_name = "accessors_label" + (i-1);
					
					//alert("New name is " + new_name);
					
					target = document.getElementById(target_name);
					
					target.name = new_name;
					
					target.id = new_name;
					
					//alert("label renamed successfully.");
					
					//alert("Now renaming Textboxes.");
					
					target_name = "accessors" + i;
					
					//alert("Target name is " + target_name);
					
					new_name = "accessors" + (i-1);
					
					//alert("New name is " + new_name);
					
					target = document.getElementById(target_name);
					
					target.name = new_name;
					
					target.id = new_name;
					
					//alert("Text box renamed successfuly");
					
					
					//now, rename the class year text boxes
					
					target_name = "accessorsYear" + i;
					
					new_name = "accessorsYear" + (i-1);
					
					target = document.getElementById(target_name);
					
					target.name = new_name;
					
					target.id = new_name;
					
					//now, rename the class year blank text buffers at the end
					
					target_name = "accessorsBuffer" + i;
					
					new_name = "accessorsBuffer" + (i-1);
					
					target = document.getElementById(target_name);
					
					target.name = new_name;
					
					target.id = new_name;
					
					//now rename the bold parts
					
					target_name = "accessorsBold" + i;
					
					new_name = "accessorsBold" + (i-1);
					
					target = document.getElementById(target_name);
					
					target.name = new_name;
					
					target.id = new_name;
					
					//now, rename the removal buttons
					target_name = i;
					
					//alert("Now attempting to rename a removal box.");
					
					//alert("Target number is " + target_name);
					
					new_name = "removeBox" + (i-1);
					
					//alert("New name is " + new_name);
					
					target = document.getElementById(target_name);
					
					target.name = new_name;
					
					//alert("New id is :" +  (i-1));
					
					target.id = (i-1);
					
					//alert("Removal buttons renamed successfully.");
					
					target_name = "accessors_break" + i;
					
					//alert("Target name is " + target_name);
					
					new_name = "accessors_break" + (i-1);
					
					//alert("New name is " + new_name);
					
					target = document.getElementById(target_name);
					
					target.name = new_name;
					
					target.id = new_name;
					
					i++;
					
				}
				
				//alert("Decrementing accessors number");
			
				numBoxes--;
				
				//alert("Now down to " + numBoxes);
				
				destination = document.getElementById("total_accessors");
			
				destination.value = numBoxes;
				
				target_name = null;
				target_id = null;
				target = null;

			}
			
			else
			{
				alert("You must provide the name of at least one person to whom you wish to grant access.");
			}
		
			return true;
		
		}
	
	}