function addRelFields(fsetId) {
	// purpose of function is to add extra fields to field set specified by 'fsetId',
	// where that field set has "Track Relationships" checked off
	// Function will grab all fields from a hidden div with id fsetId + "_tpl", and add those to the bottom
	// of the content inside the fset

	// reference to template div
	tplRef = document.getElementById(fsetId + '_tpl');
	// reference to field set...this is where we will put the fields
	fsetRef = document.getElementById(fsetId + '_fieldset');
	
	// track how many sets of fields have been added inside the fieldset object
	if (!fsetRef.newCount) {
		fsetRef.newCount = 1;
	} else {
		fsetRef.newCount++;
	}		

	// var for template node (div)...this containg everything we will append to the fieldset
	//tplNode = tplRef.firstChild;
	tplNode = tplRef;
	
	
	
	// create a new node copy of the template div
	newNode = tplNode.cloneNode(true);
	newNode.id = newNode.id.replace(/NEW_INDEX/g,'new' + fsetRef.newCount);	

	
	// variables used to store each set of field types within the template
	// and the copy of it (newNode)
	var newFields = new Array;
	var tplFields = new Array;
	
	// store all fields in arrays, for both copy and original (template)
	newFields[0] = newNode.getElementsByTagName('input');
	tplFields[0] = tplNode.getElementsByTagName('input');
	newFields[1] = newNode.getElementsByTagName('select');
	tplFields[1] = tplNode.getElementsByTagName('select');
	newFields[2] = newNode.getElementsByTagName('textarea');
	tplFields[2] = tplNode.getElementsByTagName('textarea');
	newFields[3] = newNode.getElementsByTagName('button');
	tplFields[3] = tplNode.getElementsByTagName('button');
	newFields[4] = newNode.getElementsByTagName('div');
	tplFields[4] = tplNode.getElementsByTagName('div');
	
	
	// loop through each set of field types
	for(x=0; x < newFields.length; x++) {
		newFieldGroup = newFields[x];
		tplFieldGroup = tplFields[x];
		
		// look through each field
		for(i=0; i < newFieldGroup.length; i++) {
			
			newField = newFieldGroup[i];
			tplField = tplFieldGroup[i];

			// replace NEW_INDEX text in name and id properties to make them unique to this set of fields
			if (newField.name) newField.name = newField.name.replace(/NEW_INDEX/g,'new' + fsetRef.newCount);
			if (newField.id) newField.id = newField.id.replace(/NEW_INDEX/g,'new' + fsetRef.newCount);
			
			// IE 5.2 on Mac does not copy eventhandlers with cloneNode, so this puts them all back
			newField.onclick 		= tplField.onclick 		? tplField.onclick 		: null;
			newField.onload 		= tplField.onload 		? tplField.onload 		: null;
			newField.onfocus 		= tplField.onfocus 		? tplField.onfocus 		: null;
			newField.onblur 		= tplField.onblur 		? tplField.onblur 		: null;
			newField.onmouseover 	= tplField.onmouseover 	? tplField.onmouseover 	: null;
			newField.onmouseout 	= tplField.onmouseout 	? tplField.onmouseout 	: null;
			newField.onmousedown 	= tplField.onmousedown 	? tplField.onmousedown 	: null;
			newField.onmouseup 		= tplField.onmouseup 	? tplField.onmouseup 	: null;
		}
	}

	// change visiblity on the new row, so it will display
	newNode.style.display = '';
	
	newNode.id = fsetRef.newCount;
	
	// grab delete images...these are the x images that you click on to delete the relationship fields
	//deleteImages = document.getElementsByName(fsetId + '_delete_link');
	deleteImages = newNode.getElementsByTagName('img');
	// loop through them, and change the string NEW_INDEX to new + fsetRef.newCount...this will allow clicking on the
	// trash can icon to pass the correct record number corresponding to this rel. record
	for(x=0; x < deleteImages.length; x++) {
		deleteImage = deleteImages[x];
		if (deleteImage.onclick && deleteImage.name == fsetId + '_delete_link') {
			
			tbl = deleteImage.getAttribute('tbl');
			if (tbl) {
				deleteImage.onclick = function onclick(event) { delRelFields(this.parentNode.id, fsetId + '_fieldset', tbl, 'new' + fsetRef.newCount) } ;
			}
		}
	}

	// add new node to bottom of field set, before the add new fields button
	//fsetRef.appendChild(newNode);
	//alert(document.getElementById(fsetId + '_add_before'));
	//alert(newNode);
	beforeNode = document.getElementById(fsetId + '_add_before');

	//fsetRef.insertBefore(newNode,beforeNode);
	beforeNode.parentNode.insertBefore(newNode,beforeNode);

	
	
	
	// fix calendar fields...check for attributes in the trigger button..if present, setup the new field to show the calendar popup
	newImages = newNode.getElementsByTagName('img');
	for(x=0; x < newImages.length; x++) {
		newImage = newImages[x];
		date_trigger = newImage.getAttribute('date_trigger');
		if(date_trigger) {
			// replace NEW_INDEX text in name and id properties to make them unique to this set of fields
			if (newImage.name) newImage.name = newImage.name.replace(/NEW_INDEX/g,'new' + fsetRef.newCount);
			if (newImage.id) newImage.id = newImage.id.replace(/NEW_INDEX/g,'new' + fsetRef.newCount);
			date_field = newImage.getAttribute('date_field');
			show_time = newImage.getAttribute('show_time');
			if (show_time) {
				show_time = true;
				if_format = '%b %e, %Y %l:%M %P';
			} else {
				show_time = false;
				if_format = '%b %e, %Y';
			}
			
			Calendar.setup({
				inputField     :    date_field + '_new' + fsetRef.newCount,     // id of the input field
				showsTime : show_time,
				ifFormat : if_format,
				button         :    'date_trigger_' + date_field + '_new' + fsetRef.newCount,  // trigger for the calendar (button ID)
				align          :    'bR',           // alignment (defaults to Br)
				singleClick    :    true,
				weekNumbers    :    false,
				step           :    1,
				timeFormat : 12
			})
		}
	}
	
	
}

function delRelFields(fsetFieldsId, fsetId, tableName, rn) {
	/* 
	Purpose:
		Change fields on form so PHP will then delete corresponding relationship records
		What this will do is delete the set of relationship fields from HTML, and add a new field which PHP will use to delete the relationship
	
		The new field will be similar to this:
			<input type="hidden" name="del_data[Table_Name][]" value="123">
			(where 123 is the record_number of the relationship record)
	
	Parameters:
		fsetFieldsId - the id of the div containing this particular set of fields
		tableName - name of table the fields are from
		rn - record number to delete
	*/

	
	//alert("fsetId: " + fsetId + ", fsetFieldsId: " + fsetFieldsId);
	// grab reference to the div containing the relationship fields
	fsetFieldsRef = document.getElementById(fsetFieldsId);
	
	
	// reference to field set
	fsetRef = document.getElementById(fsetId);
	
	// create a new input element to represent what will be deleted
	delInput = document.createElement("input");
	delInput.name = 'del_data[' + tableName + '][]';
	delInput.value = rn;
	delInput.style.display = 'none';
	
	// add hidden field that will control deletion of record
	fsetRef.appendChild(delInput);

	// remove node that contains the fields
	fsetFieldsRef.parentNode.removeChild(fsetFieldsRef);

}
