function nl2br(str, is_xhtml) {
    	var breakTag = '';
 
    	breakTag = '<br />';
    	if (typeof is_xhtml != 'undefined' && !is_xhtml) 
	{
        	breakTag = '<br>';
    	}
 
    	return (str + '').replace(/([^>]?)\n/g, '$1'+ breakTag +'\n');
}

function afferr(message){
	$("#graph_err").css('display','block');

   	if(typeof(message) != 'undefined')
      		$("#graph_err").html('<p>' + message + '</p>');
       	else
            	$("#graph_err").html('<p>erreur!</p>');
}

function hideerr(){
        $("#graph_err").css('display','none');
}


function swfUploadLoaded() {
	try 
	{
		swfu.startUpload();
	} 
	catch (ex) 
	{
		afferr("pas de photo?");
	}

	return false;
}

 // Called by the queue complete handler to submit the form
function uploadDone(id) 
{
	try 
	{
		s = $("#form_comm").serialize();
		$.ajax({
			type: "POST",
			data: s,
			url: "/ajax/graph/new_graph.php",
			dataType : "json",
			error: function(msg, string){
				return false;
			},
			success: function(data, status){
			 	if(typeof(data.retour) != 'undefined')
				{
			 		if (data.retour == "0")
					{
						hideerr();

						$("#commentairesPostes").load("/graph/lc.php", {'graph_entite': $("#graph_entite").val(), 'graph_entite_id': $("#graph_entite_id").val()});
						$("#GraphPagination").pagination('renumber', {pages: data.pages, current: 0});

						if (data.pages <= 1)
							$("#GraphPagination").hide();
						else
							$("#GraphPagination").show();

						$("#description").val("commentaire");
                                                $("#video_url").val("http://");
                                                $("#txtFileName").val("");
                                                $("#resume_file").val("");
                                                $("#graph_photo").val("");
                                                $(":radio:checked").attr("checked", false);
					}
					else
						afferr(data.error);
				}
			}
		});

		return false;
	} 
	catch (ex) 
	{	
		afferr("<p>Error submitting form</p>");
	}
}

function fileDialogStart() {
	var txtFileName = document.getElementById("txtFileName");
	txtFileName.value = "";

	this.cancelUpload();
}

function fileQueueError(file, errorCode, message)  {
	try 
	{
		switch (errorCode) 
		{
			case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
				afferr("You have attempted to queue too many files.\n" + (message === 0 ? "You have reached the upload limit." : "You may select " + (message > 1 ? "up to " + message + " files." : "one file.")));
				return;
			case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
				afferr("The file you selected is too big.");
				this.debug("Error Code: File too big, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
				return;
			case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
				afferr("The file you selected is empty.  Please select another file.");
				this.debug("Error Code: Zero byte file, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
				return;
			case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
				afferr("The file you choose is not an allowed file type.");
				this.debug("Error Code: Invalid File Type, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
				return;
			default:
				afferr("An error occurred in the upload. Try again later.");
				this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
				return;
		}
	} 
	catch (e) 
	{
		;
	}
}

function fileQueued(file) {
	try 
	{
		var txtFileName = document.getElementById("txtFileName");
		txtFileName.value = file.name;
		$("#typeVideoDM").attr("checked", false);
		$("#typePhotoOrdi").attr("checked", true);
	} 
	catch (e) 
	{
		;
	}

}

function fileDialogComplete(numFilesSelected, numFilesQueued) {
	;
}

function uploadProgress(file, bytesLoaded, bytesTotal) {

	try 
	{
		var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);

		file.id = "singlefile";	// This makes it so FileProgress only makes a single UI element, instead of one for each file
		var progress = new FileProgress(file, this.customSettings.progress_target);
		progress.setProgress(percent);
		progress.setStatus("Uploading...");
	} 
	catch (e) 
	{
		;
	}
}

function uploadSuccess(file, serverData) {
	try {
		file.id = "singlefile";	// This makes it so FileProgress only makes a single UI element, instead of one for each file
		var progress = new FileProgress(file, this.customSettings.progress_target);
		progress.setComplete();
		progress.setStatus("Complete.");
		progress.toggleCancel(true);
		
		if (serverData === " ") 
		{
			this.customSettings.upload_successful = false;
		} 
		else 
		{
			this.customSettings.upload_successful = true;
			document.getElementById("graph_photo").value = serverData;
		}
		
	} 
	catch (e) 
	{
		;
	}
}

function uploadComplete(file) {
	try 
	{
		if (this.customSettings.upload_successful) 
		{
			this.setButtonDisabled(false);
			uploadDone(1);
		} 
		else 
		{
			file.id = "singlefile";	// This makes it so FileProgress only makes a single UI element, instead of one for each file
			var progress = new FileProgress(file, this.customSettings.progress_target);
			progress.setError();
			progress.setStatus("File rejected");
			progress.toggleCancel(true);
			
			var txtFileName = document.getElementById("txtFileName");
			txtFileName.value = "";

			afferr("There was a problem with the upload.\nThe server did not accept it.");
		}
	} 
	catch (e) 
	{
		;
	}
}

function uploadError(file, errorCode, message) {
	try 
	{	
		if (errorCode === SWFUpload.UPLOAD_ERROR.FILE_CANCELLED) 
		{
			return;
		}
		
		var txtFileName = document.getElementById("txtFileName");
		txtFileName.value = "";
		
		switch (errorCode) 
		{
			case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:
				afferr("There was a configuration error.  You will not be able to upload a resume at this time.");
				this.debug("Error Code: No backend file, File name: " + file.name + ", Message: " + message);
				return;
			case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
				afferr("You may only upload 1 file.");
				this.debug("Error Code: Upload Limit Exceeded, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
				return;
			case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
				break;
			case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
				break;
			default:
				afferr("An error occurred in the upload. Try again later.");
				this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
				return;
		}

		file.id = "singlefile";	// This makes it so FileProgress only makes a single UI element, instead of one for each file
		var progress = new FileProgress(file, this.customSettings.progress_target);
		progress.setError();
		progress.toggleCancel(true);

		switch (errorCode) 
		{
			case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
				progress.setStatus("Upload Error");
				this.debug("Error Code: HTTP Error, File name: " + file.name + ", Message: " + message);
				break;
			case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
				progress.setStatus("Upload Failed.");
				this.debug("Error Code: Upload Failed, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
				break;
			case SWFUpload.UPLOAD_ERROR.IO_ERROR:
				progress.setStatus("Server (IO) Error");
				this.debug("Error Code: IO Error, File name: " + file.name + ", Message: " + message);
				break;
			case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
				progress.setStatus("Security Error");
				this.debug("Error Code: Security Error, File name: " + file.name + ", Message: " + message);
				break;
			case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
				progress.setStatus("Upload Cancelled");
				this.debug("Error Code: Upload Cancelled, File name: " + file.name + ", Message: " + message);
				break;
			case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
				progress.setStatus("Upload Stopped");
				this.debug("Error Code: Upload Stopped, File name: " + file.name + ", Message: " + message);
				break;
		}
	} 
	catch (ex) 
	{
		;
	}
}

