var ajax = new ajax();
function ajax () {

	this.load = function (parent) {
		
		if (parent == null) {
			ajax.links();
			ajax.forms();
		} else {
			ajax.links(parent);
			ajax.forms(parent);
		}
	};
	
	this.links = function (parent) {
		
		var links;
		
		if (parent == null) {
			
			links=$("a[href*='ajax-']");
		} else {
			
			links=$(parent).find("a[href*='ajax-']");
		}
		
		links.bind('click', function() { 
					
			var href=this.getAttribute('href');
			if(href != "#") {
				ajax.request(href, 'GET', '');
				return false;
			}
		});
	};
			
	this.forms = function (parent) {
		
		var error=0, forms, cls;
		
		if (parent == null) {
			
			forms=$("form[action*='ajax-']");
		} else {
			
			forms=$(parent).find("form[action*='ajax-']");
		}
		
		forms.bind('submit', function() { 
			
			var id = this.getAttribute("id");
			$("#error_"+id).html("");
			var msg = true;
			
			var validate=control.save_forms[id];
			if(validate != null) {
				validate = (control.save_forms[id].v != null) ? control.save_forms[id].v : null;
			}
			if(validate != null) {
				$(this).find("input[type!='hidden']").each(function() { 
					
					var cls= (validate[this.getAttribute('name')] != null) ? validate[this.getAttribute('name')] : null;
					if (cls != null) {
						msg = ajax.validate(this.name, this.value, cls);
						if (msg != true) { 
							error = 1;
							ajax.showMessageForm(msg,id);
						} 
					}
				});
				
				$(this).find("textarea").each(function() { 
					
					var cls= (validate[this.getAttribute('name')] != null) ? validate[this.getAttribute('name')] : null;
					if (cls != null) {
						msg = ajax.validate(this.name, this.value, cls);
						if (msg != true) { 
							error = 1;
							ajax.showMessageForm(msg,id);
						} 
					}
				});
				
				$(this).find("select").each(function() { 
					
					var cls= (validate[this.getAttribute('name')] != null) ? validate[this.getAttribute('name')] : null;
					if (cls != null) { 
						var val=$(this).val();
						if(val == null) {
							val=0;
						}
						msg = ajax.validate(this.name, val, cls);
						if (msg != true) { 
							error = 1;
							ajax.showMessageForm(msg,id);
						} 
					}
				});
			}
			
			if (error == 0) {
				
				if(id != null && id != "") {
					
					if(control.save_forms[id].func != null && control.save_forms[id].func != undefined) {
						
						var resp=control.attachCallback(control.save_forms[id].func);
						if(resp == "do_request") {
							
							ajax.request(this.getAttribute('action'), 'POST', $(this).serialize()+"&reqid="+id,(control.save_forms[id].responseFunc != null) ? control.save_forms[id].responseFunc : null);
						}
					} else {
						ajax.request(this.getAttribute('action'), 'POST', $(this).serialize()+"&reqid="+id,(control.save_forms[id].responseFunc != null) ? control.save_forms[id].responseFunc : null);
					}
				} else {
					ajax.request(this.getAttribute('action'), 'POST', $(this).serialize());
				}
			}
			error=0;
			return false;
		});
	};

	this.request = function (url, method, content, func) {
		
		$(".div_error").html("").css({display : "none"});
		$.ajax({
			type: method,
			url: url,
			data: (content!=null) ? content : '',
			dataType : "json",
			success: function(data){
		   	 	ajax.response(data);
				if(func != null) {
					control.attachCallback(func,data);
				}
		   	}
		});
		return false;
	};
	
	this.basicMessage = function(name,re1,re2,re3) {
		
		var msg, r1='', r2='', r3='';
		if(re3) {
			r3=re3;
		}
		if(re2) {
			r2=re2;
		}
		if(re1) {
			r1=re1;
		}
		var msg=lang.v['basicmsg'][name];
		msg = msg.replace('$', r1); 
		msg = msg.replace('$', r2);
		msg = msg.replace('$', r3);
		return "<p>"+msg+"</p>";
	};
	
	this.message = function (name, ret, vfunc, vfunc2) {
		var w = lang.v['word'][name];
		var n=w['n'];
		var det=w['det'];
		var reason, msg;
		if(vfunc == 1 && (ret == "min" || ret == "max" || ret == "eq")) {
			ret=ret+'_one';
		}
		reason=lang.v['msg'][ret];
		
		if(reason[det] != null) {
			
			reason=reason[det];
		} else {
			reason=reason['fm'];
		}
		
		if (reason != null) {
			if(ret != "selreq" && ret != "terms") {
				
				msg=lang.v['msg']['det'][det]+" "+reason;
				msg = msg.replace('$', n); 
				msg = msg.replace('$', vfunc);
				msg = msg.replace('$', vfunc2);
				
			} else {
				
				msg=reason+" "+lang.v['msg']['det'][det]+" "+n;
			}
			if(name == "terms" || name == "date" )
			{
				msg=msg;
			}
			return "<p>"+msg+"</p>";
		}
	};
	
	this.validate = function (name, value, arg) {

		var x, exp = arg.split(','), m = exp.length, message = '', error;
				
		for (x=0;x<m;++x) {
		
			var opt = jQuery.trim(exp[x]);
			var size = value.length;
			var vfunc2 = "";
			var reg = new RegExp(/([a-zA-Z0-9_]+)\(([a-zA-Z0-9]+)\)\(([a-zA-Z0-9]+)\)/);
			var result = reg.exec(opt);
			if(result != null) {
				
				var vfunc2 = result[3];
			} else {
				
				var reg = new RegExp(/([a-zA-Z0-9_]+)\(([a-zA-Z0-9]+)\)/);
				var result = reg.exec(opt);
			}
			
			if (result != null) {
			
				var required=1;
				var func = result[1];
				var vfunc = result[2];
				var ret = 0;
				var n = 0;

				if(func.substr(0,3)=="if_") {
					required=0;
					func=func.replace("if_","");
				}
				
				if (required == 1 || (required == 0 && value != "")) {
					
					if (func == 'min' && vfunc > size) {
						ret = 1;
					} else if (func == 'max' && vfunc < size) {
						ret = 2;
					} else if (func == 'range' && (vfunc > size || vfunc2 < size)) {
						ret = 3;
					} else if (func == 'eq' && vfunc != size) {
						ret = 4;
					} else if (func == 'exp') {
						var expression=lang.v['exp'][name];
						if (!value.match(expression)) {
							ret = 5;
						}
					} else if (func == 'same') {
						var expression = lang.v['same'][name]['jexp'];
						var mode = lang.v['same'][name]['mode'];
						if (mode == "value") {
							if ($(expression).attr("value") != value) {
								ret = 6;
							}
						}
					} else if (func == "func") {
						if (vfunc2 != "") {
							if (!vfunc(value,vfunc2)) {
								ret = 7;
							}
						} else  {
							if (!vfunc(value)) {
								ret = 7;
							}
						}
					} else if (func == 'selreq' && vfunc == value) {
						ret = 8;
					} else if (func == 'numeric') {
						var regE = new RegExp('[0-9]{'+size+'}');
						var t=regE.exec(value);
						if(t == null) {
							ret = 9;
						}
					} else if (func == 'alpha') {
						var regE = new RegExp('[a-zñáéíóúç ]{'+size+'}',"i");
						var t=regE.exec(value);
						if(t == null) {
							ret = 10;
						}
					} else if (func == 'alphanumeric') {
						var regE = new RegExp('[0-9a-zñáéíóúç ]{'+size+'}',"i");
						var t=regE.exec(value);
						if(t == null) {
							ret = 11;
						}
					}
				}
	
				if (ret > 0) { 
					error=1;
					message+=ajax.message(name, func, vfunc, vfunc2);
				}
			}
		}
		if(error == 1) {
			
			return message;
		} else {
			
			return true;
		}
		error=0;
	};
	
	this.response = function (data) {
		if (data.redirect != null) {
			
			document.location.href=data.redirect;

		} else if (data.error != null) {
			
			if(data.error.alert_msg != null) {
				pos=0;
				var time=0;
				while(data.error.alert_msg[pos] != null) {
					
					if (data.error.alert_msg[pos].func != null) {
						
						if (data.error.alert_msg[pos].time != null) {
							time=parseInt(time+data.error.alert_msg[pos].time);
						}
						var fn=data.error.alert_msg[pos].func, params=data.error.alert_msg[pos].params;
						var msg=control.attachCallback(fn,params);
						msg=msg.replace(/<p>/g,'').replace(/<\/p>/g,'');
						if(time != 0) {
							setTimeout(function () {
								alert(msg);
							},time);
							
						} else {
							alert(msg);
						}
					}
					pos = pos + 1;
				}
			}
			if(data.error.functions !=null) {
				pos=0;
				while (data.error.functions[pos] != null) {
					
					if (data.error.functions[pos].func != null) {
						
						var fn=data.error.functions[pos].func, params=data.error.functions[pos].params;
						control.attachCallback(fn,params);
					}
					pos=pos+1;
				}
				
			}
			if(data.error.form != null) {
				time=1;
				var reqid="body";
				if(data.reqid != null) {
					reqid=data.reqid;
				}
				if(data.error.timeout != null) {
					time=data.error.timeout;
				}
				setTimeout(function () {
					
					pos=0;
					while (data.error.form[pos] != null) {
						
						var type="complet";
						var params=data.error.form[pos].params;
						if (data.error.form[pos].type != null) {
							type=data.error.form[pos].type;
						}
						if(type=="basic") {
							var msg=ajax.basicMessage(params[0],params[1],params[2],params[3]);
						} else {
							var msg=ajax.message(params[0],params[1],params[2],params[3]);
						}
						ajax.showMessageForm(msg,reqid);
						pos=pos+1;
					}
					
				},time);
			}
			
		} 
		if (data.execute != null) {
			pos=0;
			var time=0;
			while (data.execute[pos] != null) {
				if(data.execute[pos].func != null) {
					if (data.execute[pos].time != null) {
						time=parseInt(time+data.execute[pos].time);
					}
					var fn=data.execute[pos].func, params=data.execute[pos].params;
						 
					if(time != 0) { 
						setTimeout(function () { 
							control.attachCallback(fn,params);
						},time);
						
					} else {
						control.attachCallback(fn,params);
					}
				}
				pos=pos+1;
			}
		} 
	};
	
	this.showMessageForm = function(msg,reqid) {
		$("#error_"+reqid).html($("#error_"+reqid).html()+msg).css({display : "block"});
	};
	
}