//////////////////////////////////////////////////////////
// Make sure file names do not contain illegal characters.
//
// You may choose to skip this check and let the 
// uploader simply 'normalize' the file name. 
//////////////////////////////////////////////////////////
var check_file_extensions = true;                         // Change to false to skip file extension 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 = true;                         // Change to false to use pop-up imbedded progress bar.

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("Sorry, uploading files in this format is not allowed. Please ensure your file names follow this format. \n\n1. Entire file cannot exceed 32 characters\n2. Format should be filename.extention or filename\n3. Legal characters are 1-9, a-z, A-Z, '_', '-'\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_extensions == false){ return false; }
  	
  	var re = /(\.sh|\.php|\.php3|\.php4|\.shtml|\.cgi|\.pl)$/i; // /(\.sh)|(\.php)|(\.php3)|(\.php4)|(\.shtml)|(\.cgi)|(\.pl)$/i;   //Change in uber_uploader.pl 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('Sorry, uploading a file with the extention "' + file_extention + '" is not allowed. Please archive them first.');
  				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("Please Choose A File To Upload.");
		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 = serverAddrHost+"/cgi-bin/uber_uploader_progress.pl?tmp_sid="+tmpsidHead+"&config_file="+configfileHead;
		
		document.getElementById('progress_div').innerHTML = "<iframe name='progress_iframe' allowtransparency='true' src='" + progress_link + "'frameborder='0' scrolling='no' width='500' height='220' style='height:220px; background-color:#E3F3FA; float:left;'></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=yes,resizable=yes,width=420,height=300,left=360,top=250');
		
		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(); }  
}
