var lan = "ESP";
var msg = {
            FLT: {
                   ESP: "El valor introducido ha de ser un número (ejemplo 1234.90).",
                   ENG: "The typed value must be number (example 1234.90).",
                   DFL: "El valor introducido ha de ser un número (ejemplo 1234.90)."
            },
            PER: {
                   ESP: "El valor introducido ha de ser un número (ejemplo 1234.90).",
                   ENG: "The typed value must be number (example 1234.90).",
                   DFL: "El valor introducido ha de ser un número (ejemplo 1234.90)."
            },
            MON: {
                   ESP: "El valor introducido ha de ser un número (ejemplo 1234.90).",
                   ENG: "The typed value must be number (example 1234.90).",
                   DFL: "El valor introducido ha de ser un número (ejemplo 1234.90)."
            },
            INT: {
                   ESP: "El valor introducido ha de ser un número sin decimales (número entero).",
                   ENG: "The typed value must be a non-decimal number (integer number).",
                   DFL: "El valor introducido ha de ser un número sin decimales (número entero)."
            },
            DTE: {
                   ESP: "El valor introducido no es una fecha correcta (formato: dd/mm/aaaa).",
                   ENG: "The typed value is not a correct date (format: dd/mm/yyyy).",
                   DFL: "El valor introducido no es una fecha correcta (formato: dd/mm/aaaa)."
            },
            IDE: {
                   ESP: "El valor introducido ha de ser un valor alfanumérico (sólo letras y números).",
                   ENG: "The typed value must be an alphanumeric value (only letters and numbers).",
                   DFL: "El valor introducido ha de ser un valor alfanumérico (sólo letras y números)."
            },
            STR: {
                   ESP: "El símbolo \" (doble comilla) no esta permitido. Por favor, use otro símbolo alternativo.",
                   ENG: "The symbol \" (double quotation) is not allowed. Please, use an alternative one.",
                   DFL: "El símbolo \" (doble comilla) no esta permitido. Por favor, use otro símbolo alternativo."
            },
            TXT: {
                   ESP: "El símbolo \" (doble comilla) no esta permitido. Por favor, use otro símbolo alternativo.",
                   ENG: "The symbol \" (double quotation) is not allowed. Please, use an alternative one.",
                   DFL: "El símbolo \" (doble comilla) no esta permitido. Por favor, use otro símbolo alternativo."
            },
            COMMON: {
                   ESP: "Si no lo corrije, se restaurará a su valor inicial. ¿Desea corregirlo ahora?",
                   ENG: "If you do not retype it, it will be reset to the initial value. Do you with to change it now?",
                   DFL: "Si no lo corrije, se restaurará a su valor inicial. ¿Desea corregirlo ahora?"
            },
            REQUIRED: {
                   ESP: "Es obligatorio introducir un valor (campo requerido).",
                   ENG: "You must type a value, is a required field.",
                   DFL: "Es obligatorio introducir un valor (campo requerido)."
            },
            
            MAXOPTIONSEXCEED: {
                   ESP: "Sobrepasó el número máximo de elementos seleccionados permitidos.",
                   ENG: "You have exceeded the maximum selected items allowed.",
                   DFL: "Sobrepasó el número máximo de elementos seleccionados permitidos."
            },
            
            NOOPTIONSELECTED: {
                   ESP: "No se ha seleccionado ningún elemento.",
                   ENG: "No item was selected.",
                   DFL: "No se ha seleccionado ningún elemento."
            }
          }
          
function dinamicWindowOpen(x, y, width, height)
{
  var w = 20, h = 20, iw = 10, ih = 10;
  
  window.focus();
  if (width > screen.width-50)   { width  = screen.width-50;  x=10; y=10; }   // ADD-20050307-1621
  if (height > screen.height-50) { height = screen.height-50; x=10; y=10; }   // ADD-20050307-1621
  if (!window.document.referrer) {  // ADD-20050307-1625
    window.moveTo(x,y);
    while ((w<width)||(h<height)) {
      w+=iw;
      h+=iw;
      if (w>width)  w=width;
      if (h>height) h=height;
      window.resizeTo(w,h);
    }
  }
  window.resizeTo(width,height);
}

function _MON2FLT(v)
{ // Convierte un valor MON D.DDD,DD en valor FLT DDDD.DD
  var r = v + "";
  
  /*
  if (r.indexOf('.')<r.length-3) { // El punto delimita miles
    while (r.indexOf('.')>=0) r = r.replace('.','');
    while (r.indexOf(',')>=0) r = r.replace(',','.');
  }
  */
  /**/
  while (r.indexOf(',')>=0) r = r.replace(',','');
  /**/
  return r;
}

