
var zip
var i=0
var urls = []

//the list of files below is generated on the server
//see default/asp/sampleform16.resx
[filelist]

$("#blob").on("click", function () {

	zip = new JSZip()

	$(this).html('please wait... files are downloading')
	$('#blob').attr({"class":"btn btn-danger"})
	
	getUrl(urls[i][0],urls[i][1])	
		
});

function download() {	
	
	$('#blob').html('please wait... files are zipped')
	$('#blob').attr({"class":"btn btn-warning"})

	i=0

	zip.generateAsync({type:"blob"}).then(function (blob) { 
		saveAs(blob, "download.zip")
		
		$('#blob').attr({"class":"btn btn-success"})
		$('#blob').html('ready!')	
		
	}, function (err) {})
}

function getUrl(filename,url) {	
	
	$.ajax({
		url: url,
		type:'GET',
		cache:false,
		xhr:function(){		
				
			var xhr = new XMLHttpRequest();
			
			if (filename==url) {xhr.responseType= 'blob'}
			else {xhr.responseType= 'text'}
				
			return xhr;		
				
		},
		success: function(data){

			if (filename==url) {zip.file(filename, data,{binary: true})}
			else{zip.file(filename, data)}
			
			i++			
			
			if (i==urls.length) {download()}
			else {getUrl(urls[i][0],urls[i][1])}
				
		},
		error:function(){	

			console.log('err: ' + filename)
			
			i++
			
			if (i==urls.length) {download()}
			else {getUrl(urls[i][0],urls[i][1])}				
		}
	})
}