
// SWFUpload

function csvupload()
{
	/*
	swfu = new SWFUpload({
		upload_script : "/termine/upload.php",
		target : "SWFUploadTarget",
		flash_path : "SWFUpload/SWFUpload.swf",
		allowed_filesize : 10240,	// 30 MB
		allowed_filetypes : "*.csv",
		allowed_filetypes_description : "CSV-Dateien",
		browse_link_innerhtml : "CSV aussuchen",
		upload_link_innerhtml : "Upload starten",
		browse_link_class : "textbutton",
		upload_link_class : "textbutton",
		
		flash_loaded_callback : 'swfu.flashLoaded',
		upload_file_queued_callback : "replaceQueue",
		upload_file_start_callback : 'showloader',
		upload_progress_callback : 'uploadProgress',
		upload_file_complete_callback : 'showtempimage',
		auto_upload : true			
	});
	*/

	window.scrollTo(0,1);
	object_name = "tour";				 
	swfu = new SWFUpload({
		
		// Backend Settings
		upload_url: "http://www.desimo.de/fileadmin/termine/upload.php",	// Relative to the SWF file (or you can use absolute paths)
		post_params: {},
	
		// File Upload Settings
		file_size_limit : 10240,
		file_types : "*.csv",
		file_types_description : "CSV-Dateien",
		file_upload_limit : "0",
		file_queue_limit : "0",
	
		// Event Handler Settings (all my handlers are in the Handler.js file)
		file_dialog_start_handler : fileDialogStart,
		file_queued_handler : fileQueued,
		file_queue_error_handler : fileQueueError,
		file_dialog_complete_handler : fileDialogComplete,
		upload_start_handler : uploadStart,
		upload_progress_handler : uploadProgress,
		upload_error_handler : uploadError,
		upload_success_handler : uploadSuccess,
		upload_complete_handler : uploadComplete,
	
		// Flash Settings
		flash_url : "http://www.desimo.de/fileadmin/termine/SWFUpload/swfupload.swf",	// flash_url : basepath + "/swf/swfupload/swfupload_f8.swf" Relative to this file (or you can use absolute paths)
		
		swfupload_element_id : object_name + "flashUI",		// Setting from graceful degradation plugin
		degraded_element_id : object_name + "degradedUI",	// Setting from graceful degradation plugin
	
		custom_settings : {
			upload_start_action : "UploadStart()",
			upload_complete_action : "UploadQueueComplete()",
			progressTarget : object_name + "fsUploadProgress",
			cancelButtonId : object_name + "btnCancel"
		},
		
		// Debug Settings
		debug: false
	});
	window.scrollTo(0,0);
}

function UploadStart() {}

function UploadQueueComplete() {
	window.location = window.location.href;
}

function fileDialogStart() {
	/* I don't need to do anything here */
}
function fileQueued(fileObj) {
	try {
		// You might include code here that prevents the form from being submitted while the upload is in
		// progress.  Then you'll want to put code in the Queue Complete handler to "unblock" the form
		var progress = new FileProgress(fileObj, this.customSettings.progressTarget);
		progress.SetStatus("Anstehend...");
		progress.ToggleCancel(true, this);

	} catch (ex) { this.debug(ex); }

}