function _dataTypeCheck(n,t,ff)
{ // comprueba que si v no es vacío, posea un valor acorde al formato.
  var d = "0123456789", ide = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  var r, c, c2, l, dma, m, a, f, v, s, sgn="";

  v = ff.value + "";
  s = v + "";
  if (s.length==0) r = "OK";
  else {
    if ((t=="FLT")||(t=="PER")) {
      r = "NOK";
      while (v.indexOf(',')>=0) v = v.replace(',','.');
      f = new RegExp("[0-9]+([.][0-9]+)?");
      s = v.replace(f,"");
      if (s.length==0) {
        r = "OK";
        ff.value = v;
      }
    }
    else if (t=="MON") {
      r = "NOK";
      // Delimito a dos decimales.
      /*if (v.indexOf('.')<v.length-3) { // El punto delimita miles
        while (v.indexOf('.')>=0) v = v.replace('.','');
        while (v.indexOf(',')>=0) v = v.replace(',','.');
      }*/
      // La coma se cambia por punto en caso de existir.
      /*
      while (v.indexOf(',')>=0) v = v.replace(',','.');
      f = new RegExp("[0-9]+([.][0-9]+)?");
      s = v.replace(f,"");
      if (s.length==0) {
        r = "OK";
        s = Math.round(v*100).toString();
        if (s.length<3)
          if (s.length<2) s = "00" + s;
          else            s = "0" + s;

        var htmlValue = "," +s.substr(s.length-2);
        var sAux = s.substr(0,s.length-2)+""
        while (sAux.length>3) {
            htmlValue = "." + sAux.substr(sAux.length-3,3) + htmlValue;
            sAux = sAux.substr(0,sAux.length-3);
        }
        htmlValue = sAux + htmlValue + "";
        ff.value = htmlValue + "";
        
        //ff.value = s.substr(0,s.length-2)+"."+s.substr(s.length-2);
      }
      */
      /**/
      while (v.indexOf(',')>=0) v = v.replace(',','.');
      f = new RegExp("[+-]?[0-9]+([.][0-9]+)?");
      s = v.replace(f,"");
      if (s.length==0) {
        r = "OK";
        sgn = "";
        if (Math.round(v*100)<0) sgn = "-";
        s = Math.abs(Math.round(v*100)).toString();
        if (s.length<3)
          if (s.length<2) s = "00" + s;
          else            s = "0" + s;

        var htmlValue = "." +s.substr(s.length-2);
        var sAux = s.substr(0,s.length-2)+""
        while (sAux.length>3) {
            htmlValue = "," + sAux.substr(sAux.length-3,3) + htmlValue;
            sAux = sAux.substr(0,sAux.length-3);
        }
        htmlValue = sAux + htmlValue + "";
        ff.value = sgn + htmlValue + "";
        
        //ff.value = s.substr(0,s.length-2)+"."+s.substr(s.length-2);
      }
      /**/
    }
    else if (t=="INT") {
      c2=v; l=c2.length;
      r = "OK";
      for (c=0; c<l; c++) if (d.indexOf(c2.charAt(c)) == -1) r = "NOK";
      if (r=="OK") {
        r = "OK";
        ff.value = c2;
      }
    }
    else if (t=="DTE")
    {
      s = v;
      while (s.indexOf('-')>=0) s = s.replace('-','/');
      while (s.indexOf('.')>=0) s = s.replace('.','/');
      if (s.length==6) s = s.substr(0,2) + "/" + s.substr(2,2) + "/" + s.substr(4,2);
      dma=s.split('/')
    
      d=parseInt(dma[0],10);
      m=parseInt(dma[1],10);
      a=parseInt(dma[2],10);
      if (a<1000) a=a+2000;
      if (a>9999) a=9999;
      
      f=new Date(a,m-1,d)
      if (isNaN(f) || f.getDate()!=d || f.getMonth()!=(m-1) || f.getFullYear()!=a)
        r = "NOK";
      else {
        r = "OK";
        ff.value = d + '/' + m + '/' + a;
      }
    }
    else if (t=="IDE")
    {
      s = s.toUpperCase();
      /*while (s.indexOf('-')>=0) s = s.replace('-','');
      while (s.indexOf('.')>=0) s = s.replace('.','');
      while (s.indexOf('/')>=0) s = s.replace('/','');*/
      while (s.indexOf(' ')>=0) s = s.replace(' ','');
      c2 = s; l = c2.length;
      r = "OK";
      for (c=0; c<l; c++) if (ide.indexOf(c2.charAt(c)) == -1) r = "NOK";
      if (r=="OK") {
        r = "OK";
        ff.value = c2;
      }
    }
    else if ((t=="STR")||(t=="TXT")) {
      while (s.indexOf('"')>=0) s = s.replace('"',"'");
      r = "OK";
      ff.value = s;
    }
    else
    {
      r = "OK";
    }
  }
  if (r=="NOK") {
    if (n == "undefined")
      n = "";
    else
      n = "'"+n+"': ";
    alert(n + msg[t][lan]);
    if(confirm(msg["COMMON"][lan])) {
      ff.select();
      r = false;
    }
    else {
      ff.value = ff.defaultValue;
      r = true;
    }
  }
  return r;
}

