function muestro(){ 
resul='';
resul_text = '';
resulp='';
opreal = '=';
for (i=0; i < document.form1.elements['TIPO_DOC'].length; i++)
{
if (document.form1.elements['TIPO_DOC'].options[i].selected && document.form1.elements['TIPO_DOC'].options[i].value != '')
{
if (resul.length > 0 )
   {
   resul += ', ' ;
   resul_text += ', ';
   opreal = ' in ';
   }
 resul += '"' + document.form1.elements['TIPO_DOC'].options[i].value + '"';
 resul_text +=  document.form1.elements['TIPO_DOC'].options[i].text;
}
}
m_lista = resul;
if (opreal == ' in ')
   {
     resul = '(' + resul + ')';
   }
if (resul.length > 0 )
   {   
 	 dsp_oper = ' es ';
     agrecon2(resul, resul_text, opreal, dsp_oper, 'TIPO', 'Tipo/s de Documento');
   }
   return;
}

function agrecondicion1() {
var vdesde = '';
var vhasta = '';
var alias = 'DOCU';
var m_lista = '';
var	 m_and_or = ' \) y  \(';
var	 m_blancos = ' ';
var	 c_and_or = ') and (';
muestro(); /* Carga el Tipo de Documento si Corresponde */
if (document.form1.NRO_DOC.value != '')
    {
      resul_text = document.form1.NRO_DOC.value;
      resul = resul_text;
	  opreal = '=';
	  agrecon2(resul, resul_text, opreal, 'igual a', 'NUMERO', 'Nº Documento');
	 } 

if (document.form1.RESUMEN.value != '')
    {
      resul_text = document.form1.RESUMEN.value;
      resul = ltrim(rtrim(resul_text));
	  if (document.form1.elements['OPRE'].options[0].selected )
	      {
	       opreal = document.form1.elements['OPRE'].options[0].value;
		   mensa =  document.form1.elements['OPRE'].options[0].text;
		  }
		  else
		      { 
		       opreal = document.form1.elements['OPRE'].options[1].value;
		       mensa =  document.form1.elements['OPRE'].options[1].text;
              }
	  agrecon2(resul, resul_text, opreal, mensa, 'TEMA_GRAL', 'El Resúmen');
	 } 	 
document.getElementById("textcondicion").innerHTML="<b><u>Buscará:</u></b> " + document.form2.condicion.value ;	 
}
function agrecon2(resul, resul_text, opreal, dsp_oper, campo, nombre) {
var alias = 'DOCU';
var m_lista = '';
var	 m_and_or = ' \) y  \(';
var	 m_blancos = ' ';
var	 c_and_or = ') and (';					   
if (document.form2.condicion.value  != '' ) {
		document.form2.condicion.value +=  m_and_or;
    }
if  (document.form2.condicion.value == '' ) {
	  document.form2.condicion.value = '( ' + nombre   + '  ' +  dsp_oper  + '  ' +  resul_text;
      } else {
	   document.form2.condicion.value +=  m_blancos + nombre  + '  ' +  dsp_oper  + '  ' +  resul_text;
}
document.getElementById("textcondicion").innerHTML="<b><u>Buscará:</u></b> " + document.form2.condicion.value;
if (document.form2.ejecutar.value   != '' ) {
		 document.form2.ejecutar.value  +=  c_and_or;
	} else
	     {
	      document.form2.ejecutar.value  = '  ';
         }

 	  if ( opreal == 'LP' || opreal == 'AP' || opreal == 'LF')
 	 	 {
           extrae_palabras(resul, opreal);
 	 	  } else {
   document.form2.ejecutar.value  +=   alias +  '.' + campo  +  ' ' +  opreal  + ' ' + resul;
 
                  }
return;
    }


function fnumero1(desde, longi, minlon)
{
desde.value=esnumero(desde.value,longi,minlon);
}
function fnumero2(hasta, longi, desde, minlon)
{
hasta.value=esnumero(hasta.value,longi,minlon);
if ( parseInt(hasta.value) < parseInt(desde.value))
   {
     alert('Error en Rango de números');
	} 
}
function fchar1(desde, longi)
{
desde.value=eschar(desde.value,longi,1);
}
function esnumero(valor, longi, minlon)
{
  var lon = longi;
  var mini = minlon;
  var  nume = valor;
 
  if (isNaN (nume))
    { alert('Error: el campo debe ser numerico');
      return nume; }
    else {
  if (nume.length > lon)
     {
      alert('Error: longitud max. ' + lon );
      return nume;
    }
     else { if (nume.value < mini)
                {
                  alert('Error valor mínimo: ' + mini)
                  return nume;
                }
          }
return nume;
         }
}

