/*******************************************************************************
	@file: wizard.js
	@description: handles dynamic behavior of xsdweaver process
	
 * Copyright (c) 2007 University of Central Florida.
 * 
 * Licensed under the Educational Community License, Version 1.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at
 * 
 *      http://www.opensource.org/licenses/ecl1.php
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 
*******************************************************************************/



/**
 * @global variables
 */
// current field to add and edit
var editType = null;
var fieldType = null;
var currentField = null;
var target = null;

// variables used for editing selection field
var lastOption = 1;
var optionsArray = new Array('1');

// form instance
var xsdWeaverForm;

// date pickers for date edit form
var datePicker;
var dateUpdateTargetForm = null;
var dateUpdateTarget = null;

// tree viewer for tree view
var treeObj;

// base path for wysiwyg FCKeditor
var wysiwygBasePath = 'js/lib/FCKeditor/';

// array that keeps track of all groups in the form
// !!! not used anymore...
var groupList = new Array();

// flag that will control if html output will be tabular or not
var outputTableHtml = true;


/**
 * @function: init()
 * @desc: initializes xsdweaver by creating a form instance
 * @dependency: needs global variable that is used as a form instance
 */
function init() {
	// instantiate the form
	xsdWeaverForm = new fw_form('xsdWeaverForm', '');
	
	// handle window resize
	window.onresize = function() {
		resizebgOverlay('backDrop');
		resizeeditArea('edit');
	}
	
	// gets the mouse coordinates continuously
	document.onmousemove = getMouseCoordinates;
	
	// for dropdown menu mouseover on IE
	if (document.all && document.getElementById) {
		var navRoot = $("nav");
		
		/* Get all the list items within the menu */
	
		var lis=navRoot.getElementsByTagName("LI");  
		for (i=0; i<lis.length; i++) {
		   /* If the LI has another menu level */
		    if(lis[i].lastChild.tagName=="UL"){
			/* assign the function to the LI */
			lis[i].onmouseover=function() {	
				this.className+=" over";
			}
			lis[i].onmouseout=function() {   
				this.className=this.className.replace(" over", "");
			}
		    }
		}
	 }
	 // end dropdown menu mouseover on IE
	 
	 // instantiate date picker
	 initDatepicker();
	 /*
	 datePicker = new YAHOO.widget.Calendar("datePicker","datePicker");
	 datePicker.onSelect = function(){
		if(dateUpdateTargetForm != null && dateUpdateTarget != null){
			var selectedDates = datePicker.getSelectedDates();
			var selectedDate = (selectedDates[0].getMonth() + 1) + "/" + selectedDates[0].getDate() + "/" + selectedDates[0].getFullYear();
			eval('document.' + dateUpdateTargetForm + '.' + dateUpdateTarget + '.value="' + selectedDate + '"');
		}
		hidethis('datePickerBox');
	 }
	 datePicker.render();
	 */
}



function initDatepicker()
{
	// instantiate date picker
	 datePicker = new YAHOO.widget.Calendar("datePicker","datePicker");
	 
	 var datePickerHandler = function(type,args,obj)
     {
		var dates = args[0];  
		var date = dates[0]; 
		// var selectedDate = date[1] + "/" + date[2] + "/" + date[0];
		var selectedDate = date[0] + "-" + date[1] + "-" + date[2];
		eval('document.' + dateUpdateTargetForm + '.' + dateUpdateTarget + '.value="' + selectedDate + '"');
		hidethis('datePickerBox');
	 };

	 datePicker.selectEvent.subscribe(datePickerHandler, datePicker, true);
	 
	 datePicker.render();
}


/**
 * @function: checkTextField
 * @desc: checks to see if text form fields are correctly filled out
 */
function checkTextField(){
	if(isEmpty($F('fw_textName'))){
		alert('Please enter field name');
		$('fw_textName').focus();
		return false;
	}
	else if(isEmpty($F('fw_textTitle'))){
		alert('Please enter field label');
		$('fw_textTitle').focus();
		return false;
	}
	else if(isEmpty($F('fw_textDesc'))){
		alert('Please enter field description');
		$('fw_textDesc').focus();
		return false;
	}
	else if(!isEmpty($F('fw_textMax')) && ( isNaN($F('fw_textMax')) || $F('fw_textMax') < 1 )){
		alert('Please enter valid maximum length');
		$('fw_textMax').focus();
		return false;
	}
	else if(!isEmpty($F('fw_textNumberMin')) && isNaN($F('fw_textNumberMin'))){
		alert('Please enter valid minimum value');
		$('fw_textNumberMin').focus();
		return false;
	}
	else if(!isEmpty($F('fw_textNumberMax')) && isNaN($F('fw_textNumberMax'))){
		alert('Please enter valid maximum value');
		$('fw_textNumberMax').focus();
		return false;
	}
	else if((!isEmpty($F('fw_textNumberMin')) && !isNaN($F('fw_textNumberMin'))) && (!isEmpty($F('fw_textNumberMax')) && !isNaN($F('fw_textNumberMax'))) && ($F('fw_textNumberMax') < $F('fw_textNumberMin'))){
		alert('Maximum value has to be greater than minimum value');
		$('fw_textNumberMax').focus();
		return false;
	}
	else {
		return true;
	}
}



/**
 * @function: resetTextField
 * @desc: resets text edit field
 */
function resetTextField(){
	document.textEditForm.fw_textName.value = '';
	document.textEditForm.fw_textTitle.value = '';
	document.textEditForm.fw_textDesc.value = '';
	document.textEditForm.fw_textRequired.checked = false;
	document.textEditForm.fw_textRepeated.checked = false;
	document.textEditForm.fw_textMax.value = '';
	document.textEditForm.fw_textPattern[0].selected = false;
	document.textEditForm.fw_textPattern[1].selected = false;
	document.textEditForm.fw_textPattern[2].selected = false;
	document.textEditForm.fw_textPattern[3].selected = false;
	document.textEditForm.fw_textNumberMin.value = '';
	document.textEditForm.fw_textNumberMax.value = '';
	hidethis('textEditNumberOptions');
}



/**
 * @function: addTextField
 * @desc: adds new text field to form, assumes that all the forms are checked prior to calling this function
 * @dependency: uses global variables
 */
function addTextField(){
	// initialize text field
	var newField = new fw_textField($F('fw_textName'), $F('fw_textTitle'), $F('fw_textDesc'));
	
	// set optional parameters
	if($F('fw_textRequired') == 'true'){
		newField.required = true;
	}
	else{
		newField.required = false;
	}
	
	if($F('fw_textRepeated') == 'true'){
		newField.repeated = true;
	}
	else{
		newField.repeated = false;
	}
	
	if(isEmpty($F('fw_textMax'))){
		newField.maxLength = -1;
	}
	else{
		newField.maxLength = $F('fw_textMax');
	}
	
	if(typeof $F('fw_textEmail') == 'undefined'){
		newField.email = false;
	}
	else{
		newField.email = true;
	}
	
	if(typeof $F('fw_textPhone') == 'undefined'){
		newField.phone = false;
	}
	else{
		newField.phone = true;
	}
	
	if(typeof $F('fw_textNumber') == 'undefined'){
		newField.number = false;
	}
	else{
		newField.number = true;
	}
	
	if(isEmpty($F('fw_textNumberMin'))){
		newField.minNumber = false;
	}
	else{
		newField.minNumber = $F('fw_textNumberMin');
	}
	
	if(isEmpty($F('fw_textNumberMax'))){
		newField.maxNumber = false;
	}
	else{
		newField.maxNumber = $F('fw_textNumberMin');
	}
	
	if($F('fw_assignedGroup') == -1){
		// add to the form
		xsdWeaverForm.formElements[xsdWeaverForm.formElements.length] = newField;
	}
	else{
		// add to the group
		// get array of parents
		var parentsArray = $F('fw_assignedGroup').split(',');
		
		// construct javascript to execute
		var toExecute = 'xsdWeaverForm';
		for(var i=0; i<parentsArray.length; i++){
			toExecute += '.formElements[' + parentsArray[i] + ']';
		}
		toExecute += '.addMember(newField)';

		try {
			// execute
			eval(toExecute);
		}
		catch(e){ // prepare for an error
			alert('Error in adding text field to a group! ' + e.description);
		}
	}
}



/**
 * @function: editTextField
 * @desc: edits existing text field to form, assumes that all the forms are checked prior to calling this function
 * @dependency: uses global variables
 */
function editTextField(){
	// initialize text field
	var newField = new fw_textField($F('fw_textName'), $F('fw_textTitle'), $F('fw_textDesc'));
	
	// set optional parameters
	if($F('fw_textRequired') == 'true'){
		newField.required = true;
	}
	else{
		newField.required = false;
	}
	
	if($F('fw_textRepeated') == 'true'){
		newField.repeated = true;
	}
	else{
		newField.repeated = false;
	}
	
	if(isEmpty($F('fw_textMax'))){
		newField.maxLength = -1;
	}
	else{
		newField.maxLength = $F('fw_textMax');
	}
	
	if(typeof $F('fw_textEmail') == 'undefined'){
		newField.email = false;
	}
	else{
		newField.email = true;
	}
	
	if(typeof $F('fw_textPhone') == 'undefined'){
		newField.phone = false;
	}
	else{
		newField.phone = true;
	}
	
	if(typeof $F('fw_textNumber') == 'undefined'){
		newField.number = false;
	}
	else{
		newField.number = true;
	}
	
	if(isEmpty($F('fw_textNumberMin'))){
		newField.minNumber = false;
	}
	else{
		newField.minNumber = $F('fw_textNumberMin');
	}
	
	if(isEmpty($F('fw_textNumberMax'))){
		newField.maxNumber = false;
	}
	else{
		newField.maxNumber = $F('fw_textNumberMin');
	}
	
	// now reassign to proper place
	if(target == null){ // no sub group
		if($F('fw_assignedGroup') == -1){
			// reassign to same field
			xsdWeaverForm.formElements[currentField] = newField;
		}
		else {
			// add to the group
			// get array of parents
			var parentsArray = $F('fw_assignedGroup').split(',');
			
			// construct javascript to execute
			var toExecute = 'xsdWeaverForm';
			for(var i=0; i<parentsArray.length; i++){
				toExecute += '.formElements[' + parentsArray[i] + ']';
			}
			toExecute += '.addMember(newField)';
	
			// add member
			try {
				// execute
				eval(toExecute);
			}
			catch(e){ // prepare for an error
				alert('Error in editing text field to a group! ' + e.description);
				return false;
			}
			
			// then delete from old one
			xsdWeaverForm.formElements.splice(currentField,1);
			
			updateGroupList();
		}
	}
	else {	// it was a group
		
		// get array of parents
		var parentsArray = target.split(',');
		
		// construct javascript to execute
		var toExecute = 'xsdWeaverForm';
		for(var i=0; i<parentsArray.length; i++){
			toExecute += '.formElements[' + parentsArray[i] + ']';
		}
		
		if($F('fw_assignedGroup') == -1){
			// remove from group
			try {
				eval( toExecute + '.deleteMember(' + currentField + ')' );
			}
			catch(e){
				alert('Error in editing text field to a group!' + e.description);
				return false;
			}
			
			// add to form
			xsdWeaverForm.formElements[xsdWeaverForm.formElements.length] = newField;
			
			// update group box
			updateGroupList();
		}
		else if($F('fw_assignedGroup') == target){	// same group
			// update in the group
			try {
				eval( toExecute + '.formElements[' + currentField + '] = newField' );
			}
			catch(e){
				alert('Error in editing text field to a group !!' + e.description);
				return false;
			}
		}
		else {	// assigned to different group
			// remove from group
			try {
				eval( toExecute + '.deleteMember(' + currentField + ')' );
			}
			catch(e){
				alert('Error in editing text field to a group!' + e.description);
				return false;
			}
			
			// add to the group
			var newParentsArray = $F('fw_assignedGroup').split(',');
			
			// construct javascript to execute
			var newToExecute = 'xsdWeaverForm';
			for(var i=0; i<newParentsArray.length; i++){
				newToExecute += '.formElements[' + newParentsArray[i] + ']';
			}
			newToExecute += '.addMember(newField)';
	
			try {
				// execute
				eval(newToExecute);
			}
			catch(e){ // prepare for an error
				alert('Error in editing text field to a group! ' + e.description);
				return false;
			}
			
			// update group box
			updateGroupList();
		}
	}
}



/**
 * @function: checkTextareaField
 * @desc: checks to see if textarea form fields are correctly filled out
 */
function checkTextareaField(){
	if(isEmpty($F('fw_textareaName'))){
		alert('Please enter field name');
		$('fw_textareaName').focus();
		return false;
	}
	else if(isEmpty($F('fw_textareaTitle'))){
		alert('Please enter field label');
		$('fw_textareaTitle').focus();
		return false;
	}
	else if(isEmpty($F('fw_textareaDesc'))){
		alert('Please enter field description');
		$('fw_textareaDesc').focus();
		return false;
	}
	else {
		return true;
	}
}



/**
 * @function: resetTextareaField
 * @desc: resets textarea edit field
 */
