
// Version 2.1

	
function check_all(ca_name	,ca_value	,ca_type	,ca_min	,ca_max,	ca_required)
	{
	// vars
	if (ca_min==1)	es_min = '';
	else			es_min = '';
	if (ca_max==1)	es_max = '';
	else			es_max = '';


	// checks
	if		(ca_required==false && ca_value.length<1)									output = '';
	else if	(ca_required==true && ca_value.length<1)									output = "· '" +ca_name+ "' is empty.\n";
	else if (ca_type=='float' && (ca_error = check_float(ca_value,ca_min,ca_max))<1)	output = error_txt(ca_name,ca_type,ca_error,ca_min,ca_max); 
	else if (ca_min>0 && ca_value.length<ca_min)										output = "· '" +ca_name+ "' is lower of " +ca_min+ " chars" +es_min+ ".\n";
	else if (ca_max>0 && ca_value.length>ca_max)										output = "· '" +ca_name+ "' is over " +ca_max+ " chars" +es_max+ ".\n";
	else if ((ca_error = check(ca_value,ca_type))<1)									output = error_txt(ca_name,ca_type,ca_error,ca_min,ca_max); 
	else																				output = '';		
	
	return output;
	}

function error_txt(et_name	,et_type	,et_error	,et_min	,et_max)
	{
	if (et_type=='nif' && et_error==-1)
		{
		output = "· Revise '" +et_name+ "': El numero y letra no cuadran.\n";
		}
	else if	((et_type=='phone') && et_error==-1)
		{
		output = "· El formato de '"  +et_name+ "' es incorrecto.\n Ejemplo: (981010203 / +34 981123456) \n";
		}
	else if	((et_type=='fecha' || et_type=='date') && et_error==-1)
		{
		output = "· The date '"  +et_name+ "' is not correct. (yyyy/mm/dd) \n";
		}
	else if (et_type=='float' && et_error<0)
		{
		et_format = "";
		for (counter=0;counter<et_min;counter++)	et_format += '0';
		et_format += ".";
		for (counter=0;counter<et_max;counter++)	et_format += '0';
		output = "· Check '" +et_name+ "': format ( " +et_format+ " ).\n";
		}
	else if (et_error<1)
		{
		output = "· Check '" +et_name+ "'.\n";
		}
	else
		{
		output = "";
		}
		
	return output;
	}


