var check_file_extentions = true;                         // Change to false to skip file extention check
var check_null_file_count = true;                         // Change to false to skip null file upload
var check_duplicate_file_count = true;                    // Change to false to skip duplicate file upload check
var imbedded_progress_bar = true;                         // Change to false to use pop-up imbedded progress bar.
var upload_range = 1;
  
//////////////////////////////////////////////////////////////////////
// Disallow uploading files by extention (DO NOT REMOVE .sh OR .php)
//
// If you want prevent users from uploading a file based on extention
// simply modify the regular exression eg.
// var re = /(\.php)|(\.sh)|(\.gif)|(\.jpg)$/i; 
// would prevent anyone from uploading .php, .sh, .gif, .jpg files.
//////////////////////////////////////////////////////////////////////
function checkFileExtentions(){
	if(check_file_extentions == false){ return false; }
  	
  	var re = /(\.sh)|(\.php)|(\.php3)|(\.php4)|(\.shtml)|(\.cgi)|(\.pl)$/i;   //Change in uploader.cgi as well
  	
  	for(var i = 0; i < upload_range; i++){
  		if(document.form_upload.elements['upfile_' + i].value != ""){
  			if(document.form_upload.elements['upfile_' + i].value.match(re)){
  				var string = document.form_upload.elements['upfile_' + i].value;
				var num_of_last_slash = string.lastIndexOf("\\");

				if(num_of_last_slash < 1){ num_of_last_slash = string.lastIndexOf("/"); }

				var file_name = string.slice(num_of_last_slash + 1, string.length);
				var file_extention = file_name.slice(file_name.indexOf(".")).toLowerCase(); 
  				
  				alert('Der Upload einer Datei mit dieser Dateierweiterung "' + file_extention + '" ist nicht erlaubt.');
  				return true;
  			}
  		}
  	}
	return false;
}
  
///////////////////////////////////////////////////////
// Make sure user selected at least one file to upload
///////////////////////////////////////////////////////
function checkNullFileCount(){
  	if(check_null_file_count == false){ return false; }
  	
  	var null_file_count = 0;
  	
  	for(var i = 0; i < upload_range; i++){
  		if(document.form_upload.elements['upfile_' + i].value == ""){ null_file_count++; }
  	}
  	
  	if(null_file_count == upload_range){
		alert("Bitte Datei auswählen.");
		return true;
  	}
  	else{ return false; }
}
  
////////////////////////////////////////////////////////
// Make sure user did not select duplicate file uploads
////////////////////////////////////////////////////////
function checkDuplicateFileCount(){
	if(check_duplicate_file_count == false){ return false; }
  	
	var duplicate_flag = false;
	var file_count = 0;
	var duplicate_msg = "Duplicate Upload Files Detected.\n\n";
	var file_name_array = new Array();
        
	for(var i = 0; i < upload_range; i++){
		if(document.form_upload.elements['upfile_' + i].value != ""){
  			var string = document.form_upload.elements['upfile_' + i].value;
			var num_of_last_slash = string.lastIndexOf("\\");

			if(num_of_last_slash < 1){ num_of_last_slash = string.lastIndexOf("/"); }

			var file_name = string.slice(num_of_last_slash + 1, string.length);
			            
			file_name_array[i] = file_name;
  		}
  	}
       
	for(var i = 0; i < file_name_array.length; i++){
		for(var j = 0; j < file_name_array.length; j++){
			if(file_name_array[i] == file_name_array[j] && file_name_array[i] != null){ file_count++; }
		}
		if(file_count > 1){
			duplicate_msg += 'Duplicate file "' + file_name_array[i] + '" detected in slot ' + (i + 1) + ".\n";
			duplicate_flag = true;
		}
		file_count = 0;
	}
       
	if(duplicate_flag){ 
		alert(duplicate_msg);
		return true; 
	}
	else{ return false; }
}
  
////////////////////////////////////////
// User has selected a new upload range
////////////////////////////////////////
function submitRefreshForm(){
	var r_index = document.form_upload.upload_range.selectedIndex;
	
	document.form_refresh.upload_range.value = document.form_upload.upload_range.options[r_index].value
	document.form_refresh.submit();
	
	return true;
}

//////////////////////////////
// Reset the form_upload form
//////////////////////////////
function resetForm(){
	for(var i = 0; i < upload_range; i++){ 
		document.form_upload.elements['upfile_' + i].disabled = false;
		document.form_upload.elements['upfile_' + i].value = ""; 
	}
	
	document.progress_bar.upload_button.disabled = false;
	document.form_upload.upload_range.disabled = false;
	document.getElementById('progress_div').innerHTML = "";
	document.form_upload.reset()
}

////////////////////////////
// Stop the current upload
////////////////////////////
function stop_upload(msg){
	if(imbedded_progress_bar){ document.getElementById('progress_div').innerHTML = "<br>" + msg + "<br>"; }
	
	try{ document.execCommand('Stop'); }
	catch(e){ window.stop(); }  
}