/**
 *  @file                   LSVCalculator.js
 *  @purpose                Classe "calculette"
 *  @author                 Patrick Boens
 *  @company                Lato Sensu Management
 *                          Rue Bois des Mazuis, 47
 *                          5070 Vitrival
 *                          Belgium
 *                          http://www.latosensu.be
 *                          Vae Soli! : http://www.vaesoli.org
 *  @creationdate           09/03/2009 - 20:22
 *  @modificationdate       11/03/2009 - 14:41
 *  @license                http://creativecommons.org/licenses/by-sa/2.0/be/
 *
 *                          Pour obtenir les informations détaillées des
 *                          termes de licence, nous vous demandons de bien
 *                          vouloir vous en référer au texte intégral
 *                          fourni dans le fichier LSCopyright.php
 *
 *  ------------------------------------------------------------------------
 *  Changes History     :
 *  ........................................................................
 *  09/03/2009          :   1)  Original creation
 *  v. 2.0.0005
 *  ------------------------------------------------------------------------
*/
function LSCalculator( szResultID )
/*-------------------------------*/
{
    this.value      =   '0';
    this.member1    =   '';
    this.op         =   '';
    this.oResult    =   document.getElementById( szResultID );
    this.opera      =   function( szOp )
                        {
                            if ( this.oResult )
                            {
                                switch ( szOp )
                                {
                                    case '.'    :   if ( this.value.indexOf( '.' ) == -1 )
                                                    {
                                                        this.value += '.';
                                                        this.oResult.innerHTML = this.value;
                                                    }
                                                    break;
                                    case '0'    :
                                    case '1'    :
                                    case '2'    :
                                    case '3'    :
                                    case '4'    :
                                    case '5'    :
                                    case '6'    :
                                    case '7'    :
                                    case '8'    :
                                    case '9'    :   n = parseFloat( szOp );
                                                    if ( this.value == '0' )
                                                    {
                                                        this.value = szOp;
                                                    }
                                                    else
                                                    {
                                                        this.value = this.value + szOp;
                                                    }
                                                    this.oResult.innerHTML = this.value;
                                                    break;
                                
                                    case 'int' :    this.value = '' + parseInt( this.value );
                                                    this.oResult.innerHTML = this.value;
                                                    break;
                                
                                    case 'frac' :   if ( ( pos = this.value.indexOf( '.' ) ) != -1 )
                                                    {
                                                        this.value = '0' + this.value.substr( pos );
                                                        this.oResult.innerHTML = this.value;
                                                    }
                                                    break;
                                
                                    case 'back' :   if ( this.value.length > 0 )
                                                    {
                                                        this.value = this.value.substr( 0,this.value.length - 1 );
                                                    }
                                
                                                    if ( this.value.length > 0 )
                                                        this.oResult.innerHTML = this.value;
                                                    else
                                                        this.oResult.innerHTML = '0';
                                
                                                    break;

                                    case 'c'    :   this.value      = '0';
                                                    this.member1    = '';
                                                    this.op         = '';
                                                    this.oResult.innerHTML = '0';
                                                    break;

                                    case 'ce'   :   this.value      = '0';
                                                    this.oResult.innerHTML = '0';
                                                    break;
                                
                                    case '+/-'  :   if ( this.value.indexOf( '-' ) != -1 )
                                                    {
                                                        //this.value = this.value.substr( 1 );
                                                        this.value = this.value.replace( '-','' );
                                                    }
                                                    else
                                                    {
                                                        this.value = '-' + this.value;
                                                    }
                                                    this.oResult.innerHTML = this.value;
                                                    break;
                                
                                    case '1/x'  :   if ( this.value != '0' )
                                                    {
                                                        this.value = '' + ( 1 / parseFloat( this.value ) );
                                                        this.oResult.innerHTML = this.value;
                                                    }
                                                    break;
                                
                                    case 'sqrt' :   this.value = '' + Math.sqrt( parseFloat( this.value ) );
                                                    this.oResult.innerHTML = this.value;
                                                    break;
                                
                                    case 'x2'   :   this.value = '' + Math.pow( parseFloat( this.value ),2 );
                                                    this.oResult.innerHTML = this.value;
                                                    break;
                                
                                    case '+'    :   
                                    case '-'    :
                                    case '/'    :
                                    case '*'    :   this.member1 = this.value;
                                                    this.value   = '0';
                                                    this.op      = szOp;
                                                    //this.oResult.innerHTML = this.value;
                                                    break;
                                    case '='    :   switch ( this.op )
                                                    {
                                                        case    '+':    this.value = '' + ( parseFloat( this.member1 ) + parseFloat( this.value ) );
                                                                        this.oResult.innerHTML = this.value;
                                                                        break;
                                                        case    '-':    this.value = '' + ( parseFloat( this.member1 ) - parseFloat( this.value ) );
                                                                        this.oResult.innerHTML = this.value;
                                                                        break;
                                                        case    '*':    this.value = '' + ( parseFloat( this.member1 ) * parseFloat( this.value ) );
                                                                        this.oResult.innerHTML = this.value;
                                                                        break;
                                                        case    '/':    this.value = '' + ( parseFloat( this.member1 ) / parseFloat( this.value ) );
                                                                        this.oResult.innerHTML = this.value;
                                                                        break;
                                                    }
                                                    this.member1 = '';
                                                    this.op      = '';
                                                    break;
                                }
                            }
                        }   /* End of LSCalculator.opera() */
                        /* -------------------------------------- */
}   /* End of LSCalculator class */
/* ============================================================== */