function check(input,mode)
	{
	// output leyend
	//  -2	mode not found
	//	 0	empty field
	//	-1	template dosent match
	//	 1  template match
	
	if		(mode=='email' || mode=='mail')	template_ = /^([a-zA-Z0-9_-]+\.)*([a-zA-Z0-9_-])+@([a-zA-Z0-9_-]+\.)+([a-zA-Z0-9]{2,4})$/;
	else if	(mode=='float')					template_ = /^[0-9]+\.+[0-9]+$/;
	else if	(mode=='number')				template_ = /^[0-9]*(\.?[0-9]+)?$/;
	else if	(mode=='int')					template_ = /^([0-9])+$/;
	else if	(mode=='phone')					template_ = /^(\+?([0-9]{2,3})\s?)?([0-9]){6,15}$/;
	else if	(mode=='simple_word')			template_ = /^([0-9a-zA-ZáéíóúÁÉÍÓÚäöüÄÖÜÇçñÑ\s])+$/;
	else if	(mode=='extend_word')			template_ = /^([0-9a-zA-ZáéíóúÁÉÍÓÚäöüÄÖÜÇçñÑ\s%&#`^·\+\*\(\)\'\"\[\]\{\}\/\\¿?!¡.,_:;-])+$/;
	else if	(mode=='passwd') 				template_ = /^([a-zA-Z0-9ñÑ_-])+$/;
	else if	(mode=='key')	 				template_ = /^([a-zA-ZñÑáéíóúÁÉÍÓÚäöüÄÖÜÇçñÑ]{1})$/;
	else									template_ = false;


	if		(mode=='nif')					output = check_nif(input);
	else if (mode=='fecha' || mode=='date')	output = check_fecha(input);
	else if	(template_==false)				output = -2;
	else if	(input == "")					output =  0;
	else if	(template_.test(input))			output =  1;
	else									output = -1;

	return output;
	}


function check_fecha(input)
	{
	output_cfi	= 1;
	template_	= /^([0-9]){2,2}\/([0-9]){2,2}\/([0-9]){4,4}$/;
	
	if	(template_.test(input))
		{
		dd		= 1*(input.substr(0,2));
		mm		= 1*(input.substr(3,2));
		yyyy	= 1*(input.substr(6,4));
				
		if (mm==0 || mm>12)
			{
			output_cfi = -1;
			}
		else
			{
				
			if (mm==4 || mm==6 || mm==9 || mm==11)	mounth_days = 30;
			else if (mm==2)							mounth_days = 28 + check_mounth_29(yyyy);
			else									mounth_days = 31;

			if (dd>mounth_days || dd==0)			output_cfi = -1;
			else									output_cfi =  1;
			}
			

		}
	else
		{
		output_cfi = 0;
		}


	return output_cfi;
	}

function check_mounth_29(year)
	{
	if (year % 4 != 0)	output = 0;
	else
		{
		if (year % 100 == 0)
			{
			if (year % 400 == 0)	output = 1;
			else					output = 0;
			}
		else output = 1;
		}

	return output;
	}


function check_length(input,length_min,length_max)
	{
	// check field length 
	// length_min = {0,1,...,n} >> cero dont check min length
	// length_max = {0,1,...,n} >> cero dont check max length
	//
	// output leyend
	//  -1	  lower field
	//	 0	correct field
	//	 1	  upper field

	
	input = ''+input;

	if		(length_min>0 && input.length<length_min)	output = -1;
	else if	(length_max>0 && input.length>length_max)	output =  1;
	else												output =  0;
	
	return output;	
	}


function error_length(el_field,el_min,el_max,el_name)
	{
	// write error length message

	check_length_return = check_length(el_field,el_min,el_max);

	if (check_length_return<0)
		{
		if (el_field.length==0)
			{
			output = "·'" +el_name+ "' can not be void.\n";
			}
		else
			{
			if (el_min==1)	es = '';
			else			es = 's';
	
			
			output = "·'" +el_name+ "' can not be lower to "	+el_min+ " char" +es+ ".\n";
			}
		}
	else if (check_length_return>0)
		{
		if (el_max==1)	{	es = '';	los = '';		}
		else			{	es = 's';	los	= '';	}
			
		output = "·'" +el_name+ "' is over " +los+el_max+ " char" +es+ ".\n";
		}
	else
		{
		output = "";
		}

	return output;
	}



function check_float(input,integer_length,decimal_length)
	{
	// output leyend
	// - 1 (float width   correct integer length and incorrect decimal length)
	// -10 (float width incorrect integer length and   correct decimal length)
	// -11 (float width incorrect integer length and incorrect decimal length)
	//   0 (not float number)
	//   1 (float width   correct integer length and   correct decimal length)

	is_float = check(input,'float');

	if (is_float==1)
		{
		input = '' + input + '';
		dot = input.indexOf(".");
		len = input.length;

		float_int = ''+input.substr(0,dot++);
		float_dec = ''+input.substr(dot,len-dot);
		
		output = 0;
		if (integer_length>0 && float_int.length > integer_length)	output-= 10;	// error
		if (decimal_length>0 && float_dec.length > decimal_length)	output-=  1;	// error
		if (output==0)												output =  1;	// correct
		}
	else
		{
		output = 0;
		}

	return output;
	}

function check_dc(banco,sucursal,cuenta,dc)
	{
	if (check(banco,'int')+check(sucursal,'int')+check(cuenta,'int')+check(dc,'int')==4)
		{
		if (banco.length == 4 && sucursal.length == 4 && cuenta.length == 10 && dc.length == 2)
			{
			ndc			= 10*get_dc('00'+banco+sucursal) + 1*get_dc(cuenta);
			
			if (dc==ndc)	output =  1;
			else			output = -1;
			}
		else
			{
			output = 0;
			}
		}
	else
		{
		output = 0;
		}
	
	return output;
	}

function get_dc(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;
	}
		

function check_nif(nif)
	{
	if (nif.length==9 || nif.length==10)
		{
		letras	= 'trwagmyfpdxbnjzsqvhlcke';

		nif		= nif.toLowerCase();
		dni		= nif.substr(0,8);
		char_	= nif.substr(nif.length-1,1);	

		if (nif.length==10)	separator = nif.substr(8,1);
		else				separator = '';
			
		if (separator=='-' || separator==' ' || separator=='')	
			{
			if ((check(dni,'int')+check(char_,'key'))!=2)	output = 0;
			else
				{
				numero	= dni%23;
				new_nif	= dni + separator + letras.substring(numero,numero+1);
				
				if (new_nif==nif)	output =  1;
				else				output = -1;
				}
			}
		else
			{
			output = 0;
			}
		}
	else
		{
		output = 0;
		}

	return output;
	}
 		