function resetTextareaField(){
	document.textareaEditForm.fw_textareaName.value = '';
	document.textareaEditForm.fw_textareaTitle.value = '';
	document.textareaEditForm.fw_textareaDesc.value = '';
	document.textareaEditForm.fw_textareaRequired.checked = false;
	document.textareaEditForm.fw_textareaRepeated.checked = false;
}



/**
 * @function: addTextareaField
 * @desc: adds new textarea field to form, assumes that all the forms are checked prior to calling this function
 * @dependency: uses global variables
 */
function addTextareaField(){
	// initialize text field
	var newField = new fw_textareaField($F('fw_textareaName'), $F('fw_textareaTitle'), $F('fw_textareaDesc'));
	
	// set optional parameters
	if($F('fw_textareaRequired') == 'true'){
		newField.required = true;
	}
	else{
		newField.required = false;
	}
	
	if($F('fw_textareaRepeated') == 'true'){
		newField.repeated = true;
	}
	else{
		newField.repeated = false;
	}
	
	if($F('fw_assignedGroup') == -1){
		// add to the form
		xsdWeaverForm.formElements[xsdWeaverForm.formElements.length] = newField;
	}
	else{
		// add to the group
		// get array of parents
		var parentsArray = $F('fw_assignedGroup').split(',');
		
		// construct javascript to execute
		var toExecute = 'xsdWeaverForm';
		for(var i=0; i<parentsArray.length; i++){
			toExecute += '.formElements[' + parentsArray[i] + ']';
		}
		toExecute += '.addMember(newField)';

		try {
			// execute
			eval(toExecute);
		}
		catch(e){ // prepare for an error
			alert('Error in adding textarea field to a group! ' + e.description);
		}
	}
	
}



/**
 * @function: editTextareaField
 * @desc: edit textarea field to form, assumes that all the forms are checked prior to calling this function
 * @dependency: uses global variables
 */
function editTextareaField(){
	// initialize text field
	var newField = new fw_textareaField($F('fw_textareaName'), $F('fw_textareaTitle'), $F('fw_textareaDesc'));
	
	// set optional parameters
	if($F('fw_textareaRequired') == 'true'){
		newField.required = true;
	}
	else{
		newField.required = false;
	}
	
	if($F('fw_textareaRepeated') == 'true'){
		newField.repeated = true;
	}
	else{
		newField.repeated = false;
	}
	
	// now reassign to proper place
	if(target == null){
		if($F('fw_assignedGroup') == -1){
			// reassign to same field
			xsdWeaverForm.formElements[currentField] = newField;
		}
		else {
			// add to the group
			// get array of parents
			var parentsArray = $F('fw_assignedGroup').split(',');
			
			// construct javascript to execute
			var toExecute = 'xsdWeaverForm';
			for(var i=0; i<parentsArray.length; i++){
				toExecute += '.formElements[' + parentsArray[i] + ']';
			}
			toExecute += '.addMember(newField)';
	
			// add member
			try {
				// execute
				eval(toExecute);
			}
			catch(e){ // prepare for an error
				alert('Error in editing textarea field to a group! ' + e.description);
				return false;
			}
			
			// then delete from old one
			xsdWeaverForm.formElements.splice(currentField,1);
			
			// update group box
			updateGroupList();
		}
	}
	else {	// it was a group
		
		// get array of parents
		var parentsArray = target.split(',');
		
		// construct javascript to execute
		var toExecute = 'xsdWeaverForm';
		for(var i=0; i<parentsArray.length; i++){
			toExecute += '.formElements[' + parentsArray[i] + ']';
		}
		
		if($F('fw_assignedGroup') == -1){
			// remove from group
			try {
				eval( toExecute + '.deleteMember(' + currentField + ')' );
			}
			catch(e){
				alert('Error in editing textarea field to a group!' + e.description);
				return false;
			}
			
			// add to form
			xsdWeaverForm.formElements[xsdWeaverForm.formElements.length] = newField;
			
			// update group box
			updateGroupList();
		}
		else if($F('fw_assignedGroup') == target){	// same group
			// update in the group
			try {
				eval( toExecute + '.formElements[' + currentField + '] = newField' );
			}
			catch(e){
				alert('Error in editing textarea field to a group !!' + e.description);
				return false;
			}
		}
		else {	// assigned to different group
			// remove from group
			try {
				eval( toExecute + '.deleteMember(' + currentField + ')' );
			}
			catch(e){
				alert('Error in editing textarea field to a group!' + e.description);
				return false;
			}
			
			// add to the group
			var newParentsArray = $F('fw_assignedGroup').split(',');
			
			// construct javascript to execute
			var newToExecute = 'xsdWeaverForm';
			for(var i=0; i<newParentsArray.length; i++){
				newToExecute += '.formElements[' + newParentsArray[i] + ']';
			}
			newToExecute += '.addMember(newField)';
	
			try {
				// execute
				eval(newToExecute);
			}
			catch(e){ // prepare for an error
				alert('Error in editing textarea field to a group! ' + e.description);
				return false;
			}
			
			// update group box
			updateGroupList();
			
		}
	}
	
}



/**
 * @function: checkWysiwygField
 * @desc: checks to see if wysiwyg form fields are correctly filled out
 */
function checkWysiwygField(){
	if(isEmpty($F('fw_wysiwygName'))){
		alert('Please enter field name');
		$('fw_wysiwygName').focus();
		return false;
	}
	else if(isEmpty($F('fw_wysiwygTitle'))){
		alert('Please enter field label');
		$('fw_wysiwygTitle').focus();
		return false;
	}
	else if(isEmpty($F('fw_wysiwygDesc'))){
		alert('Please enter field description');
		$('fw_wysiwygDesc').focus();
		return false;
	}
	else {
		return true;
	}
}



/**
 * @function: resetWysiwygField
 * @desc: resets wysiwyg edit field
 */
function resetWysiwygField(){
	document.wysiwygEditForm.fw_wysiwygName.value = '';
	document.wysiwygEditForm.fw_wysiwygTitle.value = '';
	document.wysiwygEditForm.fw_wysiwygDesc.value = '';
	document.wysiwygEditForm.fw_wysiwygRequired.checked = false;
	document.wysiwygEditForm.fw_wysiwygRepeated.checked = false;
}



/**
 * @function: addWysiwygField
 * @desc: adds new wysiwyg field to form, assumes that all the forms are checked prior to calling this function
 * @dependency: uses global variables
 */
function addWysiwygField(){
	// initialize text field
	var newField = new fw_wysiwygField($F('fw_wysiwygName'), $F('fw_wysiwygTitle'), $F('fw_wysiwygDesc'));
	
	// set optional parameters
	if($F('fw_wysiwygRequired') == 'true'){
		newField.required = true;
	}
	else{
		newField.required = false;
	}
	
	if($F('fw_wysiwygRepeated') == 'true'){
		newField.repeated = true;
	}
	else{
		newField.repeated = false;
	}
	
	if($F('fw_assignedGroup') == -1){
		// add to the form
		xsdWeaverForm.formElements[xsdWeaverForm.formElements.length] = newField;
	}
	else{
		// add to the group
		// get array of parents
		var parentsArray = $F('fw_assignedGroup').split(',');
		
		// construct javascript to execute
		var toExecute = 'xsdWeaverForm';
		for(var i=0; i<parentsArray.length; i++){
			toExecute += '.formElements[' + parentsArray[i] + ']';
		}
		toExecute += '.addMember(newField)';

		try {
			// execute
			eval(toExecute);
		}
		catch(e){ // prepare for an error
			alert('Error in adding wysiwyg field to a group! ' + e.description);
		}
	}
	
}



/**
 * @function: editWysiwygField
 * @desc: edit wysiwyg field to form, assumes that all the forms are checked prior to calling this function
 * @dependency: uses global variables
 */
function editWysiwygField(){
	// initialize text field
	var newField = new fw_wysiwygField($F('fw_wysiwygName'), $F('fw_wysiwygTitle'), $F('fw_wysiwygDesc'));
	
	// set optional parameters
	if($F('fw_wysiwygRequired') == 'true'){
		newField.required = true;
	}
	else{
		newField.required = false;
	}
	
	if($F('fw_wysiwygRepeated') == 'true'){
		newField.repeated = true;
	}
	else{
		newField.repeated = false;
	}
	
	// now reassign to proper place
	if(target == null){
		if($F('fw_assignedGroup') == -1){
			// reassign to same field
			xsdWeaverForm.formElements[currentField] = newField;
		}
		else {
			// add to the group
			// get array of parents
			var parentsArray = $F('fw_assignedGroup').split(',');
			
			// construct javascript to execute
			var toExecute = 'xsdWeaverForm';
			for(var i=0; i<parentsArray.length; i++){
				toExecute += '.formElements[' + parentsArray[i] + ']';
			}
			toExecute += '.addMember(newField)';
	
			// add member
			try {
				// execute
				eval(toExecute);
			}
			catch(e){ // prepare for an error
				alert('Error in editing wysiwyg field to a group! ' + e.description);
				return false;
			}
			
			// then delete from old one
			xsdWeaverForm.formElements.splice(currentField,1);
			
			// update group box
			updateGroupList();
		}
	}
	else {	// it was a group
		
		// get array of parents
		var parentsArray = target.split(',');
		
		// construct javascript to execute
		var toExecute = 'xsdWeaverForm';
		for(var i=0; i<parentsArray.length; i++){
			toExecute += '.formElements[' + parentsArray[i] + ']';
		}
		
		if($F('fw_assignedGroup') == -1){
			// remove from group
			try {
				eval( toExecute + '.deleteMember(' + currentField + ')' );
			}
			catch(e){
				alert('Error in editing wysiwyg field to a group!' + e.description);
				return false;
			}
			
			// add to form
			xsdWeaverForm.formElements[xsdWeaverForm.formElements.length] = newField;
			
			// update group box
			updateGroupList();
		}
		else if($F('fw_assignedGroup') == target){	// same group
			// update in the group
			try {
				eval( toExecute + '.formElements[' + currentField + '] = newField' );
			}
			catch(e){
				alert('Error in editing wysiwyg field to a group !!' + e.description);
				return false;
			}
		}
		else {	// assigned to different group
			// remove from group
			try {
				eval( toExecute + '.deleteMember(' + currentField + ')' );
			}
			catch(e){
				alert('Error in editing wysiwyg field to a group!' + e.description);
				return false;
			}
			
			// add to the group
			var newParentsArray = $F('fw_assignedGroup').split(',');
			
			// construct javascript to execute
			var newToExecute = 'xsdWeaverForm';
			for(var i=0; i<newParentsArray.length; i++){
				newToExecute += '.formElements[' + newParentsArray[i] + ']';
			}
			newToExecute += '.addMember(newField)';
	
			try {
				// execute
				eval(newToExecute);
			}
			catch(e){ // prepare for an error
				alert('Error in editing wysiwyg field to a group! ' + e.description);
				return false;
			}
			
			// update group box
			updateGroupList();
			
		}
	}
	
}



/**
 * @function: checkDateField
 * @desc: checks to see if date form fields are correctly filled out
 */
function checkDateField(){
	if(isEmpty($F('fw_dateName'))){
		alert('Please enter field name');
		$('fw_dateName').focus();
		return false;
	}
	else if(isEmpty($F('fw_dateTitle'))){
		alert('Please enter field label');
		$('fw_dateTitle').focus();
		return false;
	}
	else if(isEmpty($F('fw_dateDesc'))){
		alert('Please enter field description');
		$('fw_dateDesc').focus();
		return false;
	}
	else if(!isEmpty($F('fw_dateMin')) && !isDate($F('fw_dateMin'))){
		alert('Please enter valid minimum date');
		$('fw_dateMin').focus();
		return false;
	}
	else if(!isEmpty($F('fw_dateMax')) && !isDate($F('fw_dateMax'))){
		alert('Please enter valid maximum date');
		$('fw_dateMax').focus();
		return false;
	}
	else if(!isEmpty($F('fw_dateMin')) && !isEmpty($F('fw_dateMax')) && (parseDate($F('fw_dateMin')).getTime() > parseDate($F('fw_dateMax')).getTime())){
		alert('Maximum date is before minimum date');
		$('fw_dateMax').focus();
		return false;
	}
	else {
		return true;
	}
}



/**
 * @function: resetDateField
 * @desc: resets date edit field
 */
function resetDateField(){
	document.dateEditForm.fw_dateName.value = '';
	document.dateEditForm.fw_dateTitle.value = '';
	document.dateEditForm.fw_dateDesc.value = '';
	document.dateEditForm.fw_dateRequired.checked = false;
	document.dateEditForm.fw_dateRepeated.checked = false;
	document.dateEditForm.fw_dateMin.value = '';
	document.dateEditForm.fw_dateMax.value = '';
}



/**
 * @function: addDateField
 * @desc: adds new textarea field to form, assumes that all the forms are checked prior to calling this function
 * @dependency: uses global variables
 */
