function sendFileToServer(formData,status)
{
    //var uploadURL ="system/ajax.php?wgmfile=1&g=<?php echo $_GET[g];?>"; //Upload URL
    var extraData ={}; //Extra Data.
	if(uploadURL=='import') {
	 uploadURL ="system/ajax.php?importfile=1&g="+group+"&type="+$('input[name=update_time]:checked', '#importform').val();
	}
	
	
    var jqXHR=$.ajax({
            xhr: function() {
            var xhrobj = $.ajaxSettings.xhr();
            if (xhrobj.upload) {
                    xhrobj.upload.addEventListener('progress', function(event) {
                        var percent = 0;
                        var position = event.loaded || event.position;
                        var total = event.total;
                        if (event.lengthComputable) {
                            percent = Math.ceil(position / total * 100);
                        }
                        //Set progress
                        status.setProgress(percent);
                    }, false);
                }
            return xhrobj;
        },
    url: uploadURL,
    type: "POST",
    contentType:false,
    processData: false,
        cache: false,
        data: formData,
        success: function(data){
            status.setProgress(100);
 
            $("#kimenet").append(data);         
        }
    }); 
 
    status.setAbort(jqXHR);
}
 
var rowCount=0;
function createStatusbar(obj)
{
     rowCount++;
     var row="odd";
     if(rowCount %2 ==0) row ="even";
     this.statusbar = $("<div class='statusbar "+row+"'></div>");
     this.filename = $("<div class='filename'></div>").appendTo(this.statusbar);
     this.size = $("<div class='filesize'></div>").appendTo(this.statusbar);
     this.progressBar = $("<div class='progressBar'><div></div></div>").appendTo(this.statusbar);
     this.abort = $("<div class='abort'><img src='static/img/icon-table-delete.svg'></div>").appendTo(this.statusbar);
     obj.after(this.statusbar);
 
    this.setFileNameSize = function(name,size)
    {
        var sizeStr="";
        var sizeKB = size/1024;
        if(parseInt(sizeKB) > 1024)
        {
            var sizeMB = sizeKB/1024;
            sizeStr = sizeMB.toFixed(2)+" MB";
        }
        else
        {
            sizeStr = sizeKB.toFixed(2)+" KB";
        }
 
        this.filename.html(name);
        this.size.html(sizeStr);
    }
    this.setProgress = function(progress)
    {       
        var progressBarWidth =progress*this.progressBar.width()/ 100;  
        this.progressBar.find('div').animate({ width: progressBarWidth }, 10).html(progress + "% ");
        if(parseInt(progress) >= 100)
        {
            this.abort.hide();
			$("#dragandrophandler").show();
        }
    }
    this.setAbort = function(jqxhr)
    {
        var sb = this.statusbar;
        this.abort.click(function()
        {
            jqxhr.abort();
            sb.hide();
			$("#dragandrophandler").show();
        });
    }
}
function handleFileUpload(files,obj)
{
   for (var i = 0; i < files.length; i++) 
   {
        var fd = new FormData();
        fd.append('file', files[i]);
 
        var status = new createStatusbar(obj); //Using this we can set progress.
        status.setFileNameSize(files[i].name,files[i].size);
		
        sendFileToServer(fd,status);
 
   }
}
$(document).ready(function() {
	var obj = $("#dragandrophandler");
	var kijelzosobj = $("#dragandrophandler");
	obj.on('dragenter', function (e) {
		e.stopPropagation();
		e.preventDefault();
		$(this).addClass('dragenter');
	});
	
	obj.on('dragover', function (e) {
		e.stopPropagation();
		e.preventDefault();
	});
	
	
	obj.on('drop', function (e) {
		
		$("#toomuch, #toobig").hide();
			
		e.preventDefault();
		var files = e.originalEvent.dataTransfer.files;
		
		if(files.length == 1) {
			var max_filesize = $(this).attr("data-filesize");

			if(max_filesize > files[0].size) {
				$(this).hide();
				handleFileUpload(files,kijelzosobj);
			} else {
				$("#toobig").show();
				$(this).removeClass('dragenter');
			}
		} else {
			$("#toomuch").show();
			$(this).removeClass('dragenter');
		}
		
	});

	$(document).on('dragenter', function (e){
		e.stopPropagation();
		e.preventDefault();
	});
	
	$(document).on('dragover', function (e) {
	  e.stopPropagation();
	  e.preventDefault();
	  $(this).addClass('dragenter');
	});
	
	$(document).on('drop', function (e) {
		e.stopPropagation();
		e.preventDefault();
	});
	 
	 
}); //document.ready
