var questionDatePermis;
var questionCRM;
var bDejaProposeCRM = false;
var crmEstime;
var nbAnneesCRM05Estime
var crmMin;
var estJeuneConducteur;
var moisDerniereEcheance;
var dateAuj;
var evenement;

//propose le CRM a partir de la date de permis
function proposerCRM(idSession, antecedentsCPCommeCP, crmActuel, idQuestionDatePermis, idQuestionCRM, dateCourante) {
    questionDatePermis = idQuestionDatePermis;
    questionCRM = idQuestionCRM;
    estJeuneConducteur = (antecedentsCPCommeCP == '3');
    dateAuj = dateCourante;

    var datePermisCP = '01/' + recupereReponseAvantSubmit(questionDatePermis);

    var diffDate = comparerDate(datePermisCP, dateCourante);

    //on propose un crm une seule fois, ie. pas de crm deja renseigne
    //ne rien faire pour les jeunes conducteurs car crm=1 par defaut
    if (!bDejaProposeCRM && !estJeuneConducteur && crmActuel == '' && (diffDate == "<" || diffDate == "==")) {
        DA.WEB.PS.MoteurRendu.Auto.Web.CRM.EstimerCRM(idSession, datePermisCP, OnEstimerCRMComplete, OnFailed, 'PropositionCRM');
    }
}

function OnEstimerCRMComplete(resultats, contexte) {
    crmMin = resultats.CRMMin;
    crmEstime = resultats.CRMEstime;
    nbAnneesCRM05Estime = resultats.NbAnneesCRM05Estime;

    if (contexte == 'PropositionCRM') {
        var nbAnneesCRM05Propose = nbAnneesCRM05Estime;
        if (!isNaN(nbAnneesCRM05Propose) && nbAnneesCRM05Propose >= 6) {
            nbAnneesCRM05Propose = '6';
        }
        selectionneCRM(crmEstime, nbAnneesCRM05Propose, questionCRM);

        bDejaProposeCRM = true;
    }
    else if (contexte == 'ValiderGroupeConducteurs') {
        //affiche la div d'alerte en cas d'incoherence CRM
        try {
            var crmSaisi = recupereReponseAvantSubmit(questionCRM);
            var datePermisCP = '01/' + recupereReponseAvantSubmit(questionDatePermis);
            if (crmSaisi < crmMin ||
				(crmSaisi.indexOf("0.50") != -1 && parseInt(crmSaisi.substr(4, 1)) > nbAnneesCRM05Estime)) {
                completerAlerteCRMIncoherent(crmSaisi.substr(0, 4), crmSaisi.substr(4, 1), crmEstime, nbAnneesCRM05Estime, datePermisCP, moisDerniereEcheance, dateAuj);
                montreAide('CRMIncoherent');
                cacheDiv('divAttente');
                return false;
            }
            else {
                valider(evenement);
            }
        }
        catch (Exception) {
            valider(evenement);
        }
    }
}

function OnValiderGroupeFailed(error)
{
    //on fait un postback quand meme s'il y a un probleme d'ajax pour ne pas rester bloque sur la page
    valider(evenement);
}

//valide le groupe Conducteurs et affiche eventuellement la div d'alerte en cas d'incoherence CRM
function validerGroupeConducteurs(event, idSession, antecedentsCPCommeCP, idQuestionDatePermis, idQuestionCRM, dateCourante, moisDerniereEcheancePrincipale) {
    attente();
    questionDatePermis = idQuestionDatePermis;
    questionCRM = idQuestionCRM;
    estJeuneConducteur = (antecedentsCPCommeCP == '3');
    moisDerniereEcheance = moisDerniereEcheancePrincipale;
    dateAuj = dateCourante;
    evenement = event;
    
    var datePermisCP = '01/' + recupereReponseAvantSubmit(questionDatePermis);

    var diffDate = comparerDate(datePermisCP, dateCourante);
    if (!estJeuneConducteur && (diffDate == "<" || diffDate == "==")) {
        DA.WEB.PS.MoteurRendu.Auto.Web.CRM.EstimerCRM(idSession, datePermisCP, OnEstimerCRMComplete, OnValiderGroupeFailed, 'ValiderGroupeConducteurs');
    }
    else {
        valider(evenement);
    }
}

function completerAlerteCRMIncoherent(crmSaisi, nbAnneesCRM05Saisi, crmApplique, nbAnneesCRM05Applique, datePermis, moisDerniereEcheance, dateAuj) {
    ajouteTexte(afficherDateMMAAAA(datePermis), 'spanCRMIncoherentDatePermis', false);

    ajouteTexte(calculePourcentageBonus(crmSaisi), 'spanCRMIncoherentPourcentageCRM', false);
    ajouteTexte(crmSaisi, 'spanCRMIncoherentCRM', false);
    txtNbAnnees05 = '';
    if (parseFloat(crmSaisi) == 0.5) {
        if (nbAnneesCRM05Saisi == 1) {
            txtNbAnnees05 = 'depuis 1 an';
        }
        else if (nbAnneesCRM05Saisi == 6) {
            txtNbAnnees05 = 'depuis 6 ans et plus';
        }
        else if (nbAnneesCRM05Saisi < 6 && nbAnneesCRM05Saisi > 1) {
            txtNbAnnees05 = 'depuis ' + nbAnneesCRM05Saisi + ' ans';
        }
    }
    ajouteTexte(txtNbAnnees05, 'spanCRMIncoherentNbAnnees05', false);

    ajouteTexte(calculePourcentageBonus(crmApplique), 'spanCRMIncoherentPourcentageCRMPropose', false);
    ajouteTexte(transformeCRM(crmApplique), 'spanCRMIncoherentCRMPropose', false);
    txtNbAnnees05Applique = '';
    if (parseFloat(crmApplique) == 0.5) {
        if (nbAnneesCRM05Applique == 1) {
            txtNbAnnees05Applique = 'depuis 1 an';
        }
        else if (nbAnneesCRM05Applique >= 6) {
            txtNbAnnees05Applique = 'depuis 6 ans et plus';
        }
        else if (nbAnneesCRM05Applique < 6 && nbAnneesCRM05Applique > 1) {
            txtNbAnnees05Applique = 'depuis ' + nbAnneesCRM05Applique + ' ans';
        }
    }
    ajouteTexte(txtNbAnnees05Applique, 'spanCRMIncoherentNbAnnees05Propose', false);
}