function addDateField(){
	// initialize text field
	var newField = new fw_dateField($F('fw_dateName'), $F('fw_dateTitle'), $F('fw_dateDesc'));
	
	// set optional parameters
	if($F('fw_dateRequired') == 'true'){
		newField.required = true;
	}
	else{
		newField.required = false;
	}
	
	if($F('fw_dateRepeated') == 'true'){
		newField.repeated = true;
	}
	else{
		newField.repeated = false;
	}
	
	if(!isEmpty($F('fw_dateMin'))){
		newField.minDate = $F('fw_dateMin');
	}
	else{
		newField.minDate = false;
	}
	
	if(!isEmpty($F('fw_dateMax'))){
		newField.maxDate = $F('fw_dateMax');
	}
	else{
		newField.maxDate = false;
	}
	
	if($F('fw_assignedGroup') == -1){
		// add to the form
		xsdWeaverForm.formElements[xsdWeaverForm.formElements.length] = newField;
	}
	else{
		// add to the group
		// get array of parents
		var parentsArray = $F('fw_assignedGroup').split(',');
		
		// construct javascript to execute
		var toExecute = 'xsdWeaverForm';
		for(var i=0; i<parentsArray.length; i++){
			toExecute += '.formElements[' + parentsArray[i] + ']';
		}
		toExecute += '.addMember(newField)';

		try {
			// execute
			eval(toExecute);
		}
		catch(e){ // prepare for an error
			alert('Error in adding date field to a group! ' + e.description);
		}
	}
}



/**
 * @function: editDateField
 * @desc: edit textarea field to form, assumes that all the forms are checked prior to calling this function
 * @dependency: uses global variables
 */
function editDateField(){
	// initialize text field
	var newField = new fw_dateField($F('fw_dateName'), $F('fw_dateTitle'), $F('fw_dateDesc'));
	
	// set optional parameters
	if($F('fw_dateRequired') == 'true'){
		newField.required = true;
	}
	else{
		newField.required = false;
	}
	
	if($F('fw_dateRepeated') == 'true'){
		newField.repeated = true;
	}
	else{
		newField.repeated = false;
	}
	
	if(!isEmpty($F('fw_dateMin'))){
		newField.minDate = $F('fw_dateMin');
	}
	else{
		newField.minDate = false;
	}
	
	if(!isEmpty($F('fw_dateMax'))){
		newField.maxDate = $F('fw_dateMax');
	}
	else{
		newField.maxDate = false;
	}
	
	// now reassign to proper place
	if(target == null){
		if($F('fw_assignedGroup') == -1){
			// reassign to same field
			xsdWeaverForm.formElements[currentField] = newField;
		}
		else {
			// add to the group
			// get array of parents
			var parentsArray = $F('fw_assignedGroup').split(',');
			
			// construct javascript to execute
			var toExecute = 'xsdWeaverForm';
			for(var i=0; i<parentsArray.length; i++){
				toExecute += '.formElements[' + parentsArray[i] + ']';
			}
			toExecute += '.addMember(newField)';
	
			// add member
			try {
				// execute
				eval(toExecute);
			}
			catch(e){ // prepare for an error
				alert('Error in editing date field to a group! ' + e.description);
				return false;
			}
			
			// then delete from old one
			xsdWeaverForm.formElements.splice(currentField,1);
			
			// update group box
			updateGroupList();
		}
	}
	else {	// it was a group
		
		// get array of parents
		var parentsArray = target.split(',');
		
		// construct javascript to execute
		var toExecute = 'xsdWeaverForm';
		for(var i=0; i<parentsArray.length; i++){
			toExecute += '.formElements[' + parentsArray[i] + ']';
		}
		
		if($F('fw_assignedGroup') == -1){
			// remove from group
			try {
				eval( toExecute + '.deleteMember(' + currentField + ')' );
			}
			catch(e){
				alert('Error in editing date field to a group!' + e.description);
				return false;
			}
			
			// add to form
			xsdWeaverForm.formElements[xsdWeaverForm.formElements.length] = newField;
			
			// update group box
			updateGroupList();
		}
		else if($F('fw_assignedGroup') == target){	// same group
			// update in the group
			try {
				eval( toExecute + '.formElements[' + currentField + '] = newField' );
			}
			catch(e){
				alert('Error in editing date field to a group !!' + e.description);
				return false;
			}
		}
		else {	// assigned to different group
			// remove from group
			try {
				eval( toExecute + '.deleteMember(' + currentField + ')' );
			}
			catch(e){
				alert('Error in editing date field to a group!' + e.description);
				return false;
			}
			
			// add to the group
			var newParentsArray = $F('fw_assignedGroup').split(',');
			
			// construct javascript to execute
			var newToExecute = 'xsdWeaverForm';
			for(var i=0; i<newParentsArray.length; i++){
				newToExecute += '.formElements[' + newParentsArray[i] + ']';
			}
			newToExecute += '.addMember(newField)';
	
			try {
				// execute
				eval(newToExecute);
			}
			catch(e){ // prepare for an error
				alert('Error in editing date field to a group! ' + e.description);
				return false;
			}
			
			// update group box
			updateGroupList();
		}
	}
}



/**
 * @function: checkSelectField
 * @desc: checks to see if select field is correctly filled out
 *        Assumes all input values are correct.
 */
function checkSelectField(){
	if(isEmpty($F('fw_selectionName'))){
		alert("Please enter the form name");
		document.selectionEditForm.fw_selectionName.focus();
		return false;
	}
	else if(isEmpty($F('fw_selectionTitle'))){
		alert("Please enter the form label");
		document.selectionEditForm.fw_selectionTitle.focus();
		return false;
	}
	else if(isEmpty($F('fw_selectionDesc'))){
		alert("Please enter the form description");
		document.selectionEditForm.fw_selectionDesc.focus();
		return false;
	}
	else if(!checkSelectOptions()){ // checks options
		return false;
	}
	else{
		// checked out
		return true;
	}
}



/**
 * @function: addSelectField
 * @desc: Adds select field to form
 */
function addSelectField(){
	// create form field object
	var newField = new fw_selectField($F('fw_selectionName'),$F('fw_selectionTitle'),$F('fw_selectionDesc'));
	
	// set optional parameters
	if($F('fw_selectionRequired') == 'true'){
		newField.required = true;
	}
	else{
		newField.required = false;
	}
	
	if($F('fw_selectionRepeated') == 'true'){
		newField.repeated = true;
	}
	else{
		newField.repeated = false;
	}
	
	// add options
	// loop through the choices and check if the values are correct
	for(var i=0; i<optionsArray.length; i++){
		// check if exists
		if(typeof eval('document.selectionEditForm.fw_selectFieldOption' + optionsArray[i]) != 'undefined'){
			// get basename
			var baseName = 'fw_selectFieldOption' + optionsArray[i];
			
			// add choice
			newField.addOption(eval('document.selectionEditForm.' +baseName + "_Label" + '.value'),eval('document.selectionEditForm.' +baseName + "_Value" + '.value'));
			
		}
	}
	
	if($F('fw_assignedGroup') == -1){
		// add to the form
		xsdWeaverForm.formElements[xsdWeaverForm.formElements.length] = newField;
	}
	else{
		// add to the group
		// get array of parents
		var parentsArray = $F('fw_assignedGroup').split(',');
		
		// construct javascript to execute
		var toExecute = 'xsdWeaverForm';
		for(var i=0; i<parentsArray.length; i++){
			toExecute += '.formElements[' + parentsArray[i] + ']';
		}
		toExecute += '.addMember(newField)';

		try {
			// execute
			eval(toExecute);
		}
		catch(e){ // prepare for an error
			alert('Error in adding select field to a group! ' + e.description);
		}
		
	}
	
	// exit editor
	resetSelectField();
}



/**
 * @function: editSelectField
 * @desc: edit select field to form
 */
function editSelectField(){
	// create form field object
	var newField = new fw_selectField($F('fw_selectionName'),$F('fw_selectionTitle'),$F('fw_selectionDesc'));
	
	// set optional parameters
	if($F('fw_selectionRequired') == 'true'){
		newField.required = true;
	}
	else{
		newField.required = false;
	}
	
	if($F('fw_selectionRepeated') == 'true'){
		newField.repeated = true;
	}
	else{
		newField.repeated = false;
	}
	
	// add options
	// loop through the choices and check if the values are correct
	for(var i=0; i<optionsArray.length; i++){
		// check if exists
		if(typeof eval('document.selectionEditForm.fw_selectFieldOption' + optionsArray[i]) != 'undefined'){
			// get basename
			var baseName = 'fw_selectFieldOption' + optionsArray[i];
			
			// add choice
			newField.addOption(eval('document.selectionEditForm.' +baseName + "_Label" + '.value'),eval('document.selectionEditForm.' +baseName + "_Value" + '.value'));
			
		}
	}
	
	// now reassign to proper place
	if(target == null){
		if($F('fw_assignedGroup') == -1){
			// reassign to same field
			xsdWeaverForm.formElements[currentField] = newField;
		}
		else {
			// add to the group
			// get array of parents
			var parentsArray = $F('fw_assignedGroup').split(',');
			
			// construct javascript to execute
			var toExecute = 'xsdWeaverForm';
			for(var i=0; i<parentsArray.length; i++){
				toExecute += '.formElements[' + parentsArray[i] + ']';
			}
			toExecute += '.addMember(newField)';
	
			// add member
			try {
				// execute
				eval(toExecute);
			}
			catch(e){ // prepare for an error
				alert('Error in editing select field to a group! ' + e.description);
				return false;
			}
			
			// then delete from old one
			xsdWeaverForm.formElements.splice(currentField,1);
			
			// update group box
			updateGroupList();
		}
	}
	else {	// it was a group
		
		// get array of parents
		var parentsArray = target.split(',');
		
		// construct javascript to execute
		var toExecute = 'xsdWeaverForm';
		for(var i=0; i<parentsArray.length; i++){
			toExecute += '.formElements[' + parentsArray[i] + ']';
		}
		
		if($F('fw_assignedGroup') == -1){
			// remove from group
			try {
				eval( toExecute + '.deleteMember(' + currentField + ')' );
			}
			catch(e){
				alert('Error in editing select field to a group!' + e.description);
				return false;
			}
			
			// add to form
			xsdWeaverForm.formElements[xsdWeaverForm.formElements.length] = newField;
			
			// update group box
			updateGroupList();
		}
		else if($F('fw_assignedGroup') == target){	// same group
			// update in the group
			try {
				eval( toExecute + '.formElements[' + currentField + '] = newField' );
			}
			catch(e){
				alert('Error in editing select field to a group !!' + e.description);
				return false;
			}
		}
		else {	// assigned to different group
			// remove from group
			try {
				eval( toExecute + '.deleteMember(' + currentField + ')' );
			}
			catch(e){
				alert('Error in editing select field to a group!' + e.description);
				return false;
			}
			
			// add to the group
			var newParentsArray = $F('fw_assignedGroup').split(',');
			
			// construct javascript to execute
			var newToExecute = 'xsdWeaverForm';
			for(var i=0; i<newParentsArray.length; i++){
				newToExecute += '.formElements[' + newParentsArray[i] + ']';
			}
			newToExecute += '.addMember(newField)';
	
			try {
				// execute
				eval(newToExecute);
			}
			catch(e){ // prepare for an error
				alert('Error in editing select field to a group! ' + e.description);
				return false;
			}
			
			// update group box
			updateGroupList();
		}
	}
	
	// exit editor
	resetSelectField();
}



// cancels select edit field
function resetSelectField(){
	// reset values
	document.selectionEditForm.fw_selectionName.value = '';
	document.selectionEditForm.fw_selectionTitle.value = '';
	document.selectionEditForm.fw_selectionDesc.value = '';
	document.selectionEditForm.fw_selectionRequired.checked = false;
	document.selectionEditForm.fw_selectionRepeated.checked = false;
	
	// reset options
	resetSelectOptions();
}



// check select options
function checkSelectOptions(){
	// check length
	if(optionsArray.length == 0){
		alert('There must be at least one option');
		return false;
	}
	
	var numOptions = 0;
	// loop through the choices and check if the values are correct
	for(var i=0; i<optionsArray.length; i++){
		// check if exists
		if(typeof eval('document.selectionEditForm.fw_selectFieldOption' + optionsArray[i]) != 'undefined'){
			// increment the choices
			numOptions++;

			var baseName = 'fw_selectFieldOption' + optionsArray[i];
			
			// check label, value and id
			if(isEmpty(eval('document.selectionEditForm.' +baseName + "_Label" + '.value'))){
				alert('Please enter label for option ' + i);
				eval('document.selectionEditForm.' +baseName + "_Label" + '.focus()');
				return false;
			}
			else if(isEmpty(eval('document.selectionEditForm.' +baseName + "_Value" + '.value'))){
				alert('Please enter value for option ' + i);
				eval('document.selectionEditForm.' +baseName + "_Value" + '.focus()');
				return false;
			}
		}
	}
	
	if(numOptions > 0){
		return true;	// checked out
	}
	else{
		alert('There must be at least one option');
		return false;
	}
}



