// Expresiones regulares

sExpFec = /^(\d?\d)([-\/])(\d?\d)\2(\d?\d?\d\d)$/;   // Definicion de fecha (dd-mm-(aa)aa o dd/mm/(aa)aa)
sExpFec2 = /^(\d\d?)[-\/](\d?\d?\d\d)$/;   // Definicion de fecha mm/[aa]aa
sExpFec3 = /^(\d\d)(\d\d)(\d\d\d\d)$/;   // Definicion de fecha ddmmaaaa
tfno=/^[0-9]+$/; // Numero de telefono
// Direccion e-mail de acuerdo con la definicion del RFC 2822
correo=/^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!\.)){0,61}[a-zA-Z0-9]?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!$)){0,61}[a-zA-Z0-9]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/
// Direccion e-mail aplicando normas mas restrictivas
correo2=/^([0-9a-zA-Z][0-9a-zA-Z\-\_]*\.)+[A-Za-z]*@([0-9a-zA-Z][0-9a-zA-Z\-]*\.)+[A-Za-z]{2,6}$/;
correo3=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/

dns=/^([a-zA-Z][0-9a-zA-Z\-]{0,61}[0-9a-zA-Z]\.)*[a-zA-Z][0-9a-zA-Z\-]{0,61}[0-9a-zA-Z]$/

uppercaseLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' ;
lowercaseLetters = 'abcdefghijklmnopqrstuvwxyz' ;


// ********************************   Funcion auxiliar para comprobar campos vacios.

function compruebaNoVacioCampo(campo)
{
	if (campo.value == '')
	{
	alert(aIdioma['CAMPO_VACIO']) ;
    campo.focus();
    return false;
	}	
  else
    return true;
}

// s es una direccion de correo valida

/* Sustituida por otra funcion con el mismo nombre mas abajo
function isEmail (campo)
{
    var s = campo.value;
    var i = 1;
    var sLength = s.length;
    if(s=="")return true;
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@"))
    {
      alert("email incorrecto");
      campo.focus();
      return false;
    }
    else i += 2;

    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    if ((i >= sLength - 1) || (s.charAt(i) != "."))
    {
      alert("email incorrecto");
      campo.focus();
      return false;
    }
    else return true;
}
*/

// c es un digito
function isDigit (c)
{
    return ((c >= "0") && (c <= "9"))
}

// s es un numero entero
function isInteger (campo)
{
  var s = campo.value;
  var i;
    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);
        if( i != 0 ) {
            if (!isDigit(c))
            {
              alert(aIdioma['VALOR_NUMERICO']);
              campo.focus();
              return false;
            }
        }
    }
    return true;
}

// s es un numero (sin signo)
function isNumeric (campo)
{
  var s = campo.value;
  var i;
    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);
        if (!isDigit(c))
        {
          alert(aIdioma['VALOR_NUMERICO']);
          campo.focus();
          return false;
        }
    }
    return true;
}

// comprueba Código Postal
function compruebaCodigoPostal(campo)
{
   var s = campo.value;
   if (s>52999 || s < 1000){
      alert(aIdioma['CODIGO_POSTAL_INCORRECTO']);
      campo.focus();
      return false;
    }
    else
    return true
}

// Valida fecha. En caso de OK devuelve true; si no, devuelve null
function esFecha(campo)
{
  var sFecha = campo.value;
  var resx;
  var date=null; // resultado
  var iDia;
  var iMes;
  var iAnno;
  var ok=true;

  if (sFecha != '')
	{
    resx=sExpFec.exec(sFecha);
    if (resx!=null)
    { // Formato numerico con separadores
      iDia = parseInt(resx[1],10);
      iMes = parseInt(resx[3],10);
      iAnno = parseInt(resx[4],10);
    }
    else if ((resx=sExpFec2.exec(sFecha))!=null)
    { // Formato de mes/año
      iMes=parseInt(resx[1],10);
      iAnno=parseInt(resx[2],10);
      iDia=1;
    }
    else if ((resx=sExpFec3.exec(sFecha))!=null)
    { // Formato de ddmmaaaa
      iDia = parseInt(resx[1],10);
      iMes = parseInt(resx[2],10);
      iAnno = parseInt(resx[3],10);
    }
    else
      ok=false;

    if (ok)
    {
    
      if (!esMesLogico(iMes) || !esDiaLogico(iDia,iMes,iAnno))
        ok=false;
      else
      {
        if (iAnno<80)
          iAnno+=2000;
        else if (iAnno<100)
          iAnnio+=1900;
        date = new Date(iAnno, iMes-1, iDia);
      }
    }
    if(!ok)
    {
      alert(aIdioma['FECHA_INCORRECTA']);
      campo.focus();
    }
  }
  return ok;
}

function esMesLogico (iMes) {
  return (validar_rango(iMes,1,12)) ? true : false;	
}	

function esDiaLogico (iDia,iMes,iAnno) {
  switch (iMes) {
    case 4,6,9,11:
	if (!validar_rango(iDia,1,30)) return false;    
    case 2:
              if ((iAnno % 400 == 0)||((iAnno % 4 == 0)&&(iAnno % 100 != 0))) {
    	   if (!validar_rango(iDia,1,29)) return false;
   	} else {
    	   if (!validar_rango(iDia,1,28)) return false;	
    	};
    default:
	if (!validar_rango(iDia,1,31)) return false;	        	
  }
  return true;  		
}

// Comprueba que un numero se encuentra entre dos valores
function validar_rango (iNum,iMin,iMax) {
  return (iMin > iNum || iMax < iNum) ? false : true; 
}

