//***-------------- INICIO JAIME CAMARGO
/*===============================================================================
 *= 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;

  if (nKey == 13) return;

  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;

  if (nKey == 13) return;

  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;

  if (nKey == 13) return;

  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 buscar. (a)
 *=
 *= Retorna:
 *=   true     = encontrado.
 *=   false    = no encontrado.
 *=
 *= Ejemplo:
 *= <SCRIPT language="javascript">if (FindTextInDocument("abc")) alert("Se encontró el texto");</SCRIPT>
 *===============================================================================*/
function FindTextInDocument(Text,MessageNotFound)
{
  //el texto a buscar es obligatorio
  if (typeof(Text) != "string") return false;
  if (!Text.length) return false;

  if (typeof(MessageNotFound) != "string")
    MessageNotFound = "";

  var bFound = false;

  try
  {
    //IE?
    if (document.all)
    {
      var oRange = document.body.createTextRange();

      bFound = oRange.findText(Text);
      if (bFound) oRange.select();
    }//if
    else
      bFound = window.find(Text);

    if (bFound == false && MessageNotFound.length > 0)
      alert(MessageNotFound);
  }//try
  catch (e)
  {
    alert("No se pudo efectuar la búsqueda\nError: " + e.toString());
  }//catch

  return bFound;
}//FindTextInDocument()

/*===============================================================================
 *= 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 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: 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(Year + "/" + Month + "/" + Day);

	with (MyDate)
		if (getFullYear() != Year || getMonth() != --Month || getDate() != Day) return false;

	return true;
}//IsDate()

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})+$/);
	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: Marzo 2004
 *= Modif:
 *=        Noviembre 29 2004
 *=          Soporte para <TEXTAREA>
 *=        Enero 24 2005
 *=          Soporte para <INPUT type="password"/>
 *===============================================================================*/
function IsValidFormData(Form)
{
  var o  = null;
  var dv = ""; //delimiter value
  var df = ""; //date format
  var vl = ""; //values
  var dt = ""; //datatype
  var rq = ""; //required
  var ml = ""; //minimum length
  var ms = ""; //error message
  var mv = ""; //validation message
  var mc = "Dato incompleto o erróneo";

  with (Form)
    for (var i=0; i<elements.length && !mv.length; i++)
    {
      o = elements[i];

      //is this control a textbox, combo, options and is it required
      if (o.type == "text" || o.type == "select-one" || o.type == "radio" || o.type == "textarea" || o.type == "password")
      {
        //is visible this object?
        if (!IsVisible(o)) continue;

        dv = o.getAttribute("DelimiterValue");
        df = o.getAttribute("DateFormat");
        vl = o.getAttribute("Values");
        rq = o.getAttribute("Required");
        ms = o.getAttribute("Message");
        ml = o.getAttribute("MinLength");

        //verify the date format. by default is year/month/day
        df = (df != null) ? df.toUpperCase() : "YMD";
        if (df != "YMD" && df != "DMY" && df != "MDY") df = "YMD";

        //if the REQUIRED attribute is not specified or it's disabled or readonly then it's false
        if (rq == null || o.disabled == true || o.readOnly == true)
          rq = "false"
        // is boolean type REQUIRED attribute?
        else if (typeof(rq) == "boolean")
          rq = (rq) ? "true" : "false";

        //is there message?
        if (ms == null) ms = "";
        //is there minimum length?
        if (ml != null)
        {
          if (IsNumeric(ml))
            ml = parseInt(ml);
        }
        else
          ml = 0;

        //is required?
        if (rq.toLowerCase() == "true")
        {
          if (o.type == "radio")
          {
            var op = elements[o.name];

            //is there only an option?
            if (typeof(op.length) == "undefined")
            {
              //isn't this option selected?
              if (!op.checked) mv = ((ms.length) ? ms : "No ha seleccionado una opción");
            }//if
            else
            {
              var j;

              //find for one selected option
              for (j=0; j<op.length; j++)
                if (op[j].checked) break;

              //aren't there any options selected?
              if (j == op.length)
                mv = ((ms.length) ? ms : "No ha seleccionado una opción");
              else
                i += op.length - 1;
            }//else
          }//if
          else if (!o.value.length)
            mv = ((ms.length) ? ms : "El dato no es correcto");
        }//if

        // is correct the length?
        if ((o.type == "text" || o.type == "password" || o.type == "textarea") && ml && o.value.length < ml)
          mv = "Se exige un mínimo " + ml + " caracteres";

        //isn't there problem?
        if (!mv.length)
        {
          dt = o.getAttribute("DataType");
          if (dt != null && o.value.length > 0)
            switch (dt.toLowerCase())
            {
              case "number":
                if (!IsNumeric(o.value)) mv = ((ms.length) ? ms : "El valor no es válido"); break;
              /*
              case "formattednumber":
                if (!IsNumeric(UnformatNumber(o.value))) mv = ((ms.length) ? ms : mc); break;
              */
              case "date":
                if (!IsDate(o.value,df)) mv = ((ms.length) ? ms : "La fecha no es válida"); break;
              case "email":
                if (!IsEmail(o.value)) mv = ((ms.length) ? ms : "El email no es válido");
            }//switch

          //are there values?
          if (vl != null && !mv.length && o.value.length)
          {
            var va; //value array

            if (dv == null) dv = "|";

            va = vl.split(dv);

            for (var k=0; k<va.length; k++)
              if (o.value == va[k])
                break;

            //isn't a correct value?
            if (k == va.length) mv = ((ms.length) ? ms : "El valor no es correcto"); //mv = "El valor digitado ó seleccionado no es el esperado";
          }//
        }//if
      }//if
    }//for

  if (mv.length)
  {
    alert(mv);
    try{o.focus();o.select();}catch(e){}
    return false;
  }//if

  return true;
}//IsValidFormData()

/*===============================================================================
 *= Ing. Software: Jaime R. Camargo Z.
 *= Fecha: Mar 28 2005
 *= Modif:
 *===============================================================================
 *=
 *= Argumentos:
 *=   Object = Objeto HTML a verificar si está visible. (a,o)
 *=
 *= Retorna:
 *=   true     = El objeto es visible.
 *=   false    = El objeto o su contenedor no está visible.
 *=
 *= Ejemplo:
 *= <SCRIPT language="javascript">if (IsVisible("IdObjetoHtml")) alert("Está Visible");</SCRIPT>
 *= <SCRIPT language="javascript">if (IsVisible(document.getElementById("IdObjetoHtml"))) alert("Está Visible");</SCRIPT>
 *===============================================================================*/
function IsVisible(Object)
{
  if (typeof(Object) == "undefined") return false;

  var o = null;

  try
  {
    if (typeof(Object) == "string")
      o = document.getElementById(Object);
    else
      o = Object;

    for(var p = o; p; p=p.parentNode)
    {
      if (p.style)
        if (p.style.display == "none")
          return false;
    }//for

    return true;
  }//try
  catch (e)
  {
    return false;
  }//catch
}//IsVisible()

function ShowObject(Object, Show)
{
  if (typeof(Object) == "undefined") return false;
  if (typeof(Show)   != "boolean")   return false;

  var o = null;

  try
  {
    if (typeof(Object) == "string")
      o = document.getElementById(Object);
    else
      o = Object;

    o.style.display = (Show) ? "" : "none";
  }//try
  catch (e)
  {
    return false;
  }//catch

  return true;
}//ShowObject()

/*===============================================================================
 *= 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:
 *=   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()
