/*
 * validators.js
 *
 * Copyright (C) 2006 - OS3 srl - http://www.os3.it
 *
 * Written by: Fabio Rotondo - fabio.rotondo@os3.it
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation;
 * version 2 of the License ONLY.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * NOTE: this is the GPL version of the library. If you want to include this 
 *       library inside a CLOSED SOURCE / PROPRIETARY software, you need 
 *       to purchase a COMMERCIAL LICENSE. See http://www.os3.it/license/
 *       for more info.
 */
liwe.validators = {};

liwe.validators.is_integer  = function ( n ) { return RegExp ( "^[-+]?[0-9]+$" ).test ( n ); };
liwe.validators.is_alpha    = function ( s ) { return RegExp ( "^[a-zA-Z]+$" ).test ( s ); };
liwe.validators.is_alphanum = function ( s ) { return RegExp ( "^[a-zA-Z0-9]+$" ).test ( s ); };
liwe.validators.is_date     = function ( s ) { return RegExp ( "^[0-9]{4,4}.[0-9]{2,2}.[0-9]{2,2}$" ).test ( s ); };
liwe.validators.is_time     = function ( s ) { return RegExp ( "^[012][0-9]:[0-5][0-9]$" ).test ( s ); };
liwe.validators.is_email    = function ( s ) { return RegExp ( "^[a-zA-Z0-9-_.]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$" ).test ( s ); };
liwe.validators.is_float    = function ( n ) { return RegExp ( "^[-+]?[0-9]*.[0-9]*$" ).test ( n ); };

// PUBLIC: filter_valid_chars
function filter_valid_chars ( evt, valids, case_ins )
{
        evt = (evt) ? evt : event;

	if ( evt.charCode < 32 ) return true;

	var ccode = ( evt.charCode ) ? evt.charCode : ( ( evt.which ) ? evt.which : evt.keyCode );
        var ch = String.fromCharCode ( ccode );

        if ( case_ins )
        {
                ch = ch.toLowerCase ();
                valids = valids.toLowerCase ();
        }
                                                                                                                                                            
        if ( valids.indexOf ( ch ) == -1 ) return false;
                                                                                                                                                            
        return true;
}

liwe.validators.is_codice_fiscale = function ( value )
{
	var caratteri = new Array ("0","1","2","3","4","5","6","7","8","9","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" );
	var pari      = new Array ( 0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25 );
   	var dispari   =new Array ( 1,0,5,7,9,13,15,17,19,21,1,0,5,7,9,13,15,17,19,21,2,4,18,20,11,3,6,8,12,14,16,10,22,25,24,23 );
   	var cod = value;//.toLowerCase();
   	var check = true;
	var numeri = '';
	var lettere = '';
	var i, test, somma = 0;
	var lettera, carattere, resto, k;
	
   	if ( cod.length != 16 ) return false;

	cod = cod.toLowerCase();

	lettere = cod.substr ( 0, 6 ) + cod.substr ( 8, 1 ) + cod.substr ( 11, 1) + cod.substr ( 15 );
	numeri  = cod.substr ( 6, 2 ) + cod.substr ( 9, 2 ) + cod.substr ( 12, 3 );

	for  ( i = 0; i < 10; i++ )
		if ( lettere.charCodeAt ( i ) < 97 || lettere.charCodeAt ( i ) > 122 ) return false;


	for  ( i = 0; i < 8; i++ )
		if ( numeri.charCodeAt ( i ) < 48 || numeri.charCodeAt ( i ) > 57 ) return false;

   	//checksum del codice fiscale
   	test = cod.substr ( 15,1 );
   	somma = 0;
   	for ( i = 0; i < 16; i = i + 2 )
	{ //dispari
       		carattere = cod.substr ( i, 1 );
       		for ( k = 0; k < 36; k++ )
		{
             		if ( carattere == caratteri [ k ] )
			{
                		somma += dispari [ k ];
             			break;
          		}
       		}
    	}

    	for ( i = 1; i < 15; i= i + 2 )
	{ //pari
       		carattere = cod.substr ( i, 1 );
       		for ( k = 0; k < 36; k++ )
		{
            		if ( carattere == caratteri [ k ] )
			{
             			somma += pari [ k ]; 
             			break;
          		}
       		}
    	}

   	resto = somma % 26;
   	lettera = String.fromCharCode ( 97 + resto );            

   	if ( test != lettera ) return false;

   	return true;
};

liwe.validators.is_partita_iva = function ( pi )
{
	var i, validi, s, c;

	if ( pi == '' ) return false;

	if( pi.length != 11 )
	{
		console.warn ( "PIVA: %s - length invalid: %d", pi, pi.length );
		return false;
	}

	validi = "0123456789";

	for ( i = 0; i < 11; i++ )
	{
		if ( validi.indexOf ( pi.charAt ( i ) ) == -1 )
		{
			console.warn ( "PIVA: %s - invalid char: %s at pos: %d", pi, pi.charAt ( i ), i );
			return false;
		}
	}

	s = 0;
	for ( i = 0; i <= 9; i += 2 ) s += pi.charCodeAt ( i ) - '0'.charCodeAt ( 0 );

	for( i = 1; i <= 9; i += 2 )
	{
		c = 2 * ( pi.charCodeAt ( i ) - '0'.charCodeAt ( 0 ) );
		if ( c > 9 ) c = c - 9;
		s += c;
	}

	if( ( 10 - s % 10  ) % 10 != pi.charCodeAt ( 10 ) - '0'.charCodeAt ( 0 ) )
	{
		console.warn ( "PIVA: %s - invalid checksum" );
		return false;
	}

	return true;
};