function fileQueueError(fileObj, error_code, message) {
	try {
		if (error_code === SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED) {
			alert("Sie haben zu viele Dateien in der Warteschlange.\n" + (message === 0 ? "Sie haben das Upload-Limit erreicht." : "Sie koennen " + (message > 1 ? "bis zu " + message + " Dateien auswaehlen." : "eine Datei auswaehlen.")));
			return;
		}

		var progress = new FileProgress(fileObj, this.customSettings.progressTarget);
		progress.SetError();
		progress.ToggleCancel(false);

		switch(error_code) {
			case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
				progress.SetStatus("Datei ist zu gross.");
				this.debug("Fehler Code: Datei ist zu gross, Dateiname: " + fileObj.name + ", Dateigroesse: " + fileObj.size + ", Hinweis: " + message);
				break;
			case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
				progress.SetStatus("Kann keine Null-Byte-Datei hochladen.");
				this.debug("Fehler Code: Null-Byte-Datei, Dateiname: " + fileObj.name + ", Dateigroesse: " + fileObj.size + ", Hinweis: " + message);
				break;
			case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
				progress.SetStatus("Ungueltiger Dateityp.");
				this.debug("Fehler Code: Ungueltiger Dateityp, Dateiname: " + fileObj.name + ", Dateigroesse: " + fileObj.size + ", Hinweis: " + message);
				break;
			case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
				alert("Sie haben zu viele Dateien ausgewaehlt.  " +  (message > 1 ? "Sie koennen nur noch " +  message + " Dateien hinzufuegen" : "SIe koennen keine weiteren Dateien hinzufuegen."));
				break;
			default:
				if (fileObj !== null) {
					progress.SetStatus("Ungehandleter Fehler");
				}
				this.debug("Fehler Code: " + error_code + ", Dateiname: " + fileObj.name + ", Dateigroesse: " + fileObj.size + ", Hinweis: " + message);
				break;
		}
	} catch (ex) {
        this.debug(ex);
    }
}

function fileDialogComplete(num_files_queued) {
	try {
		if (this.getStats().files_queued > 0) {
			document.getElementById(this.customSettings.cancelButtonId).disabled = false;
		}
		
		/* I want auto start and I can do that here */
		this.startUpload();
	} catch (ex)  {
        this.debug(ex);
	}
}

function uploadStart(fileObj) {
	try {
		/* I don't want to do any file validation or anything,  I'll just update the UI and return true to indicate that the upload should start */
		var progress = new FileProgress(fileObj, this.customSettings.progressTarget);
		progress.SetStatus("Es wird hochgeladen...");
		progress.ToggleCancel(true, this);
		window.setTimeout(this.customSettings.upload_start_action);
	}
	catch (ex) {}
	
	return true;
}

function uploadProgress(fileObj, bytesLoaded, bytesTotal) {

	try {
		var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);

		var progress = new FileProgress(fileObj, this.customSettings.progressTarget);
		progress.SetProgress(percent);
		progress.SetStatus("Es wird hochgeladen...");
	} catch (ex) { this.debug(ex); }
}

function uploadSuccess(fileObj, server_data) {
	try {
		var progress = new FileProgress(fileObj, this.customSettings.progressTarget);
		progress.SetComplete();
		progress.SetStatus("Beendet");
		progress.ToggleCancel(false);

	} catch (ex) { this.debug(ex); }
}

function uploadComplete(fileObj) {
	try {
		/*  I want the next upload to continue automatically so I'll call startUpload here */
		if (this.getStats().files_queued === 0) {
			document.getElementById(this.customSettings.cancelButtonId).disabled = true;
			hideloader();
		} else {	
			this.startUpload();
		}
	} catch (ex) { this.debug(ex); }
	window.setTimeout(this.customSettings.upload_complete_action);
}

