﻿var isValid = false;
Type.registerNamespace('SimulateurMVE.Web');

SimulateurMVE.Web.Tools = function() {
    SimulateurMVE.Web.Tools.initializeBase(this);
    this._btnCalculerOnClickHandler = null;    
    this._btnCalculer = null;
    this._btnCalculerDummy = null;
    
    this._pnlCriteresKeyUpHandler = null;    
    this._pnlCriteresKeyDownHandler = null;    
    this._pnlCriteres = null;
    
    this._pnlObjectifKeyUpHandler = null;
    this._pnlObjectifKeyDownHandler = null;
    this._pnlObjectif = null;
    
    this._input1 = null;
    this._input2 = null;
    this._duree = null;
    
    this._objectifClass = null;
    
    this._simulation = SimulateurMVE.Web.Tools.Simulations.Capital;
}

SimulateurMVE.Web.Tools.prototype = {

    initialize : function() {
        SimulateurMVE.Web.Tools.callBaseMethod(this, 'initialize');   
        
        this._btnCalculerOnClickHandler = Function.createDelegate(this, this._onClick);
        $addHandler(this._btnCalculer, 'click', this._btnCalculerOnClickHandler);

        this._pnlCriteresKeyUpHandler = Function.createDelegate(this, this._keyUp);
        $addHandler(this._pnlCriteres, 'keyup', this._pnlCriteresKeyUpHandler);

        this._pnlCriteresKeyDownHandler = Function.createDelegate(this, this._keyDown);
        $addHandler(this._pnlCriteres, 'keydown', this._pnlCriteresKeyDownHandler);
        
        var pnlObjectifJQuery = $('.' + this._objectifClass);
        if (pnlObjectifJQuery !== null && pnlObjectifJQuery.length > 0) {
            this._pnlObjectif = pnlObjectifJQuery[0];
            this._pnlObjectifKeyUpHandler = Function.createDelegate(this, this._keyUp);
            $addHandler(this._pnlObjectif, 'keyup', this._pnlObjectifKeyUpHandler);

            this._pnlObjectifKeyDownHandler = Function.createDelegate(this, this._keyDown);
            $addHandler(this._pnlObjectif, 'keydown', this._pnlObjectifKeyDownHandler);
        }
        
        this._input1.focus();
    },
    
    
    dispose: function() {        
        $removeHandler(this._btnCalculer, 'click', this._btnCalculerOnClickHandler);
        this._btnCalculerOnClickHandler = null;
        
        $removeHandler(this._pnlCriteres, 'keyup', this._pnlCriteresKeyUpHandler);
        this._pnlCriteresKeyUpHandler = null;
        
        $removeHandler(this._pnlCriteres, 'keydown', this._pnlCriteresKeyDownHandler);
        this._pnlCriteresKeyDownHandler = null;
    
        if (this._pnlObjectif  !== null) {
    
            $removeHandler(this._pnlObjectif, 'keyup', this._pnlObjectifKeyUpHandler);
            this._pnlObjectifKeyUpHandler = null;
            
            $removeHandler(this._pnlObjectif, 'keydown', this._pnlObjectifKeyDownHandler);
            this._pnlObjectifKeyDownHandler = null;
        }
    
        SimulateurMVE.Web.Tools.callBaseMethod(this, 'dispose');
    }, 
    
    
    
 
    _keyUp : function(e) {
        var upKey = e.charCode || e.keyCode || -1;
        if (isValid && upKey == 13) {
            this._btnCalculerDummy.click();
        }
    },
    
    _keyDown : function(e) {    
        var downKey = e.charCode || e.keyCode || -1;
        if (downKey == 13) {
            isValid = this.valider();
        }
    },
    
    _onClick : function(e)
    {
        isValid = this.valider();
        if (isValid)
            this._btnCalculerDummy.click();
    },         
         
    
    valider : function() {
        if (!this._validaterObjectifs()) {
            this._gererError(SimulateurMVE.Web.Tools.Res.objectifEpargne, this._input1);
            return false;
        }
    
        switch (this._simulations) {
            case SimulateurMVE.Web.Tools.Simulations.Capital :                 
                return this._validerSimulationCapital();                
            case SimulateurMVE.Web.Tools.Simulations.VM : 
                return this._validerSimulationVM();                
            default : return false;
        }
    },
        
    _validaterObjectifs : function() {
        var checked = true;
        var radioGroup = $('.' + this._objectifClass + ' input[@type=radio]');
        if (radioGroup != null && radioGroup.length > 0) {
            checked = false;       
            radioGroup.each(function(index, radioElement) {
                if($(radioElement).attr('checked')) {
                    checked = true;
                    return false;
                }
            });
        }
        return checked;
    },
    
    _validerSimulationCapital : function() { 
        var valeurVIString = this._input1.value;
        var valeurVMString = this._input2.value;
        var valeurVI = 0;
        var valeurVM = 0;
        if (valeurVIString.length > 0) 
            valeurVI = parseFloat(valeurVIString);
        if (valeurVMString.length > 0)
            valeurVM = parseFloat(valeurVMString);
            
            
        if (valeurVMString.length == 0 && valeurVIString.length == 0) {
            this._gererError(SimulateurMVE.Web.Tools.Res.valeursNulles, this._input1);
            return false;
        }                          
    
        if (isNaN(valeurVM))
        {
            this._gererError(SimulateurMVE.Web.Tools.Res.valeurVersementMensuelSim1, this._input2);
            return false;
        }
        if (isNaN(valeurVI))
        {
            if (valeurVM > 0)
                this._gererError(SimulateurMVE.Web.Tools.Res.valeurVersementInitial1Sim1, this._input1);
            else     
                this._gererError(SimulateurMVE.Web.Tools.Res.valeurVersementInitial2Sim1, this._input1);
            return false;
        }
        
        if ((valeurVM != 0) && (valeurVM < 45.0 || valeurVM > 49999)) {
            this._gererError(SimulateurMVE.Web.Tools.Res.valeurVersementMensuelSim1, this._input2);
            return false;
        }
        if ((valeurVM >= 45) && (valeurVI < valeurVM)) {
            this._gererError(SimulateurMVE.Web.Tools.Res.valeurVersementInitial1Sim1, this._input1);
            return false;
        }
        if ((valeurVM == 0) && (valeurVI < 500.0)) {
            this._gererError(SimulateurMVE.Web.Tools.Res.valeurVersementInitial2Sim1, this._input1);
            return false;
        }      
        if (valeurVI > 9999999) {
            this._gererError(SimulateurMVE.Web.Tools.Res.valeurVersementInitial2Sim1, this._input1);
            return false;
        }       
        this._clearError();             
        return true; 
    },
    
    _validerSimulationVM : function() {                
        var valeurCSString = this._input1.value;
        var valeurVLString = this._input2.value;
        var valeurCS = 0;
        var valeurVL = 0;
        if (valeurCSString.length > 0) 
            valeurCS = parseFloat(valeurCSString);
        if (valeurVLString.length > 0)
            valeurVL = parseFloat(valeurVLString);
            
            
        if (valeurCSString.length == 0 && valeurVLString.length == 0) {
            this._gererError(SimulateurMVE.Web.Tools.Res.valeursNulles, this._input1);
            return false;
        }
        
        if (isNaN(valeurCS))
        {
            this._gererError(SimulateurMVE.Web.Tools.Res.valeurCapitalSouhaite, this._input1);
            return false;
        }
        if (isNaN(valeurVL))
        {
            this._gererError(SimulateurMVE.Web.Tools.Res.valeurVersementLibre, this._input2);         
            return false;
        }
        if (valeurCS > 9999999) {
            this._gererError(SimulateurMVE.Web.Tools.Res.valeurCapitalSouhaite, this._input1);
            return false;
        }

        if ((valeurVL != 0) && (valeurVL > 9999999)) {            
            this._gererError(SimulateurMVE.Web.Tools.Res.valeurVersementLibre, this._input2);
            return false;
        }        
        
        this._clearError();             
        return true; 
    },
    
    _gererError : function(message, inputFocus) {
        this._writeError(message);        
        inputFocus.focus(); 
        var pos = inputFocus.value.length;  
        SetSelectionRange(inputFocus, pos, pos);    
    },    
    
    
    _clearError : function() {
        this._write('', 'invisible');
    },
    
    _writeError : function(text) {
        this._write(text, 'error');
    },        
    
    _write : function(text, className) {        
        var element = this._getErrorMessageElement();
        if (element.size() < 1) {
            if (text && text != '') {
                alert(text);
            }
            return;
        }
        
        element.removeClass().addClass(className);
        element.find('div').not('[@class]').each(function(index) {
            $(this).html(text);
        });
    },
    
    _getErrorMessageElement : function() {
        return $('div[@class] > div.image').parent('div');
    }, 
    
    
    
    
    
    
    
    
    
    
    
    
    /** Getters/Setters **/
    
    get_btnCalculer: function() {
        return this._btnCalculer;
    },
    
    set_btnCalculer: function(value) {
        if (this._btnCalculer !== value) {
            this._btnCalculer = value;
            this.raisePropertyChanged('btnCalculer');
        }
    },
    
    get_btnCalculerDummy: function() {
        return this._btnCalculerDummy;
    },
    
    set_btnCalculerDummy: function(value) {
        if (this._btnCalculerDummy !== value) {
            this._btnCalculerDummy = value;
            this.raisePropertyChanged('btnCalculerDummy');
        }
    },
    
    get_pnlCriteres: function() {
        return this._pnlCriteres;
    },
    
    set_pnlCriteres: function(value) {
        if (this._pnlCriteres !== value) {
            this._pnlCriteres = value;
            this.raisePropertyChanged('pnlCriteres');
        }
    },
    
    get_input1: function() {
        return this._input1;
    },
    
    set_input1: function(value) {
        if (this._input1 !== value) {
            this._input1 = value;
            this.raisePropertyChanged('input1');
        }
    },
    
    get_input2: function() {
        return this._input2;
    },
    
    set_input2: function(value) {
        if (this._input2 !== value) {
            this._input2 = value;
            this.raisePropertyChanged('input2');
        }
    },
    
    get_duree: function() {
        return this._duree;
    },
    
    set_duree: function(value) {
        if (this._duree !== value) {
            this._duree = value;
            this.raisePropertyChanged('duree');
        }
    },
    
    get_objectifClass: function() {
        return this._objectifClass;
    },
    
    set_objectifClass: function(value) {
        if (this._objectifClass !== value) {
            this._objectifClass = value;
            this.raisePropertyChanged('objectifClass');
        }
    },
    
    get_simulations: function() {
        return this._simulations;
    },
    
    set_simulations: function(value) {
        if (this._simulations !== value) {
            this._simulations = value;
            this.raisePropertyChanged('simulations');
        }
    }
    
    
    
    
    
};

