
//////////////////////////////////////////////////////////
// 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. 
//////////////////////////////////////////////////////////
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("Üzgünüz, bu formatta dosya gönderemezsiniz.\n\n Dosya isminde Türkçe karakter olmamasına . \n\n32 Karakterden daha uzun olmamasına ve\nDosya_adi.uzn şeklinde				isimlendirilmiş olmasına dikkat ediniz.\n Dosya isminde şu karakterler olabilir 1-9, a-z, A-Z, '_', '-'\n");
  				return true;
  			}
  		}
  	}
	return false;
}
  
///////////////////////////////////////////////////////////////////////////////////////
// Stop upload if user is attempting to upload a file with a disallowed file extention
///////////////////////////////////////////////////////////////////////////////////////
function checkDisallowFileExtentions(){
	if(check_disallow_extensions == false){ return false; }
  	
  	//File extentions that will not upload
  	var re = /(\.sh)|(\.php)|(\.php3)|(\.php4)|(\.php5)|(\.py)|(\.shtml)|(\.phtml)|(\.cgi)|(\.pl)|(\.htaccess)|(\.htpasswd)$/i;
  	
  	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('Uzantısı "' + file_extention + '" olan dosyalar kabul edilmemektedir.');
  				return true;
  			}
  		}
  	}
	return false;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Stop upload if user is attempting to upload a file that does not have an allowed file extention
///////////////////////////////////////////////////////////////////////////////////////////////////
function checkAllowFileExtentions(){
	if(check_allow_extentions == false){ return false; }
	
  	//File extentions that will upload
  	var re = /(\.jpg)|(\.jpeg)|(\.gif)|(\.bmp)$/i;
  	
  	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('Uzantısı "' + file_extention + '" olan dosyalar kabul edilmemektedir.');
  				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("Lütfen yüklemek için bir dosya seçiniz.");
		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 = "Aynı dosya birden çok kez eklenmiş.\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; }
}

//////////////////////
// Count file uploads
//////////////////////
function countUploads(){
	var total_uploads = 0;
	
	for(var i = 0; i < upload_range; i++){
		if(document.form_upload.elements['upfile_' + i].value != ""){ total_uploads++; }
	}
	
	return total_uploads;
}
  
//////////////////////////////////////////////////////
// Check files, submit upload and pop up progress bar
//////////////////////////////////////////////////////
function uploadFiles(pr_link){
	if(checkFileNameFormat()){ return false; }
	if(checkDisallowFileExtentions()){ return false; }
	if(checkAllowFileExtentions()){ return false; }
	if(checkNullFileCount()){ return false; }
	if(checkDuplicateFileCount()){ return false; }
	
	// If opera browser force pop-up mode
	if(window.opera){ imbedded_progress_bar = false; }

	if(imbedded_progress_bar){ document.form_upload.imbedded_progress_bar.value = 1; }
	else{ document.form_upload.imbedded_progress_bar.value = 0; }
	
	var total_uploads = countUploads();
	
	document.form_upload.upload_range.value = upload_range;
	document.form_upload.submit();
	document.progress_bar.upload_button.disabled = true;
	
	for(var i = 0; i < upload_range; i++){ document.form_upload.elements['upfile_' + i].disabled = true;
		document.form_upload.elements['neyapilmali' + i].disabled = true; }
		
	
		var progress_link = pr_link + total_uploads;
		
		document.getElementById('progress_div').innerHTML = "<iframe name='progress_iframe' background-color='#EEEEEE' src='" + progress_link + "'frameborder='0' scrolling='no' width='500' height='260'><\/iframe>";
		document.getElementById('logs').innerHTML = "";
		// document.getElementById('upload_slots').innerHTML = "";
		//We've opened the progress bar in an iframe so we return false to prevent the progress form from posting
		return false;
}
 
//////////////////////////////
// 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.form_upload.elements['neyapilmali' + i].disabled = false;
		document.form_upload.elements['neyapilmali' + i].value = "";  
	}
	
	document.progress_bar.upload_button.disabled = false;
	document.getElementById('progress_div').innerHTML = "";
	document.form_upload.reset();
	stop_upload("");
}

////////////////////////////
// Stop the current upload
////////////////////////////
function stop_upload(msg){
	if(imbedded_progress_bar){ document.getElementById('progress_div').innerHTML = msg; }
	
	try{ document.execCommand('Stop'); }
	catch(e){ window.stop(); }  
}

////////////////////////////
// Add another upload slot
////////////////////////////
function addUploadSlot(num){
	if(upload_range < max_upload_slots){
		if(num == upload_range){
			var up = document.getElementById('upload_slots');
			var dv = document.createElement("div");
			dv.innerHTML = '<table width="90%" style="padding-left: 16px; vertical-align: top;" border="0" cellspacing="5" cellpadding="3"><tr><td>Dosya '+ upload_range +':<\/td><td><input type="file" name="upfile_' + upload_range + '" size="30" onchange="addUploadSlot('+(upload_range + 1)+')"><\/td><\/tr><tr><td><label>Açıklama '+ upload_range +':<\/label><\/td><td><input name="neyapilmali' + upload_range + '" type="text" size="40" /><\/td><\/tr><\/table>';
			up.appendChild(dv);
			upload_range++;
			document.form_upload.upload_range.value = upload_range;
		}
	}
}

