/*  =====================================  */
//  Checks against errors on all input fields

//  (c) 2006 Gaslight Media
//  http://www.gaslightmedia.com
//	Jamie Kahgee <jamie.kahgee@gmail.com>
/*  =====================================  */

//	our first child will be #1
var childCount = 1;
//	how many uploads are there currently on the page.
var	actualCount = 0;

/**
 *	Attach another file upload box to the page
 *	so users can upload as many files as they want
 *	and not have to refresh the page after each upload.
 */
function addChildField(e) {
	//	If we are appending the first upload, change the text.
	if (actualCount == 0)
		document.getElementById( 'mc' ).innerHTML = "Add another child";

	//	Create the label boxes and field identifiers
	var newLabel		= document.createElement( 'label' );
	newLabel.className	= "preField";

	var newLink			= document.createElement( 'a' );
	newLink.className	= "info";
	newLink.title		= "What is the childs name?";

	var newSpan			= document.createElement( 'span' );
	newSpan.innerHTML	= "Children (name / DOB). 00/00/00 format";

	//	Attach the label box, anchor, and span elements together
	newLink.appendChild(newSpan);
	newLabel.appendChild(newLink);

	//	Create the new file input field.
	var newChild	= document.createElement( 'input' );
	newChild.type	= 'text';
	newChild.name	= 'children[]';
	newChild.id		= 'child'+childCount;

	//	Create the new file input field.
	var newAge		= document.createElement( 'input' );
	newAge.type		= 'text';
	newAge.name		= 'ages[]';
	newAge.id		= 'age'+childCount;
	newAge.setAttribute( 'size', 6);

	//	Create a new link to remove that field.
	var removeChild			= document.createElement( 'a');
	removeChild.innerHTML	= 'remove';
	removeChild.name		= 'remove'+childCount;
	removeChild.id			= 'remove'+childCount;
	removeChild.className	= 'uploadlink removelink';

	//	Create a container to hold the two new fields.
	var container = document.createElement( 'div' );
	container.name	= 'con'+childCount;
	container.id	= 'con'+childCount;
	container.className = "oneField oneChoice";

	//	Put the newly created fields in the container.
	container.appendChild(newLabel);	//	field text
	container.appendChild(newChild);	//	input box
	container.appendChild(newAge);		//	input box
	container.appendChild(removeChild);	//	remove child link

	//	Attach the container to the document for all to see!
	document.getElementById( 'children' ).appendChild(container);
	addEvent(removeChild, 'click', removeChildField, false);

	//	Keep track of our kids
	childCount++;
	actualCount++;
}

function removeChildField(e)
{
	//	Obtain which element we are actually on.
	var targetElement = e.target || e.srcElement;
	//	Propagate to the elements parent
	while (targetElement.nodeType == 3 && targetElement.parentNode != null)
		targetElement = targetElement.parentNode;

	//	Our target element is inside of a container div
	//	we want to remove that container div from ITS parent
	//	<span id="children">
	//		<span id="container1">
	//			{target element and other stuff}
	//		</span>
	//	</span>
	targetElement.parentNode.parentNode.removeChild(targetElement.parentNode);
	//	Adjust the actual count of file uploads on the page
	actualCount--;

	//	If we are removing the last upload, change the text back.
	if (actualCount == 0)
		document.getElementById( 'mc' ).innerHTML = "Add more children";
}

//	Attach a listener to append more upload fields.
function addListeners(e) {
	var attachChild = document.getElementById( "mc" );
	addEvent(attachChild, 'click', addChildField, false);

	//	In case the form gets bounced back off the server
	//	after the server checks the submission, we need to verify
	//	how many children were entered and adjust our variables
	//	when the pages reloads so our javascript still works.
	
	//	Get all the children
	var prevChild = document.getElementById( "children" );
	//	how many children are entered.
	var headCount = prevChild.getElementsByTagName( "div" );
	//	Make sure to attach all the event listeners to the remove link 
	//	that way if they decide to remove a child they still can.
	for (var i = 1; i <= headCount.length; i++)
	{
		var child = document.getElementById( 'remove'+i);
		addEvent(child, 'click', removeChildField, false);
	}
	//	adjust what child count we are up too.
	childCount += headCount.length;
	//	adjust how many uploads we are showing on the page.
	actualCount = headCount.length;
}

function addEvent(elm, evType, fn, useCapture)
{
	//	Firefox likes this
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	//	IE likes this
	} else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	} else {
		elm['on' + evType] = fn;
	}
}

addEvent(window, 'load', addListeners, false);