// reset options
function resetSelectOptions(){
	
	// reset global variables
	lastOption = 1;
	optionsArray = new Array('1');
	
	// reset html
	var selectFieldResetHTML = '\
	<table>\
	<tbody id="fw_selectFieldOptions">\
	<tr class="oddRow">\
		<th scope="col">Select</th>\
		<th scope="col">Label<span class="required">*</span></th>\
		<th scope="col">Value<span class="required">*</span></th>\
	</tr>\
	<tr id="fw_selectFieldDefaultOption" class="evenRow">\
		<td><input type="checkbox" name="fw_selectFieldOption1" id="fw_selectFieldOption1" value="1" /></td>\
		<td><input type="text" name="fw_selectFieldOption1_Label" id="fw_selectFieldOption1_Label" /></td>\
		<td><input type="text" name="fw_selectFieldOption1_Value" id="fw_selectFieldOption1_Value" /></td>\
	</tr>\
	</tbody>\
	</table>';
	
	// reset fields
	$('fw_selectFieldOptionsBox').innerHTML = selectFieldResetHTML;
	
}



function updateSelectOption(){
	$("fw_selectFieldOptionsBox").innerHTML = getSelectOptionHTML() + '</tbody></table>';
}



function getSelectOptionHTML(){
	
	var optionHTML = '<table>\
	<tbody id="fw_selectFieldOptions">\
	<tr class="oddRow">\
		<th scope="col">Select</th>\
		<th scope="col">Label<span class="required">*</span></th>\
		<th scope="col">Value<span class="required">*</span></th>\
	</tr>';
	
	// set the value for all existing choices, to prevent the values from being lost
	for(var i=0; i<optionsArray.length; i++){
		// check if exists
		if(typeof eval('document.selectionEditForm.fw_selectFieldOption' + optionsArray[i]) != 'undefined'){
			var baseName = 'fw_selectFieldOption' + optionsArray[i];
			
			var className = 'oddRow';
			if(i%2 == 0){
				className = 'evenRow';
			}
			
			// create dom elements and now append row to the table of choices
			optionHTML += '<tr class="' + className + '">';
			optionHTML += '<td><input type="checkbox" name="' + baseName + '" id="' + baseName + '" value="' + optionsArray[i] + '"';
			// check if checked
			if(eval('document.selectionEditForm.fw_selectFieldOption' + optionsArray[i] + '.checked')){
				optionHTML += ' checked="checked"';
			}
			optionHTML += '/></td>';
			optionHTML += '<td><input type="text" name="' + baseName + '_Label" id="' + baseName + '_Label" value="' + eval('document.selectionEditForm.' +baseName + "_Label" + '.value') + '"/></td>';
			optionHTML += '<td><input type="text" name="' + baseName + '_Value" id="' + baseName + '_Value" value="' + eval('document.selectionEditForm.' +baseName + "_Value" + '.value') + '" /></td>';
			optionHTML += '</tr>';			
		}
	}
	
	return optionHTML;
}



// adds new option for select
function addSelectOption(){
	
	// set the value for all existing choices, to prevent the values from being lost
	var newRow = getSelectOptionHTML();
	
	// get new choice number	
	lastOption++;
	// insert to an array of choices
	optionsArray[optionsArray.length] = lastOption;
	// create name of the select field
	var choiceBaseName = "fw_selectFieldOption" + lastOption;
	
	var className = 'evenRow';
	if(optionsArray.length%2 == 0){
		className = 'oddRow';
	}
	
	// create dom elements and now append row to the table of choices
	newRow += '<tr class="' + className + '">';
	newRow += '<td><input type="checkbox" name="' + choiceBaseName + '" id="' + choiceBaseName + '" value="' + lastOption + '" /></td>';
	newRow += '<td><input type="text" name="' + choiceBaseName + '_Label" id="' + choiceBaseName + '_Label" /></td>';
	newRow += '<td><input type="text" name="' + choiceBaseName + '_Value" id="' + choiceBaseName + '_Value" /></td>';
	newRow += '</tr>';
	newRow += '</tbody>';
	newRow += '</table>';

	$("fw_selectFieldOptionsBox").innerHTML = newRow;
}



// deletes selected option for select
function deleteSelectOption(){
	// check if only one choice 
	if(optionsArray.length == 1) {
		// only default choice
		// can't delete all choices
		alert('Cannot delete all options! You must have at least one option');
		return;
	}
	
	// go through the choices and delete them
	var tbodyDom = document.getElementById("fw_selectFieldOptions");
	var checkedFound = false;
	var fieldToDelete = new Array();
	for(var i=0; i<optionsArray.length; i++){
		// check if exists
		if(typeof eval('document.selectionEditForm.fw_selectFieldOption' + optionsArray[i]) != 'undefined'){
			// check if checked
			if(eval('document.selectionEditForm.fw_selectFieldOption' + optionsArray[i] + '.checked')){
				// set the flag for if deleted any
				checkedFound = true;
				
				// delete the entire row
				var baseName = 'fw_selectFieldOption' + optionsArray[i];
				// alert(typeof $(baseName).parentNode.parentNode + " " + baseName + " : " + i + " : " + optionsArray[i]);
				tbodyDom.removeChild($(baseName).parentNode.parentNode);
				fieldToDelete[fieldToDelete.length] = i;
			}
		}
		
		if(optionsArray.length == 1){
			alert('Cannot delete all options! You must have at least one option');
			break;
		}
	}
	
	// now delete them
	for(var m=0; m<fieldToDelete.length; m++){
		// delete from the array
		optionsArray.splice(fieldToDelete[m],1);
	}
	
	// update table
	updateSelectOption();
	
	if(!checkedFound){
		alert('Please select an option to delete!');
	}
}



// moves selected select option up
function moveSelectOptionUp(){
	// check if only one choice
	if(optionsArray.length == 1) {
		// only default choice
		// can't move up
		return;
	}
	
	// get which one is checked
	var checkedIndex = -1;
	var checkedFound = false;
	for(var i=0; i<optionsArray.length; i++){
		// check if exists
		if(typeof eval('document.selectionEditForm.fw_selectFieldOption' + optionsArray[i]) != 'undefined'){
			// check if checked
			if(eval('document.selectionEditForm.fw_selectFieldOption' + optionsArray[i] + '.checked')){
				// check if there is already one found
				if(!checkedFound){
					// set the flag for if deleted any
					checkedFound = true;
					checkedIndex = i;
				}
				else{
					// more than one is checked!
					alert('Please select only one option to move up');
					return;
				}
			}
		}
	}
	
	// now see if any choices is to be moved up
	if(!checkedFound){
		alert('Please select an option to move up!');
		return;
	}
	else if(checkedIndex == 0){
		// the first item.. can't move up anymore
		return;
	}
	else {
		// get id of respective selected checkboxes
		var bottomCheckbox = 'fw_selectFieldOption' + optionsArray[checkedIndex];
		var topCheckbox = 'fw_selectFieldOption' + optionsArray[(checkedIndex-1)];
		
		// move choice up in the array
		var tempValue = optionsArray[checkedIndex];
		optionsArray[checkedIndex] = optionsArray[checkedIndex-1];
		optionsArray[checkedIndex-1] = tempValue;
		
		// move the row up in the table
		$("fw_selectFieldOptions").insertBefore($("fw_selectFieldOptions").removeChild($(bottomCheckbox).parentNode.parentNode),$(topCheckbox).parentNode.parentNode);
		
		// exchange classname;
		var tmpClassname = document.getElementById(bottomCheckbox).parentNode.parentNode.className;
		document.getElementById(bottomCheckbox).parentNode.parentNode.className = document.getElementById(topCheckbox).parentNode.parentNode.className;
		document.getElementById(topCheckbox).parentNode.parentNode.className = tmpClassname;
		
	}
}



// moves selected select option down
function moveSelectOptionDown(){
	// check if only one choice
	if(optionsArray.length == 1) {
		// only default choice
		// can't move up
		return;
	}
	
	// get which one is checked
	var checkedIndex = -1;
	var checkedFound = false;
	for(var i=0; i<optionsArray.length; i++){
		// check if exists
		if(typeof eval('document.selectionEditForm.fw_selectFieldOption' + optionsArray[i]) != 'undefined'){
			// check if checked
			if(eval('document.selectionEditForm.fw_selectFieldOption' + optionsArray[i] + '.checked')){
				// check if there is already one found
				if(!checkedFound){
					// set the flag for if deleted any
					checkedFound = true;
					checkedIndex = i;
				}
				else{
					// more than one is checked!
					alert('Please select only one option to move up');
					return;
				}
			}
		}
	}
	
	// now see if any choices is to be moved up
	if(!checkedFound){
		alert('Please select an option to move up!');
		return;
	}
	else if(checkedIndex == (optionsArray.length-1)){
		// the last item.. can't move down anymore
		return;
	}
	else {
		// get id of respective selected checkboxes
		var bottomCheckbox = 'fw_selectFieldOption' + optionsArray[(checkedIndex+1)];
		var topCheckbox = 'fw_selectFieldOption' + optionsArray[checkedIndex];
		
		// move choice up in the array
		var tempValue = optionsArray[checkedIndex];
		optionsArray[checkedIndex] = optionsArray[checkedIndex+1];
		optionsArray[checkedIndex+1] = tempValue;
		
		// move the row up in the table
		$("fw_selectFieldOptions").insertBefore($("fw_selectFieldOptions").removeChild($(bottomCheckbox).parentNode.parentNode),$(topCheckbox).parentNode.parentNode);
		
		// exchange classname;
		var tmpClassname = document.getElementById(bottomCheckbox).parentNode.parentNode.className;
		document.getElementById(bottomCheckbox).parentNode.parentNode.className = document.getElementById(topCheckbox).parentNode.parentNode.className;
		document.getElementById(topCheckbox).parentNode.parentNode.className = tmpClassname;
	}
}



/**
 * @function: checkGroupField
 * @desc: checks to see if group form fields are correctly filled out
 */
function checkGroupField(){
	if(isEmpty($F('fw_groupName'))){
		alert('Please enter group name');
		$('fw_groupName').focus();
		return false;
	}
	else if(isEmpty($F('fw_groupTitle'))){
		alert('Please enter group label');
		$('fw_groupTitle').focus();
		return false;
	}
	else if(isEmpty($F('fw_groupDesc'))){
		alert('Please enter group description');
		$('fw_groupDesc').focus();
		return false;
	}
	else {
		// further check needed...
		if(editType == 'edit'){
			// need to check couple of things, like self assigning
			if(target == null){
				if( $F('fw_assignedGroup') == currentField ){
					alert('Cannot add group to itself!');
					return false;
				}
				else if( $F('fw_assignedGroup').indexOf(currentField) == 0 ){
					alert('Cannot add group to its subgroup!');
					return false;
				}
				else{
					return true;
				}
			}
			else{
				var currentFieldIndex = target + ',' + currentField;
				// check if adding to itself
				if( currentFieldIndex == $F('fw_assignedGroup') ){
					alert('Cannot add group to itself!');
					return false;
				}
				else if( $F('fw_assignedGroup').indexOf(currentFieldIndex) == 0 ){
					alert('Cannot add group to its subgroup!');
					return false;
				}
				else {
					return true;
				}
			}
		}
		else{
			return true;
		}
	}
}



/**
 * @function: resetGroupField
 * @desc: resets group edit field
 */
function resetGroupField(){
	document.groupEditForm.fw_groupName.value = '';
	document.groupEditForm.fw_groupTitle.value = '';
	document.groupEditForm.fw_groupDesc.value = '';
	document.groupEditForm.fw_groupRequired.checked = false;
	document.groupEditForm.fw_groupRepeated.checked = false;
}



/**
 * @function: addGroupField
 * @desc: adds new group field to form, assumes that all the forms are checked prior to calling this function
 * @dependency: uses global variables
 */
function addGroupField(){
	// initialize text field
	var newField = new fw_group($F('fw_groupName'), $F('fw_groupTitle'), $F('fw_groupDesc'));
	
	// set optional parameters
	if($F('fw_groupRequired') == 'true'){
		newField.required = true;
	}
	else{
		newField.required = false;
	}
	
	if($F('fw_groupRepeated') == 'true'){
		newField.repeated = true;
	}
	else{
		newField.repeated = false;
	}
	
	if($F('fw_assignedGroup') == -1){
		
		// add to the form
		xsdWeaverForm.formElements[xsdWeaverForm.formElements.length] = newField;
		
	}
	else{
		// add to the group
		// get array of parents
		var parentsArray = $F('fw_assignedGroup').split(',');
		
		// construct javascript to execute
		var toExecute = 'xsdWeaverForm';
		for(var i=0; i<parentsArray.length; i++){
			toExecute += '.formElements[' + parentsArray[i] + ']';
		}
		toExecute += '.addMember(newField)';

		try {
			// execute
			eval(toExecute);
		}
		catch(e){ // prepare for an error
			alert('Error in adding group field to a group! ' + e.description);
		}
	}
	
	// update group list
	updateGroupList();
}



/**
 * @function: editGroupField
 * @desc: edit new group field to form, assumes that all the forms are checked prior to calling this function
 * @dependency: uses global variables
 */