function uploadError(fileObj, error_code, message) {
	try {
		var progress = new FileProgress(fileObj, this.customSettings.progressTarget);
		progress.SetError();
		progress.ToggleCancel(false);

		switch(error_code) {
			case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
				progress.SetStatus("Upload Fehler: " + message);
				this.debug("Fehler Code: HTTP Error, Dateiname: " + fileObj.name + ", Hinweis: " + message);
				break;
			case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:
				progress.SetStatus("Konfigurationsfehler");
				this.debug("Fehler Code: kein Backendscript, Dateiname: " + fileObj.name + ", Hinweis: " + message);
				break;
			case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
				progress.SetStatus("Upload fehlgeschlagen.");
				this.debug("Fehler Code: Upload Failed, Dateiname: " + fileObj.name + ", Dateigroesse: " + fileObj.size + ", Hinweis: " + message);
				break;
			case SWFUpload.UPLOAD_ERROR.IO_ERROR:
				progress.SetStatus("Server (IO) Fehler");
				this.debug("Fehler Code: IO Error, Dateiname: " + fileObj.name + ", Hinweis: " + message);
				break;
			case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
				progress.SetStatus("Sicherheitsfehler");
				this.debug("Fehler Code: Security Error, Dateiname: " + fileObj.name + ", Hinweis: " + message);
				break;
			case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
				progress.SetStatus("Upload limit exceeded.");
				this.debug("Fehler Code: Dateigroessen-Begrenzung ueberschritten, Dateiname: " + fileObj.name + ", Dateigroesse: " + fileObj.size + ", Hinweis: " + message);
				break;
			case SWFUpload.UPLOAD_ERROR.SPECIFIED_FILE_ID_NOT_FOUND:
				progress.SetStatus("Datei nicht gefunden.");
				this.debug("Fehler Code: Die Datei wurde nicht gefunden, Dateiname: " + fileObj.name + ", Dateigroesse: " + fileObj.size + ", Hinweis: " + message);
				break;
			case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
				progress.SetStatus("Failed Validation.  Upload skipped.");
				this.debug("Fehler Code: Datei Validierung fehlgeschlagen, Dateiname: " + fileObj.name + ", Dateigroesse: " + fileObj.size + ", Hinweis: " + message);
				break;
			case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
				if (this.getStats().files_queued === 0) {
					document.getElementById(this.customSettings.cancelButtonId).disabled = true;
				}
				progress.SetStatus("Abgebrochen");
				progress.SetCancelled();
				break;
			case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
				progress.SetStatus("Gestoppt");
				break;
			default:
				progress.SetStatus("Ungehandleter Fehler: " + error_code);
				this.debug("Fehler Code: " + error_code + ", Dateiname: " + fileObj.name + ", Dateigroesse: " + fileObj.size + ", Hinweis: " + message);
				break;
		}
	} catch (ex) {
        this.debug(ex);
    }
}



function FileProgress(fileObj, target_id) {
	this.file_progress_id = fileObj.id;

	this.opacity = 100;
	this.height = 0;

	this.fileProgressWrapper = document.getElementById(this.file_progress_id);
	if (!this.fileProgressWrapper) {
		this.fileProgressWrapper = document.createElement("div");
		this.fileProgressWrapper.className = "progressWrapper";
		this.fileProgressWrapper.id = this.file_progress_id;

		this.fileProgressElement = document.createElement("div");
		this.fileProgressElement.className = "progressContainer";

		var progressCancel = document.createElement("a");
		progressCancel.className = "progressCancel";
		progressCancel.href = "#";
		progressCancel.style.visibility = "hidden";
		progressCancel.appendChild(document.createTextNode(" "));

		var progressText = document.createElement("div");
		progressText.className = "progressName";
		progressText.appendChild(document.createTextNode(fileObj.name));

		var progressBar = document.createElement("div");
		progressBar.className = "progressBarInProgress";

		var progressStatus = document.createElement("div");
		progressStatus.className = "progressBarStatus";
		progressStatus.innerHTML = "&nbsp;";

		this.fileProgressElement.appendChild(progressCancel);
		this.fileProgressElement.appendChild(progressText);
		this.fileProgressElement.appendChild(progressStatus);
		this.fileProgressElement.appendChild(progressBar);

		this.fileProgressWrapper.appendChild(this.fileProgressElement);

		document.getElementById(target_id).appendChild(this.fileProgressWrapper);
	} else {
		this.fileProgressElement = this.fileProgressWrapper.firstChild;
	}

	this.height = this.fileProgressWrapper.offsetHeight;

}
FileProgress.prototype.SetProgress = function(percentage) {
	this.fileProgressElement.className = "progressContainer green";
	this.fileProgressElement.childNodes[3].className = "progressBarInProgress";
	this.fileProgressElement.childNodes[3].style.width = percentage + "%";
};
FileProgress.prototype.SetComplete = function() {
	this.fileProgressElement.className = "progressContainer blue";
	this.fileProgressElement.childNodes[3].className = "progressBarComplete";
	this.fileProgressElement.childNodes[3].style.width = "";

	var oSelf = this;
	setTimeout(function() { oSelf.Disappear(); }, 10000);
};
FileProgress.prototype.SetError = function() {
	this.fileProgressElement.className = "progressContainer red";
	this.fileProgressElement.childNodes[3].className = "progressBarError";
	this.fileProgressElement.childNodes[3].style.width = "";

	var oSelf = this;
	setTimeout(function() { oSelf.Disappear(); }, 5000);
};
FileProgress.prototype.SetCancelled = function() {
	this.fileProgressElement.className = "progressContainer";
	this.fileProgressElement.childNodes[3].className = "progressBarError";
	this.fileProgressElement.childNodes[3].style.width = "";

	var oSelf = this;
	setTimeout(function() { oSelf.Disappear(); }, 2000);
};
FileProgress.prototype.SetStatus = function(status) {
	this.fileProgressElement.childNodes[2].innerHTML = status;
};

