;(function ($) {
/*
 * jqGrid  3.3 - jQuery Grid
 * Author: Jesus Esparza based on jqGrid codes
 * Copyright (c) 2008, Tony Tomov, tony@trirand.com
 * Dual licensed under the MIT and GPL licenses
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * Date: 2008-10-29 rev 1
 */
 function appendObject_md(forma, id, value)
{
	var obj = document.createElement("input");
	obj.setAttribute("type", "hidden");
	obj.setAttribute("id", id);
	obj.setAttribute("name", id);
	obj.setAttribute("value", value);
	forma.appendChild(obj);
}
$.fn.modalForm = function( p ) {
	p = $.extend(true,{
	left: 0,
	top: 0,
	width: 300,
	height: 150,
	imgpath: "",
	closeicon: "ico-close.gif",
	caption: "Dialog",
	center: true,
	modal: true,
	drag: true,
	idForm: "modalFrm",
	url: "",
	mtype: "POST",
	postData: null,
	iframe_obj:null,
	iframe_id:null,
	refresh: false
	}, $.modalForm, p || {});
	$.fn.close = function() {
		p.iframe_obj.remove();
		this.jqmHide();
	};
	$.fn.reload = function() {
		var loForma = document.createElement("form");
		
		loForma.setAttribute("id", "frm_modal");
		loForma.setAttribute("name", "frm_modal");
		loForma.setAttribute("method", p.mtype);
		loForma.setAttribute("action", p.url);
		loForma.setAttribute("target", p.iframe_id);
		// Barrido de paso de datos
		if ( p.postData != null )
		{
			for (var lsPar in p.postData)
			{
				appendObject_md(loForma,lsPar,p.postData[lsPar]);
			}
		}
		document.body.appendChild(loForma);
		loForma.submit();
        	document.body.removeChild(loForma); 
	};
	return this.each( function() {
		this.p = p;
		//var myDate=new Date();
		//var id_unique_name = this.p.idForm + myDate.getTime();
		var id_unique_name = this.p.idForm;
		var IDs = { themodal:id_unique_name,
			    modalhead:id_unique_name + "_head",
			    modalcontent:id_unique_name + "_cnt" };
		
		// Remover el objeto si existe. Es necesario remover el objeto porque sino despliega la pantalla en blanco
		if($('#' + id_unique_name)) {
			$('#' + id_unique_name).remove();
		}
		
		// Centrar la forma?
		if (this.p.center) {
			var x_offset = this.clientLeft + this.offsetLeft;
			var y_offset = this.clientTop + this.offsetTop;
			var pos_left = ($(document).width()/2 - this.p.width/2) - x_offset;
			var pos_top  = ($(document).height()/2 - this.p.height/2) - y_offset;
			this.p.left = ( pos_left > 0 ? pos_left : 5 ); 
			this.p.top  = ( pos_top  > 0 ? pos_top  : 5 );
		}
		
		/* Para metodo GET
		var lsParsArgs = "";
		if ( this.p.postData != null )
		{
			for (var lsPar in this.p.postData)
			{
				lsParsArgs += ( lsParsArgs == "" ? "" : "&" ) + lsPar + "=" + this.p.postData[lsPar];
			}
		}
		//var cnt = $("<iframe id='" + id_unique_name + "_frm' name='" + id_unique_name + "_frm' src='" + this.p.url + (lsParsArgs=="" ? "" : "?" + lsParsArgs) + "' width='100%' height='92%' marginwidth='0' marginheight='0' frameborder='0' scrolling='auto'></iframe>");
		*/
		
		var cnt = $("<iframe id='" + id_unique_name + "_frm' name='" + id_unique_name + "_frm' src='javascript:false;document.write(\"\");' width='100%' height='92%' marginwidth='0' marginheight='0' frameborder='0' scrolling='auto'></iframe>");
		this.p.iframe_id = id_unique_name + "_frm";
		this.p.iframe_obj = cnt;
		createModal(IDs,cnt,this.p,this,this);
		if(this.p.drag) { DnRModal("#"+IDs.themodal,"#"+IDs.modalhead+" td.modaltext"); }
		viewModal("#"+IDs.themodal,{modal:this.p.modal});
		$.fn.reload();
	});
};
})(jQuery);
