
function esfecha(entr) {
   var numeros, n;
   var dd, mm, mmll, aaaa;

   entr=""+entr;
   
   if(entr.length!=10) { return 0; }

   // Primero comprobar que solo tiene los caracteres apropiados 
   numeros="0123456789"
   n=numeros.indexOf(entr.charAt(0));
   if(n==-1) { return 0; } 
   n=numeros.indexOf(entr.charAt(1));
   if(n==-1) { return 0; } 
   n=numeros.indexOf(entr.charAt(3));
   if(n==-1) { return 0; } 
   n=numeros.indexOf(entr.charAt(4));
   if(n==-1) { return 0; } 
   n=numeros.indexOf(entr.charAt(6));
   if(n==-1) { return 0; } 
   n=numeros.indexOf(entr.charAt(7));
   if(n==-1) { return 0; } 
   n=numeros.indexOf(entr.charAt(8));
   if(n==-1) { return 0; } 
   n=numeros.indexOf(entr.charAt(9));
   if(n==-1) { return 0; }

   if(entr.charAt(2)!="/") {return 0; }
    
   if(entr.charAt(5)!="/") {return 0; }

   // Comprobar el rango de dias y meses y año (no anterior a 1800)

   dd=parseInt(treurezerosdevant(entr.substring(0,2)));
   if(dd<1 || dd>31) { return 0;}
   
   mmll=entr.substring(3,5);
   mmll=treurezerosdevant(mmll);
   mm=parseInt(mmll);
   /*if(mmll=="08"){
      mm=8;
   }
   else
   {
      if(mmll=='09') {
          mm=9;
      }
   }*/
   if(mm<1 || mm>12) { return 0; }
 
   aaaa=parseInt(treurezerosdevant(entr.substring(6,10)));
   if(aaaa<1800 || aaaa>2050) { return 0; }

   switch(mm) { 
   
     case 2: 
             if((aaaa%4==0 && aaaa%400!=0) || aaaa==2000){
                  if(dd>29) { return 0; }
             }
             else
             {
                  if(dd>28) { return 0; }
             }

     case 4:
             if(dd>30) { return 0; }
 
     case 6:
             if(dd>30) { return 0; }
 
     case 9:
             if(dd>30) { return 0; }
 
     case 11:
             if(dd>30) { return 0; }
 
   }
   return 1;
}

function esanyproper(entr) {

   var n;

   entr=""+entr
   entr=rtrim(ltrim(entr))

   if(entr.length!=4) { return 0; }

   // Primero comprobar que solo tiene los caracteres apropiados 
   numeros="0123456789"
   n=numeros.indexOf(entr.charAt(0));
   if(n==-1) { return 0; } 
   n=numeros.indexOf(entr.charAt(1));
   if(n==-1) { return 0; } 
   n=numeros.indexOf(entr.charAt(2));
   if(n==-1) { return 0; } 
   n=numeros.indexOf(entr.charAt(3));
   if(n==-1) { return 0; }

   if(parseInt(entr)>2050 || parseInt(entr)<1000) { return 0; }
 
   return 1;
}

function ltrim(entr) {
 
   var larg, i, sal;

   entr=""+entr;
   larg=entr.length;
   if(larg==0) { return entr; }

   sal="";
   i=0;
   while(entr.charAt(i)==" "){ 
      i++;
   }
   
   while(i<larg) {

     sal=sal+entr.charAt(i);
     i++
 
   }   

   return sal;

}
   

function rtrim(entr) {

   var larg, sal, i;

   entr=""+entr;
   larg=entr.length
   if(larg==0) { 
      return entr
   }

   sal=""
   i=larg-1
   while(entr.charAt(i)==" ") { 
     i--
   }

   while(i>=0) {

      sal=entr.charAt(i) + sal
      i--
 
   }

   return sal

}

function contains(smstring,lrgstring) {

        var strlen1, strlen2, istrue, i, comp;

        smstring=""+smstring;
        lrgstring=""+lrgstring;

        //devuelve true si lrgstring contiene smstring.

        strlen1 = smstring.length

        strlen2 = lrgstring.length

        istrue = false  

        for (i=0;i<=strlen2;i++) {

                comp=lrgstring.substring(i-1,strlen2)

                comp = comp.substring(0,strlen1)                

                if (comp == smstring) {

                        istrue = true

                        break

                }

        }

        return istrue

}


function esnumeroentero(cadena) {
     // En realidad mira si es numero natural !!!

     var ref, larg, x, letra, cont;

     ref="0123456789";
     cadena=""+cadena;
     larg=cadena.length;

     x=0;
     letra=cadena.substring(x,x+1);
     cont=contains(letra,ref);

     while(x<larg && cont) {
         x++;
         letra=cadena.substring(x,x+1);
         cont=contains(letra,ref);
     }
     
     if(x<larg) {
       return 0;
     }
     else
     {
       return 1;
     }
}

function treurezerosiespaisdevant(entr) {
   
   var larg, sal, i;

   entr=""+entr;
   larg=entr.length;
   if(larg==0) { return entr; }

   sal="";
   i=0;
   while(entr.charAt(i)=="0" || entr.charAt(i)==" "){ 
      i++;
   }
   
   while(i<larg) {

     sal=sal+entr.charAt(i);
     i++
 
   }
   
   if(sal=="") { sal="0"; }   

   return sal;

}