//.·'·.·'·.·'·.·'·.·'·.·'·.·'·.·'·.·'·.
//·.·'·.·'·.·'·.·'·.·'·.·'·.·'·.·'·..·'
//.·'·.·'·.·'·.·'·.·'·.·'·.·'·.·'·.·'·.
//·.·'·.·'·.·'·.·'·.·'·.·'·.·'·.·'·..·'
//.·'·.·'·.·'·.·'·.·'·.·'·.·'·.·'·.·'·.
//·.·'·.·'·.·'·.·'·.·'·.·'·.·'·.·'·..·'

function validaIPs(campo,campo2)
{
	var resultado;
  var valor = campo.value;
//  alert("tamaño-> "+valor.length);
	var i;
  var contadorPuntos = 0;
  var porcion;
  var mal;
  var count = 0;
  if(valor=="") return true;
  for (i=0; i < valor.length; i++)
  {
    if (valor.charAt(i) == ".")
    {
      contadorPuntos ++;
      porcion = valor.substring(count, i);
//      alert("analizo este numero de IP: "+porcion);
      if (isDigitElCampo(porcion))
      {
        if (porcion <0)
        {
//          alert("MAL if 1");
          mal = true;
        }
        if (porcion > 255)
        {
//          alert("MAL if 2");
          mal = true;
        }
      }
      else
      {
        mal = true;
      }
      count = i+1;
      porcion = "";
    }
  }

  porcion = valor.substring(count, valor.length);
//  alert("analizo este numero de IP: "+porcion);
  if (isDigitElCampo(porcion))
  {
    if (porcion <0)
    {
//    alert("MAL if 1");
      mal = true;
    }
    if (porcion > 255)
    {
//    alert("MAL if 2");
      mal = true;
    }
  }

  if (contadorPuntos == 3)
  { resultado = true; }
  else
  { mal = true; }
  
  if (mal == true)
  {
    alert("IP no valida");
    campo2.focus();
    return false ;
  }
  else
    return resultado;
}

//------------------------------------
//------------------------------------
//------------------------------------
//------------------------------------
function isDigitElCampo(valor)
{
  var i;
  var caracter;
  var mal;
  if (valor.length > 0)
  {
    for (i=0; i<valor.length; i++)
    {
      caracter = valor.substring(i, i+1);
      expresion = "[0-9]{1}";  

      matchStr = new RegExp(expresion).exec(caracter);
  
      if (matchStr == null) 
      {
        mal = true;
        retorno = false;
      }
      else 
      {
        retorno = (caracter == matchStr[0]);
      }
    }
  }
  else
  {
//    alert("longitud de:" +valor +"  y longitud: " +valor.length);
    mal = true;
  }
  if (mal == true)
    return false
  else
    return retorno;
}

// --------------------
// --------------------

function isEmail(campo)
{
	var resultado;
  resultado = campo.value.match(correo);
  if (resultado == null)
	{
	  alert(aIdioma['CORREO_EMECTRONICO_INVALIDO']);
	  campo.focus();
	  campo.select();
	  return false;
  }
  return true;
}

// --------------------
// --------------------

function validarTfno (campo)
{
	var resultado;
  resultado = campo.value.match(tfno);
  if (resultado == null)
	{
	  alert(aIdioma['TELEFONO_INVALIDO']);
	  campo.focus();
	  campo.select();
	  return false;
  }
  return true;
}

// --------------------
// --------------------

function validaServidorDNS(campo) 
{
	var valor = campo.value;
  var i;
  var contadorPuntos = 0;
  var contadorEspacios = 0;
  
  if(valor=="") return true;
  if((valor.charAt(0)==".")||(valor.charAt(valor.length-1)==".")||(valor.charAt(0)=="-")||(valor.charAt(valor.length-1)=="-"))
  {
    alert(aIdioma['DNS_INCORRECTO_1'])
    campo.focus()
    return false
  }
  if((!isLetterOrDigit(valor.charAt(0)))||(!isLetterOrDigit(valor.charAt(valor.length-1))))
  {
    alert(aIdioma['DNS_INCORRECTO_2'])
    campo.focus()
    return false
  }
  for (i=1; i < ((valor.length)-1); i++)
  {
    if(((valor.charAt(i)==".")||(valor.charAt(i)=="-")) && ((valor.charAt(i-1)==".")||(valor.charAt(i-1)=="-")))
    {
      alert(aIdioma['DNS_INCORRECTO_3'])
      campo.focus()
      return false
    }
    if((isLetterOrDigit(valor.charAt(i)))||(valor.charAt(i)==".")||(valor.charAt(i)=="-"))
    {
      if (valor.charAt(i) == ".")
      {
        contadorPuntos ++;
      }
    }
    else
    {
      alert(aIdioma['DNS_INCORRECTO_4'])
      campo.focus()
      return false
    }
    
  }
  if(contadorPuntos >0)
    return true
  else
  { 
    alert(aIdioma['DNS_INCORRECTO_5'])
    campo.focus()
    return false
  }
}

function isLetter (c)
{
    return( ( uppercaseLetters.indexOf( c ) != -1 ) ||
            ( lowercaseLetters.indexOf( c ) != -1 ) )
}

function isLetterOrDigit (c)
{   return (isLetter(c) || isDigit(c))
}

function tieneLongitud(campo, longitud){
     if(campo.value.length == longitud) {return true;}
  else
  {
    alert(aIdioma['LONGITUD_CAMPO'] + longitud);
    campo.focus();
    return false;
  }
}

function isDNS(campo)
{
	var resultado;
  	resultado = campo.value.match(dns);
  	if (resultado == null)
	{
	  alert(aIdioma['DNS_INVALIDO']);
	  campo.focus();
	  campo.select();
	  return false;
  	}
  	return true;
}


function toUpper(campo){
    campo.value = campo.value.toUpperCase();
}