function esfecha (campo) {
   var sFecha = campo;
   var vFecha = sFecha.split ('/');
 
   if (vFecha.length == 1) {
          vFecha = sFecha.split ('-');
      }
   if (vFecha.length == 3 )
      { var iDia = vFecha [0];
        var iMes = vFecha [1];
		var iYear = vFecha [2];}
      else {
           if (vFecha.length == 1  && (sFecha.length == 6 || sFecha.length == 8) )             {
                var iDia = sFecha.substr(0,2);
                var iMes = sFecha.substr(2,2);
                var iYear = sFecha.substr(4);
              }
              else
                   { alert('Error en Fecha');
                     return sFecha; }
            }

if (iYear.length == 2 && iYear < 50)
   { iYear = '20' + iYear;}
   else {
         if (iYear.length == 2 && iYear >= 50)
            { iYear = '19' + iYear;}
        }
if (iYear.length != 4 )
   { alert('Error en Año');
     return sFecha;
   }
if (isNaN (iDia) | isNaN (iMes) | isNaN (iYear)) {
	alert('Error en Fecha');
    return sFecha;
    }
 switch (iMes) {
               case '01':
               case '03':
               case '05':
               case '07':
               case '08':
               case '10':
               case '12': {
                  if  ((iDia > 31) || (iDia <= 0))
				      {
                        alert('Error en Día');
                        return sFecha;
			    	  }
				   break;
               }
               case '04':
               case '06':
               case '09':
               case '11': {
                     if  ((iDia > 30) || (iDia <= 0))
				      {
                        alert('Error en Día');
                        return sFecha;
			    	  }
                     break;
                    }
               case '02': {
                  if ((iDia <= 28) || ((iDia == 29) && (((iYear % 400) == 0) || ((iYear % 4) ==0) && ((iYear % 100) != 0)))){
                      break;
                     }
                      else
                         { alert ('Error en Dia');
                           return sFecha;
                          }
                        }
               default: alert ('Error en Mes' );
                        return sFecha;

            }
 var resul = iDia + '/' + iMes + '/' + iYear;
 
 return resul;
}
function eschar(valor, longi, minlon)
{
  var lon = longi;
  var mini = minlon;
  var  nume = valor;


  if (nume.length > lon)
     {
      alert('Error: longitud max. ' + lon );
      return nume;
    }
     else { if (nume.length < mini)
                {
                  alert('Error longitud min. ' + mini)
                  return nume;
                }
          }
         return nume;
}
function ltrim(cadena) {
  var scadena = '';
  for (i=0; i < cadena.length; i++)
       {
         if ( cadena.charAt(i) != ' ')
             {
             scadena = cadena.substr(i);
             i = cadena.length;
             }
       }
  return scadena;
 }
function rtrim(cadena) {
   var scadena = '';
  for (i=(cadena.length - 1); i >= 0; i--)
       {
         if ( cadena.charAt(i) != ' ')
             {
              scadena = cadena.substr(0,i+1);
              i= -1;
             }
       }
  return scadena;
 }
 
function translate(palabra, patron1, patron2)
{
  var resul = '';
var entra= 'x' + palabra.toUpperCase() + 'x';
for(i=0; i<patron1.length; i++)
   {
    var par = new Array();
    par = entra.split(patron1[i]);
    resul = '';
    for (k= 0; k < par.length ; k++)
         {
          if (k == (par.length - 1))
              {
                resul += par[k];
              }
              else
                  {
                   resul += par[k] + patron2[i];
                   }
         }
       entra = resul;
    
     }
var longi = resul.length - 2;
var sale = resul.substr(1, longi);
return sale;
}
function extrae_palabras(frase, operador)
{
  var	  vtexto   = '';
  var	  p_item   = 'TEMA_GRAL';
  patron1 = new Array('Á','É','Í','Ó','Ú');
  patron2 = new Array('A','E','I','O','U');
  vtexto = translate(frase,patron1, patron2);
  palabras = new Array();
  palabras = vtexto.split(' ');
  for (i = 0; i < palabras.length; i++)
      {
       if (i == 0 )
          {
          document.form2.ejecutar.value  +=  '\(' + p_item  +  ' regexp \'.*' + palabras[i] +  '.*\'';
          }
         else {
 	           if (operador  == 'LP')
                  {
                  document.form2.ejecutar.value  +=  ' and ' + p_item +  ' regexp \'.*' + palabras[i] +  '.*\'';
 	    	 	  } else {
	                  document.form2.ejecutar.value  +=  ' or '  + p_item +  ' regexp \'.*' + palabras[i] +  '.*\'';
                         } 
               }
      }
  document.form2.ejecutar.value  +=  '\)';
  return;
}
function valida()
{
agrecondicion1();
  if (document.form2.ejecutar.value != '')
  {
      abro('resul_dig.php?ejecutar='+ escape(document.form2.ejecutar.value)+'&tablas='+escape(document.form2.tablas.value),'','width=620,height=400,scrollbars=yes, menubar=yes ,resizable=yes, location=no');

  }
  else
     { alert(' No Ingresó Condiciones de Búsqueda');
	 return; }
	 return false;
}
function restaura()
{
  document.form2.ejecutar.value ='';
  document.form2.condicion.value ='';
  document.form2.tablas.value ="dgnorma as DOCU";
  document.getElementById("textcondicion").innerHTML='';
  document.form1.TIPO_DOC.value ='';
  document.form1.NRO_DOC.value ='';
  document.form1.RESUMEN.value ='';


  return;
}
