// for tracking the active file field
var ffId;
var refreshPath;

// callbacks for webtoolkit ifram upload
function startCallback() {
	// show the loading image
	$('#attLoadImg' + ffId).show();
	return true;
}

function completeCallback(response) {
	// hide the loading image
	$('#attLoadImg' + ffId).hide();
	// replace the field's html with the response
	$('#attField' + ffId).html(response);
}

// submit function -- sets the id tracking variable then submits via webtoolkit
function submitFileField(id, f, c) {
	ffId = id;
	return AIM.submit(f,c);
}

// refreshPath setter
function setRefreshPath(path) {
	refreshPath = path;
}

// showFFIcons setter
function setShowFFIcons(value) {
	showFFIcons = value
}

// reset a file field's html to its initial state
function refreshFileField(id) {
	$('#attField' + id).load(refreshPath , {'id':id});
}

// reset a file field's html to its initial state
function controlFileField(id, controlPath) {
	$('#attField' + id).load(controlPath);
}

// expand a field to show the form
function showFileForm(id) {
	// if a field is already expanded, remember it's id 
	var lastId;
	$('.attForm:visible').each(function(){
		lastId = $(this).children('input[name="id"]')[0].value;
	});
	
	// hide all expanded forms
	$('.attForm:visible').hide();
	
	// show all hidden expand buttons
	$('.attAddFileButton:hidden').show();
	
	// hide the expand button for the field to expand
	$('#attAddFileButton' + id).hide();
	
	// reset the form to show
	$('#attForm' + id)[0].reset();
	
	// show the form
	$('#attForm' + id).show('slow');
	
	// reset the html for the previously expanded field
	if(lastId != undefined)
		refreshFileField(lastId);
}