/*===============================================================================
 *= Ing. Software: Jaime R. Camargo Z.
 *= Fecha: Abr 22 2003
 *= Modif:
 *===============================================================================
 *=
 *= Argumentos:
 *=   event    = objeto evento. (o)
 *=   Exclude  = caracteres o teclas a aceptar aparte del alfabeto. (a)
 *=
 *= Retorna:
 *=   true     = perfecto.
 *=   false    = parámetros errados.
 *=
 *= Ejemplo:
 *= <INPUT type="text" onkeypress="AcceptOnlyAlphabet(event,'01');">
 *= <INPUT type="text" onkeypress="AcceptOnlyAlphabet(event);">
 *===============================================================================*/
function AcceptOnlyAlphabet(event, Exclude)
{
  var nKey, cKey;

  //No es un objeto el argumento?
  if (typeof(event) != "object") return false;

  //El segundo argumento no se obvió y es de tipo diferente a cadena?
  if (typeof(Exclude) != "undefined" && typeof(Exclude) != "string") return false;

  if (document.all)
    nKey = event.keyCode;
  else
    nKey = event.charCode;

  cKey = String.fromCharCode(nKey)

  if (!Exclude) Exclude = "";

  if (IsAlphabet(cKey) == false && Exclude.indexOf(cKey) == -1 && nKey != 0)
  {
    if (document.all)
      event.keyCode = 0;
    else
      event.preventDefault();
  }

  return true;
}//AcceptOnlyAlphabet()

/*===============================================================================
 *= Ing. Software: Jaime R. Camargo Z.
 *= Fecha: Abr 22 2003
 *= Modif:
 *===============================================================================
 *=
 *= Argumentos:
 *=   event    = objeto evento. (o)
 *=   Chars    = caracteres o teclas a aceptar. (a)
 *=
 *= Retorna:
 *=   true     = perfecto.
 *=   false    = parámetros errados.
 *=
 *= Ejemplo:
 *= <INPUT type="text" onkeypress="AcceptOnlyChars(event,'1A-452');">
 *===============================================================================*/
function AcceptOnlyChars(event, Chars)
{
  var nKey, cKey;

  //No es un objeto el argumento?
  if (typeof(event) != "object") return false;

  //El segundo argumento no se obvió y es de tipo diferente a cadena?
  if (typeof(Chars) != "undefined" && typeof(Chars) != "string") return false;

  if (document.all)
    nKey = event.keyCode;
  else
    nKey = event.charCode;

  cKey = String.fromCharCode(nKey)

  if (!Chars) Chars = "";

  if (Chars.indexOf(cKey) == -1 && nKey != 0)
  {
    if (document.all)
      event.keyCode = 0;
    else
      event.preventDefault();
  }

  return true;
}//AcceptOnlyChars()

/*===============================================================================
 *= Ing. Software: Jaime R. Camargo Z.
 *= Fecha: Abr 22 2003
 *= Modif:
 *===============================================================================
 *=
 *= Argumentos:
 *=   event    = objeto evento. (o)
 *=   Exclude  = caracteres o teclas a aceptar aparte de los números. (a)
 *=
 *= Retorna:
 *=   true     = perfecto.
 *=   false    = parámetros errados.
 *=
 *= Ejemplo:
 *= <INPUT type="text" onkeypress="AcceptOnlyNumbers(event,'AB');">
 *= <INPUT type="text" onkeypress="AcceptOnlyNumbers(event);">
 *===============================================================================*/
function AcceptOnlyNumbers(event, Exclude)
{
  var nKey, cKey;

  //No es un objeto el argumento?
  if (typeof(event) != "object") return false;

  //El segundo argumento no se obvió y es de tipo diferente a cadena?
  if (typeof(Exclude) != "undefined" && typeof(Exclude) != "string") return false;

  if (document.all)
    nKey = event.keyCode;
  else
    nKey = event.charCode;

  cKey = String.fromCharCode(nKey)

  if (!Exclude) Exclude = "";

  if (IsNumeric(cKey) == false && Exclude.indexOf(cKey) == -1 && nKey != 0)
  {
    if (document.all)
      event.keyCode = 0;
    else
      event.preventDefault();
  }

  return true;
}//AcceptOnlyNumbers()

/*===============================================================================
 *= Ing. Software: Jaime R. Camargo Z.
 *= Fecha: Abr 22 2003
 *= Modif:
 *===============================================================================
 *=
 *= Argumentos:
 *=   Text   = Texto a verificar si es alfabético. (a)
 *=
 *= Retorna:
 *=   true     = es alfabético.
 *=   false    = no es alfabético.
 *=
 *= Ejemplo:
 *= <SCRIPT language="javascript">if (IsAlphabet("abc")) alert("alfabético");</SCRIPT>
 *===============================================================================*/