SimulateurMVE.Web.Tools.registerClass('SimulateurMVE.Web.Tools', Sys.Component);
Sys.Application.notifyScriptLoaded();



SimulateurMVE.Web.Tools.Simulations = function() {
    throw Error.invalidOperation();
}
SimulateurMVE.Web.Tools.Simulations.prototype = {
        Capital : 0x1,
        VM : 0x2        
}
SimulateurMVE.Web.Tools.Simulations.registerEnum('SimulateurMVE.Web.Tools.Simulations', true);




SimulateurMVE.Web.Tools.Res={
'valeurCapitalSouhaite':'Le capital souhait&#233; doit &#234;tre inf&#233;rieur &#224; 10 millions d\'&#8364;',
'valeurVersementInitial1Sim1':'Le versement initial doit &#234;tre sup&#233;rieur ou &#233;gal au versement mensuel',
'valeurVersementInitial2Sim1':'Le versement initial doit &#234;tre compris entre 500 &#8364; et 9 999 999 &#8364;',
'valeurVersementLibre':'Le versement libre doit &#234;tre inf&#233;rieur &#224; 10 millions d\'&#8364;',
'valeurVersementMensuelSim1':'Le versement mensuel doit &#234;tre &#233;gal &#224; 0 ou compris entre 45 &#8364; et 49 999 &#8364;',
'versementLibre':'Versement libre',
'versementInitial':'Versement initial',
'versementMensuel':'Versement mensuel',
'capitalSouhaite':'Capital souhait&#233;',
'valeursNulles':'Saisir des valeurs pour la simulation',
'objectifEpargne':'Un objectif d\'&#233pargne doit &#234;tre choisi'
};
