
function ControlarFecha(Dame,Minimo,Maximo,Nombre)
{
var Fecha = new String(Dame);
if (Fecha=="") 
{
return true;
}
       
        var Topes = "312831303130313130313031"
        var Mensaje = Nombre + " es errónea ";
if ( (Fecha.length != 8) && (Fecha.length != 10) && (Fecha.length != 6))
  {
  
   alert(Mensaje);
   return false;
   }

if (Fecha.length == 10)
{
        if (Fecha.substring(2,3)  != '/' || Fecha.substring(5,6) != '/')
        {
         alert(Mensaje);
         return false;
        }
        else
        {
        var Apoyodia    = Fecha.substring(0,2)
        var Apoyomes    = Fecha.substring(3,5)
        var Apoyoano    = Fecha.substring(6)
        }
}
else
{
     var Apoyodia    = Fecha.substring(0,2)
     var Apoyomes    = Fecha.substring(2,4)    
        if (Fecha.length == 8)
        {
       
        var Apoyoano    = Fecha.substring(4)
        }
        else
        {
       
        var Apoyoano= '19' + Fecha.substring(4)
        }
}

        

        if ( (EsTodoNumero(Apoyodia)== false) || (EsTodoNumero(Apoyomes)== false) || ( EsTodoNumero(Apoyoano)== false) )
        {
                alert(Mensaje);
                return false;
        }
        /*
        if (Apoyoano < '1900')
        {
                alert(Mensaje);
                return false;
        }
        */
         if (Apoyoano > '2050')
        {
                alert(Mensaje);
                return false;
        }
        
        if ( (Apoyomes > '12') || (Apoyomes < '01') )
        {
        
                alert(Mensaje);
                return false;
        }

        var Febrerobis = '28';
        if (Apoyomes == '02')
        {
                if (VerBisiesto(Apoyoano))
                {
                        Febrerobis = '29'
                }
             
        }
        else
        {
                Febrerobis = Topes.substring((Apoyomes-1)*2, (Apoyomes-1)*2+2)
        }

        if ( (Apoyodia > Febrerobis) || (Apoyodia <'01') )
        {
                alert(Mensaje);
                return false;
        }


    
      if (Fecha=="")
      {
      return true;
      }

 
var devuelve =  new String(ComponerFecha(Fecha));    
var fechacompuesta =  devuelve.substring(6) + devuelve.substring(3,5) + devuelve.substring(0,2);
if (Minimo != 'N')
{
devuelve =  new String(ComponerFecha(Minimo));    
var minimocompuesto =  devuelve.substring(6) + devuelve.substring(3,5) + devuelve.substring(0,2);
//alert(fechacompuesta + ' ' + minimocompuesto)
   if (fechacompuesta < minimocompuesto)
   {
    alert(Nombre + ' no puede ser anterior a ' + devuelve);
    return false;
   }

} 

if (Maximo != 'N')
{
devuelve =  new String(ComponerFecha(Maximo));    
var maximocompuesto =  devuelve.substring(6) + devuelve.substring(3,5) + devuelve.substring(0,2);


   if (fechacompuesta  > maximocompuesto)
   {
    alert(Nombre + ' no puede ser posterior a ' + devuelve);
    return false;
   }

} 
return true;
}
function ComponerFecha(Dame)
{
var Fecha = new String(Dame);
if (Fecha.length == 10 || Fecha == "")
{
  return Fecha;
}  
else
{
 var Apoyodia    = new String(Fecha.substring(0,2));
 var Apoyomes    = new String(Fecha.substring(2,4));
 if (Fecha.length == 8)
 {
 var Apoyoano    = new String(Fecha.substring(4,8));
 }
 else
 {
 var Apoyoano    = '19'
 Apoyoano = Apoyoano + Fecha.substring(4,8);
 }

 return Apoyodia+"/"+Apoyomes+"/"+Apoyoano;
}

}

function VerBisiesto (Ano)
{
        if (((Ano % 4)==0) && ((Ano % 100)!=0) || ((Ano % 400)==0))
        {
          return (true);
        }
        else
        {
         return (false);
        }
}
function UnirFecha(Dia,Mes,Ano,Destino)
{
var strDia = new String(Dia.value)
var strMes = new String(Mes.value)
var strAno = new String(Ano.value)
var strSiglo = '2000'
  if (strDia.length == 1)
  {
   strDia = '0' + strDia;
  }
  if (strMes.length == 1)
  {
   strMes = '0' + strMes;
  }
  if (strAno.length > 0 && strAno.length < 4)
  {
   strAno = strSiglo.substring(0,4-strAno.length) + strAno;
  }
Destino.value = strDia + '/' + strMes + '/' + strAno
}

function Volver()
{
		history.back();
}


function EsTodoNumero (Dame,Label,Especial)
{

if (Dame == "")
{
return false;
}
for (var i=0 ; i < Dame.length; i++)
{
  if (Especial == "S")
  {
  if (Dame.substring(i,i+1)== "-" || Dame.substring(i,i+1) == "." || Dame.substring(i,i+1) == ",")
    {
    return true;
    }
  } 
  if (isNaN(Dame.substring(i,i+1)))
   {
    return false;
   } 
 
 
}
return true;
}



