﻿// Assurland moto form javascript ressources

// Business data
// ...

/////////////////
// Page 1 (moto & cyclo)
/////////////////

// UI element's ID(s) page 1
var moto_rblMotoTypeId;
var moto_rblIsCycloId;
var moto_ddlOwningStatusId;
var moto_rowGreyCardOwnerId;
var moto_rblGreyCardOwnerId;
var moto_lblGreyCardOwnerId;
var moto_rowAddAgeId;
var moto_rblAddAgeId;
var moto_rowGuaranteeCodeId;
var moto_rblGuaranteeCodeId;
var moto_rowGuaranteeCodeCycloId;
var moto_rblGuaranteeCodeCycloId;

function moto_updateGreyCardOwnerRow()
{
     var isCyclo = radioButtonList_getSelectedValue(moto_rblIsCycloId);
     var owningStatus = $get(moto_ddlOwningStatusId).value;
     
     if(isCyclo == 1)
     {
        // Hide row
        hideRow(moto_rowGreyCardOwnerId);
        radioButtonList_clearError(moto_rblGreyCardOwnerId);
     }
     else
     {
        if(owningStatus == 3 || owningStatus == 4 || owningStatus == 5) {
            // Set label
            if (al_siteMode == 3)
                setInnerHtml(moto_lblGreyCardOwnerId, "Serez-vous le titulaire de la carte grise française de la moto ?");
            else if(al_siteMode == 4)
                setInnerHtml(moto_lblGreyCardOwnerId, "Vous serez le titulaire de la carte grise :");
            else
                setInnerHtml(moto_lblGreyCardOwnerId, "Serez-vous le titulaire de la carte grise ?"); 
        }
        else
        {
            // Set label
            if (al_siteMode == 3)
                setInnerHtml(moto_lblGreyCardOwnerId, "Etes-vous le titulaire de la carte grise française de la moto ?");
            else if (al_siteMode == 4)
                setInnerHtml(moto_lblGreyCardOwnerId, "Vous êtes le titulaire de la carte grise :");
            else
                setInnerHtml(moto_lblGreyCardOwnerId, "Etes-vous le titulaire de la carte grise ?");
        }
        // Show row
        showRow(moto_rowGreyCardOwnerId);
     }
}
function moto_updateAddAgeRow()
{
    var motoType = radioButtonList_getSelectedValue(moto_rblMotoTypeId);
    var owningStatus = $get(moto_ddlOwningStatusId).value;
    
    if(owningStatus == 3 || owningStatus == 4 || owningStatus == 5 || owningStatus == 6)
    {
        var str = "";
        switch(motoType)
        {
            case "1" : str = "Neuve"; break;
            case "2" : str = "Neuf"; break;
            case "3" : str = "Neuf"; break;
            case "4" : str = "Neuf"; break;
            default : str = "#";
        }
        radioButtonList_updateLabelByValue(moto_rblAddAgeId, "1", str)
        showRow(moto_rowAddAgeId);
    }   
    else
    {
        hideRow(moto_rowAddAgeId);
        radioButtonList_clearError(moto_rblAddAgeId);
    } 
}

function moto_updateGuaranteeCodeRow()
{
    var isCyclo = radioButtonList_getSelectedValue(moto_rblIsCycloId);
    
    if(isCyclo == 0)
    {
        showRow(moto_rowGuaranteeCodeId);
        hideRow(moto_rowGuaranteeCodeCycloId);
        radioButtonList_clearError(moto_rblGuaranteeCodeCycloId)
    }   
    else
    {
        hideRow(moto_rowGuaranteeCodeId);
        radioButtonList_clearError(moto_rblGuaranteeCodeId)
        showRow(moto_rowGuaranteeCodeCycloId);
    } 
}
function moto_updateOwningStatusList()
{
    var ddlOwningStatus = $get(moto_ddlOwningStatusId);
    var motoType = radioButtonList_getSelectedValue(moto_rblMotoTypeId);

    // Update label
    if (al_siteMode == 4) {
        switch (motoType) {
            case "1":
                ddlOwningStatus.options[1].text = "une moto que je possède déjà";
                ddlOwningStatus.options[2].text = "une nouvelle moto, en remplacement de la mienne";
                ddlOwningStatus.options[3].text = "une nouvelle moto, en plus de la mienne";
                ddlOwningStatus.options[4].text = "ma première moto";
                break;
            case "2":
                ddlOwningStatus.options[1].text = "un scooter que je possède déjà";
                ddlOwningStatus.options[2].text = "un nouveau scooter, en remplacement du mien";
                ddlOwningStatus.options[3].text = "un nouveau scooter, en plus du mien";
                ddlOwningStatus.options[4].text = "mon premier scooter";
                break;
            case "3":
                ddlOwningStatus.options[1].text = "un quad que je possède déjà";
                ddlOwningStatus.options[2].text = "un nouveau quad, en remplacement du mien";
                ddlOwningStatus.options[3].text = "un nouveau quad, en plus du mien";
                ddlOwningStatus.options[4].text = "mon premier quad";
                break;
            case "4":
                ddlOwningStatus.options[1].text = "un cyclo que je possède déjà";
                ddlOwningStatus.options[2].text = "un nouveau cyclo, en remplacement du mien";
                ddlOwningStatus.options[3].text = "un nouveau cyclo, en plus du mien";
                ddlOwningStatus.options[4].text = "mon premier cyclo";
                break;
        }
    }
    else {
        switch (motoType) {
            case "1":
                ddlOwningStatus.options[1].text = "d'une moto que vous possédez actuellement";
                ddlOwningStatus.options[2].text = "d'une nouvelle moto, en remplacement de la vôtre";
                ddlOwningStatus.options[3].text = "d'une nouvelle moto, en plus de la vôtre";
                ddlOwningStatus.options[4].text = "d'une première moto";
                break;
            case "2":
                ddlOwningStatus.options[1].text = "d'un scooter que vous possédez actuellement";
                ddlOwningStatus.options[2].text = "d'un nouveau scooter, en remplacement du vôtre";
                ddlOwningStatus.options[3].text = "d'un nouveau scooter, en plus du vôtre";
                ddlOwningStatus.options[4].text = "d'un premier scooter";
                break;
            case "3":
                ddlOwningStatus.options[1].text = "d'un quad que vous possédez actuellement";
                ddlOwningStatus.options[2].text = "d'un nouveau quad, en remplacement du vôtre";
                ddlOwningStatus.options[3].text = "d'un nouveau quad, en plus du vôtre";
                ddlOwningStatus.options[4].text = "d'un premier quad";
                break;
            case "4":
                ddlOwningStatus.options[1].text = "d'un cyclo que vous possédez actuellement";
                ddlOwningStatus.options[2].text = "d'un nouveau cyclo, en remplacement du vôtre";
                ddlOwningStatus.options[3].text = "d'un nouveau cyclo, en plus du vôtre";
                ddlOwningStatus.options[4].text = "d'un premier cyclo";
                break;
        }
    }
}

function moto_updateMotoType(motoType)
{
    moto_updateOwningStatusList();
    moto_updateAddAgeRow();

    // Select default value for cyclo if possible
    if (motoType == 4) {
        if (radioButtonList_isCurrentValueIsDefaultValue(moto_rblIsCycloId)) {
            radioButtonList_setSelectedValue(moto_rblIsCycloId, 1);
            moto_updateIsCyclo(1);
        }
    }
}
function moto_updateIsCyclo(isCyclo) {
    moto_updateOwningStatusList();
    moto_updateGreyCardOwnerRow();
    moto_updateGuaranteeCodeRow();
}
function moto_updateOwningStatus(owningStatus)
{
    moto_updateGreyCardOwnerRow();
    moto_updateAddAgeRow();
}

/////////////////
// Page 2 (moto)
/////////////////

// UI element's ID(s) page 2
var moto_rowMotoDrivLicenceDateId;
var moto_rowCarDrivLicenceDateId;
var moto_rowDrivLicenceSuspId;
var moto_rowDrivLicenceSuspLastThreeYearsId;
var moto_rowDrivLicenceSuspDurationId;
var moto_rowDrivLicenceSuspCauseId;
var moto_rowDrivLicenceSuspIsRepeatingId;
var moto_rowContractCancelId;
var moto_rowContractCancelLastThreeYearsId;
var moto_rowDuiId;
var moto_rowDuiLastThreeYearsId;
var moto_rowDuiMeasuringApparatusId;
var moto_rowDuiAlcoholRateId;
var moto_rowDuiWithDisasterId;
var moto_rowDuiIsRepeatingId;
var moto_rowIsDrugPositiveId;
var moto_rowIsDrugPositiveLastThreeYearsId;
var moto_rowIsRunningAwayOrRefusingOrderId;
var moto_rowIsRunningAwayOrRefusingOrderLastThreeYearsId;
var moto_tbBirthDateId;
var moto_ddlDrivLicenceTypeId;
var moto_ddlMotoDrivLicenceDateId;
var moto_ddlCarDrivLicenceDateId;
var moto_ddlDrivLicenceSuspId;
var moto_rblDrivLicenceSuspLastThreeYearsId;
var moto_ddlDrivLicenceSuspDurationId;
var moto_ddlDrivLicenceSuspCauseId;
var moto_rblDrivLicenceSuspIsRepeatingId;
var moto_rblContractCancelLastThreeYearsId;
var moto_rblDuiId;
var moto_rblDuiLastThreeYearsId;
var moto_rblDuiMeasuringApparatusId;
var moto_ddlDuiAlcoholRateIdId;
var moto_ddlDuiWithDisasterId;
var moto_rblDuiIsRepeatingId;
var moto_rblIsDrugPositiveId;
var moto_rblIsDrugPositiveLastThreeYearsId;
var moto_rblIsRunningAwayOrRefusingOrderId;
var moto_rblIsRunningAwayOrRefusingOrderLastThreeYearsId;
var moto_lblDrivLicenceSuspDurationId;
var moto_lblDrivLicenceSuspCauseId;
var moto_ddlTitleId;

