//******************************************************************************************************
//	Name: ubr_file_upload.js
//	Revision: 3.2
//	Date: 10:40 AM May 16, 2009
//	Link: http://uber-uploader.sourceforge.net
//	Developer Peter Schmandra
//
//	BEGIN LICENSE BLOCK
//	The contents of this file are subject to the Mozilla Public License
//	Version 1.1 (the "License"); you may not use this file except in
//	compliance with the License. You may obtain a copy of the License
//	at http://www.mozilla.org/MPL/
//
//	Software distributed under the License is distributed on an "AS IS"
//	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
//	the License for the specific language governing rights and
//	limitations under the License.
//
//	Alternatively, the contents of this file may be used under the
//	terms of either the GNU General Public License Version 2 or later
//	(the "GPL"), or the GNU Lesser General Public License Version 2.1
//	or later (the "LGPL"), in which case the provisions of the GPL or
//	the LGPL are applicable instead of those above. If you wish to
//	allow use of your version of this file only under the terms of
//	either the GPL or the LGPL, and not to allow others to use your
//	version of this file under the terms of the MPL, indicate your
//	decision by deleting the provisions above and replace them with the
//	notice and other provisions required by the GPL or the LGPL. If you
//	do not delete the provisions above, a recipient may use your
//	version of this file under the terms of any one of the MPL, the GPL
//	or the LGPL.
//	END LICENSE BLOCK
//***************************************************************************************************************

var seconds = 0;
var minutes = 0;
var hours = 0;
var get_status_url;
var total_upload_size = 0;
var total_kbytes = 0;
var toggle_upload_stats = 0;
var file_label_highlight_on = '#FFFFE0';
var file_label_highlight_off = '#F9F9F9';
var CPB_loop = false;
var CPB_width = 0;
var CPB_bytes = 0;
var CPB_time_width = 500;
var CPB_time_bytes = 15;
var CPB_hold = true;
var CPB_byte_timer;
var CPB_status_timer;
var BPB_width_inc = 0;
var BPB_width_new = 0;
var BPB_width_old = 0;
var BPB_timer;
var UP_timer;

function getFileName(slot_value){
	var index_of_last_slash = slot_value.lastIndexOf("\\");

	if(index_of_last_slash < 1){ index_of_last_slash = slot_value.lastIndexOf("/"); }

	var file_name = slot_value.slice(index_of_last_slash + 1, slot_value.length);

	return file_name;
}

function getFileExtension(slot_value){
	var file_extension = slot_value.substring(slot_value.lastIndexOf('.') + 1, slot_value.length).toLowerCase();

	return file_extension;
}

function highlightFileLabel(file_label, color){ JQ("#"+file_label).css({background:color}); }

function clearFileLabels(){
	JQ(":file").each(function(){
		highlightFileLabel(JQ(this).attr('id')+"_label", file_label_highlight_off);
	});
}

// Check the file format before uploading
function checkFileNameFormat(){
	if(!check_file_name_format){ return false; }

	var found_error = false;

	JQ(":file").each(function(){
		if(JQ(this).val() !== ""){
			var file_name = getFileName(JQ(this).val());

			if(file_name.length > max_file_name_chars){
				highlightFileLabel(JQ(this).attr('id')+"_label", file_label_highlight_on);
				alert("Error, file name cannot be more than " + max_file_name_chars + " characters.");
				found_error = true;
			}

			if(file_name.length < min_file_name_chars){
				highlightFileLabel(JQ(this).attr('id')+"_label", file_label_highlight_on);
				alert("Error, file name cannot be less than " + min_file_name_chars + " characters.");
				found_error = true;
			}

			if(!check_file_name_regex.test(file_name)){
				highlightFileLabel(JQ(this).attr('id')+"_label", file_label_highlight_on);
				alert(check_file_name_error_message);
				found_error = true;
			}
		}
	});

	return found_error;
}

// Check for legal file extentions
function checkAllowFileExtensions(){
	if(!check_allow_extensions_on_client){ return false; }

	var found_error = false;

	JQ(":file").each(function(){
		if(JQ(this).val() !== ""){
			var file_extension = getFileExtension(getFileName(JQ(this).val()));

			if(!file_extension.match(allow_extensions)){
				highlightFileLabel(JQ(this).attr('id')+"_label", file_label_highlight_on);
				alert('Sorry, uploading a file with the extension "' + file_extension + '" is not allowed.');
				found_error = true;
			}
		}
	});

	return found_error;
}