function IsAlphabet(Text)
{
  return /^[a-zA-Z]+$/.test(Text);
}//IsAlphabet()

/*===============================================================================
 *= Ing. Software: Jaime R. Camargo Z.
 *= Fecha: Abr 28 2003
 *= Modif:
 *===============================================================================
 *=
 *= Argumentos:
 *=   Text   = Texto a verificar si es un número con fracción. (a|n)
 *=
 *= Retorna:
 *=   true     = es un número con fracción.
 *=   false    = no es un número con fracción.
 *=
 *= Ejemplo:
 *= <SCRIPT language="javascript">if (IsDecimal("123")) alert("Fracción");</SCRIPT>
 *===============================================================================*/
function IsDecimal(Text)
{
  return /^[-+]?\d+\.?\d+$/.test(Text);
}//IsDecimal()

function IsEmail(Email)
{
	//Tipo de datos de los parámetros son válidos?
	if (typeof(Email) != "string") return false;

	//var re = new RegExp(/\w{4,}@\w{4,}\.\w{2}[\.\w{2}]/);
	var re = new RegExp(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/);

	return re.test(Email);
}//IsEmail()

/*===============================================================================
 *= Ing. Software: Jaime R. Camargo Z.
 *= Fecha: Abr 22 2003
 *= Modif:
 *===============================================================================
 *=
 *= Argumentos:
 *=   Text   = Texto a verificar si es un número. (a|n)
 *=
 *= Retorna:
 *=   true     = es un número.
 *=   false    = no es un número.
 *=
 *= Ejemplo:
 *= <SCRIPT language="javascript">if (IsNumeric("123")) alert("número");</SCRIPT>
 *===============================================================================*/
function IsNumeric(Text)
{
  return /^[-+]?\d+$/.test(Text);
}//IsNumeric()

/*===============================================================================
 *= Ing. Software: Jaime R. Camargo Z.
 *= Fecha: Mar 17 2003
 *= Modif:
 *===============================================================================
 *=
 *= Argumentos:
 *=   Number   = Número a formatear. (a|n)
 *=   Decimals = Número de decimales a formatear. (n)
 *=
 *= Retorna:
 *=   Número formateado a millares.
 *=
 *= Ejemplo:
 *= <INPUT type="text" onchange="this.value=FormatNumber(this.value,0);">
 *===============================================================================*/
function FormatNumber(Number, Decimals)
{
  Number = UnformatNumber(Number);

  if (!/^-?\d+$/.test(Number)) return 0;

  var sPartDec="", n="", i, id;

  Number = parseFloat(Number).toString();

  with (Number)
  {
    id = indexOf(".");
    if (id != -1)
    {
      if (Decimals > 0)
      {
        sPartDec = substr(id + 1);
        sPartDec = sPartDec.substr(0,Decimals);
        sPartDec = "." + sPartDec;
      }//if
      Number   = substr(0,id);
    }//if
    else
      id = length;

    i = length % 3;
    for (i; i<id; i+=3)
      n = n + "," + substr(i,3);

    i = length % 3;
    n = substr(0,i) + (i ? n : n.substr(1)) + sPartDec;
  }//with

  return n;
}//FormatNumber()

/*===============================================================================
 *= Ing. Software: Jaime R. Camargo Z.
 *= Fecha: Abr 23 2003
 *= Modif:
 *===============================================================================
 *=
 *= Argumentos:
 *=   event = objeto event
 *=
 *= Retorna:
 *=   Número formateado a millares en línea.
 *=
 *= Ejemplo:
 *= <INPUT type="text" onkeypress="FormatNumberOnLine(event);">
 *===============================================================================*/
function FormatNumberOnLine(event)
{
  var nKey, cKey;
  var InputText=null;

  //No es un objeto el argumento?
  if (typeof(event) != "object") return false;

  if (document.all)
  {
    nKey      = event.keyCode;
    InputText = event.srcElement;
    event.keyCode = 0;
  }
  else
  {
    nKey      = event.charCode;
    InputText = event.target;

    if (nKey != 0) event.preventDefault();
  }

  cKey = String.fromCharCode(nKey);

  if (IsNumeric(cKey))
    with (InputText)
      value = FormatNumber(value.toString() + cKey, 0);
  else
    return false;

  return true;
}//FormatNumberOnLine()

/*===============================================================================
 *= Ing. Software: Jaime R. Camargo Z.
 *= Fecha: Feb 24 2003
 *= Modif:
 *===============================================================================
 *=
 *= Argumentos:
 *=   YourDate = Fecha a validar. (a)
 *=   Format   = Formato de fecha a tomar. (a,1)
 *=
 *=   (1): Posibles valores YMD, MDY, DMY
 *=
 *= Retorna:
 *=   true     = fecha válida.
 *=   false    = fecha nó valida.
 *=
 *= Ejemplo:
 *= <SCRIPT language="javascript">if (IsDate("1978/04/11","YMD")) alert("fecha");</SCRIPT>
 *===============================================================================*/