function moto_vdDrivLicenseDate()
{

    if ( $get(moto_ddlDrivLicenceTypeId).value > 1 )
    {
        var dLicenseDate = dateDropDownList_getDate(moto_ddlCarDrivLicenceDateId);
        if (dLicenseDate==null)
        {
            var dBirthDate = getDateByFormat($get(moto_tbBirthDateId).value, 'dd/MM/yyyy');
            if (isValidDate(dBirthDate)) {                
                var dBirthDateMonth = dBirthDate.getMonth();
                var dBirthDateYear = dBirthDate.getFullYear() + 18;
                dBirthDate = new Date(dBirthDateYear, dBirthDateMonth, 1);
                dateDropDownList_setDate(moto_ddlCarDrivLicenceDateId,dBirthDate);
            }
        }
    }
}

function moto_updateDrivLicenceType(value)
{
    if(value == "1")
    {
        showRow(moto_rowMotoDrivLicenceDateId);
        hideRow(moto_rowCarDrivLicenceDateId);
        dateDropDownList_clearError(moto_ddlCarDrivLicenceDateId);
    }
    else if(value == "2")
    {
        hideRow(moto_rowMotoDrivLicenceDateId);
        dateDropDownList_clearError(moto_ddlMotoDrivLicenceDateId);
        showRow(moto_rowCarDrivLicenceDateId);
         moto_vdDrivLicenseDate();
    }
    else if(value == "3")
    {
        showRow(moto_rowMotoDrivLicenceDateId);
        showRow(moto_rowCarDrivLicenceDateId);
        moto_vdDrivLicenseDate();
    }
    else
    {
        hideRow(moto_rowMotoDrivLicenceDateId);
        dateDropDownList_clearError(moto_ddlCarDrivLicenceDateId);
        hideRow(moto_rowCarDrivLicenceDateId);
        dateDropDownList_clearError(moto_ddlMotoDrivLicenceDateId);
    }
}
function moto_updateContractCancel(value)
{
    if(value == "1" || value == "3" || value == "4")
    {
        showRow(moto_rowContractCancelLastThreeYearsId);
    }
    else {
        hideRow(moto_rowContractCancelLastThreeYearsId);
        radioButtonList_clearError(moto_rblContractCancelLastThreeYearsId);
    }
}
function moto_updateDui(value)
{
    if(value == "1")
    {
        showRow(moto_rowDuiLastThreeYearsId);
        moto_updateDuiLastThreeYears(radioButtonList_getSelectedValue(moto_rblDuiLastThreeYearsId))
    }
    else
    {
        hideRow(moto_rowDuiLastThreeYearsId);
        radioButtonList_clearError(moto_rblDuiLastThreeYearsId);
        moto_updateDuiLastThreeYears("0");
    }
}
function moto_updateDuiLastThreeYears(value)
{
    if(value == "1")
    {
        showRow(moto_rowDuiMeasuringApparatusId);
        showRow(moto_rowDuiAlcoholRateId);
        showRow(moto_rowDuiWithDisasterId);
        showRow(moto_rowDuiIsRepeatingId);
    }
    else
    {
        hideRow(moto_rowDuiMeasuringApparatusId);
        radioButtonList_clearError(moto_rblDuiMeasuringApparatusId);
        hideRow(moto_rowDuiAlcoholRateId);
        dropDownList_clearError(moto_ddlDuiAlcoholRateId);
        hideRow(moto_rowDuiWithDisasterId);
        dropDownList_clearError(moto_ddlDuiWithDisasterId);
        hideRow(moto_rowDuiIsRepeatingId);
        radioButtonList_clearError(moto_rblDuiIsRepeatingId);
    }
}
function moto_updateIsDrugsPositive(value)
{
    if(value == "1")
    {
        showRow(moto_rowIsDrugPositiveLastThreeYearsId);
    }
    else
    {
        hideRow(moto_rowIsDrugPositiveLastThreeYearsId);
        radioButtonList_clearError(moto_rblIsDrugPositiveLastThreeYearsId);
    }
}
function moto_updateIsRunningAwayOrRefusingOrder(value)
{
    if(value == "1")
    {
        showRow(moto_rowIsRunningAwayOrRefusingOrderLastThreeYearsId);
    }
    else
    {
        hideRow(moto_rowIsRunningAwayOrRefusingOrderLastThreeYearsId);
        radioButtonList_clearError(moto_rblIsRunningAwayOrRefusingOrderLastThreeYearsId);
    }
}
function moto_updateDrivLicenceSusp(value)
{
    if(value == "2" || value == "4" || value == "5")
    {
         switch(value)
         {
            case "2" :
                setInnerHtml(moto_lblDrivLicenceSuspDurationId, "Pendant combien de temps votre permis vous a-t-il été suspendu ?");
                setInnerHtml(moto_lblDrivLicenceSuspCauseId, "Quel était le motif de votre suspension de permis ?");
                break;
            case "4" :
                setInnerHtml(moto_lblDrivLicenceSuspDurationId, "#");
                setInnerHtml(moto_lblDrivLicenceSuspCauseId, "Quel était le motif de votre annulation de permis ?");
                break;
            case "5" :
                setInnerHtml(moto_lblDrivLicenceSuspDurationId, "Pendant combien de temps votre permis vous a-t-il été retiré ?");
                setInnerHtml(moto_lblDrivLicenceSuspCauseId, "Quel était le motif de votre retrait de permis ?");
                break;
         }
    
         showRow(moto_rowDrivLicenceSuspLastThreeYearsId);
         moto_updateDrivLicenceSuspLastThreeYears(radioButtonList_getSelectedValue(moto_rblDrivLicenceSuspLastThreeYearsId));
    }
    else
    {
        hideRow(moto_rowDrivLicenceSuspLastThreeYearsId);
        radioButtonList_clearError(moto_rblDrivLicenceSuspLastThreeYearsId);
        moto_updateDrivLicenceSuspLastThreeYears("0");
    }
}
function moto_updateDrivLicenceSuspLastThreeYears(value)
{
    var susp = $get(moto_ddlDrivLicenceSuspId).value;
    if(value == "1")
    {
        // Show impaired risk
        if(susp == "2" || susp == "5")
        {
            showRow(moto_rowDrivLicenceSuspDurationId);
        }
        else
        {
             hideRow(moto_rowDrivLicenceSuspDurationId);
             dropDownList_clearError(moto_ddlDrivLicenceSuspDurationId);           
        }
        showRow(moto_rowDrivLicenceSuspCauseId);
        showRow(moto_rowDrivLicenceSuspIsRepeatingId);
        showRow(moto_rowDuiId);
        moto_updateDui(radioButtonList_getSelectedValue(moto_rblDuiId));
        showRow(moto_rowIsDrugPositiveId);
        moto_updateIsDrugsPositive(radioButtonList_getSelectedValue(moto_rblIsDrugPositiveId));
        showRow(moto_rowIsRunningAwayOrRefusingOrderId);
        moto_updateIsRunningAwayOrRefusingOrder(radioButtonList_getSelectedValue(moto_rblIsRunningAwayOrRefusingOrderId));
    }
    else
    {
        // Hide all impaired risk
        hideRow(moto_rowDrivLicenceSuspDurationId);
        dropDownList_clearError(moto_ddlDrivLicenceSuspDurationId);
        hideRow(moto_rowDrivLicenceSuspCauseId);
        dropDownList_clearError(moto_ddlDrivLicenceSuspCauseId);
        hideRow(moto_rowDrivLicenceSuspIsRepeatingId);
        radioButtonList_clearError(moto_rblDrivLicenceSuspIsRepeatingId);
        hideRow(moto_rowDuiId);
        radioButtonList_clearError(moto_rblDuiId);
        moto_updateDui("0");
        hideRow(moto_rowIsDrugPositiveId);
        radioButtonList_clearError(moto_rblIsDrugPositiveId);
        moto_updateIsDrugsPositive("0");
        hideRow(moto_rowIsRunningAwayOrRefusingOrderId);
        radioButtonList_clearError(moto_rblIsRunningAwayOrRefusingOrderId);
        moto_updateIsRunningAwayOrRefusingOrder("0");
    }   
}
function moto_updateDuiMeasuringApparatus(value)
{
    var listId = 0;
    if(value == "1")
        listId = 291;
    else if(value == "2")
        listId = 292;
    dropDownList_fill(moto_ddlDuiAlcoholRateId, listId);
}