function Limitar(Origen,Nombre,Tamano)
{
var Destino =new String(Trim(Origen))
if (Destino.length > Tamano)
{
   alert("Resuma el contenido de " + Nombre)
   return false
}
else
{
  return true    
}   

}


function BlancosRedundantes(Literal)
{
var dame = new String(Literal);
MuchosBlancos = / {2,}/gi;
IzDer = /^ {1,}| {1,}$/gi;
dame = dame.replace(MuchosBlancos, " ");
dame = dame.replace(IzDer, "");
return dame //.toUpperCase();
}

function Rtrim (dame)
{
if (dame == "")
{
 return (dame);
}
for (var i = dame.length - 1; i >= 0; i--)
{
  if (dame.charAt (i) != " ")
  {
  return (dame.substring (0, i + 1));
  }
}
if (i < 0)
{
return "";
}
else
{
return (dame);
}
}



function Ltrim (dame)
{
if (dame == "")
{
 return (dame);
}
for (var i = 0; i < dame.length; i++)
{
if (dame.charAt (i) != " ")
{
return (dame.substring (i));
}
}
if (i==dame.length)
{
return "";
}
else
{
return (dame);
}
}
function Trim(Dame)
{
return Ltrim(Rtrim(Dame));
}

function ControlDeFecha(Toma)
{
return true;
}
function BorrarRegistro ()
{
if (! confirm ("¿Está seguro de que desea eliminar el registro?"))
{
return (false);
}
else
{
return (true);
}
}





function AnalizarE_mail(Literal)
{
  
  var Origen = new String(BlancosRedundantes(Literal));

 
  
  var n = Origen.indexOf("@");
 
  if (n == -1)
  {
   alert("E-mail incorrecto.");
   return false;
  }
  var j = Origen.indexOf("@",n+1)
  if (j != -1)
  {
   alert("E-mail incorrecto.");
   return false;
  }
  else
  {
  
  if (Origen.length == n+1)
  {
   alert("E-mail incorrecto. @ no puede ser el último carácter.");
   return false;
  }
  else
  {
  // Esta última parte está tomada de no sé dónde,cubre alguna cosa
    arroba=Origen.indexOf("@")
	punto=Origen.indexOf(".",arroba)
	coma=Origen.indexOf(",")
	espacio=Origen.indexOf(" ")
	ultimopunto=Origen.lastIndexOf(".")+1
	tamano=Origen.length
	if ((arroba>0) && (punto>(1+1)) && (coma==-1) && (espacio==-1) && (tamano-ultimopunto >=2) && (tamano-ultimopunto<=3)) 
	{
	return true;
	

	}
	else 
	{
    alert("E-mail incorrecto.");
	return false;
	}
  
  
  }
  }
} 


function Cambiar(dime)
{

if (dime == "S")
{
return  "N";
}
else
{
return "S";

}
}


function AMayuscula(dame)
{
s = new String(dame);
return s.toUpperCase();
}


function BlancosRedundantes(Literal){
	var dame = new String(Literal);
	MuchosBlancos = / {2,}/gi;
	IzDer = /^ {1,}| {1,}$/gi;
	dame = dame.replace(MuchosBlancos, " ");
	dame = dame.replace(IzDer, "");
	return dame 
}

function GuionesRedundantes(Literal){
	var dame = new String(Literal);
	MuchosGuiones = /-{2,}/gi;
	IzDer = /^-{1,}|-{1,}$/gi;
	dame = dame.replace(MuchosGuiones, "-");
	dame = dame.replace(IzDer, "");
	return dame 
}

function Validar(vStr) {
/*Responde: 
0 = teléfono correcto y español
1 = teléfono puede ser correcto, pero no es español
3 = teléfono incorrecto.
*/
	var ret;

	var ESBueno = /^(\+ ?34( |\-|\/)?)?[96](( |\-|\/)?\d){8}$/g;
	var ESMalo = /^\+ ?34( |\-|\/)(( |\-|\/)?\d){0,9}$/g;
	var OTRO = /^(\+ ?[1-9](\d){0,4}( |\-|\/)?)?[1-9](( |\-|\/)?\d){5,8}$/g;
	
	if (ESBueno.test(vStr)) ret = 0;
	else if (ESMalo.test(vStr)) ret = 3;
	else if (OTRO.test(vStr)) ret = 1;
	else ret = 3;
	return (ret);
}

function validaTlf(pStr) {
//Función de validación de números de tlf
	var ret;
	
	ret = BlancosRedundantes(pStr)
	ret = GuionesRedundantes(ret)
	ret = Validar (ret)
	
	return (ret);
}

function formateaTlf(pStr) {
//Función de validación de números de tlf
	var ret;
	
	ret = BlancosRedundantes(pStr)
	ret = GuionesRedundantes(ret)
	
	return (ret);
}
