document.observe("dom:loaded", function() {
	/* example 1 */
	var button = $('btn_file_browse'), interval;
	new Ajax_upload(button,{
		action: '/quote_upload.php', // I disabled uploads in this example for security reasons
		name: 'myfile',
		onSubmit : function(file, ext) {
			// change button text, when user selects file			
//			button.update('Uploading');
			document.getElementById('quotes_status').value = 'Uploading';
			// If you want to allow uploading only 1 file at time,
			// you can disable upload button
			this.disable();
			
			// Animating upload button
			// Uploding -> Uploading. -> Uploading...
			interval = window.setInterval(function(){
//				var text = button.innerHTML;
				var text = document.getElementById('quotes_status').value;
				if (text.length < 13){
					document.getElementById('quotes_status').value = text + '.';
//					button.update(text + '.');					
				} else {
					document.getElementById('quotes_status').value = 'Uploading';
//					button.update('Uploading');				
				}
			}, 200);
		},
		onComplete: function(file, response){
			//console.log(response);
			document.getElementById('quotes_status').value = 'Attach File';
//			button.update('Attach File');
			window.clearInterval(interval);
			
			// add file to the list
			if (getTagId() > -1) {
//				$$('#quotes_files_'+getTagId())[0].show();
				document.getElementById('quotes_files_'+getTagId()).style.display = "";
//				$$('#quotes_files_'+getTagId()+' .files')[0].update(Element('li').update(file));
				document.getElementById('file['+getTagId()+']').value = file;
			} else {
				alert('Maximum permitted number of files is reached');
			}

			// enable upload button
			this.enable();
//			if (isTagFree()) {
//				this.enable();
//			} else {
//				this.disable();
//			}
		}
	});

});

function isTagFree() {
	for (i=0; i<10; i++) {
		if (document.getElementById('file['+i+']').value == "") {
			return true;
		}
	}
	return false;
}
function getTagId() {
	for (i=0; i<10; i++) {
		if (document.getElementById('file['+i+']').value == "") {
			return i;
		}
	}
	return -1;
}
