// JavaScript Document
var swf_upload_control;

function initSWFUpload(upload_path) {
	swf_upload_control = new SWFUpload({
		// Backend settings
		upload_url: upload_path,	// Relative to the SWF file, you can use an absolute URL as well.
		file_post_name: "userfile",

		// Flash file settings
		file_size_limit : "10240",	// 10 MB
		file_types :  "*.gpx;*.kml;*.tcx",	// or you could use something like: "*.doc;*.wpd;*.pdf",
		file_types_description : "GPX, KML, TCX Files",
		file_upload_limit : "0", // Even though I only want one file I want the user to be able to try again if an upload fails
		file_queue_limit : "1", // this isn't needed because the upload_limit will automatically place a queue limit

		// Event handler settings
		swfupload_loaded_handler : myShowUI,
		
		//file_dialog_start_handler : fileDialogStart,		// I don't need to override this handler
		file_queued_handler : fileQueued,
		file_queue_error_handler : fileQueueError,
		file_dialog_complete_handler : fileDialogComplete,
		
		//upload_start_handler : uploadStart,	// I could do some client/JavaScript validation here, but I don't need to.
		upload_progress_handler : uploadProgress,
		upload_error_handler : uploadError,
		upload_success_handler : uploadSuccess,
		upload_complete_handler : uploadComplete,

		post_params : { "swfupload" : "1" }, 
 
 		// Flash Settings
		flash_url : "../swfupload_f9.swf",	// Relative to this file

		// UI settings
		swfupload_element_id : "flashUI",		// setting for the graceful degradation plugin
		degraded_element_id : "degradedUI",

		custom_settings : {
			progress_target : "fsUploadProgress",
			upload_successful : false
		},
		
		// Debug settings
		debug: false
	});

}

function myShowUI() {
	var btnSubmit = document.getElementById("btnSubmit");
	
	//btnSubmit.onclick = doSubmit;
	//btnSubmit.disabled = true;
	
	
	
	SWFUpload.swfUploadLoaded.apply(this);  // Let SWFUpload finish loading the UI.
	//validateForm();
}

function validateForm() {			

}

function fileBrowse() {
	var txtFileName = document.getElementById("txtFileName");
	txtFileName.value = "";

	this.cancelUpload();
	this.selectFile();
}


// Called by the submit button to start the upload
function doSubmit(e) {
	e = e || window.event;
	if (e.stopPropagation) e.stopPropagation();
	e.cancelBubble = true;
	
	try {
		swf_upload_control.startUpload();
	} catch (ex) {

	}
	return false;
}

 // Called by the queue complete handler to submit the form
function uploadDone() {
	try {
		document.choose_file.submit();
	} catch (ex) {
		alert("Error submitting form");
	}
}