FileProgress.prototype.ToggleCancel = function(show, upload_obj) {
	this.fileProgressElement.childNodes[0].style.visibility = show ? "visible" : "hidden";
	if (upload_obj) {
		var file_id = this.file_progress_id;
		this.fileProgressElement.childNodes[0].onclick = function() { upload_obj.cancelUpload(file_id); return false; };
	}
};

FileProgress.prototype.Disappear = function() {

	var reduce_opacity_by = 15;
	var reduce_height_by = 4;
	var rate = 30;	// 15 fps

	if (this.opacity > 0) {
		this.opacity -= reduce_opacity_by;
		if (this.opacity < 0) {
			this.opacity = 0;
		}

		if (this.fileProgressWrapper.filters) {
			try {
				this.fileProgressWrapper.filters.item("DXImageTransform.Microsoft.Alpha").opacity = this.opacity;
			} catch (e) {
				// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.
				this.fileProgressWrapper.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + this.opacity + ")";
			}
		} else {
			this.fileProgressWrapper.style.opacity = this.opacity / 100;
		}
	}

	if (this.height > 0) {
		this.height -= reduce_height_by;
		if (this.height < 0) {
			this.height = 0;
		}

		this.fileProgressWrapper.style.height = this.height + "px";
	}

	if (this.height > 0 || this.opacity > 0) {
		var oSelf = this;
		setTimeout(function() { oSelf.Disappear(); }, rate);
	} else {
		this.fileProgressWrapper.style.display = "none";
	}
};

function cancelQueue(instance) {
	document.getElementById(instance.customSettings.cancelButtonId).disabled = true;
	instance.stopUpload();
	var stats;
	
	do {
		stats = instance.getStats();
		instance.cancelUpload();
	} while (stats.files_queued !== 0);
	
}
////////////////////////////////////////////////////

function Tastendruck (Ereignis) 
	{
	if (!Ereignis)
	Ereignis = window.event;
	// alert(Ereignis.keyCode);
	// Vormals 123 = F12, nun 121 = F10
	// Shift + F12
	if (Ereignis.shiftKey == 1 && Ereignis.keyCode == 121) 
		{
			if(document.getElementById('clearer'))
			{
				elem = document.getElementById('clearer');
				/*
				elem.innerHTML = '<div id="SWFUploadTarget"><form action="upload.php" target="uploadframe" method="post" enctype="multipart/form-data"><input type="file" name="Filedata" id="Filedata" /><br /><input type="submit" value="hochladen" /></form></div><div id="tempimage"></div><div id="SWFUploadFileListingFiles"></div>';
				*/
				elem.style.display = 'block';
				elem.innerHTML = '<div id="tourflashUI">\
					<button onclick="swfu.selectFiles()">CSV ausw&auml;hlen</button>\
					<button id="tourbtnCancel" onclick="cancelQueue(swfu);" disabled="disabled" class="swfupload_hidden">Upload abbrechen</button>\
				</div>\
				<div id="tourfsUploadProgress" class="fsUploadProgress swfupload_hidden"></div>';

				window.setTimeout('csvupload();',1000);
			}
		}
	}

document.onkeydown = Tastendruck;

