/*****************************************/
/** Usable Forms 1.0, May 2003          **/
/** Written by ppk, www.quirksmode.org  **/
/** Instructions for use on my site     **/
/**                                     **/
/** You may use or change this script   **/
/** only when this copyright notice     **/
/** is intact.                          **/
/**                                     **/
/** If you extend the script, please    **/
/** add a short description and your    **/
/** name below.                         **/
/*****************************************/
/** Extended and rearranged             **/
/** for use as form manipulator for CC  **/
/** forms by MK.                        **/
/** Added multiple show options per     **/
/** Component.                          **/
/** Added update attribute for calling  **/
/** javascript functions.               **/
/*****************************************/

if (document.getElementById && document.getElementsByTagName && document.createElement &&
	!(navigator.userAgent.indexOf('MSIE 5') != -1 && navigator.userAgent.indexOf('Mac') != -1))
		document.write('<style>.accessibility{display: none}</style>');

function prepareForm() {
	var selects = document.getElementsByTagName('select');
	for (var i=0;i<selects.length;i++) {
		if (selects[i].options.length != 0) {
			if (selects[i].options[selects[i].selectedIndex].getAttribute('show')) {
				selects[i].onchange = arrangeFormFields;
			}
		}
		if (!isInCubbyForm(selects[i])) {
			updateFields(selects[i]);
		}
	}
	var inputs = document.getElementsByTagName('input');
	for (var i=0; i<inputs.length; i++) {
        if (inputs[i].getAttribute('show')) {
            inputs[i].onchange = arrangeFormFields;
        }
		if (!isInCubbyForm(inputs[i]) && (inputs[i].type == 'checkbox' || inputs[i].type == 'radio' || inputs[i].type == 'text')) {
			updateFields(inputs[i]);
		}
	}
	document.onclick = arrangeFormFields;
}
function arrangeFormFields(e) {
try {
	if (!e)
	var e = window.event;
	var element = (e.target) ? e.target : e.srcElement;

	if (!(element.nodeName == 'SELECT' && e.type == 'change') && !(element.nodeName == 'INPUT' && element.getAttribute('show'))) return;
	   updateFields(element);
	} catch (er) {}
}

function updateFields(element) {
	if (element.type == 'checkbox' || element.type == 'radio') {
		removeRowsByElement(element); // single checkbox or radio button element
		if (element.checked && !isInCubbyForm(element) && invokeUpdate(element)) {
			insertRowsByElement(element);
		}
	} else {
		removeRowsByElement(element); // single text element
		if (!isInCubbyForm(element) && invokeUpdate(element)) {
			insertRowsByElement(element);
		}
	}
}
function invokeUpdate(element) {
	var updateResult = true;
	var updateMethodStr = element.getAttribute('update');
	if (updateMethodStr != null) {
		var updateMethodArray = updateMethodStr.split(';');
		for (var i=0; i<updateMethodArray.length;i++) {
			var updateMethod = updateMethodArray[i].replace(/this/, 'element');
			updateResult = eval(updateMethod);
		}
	}
	return updateResult;
}
function insertRowsByElement(element) {
	var j = 1;
	var rowNames = new Array();
	var rowName = null;
	if (element.type == 'select-one') {
		if (element.options.length != 0) {
			rowName = element.options[element.selectedIndex].getAttribute('show');
		}
		while (rowName != null) {
			rowNames.push(rowName);
			rowName = null;
			if (element.options.length != 0) {
				rowName = element.options[element.selectedIndex].getAttribute('show' + j++);
			}
		}
	} else {
		rowName = element.getAttribute('show');
		while (rowName != null) {
			rowNames.push(rowName);
			rowName = element.getAttribute('show' + j++);
		}
	}
	for (var k=0;k<rowNames.length;k++) {
		moveToMainForm(rowNames[k]);
	}
}
function removeRowsByElement(element) {

	var j = 1;
	var rowNames = new Array();
	var rowName = null;
	if (element.type == 'select-one') {
		rowName = element.getAttribute('hide');
		while (rowName != null) {
			rowNames.push(rowName);
			rowName = element.getAttribute('hide' + j++);
		}
	} else {
		rowName = element.getAttribute('show');
		while (rowName != null) {
			rowNames.push(rowName);
			rowName = element.getAttribute('show' + j++);
		}
        if (element.type == 'radio' && element.checked) {
            rowName = element.getAttribute('hide');
            while (rowName != null) {
                rowNames.push(rowName);
                rowName = element.getAttribute('hide' + j++);
            }
        }
	}
	for (var j=0;j<rowNames.length;j++) {
		moveToCubbyForm(rowNames[j]);
	}
}
function getAllRows(rowName) {
	var rows = new Array();
	var x = document.getElementsByTagName('TR');
	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('dynaRow') == rowName) {
			rows.push(x[i]);
		}
	}
	return rows;
}
function moveToCubbyForm(rowName) {

	if (rowName == 'none') return;
	var rows = getAllRows(rowName);
	if (rows[0] == null) return;
	if (isInCubbyForm(rows[0])) return;
	while (rows.length)	{
		// *** Add id before moving to cubbyForm ***
		var rel = rows[0].getAttribute('dynaRow');
		if (!document.getElementById(rel)) {
			var newMarker = document.createElement('TR');
			newMarker.id = rel;
			rows[0].parentNode.replaceChild(newMarker,rows[0]);
		}
		var row = rows.shift();
		document.getElementById('CubbyForm').appendChild(row);
		var selects = row.getElementsByTagName('select');
		for (var i=0; i<selects.length; i++) {
			updateFields(selects[i]);
		}
		var inputs = row.getElementsByTagName('input');
		for (var i=0; i<inputs.length; i++) {
			if (inputs[i].type == 'checkbox' || inputs[i].type == 'radio' || inputs[i].type == 'text') {
				updateFields(inputs[i]);
			}
		}
	}
}
function moveToMainForm(rowName) {
	if (rowName == 'none') return;
	var rows = getAllRows(rowName);
	if (rows[0] == null) return;
	if (!isInCubbyForm(rows[0])) return;
	var insertPosition = document.getElementById(rowName);
	while (rows.length) {
		var row = rows.shift();

		insertPosition.parentNode.insertBefore(row,insertPosition);
		var selects = row.getElementsByTagName('select');
		for (var i=0; i<selects.length; i++) {
			updateFields(selects[i]);
		}
		var inputs = row.getElementsByTagName('input');
		for (var i=0; i<inputs.length; i++) {
			if (inputs[i].type == 'checkbox' || inputs[i].type == 'radio' || inputs[i].type == 'text') {
				updateFields(inputs[i]);
			}
		}
	}
}
function isInCubbyForm(obj) {
	while(obj.nodeName != 'BODY') {
		obj=obj.parentNode;
		if (obj.id == 'CubbyForm') return true;
	}
	return false;
}
function new_push() {
	var p = 0;
	for (p = 0; p < arguments.length; p++) {
		this[this.length] = arguments[p];
	}
	return this.length;
}
if (typeof Array.prototype.push == 'undefined') {
	Array.prototype.push = new_push;
}
function new_shift() {
	var s = 0;
	var response = this[0];
	for (s = 0; s < this.length-1; s++) {
		this[s]=this[s+1];
	}
	this.length--;
	return response;
}
if (typeof Array.prototype.shift == 'undefined') {
	Array.prototype.shift = new_shift;
}