
var uploadifyQueueCount = 0;
var uploadifyFilesList = new Array();
var doSubmit = false;

	function select_other_input_control(field_name){

		/*
		 * How to use:
		 *  A text input field should be named myfield_other
		 *  The control field (named myfield) has the value "other", it will show the element with the ID myfield_other
		*/

		var input_field_name = "#" + field_name + "_input";
		$( input_field_name ).hide();

		var control_field_name = "select[name=" + field_name + "]";
		$( control_field_name ).change(function() {
			if( $(this).val() == 'Other' ){
				$( input_field_name ).fadeIn();
			} else {
				$( input_field_name ).fadeOut();
			}
		});

	}

	function radio_other_input_control(field_name){

		/*
		 * How to use:
		 *  A text input field should be named myfield_other
		 *  The control field (named myfield) has the value "other", it will show the element with the ID myfield_other
		*/

		var input_field_name = "#" + field_name + "_input";
		$( input_field_name ).hide();

		var control_field_name = "input[name=" + field_name + "]:radio";
		$( control_field_name ).change(function() {
			if( $(this).val() == 'Other' || $(this).val() == 'Yes' ){
				$( input_field_name ).fadeIn();
			} else {
				$( input_field_name ).fadeOut();
			}
		});

	}

	function check_other_input_control(field_name){

		/*
		 * How to use:
		 *  A text input field should be named myfield_other
		 *  The control field (named myfield) has the value "other", it will show the element with the ID myfield_other
		*/

		var input_field_name = "#" + field_name + "_input";
		$( input_field_name ).hide();

		var control_field_name = "#" + field_name;
		$( control_field_name ).change(function() {
			if( $( control_field_name ).attr('checked') ){
				$( input_field_name ).fadeIn();
			} else {
				$( input_field_name ).fadeOut();
			}
		});

	}

function formSubmit() {

	if ( doSubmit ) {

		$("#process_notice").text('Submitting form...');
		return true;

	} else {

		$('[name=submit_button]').attr('disabled', 'disabled');

		// Do a full screen notice that removes control from user
		$("#process_notice").modal();

		// Check fields for any that are incomplete and notify
		$("#process_notice").text('Checking fields for completeness...');

		// Initiate upload of attached files
		if ( uploadifyQueueCount ) {
			// There is something in the queue, so upload them;
			$('#attach_files').uploadifyUpload();

			// Uploadify will run afterUpload() at the desired time, but will also run anything following this line imediately after being told to initiate the upload

		} else {
			// There isn't anything in the attachment queue, so don't try to upload;
			$("#process_notice").text('No attachments to upload, continuing...');
			return afterUpload(false);
			// Execution doesn't get to here, due to a "return true" in afterUpload()

		}

		// Prevent premature submisison by Uploadify - it continues script execution without waiting for a result from afterUpload()
		return false;

	}

}

function afterUpload(data) {

	if ( data ) {
		$("#process_notice").text('Uploading complete.');
		$("[name='attachments_list']").val( uploadifyFilesList );
		doSubmit = true;
		$("#energy_assessment_application_form").submit();
	} else {
		$("#process_notice").text('No attachments, submitting form contents...');
	}

	// Submits the form when no files attached
	return true;

}
