//Retourne true si le caractère c est un chiffre (0 .. 9)
function isDigit (c) {   
	return ((c >= "0") && (c <= "9"));
}

function espacerChiffres(v)
{
    if( v.length <= 3 ) return v;
    newv = '';
    j = -1;
    for (i = v.length; i >= 0; i--)
    {
        if( j == 3 )
        {
            newv = ' ' + newv;
            j = 0;
        }
        j++;
        c = v.charAt(i);
        newv = c + newv;

    }
    return newv;
}

function supprimerPremiersZeros(v)
{
    if( v.indexOf(',') != -1 && v.indexOf(',') < 2 ) return v;
    if( v.indexOf('.') != -1 && v.indexOf('.') < 2 ) return v;
    for (i = 0; i < v.length; i++)
    {
        c = v.charAt(i);
        if( c == 0 ) continue;
        break;
    }
    v = v.substring(i);
    return v;
}

function forceFloatVal(inp,dec,limit,espacement)
{
    inp        = forceFloatVal.arguments[0];
    dec        = forceFloatVal.arguments[1];
    limit      = forceFloatVal.arguments[2];
    espacement = forceFloatVal.arguments[3];
    
    forceAbsFloatVal(inp,dec,limit,espacement,1);
}

function AbsFloatVal(valeur)
{
    valeur  =   valeur.toString();
    valeur  =   valeur.replace('.',',');

    var virgule = valeur.lastIndexOf(',');

    if( virgule > 0 )
    {
        var vleft   =   valeur.substring(0,virgule);
        vleft       =   vleft.replace(',','');

        var vright  =   valeur.substring(virgule+1);
        vright      =   arrondir(vright,2);

        for(var cptzeros=vright.length; cptzeros<2; cptzeros++)
            vright +=   "0";

        valeur      = vleft + "," + vright;
    }
    else
        valeur      +=  ",00";

    return valeur;
}

function forceAbsFloatVal()
{
    inp        = forceAbsFloatVal.arguments[0];
    dec        = forceAbsFloatVal.arguments[1];
    limit      = forceAbsFloatVal.arguments[2];
    espacement = forceAbsFloatVal.arguments[3];
    allowneg   = forceAbsFloatVal.arguments[4];
    
    nd();
    var v = inp.value;
    var valeurInitiale = v;
    v = v.replace(/\s/g,'');
    
    if( allowneg )
    {
        var estNegatif = ( v.substring(0,1) == '-' );
        if( estNegatif ) v = v.substring(1);
    }
    
    v = supprimerPremiersZeros(v);
    var intv = '';
    valide = true;
    for (i = 0; i < v.length; i++)
    {
        var c = v.charAt(i);
        if( !isDigit(c) && c != '.' && c != ',' && c != ' ')
            valide = false;
        else
            intv += c;
    }
    if( !intv ) { inp.value = 0; return; }

    if( valide ) intv = v;

    intv = intv.replace('.',',');
    virgule = intv.lastIndexOf(',');
    if( virgule > 0 )
    {
        vleft  = intv.substring(0,virgule);
        vleft  = vleft.replace(',','');
        if( espacement )
            vleft  = espacerChiffres(vleft);
        vright = intv.substring(virgule+1);
        vright = arrondir(vright,dec);
        if( limit && vleft > limit ) vleft = limit;
        if( limit && vleft == limit )
            intv = limit;
        else
            intv   = vleft + ',' + vright
    }
    else
    {
        if( limit && intv > limit ) intv = limit;
        if( espacement )
            intv = espacerChiffres(intv);
    }
    if( allowneg && estNegatif ) intv = '-' + intv;
    inp.value = intv;
    //inp.focus();
    
    if( !valide )
    {
        overlib('Valeur incorrecte !<br /><b>' + valeurInitiale + '</b> a &eacute;t&eacute; remplac&eacute; par <b> ' + intv + '</b>', STICKY, CAPTION, 'Correction automatique', CENTER);
    }
}

function arrondir(val,dec)
{
    if( val.length <= dec ) return val;
    l = val.substring(0,dec);
    r = val.substring(dec,dec+1);
    if( r >= 5 ) l++;
    return l;
}
	
function forceAbsIntVal()
{
    inp   = forceAbsIntVal.arguments[0];
    limit = forceAbsIntVal.arguments[1];
    
    nd();
    v = inp.value;
    valeurInitiale = v;
    v = v.replace(/\s/g,'');
    v = supprimerPremiersZeros(v);
    intv = '';
    valide = true;
    for (i = 0; i < v.length; i++)
    {
        var c = v.charAt(i);
        if( !isDigit(c) )
            valide = false;
        else
            intv += c;
    }
    if( !intv ) { inp.value = 0; return; }
    if( valide ) intv = v;
    
    if( limit && intv > limit ) intv = limit;
    
    inp.value = intv;
    
    if( !valide )
    {
        overlib('Valeur incorrecte !<br /><b>' + valeurInitiale + '</b> a &eacute;t&eacute; remplac&eacute; par <b> ' + intv + '</b>', STICKY, CAPTION, 'Correction automatique', CENTER);
    }
}