/*
**************** DESCRIPCION DE JSON ******************************
forma:{id:'id_forma',clase:'prefijo_de_campos'},
url_guardar:'url',
url_eliminar:'url',
id_resultados:'id_resultados',
instancia:'nombre de la instancia',
eval_script:"alert('el usuario a sido guardado correctamente #{id_de_usuario}');"
**************** DESCRIPCION DE JSON ******************************
*/
var system_forma = Class.create();
system_forma.prototype = {
	initialize : function(json)
	{
		try
		{
			var un_div						= '<div class="#{class}" onclick="#{onclick}">#{valor}</div>';
			var contenedor					= '<div class="#{class} system_contenedor_titulo">#{titulo}</div><div class="system_contenedor_contenido">#{valor}</div>';
			this.un_div						= new Template(un_div);
			this.contenedor_errores			= new Template(un_div);
			this.contenedor_advertencias	= new Template(un_div);
			this.contenedor					= new Template(contenedor);
			this.opciones_json				= eval('(' + json + ')');
			this.json_array					= new Array();
		}
		catch(ex)
		{
			alert('system_forma.js->initialize::' + js_print_r(ex,'\n\r'));
		}
	}
	,
	eliminar:function()
	{
		try
		{
			var post	= '';
			eval("new Ajax.Request('" + this.opciones_json.url_eliminar + "',{contentType:'application/x-www-form-urlencoded',encoding:'UTF-8',postBody:'" + post.escapeHTML() + "&system_forma=guardar&system_forma_clase=" + this.opciones_json.forma.clase.escapeHTML() + "',onSuccess: function(t) {" + this.opciones_json.instancia.escapeHTML() + ".recibir_datos(t.responseText);} });");
		}
		catch(ex)
		{
			alert('system_forma.js->eliminar::' + js_print_r(ex,'\n\r'));
		}
	}
	,
	guardar:function()
	{
		try
		{
			mostrar_cargando();
			var post	= 'ajax=1&' + this.obtener_post() + "&system_forma=guardar&system_forma_clase=" + this.asegurar_string(this.opciones_json.forma.clase);
			if(this.resultados)
			{
				if(this.resultados.status)
				{
					this.resultados.status = 'proceso';
				}
			}
			eval("new Ajax.Request('" + this.asegurar_string(this.opciones_json.url_guardar) + "',{contentType:'application/x-www-form-urlencoded',encoding:'UTF-8',postBody:'" + post + "',onSuccess: function(t) {" + this.asegurar_string(this.opciones_json.instancia) + ".recibir_datos(t.responseText);} });");
		}
		catch(ex)
		{
			alert('system_forma.js->guardar::' + js_print_r(ex,'\n\r'));
		}
	}
	,
	agregar_post:function(nombre,valor)
	{
		try
		{
			this.post +=( this.post ? '&' : '' ) + this.asegurar_string(nombre) + '=' + this.asegurar_string(valor);
			this.json_array[this.asegurar_string(nombre)]	= this.asegurar_string(valor);
		}
		catch(ex)
		{
			alert('system_forma.js->agregar_post::' + js_print_r(ex,'\n\r'));
		}
	}
	,
	asegurar_string:function(texto)
	{
		return texto.sub("'", "\\'",50).sub(/[\n|\r]+/,"\\n",50).escapeHTML();
	}
	,
	obtener_post:function()
	{
		try
		{
			this.post 	= '';
			eval("$$('#" + this.asegurar_string(this.opciones_json.forma.id) + ' input.' + this.asegurar_string(this.opciones_json.forma.clase) + "').each(function(input){ " + this.asegurar_string(this.opciones_json.instancia) + ".agregar_post(input.name , $F(input.id));});");
			eval("$$('#" + this.asegurar_string(this.opciones_json.forma.id) + ' select.' + this.asegurar_string(this.opciones_json.forma.clase) + "').each(function(select){ " + this.asegurar_string(this.opciones_json.instancia) + ".agregar_post(select.name ,$F(select.id));});");
			eval("$$('#" + this.asegurar_string(this.opciones_json.forma.id) + ' textarea.' + this.asegurar_string(this.opciones_json.forma.clase) + "').each(function(textarea){ " + this.asegurar_string(this.opciones_json.instancia) + ".agregar_post(textarea.name , $F(textarea.id));});");
			return this.post;
		}
		catch(ex)
		{
			alert('system_forma.js->obtener_post::' + js_print_r(ex,'\n\r'));
		}
	}
	,
	eval_script:function(eval_template)
	{
		var eval_template_html	= new Template(eval_template);
		var script	 			= eval_template_html.evaluate(this.json_array)
		try{
			eval(script);
		}catch(ex){}
	}
	,
	recibir_datos:function(json)
	{
		try
		{
			if(json!="")
			{
				this.resultados		= eval('(' + json + ')');
				if(this.resultados.status=='error')
				{
					this.mostrar_errores();
					ocultar_cargando();
				}
				else if(this.resultados.status=='correcto')
				{
					if($(this.opciones_json.id_resultados))
					{
						$(this.opciones_json.id_resultados).innerHTML	= '';
					}
					ocultar_cargando();
				}
				else
				{
					alert('Your Connection to the server has been lost, please try again later');
					ocultar_cargando();
				}
				if(this.resultados.eval_script)
				{
					this.eval_script(this.resultados.eval_script);
					this.resultados.eval_script	= '';
				}			
			}
		}
		catch(ex)
		{
			alert('system_forma.js->recibir_datos::' + js_print_r(ex,'\n\r'));
		}
	}
	,
	redireccionar:function()
	{
		try
		{
			if(this.opciones_json.redireccionar)
			{
				location.href=this.resultados.redireccionar;
			}
		}
		catch(ex)
		{
			alert('system_forma.js->redireccionar::' + js_print_r(ex,'\n\r'));
		}
	}
	,
	seleccionar:function(campo)
	{
		Try.these(
			function(){$(campo).focus();}
  		) || false;
	}
	,
	mostrar_errores:function()
	{
		try
		{
			if(this.opciones_json.id_resultados && typeof(this.resultados['errores']['errores'])=="Array")
			{
				var html_errores 		= '';
				var html_advertencias 	= '';
				for (i =0; i < this.resultados['errores']['errores'].length; i++)
				{
					if(i>=0 && i<=this.resultados['errores']['errores'].length )
					{
						var dato = new Array();
						if(this.resultados['errores']['errores'][i].campo)
						{
							dato['onclick']		= this.opciones_json.instancia + '.seleccionar(' + this.resultados['errores']['errores'][i].campo + ');';
						}
						switch(this.resultados['errores']['errores'][i].type)
						{
							case '1':
								dato['valor']		= this.resultados['errores']['errores'][i].valor;
								dato['class']		= 'system_contenedor_errores pointer_hand pointer_hand';
								html_errores		+= this.contenedor_errores.evaluate(dato);
							break;
							case '2':
								dato['valor']		= '<b>Advertencia: </b>' + this.resultados['errores']['errores'][i].valor;
								dato['class']		= 'system_contenedor_advertencias pointer_hand';
								html_advertencias	+= this.contenedor_advertencias.evaluate(dato);
							break;
						}
					}
				}
				var dato = new Array();
				dato['valor']		= html_advertencias + html_errores;
				var html_final	= this.contenedor.evaluate(dato);
				
				if($(this.opciones_json.id_resultados))
				{
					$(this.opciones_json.id_resultados).innerHTML	= html_final;
				}
			}
		}
		catch(ex)
		{
			alert('system_forma.js->mostrar_errores::' + js_print_r(ex,'\n\r'));
		}
	}
};