function editGroupField(){
	
	// initialize text field
	var newField = new fw_group($F('fw_groupName'), $F('fw_groupTitle'), $F('fw_groupDesc'));
	
	// set optional parameters
	if($F('fw_groupRequired') == 'true'){
		newField.required = true;
	}
	else{
		newField.required = false;
	}
	
	if($F('fw_groupRepeated') == 'true'){
		newField.repeated = true;
	}
	else{
		newField.repeated = false;
	}

	// now reassign to proper place
	if(target == null){ // no sub group
		
		// copy all children
		newField.formElements = xsdWeaverForm.formElements[currentField].formElements;
			
		if($F('fw_assignedGroup') == -1){
			// reassign to same field
			xsdWeaverForm.formElements[currentField] = newField;
		}
		else {
			// add to the group
			
			// get array of parents
			var parentsArray = $F('fw_assignedGroup').split(',');
			
			// construct javascript to execute
			var toExecute = 'xsdWeaverForm';
			for(var i=0; i<parentsArray.length; i++){
				toExecute += '.formElements[' + parentsArray[i] + ']';
			}
			toExecute += '.addMember(newField)';
	
			// add member
			try {
				// execute
				eval(toExecute);
			}
			catch(e){ // prepare for an error
				alert('Error in editing group field to a group! ' + e.description);
				return false;
			}
			
			// then delete from old one
			xsdWeaverForm.formElements.splice(currentField,1);

			// update group box
			// updateGroupList();
		}
	}
	else {	// it was a group
		
		// get array of parents
		var parentsArray = target.split(',');
		
		// construct javascript to execute
		var toExecute = 'xsdWeaverForm';
		for(var i=0; i<parentsArray.length; i++){
			toExecute += '.formElements[' + parentsArray[i] + ']';
		}
		
		// copy all children
		try {
			eval('newField.formElements=' + toExecute + '.formElements[' + currentField + '].formElements');
		}
		catch(e){
			alert('Error in editing group field to a group!' + e.description);
			return false;
		}
		
		
		if($F('fw_assignedGroup') == -1){
			// remove from group
			try {
				eval( toExecute + '.deleteMember(' + currentField + ')' );
			}
			catch(e){
				alert('Error in editing group field to a group!' + e.description);
				return false;
			}
			
			// add to form
			xsdWeaverForm.formElements[xsdWeaverForm.formElements.length] = newField;
		}
		else if($F('fw_assignedGroup') == target){	// same group
			// update in the group
			try {
				eval( toExecute + '.formElements[' + currentField + '] = newField' );
			}
			catch(e){
				alert('Error in editing group field to a group !!' + e.description);
				return false;
			}
		}
		else {	// assigned to different group
				
			// remove from group
			try {
				eval( toExecute + '.deleteMember(' + currentField + ')' );
			}
			catch(e){
				alert('Error in editing group field to a group!' + e.description);
				return false;
			}
			
			
			// add to the group
			var newParentsArray = $F('fw_assignedGroup').split(',');
			
			// construct javascript to execute
			var newToExecute = 'xsdWeaverForm';
			for(var i=0; i<newParentsArray.length; i++){
				newToExecute += '.formElements[' + newParentsArray[i] + ']';
			}
			newToExecute += '.addMember(newField)';
	
			try {
				// execute
				eval(newToExecute);
			}
			catch(e){ // prepare for an error
				alert('Error in editing group field to a group! ' + e.description);
				return false;
			}
			
		}
	}
	
	// update group
	// updateGroupArray();
	updateGroupList();

}



/** !!!!! not used anymore
 * @function: updateGroupArray
 * @desc: Updates group array when fields get deleted
 * @dependency: uses global variables
 */
function updateGroupArray(oOptions){
	
	var oOptions = augment({level: 1, formParent: null}, oOptions );
	
	if(oOptions.level == 1){
		// level 1 does not have form parent
		for(var i=0; i<xsdWeaverForm.formElements.length; i++){
			if(xsdWeaverForm.formElements[i].type == 'group'){
				
			}
		}
	}
	else{
		
	}
	/*
	// reset group array
	groupList = new Array();
	
	for(var i=0; i<xsdWeaverForm.formElements.length; i++){
		if(xsdWeaverForm.formElements[i].type == 'group'){
			groupList[groupList.length] = new Array(i, xsdWeaverForm.formElements[i].name);
		}
	}
	*/
}



/**
 * @function: updateGroupList
 * @desc: Updates grouplist on edit forms
 * @dependency: uses global variables
 */
function updateGroupList(oOptions){
	
	// options
	var oOptions = augment({formParent: null, parentIndexes: new Array()}, oOptions );
	
	if(oOptions.parentIndexes.length == 0){
		var groupListHtml = '\
		Assign to Group:\
		<form name="groupSelectForm" id="groupSelectForm" method="post" action="#" onsubmit="return false;">\
		<select name="fw_assignedGroup" id="fw_assignedGroup">\
			<option value="-1">------ No group ------</option>';
		
		// level 1 does not have form parent
		for(var i=0; i<xsdWeaverForm.formElements.length; i++){
			if(xsdWeaverForm.formElements[i].type == 'group'){
				groupListHtml += '<option value="' + i + '">&nbsp;' + htmlEntities(xsdWeaverForm.formElements[i].name) + '</option>';	
				var parentIndex = new Array();
				parentIndex[0] = i;
				groupListHtml += updateGroupList( {formParent: xsdWeaverForm.formElements[i], parentIndexes: parentIndex} );
			}
		}
		
		groupListHtml += '</select></form>';
		
		// update html
		$('groupListBox').innerHTML = groupListHtml;
	}
	else{
		var groupListHtml = '';
		for(var i=0; i<oOptions.formParent.formElements.length; i++){
			if(oOptions.formParent.formElements[i].type == 'group'){
				groupListHtml += '<option value="' + oOptions.parentIndexes.join(',') + ',' + i + '">';
				for(var j=0; j<oOptions.parentIndexes.length; j++){
					groupListHtml += '&nbsp;-';
				}
				groupListHtml += '&nbsp;';
				groupListHtml += htmlEntities(oOptions.formParent.formElements[i].name) + '</option>';
				
				//oOptions.parentIndexes[oOptions.parentIndexes.length] = i;
				var newCurrentIndex = new Array();
				newCurrentIndex[0] = i;
				var newParentIndexes = oOptions.parentIndexes.concat(newCurrentIndex);
				groupListHtml += updateGroupList( {formParent: oOptions.formParent.formElements[i], parentIndexes: newParentIndexes} );
			}
		}
		return groupListHtml;
	}
	
	/*
	var groupListHtml = '\
	Assign to Group:\
	<form name="groupSelectForm" id="groupSelectForm" method="post" action="#" onsubmit="return false;">\
	<select name="fw_assignedGroup" id="fw_assignedGroup">\
		<option value="-1">------ No group ------</option>';
		
	// add existing groups
	for(var i=0; i<groupList.length; i++){
		var existingGroup = groupList[i];
		groupListHtml += '<option value="' + existingGroup[0] + '">' + htmlEntities(existingGroup[1]) + '</option>';
	}

	groupListHtml += '</select></form>';
	
	// update html
	$('groupListBox').innerHTML = groupListHtml;
	*/
}



/**
 * @function: checkFileField
 * @desc: checks to see if check form fields are correctly filled out
 */
function checkFileField(){
	if(isEmpty($F('fw_fileName'))){
		alert('Please enter field name');
		$('fw_fileName').focus();
		return false;
	}
	else if(isEmpty($F('fw_fileTitle'))){
		alert('Please enter field label');
		$('fw_fileTitle').focus();
		return false;
	}
	else if(isEmpty($F('fw_fileDesc'))){
		alert('Please enter field description');
		$('fw_fileDesc').focus();
		return false;
	}
	else {
		return true;
	}
}



/**
 * @function: resetFileField
 * @desc: resets file edit field
 */
function resetFileField(){
	document.fileEditForm.fw_fileName.value = '';
	document.fileEditForm.fw_fileTitle.value = '';
	document.fileEditForm.fw_fileDesc.value = '';
	document.fileEditForm.fw_fileRequired.checked = false;
	document.fileEditForm.fw_fileRepeated.checked = false;
}



/**
 * @function: addFileField
 * @desc: adds new file field to form, assumes that all the forms are checked prior to calling this function
 * @dependency: uses global variables
 */
function addFileField(){
	// initialize text field
	var newField = new fw_fileField($F('fw_fileName'), $F('fw_fileTitle'), $F('fw_fileDesc'));
	
	// set optional parameters
	if($F('fw_fileRequired') == 'true'){
		newField.required = true;
	}
	else{
		newField.required = false;
	}
	
	if($F('fw_fileRepeated') == 'true'){
		newField.repeated = true;
	}
	else{
		newField.repeated = false;
	}
	
	if($F('fw_assignedGroup') == -1){
		// add to the form
		xsdWeaverForm.formElements[xsdWeaverForm.formElements.length] = newField;
	}
	else{
		// add to the group
		// get array of parents
		var parentsArray = $F('fw_assignedGroup').split(',');
		
		// construct javascript to execute
		var toExecute = 'xsdWeaverForm';
		for(var i=0; i<parentsArray.length; i++){
			toExecute += '.formElements[' + parentsArray[i] + ']';
		}
		toExecute += '.addMember(newField)';

		try {
			// execute
			eval(toExecute);
		}
		catch(e){ // prepare for an error
			alert('Error in adding file field to a group! ' + e.description);
		}
	}
}



/**
 * @function: editFileField
 * @desc: edit file field to form, assumes that all the forms are checked prior to calling this function
 * @dependency: uses global variables
 */
function editFileField(){
	// initialize text field
	var newField = new fw_fileField($F('fw_fileName'), $F('fw_fileTitle'), $F('fw_fileDesc'));
	
	// set optional parameters
	if($F('fw_fileRequired') == 'true'){
		newField.required = true;
	}
	else{
		newField.required = false;
	}
	
	if($F('fw_fileRepeated') == 'true'){
		newField.repeated = true;
	}
	else{
		newField.repeated = false;
	}
	
	// now reassign to proper place
	if(target == null){
		if($F('fw_assignedGroup') == -1){
			// reassign to same field
			xsdWeaverForm.formElements[currentField] = newField;
		}
		else {
			// add to the group
			// get array of parents
			var parentsArray = $F('fw_assignedGroup').split(',');
			
			// construct javascript to execute
			var toExecute = 'xsdWeaverForm';
			for(var i=0; i<parentsArray.length; i++){
				toExecute += '.formElements[' + parentsArray[i] + ']';
			}
			toExecute += '.addMember(newField)';
	
			// add member
			try {
				// execute
				eval(toExecute);
			}
			catch(e){ // prepare for an error
				alert('Error in editing file field to a group! ' + e.description);
				return false;
			}
			
			// then delete from old one
			xsdWeaverForm.formElements.splice(currentField,1);
			
			// update group box
			updateGroupList();
		}
	}
	else {	// it was a group
		
		// get array of parents
		var parentsArray = target.split(',');
		
		// construct javascript to execute
		var toExecute = 'xsdWeaverForm';
		for(var i=0; i<parentsArray.length; i++){
			toExecute += '.formElements[' + parentsArray[i] + ']';
		}
		
		if($F('fw_assignedGroup') == -1){
			// remove from group
			try {
				eval( toExecute + '.deleteMember(' + currentField + ')' );
			}
			catch(e){
				alert('Error in editing file field to a group!' + e.description);
				return false;
			}
			
			// add to form
			xsdWeaverForm.formElements[xsdWeaverForm.formElements.length] = newField;
			
			// update group box
			updateGroupList();
		}
		else if($F('fw_assignedGroup') == target){	// same group
			// update in the group
			try {
				eval( toExecute + '.formElements[' + currentField + '] = newField' );
			}
			catch(e){
				alert('Error in editing file field to a group !!' + e.description);
				return false;
			}
		}
		else {	// assigned to different group
			// remove from group
			try {
				eval( toExecute + '.deleteMember(' + currentField + ')' );
			}
			catch(e){
				alert('Error in editing file field to a group!' + e.description);
				return false;
			}
			
			// add to the group
			var newParentsArray = $F('fw_assignedGroup').split(',');
			
			// construct javascript to execute
			var newToExecute = 'xsdWeaverForm';
			for(var i=0; i<newParentsArray.length; i++){
				newToExecute += '.formElements[' + newParentsArray[i] + ']';
			}
			newToExecute += '.addMember(newField)';
	
			try {
				// execute
				eval(newToExecute);
			}
			catch(e){ // prepare for an error
				alert('Error in editing file field to a group! ' + e.description);
				return false;
			}
			
			// update group box
			updateGroupList();
		}
	}
}



/**
 * @function: checkBooleanField
 * @desc: checks to see if check form fields are correctly filled out
 */
function checkBooleanField(){
	if(isEmpty($F('fw_booleanName'))){
		alert('Please enter field name');
		$('fw_booleanName').focus();
		return false;
	}
	else if(isEmpty($F('fw_booleanTitle'))){
		alert('Please enter field label');
		$('fw_booleanTitle').focus();
		return false;
	}
	else if(isEmpty($F('fw_booleanDesc'))){
		alert('Please enter field description');
		$('fw_booleanDesc').focus();
		return false;
	}
	else {
		return true;
	}
}



/**
 * @function: resetBooleanField
 * @desc: resets file edit field
 */