// Check for illegal file extentions
function checkDisallowFileExtensions(){
	if(!check_disallow_extensions_on_client){ return false; }

	var found_error = false;

	JQ(":file").each(function(){
		if(JQ(this).val() !== ""){
			var file_extension = getFileExtension(getFileName(JQ(this).val()));

			if(file_extension.match(disallow_extensions)){
				highlightFileLabel(JQ(this).attr('id')+"_label", file_label_highlight_on);
				alert('Sorry, uploading a file with the extension "' + file_extension + '" is not allowed.');
				found_error = true;
			}
		}
	});

	return found_error;
}

// Make sure the user selected at least one file
function checkNullFileCount(){
	if(!check_null_file_count){ return false; }

	var found_file = false;

	JQ(":file").each(function(){
		if(JQ(this).val() !== ""){ found_file = true; }
	});

	if(!found_file){
		alert("Please Choose A File To Upload.");
		return true;
	}
	else{ return false; }
}

// Make sure the user is not uploading duplicate files
function checkDuplicateFileCount(){
	if(!check_duplicate_file_count){ return false; }

	var found_duplicate = false;
	var file_count = 0;
	var file_name_array = [];

	JQ(":file").each(function(){
		if(JQ(this).val() !== ""){
			var obj = new Object;
			obj.file_name = getFileName(JQ(this).val());
			obj.label_name = JQ(this).attr('id')+"_label";
			file_name_array[file_count] = obj;
			file_count++;
		}
	});
	
	for(var i = 0; i < file_name_array.length; i++){
		var obj_1 = file_name_array[i];
		
		for(var j = 0; j < file_name_array.length; j++){
			var obj_2 = file_name_array[j];
			
			if(obj_1.file_name === obj_2.file_name && obj_1.label_name !== obj_2.label_name){
				found_duplicate = true;
				highlightFileLabel(obj_1.label_name, file_label_highlight_on);
				highlightFileLabel(obj_2.label_name, file_label_highlight_on);
			}
		}
	}
	
	if(found_duplicate){
		alert("Duplicate upload files detected");
		return true;
	}
	else{ return false; }
}

function showCGIOutput(message){ alert(message); }
function showDebugMessage(message){ JQ("#ubr_debug").append(message + "<br>"); }
function showAlertMessage(message){ JQ("#ubr_alert").html(message); }

function showEmbeddedUploadResults(){
	stopDataLoop();
	resetProgressBar();

	JQ("#ubr_alert").html("");
	JQ("#upload_container").show();
	JQ("#reset_button").val("Reset");
	JQ(".upfile_ultimo").remove();
	JQ(".upfile").remove();
	JQ(".upfile_label").remove();
	JQ("#upload_button").show();
	JQ("#upload_slots_container").hide();

	addUploadSlot();
}

function stopDataLoop(){
	CPB_loop = false;
	clearInterval(UP_timer);
	clearInterval(BPB_timer);

	if(cedric_progress_bar){
		if(show_current_position){ clearTimeout(CPB_byte_timer); }
		clearTimeout(CPB_status_timer);
	}
}

// Reset the progress bar
function resetProgressBar(){
	JQ("#progress_bar_container").hide();
	JQ("#upload_stats_container").hide();

	seconds = 0;
	minutes = 0;
	hours = 0;
	get_status_url = '';
	total_upload_size = 0;
	total_kbytes = 0;
	toggle_upload_stats = 0;
	CPB_loop = false;
	CPB_width = 0;
	CPB_bytes = 0;
	CPB_hold = true;
	BPB_width_inc = 0;
	BPB_width_new = 0;
	BPB_width_old = 0;

	JQ("#progress_bar").css("width", "0px");

	if(show_files_uploaded || show_current_position || show_elapsed_time || show_est_time_left || show_est_speed){
		JQ("#upload_stats_toggle").html("[+]");
		//JQ("#upload_stats_toggle").css({ backgroundImage : "url(./images/toggle.png)" });
	}

	if(show_percent_complete){ JQ("#percent_complete").html("0%"); }
	if(show_files_uploaded){ JQ("#files_uploaded").html("0"); }
	if(show_files_uploaded){ JQ("#total_uploads").html("0"); }
	if(show_current_position){ JQ("#current_position").html("0"); }
	if(show_current_position){ JQ("#total_kbytes").html("0"); }
	if(show_elapsed_time){ JQ("#elapsed_time").html("00:00:00"); }
	if(show_est_time_left){ JQ("#est_time_left").html("00:00:00"); }
	if(show_est_speed){ JQ("#est_speed").html("0"); }
}