/////////////////
// Page 3 (moto)
/////////////////
var moto_OccupationCode;

function moto_p3_checkBeforePost()
{
    if($get(moto_hfMotoId).value == '' && $get(moto_ddlMotoCcId).value != '')
    {
        //alert("Veuillez sélectionner un véhicule");
        // Vehicule search field are filled but vehicule is not selected
        // Force vehicule window search to opend
        moto_vehiculeSearchBtnClick();
        return false;
    }
    return true;
}
//PAGE 3 WARNING
function moto_warningOccupationCode(sender) {
    if (moto_OccupationCode == "09"){
        if( sender.value != 1 ){
            return "Etes-vous certain de ne pas vous être trompé dans la saisie de l'usage du véhicule ou de la catégorie professionnelle ? <br/> Si le conducteur est bien retraité, cette profession n'autorise pas un autre usage que 'Privé'.";
        }
    }
    if( moto_OccupationCode == "20"){
        if( sender.value != 1) {
            return "Etes-vous certain de ne pas vous être trompé dans la saisie de l'usage du véhicule ou de la catégorie professionnelle ? <br/> Si le conducteur est bien à la recherche d'un emploi, cette profession n'autorise pas un autre usage que 'Privé'.";
        }
    }
    if( moto_OccupationCode == "12"){
        if( sender.value != 1) {                
            return "Etes-vous certain de ne pas vous être trompé dans la saisie de l'usage du véhicule ou de la catégorie professionnelle ? <br/> Si le conducteur est bien sans profession, cette profession n'autorise pas un autre usage que 'Privé'.";
        }            
    }
    if( moto_OccupationCode == "08"){
        if (sender.value == 2){
            return "Etes-vous certain de ne pas vous être trompé dans la saisie de l'usage du véhicule ou de la catégorie professionnelle ? <br/> Si le conducteur est bien étudiant, cette profession n'autorise pas un usage 'Privé et professionel'.";
        }
        else{
            if (sender.value == 8){
                return "Etes-vous certain de ne pas vous être trompé dans la saisie de l'usage du véhicule ou de la catégorie professionnelle ? <br/> Si le conducteur est bien étudiant, cette profession n'autorise pas un usage 'Tournée régulière'.";
            }
            else{
                if( sender.value == 1){
                return "Vous avez déclaré être étudiant et utiliser votre véhicule en usage privé. <br/>Si vous utilisez votre véhicule pour vous rendre sur votre lieu d'étude ou sur un lieu professionnel, <br/>vous devez répondre que l'usage de votre véhicule est 'Privé et trajet travail'.";
                }
            }
        }
    }
    return "";
}
function moto_warningPurchaseMode(sender) {        
    if(sender.value == "L" && moto_contrGuaranteeCode != "C" )        
        return "Votre véhicule fait l'objet d'un contrat de leasing (crédit-bail ou location avec option d'achat). Il est normalement obligatoire d'assurer ce type de véhicule en formule 'Tous risques'.";
    return "";
}

function moto_warningPurchaseDatePrev(sender) {
    var v = getDateByFormat(sender.value, 'dd/MM/yyyy');   
    if (v != null && v != "") {        
        if ((new Date) >= v) {
            return "Cette date ne semble pas correcte. Vous avez déclaré sur la 1ère page que vous étiez en train d'acheter une véhicule.   Merci de la corriger.";            
        }
    }
    return "";
}

function moto_warningPurchaseDate(sender) {
    var v = dateDropDownList_getDate(moto_ddlPurchaseDateId);   
    if (v != null && v != "") {        
        if ( v>= (new Date)) {
            return "Cette date ne semble pas correcte. Vous avez déclaré sur la 1ère page que vous possédiez le véhicule. Merci de la corriger.";            
        }
    }
    return "";
}

//PAGE 3 Error
function moto_errorFirstDrivingDate(sender) {
    var purchaseDate = dateDropDownList_getDate(moto_ddlPurchaseDateId);
    var firstDrivingDate = dateDropDownList_getDate(moto_ddlFirstDrivingDateId);
    if (firstDrivingDate > purchaseDate) {
        return "La date de 1ère mise en circulation ne peut être posterieure à la date d'achat du véhicule.";
    }
    return "";
}

function moto_errorFirstDrivingDate2(sender) {
    var firstCarDrivingDate = dateDropDownList_getDate(moto_ddlFirstDrivingDateId);
    if (firstCarDrivingDate > getTodayDate()) {
        return "La date de 1ère mise en circulation ne peut être posterieure à la date du jour.";
    }
    return "";
}
/////////////////
// Page 4 (moto)
/////////////////

// UI element's ID(s) page 4
var moto_rowMbInsuranceYearNumberId;
var moto_rowMbHasBeenInsuredLast4YearsId;
var moto_rowMbInsuranceYearDetailLabelId;
var moto_rowMbInsuranceYearDetail1Id;
var moto_rowMbInsuranceYearDetail2Id;
var moto_rowMbInsuranceYearDetail3Id;
var moto_rowMbInsuranceYearDetail4Id;
var moto_rowMbInsYearLabelId;
var moto_rowInsYearLess125Id;
var moto_rowInsYearMore125Id;
var moto_rowMbDisasterLast3YearsId;
var moto_rowMbDisaster1Id;
var moto_rowMbDisaster2Id;
var moto_rowMbDisaster3Id;
var moto_rowMbDisaster4Id;
var moto_rowMbDisaster5Id;
var moto_rowMbBonusId;
var moto_rowMbBonusCommentId;
var moto_rowBestMbBonusId;
var moto_rowContrAnniverMthId;
var moto_rowCurrentCarrierId;
var moto_rowEffectiveDateId;
var moto_rowCarHasBeenInsuredId;
var moto_rowCarInsuranceYearNumberId;
var moto_rowCarInsMonthsLast2YearsId;
var moto_rowCarDisasterLast3YearsId;
var moto_rowCarBonusId;
var moto_rowCarBonusCommentId;
var moto_rowCarDisaster1Id;
var moto_rowCarDisaster2Id;
var moto_rowCarDisaster3Id;
var moto_rowCarDisaster4Id;
var moto_rowCarDisaster5Id;

var moto_ddlMbInsuranceYearNumberId;
var moto_rblMbHasBeenInsuredLast4YearsId;
var moto_ddlInsuranceYearDetailCc1Id;
var moto_ddlInsuranceYearDetailCc2Id;
var moto_ddlInsuranceYearDetailCc3Id;
var moto_ddlInsuranceYearDetailCc4Id;
var moto_ddlInsYearLess125Id;
var moto_ddlInsYearMore125Id;
var moto_rblMbDisasterLast3YearsId;
var moto_ddlMbBonusId;
var moto_ddlBestMbBonusId;
var moto_ddlContrAnniverMthId;
var moto_ddlCurrentCarrierId;
var moto_tbEffectiveDateId;
var moto_ddlCarInsuranceYearNumberId;
var moto_ddlCarInsMonthsLast2YearsId;
var moto_rblCarDisasterLast3YearsId;
var moto_ddlCarBonusId;
var moto_ddlMbDisasterType1Id;
var moto_tbMbDisasterDate1Id;
var moto_ddlMbDisasterType2Id;
var moto_tbMbDisasterDate2Id;
var moto_ddlMbDisasterType3Id;
var moto_tbMbDisasterDate3Id;
var moto_ddlMbDisasterType4Id;
var moto_tbMbDisasterDate4Id;
var moto_ddlMbDisasterType5Id;
var moto_tbMbDisasterDate5Id;
var moto_ddlCarDisasterType1Id;
var moto_tbCarDisasterDate1Id;
var moto_ddlCarDisasterType2Id;
var moto_tbCarDisasterDate2Id;
var moto_ddlCarDisasterType3Id;
var moto_tbCarDisasterDate3Id;
var moto_ddlCarDisasterType4Id;
var moto_tbCarDisasterDate4Id;
var moto_ddlCarDisasterType5Id;
var moto_tbCarDisasterDate5Id;

var moto_owningStatus;
var moto_rblMbHasBeenInsuredId;
var moto_drivLicenceaDate;
var moto_drivLicencebDate;
        
