function filtrar_caracteres(value)
{
	nopermitidos = '\\\'<>';
	for (i=0;i < nopermitidos.length;i++) 
	{
		value = value.replace (nopermitidos.charAt(i),'');
	} 
	return(value);
}

function validar_normaltext(campo)
{
	return (true);
}

function validar_expreg(valor, expreg)
{
	var test = new RegExp (expreg);
	var result = test.exec(valor);
	if (result==null)
		return(false);
	return(true);
}

function validar_mute (campo,datatype,obligado)
{
	var error=false;
	if ((datatype != 'autodate') && (datatype != 'date') && (datatype != 'reference') && 
		 (datatype != 'sex') && (datatype != 'bool') && (campo != null) && (datatype != 'picture'))
	{
		campo.value = filtrar_caracteres(campo.value);
		if ((obligado==1) && (validar_expreg(campo.value,"^\s*$")))
		{
			error=true;
			campo.value="Campo Obligatorio";
		}
		if ((obligado==0) && (validar_expreg(campo.value,"^\s*$")))
		{
			campo.style.backgroundColor="#fff";
			return (true);
		}

		if (datatype == "integer")
			if (!validar_expreg(campo.value,"^[+|-]?[0-9][0-9]*$"))
			{
				error = true;
				campo.value = "NO NUMERO";
			}

		if (datatype == "float")
			if (!validar_expreg(campo.value,"^[+|-]?[0-9][0-9]*(\\.[0-9][0-9]*)*$"))
			{
				error = true;
				campo.value = "NO NUMERO";
			}

		if (datatype == "nif")
			if (!check_nif (campo.value.toUpperCase()))
			{
				error=true;
				campo.value = "NIF no válido";
			}

		if (datatype=="dni")
			if (!check_ident (campo.value.toUpperCase()))
			{
				error=true;
				campo.value = "DNI no válido";
			}

		if (datatype=="mail")
			if (!check_mail(campo.value))
			{
				error=true;
				campo.value = "Formato de Email incorrecto";
			}

		if (datatype=="cif")
			if (!check_cif (campo.value.toUpperCase()))
			{
				error=true;
				campo.value = "CIF no válido";
			}
		if (error)
		{
			campo.style.backgroundColor="#f00";
			return (false);
		}
		else
		{
			campo.style.backgroundColor="#fff";
			return (true);
		}
	}
	return (true);

}

function validar (campo,datatype,obligado)
{
	var error=false;
		campo.value = filtrar_caracteres(campo.value);
	if ((obligado==1) && (validar_expreg(campo.value,"^\s*$")))
	{
		if (campo.style.backgroundColor != "#f00")
			alert ('Campo Obligatorio. Debe rellenarlo');
		error=true;
	}
	if ((obligado==0) && (validar_expreg(campo.value,"^\s*$")))
	{
		campo.style.backgroundColor="#fff";
		return(true);
	}

	if (datatype == "integer")
		if (!validar_expreg(campo.value,"^[+|-]?[0-9][0-9]*$"))
		{
			alert ("El campo debe contener un número entero");
			error = true;
		}

	if (datatype == "float")
		if (!validar_expreg(campo.value,"^[+|-]?[0-9][0-9]*(\\.[0-9][0-9]*)?$"))
		{
			error = true;
			alert ("El campo debe contener un número decimal (separado por .)");
		}


	if (datatype == "nif")
		if (!check_nif (campo.value))
		{
			alert ("NIF no valido");
			error=true;
		}

	if (datatype=="dni")
		if (!check_ident (campo.value))
		{
			alert ("DNI no valido.");
			error=true;
		}

	if (datatype=="mail")
		if (!check_mail(campo.value))
		{
			alert ("E-mail no valido.");
			error=true;
		}

	if (datatype=="cif")
		if (!check_cif (campo.value))
		{
			alert ("CIF no válido.");
			error=true;
		}

   if (datatype=="reference")
      if (!check_reference(campo.value))
      {
			if (campo.style.backgroundColor != "#f00")
         	alert("Debe seleccionar un valor.");
         error=true;
      }

	if (error)
	{
		campo.style.backgroundColor="#f00";
		return (false);
	}
	else
	{
		campo.style.backgroundColor="#fff";
		return (true);
	}
}

function valida_submit (form)
{
}

function check_mail(mail) 
{
	var er_mail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	if (!er_mail.test(mail)) 
	{
		return false;
	}
	return true;
}

// Función que comprueba que el cif es válido 
function check_cif(cif) {
  var er_cif = /^[ABCDEFGHKLMNPQS]([0-9]{7})[A-J0-9]$/;
  var v1 = new Array(0, 2, 4, 6, 8, 1, 3, 5, 7, 9);
  var v2 = new Array('J','A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I');
  var temp = 0;

  if (!er_cif.test(cif)) {
    return false;
  }

  for(i = 2; i <= 6; i += 2) {
    temp = temp + v1[parseInt(cif.substr(i-1,1))];
    temp = temp + parseInt(cif.substr(i,1));
  }

  temp = temp + v1[ parseInt(cif.substr(7,1))];
  temp = ((10 - ( temp % 10)) % 10);
  control = cif.substr(8,1);

  return ((control == temp) || (control == v2[temp]));
}


function check_ident (codigo) {
  var er_codigo = /^([0-9]|[A-Z]){9,14}$/;

  return (er_codigo.test(codigo));
}

//Comprueba que el nif sea correcto.
function check_nif (cod_nif) {
  var letras_nif="TRWAGMYFPDXBNJZSQVHLCKET";
  var pos;     
  var letra_ok;
  var letra_nif;
  var num_nif;
  var er_nif = /(^([0-9]{8,8}[A-Z])|^)$/;
  

  if(!er_nif.test(cod_nif)) { 
    return false;
  } 

  s = new String(cod_nif);
  if(s.length != 9) {
    return false;
  } 

  num_nif = s.substring(0,8);
  letra_nif = s.substring(8,9);
  pos = num_nif % 23;     
  letra_ok = letras_nif.substring(pos,pos+1);
   
  if (letra_nif != letra_ok) {
    return false;
  }
  return true;
}

function check_reference (ref)
{
   er_ref = /\d+/;

   if (er_ref.test(ref))
      return true;
   return false;
}

function obtenerDigito(valor)
{
	valores = new Array(1, 2, 4, 8, 5, 10, 9, 7, 3, 6);
	control = 0;
	for (i=0; i<=9; i++)
		control += parseInt(valor.charAt(i)) * valores[i];
	control = 11 - (control % 11);
	if (control == 11) control = 0;
	else if (control == 10) control = 1;
	return control;
}

