/*==============================================================
====================== FUNCIONES DE VALIDACION =================
================================================================
========Luis Viejo Carnicero y  Miguel Angel Bueno Sánchez=====
==============================================================*/


/*=================================================================
 fncEsNumerico(objeto) 
==================================================================*/
	
			function fncNoEsNumerico(objeto)
			{
				if (isNaN(objeto.value))
				{
					return true
				}
				else
				{
					return false
				}  
			}




/*=================================================================
 fncEstaVacio(objeto) 
==================================================================*/

			function fncEstaVacio(objeto)
			{	
				
				if(Trim(objeto.value)=='')
				{
					return true
				}
				else
				{
					return false
				}
			}


/* =================================================================
 fnMarcarError(objeto) 
===================================================================*/

			function fncMarcarError(objeto)
			{
				objeto.focus()
				objeto.style.backgroundColor='#F8AC3E'

			}


/*=================================================================
 fndDescarnarError(objeto) 
==================================================================*/


			function fncDesmarcarError(objeto)
			{
				objeto.style.backgroundColor='white'
			}


/*=====================================================================
 LTrim(cadena) : 
 Devuelve una copia de "cadena" sin espacios en blanco al inicio.
=====================================================================*/


			function LTrim(str)
			/*
			   OBJETIVO: Eliminar los espacios en blancos iniciales de str.
			   IN: str - Cadena que queremos limpiar
			*/

			{
			   //We don't want to trip JUST spaces, but also tabs,
			   // line feeds, etc.  Add anything else you want to
			   // "trim" here in Whitespace
			   var whitespace = new String(" \t\n\r");

			   var s = new String(str);

			   if (whitespace.indexOf(s.charAt(0)) != -1) {
			      // We have a string with leading blank(s)...

			      var j=0, i = s.length;

			      // Iterate from the far left of string until we
			      // don't have any more whitespace...
			      while (j <i && whitespace.indexOf(s.charAt(j)) != -1)
			         j++;

			      // Get the substring from the first non-whitespace
			      // character to the end of the string...
			      s = s.substring(j, i);
			   }
			   return s;
			}





/*==================================================================
 RTrim(string) :
 Returns a copy of a string without trailing spaces.
==================================================================*/



			function RTrim(str)

			{

			   var whitespace = new String(" \t\n\r");

			   var s = new String(str);

			   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
			      // We have a string with trailing blank(s)...

			      var i = s.length - 1;       // Get length of string

			      // Iterate from the far right of string until we
			      // don't have any more whitespace...
			      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
			         i--;


			      // Get the substring from the front of the string to
			      // where the last non-whitespace character is...
			      s = s.substring(0, i+1);
			   }

			   return s;
			}




/*=====================================================================
 Trim(string) :
 Devuelve una copia de la cadena sin espacios al final o al principio
=====================================================================*/



			function Trim(str)

			{
			   return RTrim(LTrim(str));
			}

  // La función recibe el CIF completo: A58818501
function validar_cif(cif) 
{	
	var cif_up = cif.toUpperCase(); 
	if(cif_up == 'Z00000000')
		return true;
		
	if (!/^[A-Za-z0-9]{9}$/.test(cif_up)) // Son 9 dígitos?
		return false;
		
	else if (!/^[ABCDEFGHKLMNPQS]/.test(cif_up)) // Es una    letra de las admitidas ?		
		return false;	
	
	var v1 = new Array(0,2,4,6,8,1,3,5,7,9); 
	var temp = 0;

	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));
	
	var checked = new Array('J','A','B','C','D','E','F','G','H','I');
	if(temp == 10)
		return (cif_up.charAt(8) == 'J' || cif_up.charAt(8) == '0');		
	else
		return (cif_up.charAt(8) == temp.toString() || cif_up.charAt(8) == checked[temp]);
	return true;
}