function moto_updateMbHasBeenInsured(value)
{
    if(value == "1") {
        showRow(moto_rowMbInsuranceYearNumberId);
        showRow(moto_rowMbHasBeenInsuredLast4YearsId);
        moto_updateMbHasBeenInsuredLast4Years(radioButtonList_getSelectedValue(moto_rblMbHasBeenInsuredLast4YearsId));
        showRow(moto_rowMbDisasterLast3YearsId);
        moto_updateMbDisasterLast3Years(radioButtonList_getSelectedValue(moto_rblMbDisasterLast3YearsId));
        showRow(moto_rowMbBonusId);  
        showRow(moto_rowMbBonusCommentId);
        // Following line may not exist if owning status = 5 ("première moto").
        // If this is the case, following line are ignored
        showRow(moto_rowContrAnniverMthId);  
        showRow(moto_rowCurrentCarrierId);  
    }
    else {
        hideRow(moto_rowMbInsuranceYearNumberId);
        dropDownList_clearError(moto_ddlMbInsuranceYearNumberId);
        hideRow(moto_rowMbHasBeenInsuredLast4YearsId);
        radioButtonList_clearError(moto_rblMbHasBeenInsuredLast4YearsId);
        moto_hideInsuranceLast4Years();
        hideRow(moto_rowMbInsYearLabelId);
        hideRow(moto_rowInsYearLess125Id);
        dropDownList_clearError(moto_ddlInsYearLess125Id);
        hideRow(moto_rowInsYearMore125Id);
        dropDownList_clearError(moto_ddlInsYearMore125Id);
        hideRow(moto_rowMbDisasterLast3YearsId);
        radioButtonList_clearError(moto_rblMbDisasterLast3YearsId);
        moto_hideMbDisasters();
        hideRow(moto_rowMbBonusId);
        dropDownList_clearError(moto_ddlMbBonusId);
        hideRow(moto_rowMbBonusCommentId);
        hideRow(moto_rowBestMbBonusId);
        dropDownList_clearError(moto_ddlBestMbBonusId);
        hideRow(moto_rowContrAnniverMthId);
        dropDownList_clearError(moto_ddlContrAnniverMthId);
        hideRow(moto_rowCurrentCarrierId);
        dropDownList_clearError(moto_ddlCurrentCarrierId);
        textBox_clearError(moto_tbEffectiveDateId);
    }
}
function moto_updateMbInsuranceYearNumber(value) {
    // update warning bonus
    dropDownList_onUpdate($get(moto_ddlMbBonusId));
}
function moto_updateMbDisasterLast3Years(value)
{
    if(value != "" && value > 0)
    {
        if(value == 1)
        {
            showRow(moto_rowMbDisaster1Id);
            hideRow(moto_rowMbDisaster2Id);
            dropDownList_clearError(moto_ddlMbDisasterType2Id);
            textBox_clearError(moto_tbMbDisasterDate2Id);
            hideRow(moto_rowMbDisaster3Id);
            dropDownList_clearError(moto_ddlMbDisasterType3Id);
            textBox_clearError(moto_tbMbDisasterDate3Id);
            hideRow(moto_rowMbDisaster4Id);
            dropDownList_clearError(moto_ddlMbDisasterType4Id);
            textBox_clearError(moto_tbMbDisasterDate4Id);
            hideRow(moto_rowMbDisaster5Id);
            dropDownList_clearError(moto_ddlMbDisasterType5Id);
            textBox_clearError(moto_tbMbDisasterDate5Id);   
        }
        else if(value == 2)
        {
            showRow(moto_rowMbDisaster1Id);
            showRow(moto_rowMbDisaster2Id);
            hideRow(moto_rowMbDisaster3Id);
            dropDownList_clearError(moto_ddlMbDisasterType3Id);
            textBox_clearError(moto_tbMbDisasterDate3Id);
            hideRow(moto_rowMbDisaster4Id);
            dropDownList_clearError(moto_ddlMbDisasterType4Id);
            textBox_clearError(moto_tbMbDisasterDate4Id);
            hideRow(moto_rowMbDisaster5Id);
            dropDownList_clearError(moto_ddlMbDisasterType5Id);
            textBox_clearError(moto_tbMbDisasterDate5Id);      
        }
        else if(value == 3)
        {
            showRow(moto_rowMbDisaster1Id);
            showRow(moto_rowMbDisaster2Id);
            showRow(moto_rowMbDisaster3Id);
            hideRow(moto_rowMbDisaster4Id);
            dropDownList_clearError(moto_ddlMbDisasterType4Id);
            textBox_clearError(moto_tbMbDisasterDate4Id);
            hideRow(moto_rowMbDisaster5Id);
            dropDownList_clearError(moto_ddlMbDisasterType5Id);
            textBox_clearError(moto_tbMbDisasterDate5Id);
        }
        else if (value == 4) {
            showRow(moto_rowMbDisaster1Id);
            showRow(moto_rowMbDisaster2Id);
            showRow(moto_rowMbDisaster3Id);
            showRow(moto_rowMbDisaster4Id);
            hideRow(moto_rowMbDisaster5Id);
            dropDownList_clearError(moto_ddlMbDisasterType5Id);
            textBox_clearError(moto_tbMbDisasterDate5Id);
        }
        else if (value == 5) {
            showRow(moto_rowMbDisaster1Id);
            showRow(moto_rowMbDisaster2Id);
            showRow(moto_rowMbDisaster3Id);
            showRow(moto_rowMbDisaster4Id);
            showRow(moto_rowMbDisaster5Id);
        }
    }
    else
    {   
        moto_hideMbDisasters();
    }
}
function moto_updateMbHasBeenInsuredLast4Years(value)
{
    if(value == "1")
    {
        hideRow(moto_rowMbInsYearLabelId);
        hideRow(moto_rowInsYearLess125Id);
        dropDownList_clearError(moto_ddlInsYearLess125Id);
        hideRow(moto_rowInsYearMore125Id);
       dropDownList_clearError(moto_ddlInsYearMore125Id);
        moto_showInsuranceLast4Years();
        moto_updateMbBonus($get(moto_ddlMbBonusId).value);
    }
    else if(value == "0")
    {
        moto_hideInsuranceLast4Years();
        hideRow(moto_rowBestMbBonusId);
        dropDownList_clearError(moto_ddlBestMbBonusId);
        showRow(moto_rowMbInsYearLabelId);
        showRow(moto_rowInsYearLess125Id);
        showRow(moto_rowInsYearMore125Id);
        /*put 1 in moto bonus*/
        $get(moto_ddlMbBonusId).value="1";
    }
    else
    {
        moto_hideInsuranceLast4Years();
        hideRow(moto_rowBestMbBonusId);
        dropDownList_clearError(moto_ddlMbBonusId)
        hideRow(moto_rowMbInsYearLabelId);
        hideRow(moto_rowInsYearLess125Id);
        dropDownList_clearError(moto_ddlInsYearLess125Id);
        hideRow(moto_rowInsYearMore125Id);
        dropDownList_clearError(moto_ddlInsYearMore125Id);
    }
}
function moto_updateMbBonus(value)
{
    if(value != "" && value > 1.0 && value <= 3.5)
    {
        showRow(moto_rowBestMbBonusId);
    }
    else
    {
        hideRow(moto_rowBestMbBonusId);
        dropDownList_clearError(moto_ddlBestMbBonusId);   
    }
    moto_mbBonusHelp();
}
function moto_updateCarHasBeenInsured(value)
{
    if(value == "1") {
         showRow(moto_rowCarInsuranceYearNumberId);
         showRow(moto_rowCarInsMonthsLast2YearsId);
         showRow(moto_rowCarDisasterLast3YearsId);  
         moto_updateCarDisasterLast3Years(radioButtonList_getSelectedValue(moto_rblCarDisasterLast3YearsId));
         showRow(moto_rowCarBonusId);
         showRow(moto_rowCarBonusCommentId);
    }
    else {
         hideRow(moto_rowCarInsuranceYearNumberId);
         dropDownList_clearError(moto_ddlCarInsuranceYearNumberId);
         hideRow(moto_rowCarInsMonthsLast2YearsId);
         dropDownList_clearError(moto_ddlCarInsMonthsLast2YearsId);
         hideRow(moto_rowCarDisasterLast3YearsId); 
         radioButtonList_clearError(moto_rblCarDisasterLast3YearsId);
         moto_hideCarDisasters();
         hideRow(moto_rowCarBonusId); 
         dropDownList_clearError(moto_ddlCarBonusId);
         hideRow(moto_rowCarBonusCommentId);        
    }
}
function moto_updateCarInsuranceYearNumber(value) {
    if (value == "0") {
        dropDownList_fill(moto_ddlCarInsMonthsLast2YearsId, 270, "11", null, false, null, true);
    } else if (value == "1") {
        dropDownList_fill(moto_ddlCarInsMonthsLast2YearsId, 269, "18", null, false, null, true);
    } else {
        dropDownList_fill(moto_ddlCarInsMonthsLast2YearsId, 269, "24", null, false, null, true);
    }
    // update warning bonus
    dropDownList_onUpdate($get(moto_ddlCarBonusId));  
}
function moto_updateCarDisasterLast3Years(value)
{
    if(value != "" && value > 0)
    {
        if(value == 1)
        {
            showRow(moto_rowCarDisaster1Id);
            hideRow(moto_rowCarDisaster2Id);
            dropDownList_clearError(moto_ddlCarDisasterType2Id);
            textBox_clearError(moto_tbCarDisasterDate2Id);
            hideRow(moto_rowCarDisaster3Id);
            dropDownList_clearError(moto_ddlCarDisasterType3Id);
            textBox_clearError(moto_tbCarDisasterDate3Id);
            hideRow(moto_rowCarDisaster4Id);
            dropDownList_clearError(moto_ddlCarDisasterType4Id);
            textBox_clearError(moto_tbCarDisasterDate4Id);
            hideRow(moto_rowCarDisaster5Id);
            dropDownList_clearError(moto_ddlCarDisasterType5Id);
            textBox_clearError(moto_tbCarDisasterDate5Id);     
        }
        else if(value == 2)
        {
            showRow(moto_rowCarDisaster1Id);
            showRow(moto_rowCarDisaster2Id);
            hideRow(moto_rowCarDisaster3Id); 
            dropDownList_clearError(moto_ddlCarDisasterType3Id);
            textBox_clearError(moto_tbCarDisasterDate3Id);
            hideRow(moto_rowCarDisaster4Id);
            dropDownList_clearError(moto_ddlCarDisasterType4Id);
            textBox_clearError(moto_tbCarDisasterDate4Id);
            hideRow(moto_rowCarDisaster5Id);
            dropDownList_clearError(moto_ddlCarDisasterType5Id);
            textBox_clearError(moto_tbCarDisasterDate5Id);  
        }
        else if(value == 3)
        {
            showRow(moto_rowCarDisaster1Id);
            showRow(moto_rowCarDisaster2Id);
            showRow(moto_rowCarDisaster3Id);
            hideRow(moto_rowCarDisaster4Id);
            dropDownList_clearError(moto_ddlCarDisasterType4Id);
            textBox_clearError(moto_tbCarDisasterDate4Id);
            hideRow(moto_rowCarDisaster5Id);
            dropDownList_clearError(moto_ddlCarDisasterType5Id);
            textBox_clearError(moto_tbCarDisasterDate5Id);
        }
        else if (value == 4) {
            showRow(moto_rowCarDisaster1Id);
            showRow(moto_rowCarDisaster2Id);
            showRow(moto_rowCarDisaster3Id);
            showRow(moto_rowCarDisaster4Id);
            hideRow(moto_rowCarDisaster5Id);
            dropDownList_clearError(moto_ddlCarDisasterType5Id);
            textBox_clearError(moto_tbCarDisasterDate5Id);
        }
        else if (value == 5) {
            showRow(moto_rowCarDisaster1Id);
            showRow(moto_rowCarDisaster2Id);
            showRow(moto_rowCarDisaster3Id);
            showRow(moto_rowCarDisaster4Id);
            showRow(moto_rowCarDisaster5Id);
        }
    }
    else
    {
        moto_hideCarDisasters();  
    }
}
function moto_showInsuranceLast4Years()
{
    showRow(moto_rowMbInsuranceYearDetailLabelId);
    showRow(moto_rowMbInsuranceYearDetail1Id);
    showRow(moto_rowMbInsuranceYearDetail2Id);
    showRow(moto_rowMbInsuranceYearDetail3Id);
    showRow(moto_rowMbInsuranceYearDetail4Id);
}
function moto_hideInsuranceLast4Years()
{
    hideRow(moto_rowMbInsuranceYearDetailLabelId);
    hideRow(moto_rowMbInsuranceYearDetail1Id);
    hideRow(moto_rowMbInsuranceYearDetail2Id);
    hideRow(moto_rowMbInsuranceYearDetail3Id);
    hideRow(moto_rowMbInsuranceYearDetail4Id);
    dropDownList_clearError(moto_ddlInsuranceYearDetailCc1Id);
    dropDownList_clearError(moto_ddlInsuranceYearDetailCc2Id);
    dropDownList_clearError(moto_ddlInsuranceYearDetailCc3Id);
    dropDownList_clearError(moto_ddlInsuranceYearDetailCc4Id);
}
function moto_hideMbDisasters()
{
    hideRow(moto_rowMbDisaster1Id);
    dropDownList_clearError(moto_ddlMbDisasterType1Id);
    textBox_clearError(moto_tbMbDisasterDate1Id);
    hideRow(moto_rowMbDisaster2Id);
    dropDownList_clearError(moto_ddlMbDisasterType2Id);
    textBox_clearError(moto_tbMbDisasterDate2Id);
    hideRow(moto_rowMbDisaster3Id);
    dropDownList_clearError(moto_ddlMbDisasterType3Id);
    textBox_clearError(moto_tbMbDisasterDate3Id);
    hideRow(moto_rowMbDisaster4Id);
    dropDownList_clearError(moto_ddlMbDisasterType4Id);
    textBox_clearError(moto_tbMbDisasterDate4Id);
    hideRow(moto_rowMbDisaster5Id);
    dropDownList_clearError(moto_ddlMbDisasterType5Id);
    textBox_clearError(moto_tbMbDisasterDate5Id);
}
function moto_hideCarDisasters()
{
    hideRow(moto_rowCarDisaster1Id);
    dropDownList_clearError(moto_ddlCarDisasterType1Id);
    textBox_clearError(moto_tbCarDisasterDate1Id);
    hideRow(moto_rowCarDisaster2Id);
    dropDownList_clearError(moto_ddlCarDisasterType2Id);
    textBox_clearError(moto_tbCarDisasterDate2Id);
    hideRow(moto_rowCarDisaster3Id);
    dropDownList_clearError(moto_ddlCarDisasterType3Id);
    textBox_clearError(moto_tbCarDisasterDate3Id);
    hideRow(moto_rowCarDisaster4Id);
    dropDownList_clearError(moto_ddlCarDisasterType4Id);
    textBox_clearError(moto_tbCarDisasterDate4Id);
    hideRow(moto_rowCarDisaster5Id);
    dropDownList_clearError(moto_ddlCarDisasterType5Id);
    textBox_clearError(moto_tbCarDisasterDate5Id);
}
function moto_updateMbInsYearLessOrMore125()
{
    // Clear error
    dropDownList_clearError(moto_ddlInsYearLess125Id);
    dropDownList_clearError(moto_ddlInsYearMore125Id);
}
function moto_updateInsuranceMonth(indexYear,value)
{
    var ddlId;
    if(indexYear == 1)
        ddlId = moto_ddlInsuranceYearDetailCc1Id;
    else if(indexYear == 2)
        ddlId = moto_ddlInsuranceYearDetailCc2Id;
    else if(indexYear == 3)
        ddlId = moto_ddlInsuranceYearDetailCc3Id;
    else if(indexYear == 4)
        ddlId = moto_ddlInsuranceYearDetailCc4Id;
    if(value == 0)
    {
        $get(ddlId).value = "";
        setDropDownListEnabled(ddlId, false);
        dropDownList_clearError(ddlId);
    }
    else
    {
        setDropDownListEnabled(ddlId, true);        
    }
}