function treurezerosdevant(entr) {
   
   var larg, sal, i;
   
   entr=""+entr;
   larg=entr.length;
   if(larg==0) { return entr; }

   sal="";
   i=0;
   while(entr.charAt(i)=="0"){ 
      i++;
   }
   
   while(i<larg) {
     sal=sal+entr.charAt(i);
     i++
   }
   
   if(sal=="") { sal="0"; }   

   return sal;

}

function avuidb() {

     var actual, any_act, mes_act, aux, dia_act, fech_act;

     actual= new Date();
     any_act= actual.getYear();
     if(any_act<=1000){
       any_act+=1900;
     }
     any_act_car=""+any_act;
     mes_act= actual.getMonth()+1 ;
     aux="";
     if(mes_act<10)
     {
      aux="0";
     }
     dia_act= actual.getDate();
     aux2="";
     if(dia_act<10) {
         aux2="0";
     }
     fech_act=""+any_act+aux+mes_act+aux2+dia_act;
     return fech_act;
}

function avuivisual() {

     var actual, any_act, mes_act, aux, dia_act, aux, aux2, fech_act;

     actual= new Date();
     any_act= actual.getYear();
     if(any_act<=1000){
       any_act+=1900;
     }
     any_act_car=""+any_act;
     mes_act= actual.getMonth()+1 ;
     aux="";
     if(mes_act<10)
     {
      aux="0";
     }
     dia_act= actual.getDate();
     aux2="";
     if(dia_act<10) {
          aux2="0";
     }
     fech_act=""+aux2+dia_act+"/"+aux+mes_act+"/"+any_act;
     return fech_act;
}

function trim(entr)
{
    sal=ltrim(rtrim(entr));
    return sal;
}

function datadb(entr)
{
    var sal;
    entr=""+entr;
    sal=entr.substring(6,10)+entr.substring(3,5)+entr.substring(0,2);
    return sal;
}

function enter(entr)
{
  var sal;

  entr=""+entr;
  entr=treurezerosdevant(trim(entr));
  sal=parseInt(entr);

  return sal;
}

function isbn_corr(isbn)
{
   var isbn9, ult;

   if(isbn.length!=10){ return 0; }

   isbn9=isbn.substring(0,9);
   ult=isbn.substring(9,10);
   if(!esnumeroentero(isbn) && (!esnumeroentero(isbn9) || (ult!='x' && ult!='X'))){
         return 0;
   }

   return 1;
}

function sustit(carentr,carsal,entr)
{
     
     // Retorna una cadena que es com 'entr' però sustitutint tots els caracters 'carentr' per 'carsal':
     
     var sal, lon, i;

     carentr=""+carentr;
     carsal=""+carsal;
     entr=""+entr;
     sal="";

     lon=entr.length;

     i=0;
     while(i<lon){
          if(entr.substring(i,i+1)==carentr){
             sal+=carsal;
          }
          else
          {
             sal+=entr.substring(i,i+1);
          }
          i++;
     }
     return sal;
}

function spot(smstring,lrgstring) {

    var strlen1, strlen2, foundat, i, comp;

    //returns a number indicating the spot where smstring appears in lrgstring, or -1 if it does not appear:
	strlen1 = smstring.length
	strlen2 = lrgstring.length
	foundat = -1;
	i=0;
	while(i<strlen2) {
		comp=lrgstring.substring(i,i+strlen1)		
		if (comp == smstring) {
			foundat = i
			break
		}
		i++;
	}
	return foundat
}

function ponermayus (paraula) {
 var ini, fni, tama, largo, pasado, x, incid;

 ini="abcdefghijklmnñopqrstuvwxyzáéíóúàèìòùäëïöüâêîôûç\"";
 fni="ABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚÀÈÌÒÙÄËÏÖÜÂÊÎÔÛÇ'";
 tama=paraula.length;
 largo=ini.length;
 pasado="";
 x=0;
 while (x<tama){

    letra=paraula.substring(x,x+1);
    incid=spot(letra,ini);
    if (incid!=-1){
       pasado+=fni.substring(incid,incid+1);
    }
    else
    {
       pasado+=letra;
    }
    x++;
 }
 return pasado;
};

function ponernorm (paraula) {
 var ini, fni, tama, largo, pasado, x, incid;

 ini="ºªabcdefghijklmnñopqrstuvwxyzáéíóúàèìòùäëïöüâêîôûçABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚÀÈÌÒÙÄËÏÖÜÂÊÎÔÛÇ\"";
 fni="OAABCDEFGHIJKLMNÑOPQRSTUVWXYZAEIOUAEIOUAEIOUAEIOUÇABCDEFGHIJKLMNÑOPQRSTUVWXYZAEIOUAEIOUAEIOUAEIOUÇ'";

 tama=paraula.length;
 largo=ini.length;
 pasado="";
 x=0;
 while (x<tama){

    letra=paraula.substring(x,x+1);
    incid=spot(letra,ini);
    if (incid!=-1){
       pasado+=fni.substring(incid,incid+1);
    }
    else
    {
       pasado+=letra;
    }
    x++;
 }
 return pasado;
};