var upload;
var FreeUtil;
if(!FreeUtil)FreeUtil = {};

FreeUtil.Upload = function(p){
	var props = {
		loadMessage: 'Please wait',
		maxUploadSize: 1048576,
		loadInterval: 1000
	};
	
	$.extend(props, p);
	
	var count = 0;
	var maxCount = 10;
	var block = false;
	var intervalId = 0;
	
	this.isIE = function(){return $.browser.msie;};
	this.getLoadMessage = function(){return props.loadMessage;};
	this.getLoadInterval = function(){return props.loadInterval;};
	this.getMaxUploadSize = function(){return props.maxUploadSize;};
	
	this.startLoading = function(){
		var $elm = $('#lblLoadMessage');
		if($elm.size()>0 && block==false){
			block = true;
			if(count >= maxCount){
				$elm.html('');
				count = 0;
			}
			count++;
			$elm.html($elm.html()+ '.');
			block = false;
		}
	};
	this.stopLoading = function(){
		var $container = $('#divShowFile');
		if($container.size()>0){
			var $elm = $container.find('label');
			$elm.each(function(){
				$(this).remove();
			});
		}
		window.clearInterval(intervalId);
	};
	this.showLoader = function(){
		var $container = $('#divShowFile');
		if($container.size()>0){
			$container.addClass('text1');
			$container.html(this.getLoadMessage()+'<br/>');
			$('<label></label>')
				.attr('id','lblLoadMessage')
				.addClass('head1')
				.appendTo($container);
			this.startLoading();
			if(this.isIE()){
				intervalId = window.setInterval(upload.startLoading, upload.getLoadInterval());
			}
			else{
				intervalId = window.setInterval('upload.startLoading();', upload.getLoadInterval());
			}
		}
	};
	this.checkFileSize = function(obj, msg){
		if(obj && obj.type=='file'){
			if(this.isIE()){
				//var oas = new ActiveXObject("Scripting.FileSystemObject");
				//var d = obj.value;
				//var e = oas.getFile(d);
				//var f = e.size;
				//alert(f + " bytes");
			}
			else{
				var size = obj.files.item(0).fileSize;
				if(size > this.getMaxUploadSize()){
					alert(msg);
					return false;
				}
			}
			this.showLoader();
			return true;
		}
		return false;
	};
	this.viewFile = function(form){
		if(form){
			if(form.file.value.length == 0){
				alert(form.errNofileselected.value);
				return false;
			}
			if(form.file.value.toUpperCase().lastIndexOf('.MPP') == -1){
				alert(form.errInvalidfile.value);
				return false;
			}
			return this.checkFileSize(form.file, form.errInvaliduploadsize.value);
		}
		return false;
	};
};

/*window.onload = function(){
	FreeUtil.stopLoading();
	new TableWidget('tableViewFile');
};*/

$(document).ready(function(){
	upload = new FreeUtil.Upload();
	upload.stopLoading();
	new TableWidget('tableViewFile');
});