function resetUploadDiv(){
	JQ("#upload_container").hide();
	JQ("#upload_container").html("");
}

// Initialize the file upload page
function resetFileUploadPage(){
	stopDataLoop();
	resetProgressBar();
	resetUploadDiv();

	JQ("#ubr_alert").html("");
	JQ("#reset_button").val("Reset");
	JQ(".upfile_ultimo").remove();
	JQ(".upfile").remove();
	JQ(".upfile_label").remove();
	JQ("#upload_button").show();
	JQ("#upload_slots_container").hide();

	addUploadSlot();
}

// Link the upload
function linkUpload(){
	var terms=JQ("#chkterms").attr('checked');
	//alert(terms);
	if(!terms){alert("Please check on the agreement!"); return false;}

	if(check_file_name_format || check_allow_extensions_on_client || check_disallow_extensions_on_client || check_duplicate_file_count){ clearFileLabels(); }
	if(checkFileNameFormat()){ return false; }
	if(checkAllowFileExtensions()){ return false; }
	if(checkDisallowFileExtensions()){ return false; }
	if(checkNullFileCount()){ return false; }
	if(checkDuplicateFileCount()){ return false; }

	JQ("#upload_button").hide();
	toggleUploadStats();
	if(show_files_uploaded){ JQ("#total_uploads").html(JQ('.upfile').length - 1); }

	JQ.getScript(path_to_link_script, function(){});
}

// Initialize progress bar
function initializeProgressBar(upload_id, debug_ajax){
	if(debug_ajax){ showDebugMessage("Initializing Progress Bar: " + path_to_set_progress_script + '?upload_id=' + upload_id); }

	JQ.getScript(path_to_set_progress_script + '?upload_id=' + upload_id, function(){});
}

//Submit the upload form
function startUpload(upload_id, debug_upload, debug_ajax){
	
	resetUploadDiv();

	var id = new Date().getTime();
	var iframe_name = "upload_iframe_" + id;

	if(debug_ajax){ showDebugMessage("Submitting Upload: " + path_to_upload_script + "?upload_id=" + upload_id); }

	JQ("#upload_container").html("<iframe name='"+iframe_name+"' frameborder='0' width='780' height='200' scrolling='auto'></iframe>");
	JQ("#uu_upload").attr("target", iframe_name);
	JQ("#uu_upload").attr("action", path_to_upload_script + "?upload_id=" + upload_id);
	JQ("#upload_slots_container").fadeOut("fast");
	JQ(".upfile_ultimo").fadeOut("fast");
	JQ("#uu_upload").submit();
	JQ("#reset_button").val("Stop Upload");

	if(!debug_upload){ initializeProgressBar(upload_id, debug_ajax); }
	else{ showAlertMessage("Debug Uploader Detected, Please Wait..."); }
}

// Stop the upload
function stopUpload(){
	try{ window.stop(); }
	catch(e){
		try{ document.execCommand("Stop"); }
		catch(e){}
	}

	JQ("#upload_slots_container").fadeIn("fast");
	JQ("#upload_button").show();
	JQ("#reset_button").val("Reset");
}

// Get the progress of the upload
function getProgressStatus(){
	if(CPB_loop){ JQ.getScript(get_status_url, function(){}); }
}

// Make the progress bar smooth
function smoothCedricStatus(){
	if(CPB_width < progress_bar_width && !CPB_hold){
		CPB_width++;
		JQ("#progress_bar").css("width", CPB_width + "px");
	}

	if(CPB_loop){
		clearTimeout(CPB_status_timer);
		CPB_status_timer = setTimeout("smoothCedricStatus()", CPB_time_width);
	}
}

