function uploadDone() {
	try {
		document.submitform.submit();
	} catch (ex) {
		alert("Error submitting form: " + ex);
	}
}

function flash_upload(upload_url, base_url, browse_text) {
	
	var sc = '<div id="swfupload-control">'+
				'<input type="button" id="button" />'+
				'<p id="queuestatus" ></p>'+
				'<ol id="log" class="roundborder"></ol>'+
			'</div>';
	
	$('#upload_container').append(sc);

	
	$('#swfupload-control').swfupload({
		upload_url: upload_url,
		file_post_name: 'uploadfile',
		file_size_limit : "51200",
		file_types : "*.zip;*.pdf;*.eps;*.tif;*.tiff;*.psd;*.indd;*.doc;*.swf;*.rar;*.txt;*.ai;*.gif;*.png;*.jpg",
		file_types_description : "Allowed files",
		file_upload_limit : 5,
		flash_url : base_url + "static/js/swfupload/swfupload.swf",
		button_image_url : base_url + "static/js/swfupload/SmallSpyGlassWithTransperancy_17x18.png",
		button_width: 180,
		button_height: 18,
		button_text : '<span class="button">'+ browse_text +'</span>',
		button_text_style : '.button { font-family: Verdana, Arial, sans-serif; font-size: 12pt; } .buttonSmall { font-size: 12pt; }',
		button_text_top_padding: 0,
		button_text_left_padding: 18,
		button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
		button_cursor: SWFUpload.CURSOR.HAND,
		button_placeholder : $('#button')[0],
		debug: false
		})
		.bind('fileQueued', function(event, file){
			var listitem='<li id="'+file.id+'" class="roundborder">'+
				'File: <em>'+file.name+'</em> ('+Math.round(file.size/1024)+' KB) <span class="progressvalue" ></span>'+
				'<div class="progressbar" ><div class="progress" ></div></div>'+
				'<p class="status" >Pending</p>'+
				'<span class="cancel" >&nbsp;</span>'+
				'</li>';
			$('#log').append(listitem);
			$('li#'+file.id+' .cancel').bind('click', function(){
				var swfu = $.swfupload.getInstance('#swfupload-control');
				swfu.cancelUpload(file.id);
				$('li#'+file.id).slideUp('fast');
			});
			// start the upload since it's queued
			//$(this).swfupload('startUpload');
		})
		.bind('fileQueueError', function(event, file, errorCode, message){
			alert('Size of the file '+file.name+' is greater than limit');
		})
		/*.bind('fileDialogComplete', function(event, numFilesSelected, numFilesQueued){
			$('#queuestatus').text('Files Selected: '+numFilesSelected+' / Queued Files: '+numFilesQueued);
		})*/
		.bind('uploadStart', function(event, file){
			$('#log li#'+file.id).prependTo($("#log"));
			//$('#log li#'+file.id).insertBefore($("#log:first-child"));
			$('#log li#'+file.id).find('p.status').text('Uploading...');
			$('#log li#'+file.id).find('span.progressvalue').text('0%');
			$('#log li#'+file.id).find('span.cancel').hide();
		})
		.bind('uploadProgress', function(event, file, bytesLoaded){
			//Show Progress
			var percentage=Math.round((bytesLoaded/file.size)*100);
			$('#log li#'+file.id).find('div.progress').css('width', percentage+'%');
			$('#log li#'+file.id).find('span.progressvalue').text(percentage+'%');
		})
		.bind('uploadSuccess', function(event, file, serverData){
			var item=$('#log li#'+file.id);
			item.find('div.progress').css('width', '100%');
			item.find('span.progressvalue').text('100%');
	
			try {
				var data = JSON.parse(serverData);
				
				if(data.error == "") {
					item.addClass('success').find('p.status').text('Complete');
					var inputhidden = '<input type="hidden" name="file[]" value="'+data.data+'">';
					$("#submitform").append(inputhidden);
				}
				else {
					alert(data.error);
				}
			} catch(err) {
				alert(err);
			}
	
		})
		.bind('uploadComplete', function(event, file){
			var stats = $.swfupload.getInstance(this).getStats();
			
			if (stats.files_queued == 0) {
				uploadDone();
			} else {
				// try the next one in the queue
				$(this).swfupload('startUpload');
			}
		})
		.bind('uploadError', function(event, file, errorcode, msg) {
			if(errorcode != -280) {
				var item=$('#log li#'+file.id);
				item.addClass('error').find('p.status').text('Error: ' + msg);
			}
		})
	
	$("#button").click(function(){
		//alert("submit");
		$(this).attr('disabled', 'disabled');
		$('#swfupload-control').swfupload('startUpload');
		return false;
	});
}

function ajax_upload(upload_url, base_url, browse_text) {

	
	var imageurl = base_url + "static/js/swfupload/SmallSpyGlassWithTransperancy_17x18.png";
	
	var sc = '<a href="#" id="button2">' + browse_text + '</a>'+
			 '<ul id="log" class="roundborder ajax"></ul>' +
			 '<br /><br />';
	
	$('#upload_container').append(sc);
	
	var ajaxupload = new AjaxUpload('#button2', {
		action: upload_url,
		name: 'uploadfile',
		autoSubmit: false,
		responseType: 'json',
		onChange: function(file, extension){
		
			var listitem='<li class="roundborder">'+
				'File: <em>'+file+'</em><span class="progressvalue" ></span>'+
				'<div class="progressbar" ><div class="progress" ></div></div>'+
				'<p class="status" >Pending</p>'+
				'</li>';
			$('#log li').remove();
			$('#log').append(listitem);
		},
		onSubmit : function(file , ext){
			//if (ext && new RegExp('^(' + allowed.join('|') + ')$').test(ext)){
			
			var item=$('#log li');
			if (ext && /^(zip|pdf|eps|tif|tiff|psd|indd|doc|swf|rar|txt|ai|gif|png|jpg)$/.test(ext.toLowerCase())){
				var item=$('#log li');
				item.find('div.progress').addClass('loading');
				item.find('p.status').text('Uploading...');
			} else {
				item.addClass('error').find('p.status').text('Error:  Invalid file type');
				$('#button').removeAttr("disabled");
				ajaxupload.enable();
				return false;
			}
	
		},
		onComplete : function(file, response){
			var item=$('#log li');
			item.find('div.progress').removeClass('loading');
			item.find('p.status').text('Complete');
			
			var data = response;
			
			if(data.error == "") {
				item.addClass('success').find('p.status').text('Complete');
				var inputhidden = '<input type="hidden" name="file[]" value="'+data.data+'">';
				$("#submitform").append(inputhidden);
				
				uploadDone();
			}
			else {
				alert(data.error);
				$('#button').removeAttr("disabled"); 
				ajaxupload.enable();
				return false;
			}
	
		}		
	});
	
	$("#button").click(function(){
		$(this).attr('disabled', 'disabled');
		ajaxupload.disable();
		ajaxupload.submit();
		return false;
	});
}
