var vsf = {
	get:function(act, id) {
	// Luu Quang Vu
	// ********************************************
	// use to remove sub form
	($("div[id]").each(function(){
		if(this.id.indexOf('subForm')!= -1)
			$("#"+this.id).html('');
	}));
	// ********************************************
	var params = { vs: act, ajax:1};
	var noimage = "";
	if(typeof(noimage)=="undefine" || !noimage && id!='')
		$("#"+id).html('<img src="'+imgurl+'loader.gif"/>');
	$.get(ajaxfile,params,function(data){
		if(id!='') {
			data=data.replace("id=\""+id+"\"","");
			data=data.replace("id='"+id+"'","");
			$("#"+id).html(data).css('display','none')
			
			$("#"+id).fadeIn('slow');
			if($('#page_tabs').html() != null && $('#page_tabs').html() != 'undefined')
				$('#page_tabs').tabs();
		}
	});
	},
	popupGet:function(act, id, w, h) {
		if(!this.isDefined(w)) w = 500;
		if(!this.isDefined(h)) h = 500;
		if(!$("#"+id).html())
			$("body").append("<div id='"+id+"' class='"+id+"' >	</div>");
		vsf.get(act, id);
		$(document).ready(function() {
	    	$("#"+id).dialog({resizable: false, width:w, height:h});
		    $("#"+id ).bind( "dialogclose", function(event, ui) {
		  		$(this).remove(); 
			});
		});
	},
	submitForm:function(obj,act,id) {
		if(typeof(tinyMCE) != "undefined") tinyMCE.triggerSave();
		if(id!='') 
			$("#"+id).html('<img src="'+imgurl+'loader.gif"/>');
		var params = {
			vs:act,
			ajax: 1
		};
		var count = 0;
		obj
		.find("input[type='radio']:checked, input[checked], input[type='text'], input[type='hidden'], input[type='password'], input[type='submit'], option[selected], textarea")
		.each(function() {
			params[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value;
		});
		$.post(ajaxfile,params,function(data) {
			if(id!='') {
				data=data.replace("id=\""+id+"\"","");
				data=data.replace("id='"+id+"'","");
				$("#"+id).html(data).css('display','none')
				$("#"+id).fadeIn('slow');
				$('#page_tabs').tabs();
			}
		});
	},	
	
	isDefined:function(obj) {
    	return (typeof(obj) == "undefined")?  false: true;
	},
	
	removeForm:function(id){
		$("#"+id).html('');
	},
	
	select_switch:function(status ,object){
		for (i = 0; i < object.length; i++)
			object.elements[i].checked = status;
	},
	
	jSelect:function(the_value,idselect){
		$("#"+idselect+" option").each(function () {
			if(the_value == $(this).val())
				$(this).attr('selected','selected');
	    });
	},
	
	checkbox:function(the_value,the_list){
		if(the_value==the_list.value){
			the_list.checked=true;
			return true;
		}
	},
	
	jCheckbox:function(the_value,id){
		if(!$('#'+id))
			return;
		if(the_value==$('#'+id).val()){
			$('#'+id).attr('checked','checked');
			return true;
		}
	}, 
	
	jRadio:function(the_value,name){
		$("[name="+name+"]").each(function () {
			if(the_value == $(this).val())
			{ 
				$(this).attr('checked','checked');
			}
		});
	 },
	 
	radio:function(the_value,the_list){
		var name_count = the_list.length; 
		for(i=0;i<name_count;i++)
			if (the_value==the_list[i].value){
				the_list[i].checked=true;
				return true;
			}
	 },
	 
	select:function (the_value,the_list){
		var option_count = the_list.options.length; 
		for(i=0;i<option_count;i++)
			if(the_value==the_list.options[i].value){
				the_list.options[i].selected=true;
				return true;
			}
	},
	
	alert:function (message){
		jAlert(
			message,
			global_website_title +" Dialog"
		);
	},
	
	uploadFile:function( formId, module, action, objIdCallBack, fileFolder){
		var countFile = 0;
		$("#"+formId).find("input[type='file']").each(function(){
			if(this.value){
				countFile ++;
			}
		});
		
		if(countFile > 0){
			$('#error-message').ajaxStart(function(){
				$(this).html("<img src='skins/admin/blue/images/loader.gif' alt='loading' />");
			});
			var file = "";
			$("#"+formId).find("input[type='file']").each(function(){
				if(this.value){
					var name = this.name;
					var uri =baseUrl+"files/uploadfile/&ajax=1&uploadName="+name+"&fileFolder="+fileFolder+"&table="+module;
					$.ajaxFileUpload({
							url:uri,
							secureuri:false,
							fileElementId:name,
							dataType:"json",
							success: function (data, status)
							{
								countFile--;
								if(typeof(data.error) != 'undefined')
								{
									if(data.error != '')
									{
										jAlert(data.error,"Vietsol Infomation");
									}
									else
									{
										file += data.fileId + ",";
										if(countFile == 0){
											$("#"+formId).append("<input type='hidden' name='fileId' id='fileId' value='"+file.substring(0,file.length-1)+"'/>");
											vsf.submitForm($('#'+formId), module+'/'+action+'/', objIdCallBack);
											return false;
										}
									}
								}
							},
							error: function (data, status, e)
							{
								countFile--;
								$('#error-message').ajaxStop(function(){
									$(this).html(e);
								});
								return false;
							}
						}
					)
				}
			});
			
		}
		else{
			$('#error-message').ajaxStop(function(){
				$(this).html('');
			});
			vsf.submitForm($('#'+formId), module+'/'+action+'/', objIdCallBack);
			return false;
		}
		$('#error-message').ajaxStop(function(){
			$(this).html('');
		});
		return false;
	}
}
