var month = new Array(12);

month[0] = 31;
month[1] = 29;
month[2] = 31;
month[3] = 30;
month[4] = 31;
month[5] = 30;
month[6] = 31;
month[7] = 31;
month[8] = 30;
month[9] = 31;
month[10] = 30;
month[11] = 31;


function LTrim(s){
	// Devuelve una cadena sin los espacios del principio
	var i=0;
	var j=0;
	
	if( s.length > 0 ){
	   // Busca el primer caracter <> de un espacio
	   for(i=0; i<=s.length-1; i++)
		    if(s.substring(i,i+1) != ' '){
			     j=i;
			     break;
		  }
	    return s.substring(j, s.length);
  }else{
      return s;
  }
	
}

function RTrim(s){
	// Quita los espacios en blanco del final de la cadena
	var j=0;
	
	if( s.length > 0 ){
	 // Busca el último caracter <> de un espacio
	 for(var i=s.length-1; i>-1; i--)
		  if(s.substring(i,i+1) != ' '){
			 j=i;
			 break;
		  }
	 return s.substring(0, j+1);
  }else{
    return s;
  }
	
}

function Trim(s){
	// Quita los espacios del principio y del final
	return LTrim(RTrim(s));
}


function check_date(dest)
{
	var date_tmp = dest.value;
	var correct = true;
	if( date_tmp.length == 10 ){
	   bar_ini_1 = date_tmp.substring(2,3);
	   bar_ini_2 = date_tmp.substring(5,6);
	   day_ini = date_tmp.substring(0,2);
	   if ( day_ini.substring(0,1) == "0")
	   {
		    day_ini = day_ini.substring(1,2);
	   }
	   month_ini = date_tmp.substring(3,5);
	   if ( month_ini.substring(0,1) == "0")
	   {
		    month_ini = month_ini.substring(1,2);
	   }
	   year_ini = date_tmp.substring(6,10);
	   longd = date_tmp.length;
	   if ( longd == 0 )
	   {
		    correct = false;
	   }
	}else{
	   correct = false;
	   window.alert("Fecha Incorrecta. (dd/mm/yyyy)");	
  }

	if ( correct )
	{
		if ( bar_ini_1 == "/" && bar_ini_2 == "/" )
		{
			if ( parseInt(month_ini) >= 1 && parseInt(month_ini) <= 12)
			{
				if ( month[parseInt(month_ini) - 1] < parseInt(day_ini) )
				{
					correct = false;
					window.alert("Fecha Incorrecta. (DD/mm/yyyy)");			
				}
				else
				{
					if ( ! check_num(year_ini,1900,2099) )
					{
						correct = false;
						window.alert("Fecha Incorrecta. (dd/mm/YYYY)");			
					}
				}
			}	
			else
			{
				correct = false;
				window.alert("Fecha Incorrecta. (dd/MM/yyyy)");			
			}
		}
		else
		{
			correct = false;
			window.alert("Fecha Incorrecta. (dd/mm/yyyy)");			
		}
	}
	if ( ! correct)
	{
		dest.focus();
		dest.select();
	}
	return correct;
}


function check_hora(dest)
{
	var hora_tmp = dest.value;
	var correct = true;
	if( hora_tmp.length == 5 ){
	   separador = hora_tmp.substring(2,3);
	   hora_ini = hora_tmp.substring(0,2);
	   if ( hora_ini.substring(0,1) == "0")
	   {
		    hora_ini = hora_ini.substring(1,2);
	   }
	   minutos_ini = hora_tmp.substring(3,5);
	   if ( minutos_ini.substring(0,1) == "0")
	   {
		    minutos_ini = minutos_ini.substring(1,2);
	   }
	}else{
	   correct = false;
	   window.alert("Hora Incorrecta. (hh:mm)");	
  	}
	if ( correct )
	{
		if ( separador == ":" )
		{
			if ( parseInt(minutos_ini) >= 0 && parseInt(minutos_ini) <= 59)
			{
				if ( parseInt(hora_ini) >= 0 && parseInt(hora_ini) <= 23)
				{
				}else{			 
					correct = false;
					window.alert("Hora Incorrecta. (HH:mm)");	
				}
			}	
			else
			{
				correct = false;
				window.alert("Hora Incorrecta. (hh:MM)");			
			}
		}
		else
		{
			correct = false;
			window.alert("Hora Incorrecta. (hh:MM)");			
		}
	}
	if ( ! correct)
	{
		dest.focus();
		dest.select();
	}
	return correct;
}


function val_float(dest,deflt)
{
	var num = dest.value;
	if ( num.indexOf(",") != -1 )
	{
		var pos = num.indexOf(",");
		num = num.substring(0,pos) + "." + num.substring(pos+1,num.length);
	}
	var comp = parseFloat(num);
	var num_ok = true;
	if (comp == "NaN" || comp == "" || isNaN(comp) )
	{
		if ( comp != 0 )
		{
			num_ok = false;
		}
	}
	if ( ! num_ok )
	{
		window.alert("Numero Incorrecto: " + num);
		dest.value = deflt;
	}
	else
	{
		dest.value = comp;
	}
}

function val_int(dest,deflt)
{
	var num = dest.value;
	var comp = parseInt(num);
	var num_ok = true;
	if ( comp == "NaN" || comp == "" || isNaN(comp) )
	{
		num_ok = false;
		// window.alert(comp);
	}
	if ( ! num_ok )
	{
		window.alert("Numero Incorrecto. " + num);
		dest.value = deflt;
	}
	else
	{
		dest.value = comp;
	}		
}


