
var AjaxC=function(siteroot) {
	this.site_root=siteroot;
	this.name;
	this.data=Array();
	this.lock=Array();
	this.url='ajax/caller_v2.php';
		
	var self=this;
	
	this.Call=function(method,form,callerb,succ) {
		var urld=self.site_root+self.url;
		
		if (!this.locked(callerb)) {
			this.lock(callerb);
			this.addData('ajaxc_callerb',callerb);
			this.addData('ajaxc_method',method);
			this.addData('ajaxc_form',form);
			this.addData('ajaxc_name',this.name);
			this.addData('ajaxc_action','call');
			this.addData('ajaxc_succ',succ);
			var set=this.setPrep(form);
			jQuery.ajax({
			   type: "POST",
			   url: urld,
			   data: set,
			   success: function(msg){
				  //alert(msg);
				  eval(msg);
			   }
			 });
		}
	};

	this.Update=function(method,destdiv,callerl,succ) {
		var urld=self.site_root+self.url;
		if (!this.locked(callerl)) {
			this.lock(callerl);	
			this.addData('ajaxc_callerb',callerl);
			this.addData('ajaxc_destdiv',destdiv);
			this.addData('ajaxc_method',method);
			this.addData('ajaxc_name',this.name);
			this.addData('ajaxc_action','update');	
			this.addData('ajaxc_succ',succ);
			var set=this.setPrep();
			jQuery.ajax({
			   type: "POST",
			   url: urld,
			   data: set,
			   success: function(msg){
				  // alert(msg);
					 eval(msg);
			   }
			 });			
		}

	};


	this.addData=function(label,data) {
		this.data[label]=data;
	};

	/* Funciones cerrojo */
	this.lock=function(callerb) {
		if (callerb) {
			self.lock[callerb]=true;
			jQuery('#'+callerb).addClass('cargando');
		}
	};
	this.unlock=function(callerb) {
		if (callerb) {
			self.lock[callerb]=false;
			jQuery('#'+callerb).removeClass('cargando');
		}
	};
	this.locked=function(callerb) {
		if (callerb) {
			if (self.lock[callerb]==true) return true;
			else return false;
		} else
			return false;
	};



	/* Tratamiento de los datos */
	this.setPrep=function(form) {
		var ret="";
		if (form) ret+=jQuery('#'+form).serialize();
		for (var i in self.data) {
			ret+='&'+i;
			ret+='='+self.urlencode(self.data[i]);
		}
		return ret;
	};

	this.urlencode=function (str) {
		return escape(str).replace('+', '%2B').replace('%20', '+').replace('*', '%2A').replace('/', '%2F').replace('@', '%40');
	};

}
