function DameOrigen(event)
{
	var Origen;
	if (!event)
      event = window.event;
	if (event.srcElement)
	  Origen = event.srcElement;
	else if (event.currentTarget)
	  Origen = event.currentTarget;
	return Origen;
}

function DameIDOrigen(event)
{
	var IDOrigen = '';
	
	if (event.srcElement)
	  IDOrigen = event.srcElement.id;
	else if (event.currentTarget)
	  IDOrigen = event.currentTarget.id;
	  
	return IDOrigen;
}

function Dialogo(Mensaje)
{
	$('#Dialogo').dialog('close');
	if (Mensaje)
		$('#Dialogo').html(Mensaje);
	$('#Dialogo').dialog('open');
}

function Editando()
{
    $("#BAnnadir").attr("disabled", true); 
	QuitaOnClick(".LinkEB");
}

function QuitaOnClick(Selector)
{
	$(Selector).each(function () {
		this.onclick = ""; });
}

function BeginEspere()
{
	$("#Espere").dialog('open');
	$(".ui-dialog-titlebar-close").hide();
}

function EndEspere()
{
	$("#Espere").dialog('close');
}

function ArrayToString(ElArray)
{
	var ElString = "";
	ElString = ElArray.join(",");
	return ElString;
}

function StringToArray(ElString)
{
	var ElArray = new Array()
	if (ElString.length > 0)
		ElArray = ElString.split(",");
	return ElArray;
}

function jQueryValToString($CajasTexto)
{
	var Resultado = "";
	var MiArray = new Array()
	$CajasTexto.each(function (i) {
		MiArray[i] = $(this).val();
	});
	return ArrayToString(MiArray);	
}

function ESDoPostBack(Boton, Parametro)
{
	$('#TAJAXUtil_Espere').dialog('open');
	__doPostBack(Boton, Parametro);
	
}

function IgualaBotones($Botones)
{
    if ($Botones.length > 1)
    {
      MaxAncho = $($Botones.get(0)).outerWidth();
      for (var i = 1; i < $Botones.length; i++) 
      {
        Wn = $($Botones.get(i)).outerWidth();
        MaxAncho = Math.max(MaxAncho, Wn);
      }
      $Botones.width(MaxAncho);
    }	
}

$.maxZIndex = $.fn.maxZIndex = function(opt) {
    /// <summary>
    /// Returns the max zOrder in the document (no parameter)
    /// Sets max zOrder by passing a non-zero number
    /// which gets added to the highest zOrder.
    /// </summary>    
    /// <param name="opt" type="object">
    /// inc: increment value, 
    /// group: selector for zIndex elements to find max for
    /// </param>
    /// <returns type="jQuery" />
    var def = { inc: 10, group: "*" };
    $.extend(def, opt);
    var zmax = 0;
    $(def.group).each(function() {
        var cur = parseInt($(this).css('z-index'));
        zmax = cur > zmax ? cur : zmax;
    });
    if (!this.jquery)
        return zmax;

    return this.each(function() {
        zmax += def.inc;
        $(this).css("z-index", zmax);
    });
}

$.fn.focusNextInputField = function() {
    return this.each(function() {
        var fields = $(this).parents('form:eq(0),body').find(':input').not('[type=hidden]');
        var index = fields.index( this );
        if ( index > -1 && ( index + 1 ) < fields.length ) {
            fields.eq( index + 1 ).focus();
        }
        return false;
    });
};

$.fn.CapturaEsc = function() {
    return this.each(function() {
		$(this).parent().unbind("keydown");
        $(this).parent().keydown(function(e) {
	        if ((e.which && e.which == 27) || (e.keyCode && e.keyCode == 27)) 
			{
				var zmax = 0;
				var DialogoTop;
				$(".Dialogo").each(function() {
			        var cur = parseInt($(this).parent().css('z-index'));
					if ($(this).parent().is(":visible"))
					{
						if (cur > zmax)
						{
					        zmax = cur;
							DialogoTop = this;
						}
					}
			    });
				$(DialogoTop).dialog("close");
		        return false;
	        }
    	});
	});
};

$.fn.IntroNextInputField = function() {
    return this.each(function() {
        $(this).keypress(function(e) {
	        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) 
			{
		        var fields = $(this).parents('form:eq(0),body').find(':button,input,textarea,select').not('[type=hidden]');
		        var index = fields.index( this );
		        if ( index > -1 && ( index + 1 ) < fields.length ) 
				{
					for (var i = index + 1; i < fields.length; i++)
					{
						if (fields.eq(i).is(':button'))
						{
				            fields.eq(i).click();
					        return false;
						}
					}
		        }
		        return false;
	        }
    	});
	});
};

$.fn.IgualaBotones = function() {
    if ($(this).length > 1)
    {
		$Botones = $(this).filter(".Boton")
	    if ($Botones.length > 1)
	    {
	      MaxAncho = $($Botones.get(0)).outerWidth();
	      for (var i = 1; i < $Botones.length; i++)
	      {
	        Wn = $($Botones.get(i)).outerWidth();
	        MaxAncho = Math.max(MaxAncho, Wn);
	      }
	      $Botones.width(MaxAncho);
	    }	
    }
	return this;	
};

$.fn.ShowMasterValidator = function() {
    if ($(this).length = 0)
		$MVs = $(".MasterValidatorDialog")
	else
		$MVs = $(this).filter(".MasterValidatorDialog")
    if ($MVs.length > 0 && $MVs.css("visibility") != "hidden")
    {
		Validator_Msj($MVs.text());
//		if ($("#MasterValidatorDialog").length == 0)
//			$("body").append('<div id ="MasterValidatorDialog"></div>');
//			
//		$("#MasterValidatorDialog").dialog({autoOpen: false, modal: true, resizable: true, minHeight: 100});
//		$("#MasterValidatorDialog").html($MVs.text());
//		$("#MasterValidatorDialog").dialog("open");
    }
	return $MVs;	
};
