/*	JavaScript string functions
 *
 */
function trim( str ){
	if( !str ) return '';
	str = str.replace( /^\s/g, '' );
	str = str.replace( /\s$/g, '' );
	return str;
}
// Détermine si la chaine de caractère passée en paramètre correspond à une date au format jj/mm/aaaa
function validDate( str ){
	if( !str ) return false;
	return str.match( /^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$/ );
}
// Détermine si la chaine de caractère passée en paramètre correspond à une heure au format hh:mm
function validHour( str ){
	if( !str ) return false;
	return str.match( /^[0-9]{1,2}:[0-9]{1,2}$/ );
}
// Retourne un booléen indiquant si la chaine passée en paramètre contient une adresse email valide.
function validEmail( str ){
	return str.match( /^[a-z0-9\-\_]+(\.[a-z0-9\-\_]+)*@[a-z0-9\-\_]+(\.[a-z0-9\-\_]+)*$/i );
}
// Retourne vrai si la chaîne de caractère passée en paramètre contient un entier non signé
function validInt( str ){
	return str.match( /^[0-9]+$/ );
}
// Retourne vrai si la chaîne de caractère passée en paramètre contient un nombre à virgule flottante non signé
function validFloat( str ){
	str = str.replace( /\s/g, '' );
	return str.match( /^[0-9]+(\.|,[0-9]+)?$/ );
}

// Retourne vrai si la chaîne passée en paramètre contient un SIRET valide
function validSIRET( str ){
	str = str.replace( /\s/g, '' );
	return str.match( /^[0-9]{14}$/ );
}

// Complète la fonction parseInt en lui permettant de traiter des nombres mis en forme
function parseFInt(str){
	str = str.replace( /\s/g, '' );
	if( str.match( /^0+$/ ) ) return 0;
	str = str.replace( /^0+/, '' );
	return parseInt(str);
}
// Complète la fonction parseFloat en lui permettant de traiter des nombres mis en forme
function parseFFloat(str){
	return parseFloat(str.replace( /\s/g, '' ).replace( ',', '.' ));
}
// Convertie une date et heure sous forme de chaîne en date/heure native
function parseDateTime(str){
	var re = /([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4}) ([0-9]{1,2}):([0-9]{1,2})/;
	var dparts = re.exec( str );
	return new Date( parseFInt(dparts[3]), parseFInt(dparts[2])-1, parseFInt(dparts[1]), parseFInt(dparts[4]), parseFInt(dparts[5]) );
}
// Retourne vrai si la chaîne passée en argument contient un mot de passe valide.
function validPassword(str){
	return str.match( /^[a-z0-9\-\_\=\.]{6,16}$/i );
}

function validCardNumber(str){
	str = str.replace( /[^0-9]/g, '' );
	if( str=='' ) return false;
	str = reversestr( str );
	return mod10(str)==0;
}

function reversestr( str ){
	var res = '';
	for( var i=str.length-1; i>=0; i-- )
		res += str[i];
	return res;
}

function mod10(number){
	var sum = 0;
	for(var i=0; i<number.length; i++ ){
		var digit = number.charCodeAt(i)-48;
		if( (i % 2)==1 )
			digit *= 2;
		if( digit>9 )
			digit = Math.floor(digit/10) + (digit-10);
		sum += digit;
	}
	return sum % 10;
}
//Prend en charge le formatage d'un nombre
function number_format( number, decimals, sep, th_sep ) {
	if( number=='' || isNaN(number) ) return '';
	number = Math.round( number * Math.pow(10, decimals) ) / Math.pow(10, decimals);
	str_number = number.toString();
	arr_int = str_number.split(".");
	if( !arr_int[0] ) arr_int[0] = "0";
	if( !arr_int[1] ) arr_int[1] = "";
	if( arr_int[1].length < decimals){
		nachkomma = arr_int[1];
		for( i=arr_int[1].length+1; i<=decimals; i++) nachkomma += "0";
		arr_int[1] = nachkomma;
	}
	if( th_sep!="" && arr_int[0].length>3 ){
		Begriff = arr_int[0];
		arr_int[0] = "";
		for( j=3; j<Begriff.length; j+=3 ){
			Extrakt = Begriff.slice(Begriff.length - j, Begriff.length - j + 3);
			arr_int[0] = th_sep + Extrakt +  arr_int[0] + "";
		}
		str_first = Begriff.substr(0, (Begriff.length % 3 == 0)?3:(Begriff.length % 3));
		arr_int[0] = str_first + arr_int[0];
	}
	if( decimals==0 )
		return arr_int[0];
	else
		return arr_int[0]+sep+arr_int[1]
	
}
// Met en forme une heure
function formatHour(hour){
	hour = trim(hour);
	if( hour.match( /^[0-9]{1}$/ ) )
		return '0' + hour + ':00';
	else if( hour.match( /^[0-9]{2}$/ ) )
		return hour + ':00';
	else if( hour.match( /^[0-9]{1}:[0-9]{1}$/ ) )
		return hour.replace( /^([0-9]{1}):([0-9]{1})$/, '0$1:0$2' );
	else if( hour.match( /^[0-9]{1}:[0-9]{2}$/ ) )
		return '0' + hour;
	else
		return hour;
}
