	
	function SaveToDisk(fileURL, fileName) {
    // for non-IE
	    var ms_ie = false;
        var ua = window.navigator.userAgent;
        var old_ie = ua.indexOf('MSIE ');
        var new_ie = ua.indexOf('Trident/');

    if ((old_ie > -1) || (new_ie > -1)) {
        ms_ie = true;
    }

    if ( !ms_ie ) {
     
	 
        var save = document.createElement('a');
        save.href = fileURL;
        save.target = '_blank';
        save.download = fileName || 'unknown';

        var event = document.createEvent('Event');
        event.initEvent('click', true, true);
        save.dispatchEvent(event);
        (window.URL || window.webkitURL).revokeObjectURL(save.href);
		
    }

    
    else 
	{
	
        var _window = window.open(fileURL, '_blank');
        _window.document.close();
        _window.document.execCommand('SaveAs', true, fileName || fileURL)
        _window.close();
    }
}

		
		function nlp_process(nlp_process_object)					
		{   if (nlp_process_object.service=='download')
		    {  nlp_process_object.onstart('/rs/downloadText/'+nlp_process_object.fileID);
			   return;
		    }
			
   		    nlp_process_object.onstart(nlp_process_object.service);
		  	nlp_process_object.starttime = new Date().getTime();
			try 
			{
			 nlp_process_object.taskID=nlp_call('/rs/'+nlp_process_object.service+'/addTask/'+nlp_process_object.fileID);
			
					
			nlp_process_object.check = function(){
			
			 result=nlp_call('/rs/'+nlp_process_object.service+'/checkTask/'+nlp_process_object.taskID);  
             if (result.finished){
			     nlp_process_object.onfinished(nlp_process_object.service);
				 if (nlp_process_object.next)
				 {  nlp_process_object.next.fileID=result.result;
				    nlp_process(nlp_process_object.next);
				}
				return ;
              }
            else {
			  if (result.timetofinish<0)
			     { $('#nlp_log').html( "Brak zadania na serwerze! Plik:"+nlp_process_object.taskID);
				   return;
				  }
			  else
			  {  process_time=new Date().getTime();
			     process_time=(process_time-nlp_process_object.starttime)/1000 ;
				 
			     progress=process_time/((result.timetofinish)+process_time)*100;
				 nlp_process_object.onupdated(nlp_process_object.service,progress,result.timetofinish);
				 timeout=Math.max(Math.min(5,result.timetofinish/100),0.1);
				 console.log("T:"+timeout)
				 setTimeout(nlp_process_object.check, timeout*1000); 
				 return;
			  }
            }
            }

              setTimeout(nlp_process_object.check, 1000); 
		  
			}
			catch(e)
			{ 
			}
			
        }		
		
        
		 
		 function nlp_call(what)
		 {  var nlp_cresult="NIC";
		        $.ajax({type:"GET",url:what,
				         processData: false,
						 cache: false,
						 contentType: false,
						 
						 success: function(res) {nlp_cresult=res;},
						 error:function(jqXHR, exception) {nlp_error(jqXHR,exception);return;},
						 
						 async:false
						 });
			return nlp_cresult;		 
						 
		 }
		 
		 function nlp_error(jqXHR, exception) {
            if (jqXHR.status === 0) {
                alert('Not connect.\n Verify Network.');
            } else if (jqXHR.status == 404) {
                alert('Requested page not found. [404]');
            } else if (jqXHR.status == 500) {
                alert('Internal Server Error [500].');
            } else if (exception === 'parsererror') {
                alert('Requested JSON parse failed.');
            } else if (exception === 'timeout') {
                alert('Time out error.');
            } else if (exception === 'abort') {
               alert('Ajax request aborted.');
            } else {
                alert('Uncaught Error.\n' + jqXHR.responseText);
            }
			throw "Ajax error";
        }
	   
	 function nlp_upload(formData,nlp_process_object)
	 {
	       var url = "/rs/fileUpload"; 

    $.ajax({
           type: "POST",
           url: url,
           data: formData, // serializes the form's elements.
		   cache: false,
           contentType: false,
           processData: false,
           success: function(file_id)
           {   $('#load_div').hide();
               //file_id=nlp_process1("maca",file_id);
			  
			   nlp_process_object.fileID=file_id;
			   nlp_process(nlp_process_object);
			   //file_id=nlp_process("tager",file_id);
           },
		   error: function(jqXHR, exception) {
            if (jqXHR.status === 0) {
                alert('Not connect.\n Verify Network.');
            } else if (jqXHR.status == 404) {
                alert('Requested page not found. [404]');
            } else if (jqXHR.status == 500) {
                alert('Internal Server Error [500].');
            } else if (exception === 'parsererror') {
                alert('Requested JSON parse failed.');
            } else if (exception === 'timeout') {
                alert('Time out error.');
            } else if (exception === 'abort') {
               alert('Ajax request aborted.');
            } else {
                alert('Uncaught Error.\n' + jqXHR.responseText);
            }
        }
         });
		

    return false; // avoid to execute the actual submit of the form.
};