function resetBooleanField(){
	document.booleanEditForm.fw_booleanName.value = '';
	document.booleanEditForm.fw_booleanTitle.value = '';
	document.booleanEditForm.fw_booleanDesc.value = '';
	document.booleanEditForm.fw_booleanRequired.checked = false;
	document.booleanEditForm.fw_booleanRepeated.checked = false;
}



/**
 * @function: addBooleanField
 * @desc: adds new boolean field to form, assumes that all the forms are checked prior to calling this function
 * @dependency: uses global variables
 */
function addBooleanField(){
	// initialize text field
	var newField = new fw_booleanField($F('fw_booleanName'), $F('fw_booleanTitle'), $F('fw_booleanDesc'));
	
	// set optional parameters
	if($F('fw_booleanRequired') == 'true'){
		newField.required = true;
	}
	else{
		newField.required = false;
	}
	
	if($F('fw_booleanRepeated') == 'true'){
		newField.repeated = true;
	}
	else{
		newField.repeated = false;
	}
	
	if($F('fw_assignedGroup') == -1){
		// add to the form
		xsdWeaverForm.formElements[xsdWeaverForm.formElements.length] = newField;
	}
	else{
		// add to the group
		// get array of parents
		var parentsArray = $F('fw_assignedGroup').split(',');
		
		// construct javascript to execute
		var toExecute = 'xsdWeaverForm';
		for(var i=0; i<parentsArray.length; i++){
			toExecute += '.formElements[' + parentsArray[i] + ']';
		}
		toExecute += '.addMember(newField)';

		try {
			// execute
			eval(toExecute);
		}
		catch(e){ // prepare for an error
			alert('Error in adding boolean field to a group! ' + e.description);
		}
	}
}



/**
 * @function: editBooleanField
 * @desc: edit boolean field to form, assumes that all the forms are checked prior to calling this function
 * @dependency: uses global variables
 */
function editBooleanField(){
	// initialize text field
	var newField = new fw_booleanField($F('fw_booleanName'), $F('fw_booleanTitle'), $F('fw_booleanDesc'));
	
	// set optional parameters
	if($F('fw_booleanRequired') == 'true'){
		newField.required = true;
	}
	else{
		newField.required = false;
	}
	
	if($F('fw_booleanRepeated') == 'true'){
		newField.repeated = true;
	}
	else{
		newField.repeated = false;
	}
	
	// now reassign to proper place
	if(target == null){
		if($F('fw_assignedGroup') == -1){
			// reassign to same field
			xsdWeaverForm.formElements[currentField] = newField;
		}
		else {
			// add to the group
			// get array of parents
			var parentsArray = $F('fw_assignedGroup').split(',');
			
			// construct javascript to execute
			var toExecute = 'xsdWeaverForm';
			for(var i=0; i<parentsArray.length; i++){
				toExecute += '.formElements[' + parentsArray[i] + ']';
			}
			toExecute += '.addMember(newField)';
	
			// add member
			try {
				// execute
				eval(toExecute);
			}
			catch(e){ // prepare for an error
				alert('Error in editing boolean field to a group! ' + e.description);
				return false;
			}
			
			// then delete from old one
			xsdWeaverForm.formElements.splice(currentField,1);
			
			// update group box
			updateGroupList();
		}
	}
	else {	// it was a group
		
		// get array of parents
		var parentsArray = target.split(',');
		
		// construct javascript to execute
		var toExecute = 'xsdWeaverForm';
		for(var i=0; i<parentsArray.length; i++){
			toExecute += '.formElements[' + parentsArray[i] + ']';
		}
		
		if($F('fw_assignedGroup') == -1){
			// remove from group
			try {
				eval( toExecute + '.deleteMember(' + currentField + ')' );
			}
			catch(e){
				alert('Error in editing boolean field to a group!' + e.description);
				return false;
			}
			
			// add to form
			xsdWeaverForm.formElements[xsdWeaverForm.formElements.length] = newField;
			
			// update group box
			updateGroupList();
		}
		else if($F('fw_assignedGroup') == target){	// same group
			// update in the group
			try {
				eval( toExecute + '.formElements[' + currentField + '] = newField' );
			}
			catch(e){
				alert('Error in editing boolean field to a group !!' + e.description);
				return false;
			}
		}
		else {	// assigned to different group
			// remove from group
			try {
				eval( toExecute + '.deleteMember(' + currentField + ')' );
			}
			catch(e){
				alert('Error in editing boolean field to a group!' + e.description);
				return false;
			}
			
			// add to the group
			var newParentsArray = $F('fw_assignedGroup').split(',');
			
			// construct javascript to execute
			var newToExecute = 'xsdWeaverForm';
			for(var i=0; i<newParentsArray.length; i++){
				newToExecute += '.formElements[' + newParentsArray[i] + ']';
			}
			newToExecute += '.addMember(newField)';
	
			try {
				// execute
				eval(newToExecute);
			}
			catch(e){ // prepare for an error
				alert('Error in editing boolean field to a group! ' + e.description);
				return false;
			}
			
			// update group box
			updateGroupList();
		}
	}
}



/**
 * @function: editField
 * @desc: sets up the edit form for given field
 */
function editField(fieldIndex, oOptions) {
	
	var oOptions = augment({
		groupIndex: null
	},oOptions);
	
	var fieldToEdit = null;
	
	/*
	// check groupIndex
	if(oOptions.groupIndex != null && ((oOptions.groupIndex < 0 || oOptions.groupIndex > xsdWeaverForm.formElements.length) || (xsdWeaverForm.formElements[oOptions.groupIndex].type != 'group'))){
		// invalid group index
		return false;
	}
	*/
	
	// get form element to edit
	if(oOptions.groupIndex == null){
		// check fieldIndex first
		if(fieldIndex < 0 || fieldIndex > xsdWeaverForm.formElements.length){
			// invalid index
			return false;
		}
		else {
			currentField = fieldIndex;
			target = null;
			fieldToEdit = xsdWeaverForm.formElements[fieldIndex];
			// no group, select no group
			document.groupSelectForm.fw_assignedGroup.options[0].selected = true;
		}
	}
	else{
		// get group parent indexes in array form
		var groupIndexes = oOptions.groupIndex.split(',');
		var javascriptExec = 'xsdWeaverForm';
		for(var i=0; i<groupIndexes.length; i++){
			javascriptExec += '.formElements[' + groupIndexes[i] + ']';
		}
		
		// first check if given index actually points to the group
		try {
			var elementType = eval(javascriptExec + '.type');
		}
		catch(e){
			alert('Error in editing ' + e.description);
			return false;
		}
		
		if(elementType != 'group'){
			return false;
		}
		
		// check fieldIndex first
		try {
			var elementLength = eval(javascriptExec + '.formElements.length');
		}
		catch(e){
			alert('Error in editing ' + e.description);
			return false;
		}
		
		if(fieldIndex < 0 || fieldIndex > elementLength){
			// invalid index
			return false;
		}
		else {
			currentField = fieldIndex;
			target = oOptions.groupIndex;
			
			try {
				fieldToEdit = eval(javascriptExec + '.formElements[' + fieldIndex + ']');
			}
			catch(e){
				alert('Error in editing ' + e.description);
				return false;
			}
			
			// now select proper group
			for(var i=0; i<document.groupSelectForm.fw_assignedGroup.options.length; i++){
				if(document.groupSelectForm.fw_assignedGroup.options[i].value == target){
					document.groupSelectForm.fw_assignedGroup.options[i].selected = true;
					break;
				}
			}
		}
	}
	
	editType = 'edit';
	fieldType = fieldToEdit.type;
	dateUpdateTargetForm = null;
	dateUpdateTarget = null;
	
	// now set up form
	switch(fieldType){
		case 'group':
			hidethis('datePickerBox');
			resizebgOverlay('backDrop');
			showthis('backDrop');
			resizeeditArea('edit');
			hidethis('previewView');	/* cuz IE shows select form element */
			hidethis('textFieldEdit');
			hidethis('textareaFieldEdit');
			hidethis('wysiwygFieldEdit');
			hidethis('dateFieldEdit');
			hidethis('selectionFieldEdit');
			hidethis('fileFieldEdit');
			hidethis('booleanFieldEdit');
			showthis('groupListBox');
			hidethis('openXSDWizard');
			
			// initialize values
			document.groupEditForm.fw_groupName.value = fieldToEdit.name;
			document.groupEditForm.fw_groupTitle.value = fieldToEdit.label;
			document.groupEditForm.fw_groupDesc.value = fieldToEdit.description;
			document.groupEditForm.fw_groupRequired.checked = fieldToEdit.required;
			document.groupEditForm.fw_groupRepeated.checked = fieldToEdit.repeated;
			
			showthis('groupFieldEdit');
			$('editTitle').innerHTML = "Edit group field";
			showthis('edit');
			break;
		case 'wysiwyg':
			hidethis('datePickerBox');
			resizebgOverlay('backDrop');
			showthis('backDrop');
			resizeeditArea('edit');
			hidethis('previewView');	/* cuz IE shows select form element */
			hidethis('textFieldEdit');
			hidethis('textareaFieldEdit');
			hidethis('dateFieldEdit');
			hidethis('selectionFieldEdit');
			hidethis('groupFieldEdit');
			hidethis('fileFieldEdit');
			hidethis('booleanFieldEdit');
			showthis('groupListBox');
			hidethis('openXSDWizard');
			
			// initialize values
			document.wysiwygEditForm.fw_wysiwygName.value = fieldToEdit.name;
			document.wysiwygEditForm.fw_wysiwygTitle.value = fieldToEdit.label;
			document.wysiwygEditForm.fw_wysiwygDesc.value = fieldToEdit.description;
			document.wysiwygEditForm.fw_wysiwygRequired.checked = fieldToEdit.required;
			document.wysiwygEditForm.fw_wysiwygRepeated.checked = fieldToEdit.repeated;
			
			showthis('wysiwygFieldEdit');
			$('editTitle').innerHTML = "Add wysiwyg field";
			showthis('edit');
			break;
		case 'file':
			hidethis('datePickerBox');
			resizebgOverlay('backDrop');
			showthis('backDrop');
			resizeeditArea('edit');
			hidethis('previewView');	/* cuz IE shows select form element */
			hidethis('textFieldEdit');
			hidethis('textareaFieldEdit');
			hidethis('wysiwygFieldEdit');
			hidethis('dateFieldEdit');
			hidethis('selectionFieldEdit');
			hidethis('groupFieldEdit');
			hidethis('booleanFieldEdit');
			showthis('groupListBox');
			hidethis('openXSDWizard');
			
			// initialize values
			document.fileEditForm.fw_fileName.value = fieldToEdit.name;
			document.fileEditForm.fw_fileTitle.value = fieldToEdit.label;
			document.fileEditForm.fw_fileDesc.value = fieldToEdit.description;
			document.fileEditForm.fw_fileRequired.checked = fieldToEdit.required;
			document.fileEditForm.fw_fileRepeated.checked = fieldToEdit.repeated;
			
			showthis('fileFieldEdit');
			$('editTitle').innerHTML = "Add file field";
			showthis('edit');
			break;
		case 'text':
			hidethis('datePickerBox');
			resizebgOverlay('backDrop');
			showthis('backDrop');
			resizeeditArea('edit');
			hidethis('previewView');	/* cuz IE shows select form element */
			hidethis('textareaFieldEdit');
			hidethis('wysiwygFieldEdit');
			hidethis('dateFieldEdit');
			hidethis('selectionFieldEdit');
			hidethis('groupFieldEdit');
			hidethis('fileFieldEdit');
			hidethis('booleanFieldEdit');
			showthis('groupListBox');
			hidethis('openXSDWizard');
			
			// initialize balues
			document.textEditForm.fw_textName.value = fieldToEdit.name;
			document.textEditForm.fw_textTitle.value = fieldToEdit.label;
			document.textEditForm.fw_textDesc.value = fieldToEdit.description;
			document.textEditForm.fw_textRequired.checked = fieldToEdit.required;
			document.textEditForm.fw_textRepeated.checked = fieldToEdit.repeated;
			
			if(fieldToEdit.maxLength != -1){
				document.textEditForm.fw_textMax.value = fieldToEdit.maxLength;
			}
			
			if(fieldToEdit.email){
				document.textEditForm.fw_textPattern[0].selected = true;
			}
			else if(fieldToEdit.phone){
				document.textEditForm.fw_textPattern[1].selected = true;
			}
			else if(fieldToEdit.number){
				document.textEditForm.fw_textPattern[2].selected = true;
				showthis('textEditNumberOptions');
				if(fieldToEdit.minNumber != false){
					document.textEditForm.fw_textNumberMin.value = fieldToEdit.minNumber;
				}
				if(fieldToEdit.maxNumber != false){
					document.textEditForm.fw_textNumberMax.value = fieldToEdit.maxNumber;
				}
			}
			else {
				document.textEditForm.fw_textPattern[3].selected = true;
			}
			
			showthis('textFieldEdit');
			$('editTitle').innerHTML = "Add text field";
			showthis('edit');
			break;
		case 'date':
			hidethis('datePickerBox');
			resizebgOverlay('backDrop');
			showthis('backDrop');
			resizeeditArea('edit');
			hidethis('previewView');	/* cuz IE shows select form element */
			hidethis('textFieldEdit');
			hidethis('textareaFieldEdit');
			hidethis('wysiwygFieldEdit');
			hidethis('selectionFieldEdit');
			hidethis('groupFieldEdit');
			hidethis('fileFieldEdit');
			hidethis('booleanFieldEdit');
			showthis('groupListBox');
			hidethis('openXSDWizard');
			
			// initialize values
			document.dateEditForm.fw_dateName.value = fieldToEdit.name;
			document.dateEditForm.fw_dateTitle.value = fieldToEdit.label;
			document.dateEditForm.fw_dateDesc.value = fieldToEdit.description;
			document.dateEditForm.fw_dateRequired.checked = fieldToEdit.required;
			document.dateEditForm.fw_dateRepeated.checked = fieldToEdit.repeated;
			
			if(fieldToEdit.minDate != false){
				document.dateEditForm.fw_dateMin.value = fieldToEdit.minDate;
			}
			
			if(fieldToEdit.maxDate != false){
				document.dateEditForm.fw_dateMax.value = fieldToEdit.maxDate;
			}
			
			showthis('dateFieldEdit');
			$('editTitle').innerHTML = "Add date field";
			showthis('edit');
			break;
		case 'boolean':
			hidethis('datePickerBox');
			resizebgOverlay('backDrop');
			showthis('backDrop');
			resizeeditArea('edit');
			hidethis('previewView');	/* cuz IE shows select form element */
			hidethis('textFieldEdit');
			hidethis('textareaFieldEdit');
			hidethis('wysiwygFieldEdit');
			hidethis('dateFieldEdit');
			hidethis('selectionFieldEdit');
			hidethis('groupFieldEdit');
			hidethis('fileFieldEdit');
			showthis('groupListBox');
			hidethis('openXSDWizard');
			
			// initialize values
			document.booleanEditForm.fw_booleanName.value = fieldToEdit.name;
			document.booleanEditForm.fw_booleanTitle.value = fieldToEdit.label;
			document.booleanEditForm.fw_booleanDesc.value = fieldToEdit.description;
			document.booleanEditForm.fw_booleanRequired.checked = fieldToEdit.required;
			document.booleanEditForm.fw_booleanRepeated.checked = fieldToEdit.repeated;
			
			showthis('booleanFieldEdit');
			$('editTitle').innerHTML = "Add boolean field";
			showthis('edit');
			break;
		case 'textarea':
			hidethis('datePickerBox');
			resizebgOverlay('backDrop');
			showthis('backDrop');
			resizeeditArea('edit');
			hidethis('previewView');	/* cuz IE shows select form element */
			hidethis('textFieldEdit');
			hidethis('wysiwygFieldEdit');
			hidethis('dateFieldEdit');
			hidethis('selectionFieldEdit');
			hidethis('groupFieldEdit');
			hidethis('fileFieldEdit');
			hidethis('booleanFieldEdit');
			showthis('groupListBox');
			hidethis('openXSDWizard');
			
			// initialize values
			document.textareaEditForm.fw_textareaName.value = fieldToEdit.name;
			document.textareaEditForm.fw_textareaTitle.value = fieldToEdit.label;
			document.textareaEditForm.fw_textareaDesc.value = fieldToEdit.description;
			document.textareaEditForm.fw_textareaRequired.checked = fieldToEdit.required;
			document.textareaEditForm.fw_textareaRepeated.checked = fieldToEdit.repeated;
			
			showthis('textareaFieldEdit');
			$('editTitle').innerHTML = "Add textarea field";
			showthis('edit');
			break;
		case 'select':
			hidethis('datePickerBox');
			resizebgOverlay('backDrop');
			showthis('backDrop');
			resizeeditArea('edit');
			hidethis('previewView');	/* cuz IE shows select form element */
			hidethis('textFieldEdit');
			hidethis('textareaFieldEdit');
			hidethis('wysiwygFieldEdit');
			hidethis('dateFieldEdit');
			hidethis('groupFieldEdit');
			hidethis('fileFieldEdit');
			hidethis('booleanFieldEdit');
			showthis('groupListBox');
			hidethis('openXSDWizard');
			
			// initialize values
			document.selectionEditForm.fw_selectionName.value = fieldToEdit.name;
			document.selectionEditForm.fw_selectionTitle.value = fieldToEdit.label;
			document.selectionEditForm.fw_selectionDesc.value = fieldToEdit.description;
			document.selectionEditForm.fw_selectionRequired.checked = fieldToEdit.required;
			document.selectionEditForm.fw_selectionRepeated.checked = fieldToEdit.repeated;
			
			// initialize options
			for(var j=0; j<fieldToEdit.selections.length; j++){
				eval("document.selectionEditForm.fw_selectFieldOption" + (j+1) + "_Label.value = '" + fieldToEdit.selections[j].label + "'");
				eval("document.selectionEditForm.fw_selectFieldOption" + (j+1) + "_Value.value = '" + fieldToEdit.selections[j].value + "'");
				if(j != (fieldToEdit.selections.length-1)){
					addSelectOption();
				}
			}
			
			showthis('selectionFieldEdit');
			$('editTitle').innerHTML = "Add selection field";
			showthis('edit');
			break;
		default: 
			// invalid form element
			editType = null;
			fieldType = null;
			currentField = null;
			target = null;
			break;
	}
}



