

var max = 0; // maximum # of attachments allowed
var currentUploads = 0; // current # of attachment sections on the web page
var nameDesc = ''; // Name property for the Description Input field
var nameFile = ''; // Name property for the File Input field
var scrollPosVert = 0; // stores the current scroll position on the form
// for some reason when a div is taken out, the form
// will scroll to the top on both Firefox and IE

// SCROLL FUNCTIONS
function saveScrollPos(offset)
{
scrollPosVert=(document.all)?document.body.scrollTop:window.pageYOffset-offset;
}

function setScrollPos()
{
window.scrollTo(0, scrollPosVert);
setTimeout('window.scrollTo(0, scrollPosVert)',1);
}

// This function adds a new attachment section to the form
// It is called when the user clicks the "Attach a file" button...
// It takes three arguments:
// maxUploads - the maximum number of attachments allowed
// descFieldName - the field name for the Description Input field
// fileFieldName - the field name for the File Input field
function addUpload(maxUploads, descFieldName, fileFieldName) {
	nameDesc=descFieldName;
	nameFile=fileFieldName;
	max = Number(maxUploads);

	currentUploads++;
	if (currentUploads>max) return;

	if (currentUploads>0)
	document.getElementById('addupload').childNodes[0].data='Attach another file';

	if (currentUploads==max) document.getElementById('addupload').style.visibility='hidden';

	// First, clone the hidden attachment section
	var newFields = document.getElementById('attachment').cloneNode(true);
	newFields.id = '';

	// loop through tags in the new Attachment section
	// and set ID and NAME properties
	var newField = newFields.childNodes;
	for (var i=0;i<newField.length;i++) {
		if (newField[i].name==nameFile) {
			newField[i].id=nameFile+currentUploads;
			newField[i].name=nameFile+currentUploads;
			newField[i].onchange =  updateDesc;
		}

		if (newField[i].name==nameDesc) {
			newField[i].id=nameDesc+currentUploads;
			newField[i].name=nameDesc+currentUploads;
		}
	}

	// Insert our new Attachment section into the Attachments Div
	// on the form...
	var insertHere = document.getElementById('attachmentmarker');
	insertHere.parentNode.insertBefore(newFields,insertHere);

	// Trigger click event on file element (IE5+ ONLY)
	var newAttachment = document.getElementById(nameFile+currentUploads);
	//cannot get this working!!!  ie security does not seem to allow the form to be submitted
	//if the click method has been called.
	//parent.headerFrame.clickFileElement(nameFile+currentUploads);

	// Check to see file was selected, if not remove Attachment
	//if (newAttachment.value == '') {
		//removeFile(document.questionForm,newAttachment.parentNode); //SOMETHING WRONG HERE!!!!
		//alert(newFields.id);
	//}

	// Set the forms current number of file elements
	document.questionForm.numUploads.value = currentUploads;

	// Make the new attachments section visible
	newFields.style.display = 'block';
}

// This function updates the attachments label so filename is displayed
// as we are not showing the real file element
function updateDesc(event) {
	this.parentNode.getElementsByTagName("span")[0].innerHTML = this.value;
}

// This function removes an attachment from the form
// and updates the ID and Name properties of all other
// Attachment sections
function removeFile(container, item) {
	// get the ID number of the upload section to remove
	var tmp = item.getElementsByTagName('input')[0];
	var basefieldname = '';
	if (tmp.type=='text') basefieldname = nameDesc; else basefieldname = nameFile;

	var iRemove=Number(tmp.id.substring(basefieldname.length, tmp.id.length));

	// Shift all INPUT field IDs and NAMEs down by one (for fields with a
	// higher ID than the one being removed)
	var x = document.getElementById('attachments').getElementsByTagName('input');
	for (i=0;i<x.length;i++) {
		if (x[i].type=='text') basefieldname=nameDesc; else basefieldname=nameFile;

		var iEdit = Number(x[i].id.substring(basefieldname.length, x[i].id.length));
		if (iEdit>iRemove) {
			x[i].id=basefieldname+(iEdit-1);
			x[i].name=basefieldname+(iEdit-1);
		}
	}
	currentUploads--;
	saveScrollPos(0);
	container.removeChild(item);
	setScrollPos();
	document.getElementById('addupload').style.visibility='visible';
	if (currentUploads==0)
		document.getElementById('addupload').childNodes[0].data='Attach a file';
	document.questionForm.numUploads.value = currentUploads;
}