function getMotoMaturity(month){
    if(month != "")
    {
        var maturityCtrl= $get(moto_tbEffectiveDateId);    
        if (maturityCtrl && moto_owningStatus ==1 ){
            var prop = form_eltPropertiesObjArray[maturityCtrl.id];  
            if (prop.currentValueIsDefaultText){
                prop.defaultText=getMaturity(month);
                maturityCtrl.value= prop.defaultText;
            }
        }
    }
}

function moto_warningEffectiveDate(sender) {  
  if (moto_owningStatus ==1)
  {
    var effecDate = getDateByFormat(sender.value, 'dd/MM/yyyy');
    var bWarn = false;
    var annivDate = $get(moto_ddlContrAnniverMthId).value;
    if(annivDate != "")
    {
        if (annivDate == 12) {
            if (effecDate.getMonth() < 10 && effecDate.getMonth() > 0)
                bWarn = true;
        }
        else {
            if (annivDate == 1) {
                if (effecDate.getMonth() > 1 && effecDate.getMonth() < 10)
                    bWarn = true;
            }
            else {
                if (annivDate > (effecDate.getMonth() + 2) || annivDate < (effecDate.getMonth()))
                    bWarn = true;
            }
        }
        if (bWarn)
            return "Vous avez précédemment déclaré que le mois d'échéance de votre police actuelle est en " + GetMonthText(annivDate) + " <br />La date d'effet que vous venez de renseigner n'est donc pas cohérente. Merci de corriger si c'est une erreur. Sinon continuer.";
    }
   }
   return '';
}

function moto_warningMbHasBeenInsured(sender) {
    if (moto_owningStatus == "1" && radioButtonList_getSelectedValue(moto_rblMbHasBeenInsuredId) == "0") {
        return "Vous avez déclaré que vous possédiez déjà une moto, êtes-vous sûr(e) de ne jamais avoir été assuré(e) ?";
    }
    return "";
}

function moto_warningMbBonus(sender) {
    if (sender.value != ""){
        var bonus = sender.value * 1.0;
        if (bonus >= 10.0) {
            bonus = 0.5;
        }
        /*if (isValidBonusByDrivLicenceDate(bonus, getDateByFormat(moto_drivLicenceaDate, 'dd/MM/yyyy')) == false) {
            return "La date de permis moto que vous avez renseignée ne semble pas permettre d'avoir un bonus de ce niveau.";
        }*/
        if (isValidBonus(bonus, $get(moto_ddlMbInsuranceYearNumberId).value) == false) {
            return "Le nombre d'années d'assurance que vous avez renseigné ne semble pas permettre d'avoir un bonus de ce niveau.";
        }
        if ( bonus < 1 && radioButtonList_getSelectedValue(moto_rblMbHasBeenInsuredLast4YearsId) == "0" ){
            return "Vous avez déclaré que vous n'avez-vous pas été assuré dans les 4 dernières années ce qui ne semble pas permettre d'avoir un bonus."
        }
    }
    return "";
}