// Make the bytes uploaded smooth
function smoothCedricBytes(){
	if(CPB_bytes < total_kbytes && !CPB_hold){
		CPB_bytes++;
		JQ("#current_position").html(CPB_bytes);
	}

	if(CPB_loop){
		clearTimeout(CPB_byte_timer);
		CPB_byte_timer = setTimeout("smoothCedricBytes()", CPB_time_bytes);
	}
}

//Start the progress bar
function startProgressBar(upload_id, upload_size, start_time){
	total_upload_size = upload_size;
	total_kbytes = Math.round(total_upload_size / 1024);
	CPB_loop = true;

	JQ("#progress_bar_container").fadeIn("fast");
	showAlertMessage("Upload In Progress");

	if(show_current_position){ JQ("#total_kbytes").html(total_kbytes + " "); }
	if(show_elapsed_time){ UP_timer = setInterval("getElapsedTime()", 1000); }

	get_status_url = path_to_get_progress_script + "?upload_id=" + upload_id + "&start_time=" + start_time + "&total_upload_size=" + total_upload_size;
	getProgressStatus();

	if(cedric_progress_bar){
		if(show_current_position){ smoothCedricBytes(); }
		smoothCedricStatus();
	}
}

// Calculate and display upload information
function setProgressStatus(total_bytes_read, files_uploaded, current_file, bytes_read, lapsed_time){
	var byte_speed = 0;
	var time_remaining = 0;

	if(lapsed_time > 0){ byte_speed = total_bytes_read / lapsed_time; }
	if(byte_speed > 0){ time_remaining = Math.round((total_upload_size - total_bytes_read) / byte_speed); }

	if(cedric_progress_bar == 1){
		if(byte_speed !== 0){
			var temp_CPB_time_width = Math.round(total_upload_size * 1000 / (byte_speed * progress_bar_width));
			var temp_CPB_time_bytes = Math.round(1024000 / byte_speed);

			if(temp_CPB_time_width < 5001){ CPB_time_width = temp_CPB_time_width; }
			if(temp_CPB_time_bytes < 5001){ CPB_time_bytes = temp_CPB_time_bytes; }
		}
		else{
			CPB_time_width = 500;
			CPB_time_bytes = 15;
		}
	}

	// Calculate percent_complete finished
	var percent_complete = Math.floor(100 * parseInt(total_bytes_read, 10) / parseInt(total_upload_size, 10));
	var progress_bar_status = Math.floor(progress_bar_width * (parseInt(total_bytes_read, 10) / parseInt(total_upload_size, 10)));

	// Calculate time remaining
	var remaining_sec = (time_remaining % 60);
	var remaining_min = (((time_remaining - remaining_sec) % 3600) / 60);
	var remaining_hours = ((((time_remaining - remaining_sec) - (remaining_min * 60)) % 86400) / 3600);

	if(remaining_sec < 10){ remaining_sec = "0" + remaining_sec; }
	if(remaining_min < 10){ remaining_min = "0" + remaining_min; }
	if(remaining_hours < 10){ remaining_hours = "0" + remaining_hours; }

	var est_time_left = remaining_hours + ":" + remaining_min + ":" + remaining_sec;
	var est_speed = Math.round(byte_speed / 1024);
	var current_position = Math.round(total_bytes_read / 1024);

	if(cedric_progress_bar == 1){
		if(cedric_hold_to_sync){
			if(progress_bar_status < CPB_width){ CPB_hold = true; }
			else{
				CPB_hold = false;
				CPB_width = progress_bar_status;
				CPB_bytes = current_position;
			}
		}
		else{
			CPB_hold = false;
			CPB_width = progress_bar_status;
			CPB_bytes = current_position;
		}

		JQ("#progress_bar").css("width", progress_bar_status + "px");
	}
	else if(bucket_progress_bar == 1){
		BPB_width_old = BPB_width_new;
		BPB_width_new = progress_bar_status;

		if((BPB_width_inc < BPB_width_old) && (BPB_width_new > BPB_width_old)){ BPB_width_inc = BPB_width_old; }

		clearInterval(BPB_timer);
		BPB_timer = setInterval("incrementProgressBar()", 10);
	}
	else{ JQ("#progress_bar").css("width", progress_bar_status + "px"); }

	if(show_current_position){ JQ("#current_position").html(current_position); }
	if(show_current_file){ JQ("#current_file").html(current_file); }
	if(show_percent_complete){ JQ("#percent_complete").html(percent_complete + "%"); }
	if(show_files_uploaded){ if(files_uploaded > 0){ JQ("#files_uploaded").html(files_uploaded); } }
	if(show_est_time_left){ JQ("#est_time_left").html(est_time_left); }
	if(show_est_speed){ JQ("#est_speed").html(est_speed); }
}