// frm: Nombre del formulario virtual.
// f:   id del campo a chequear.
function _isEmpty(frm,f)
{
  // Falta comprobar si es un campo de opciones
  if (document.forms.form[f].value==""){
    alert(page().forms[frm].fields[f].label+": "+msg["REQUIRED"][lan]);
    return true;
  }
  else {
    return false;
  }
}

function _isEmpty2(n,f)
{
  // Falta comprobar si es un campo de opciones
  if (f.value==""){
    if (n == "undefined")
      n = "";
    else
      n = "'"+n+"': ";
    alert(n+msg["REQUIRED"][lan]);
    return true;
  }
  else {
    return false;
  }
}

// Chequea si el número de opciones del formulario en el campo especificado
// es menor o igual. Si es mayor o no hay opcion seleccionada, no deja pasar.
// f: campo a chequear.
// m: maximo de opciones.
function _hasSelections(f,m)
{ var n=m;
  if (document.forms.form[f].length) {
    for (var c = 0; c<document.forms.form[f].length; c++)
      if (document.forms.form[f][c].checked) n--;
    if (n<0) {
      alert(msg["MAXOPTIONSEXCEED"][lan]);
      return false;
    }
    if (n==m) {
      alert(msg["NOOPTIONSELECTED"][lan]);
      return false;
    }
    else {
      return true;
    }
  }
  else if (document.forms.form[f].checked) 
    return true;
  else {
    alert(msg["NOOPTIONSELECTED"][lan]);
    return false;
  }
}

// Devuelve la seleccion efectuada en un report o cadena vacia "" si no hay seleccion.
// f: referencia del campo (ej: document.forms.form.field1)
function _getSelectedIndex(f)
{
  var r = "", s="";
  if (f.length) {
    for (var c = 0; c<f.length; c++) {
      if (f[c].checked) {
        r = r + s + f[c].value;
        s = ",";
      }
    }
  }
  else {
    r = f.value;
  }
  return r;
}

// Establece la seleccion efectuada en un report a partir de un campo y un valor de recid.
// f: referencia del campo (ej: document.forms.form.field1)
// v: valor del campo (ej: 56)
function _setSelectedIndex(f,v)
{
  var r = "", s="";
  if (f.length) {
    for (var c = 0; c<f.length; c++) {
      if (f[c].value==v) {
        f[c].checked = true;
        f[c].onclick();
      }
    }
  }
}



// frm: Nombre del formulario virtual a chequear.
function _checkRequiredFields(frm)
{
  var r = true;
  if (page().forms[frm]) {
    // Es un formulario.
    for (var f in page().forms[frm].fields)
    {
      //alert("page().forms[frm].fields["+f+"].required="+page().forms[frm].fields[f].required);
      if (page().forms[frm].fields[f].required==true)
      { // Control de requerido. Miro si el campo en cuestión esta rellenado a un valor.
        //n=""+document.form[f].value;
        if (_isEmpty(frm, f))
        {
          r = (r && false);
        }
      }
    }
  }
  else if (page().reports[frm]) {
    // Es un informe. Compruebo que el máximo de opciones está permitido
    var n = page().reports[frm].maxOptions;
    if (!_hasSelections(frm+"_idFieldSelected",n)) {
      r = (r && false);
    }
  }
  else if (page().treeReports[frm]) { // BUG-20041227-1
    // Es un informe. Compruebo que el máximo de opciones está permitido
    var n = page().treeReports[frm].maxOptions;
    if (!_hasSelections(frm+"_idFieldSelected",n)) {
      r = (r && false);
    }
  }
  return r;
}

function _checkRequiredFieldsV2(f)
{
  var r=true, mf="", n;
  for (var f in document.form)
  {
    //alert("document.form."+f);
    mf = f+"";
    if (mf.substr(0,11)=="__required_")
    { // Control de requerido. Miro si el campo en cuestión esta rellenado a un valor.
      n=""+document.form[mf].value;
      mf = mf.substr(11);
      if (_isEmpty(n, document.form[mf]))
      {
        r = (r && false);
      }
    }
  }
  return r;
}
