/*
REPEATABLE FORM SET CONTROL GROUPS
*/

var formset = {
	TagName: "DIV",
	ClassName: "",
	PKField: "id",
	maxRows: 0,// use 0 for unlimited number

	/*
	PRIVATE: FIND THE NODE FOR THIS GROUP OF CONTROLS
	*/
	getFieldGroup: function(thisObj) {
		while (thisObj.tagName != "BODY" && (thisObj.tagName != this.TagName || (this.ClassName && thisObj.className && thisObj.className != this.ClassName))) {
			thisObj = thisObj.parentNode;
		}
		if (thisObj.tagName == "BODY")
			return null;
		else
			return thisObj;
	},

	/*
	PRIVATE: INSERT A ROW
	*/
	insertRow: function(rowToInsert, refRow, insBefore) {
		if (insBefore) {
			refRow.parentNode.insertBefore(rowToInsert, refRow);
		} else {
			if (refRow.nextSibling)
				refRow.parentNode.insertBefore(rowToInsert, refRow.nextSibling);
			else
				refRow.parentNode.appendChild(rowToInsert);
		}
	},

	/*
	PUBLIC: MOVE A ROW UP OR DOWN
	*/
	moveRow: function(thisRow, moveUp, callbackFunc) {
		thisRow = this.getFieldGroup(thisRow);
		var fsGroup = thisRow.parentNode;
		var prevRow = thisRow.previousSibling;
		var nextRow = thisRow.nextSibling;
		if (moveUp && prevRow) {
			fsGroup.insertBefore(fsGroup.removeChild(thisRow), prevRow);
		} else if (!moveUp && nextRow && nextRow.nextSibling) {
			fsGroup.insertBefore(fsGroup.removeChild(thisRow), nextRow.nextSibling);
		} else if (!moveUp && nextRow) {
			fsGroup.appendChild(fsGroup.removeChild(thisRow));
		}
		if (callbackFunc)
			callbackFunc(thisRow, fsGroup);
		return false;
	},

	/*
	PUBLIC: CLONE A CONTROL SET AND ADD IT TO THE GROUP
	*/
	addRow: function(thisObj, callbackFunc) {
		var thisFSObj = $(this.getFieldGroup(thisObj));
		if (thisFSObj && thisFSObj.childNodes && (this.maxRows < 1 || this.countRealChildNodes(thisFSObj.getParent()) < this.maxRows)) {
			var newNode = $(thisFSObj).cloneNode(true);
			this.insertRow(newNode, thisFSObj, false);
			this.initFSValues(newNode);
			this.renumber($(thisFSObj).getParent());
			if (callbackFunc)
				callbackFunc(newNode, thisFSObj);
		}
		return false;
	},

	/*
	PUBLIC: DELETE THE CURRENT CONTROL SET FROM THE GROUP
	*/
	removeRow: function(thisObj, callbackFunc) {
		var thisFSObj = this.getFieldGroup(thisObj);
		var fsGroup = $(thisFSObj).getParent();
		if (this.countRealChildNodes(fsGroup) < 2)
			this.initFSValues(thisFSObj);
		else
			fsGroup.removeChild(thisFSObj);
		this.renumber(fsGroup);
		if (callbackFunc)
			callbackFunc(fsGroup);
		return false;
	},

	// mozilla reports any whitespace around nodes as a child node 
	// so can't be trusted with proper node counts
	countRealChildNodes: function(fsGroup) {
		if (!$(fsGroup) || !$(fsGroup).getChildren()) return 0;
		return $(fsGroup).getChildren().length;
	},

	/*
	PUBLIC: CLEAR/INTIALIZE ALL FIELDS IN THE CONTROL SET
	*/
	initFSValues: function(thisFSObj) {
		var inputTags = thisFSObj.getElementsByTagName("input");
		var textareaTags = thisFSObj.getElementsByTagName("textarea");
		var selectTags = thisFSObj.getElementsByTagName("select");
		for (var i = 0; selectTags[i]; i++) {
			for (var j = 0; selectTags[i][j]; j++)
				selectTags[i][j].selected = false;
			if (!selectTags[i].multiple)
				selectTags[i][0].selected = true;
		}
		for (var i = 0; textareaTags[i]; i++) {
			textareaTags[i].value = "";
		}
		for (var i = 0; inputTags[i]; i++) {
			if (inputTags[i].type == "text" || inputTags[i].type == "hidden") {
				if (inputTags[i].readOnly) {
					inputTags[i].value = inputTags[i].value;
				} else if (inputTags[i].name == this.PKField) {
					inputTags[i].value = "0";
				} else {
					inputTags[i].value = "";
				}
			} else if (inputTags[i].type == "radio" || inputTags[i].type == "checkbox") {
				inputTags[i].checked = false;
			}
		}
	},

	/*
	PRIVATE: REAPPLY ANY RECORD NUMBERING THAT HAS BEEN SET FOR THE CONTROL GROUP
	*/
	renumber: function(thisFSGroup) {
		if (!thisFSGroup) return;
		var fs_nodes = thisFSGroup.getChildren();
		if (!fs_nodes) return;
		var rCount = 1;
		for (var i = 0; fs_nodes[i]; i++) {
			var rNum = this.getElementByClassName("fsRowNumber", $(fs_nodes[i]));
			if (rNum) {
				rNum.innerHTML = rCount;
				++rCount;
			}
		}
	},

	/*
	PRIVATE: FIND A NODE BY CLASS NAME WITHIN THE DEFINED SCOPE
	Uses Mootools extensions to simplify selection
	*/
	getElementByClassName: function(clsName, srchScope) {
		return $(srchScope).getElement('.'+clsName);
	}

};




