function esNIF(campo,obligatorio)
{
	//Funciones de verificación NIF / CIF
	
	var inicio;
	// Cojo todo menos la letra del NIF
	var resto;
	// Cojo la letra del NIF
	var patron = /\D/;
	// Todo menos digitos.
	var patron1 = /T|R|W|A|G|M|Y|F|P|D|X|B|N|J|Z|S|Q|V|H|L|C|K|E/;
	//Letras posibles.
	var Letras_Posibles = "TRWAGMYFPDXBNJZSQVHLCKE" ;
	inicio = campo.value.substring(0,campo.value.length-1);
	resto = campo.value.substring(campo.value.length-1,campo.value.length);
	resto = resto.toUpperCase();
	if(obligatorio && campo.value.length == 0) return false;
	if(campo.value.length > 0)
	{
		//Busco que la letra este al final y el resto sean numeros 
		if((inicio.length == 0) || (inicio.match(patron)) || (!resto.match(patron1))) return false;
		numero_documento = ""
		for (i=0; i < inicio.length; i++)
			numero_documento = numero_documento + inicio.charAt(i)
			// Comprueba la letra del NIF 
			pos_corr = (numero_documento%23);
			letra_correcta = Letras_Posibles.charAt(pos_corr);
			if (resto != letra_correcta) return false;
	}  
	return true;
}


function esCIF(campo,obligatorio)
{
  var inicio;  	// Cojo la letra del NIF
  var resto;	// Cojo el resto -1
  var ultimo;
  var patron1 = /A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z/ //Letras posibles
  var patron3 = new RegExp(String.fromCharCode(91)+'A-J'+String.fromCharCode(93));
  var patron4 = /0|1|2|3|4|5|6|7|8|9/; //Digitos

  if(obligatorio && campo.value.length == 0)
    return false; //(MensError(campo,"Introduzca su CIF"));

  if(campo.value.length > 0)
  {
  	inicio = campo.value.substring(0,1);
  	inicio = inicio.toUpperCase();
  	resto = campo.value.substring(1,campo.value.length-1);
  	ultimo = campo.value.substring(campo.value.length-1,campo.value.length);
  	ultimo = ultimo.toUpperCase();

    if (campo.value.length != 9)
      return false; //(MensError(campo,"El CIF debe estar compuesto de 9 caracteres"));

    if (!inicio.match(patron1))
      return false; //(MensError(campo,"La letra del CIF es incorrecta"));

    for(i=0;i<resto.length;i++)
    {
      if( !resto.substring(i,i+1).match(patron4) )
        return false; //(MensError(campo,"Las posiciones del 2 al 8 del CIF solo pueden ser numeros "));
    }

    if (!ultimo.match(patron4) && !ultimo.match(patron3))
   	  return false; //(MensError(campo,"El ultimo caracter es incorrecto"));

   	//Comprueba que la letra es correcta
    N1 = resto.charAt(0)
    dosN1 = "0" + (N1 * 2)
    sumaN1 = parseInt(dosN1.charAt(dosN1.length-1)) + parseInt(dosN1.charAt(dosN1.length-2))

    N3 = resto.charAt(2)
    dosN3 = "0" + (N3 * 2)
    sumaN3 = parseInt(dosN3.charAt(dosN3.length-1)) + parseInt(dosN3.charAt(dosN3.length-2))

    N5 = resto.charAt(4)
    dosN5 = "0" + (N5 * 2)
    sumaN5 = parseInt(dosN5.charAt(dosN5.length-1)) + parseInt(dosN5.charAt(dosN5.length-2))

    N7 = resto.charAt(6)
    dosN7 = "0" + (N7 * 2)
    sumaN7 = parseInt(dosN7.charAt(dosN7.length-1)) + parseInt(dosN7.charAt(dosN7.length-2))

    N2 = resto.charAt(1)
    N4 = resto.charAt(3)
    N6 = resto.charAt(5)

    total = sumaN1 + parseInt(N2) + sumaN3 + parseInt(N4) + sumaN5 + parseInt(N6) + sumaN7
    T = "0" + total
    unidades_total = T.charAt(T.length-1)
    if(ultimo.match(patron4))
    {
      SumaFinal = parseInt(unidades_total) + parseInt(ultimo)
      SF = "0" + SumaFinal
      UNIDAD = SF.charAt(SF.length-1)
      if (UNIDAD == 0)
        return true
      else
        return false;  //(MensError(campo,"El CIF es incorrecto"));
    }
    else
    {
      if (ultimo == "A")
        numero_control = 1
      else if (ultimo == "B")
        numero_control = 2
      else if (ultimo == "C")
        numero_control = 3
      else if (ultimo == "D")
        numero_control = 4
      else if (ultimo == "E")
        numero_control = 5
      else if (ultimo == "F")
        numero_control = 6
      else if (ultimo == "G")
        numero_control = 7
      else if (ultimo == "H")
        numero_control = 8
      else if (ultimo == "I")
        numero_control = 9
      else if (ultimo == "J")
        numero_control = 0

      SumaFinal = parseInt(unidades_total) + parseInt(numero_control)
      SF = "0" + SumaFinal
      UNIDAD = SF.charAt(SF.length-1)

      if (UNIDAD == 0)
        return true
      else
        return false;
    }
  }
  return true
}