function moto_warningCarBonus(sender) {
    if (sender.value != ""){
        var bonus = sender.value * 1.0;
        if (bonus >= 10.0) {
            bonus = 0.5;
        }
        if (isValidBonus(bonus, $get(moto_ddlCarInsuranceYearNumberId).value) == false) {
            return "Le nombre d'années d'assurance que vous avez renseigné ne semble pas permettre d'avoir un bonus de ce niveau.";
        }
    }
    return "";
}

/////////////////
// Page 2 Cyclo
/////////////////

// UI element's ID(s) page 2
var cyclo_tbBirthDateId;
var cyclo_rblDrivLicenceTypeId;

function cyclo_errorDrivLicenceDate(sender) {
    var drivLicenceType = radioButtonList_getSelectedValue(cyclo_rblDrivLicenceTypeId);
    if (drivLicenceType == "5") {
        var d1 = getDateByFormat($get(cyclo_tbBirthDateId).value, "dd/MM/yyyy");
        var d2 = new Date(1988, 0, 1);
        if(d1 >= d2)
            return "Si vous êtes né après le 1 janvier 1988 vous avez l'obligation d'être titulaire au minimum du permis BSR pour conduire un cyclomoteur.";
    }
    return "";
}

/////////////////
// Page 3 Cyclo
/////////////////

// UI element's ID(s) page 3

function moto_mbBonusHelp() {
    if ($get(moto_rowMbBonusCommentId))
        setRowVisibility(moto_rowMbBonusCommentId, false);
}

function moto_carBonusHelp() {
    if ($get(moto_rowCarBonusCommentId))
        setRowVisibility(moto_rowCarBonusCommentId, false);
}
////////////////
// Page 4 Cyclo
////////////////
var cyclo_ddlContrAnniverMthId;
var cyclo_tbEffectiveDateId;

function getCycloMaturity(month){
    if(month != "")
    {
        var maturityCtrl= $get(cyclo_tbEffectiveDateId);
        if (maturityCtrl){
            var prop = form_eltPropertiesObjArray[maturityCtrl.id];  
            if (prop.currentValueIsDefaultText){
                prop.defaultText=getMaturity(month);
                maturityCtrl.value= prop.defaultText;
            }
        }
    }
}

function cyclo_warningEffectiveDate(sender) { 
    if (moto_owningStatus == 1)
    {   
        var effecDate = getDateByFormat(sender.value, 'dd/MM/yyyy');
        var bWarn = false;
        var annivDate = $get(cyclo_ddlContrAnniverMthId).value;
        if(annivDate != "")
        {
            if (annivDate == 12) {
                if (effecDate.getMonth() < 10 && effecDate.getMonth() > 0)
                    bWarn = true;
            }
            else {
                if (annivDate == 1) {
                    if (effecDate.getMonth() > 1 && effecDate.getMonth() < 10)
                        bWarn = true;
                }
                else {
                    if (annivDate > (effecDate.getMonth() + 2) || annivDate < (effecDate.getMonth()))
                        bWarn = true;
                }
            }
            if (bWarn)
                return "Vous avez précédemment déclaré que le mois d'échéance de votre police actuelle est en " + GetMonthText(annivDate) + " <br />La date d'effet que vous venez de renseigner n'est donc pas cohérente. Merci de corriger si c'est une erreur. Sinon continuer.";
        }
    }
    return '';
}

////////////////////////////////////////
// Moto vehicule selection
////////////////////////////////////////

// UI element's ID(s) page 3
var moto_btnSearchMotoId;
var moto_ddlMotoManufacturerId;
var moto_ddlMotoCcId;
var moto_ddlMotoModelId;
var moto_hfMotoId;
var moto_hfsearchTypeId;
var moto_panelVehiculeSearchResultId;
var moto_lblSelectedVehiculeId;
var moto_rowMotoManufacturerId;
var moto_rowMotoCcId;
var moto_rowMotoModelId;
var moto_rowBtnSearchVehiculeId;
var moto_rowVehiculeSelectionId;
var moto_rowSelectedVehiculeId;
var moto_vehiculeArrowEltId;

var moto_contrGuaranteeCode;
var moto_popupExtenderId;

var moto_rowErrVehiculeId;
var moto_rowSelectionTitleId;
var moto_ddlFirstDrivingDateId;
var moto_ddlFirstDrivingDateYearId;
var moto_ddlPurchaseDateId;
var moto_dtbPurchaseDatePrevId;
var moto_addAge;

var saveMotoMake;
var saveMotoCC;
var saveMotoModel;
var moto_dateRef;

function moto_getDateRef() {
    if (moto_addAge == "1") {
        return getDateByFormat($get(moto_dtbPurchaseDatePrevId).value,"dd/MM/yyyy");
    }
    else {
        return dateDropDownList_getDate(moto_ddlFirstDrivingDateId);
    }
}

function moto_vehiculeEnableSearchBtn()
{
    var btn = $get(moto_btnSearchMotoId);
    btn.className = 'al_formVehiculeBtnSearchOn';
    setBtnEnabled(btn, true);
}
function moto_vehiculeDisableSearchBtn()
{
    var btn = $get(moto_btnSearchMotoId);
    btn.className = 'al_formVehiculeBtnSearchOff';
    setBtnEnabled(btn, false);
}

function moto_saveMotoCaract()
{
 saveMotoMake = $get(moto_ddlMotoManufacturerId).value;
 saveMotoCC= $get(moto_ddlMotoCcId).value;
 saveMotoModel= $get(moto_ddlMotoModelId).value;
 
 if ($get(moto_hfMotoId).value != "")
    moto_vehiculeShowQuestion();
}

//fonction execute suite à la modification de la date --chargement des marques
function moto_vehiculeUpdateFirstDrivingDate()
{
    var FirstDrivingDate = moto_getDateRef();
    if (isValidDate(FirstDrivingDate)){
        if( moto_dateRef != FirstDrivingDate.getFullYear()){
            moto_dateRef=FirstDrivingDate.getFullYear();
            //keep moto properties
            moto_saveMotoCaract();
            // Clear sub info
            dropDownList_clear(moto_ddlMotoManufacturerId); 
            dropDownList_clear(moto_ddlMotoCcId);
            dropDownList_clear(moto_ddlMotoModelId);
            // Update button state
            moto_vehiculeDisableSearchBtn();
       
            dropDownList_show(moto_ddlMotoManufacturerId); 
            var moto_vehiculeUpdateFirstDrivingDate_Callback= function(result, eventArgs)
            {        
                dropDownList_bind(moto_ddlMotoManufacturerId, -1, result, saveMotoMake, "", (result.length > 1 ? true : false));           
                if ($get(moto_ddlMotoManufacturerId).value != "") {
                    moto_vehiculeUpdateManufacturer(saveMotoMake, false);
                }
                else 
                {
                   dropDownList_hide(moto_ddlMotoCcId);
                   dropDownList_hide(moto_ddlMotoModelId); 
                   // Display vehicule if required
                   moto_vehiculeAutoSearch();            
                }
            }
            // This is the callback function invoked if the Web service failed.
            var moto_vehiculeUpdateFirstDrivingDate_FailedCallback =function (error)
            {
                displayAspNetFrameworkError("AssurlandWeb.MotoVehiculeSearch.GetManufacturerWithDate failed",error);
            }
            // Call WS        
            AssurlandWeb.MotoVehiculeSearch.GetManufacturerWithDate(FirstDrivingDate, moto_vehiculeUpdateFirstDrivingDate_Callback, moto_vehiculeUpdateFirstDrivingDate_FailedCallback);
        }
    }
    else
    {
        dropDownList_hide(moto_ddlMotoManufacturerId); 
        dropDownList_hide(moto_ddlMotoCcId);
        dropDownList_hide(moto_ddlMotoModelId);        
    }
}




function moto_vehiculeUpdateManufacturer(manufacturer,IsEvent)
{
     var FirstDrivingDate = moto_getDateRef();
     if (IsEvent)
        moto_saveMotoCaract();
    // Clear sub info
    dropDownList_clear(moto_ddlMotoCcId);
    dropDownList_clear(moto_ddlMotoModelId);
   
    
    // Update button state
    moto_vehiculeDisableSearchBtn();
    
    var moto_vehiculeUpdateCcDropDownList_Callback = function (result, eventArgs)
    {
        var ccArray = new Array();
        for(var i=0; i<result.length; i++)
        {
            ccArray[i] = new ListItem(result[i],result[i]);
        }
        dropDownList_bind(moto_ddlMotoCcId, -1, ccArray, saveMotoCC, "", (ccArray.length > 1 ? true : false));  
        if ($get(moto_ddlMotoCcId).value != "") {
            moto_vehiculeUpdateMotoCc($get(moto_ddlMotoCcId).value, false);
        }
        else 
        {
           dropDownList_hide(moto_ddlMotoModelId); 
           // Display vehicule if required
           moto_vehiculeAutoSearch();            
        }
    }
    // This is the callback function invoked if the Web service failed.
    var moto_vehiculeUpdateCcDropDownList_FailedCallback = function (error)
    {
        displayAspNetFrameworkError("AssurlandWeb.MotoVehiculeSearch.GetMotoCcByManufacturerWithDate failed",error);
    }

    
    if ( manufacturer!= "")
    {
        dropDownList_show(moto_ddlMotoCcId)
        // Call WS
        AssurlandWeb.MotoVehiculeSearch.GetMotoCcByManufacturerWithDate(FirstDrivingDate,manufacturer, moto_vehiculeUpdateCcDropDownList_Callback, moto_vehiculeUpdateCcDropDownList_FailedCallback);
    }
    else
    {
        dropDownList_hide(moto_ddlMotoCcId);        
        dropDownList_hide(moto_ddlMotoModelId); 
    }
    
}

