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 check_file_name_format = false;                        // Change to false to skip file name format check
var imbedded_progress_bar = false;                        // Change to false to use pop-up imbedded progress bar.
var upload_range = 1;

///////////////////////////////////////////////////////
// Make sure does not upload files with bizarre names.
//
// You may choose to skip this check and let the 
// uploader simply 'normalize' the file name. 
///////////////////////////////////////////////////////
function checkFileNameFormat(){
	if(check_file_name_format == false){ return false; }
	
	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);
			var re = /^[\w][\w\.\-]{1,32}$/i;   
				
			if(!re.test(file_name)){	
  				alert("Dieser Dateityp ist für den Upload nicht freigegeben. Bitte verwenden Sie keine Sonderzeichen im Dateinamen und laden Sie nur JPG - Dateien auf den Server. \n");
  				return true;
  			}
  		}
  	}
	return false;
}
  
//////////////////////////////////////////////////////////////////////
// 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 = /(\.jpg)$/i;//(\.sh)|(\.php)|(\.php3)|(\.php4)|(\.shtml)|(\.cgi)|(\.exe)|(\.gif)|(\.png)|(\.bmp)|(\.pl)$/i;   //Change in uber_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('Das hochlanden einer Datei mit dieser Endung "' + 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 wählen Sie eine Datei aus.");
		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; }
}
  
//////////////////////////////////////////////////////
// Check files, submit upload and pop up progress bar
//////////////////////////////////////////////////////
function uploadFiles(){
	if(checkFileExtentions()){ return false; }
	if(checkNullFileCount()){ return false; }
	if(checkDuplicateFileCount()){ return false; }
	if(checkFileNameFormat()){ return false; }
	
	if(window.opera){ 
		imbedded_progress_bar = false;
		document.form_upload.imbedded_progress_bar.value = 0; 
	}

	if(imbedded_progress_bar){ 
  document.form_upload.imbedded_progress_bar.value = 1; }
	else{ document.form_upload.imbedded_progress_bar.value = 0; }
	
	document.form_upload.submit();
	document.progress_bar.upload_button.disabled = true;
	document.form_upload.upload_range.disabled = true;
	
	for(var i = 0; i < upload_range; i++){
		document.form_upload.elements['upfile_' + i].disabled = true;
	}
		
	if(imbedded_progress_bar){
		var progress_link = "../uber_uploader_progress.php?tmp_sid=<? print $k_id; ?>";
		
		document.getElementById('progress_div').innerHTML = "<iframe name='progress_iframe' background-color='#EEEEEE' src='" + progress_link + "'frameborder='0' scrolling='no' width='500' height='260'></iframe>";
		
		//We've opened the progress bar in an iframe so we return false to prevent the progress form from posting
		return false;
	}
	else{	
		var upWin = window.open('','uploadWin','toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=no,resizable=no,width=350,height=150,left=100,top=100');
		
		if(window.focus){ upWin.focus(); }
		
		return true;
	}
}
  
////////////////////////////////////////
// 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(); }  
}
