/**
 * param string entityType
 * param DOM-Element containerElement
 */
function addExistingEntity(entityType, containerElement)
{
	var parentWindowDocument = window.opener.document;

	// create new div node
	var div = parentWindowDocument.createElement('div');

	// create and initialize new input node
	var textbox = parentWindowDocument.createElement('input');
	textbox.setAttribute('id', entityType + '_' + containerElement.id);
	textbox.setAttribute('name', entityType + '_' + containerElement.id);
	textbox.setAttribute('readonly', 'true');
	textbox.setAttribute('type', 'text');

	var textboxValue = containerElement.childNodes[0].firstChild.nodeValue;
	if (containerElement.childNodes[1].hasChildNodes()) {
		textboxValue += ", ";
		textboxValue += containerElement.childNodes[1].firstChild.nodeValue;
	}
	if (containerElement.childNodes[2].hasChildNodes()) {
		textboxValue += " ";
		textboxValue += containerElement.childNodes[2].firstChild.nodeValue;
	}
	textbox.setAttribute('value', textboxValue);

	// create and initialize new button node
	var button = parentWindowDocument.createElement('input');
	button.setAttribute('type', 'button');
	button.setAttribute('value', 'Remove');
	button.setAttribute('onclick', 'this.parentNode.parentNode.removeChild(this.parentNode);');

	// append new textbox and button nodes to new div node
	div.appendChild(textbox);
	div.appendChild(button);
	
	// insert div node into main entity div node
	parentWindowDocument.getElementById(entityType + 'Container').insertBefore(div, parentWindowDocument.getElementById(entityType + 'Link'));
}



/**
 *
 */
function addNewEntity(entityType)
{
	var parentWindowDocument = window.opener.document;

	// generate a unique, random ID number for the entity between 0 and 100,000
	// (1,000,000 is just an arbitrary maximum)
	var entityId = Math.floor(Math.random() * 100000);
	while (parentWindowDocument.getElementById('new_' + entityType + '_textbox_' + entityId) != null) {
		entityId = Math.floor(Math.random() * 100000);
	}

	// create new div node
	var div = parentWindowDocument.createElement('div');

	// create and initialize new input node
	var textbox = parentWindowDocument.createElement('input');
	textbox.setAttribute('id', 'new_' + entityType + '_textbox_' + entityId);
	textbox.setAttribute('name', 'new_' + entityType + '_textbox_' + entityId);
	textbox.setAttribute('readonly', 'true');
	textbox.setAttribute('type', 'text');

	var textboxValue = document.getElementById('lastName').value;

	if (document.getElementById('suffix').value != "") {
		textboxValue += ", " + document.getElementById('suffix').value;
	}
	if (document.getElementById('firstName').value != "") {
		textboxValue += ", " + document.getElementById('firstName').value;
	}
	if (document.getElementById('middleName').value != "") {
		textboxValue += " " + document.getElementById('middleName').value;
	}
	textbox.setAttribute('value', textboxValue);

	// append new textbox node to new div node
	div.appendChild(textbox);

	// create and initialize new button node
	var button = parentWindowDocument.createElement('input');
	button.setAttribute('type', 'button');
	button.setAttribute('value', 'Remove');
	button.setAttribute('onclick', 'this.parentNode.parentNode.removeChild(this.parentNode);');

	// append new button node to new div node
	div.appendChild(button);

	// create hidden input nodes for each entity name field and add it to new div node
	var nameFieldIds = Array('lastName', 'suffix', 'firstName', 'middleName');
	for (var i = 0; i < nameFieldIds.length; i++) {
		var input = parentWindowDocument.createElement('input');
		input.setAttribute('id', 'new_' + entityType + '_' + nameFieldIds[i] + '_' + entityId);
		input.setAttribute('name', 'new_' + entityType + '_' + nameFieldIds[i] + '_' + entityId);
		input.setAttribute('type', 'hidden');
		input.setAttribute('value', document.getElementById(nameFieldIds[i]).value);
		div.appendChild(input);
	}

	// insert div node into main entity div node
	parentWindowDocument.getElementById(entityType + 'Container').insertBefore(div, parentWindowDocument.getElementById(entityType + 'Link'));
}