function moto_vehiculeUpdateMotoCc(cc,IsEvent)
{
     var FirstDrivingDate = moto_getDateRef();
     var motoMake = $get(moto_ddlMotoManufacturerId).value;
     if (IsEvent)
        moto_saveMotoCaract();
    // Clear sub info
    dropDownList_clear(moto_ddlMotoModelId);
   
     var moto_vehiculeUpdateModelDropDownList_Callback = function (result, eventArgs)
    {
        var ccArray = new Array();
        for(var i=0; i<result.length; i++)
        {
            ccArray[i] = new ListItem(result[i],result[i]);
        }
        dropDownList_bind(moto_ddlMotoModelId, -1, ccArray, saveMotoModel, "", (ccArray.length > 1 ? true : false));  
       if ($get(moto_ddlMotoModelId).value != "") {
            moto_vehiculeUpdateMotoModel($get(moto_ddlMotoModelId).value,false);
        }
        else 
        {// Display vehicule if required
           moto_vehiculeAutoSearch();            
        }
    }
    // This is the callback function invoked if the Web service failed.
    var moto_vehiculeUpdateModelDropDownList_FailedCallback = function (error)
    {
        displayAspNetFrameworkError("AssurlandWeb.MotoVehiculeSearch.GetMotoModelByCcWithDate failed",error);
    }

    
    if ( cc!= "")
    {
        dropDownList_show(moto_ddlMotoModelId)
        moto_vehiculeEnableSearchBtn();
        // Call WS
        AssurlandWeb.MotoVehiculeSearch.GetMotoModelByCcWithDate(FirstDrivingDate,motoMake,cc, moto_vehiculeUpdateModelDropDownList_Callback, moto_vehiculeUpdateModelDropDownList_FailedCallback);       
     }
    else
    {
        moto_vehiculeDisableSearchBtn();        
        dropDownList_hide(moto_ddlMotoModelId); 
    }
    
}

function moto_vehiculeUpdateMotoModel(Model,IsEvent)
{
     if (IsEvent)
        moto_saveMotoCaract();  
     if (Model != "") {
        // Automatic vehicule list display
        var manufacturer = $get(moto_ddlMotoManufacturerId).value;
        var cc = $get(moto_ddlMotoCcId).value;
        var dateref = moto_getDateRef();  
        if(cc == '')
            cc = -1;
        var model = $get(moto_ddlMotoModelId).value;
        moto_vehiculeSearch(dateref,manufacturer,cc,model,1,"Model_Asc",IsEvent);
     }
}
// Vehicule search
function moto_vehiculeAutoSearch()
{
    var FirstDrivingDate = moto_getDateRef();
    if (isValidDate(FirstDrivingDate)){
        var manufacturer = $get(moto_ddlMotoManufacturerId).value;
        if(manufacturer != ''){
            var cc = $get(moto_ddlMotoCcId).value;
            var model = $get(moto_ddlMotoModelId).value;
            if(cc == '')
                cc = -1;
              
            // Call WS
            AssurlandWeb.MotoVehiculeSearch.GetNbVehiculesWithDate(FirstDrivingDate,manufacturer, cc,model, moto_vehiculeAutoSearch_Callback, moto_vehiculeAutoSearch_FailedCallback);
        }
    }   
}
function moto_vehiculeAutoSearch_Callback(result, eventArgs)
{
    //alert(result)
    if(result > 0 && result <= 10)
    {
        // Automatic vehicule list display
        var manufacturer = $get(moto_ddlMotoManufacturerId).value;
        var cc = $get(moto_ddlMotoCcId).value;
        var dateref = moto_getDateRef();  
        if(cc == '')
            cc = -1;
        var model = $get(moto_ddlMotoModelId).value;
        moto_vehiculeSearch(dateref,manufacturer,cc,model,1,"Model_Asc",false);
    }
}
function moto_vehiculeAutoSearch_FailedCallback(error)
{
    displayAspNetFrameworkError("AssurlandWeb.MotoVehiculeSearch.GetNbVehiculesWithDate failed",error);
}
function moto_vehiculeSearchBtnClick()
{
    var manufacturer = $get(moto_ddlMotoManufacturerId).value;
    var cc = $get(moto_ddlMotoCcId).value; 
    var dateref = moto_getDateRef();  
    if(!isValidDate(dateref) || manufacturer == '' || cc == '')
    {
       return true;
    }
    var model = $get(moto_ddlMotoModelId).value;
    moto_vehiculeSearch(dateref,manufacturer,cc,model,0,"Model_Asc",false);
    return false;
}
function moto_vehiculeSearch(dateRef,manufacturer,cc,model,searchType,sort,takeIfOnlyOne)
{
    // Call WS
    //alert('Search : ' + manufacturer + ' ' + cc + ' ' + model);
    var moto_vehiculeSearch_Callback = function(result, eventArgs)
    {
        moto_vehiculeTableDisplay(result, searchType,sort,takeIfOnlyOne);
    }
    AssurlandWeb.MotoVehiculeSearch.GetVehiculesWithDate(dateRef,manufacturer, cc, model,sort , moto_vehiculeSearch_Callback, moto_vehiculeSearch_FailedCallback);
    //AssurlandWeb.MotoVehiculeSearch.GetVehicules(manufacturer, cc, model,sort , moto_vehiculeSearch_Callback, moto_vehiculeSearch_FailedCallback);
}
function moto_vehiculeSearch_FailedCallback(error)
{
    displayAspNetFrameworkError("AssurlandWeb.MotoVehiculeSearch.GetVehiculesWithDate failed",error);
}