// delete existing field
function deleteExistingField(fieldIndex, oOptions){
	
	var oOptions = augment({
		groupIndex: null
	},oOptions);
	
	/*
	// check groupIndex
	if(oOptions.groupIndex != null && ((oOptions.groupIndex < 0 || oOptions.groupIndex > xsdWeaverForm.formElements.length) || (xsdWeaverForm.formElements[oOptions.groupIndex].type != 'group'))){
		// invalid group index
		return false;
	}
	*/
	
	// get form element to edit
	if(oOptions.groupIndex == null){
		// check fieldIndex first
		if(fieldIndex < 0 || fieldIndex > xsdWeaverForm.formElements.length){
			// invalid index
			return false;
		}
		else {
			// delete field and update group
			if(confirm('Are you should you wish to delete ' + xsdWeaverForm.formElements[fieldIndex].label + ' [' + xsdWeaverForm.formElements[fieldIndex].type + '] ?')){
				// delete
				xsdWeaverForm.formElements.splice(fieldIndex,1);
				
				// update group
				// updateGroupArray();
				updateGroupList();
				
				// update display
				updateView();
			}
		}
	}
	else{
		// get group parent indexes in array form
		var groupIndexes = oOptions.groupIndex.split(',');
		var javascriptExec = 'xsdWeaverForm';
		for(var i=0; i<groupIndexes.length; i++){
			javascriptExec += '.formElements[' + groupIndexes[i] + ']';
		}
		
		// first check if given index actually points to the group
		try {
			var elementType = eval(javascriptExec + '.type');
		}
		catch(e){
			alert('Error in deleting ' + e.description);
			return false;
		}
		
		if(elementType != 'group'){
			return false;
		}
		
		// check fieldIndex first
		try {
			var elementLength = eval(javascriptExec + '.formElements.length');
		}
		catch(e){
			alert('Error in deleting ' + e.description);
			return false;
		}
		
		if(fieldIndex < 0 || fieldIndex > elementLength){
			// invalid index
			return false;
		}
		else {
			// get name and type
			try {
				var elementName = eval(javascriptExec + '.formElements[fieldIndex].label');
				var elementType = eval(javascriptExec + '.formElements[fieldIndex].type');
			}
			catch(e){
				alert('Error in deleting ' + e.description);
				return false;
			}
		
			// delete field and update group
			if(confirm('Are you should you wish to delete ' + elementName + ' [' + elementType + '] ?')){
				// delete
				try {
					eval(javascriptExec + '.formElements.splice(fieldIndex,1)');
				}
				catch(e){
					alert('Error in deleting ' + e.description);
					return false;
				}
				
				// update group
				// updateGroupArray();
				updateGroupList();
				
				// update display
				updateView();
			}
			
		}
	}
	
}



// move existing field up
function moveUpExistingField(fieldIndex, oOptions){
	
	var oOptions = augment({
		groupIndex: null
	},oOptions);
	
	/*
	// check groupIndex
	if(oOptions.groupIndex != null && ((oOptions.groupIndex < 0 || oOptions.groupIndex > xsdWeaverForm.formElements.length) || (xsdWeaverForm.formElements[oOptions.groupIndex].type != 'group'))){
		// invalid group index
		return false;
	}
	*/
	
	// get form element to edit
	if(oOptions.groupIndex == null){
		// check fieldIndex first
		if(xsdWeaverForm.formElements.length < 2 || fieldIndex < 1 || fieldIndex > (xsdWeaverForm.formElements.length-1)) {
			// invalid index
			return false;
		}
		else {
			// now move up the field
			var tempField = xsdWeaverForm.formElements[fieldIndex];
			xsdWeaverForm.formElements[fieldIndex] = xsdWeaverForm.formElements[(fieldIndex-1)];
			xsdWeaverForm.formElements[(fieldIndex-1)] = tempField;
			
			// update group
			// updateGroupArray();
			updateGroupList();
			
			// update display
			updateView();
		}
	}
	else{
		// get group parent indexes in array form
		var groupIndexes = oOptions.groupIndex.split(',');
		var javascriptExec = 'xsdWeaverForm';
		for(var i=0; i<groupIndexes.length; i++){
			javascriptExec += '.formElements[' + groupIndexes[i] + ']';
		}
		
		// first check if given index actually points to the group
		try {
			var elementType = eval(javascriptExec + '.type');
		}
		catch(e){
			alert('Error in moving up ' + e.description);
			return false;
		}
		
		if(elementType != 'group'){
			return false;
		}
		
		// check fieldIndex first
		try {
			var elementLength = eval(javascriptExec + '.formElements.length');
		}
		catch(e){
			alert('Error in moving up ' + e.description);
			return false;
		}
		
		if(elementLength < 2 || fieldIndex < 1 || fieldIndex > (elementLength-1)){
			// invalid index
			return false;
		}
		else {
			// now move up the field
			try {
				var tempField = eval(javascriptExec + '.formElements[fieldIndex]');
				eval(javascriptExec + '.formElements[fieldIndex] = ' + javascriptExec + '.formElements[(fieldIndex-1)]');
				eval(javascriptExec + '.formElements[(fieldIndex-1)] = tempField');
			}
			catch(e){
				alert('Error in moving up ' + e.description);
				return false;
			}
					
			/*
			var tempField = xsdWeaverForm.formElements[oOptions.groupIndex].formElements[fieldIndex];
			xsdWeaverForm.formElements[oOptions.groupIndex].formElements[fieldIndex] = xsdWeaverForm.formElements[oOptions.groupIndex].formElements[(fieldIndex-1)];
			xsdWeaverForm.formElements[oOptions.groupIndex].formElements[(fieldIndex-1)] = tempField;
			*/
			
			// no need to update group since sub group is not supported?
			// update group
			// updateGroupArray();
			updateGroupList();
			
			// update display
			updateView();
		}
	}
	
}


// move existing field down
function moveDownExistingField(fieldIndex, oOptions){
	
	var oOptions = augment({
		groupIndex: null
	},oOptions);
	
	/*
	// check groupIndex
	if(oOptions.groupIndex != null && ((oOptions.groupIndex < 0 || oOptions.groupIndex > xsdWeaverForm.formElements.length) || (xsdWeaverForm.formElements[oOptions.groupIndex].type != 'group'))){
		// invalid group index
		return false;
	}
	*/
	
	// get form element to edit
	if(oOptions.groupIndex == null){
		// check fieldIndex first
		if(xsdWeaverForm.formElements.length < 2 || fieldIndex < 0 || fieldIndex > (xsdWeaverForm.formElements.length-2)) {
			// invalid index
			return false;
		}
		else {
			// now move up the field
			var tempField = xsdWeaverForm.formElements[fieldIndex];
			xsdWeaverForm.formElements[fieldIndex] = xsdWeaverForm.formElements[(fieldIndex+1)];
			xsdWeaverForm.formElements[(fieldIndex+1)] = tempField;
			
			// update group
			// updateGroupArray();
			updateGroupList();
			
			// update display
			updateView();
		}
	}
	else{
		// get group parent indexes in array form
		var groupIndexes = oOptions.groupIndex.split(',');
		var javascriptExec = 'xsdWeaverForm';
		for(var i=0; i<groupIndexes.length; i++){
			javascriptExec += '.formElements[' + groupIndexes[i] + ']';
		}
		
		// first check if given index actually points to the group
		try {
			var elementType = eval(javascriptExec + '.type');
		}
		catch(e){
			alert('Error in moving down ' + e.description);
			return false;
		}
		
		if(elementType != 'group'){
			return false;
		}
		
		// check fieldIndex first
		try {
			var elementLength = eval(javascriptExec + '.formElements.length');
		}
		catch(e){
			alert('Error in moving down ' + e.description);
			return false;
		}
		
		if(elementLength < 2 || fieldIndex < 0 || fieldIndex > (elementLength-2)){
			// invalid index
			return false;
		}
		else {
			// now move down the field
			try {
				var tempField = eval(javascriptExec + '.formElements[fieldIndex]');
				eval(javascriptExec + '.formElements[fieldIndex] = ' + javascriptExec + '.formElements[(fieldIndex+1)]');
				eval(javascriptExec + '.formElements[(fieldIndex+1)] = tempField');
			}
			catch(e){
				alert('Error in moving down ' + e.description);
				return false;
			}
			
			/*
			var tempField = xsdWeaverForm.formElements[oOptions.groupIndex].formElements[fieldIndex];
			xsdWeaverForm.formElements[oOptions.groupIndex].formElements[fieldIndex] = xsdWeaverForm.formElements[oOptions.groupIndex].formElements[(fieldIndex+1)];
			xsdWeaverForm.formElements[oOptions.groupIndex].formElements[(fieldIndex+1)] = tempField;
			*/
			
			// no need to update group since sub group is not supported?
			// update group
			// updateGroupArray();
			updateGroupList();
			
			// update display
			updateView();
		}
	}
	
}