function incrementProgressBar(){
	if(BPB_width_inc < BPB_width_new){
		BPB_width_inc++;
		JQ("#progress_bar").css("width", BPB_width_inc + "px");
	}
}

// Calculate the time spent uploading
function getElapsedTime(){
	seconds++;

	if(seconds == 60){
		seconds = 0;
		minutes++;
	}

	if(minutes == 60){
		minutes = 0;
		hours++;
	}

	var hr = "" + ((hours < 10) ? "0" : "") + hours;
	var min = "" + ((minutes < 10) ? "0" : "") + minutes;
	var sec = "" + ((seconds < 10) ? "0" : "") + seconds;

	JQ("#elapsed_time").html(hr + ":" + min + ":" + sec);
}

// Add one upload slot
function addUploadSlot(){
	if(JQ(".upfile_ultimo").val() != ""){
		if(JQ(".upfile").length < max_upload_slots + 1){
			if(JQ(".upfile").length > 0) {
				JQ(".upfile_ultimo").hide();
				JQ("#upload_slots_container").show();
				JQ("#upload_slots_container").append('<div class="upfile_label" id="' + JQ(".upfile_ultimo").attr("id") +'_label"><span class="upfile_name">' + getFileName(JQ(".upfile_ultimo").val()) + '</span><span class="upfile_remove" title="Remove File" onClick="deleteUploadSlot(\'' + JQ(".upfile_ultimo").attr("id") + '\')">[x]</span></div>');
				//JQ("#upload_slots_container").append('<div class="upfile_label" id="' + JQ(".upfile_ultimo").attr("id") +'_label"><span class="upfile_name">' + getFileName(JQ(".upfile_ultimo").val()) + '</span><span class="upfile_remove" title="Remove File" onClick="deleteUploadSlot(\'' + JQ(".upfile_ultimo").attr("id") + '\')"></span></div>');
			}

			var id = new Date().getTime();

			JQ(".upfile_ultimo").removeClass("upfile_ultimo");
			JQ("#file_picker_container").prepend('<input type="file" class="upfile upfile_ultimo" name="upfile_' + id + '" id="upfile_' + id + '" size="35" value="">');
			JQ("#upfile_" + id).bind("keypress", function(e){ if(e == 13){ return false; } });
			JQ("#upfile_" + id).bind("change", function(e){ addUploadSlot(); });

			if(JQ(".upfile").length > max_upload_slots){ JQ(".upfile_ultimo").fadeOut("fast"); }
		}
	}
}

function deleteUploadSlot(id){
	JQ("#"+id).remove();
	JQ("#"+id+'_label').remove();

	if(JQ(".upfile").length <= max_upload_slots){ JQ(".upfile_ultimo").fadeIn("fast"); }
	if(JQ(".upfile").length == 1){ JQ("#upload_slots_container").hide(); }
}

function toggleUploadStats(){
	if(toggle_upload_stats){
		if(show_files_uploaded || show_current_position || show_elapsed_time || show_est_time_left || show_est_speed){
			JQ("#upload_stats_toggle").html("[+]");
			//JQ("#upload_stats_toggle").css({ backgroundImage : "url(./images/toggle.png)" });
		}

		JQ("#upload_stats_container").slideUp("fast");
		toggle_upload_stats = 0;
	}
	else{
		if(show_files_uploaded || show_current_position || show_elapsed_time || show_est_time_left || show_est_speed){
			JQ("#upload_stats_toggle").html("[-]");
			//JQ("#upload_stats_toggle").css({ backgroundImage : "url(./images/toggle_collapse.png)" });
		}

		JQ("#upload_stats_container").slideDown("fast");
		toggle_upload_stats = 1;
	}
}