var vehiculeTableId = "vehiculeTable";
var vehiculeValidateBtnId = "vehiculeValidateBtnId";
var vehiculeTableHeaderCssClass = "al_header";
var vehiculeTableHeaderOtherCssClass = "al_header2";
var vehiculeTableRow1CssClass = "al_row2";
var vehiculeTableRow2CssClass = "al_row1";
var vehiculeTableRowSelectedCssClass = "al_rowSelected";
function moto_vehiculeTableDisplay(result, searchType,sort,takeIfOnlyOne)
{
    // Reset selected moto
    $get(moto_hfMotoId).value = '';
    if(result.length == 1 && takeIfOnlyOne)
    {
        $get(moto_hfMotoId).value = result[0].InsurerId;
        moto_vehiculeValidateBtnClick();
    }
    else{
    
        $get(moto_hfsearchTypeId).value=searchType;
        // Show rows
        showRow(moto_rowVehiculeSelectionId);
        
        //alert(result);
        var str = "";
        var className = "";
        var rbId;
        var strHeader = ""
        
        if(result.length > 0)
        {
            // Header text
            strHeader = "Nous&nbsp;avons&nbsp;" + (searchType == 1 ? "déja" : "") + "&nbsp;trouvé&nbsp;<b>" + result.length  + "&nbsp;véhicule" + (result.length  == 1 ? "" : "s") + "</b>&nbsp;correspondant&nbsp;à&nbsp;vos&nbsp;critères,&nbsp;sélectionnez&nbsp;le&nbsp;vôtre&nbsp;:&nbsp;"
            
            str += '<table cellpadding="0" cellspacing="0" class="al_formVehiculeHeader">'
            str += '<tr>'
            str += '<td class="al_left"></td>'
            str += '<td class="al_center">' + strHeader + '</td>'
            str += '<td class="al_right"></td>'
            str += '</tr>'
            str += '</table>'
            // Vehicule list
            str += '<table cellpadding="0" cellspacing="0" class="al_formVehiculeTable">';
            str += '<tr>';
            str += '<td class="' + vehiculeTableHeaderCssClass + '" width="72" align="center">Cochez une case</td>';
            str += '<td class="' + vehiculeTableHeaderOtherCssClass + '" width="220" align="left" ';
            if (sort=="Model_Asc")
               str +='onclick="moto_vehiculeSearchSort(\'Model_Desc\');">Modèle <img class="al_sortAsc" src="/Images2/px.gif" /></td>';
            else {
                if (sort=="Model_Desc")
                    str +='onclick="moto_vehiculeSearchSort(\'Model_Asc\');">Modèle <img class="al_sortDesc" src="/Images2/px.gif" /></td>';
                else
                    str +='onclick="moto_vehiculeSearchSort(\'Model_Asc\');">Modèle</td>'; 
            }    
            str += '<td class="' + vehiculeTableHeaderOtherCssClass + '" width="130" align="left"';
            if (sort=="Type_Asc")
               str +='onclick="moto_vehiculeSearchSort(\'Type_Desc\');">Type Mine/CNIT <img class="al_sortAsc" src="/Images2/px.gif" /></td>';
            else {
                if (sort=="Type_Desc")
                    str +='onclick="moto_vehiculeSearchSort(\'Type_Asc\');">Type Mine/CNIT <img class="al_sortDesc" src="/Images2/px.gif" /></td>';
                else
                    str +='onclick="moto_vehiculeSearchSort(\'Type_Asc\');">Type Mine/CNIT</td>'; 
            }   
            
            str += '<td class="' + vehiculeTableHeaderOtherCssClass + '" align="left" ';
            if (sort=="Annee_Asc")
               str +='onclick="moto_vehiculeSearchSort(\'Annee_Desc\');">Année de fabrication<img class="al_sortAsc" src="/Images2/px.gif" /></td>';
            else {
                if (sort=="Annee_Desc")
                    str +='onclick="moto_vehiculeSearchSort(\'Annee_Asc\');">Année de fabrication<img class="al_sortDesc" src="/Images2/px.gif" /></td>';
                else
                    str +='onclick="moto_vehiculeSearchSort(\'Annee_Asc\');">Année de fabrication</td>'; 
            }  
            str += '</tr>';
            str += '</table>';
            str += '<div class="al_formVehiculeTableWrapper"'
            if(result.length > 15)
            {
                str += ' style="height:310px;overflow:hidden;overflow-y:auto;display:block;"';
            }
            str += '>';
            str += '<table id="' + vehiculeTableId + '" cellpadding="0" cellspacing="0" class="al_formVehiculeTable">';
            for(var i=0; i<result.length; i++)
            {
                rbId = "vehiculeRb" + i;
                if(i%2 == 0)
                    className = vehiculeTableRow2CssClass;
                else
                    className = vehiculeTableRow1CssClass;
                str += '<tr class="' + className + '" onmouseover="moto_vehiculeTableRowMouseOver(this,' + i + ',\'' + rbId + '\');" onmouseout="moto_vehiculeTableRowMouseOut(this,' + i + ',\'' + rbId + '\');" onclick="moto_vehiculeTableRowClick(this,' + i + ',\'' + rbId + '\');" onDblClick="moto_vehiculeTableRowClick(this,' + i + ',\'' + rbId + '\');moto_vehiculeValidateBtnClick();">';
                str += '<td width="72" align="center"><input type="radio" id="' + rbId + '" name="vehiculeRb" value="' + result[i].InsurerId + '" /></td>';
                str += '<td width="220" align="left">' + result[i].MotoModel + '</td>';
                str += '<td width="130" align="left">';
                if (result[i].CodeCnit != '')
                    str += result[i].CodeCnit; 
                else
                    str += result[i].TypeMine;
                
                str += '</td>';
                str += '<td align="left">';
                if(result[i].MotoFirstMadeYear != '' || result[i].MotoLastMadeYear != '')
                    str += 'de ' + (result[i].MotoFirstMadeYear != "" ? result[i].MotoFirstMadeYear : "-") + ' à ' + (result[i].MotoLastMadeYear != "" ? result[i].MotoLastMadeYear : "-");
                else
                    str += '-';
                str += '</td>';
                str += '</tr>';
            }
             str += '</table>';
            str += '</div>';
            //cancel link
            str += '<table width="100%" class="al_formNavigation"><tr><td class="al_back"><a title="Modifier" href="javascript:moto_hideVehicleSelectionPopUp();"> << MODIFIER </a></td>';
            
            // Validation buton
            str += '<td colspan="2"><input type="image" id="' + vehiculeValidateBtnId + '" title="Valider votre véhicule" class="al_formVehiculeBtnValidateOff" src="/Images2/px.gif" onclick="moto_vehiculeValidateBtnClick();return false;" /></td><table>';
            
           
        }
        else
        {
            str = "Aucun résultat";
        }
        setInnerHtml(moto_panelVehiculeSearchResultId, str);
        $find(moto_popupExtenderId).show();
    }
}
function moto_vehiculeTableRowMouseOver(row, index, rblId)
{
    row.className = vehiculeTableRowSelectedCssClass;
}
function moto_vehiculeTableRowMouseOut(row, index, rblId)
{
    if($get(rblId).checked == false)
    {
        if(index%2 == 0)
            row.className = vehiculeTableRow2CssClass
        else
            row.className = vehiculeTableRow1CssClass
    }
}
function moto_vehiculeTableRowClick(row, index, rblId)
{
    // Reset all other selected row
    var elt = $get(vehiculeTableId);
    if(elt != null)
    {
        var obj = elt.getElementsByTagName("tr");
        if(obj && obj.length)
        {
            for(var i=0; i < obj.length; i++)
            {
                if(i%2 == 0)
                    obj[i].className = vehiculeTableRow2CssClass
                else
                    obj[i].className = vehiculeTableRow1CssClass
            }
        }
    }
    // Set selected row
    $get(rblId).checked=true;
    row.className = vehiculeTableRowSelectedCssClass;
    // Update moto ID
    $get(moto_hfMotoId).value = $get(rblId).value;
    // Active button
    $get(vehiculeValidateBtnId).className = 'al_formVehiculeBtnValidateOn';
}
function moto_vehiculeValidateBtnClick()
{
    //alert($get(moto_hfMotoId).value);
    
    // Check selection
    if($get(moto_hfMotoId).value == '')
    {
        alert("Veuillez sélectionner un véhicule");
        return false;
    }
    
    var motoId = $get(moto_hfMotoId).value;
    var moto_vehiculeValidate_Callback = function(result, eventArgs)
    {
        var str = '';
        str += result.MotoCodeMake + ' ' + result.Power + ' ' + result.MotoModel;
        if(result.TypeMine != '')
            str += ' ' + result.TypeMine
        if(result.MotoFirstMadeYear != '' || result.MotoLastMadeYear != '')
            str += ' (' + (result.MotoFirstMadeYear != "" ? result.MotoFirstMadeYear : "-") + ' à ' + (result.MotoLastMadeYear != "" ? result.MotoLastMadeYear : "-") + ')';

        setInnerHtml(moto_lblSelectedVehiculeId, str);
        showRow(moto_rowSelectedVehiculeId);
        hideRow(moto_rowMotoManufacturerId);        
        hideRow(moto_rowMotoCcId);
        hideRow(moto_rowMotoModelId);
        hideRow(moto_rowBtnSearchVehiculeId);
        hideRow(moto_rowSelectionTitleId);
        moto_hideVehicleSelectionPopUp();
    }
    AssurlandWeb.MotoVehiculeSearch.GetVehicule(motoId, moto_vehiculeValidate_Callback, moto_vehiculeValidate_FailedCallback);
    
}
function moto_vehiculeValidate_FailedCallback(error)
{
    displayAspNetFrameworkError("AssurlandWeb.MotoVehiculeSearch.GetVehicule failed",error);
}
function moto_vehiculeModifySelectedVehiculeClick()
{
    moto_vehiculeShowQuestion();
}
function moto_vehiculeShowQuestion()
{
    $get(moto_hfMotoId).value = "";
    hideRow(moto_rowSelectedVehiculeId);
    showRow(moto_rowMotoManufacturerId);    
    showRow(moto_rowMotoCcId);
    showRow(moto_rowMotoModelId);
    showRow(moto_rowBtnSearchVehiculeId);
    showRow(moto_rowSelectionTitleId);
}

function hideVehicleSelectionPopUp()
{
    moto_hideVehicleSelectionPopUp();
}
function moto_hideVehicleSelectionPopUp()
{
    $find(moto_popupExtenderId).hide();
}

function moto_vehiculeSearchSort(sort)
{
    var manufacturer = $get(moto_ddlMotoManufacturerId).value;
    var cc = $get(moto_ddlMotoCcId).value;
    var model = $get(moto_ddlMotoModelId).value;
    var searchType = $get(moto_hfsearchTypeId).value;
    var dateref = moto_getDateRef();  
    moto_vehiculeSearch(dateref,manufacturer,cc,model,searchType,sort,false);
    return false;
}

function moto_updateTitle(itemSex) {
    if(itemSex.value == 'H') {
        $get(moto_ddlTitleId).value = 'MONSIEUR';
    } else {
        $get(moto_ddlTitleId).value = 'MADEMOISELLE';
    }
}

// CYCLO //
var cyclo_ddlMotoManufacturerId;
var cyclo_ddlMotoModelId;

function cyclo_vehiculeUpdateManufacturer(manufacturer)
{
    // Clear sub info
    dropDownList_clear(cyclo_ddlMotoModelId);
    // Call WS
    AssurlandWeb.MotoVehiculeSearch.GetCycloModelByManufacturer(manufacturer, cyclo_vehiculeUpdateManufacturer_Callback, cyclo_vehiculeUpdateManufacturer_FailedCallback);
}
function cyclo_vehiculeUpdateManufacturer_Callback(result, eventArgs)
{
    var ccArray = new Array();
    for(var i=0; i<result.length; i++)
    {
        ccArray[i] = new ListItem(result[i].InsurerId, result[i].MotoModel);
    }
    dropDownList_bind(cyclo_ddlMotoModelId, -1, ccArray);  
}
// This is the callback function invoked if the Web service failed.
function cyclo_vehiculeUpdateManufacturer_FailedCallback(error)
{
    displayAspNetFrameworkError("AssurlandWeb.MotoVehiculeSearch.GetCycloModelByManufacturer failed",error);
}