function IsDate(YourDate, Format)
{
	var Year, Month, Day;
	var re;

	//Tipo de datos de los parámetros son válidos?
	if (typeof(YourDate) != "string" || typeof(Format) != "string") return false;

	if (Format == "YMD")
		re = /^(\d{4})\/(\d{1,2})\/(\d{1,2})$/;
	else
		re = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;

	if (!re.test(YourDate))	return false;

  with (RegExp)
  {
	  if (Format == "YMD")
	  {
	  	Year  = $1;
	  	Month = $2;
	  	Day   = $3;
	  }//if
	  else
	  {
	  	Year  = $3;
	  	if (Format == "MDY")
	  	{
	  		Month = $1;
	  		Day   = $2;
	  	}//if
	  	else
	  	{
	  		Month = $2;
	  		Day   = $1;
	  	}//else
	  }//else
	}//with

	var MyDate = new Date(YourDate);

	with (MyDate)
		if (getFullYear() != Year || getMonth() != --Month || getDate() != Day) return false;

	return true;
}//IsDate()

/*===============================================================================
 *= Ing. Software: Jaime R. Camargo Z.
 *= Fecha: Mar 06 2003
 *===============================================================================
 *=
 *= Argumentos:
 *=   url        = Página a cargar en la nueva ventana. (n)
 *=   alias      = Nombre de la nueva ventana. (a)
 *=   width      = Ancho de la nueva ventana. (n,1)
 *=   height     = Altura de la nueva ventana. (n,1)
 *=   location   = Define si la nueva ventana tendrá la caja de direcciones. (n,1)
 *=   menubar    = Define si la nueva ventnaa tendrá barra de menúes. (n,1)
 *=   resizable  = Define si la nueva ventana podrá redimensionarse. (n,1)
 *=   scrollable = Define si la nueva ventana tendrá barra de desplazamientos. (n,1)
 *=   status     = Define si la nueva ventana tendrá barra de estado. (n,1)
 *=   toolbar    = Define si la nueva ventana tendrá barra de herramientas. (n,1)
 *=
 *=  (1): Posibles valores: no, yes, 1, 0
 *=
 *= Retorna:
 *=   objeto referencia a la nueva ventana
 *=   false = Parámetros no válidos.
 *=
 *===============================================================================*/
function OpenWindowCentered(url, alias, width, height, location, menubar, resizable, scrollable, status, toolbar)
{
  var rn = /^\d+$/;
  var r = /^(no|yes|1|0)$/i;

  if (typeof(url)         != "string"         || typeof(alias)      != "string" ||
      !rn.test(width)     || !rn.test(height) ||
      !r.test(location)   || !r.test(menubar) || !r.test(resizable) ||
      !r.test(scrollable) || !r.test(status)  || !r.test(toolbar)) return false;

  return window.open(url,alias,"left=" + ((screen.width-width)/2) + ",top=" + ((screen.height-height)/2) + ",width=" + width + ",height=" + height + ",location=" + location + ",menubar=" + menubar + ",resizable=" + resizable + ",scrollbars=" + scrollable + ",status=" + status + ",toolbar=" + toolbar);
}//OpenWindowCentered()

/*===============================================================================
 *= Ing. Software: Jaime R. Camargo Z.
 *= Fecha: Mar 17 2003
 *= Modif:
 *===============================================================================
 *=
 *= Argumentos:
 *=   Obj   = Objeto del DOM a ocultar o mostrar. (o)
 *=
 *= Retorna:
 *=   False = El parámetro Obj no es un objeto.
 *=
 *= Ejemplo:
 *= SwitchDisplay(document.getElementById("tblList"))
 *===============================================================================*/
function SwitchDisplay(Obj)
{
  if (typeof(Obj) != "object") return false;

  with (Obj.style)
    display = (display=="") ? "none" : "";

  return true;
}

/*===============================================================================
 *= Ing. Software: Jaime R. Camargo Z.
 *= Fecha: Mar 17 2003
 *= Modif:
 *===============================================================================
 *=
 *= Argumentos:
 *=   Number   = Número a formatear. (a|n)
 *=
 *= Retorna:
 *=   Número sin formato de miles.
 *=
 *= Ejemplo:
 *= <INPUT type="text" onchange="this.value=UnformatNumber(this.value,0);">
 *===============================================================================*/
function UnformatNumber(Number)
{
  if (typeof(Number) != "string" && typeof(Number) != "number") return 0;

  Number = Number.toString();

  if (!Number.length) return "0";

  //Eliminar espacios
  Number = Number.replace(/ /g,"");

  return Number.replace(/,/g,"");
}//UnformatNumber()