function updateFormInfo(){
	xsdWeaverForm.name = $F('fw_formName');
	xsdWeaverForm.label = $F('fw_formTitle');
	xsdWeaverForm.description = $F('fw_formDesc');
}



function resetFormInfo(){
	document.xsdWeaverForm.fw_formName.value = '';
	document.xsdWeaverForm.fw_formTitle.value = '';
	document.xsdWeaverForm.fw_formDesc.value = '';
}

// roll over images
var upArrowOn = new Image();
upArrowOn.src = "images/uparrowon.gif"
var upArrowOff = new Image();
upArrowOff.src = "images/uparrow.gif"
var downArrowOn = new Image();
downArrowOn.src = "images/downarrowon.gif";
var downArrowOff = new Image();
downArrowOff.src = "images/downarrow.gif";



/**
 * @function: updateView
 * @desc: updates all the views
 */
function updateView() {
	
	// update form name, title and description first
	updateFormInfo();
	
	// update preview view
	if(xsdWeaverForm.formElements.length == 0){
		$('previewContent').innerHTML = "<p>Empty form!</p>\n<p>Add a form field by selecting a form field from Add drop-down menu above.</p>";
	}
	else {
		var previewHTML = '';
		if(outputTableHtml){ 
			previewHTML += "\n" + '<table>';
		}
		for(var i=0; i<xsdWeaverForm.formElements.length; i++){
			// global variable that flags whether to use table or not
			if(outputTableHtml){
				previewHTML += xsdWeaverForm.formElements[i].toHtmlTable();
			}
			else{
				previewHTML += xsdWeaverForm.formElements[i];				
			}
		}
		if(outputTableHtml){ 
			previewHTML += "\n" + '</table>';
		}
		$('previewContent').innerHTML = previewHTML;	
	}
	
	// update tree view
	if(xsdWeaverForm.formElements.length == 0){
		$('treeContent').innerHTML = "<p>Empty form!</p>\n<p>Add a form field by selecting a form field from Add drop-down menu above.</p>";
	}
	else {
		$('treeContent').innerHTML = createTreeView();
	}
	
	// update html source view
	if(xsdWeaverForm.formElements.length == 0){
		$('htmlContent').innerHTML = "<p>Empty form!</p>\n<p>Add a form field by selecting a form field from Add drop-down menu above.</p>";
	}
	else {
		// global variable that flags whether to use table or not
		if(outputTableHtml){
			$('htmlContent').innerHTML = "<pre>" + htmlEntities(xsdWeaverForm.toHtmlTable()) + "</pre>";
		}
		else{
			$('htmlContent').innerHTML = "<pre>" + htmlEntities(xsdWeaverForm) + "</pre>";			
		}
	}
	
	
	// update xsd source view
	/*
	if(xsdWeaverForm.formElements.length == 0){
		$('xsdContent').innerHTML = "<p>Empty form!</p>\n<p>Add a form field by selecting a form field from Add drop-down menu above.</p>";
	}
	else {
		*/
		$('xsdContent').innerHTML = "<pre>" + htmlEntities(xsdWeaverForm.toXsd()) + "</pre>";
	/* } */
	
	//Sortable.create('treeViewList',{tag:'li'});
}



// function to create tree
function createTreeView(oOptions){
	
	// options
	var oOptions = augment({formParent: null, parentIndexes: new Array()}, oOptions );
	
	if(oOptions.parentIndexes.length == 0){
		
		var treeHtml = '<ul id="treeViewList"><li class="openNode"> Form: ' + htmlEntities(xsdWeaverForm.label) + '<ul>';
		
		// loop through elements
		for(var i=0; i<xsdWeaverForm.formElements.length; i++){
			
			// global variable that flags whether to use table or not
			// GROUP!!!
			if(xsdWeaverForm.formElements[i].type == "group"){
				
				treeHtml += '<li class="openNode">';
				
				// add options
				treeHtml += '<span> <a href="#" onclick="editField(' + i + '); return false;"><img src="images/stock_edit-16.png" alt="Edit" title="Edit" /></a>  <a href="#" onclick="deleteExistingField(' + i + '); return false;"><img src="images/delete.png" alt="Delete" title="Delete" /></a> ';
				if(i != 0){
					treeHtml += '<a href="#" onclick="moveUpExistingField(' + i + '); return false;"><img src="images/uparrow.gif" alt="Up" title="Up" onmouseover="this.src=upArrowOn.src" onmouseout="this.src=upArrowOff.src" /></a> ';
				}
				else{
					treeHtml += '<img src="images/arrowSpacer.gif" alt="Up" title="Up" class="noBorder" /> ';
				}
	       
				if(i != (xsdWeaverForm.formElements.length-1)){
					treeHtml += '<a href="#" onclick="moveDownExistingField(' + i + '); return false;"><img src="images/downarrow.gif" alt="Down" title="Down" onmouseover="this.src=downArrowOn.src" onmouseout="this.src=downArrowOff.src" /></a>';
				}
				else{
					treeHtml += '<img src="images/arrowSpacer.gif" alt="Down" title="Down" /> ';
				}
				treeHtml += '</span>';
				// end options
				
				treeHtml += ' <a href="#" onclick="editField(' + i + '); return false;">' + htmlEntities(xsdWeaverForm.formElements[i].label) + '</a> (' + xsdWeaverForm.formElements[i].type + ')';
				
				// now check if 
				treeHtml += '<ul>';
				if(xsdWeaverForm.formElements[i].formElements.length == 0){
					treeHtml += '<li class="node">No Members</li>';
				}
				else{
					var parentIndex = new Array();
					parentIndex[0] = i;
					treeHtml += createTreeView( {formParent: xsdWeaverForm.formElements[i], parentIndexes: parentIndex} );
				}
				treeHtml += '</ul></li>';
				
			}
			else{	// NO GROUP!!!
				
				treeHtml += '<li class="node">';
				
				// add options
				treeHtml += '<span> <a href="#" onclick="editField(' + i + '); return false;"><img src="images/stock_edit-16.png" alt="Edit" title="Edit" /></a>  <a href="#" onclick="deleteExistingField(' + i + '); return false;"><img src="images/delete.png" alt="Delete" title="Delete" /></a> ';
				if(i != 0){
					treeHtml += '<a href="#" onclick="moveUpExistingField(' + i + '); return false;"><img src="images/uparrow.gif" alt="Up" title="Up" onmouseover="this.src=upArrowOn.src" onmouseout="this.src=upArrowOff.src" /></a> ';
				}
				else{
					treeHtml += '<img src="images/arrowSpacer.gif" alt="Up" title="Up" /> ';
				}
	       
				if(i != (xsdWeaverForm.formElements.length-1)){
					treeHtml += '<a href="#" onclick="moveDownExistingField(' + i + '); return false;"><img src="images/downarrow.gif" alt="Down" title="Down" onmouseover="this.src=downArrowOn.src" onmouseout="this.src=downArrowOff.src" /></a>';
				}
				else{
					treeHtml += '<img src="images/arrowSpacer.gif" alt="Down" title="Down" /> ';
				}
				treeHtml += '</span>';
				// end options
				
				treeHtml += ' <a href="#" onclick="editField(' + i + '); return false;">' + htmlEntities(xsdWeaverForm.formElements[i].label) + '</a> (' + xsdWeaverForm.formElements[i].type + ')';
				treeHtml += '</li>';
				
			}
		}
		treeHtml += '</ul></li></ul>';
		
		return treeHtml;
	}
	else{
		// sub groups
		var treeHtml = '';
		var parentIndexes = oOptions.parentIndexes.join(',');
		
		// go through its elements
		for(var i=0; i<oOptions.formParent.formElements.length; i++){
			
			if(oOptions.formParent.formElements[i].type == 'group'){
				// another subgroup
				treeHtml += '<li class="openNode">';
				
				// add options
				treeHtml += '<span> <a href="#" onclick="editField(' + i + ',{groupIndex: \'' + parentIndexes + '\'}); return false;"><img src="images/stock_edit-16.png" alt="Edit" title="Edit" /></a>  <a href="#" onclick="deleteExistingField(' + i + ',{groupIndex: \'' + parentIndexes + '\'}); return false;"><img src="images/delete.png" alt="Delete" title="Delete" /></a> ';
				if(i != 0){
					treeHtml += '<a href="#" onclick="moveUpExistingField(' + i + ',{groupIndex: \'' + parentIndexes + '\'}); return false;"><img src="images/uparrow.gif" alt="Up" title="Up" onmouseover="this.src=upArrowOn.src" onmouseout="this.src=upArrowOff.src" /></a> ';
				}
				else{
					treeHtml += '<img src="images/arrowSpacer.gif" alt="Up" title="Up" class="noBorder" /> ';
				}
	       
				if(i != (oOptions.formParent.formElements.length-1)){
					treeHtml += '<a href="#" onclick="moveDownExistingField(' + i + ',{groupIndex: \'' + parentIndexes + '\'}); return false;"><img src="images/downarrow.gif" alt="Down" title="Down" onmouseover="this.src=downArrowOn.src" onmouseout="this.src=downArrowOff.src" /></a>';
				}
				else{
					treeHtml += '<img src="images/arrowSpacer.gif" alt="Down" title="Down" /> ';
				}
				treeHtml += '</span>';
				// end options
				
				treeHtml += ' <a href="#" onclick="editField(' + i + ',{groupIndex: \'' + parentIndexes + '\'}); return false;">' + htmlEntities(oOptions.formParent.formElements[i].label) + '</a> (' + oOptions.formParent.formElements[i].type + ')';
				
				// check for subgroup
				treeHtml += '<ul>';
				if(oOptions.formParent.formElements[i].formElements.length == 0){
					treeHtml += '<li class="node">No Members</li>';
				}
				else{
					// oOptions.parentIndexes[oOptions.parentIndexes.length] = i;
					var newCurrentIndex = new Array();
					newCurrentIndex[0] = i;
					var newParentIndexes = oOptions.parentIndexes.concat(newCurrentIndex);
					treeHtml += createTreeView({formParent: oOptions.formParent.formElements[i], parentIndexes: newParentIndexes});
				}
				treeHtml += '</ul></li>';
			}
			else{
				// regular element
				treeHtml += '<li class="node">';
						
				// add options
				treeHtml += '<span id=""> <a href="#" onclick="editField(' + i + ',{groupIndex: \'' + parentIndexes + '\'}); return false;"><img src="images/stock_edit-16.png" alt="Edit" title="Edit" /></a>  <a href="#" onclick="deleteExistingField(' + i + ',{groupIndex: \'' + parentIndexes + '\'}); return false;"><img src="images/delete.png" alt="Delete" title="Delete" /></a> ';
				if(i != 0){
					treeHtml += '<a href="#" onclick="moveUpExistingField(' + i + ',{groupIndex: \'' + parentIndexes + '\'}); return false;"><img src="images/uparrow.gif" alt="Up" title="Up" onmouseover="this.src=upArrowOn.src" onmouseout="this.src=upArrowOff.src" /></a> ';
				}
				else{
					treeHtml += '<img src="images/arrowSpacer.gif" alt="Up" title="Up" /> ';
				}
	       
				if(i != (oOptions.formParent.formElements.length-1)){
					treeHtml += '<a href="#" onclick="moveDownExistingField(' + i + ',{groupIndex: \'' + parentIndexes + '\'}); return false;"><img src="images/downarrow.gif" alt="Down" title="Down" onmouseover="this.src=downArrowOn.src" onmouseout="this.src=downArrowOff.src" /></a>';
				}
				else{
					treeHtml += '<img src="images/arrowSpacer.gif" alt="Down" title="Down" /> ';
				}
				treeHtml += '</span>';
				// end options
				
				treeHtml += ' <a href="#" onclick="editField(' + i + ',{groupIndex: \'' + parentIndexes + '\'}); return false;">' + htmlEntities(oOptions.formParent.formElements[i].label) + '</a> (' + oOptions.formParent.formElements[i].type + ')';
				treeHtml += '</li>'
			}
		}
		
		return treeHtml;
	}
}