function check_mail(mail)
{
	var comp = mail;
	var mail_ok;
	if ( comp.indexOf("@") != -1 )
	{
		mail_ok = true;
	}
	else
	{
		mail_ok = false;
	}
	if ( mail_ok )
	{
		var longd = comp.length;
		var pos_1 = comp.substring(longd - 4,longd -3);
		var pos_2 = comp.substring(longd - 3,longd -2);
		if ( pos_1 == "." || pos_2 == "." )
		{
		}
		else
		{
			mail_ok = false;
		}
	}
	return mail_ok;
}


function check_num(num,min,max)
{
	var comp = parseInt(num);
	var num_ok;
	if (comp == "NaN" || comp == "")
	{
		num_ok = false;
	}
	else if (comp > min && comp < max)
	{
		num_ok = true;
	}
	else
	{
		num_ok = false;
	}
	return num_ok;
}

function longd_data(data,longd)
{
	var comp = Trim(data);
	var data_ok;
	// window.alert(comp.length  );
	if (comp.length >= longd)
	{
		data_ok = true;
	}
	else
	{
		data_ok = false;
	}
	return data_ok;
}

function longd_data_1(data,longd)
{
	var comp = Trim(data);
	var data_ok;
	if (comp.length == longd)
	{
		data_ok = true;
	}
	else
	{
		data_ok = false;
	}
	return data_ok;
}
function longd_data_2(data,longd)
{
	var comp = data;
	var data_ok;
	if (comp.length <= longd)
	{
		data_ok = true;
	}
	else
	{
		data_ok = false;
	}
	return data_ok;
}

function all_let(data)
{
	var data_ok = true;
	var lets = "aábcdeéfghiíjklmnńoópqrstuúvwxyz ";
	var comp = data;
	comp = comp.toLowerCase();
	var longd = comp.length;
	var x;
	for (x = 0; x < longd; x++)
	{
		var let = comp.substring(x,x+1);
		if ( lets.indexOf(let) == -1 )
		{
			data_ok = false;
			break;
		}
	}
	return data_ok;
}

function all_num(data)
{
	var data_ok = true;
	var nums = "0123456789";
	nums = nums.replace(" ","");
	var comp = data;
	var longd = comp.length;
	var x;
	for (x = 0; x < longd; x++)
	{
		var num = comp.substring(x,x+1);
		if ( nums.indexOf(num) == -1 )
		{
			data_ok = false;
			break;
		}
	}
	return data_ok;
}

function some_lets(data)
{
	var data_ok = true;
	var lets = "abcdefghijklmnopqrstuvwxyz";
	var comp = data;
	comp = comp.toLowerCase();
	var longd = comp.length;
	var x;
	for (x = 0; x < longd; x++)
	{
		var let = comp.substring(x,x+1);
		if ( lets.indexOf(let) == -1 )
		{
			data_ok = false;
			break;
		}
	}
	return data_ok;
}



function check_nif(nif)
{
	var comp = nif;
	var nif_ok;
	if( ! longd_data(comp,8) ){ // Longitud Minima del NIF, 8
	   nif_ok = false;
  }else{
     nif_ok = true;
  }
	if( nif_ok && all_num(comp.substring(0,comp.length - 1) ) ){ // Todos menos el ultimo caracter debene ser Numeros
	  nif_ok = true;
  }else{
    nif_ok = false;
  }
  if( nif_ok ){
    if( letra_nif(comp.substring(0,comp.length - 1)) == comp.substring(comp.length - 1, comp.length ) ){
      nif_ok = true;
    }else{
      nif_ok = false;
    }
  }
	return nif_ok;
}


function letra_nif(nif)
{
  cadena = "TRWAGMYFPDXBNJZSQVHLCKET";
  posicion = nif % 23;
  letra = cadena.substring(posicion,posicion+1);
  return letra;
} 


function check_cif(elCIF) 
{
  var resul = false;
  if( check_cif_prev(elCIF)  ){
  
    var v1 = new Array(0,2,4,6,8,1,3,5,7,9); 
    var temp = 0; 
    var temp1;
  
    for( i = 2; i <= 6; i += 2 )     {
        temp = temp + v1[ parseInt(elCIF.substr(i-1,1)) ];
        temp = temp + parseInt(elCIF.substr(i,1));
      };

    temp = temp + v1[ parseInt(elCIF.substr(7,1)) ];
    temp = (10 - ( temp % 10));

    if( temp == 10 ){
      // alert( "El dígito de control es: J ó 0" );
      if( elCIF.substr(8,1) == "J" || elCIF.substr(8,1) == "0" ){
        resul = true;
      }
    }else{
      // alert( "El dígito de control es: "+temp ); 
      if( elCIF.substr(8,1) == temp ){
        resul = true;
      }
    }
  }

  return resul;
}


function check_cif_prev(elCIF) 
{
  var resul = false;
  var temp = elCIF.toUpperCase(); // pasar a mayúsculas

  if (!/^[A-Za-z0-9]{9}$/.test(temp)) {  // Son 9 dígitos? 
       // alert ("Longitud incorrecta, un CIF consta de 9 dígitos");
       resul = false;
  }else if (!/^[ABCDEFGHKLMNPQS]/.test(temp)) { // Es una letra de las admitidas ?
     // alert("El primer dígito es incorrecto, debe ser una letra de las siguientes: A,B,C,D,E,F,G,H,K,L,M,N,P,Q,S ");
     resul = false;
  }else {
     resul = true;
  }
  return resul;
}

