﻿var isIE6 = $.browser.name == "msie" && $.browser.versionNumber == 6;
function resetDropDownList() {
    $.timer(10, function(timer) {
        timer.stop();
        alert(arguments);
        for (var i = 0; i < arguments.length; i++) {

            select = arguments[i];
            select.reset();
        }

    });
}
function bindOptionResultsBrand(data, droplists) {
    ddlBrand = $("#brandId").html("");
    $("#brandId").html("");
    $("#modelId").html("");
    $("#cylindreId").html("");

    ddlBrand.append($("<option></option>").val('').html('Marque...'));
    for (var k = 0; k < data.BRAND.length; k++) {
        ddlBrand.append($("<option></option>").val(data.BRAND[k].brandId).html(data.BRAND[k].brandName));
    }

    $.timer(100, function(timer) {
        droplists["brandId"].reset();
        droplists["modelId"].reset();
        droplists["cylindreId"].reset();
        timer.stop();
    });
    //resetDropDownList(droplists["modelId"],droplists["cylindreId"],droplists["brandId"]);
    $("#simulator_loader").hide();

}
function anneeDropDown_Changed(droplists) {
    $("#brandId").html("");
    $("#modelId").html("");
    $("#cylindreId").html("");
    var annee = droplists["annee"].select.val();
    $("#simulator_loader").show();
    if (annee != 0) {
        $.ajax({
            type: "POST", url: FolderRacine + "/Simulator/ddlBrands.ashx", data: { annee: annee }, dataType: "json",
            success: function(data) {
                bindOptionResultsBrand(data, droplists);
            }
        });
    }
}
function bindOptionResultsModel(data, droplists) {
    $("#modelId").html("");
    $("#cylindreId").html("");
    ddlModel = $("#modelId").html("");
    ddlModel.append($("<option></option>").val('').html('Modèle...'));
    for (var k = 0; k < data.MODEL.length; k++) {

        ddlModel.append($("<option></option>").val(data.MODEL[k].modelId).html(data.MODEL[k].modelName));
    }
    $("#simulator_loader").hide();
    $.timer(100, function(timer) {
        droplists["modelId"].reset();
        droplists["cylindreId"].reset();
        timer.stop();
    });

}

function brandDropDown_Changed(droplists) {
    var annee = droplists["annee"].select.val();
    var brandId = droplists["brandId"].select.val();
    if (brandId != 0) {
        $("#simulator_loader").show();
        $.ajax(
                {
                    type: "POST",
                    url: FolderRacine + "/Simulator/ddlModels.ashx",
                    data: { annee: annee, brandId: brandId },
                    dataType: "json",
                    success: function(data) {
                        bindOptionResultsModel(data, droplists);
                    }
                });
    }
}

function redirectToSimulator() {

    if (isHomePage == true) {
        var annee = $("#annee").val();
        var brandId = $("#brandId").val();
        var modelId = $("#modelId").val();
        var cylindreId = 0;
        var query = "annee=" + annee + "&brandId=" + brandId + "&modelId=" + modelId + "&cylindreId=" + cylindreId;
        var url = FolderRacine + "/Simulator/plan-de-revision-etap.aspx?" + query;
        document.location = url;
    }
    else {
        //call in sim.js
        NonEligible();
    }
}
function bindOptionResultsModelCylindre(data, droplists) {
    $("#cylindreId").html("");
    ddlCylindre = $("#cylindreId").html("");
    ddlCylindre.append($("<option></option>").val('').html('Cylindrée...'));
    for (var k = 0; k < data.CYLINDRE.length; k++) {

        ddlCylindre.append($("<option></option>").val(data.CYLINDRE[k].cylindreId).html(data.CYLINDRE[k].cylindreName.split("(", 1) + " (" + data.CYLINDRE[k].cylindrePuissance + " ch)"));
    }
    $("#simulator_loader").hide();
    $.timer(100, function(timer) {
        droplists["cylindreId"].reset();
        timer.stop();
    });
    if (data.CYLINDRE.length == 0) {
        redirectToSimulator();
    }
}

function modelDropDown_Changed(droplists) {
    var annee = droplists["annee"].select.val();
    var brandId = droplists["brandId"].select.val();
    var modelId = droplists["modelId"].select.val();
    if (modelId != 0) {
        $("#simulator_loader").show();
        $.ajax(
                {
                    type: "POST",
                    url: FolderRacine + "/Simulator/ddlCylindre.ashx",
                    data: { annee: annee, brandId: brandId, modelId: modelId },
                    dataType: "json",
                    success: function(data) {
                        bindOptionResultsModelCylindre(data, droplists);
                    }
                });
    }
}

function disablevaliderSimuler() {
    $("#validerSimuler").unbind("click");

    $("#validerSimuler").bind("click", function() {
        return false;
    });
}

function cylindreIdDropDown_Changed(droplists) {
    var annee = droplists["annee"].select.val();
    var brandId = droplists["brandId"].select.val();
    var modelId = droplists["modelId"].select.val();
    var cylindreId = droplists["cylindreId"].select.val();
    if (cylindreId != 0) {

        $("#validerSimuler").bind("click", function() {
            var query = "annee=" + annee + "&brandId=" + brandId + "&modelId=" + modelId + "&cylindreId=" + cylindreId;
            // Xiti
            xt_med('F', '', 'Home_Page::Revision_Simulateur_Etape1_Validation');
            var url = FolderRacine + "/Simulator/plan-de-revision-etap.aspx?" + query;
            document.location = url;
        });
    }
}

function redirect(url) {
    if (url != 'all') {
        if (url.indexOf('http://') >= 0) {
            if (url == 'http://franchise.midas.fr/') {
                xt_med('C', '', 'Acces_Site_Franchise', 'S');
            }

            if (url == 'http://recrutement.midas.fr/') {
                xt_med('C', '', 'Acces_Site_RH', 'S');
            }

            window.open(url);
        }
        else {
            document.location = url;
        }
    }

}
function bindOptionResultsCentre(data, droplists) {
    ddlCentres.html("");

    ddlCentres.append($("<option></option>").val('').html('Choisissez un objet'));
    for (var k = 0; k < data.length; k++) {
        ddlCentres.append($("<option></option>").val(data[k].IdStore).html(data[k].StoreName));
    }
    droplists[ddlCentres.attr("id")].reset();
}
function ddlDeparmentsChanged(o, droplists) {
    var deptid = o.select.val();
    if (deptid > 0) {
        $.ajax(
        {
            type: "POST",
            url: "GetCentre.ashx",
            data: { id: deptid },
            dataType: "json",
            success: function(data) {
                bindOptionResultsCentre(data, droplists);
            }
        });
    }
}



/************************* On Type Change Get Width **************************************************/
function tyre_TypeDropDown_Changed(droplists) {
    $("#simulator_loader").show();
    $("#tyre_width").html("");
    ddlWidth = $get("tyre_width");
    ddlWidth.options.length = 0;
    newOption = new Option("Sélectionnez", "");
    ddlWidth.options.add(newOption);

    $("#tyre_serie").html("");
    ddlSerie = $get("tyre_serie");
    ddlSerie.options.length = 0;
    newOption = new Option("Sélectionnez", "");
    ddlSerie.options.add(newOption);

    $("#tyre_size").html("");
    ddlSize = $get("tyre_size");
    ddlSize.options.length = 0;
    newOption = new Option("Sélectionnez", "");
    ddlSize.options.add(newOption);

    $("#tyre_weight").html("");
    ddlWeight = $get("tyre_weight");
    ddlWeight.options.length = 0;
    newOption = new Option("Sélectionnez", "");
    ddlWeight.options.add(newOption);

    $("#tyre_speed").html("");
    ddlSpeed = $get("tyre_speed");
    ddlSpeed.options.length = 0;
    newOption = new Option("Sélectionnez", "");
    ddlSpeed.options.add(newOption);

    var tyre_type = droplists["tyre_type"].select.val();
    Sys.Net.WebServiceProxy.invoke("TyreWebService.asmx", "GetWidthByTyreType", false, { tyre_type: tyre_type }, bindOptionResultsWidth);

    $.timer(500, function(timer) {
        droplists["tyre_width"].reset();
        droplists["tyre_serie"].reset();
        droplists["tyre_size"].reset();
        droplists["tyre_weight"].reset();
        droplists["tyre_speed"].reset();
        $("#simulator_loader").hide();
        timer.stop();
    });


}
function bindOptionResultsWidth(data) {

    for (var k = 0; k < data.length; k++) {
        newOption = new Option(data[k].WidthId, data[k].Width);
        ddlWidth.options.add(newOption);
    }

}
/*************************   -----------  **************************************************/


/*************************  On Width Change Get Serie ***************************************/
function tyre_WidthDropDown_Changed(droplists) {
    $("#simulator_loader").show();
    $("#tyre_serie").html("");
    ddlSerie = $get("tyre_serie");
    ddlSerie.options.length = 0;
    newOption = new Option("Sélectionnez", "");
    ddlSerie.options.add(newOption);

    $("#tyre_size").html("");
    ddlSize.options.length = 0;
    newOption = new Option("Sélectionnez", "");
    ddlSize.options.add(newOption);

    $("#tyre_weight").html("");
    ddlWeight.options.length = 0;
    newOption = new Option("Sélectionnez", "");
    ddlWeight.options.add(newOption);

    $("#tyre_speed").html("");
    ddlSpeed.options.length = 0;
    newOption = new Option("Sélectionnez", "");
    ddlSpeed.options.add(newOption);

    var tyre_type = droplists["tyre_type"].select.val();
    var tyre_width = droplists["tyre_width"].select.val();
    Sys.Net.WebServiceProxy.invoke("TyreWebService.asmx", "GetSerieByTypeWidth", false, { tyre_type: tyre_type, tyre_width: tyre_width }, bindOptionResultsSerie);

    $.timer(1000, function(timer) {
        droplists["tyre_serie"].reset();
        droplists["tyre_size"].reset();
        droplists["tyre_weight"].reset();
        droplists["tyre_speed"].reset();
        $("#simulator_loader").hide();
        timer.stop();
    });
}
function bindOptionResultsSerie(data) {
    for (var k = 0; k < data.length; k++) {
        newOption = new Option(data[k].SerieId, data[k].Serie);
        ddlSerie.options.add(newOption);
    }
}
/*************************   -----------  **************************************************/


/*************************  On Serie Change Get Size ***************************************/
function tyre_SerieDropDown_Changed(droplists) {
    $("#simulator_loader").show();
    $("#tyre_size").html("");

    ddlSize.options.length = 0;
    newOption = new Option("Sélectionnez", "");
    ddlSize.options.add(newOption);

    $("#tyre_weight").html("");
    ddlWeight.options.length = 0;
    newOption = new Option("Sélectionnez", "");
    ddlWeight.options.add(newOption);

    $("#tyre_speed").html("");
    ddlSpeed.options.length = 0;
    newOption = new Option("Sélectionnez", "");
    ddlSpeed.options.add(newOption);

    var tyre_type = droplists["tyre_type"].select.val();
    var tyre_width = droplists["tyre_width"].select.val();
    var tyre_serie = droplists["tyre_serie"].select.val();
    Sys.Net.WebServiceProxy.invoke("TyreWebService.asmx", "GetSizeByTypeWidthSerie", false, { tyre_type: tyre_type, tyre_width: tyre_width, tyre_serie: tyre_serie }, bindOptionResultsSize);

    $.timer(1000, function(timer) {
        droplists["tyre_size"].reset();
        droplists["tyre_weight"].reset();
        droplists["tyre_speed"].reset();
        $("#simulator_loader").hide();
        timer.stop();
    });

}
function bindOptionResultsSize(data) {
    for (var k = 0; k < data.length; k++) {
        newOption = new Option(data[k].SizeId, data[k].Size);
        ddlSize.options.add(newOption);
    }
}
/*************************   -----------  **************************************************/

/*************************  On Size Change Get Weight ***************************************/
function tyre_SizeDropDown_Changed(droplists) {
    $("#simulator_loader").show();
    $("#tyre_weight").html("");
    ddlWeight.options.length = 0;
    newOption = new Option("Sélectionnez", "");
    ddlWeight.options.add(newOption);

    $("#tyre_speed").html("");
    ddlSpeed.options.length = 0;
    newOption = new Option("Sélectionnez", "");
    ddlSpeed.options.add(newOption);

    var tyre_type = droplists["tyre_type"].select.val();
    var tyre_width = droplists["tyre_width"].select.val();
    var tyre_serie = droplists["tyre_serie"].select.val();
    var tyre_size = droplists["tyre_size"].select.val();

    Sys.Net.WebServiceProxy.invoke("TyreWebService.asmx", "GetWeightByTypeWidthSerieSize", false, { tyre_type: tyre_type, tyre_width: tyre_width, tyre_serie: tyre_serie, tyre_size: tyre_size }, bindOptionResultsWeight);
    $.timer(1000, function(timer) {
        droplists["tyre_weight"].reset();
        droplists["tyre_speed"].reset();
        $("#simulator_loader").hide();
        timer.stop();
    });

}
function bindOptionResultsWeight(data) {
    for (var k = 0; k < data.length; k++) {
        newOption = new Option(data[k].WeightId, data[k].Weight);
        ddlWeight.options.add(newOption);
    }
}
/*************************   -----------  **************************************************/

/*************************  On Weight Change Get Speed ***************************************/
function tyre_WeightDropDown_Changed(droplists) {
    $("#simulator_loader").show();
    $("#tyre_speed").html("");
    ddlSpeed.options.length = 0;
    newOption = new Option("Sélectionnez", "");
    ddlSpeed.options.add(newOption);

    var tyre_type = droplists["tyre_type"].select.val();
    var tyre_width = droplists["tyre_width"].select.val();
    var tyre_serie = droplists["tyre_serie"].select.val();
    var tyre_size = droplists["tyre_size"].select.val();
    var tyre_weight = droplists["tyre_weight"].select.val();
    Sys.Net.WebServiceProxy.invoke("TyreWebService.asmx", "GetSpeedByTypeWidthSerieSizeWeight", false, { tyre_type: tyre_type, tyre_width: tyre_width, tyre_serie: tyre_serie, tyre_size: tyre_size, tyre_weight: tyre_weight }, bindOptionResultsSpeed);

    $.timer(1000, function(timer) {
        droplists["tyre_speed"].reset();
        $("#simulator_loader").hide();
        timer.stop();
    });

}
function bindOptionResultsSpeed(data) {
    for (var k = 0; k < data.length; k++) {
        newOption = new Option(data[k].SpeedId, data[k].Speed);
        ddlSpeed.options.add(newOption);
    }
}
/*************************   -----------  **************************************************/

/*************************  On Speed Change Get RunFlat ***************************************/
function tyre_SpeedDropDown_Changed(droplists) {
    $("#simulator_loader").show();

    var tyre_type = droplists["tyre_type"].select.val();
    var tyre_width = droplists["tyre_width"].select.val();
    var tyre_serie = droplists["tyre_serie"].select.val();
    var tyre_size = droplists["tyre_size"].select.val();
    var tyre_weight = droplists["tyre_weight"].select.val();
    var tyre_weight = droplists["tyre_weight"].select.val();
    var tyre_speed = droplists["tyre_speed"].select.val();

    Sys.Net.WebServiceProxy.invoke("TyreWebService.asmx", "GetRunFlat", false, { tyre_type: tyre_type, tyre_width: tyre_width, tyre_serie: tyre_serie, tyre_size: tyre_size, tyre_weight: tyre_weight, tyre_speed: tyre_speed }, bindRunFlat);

    $.timer(1000, function(timer) {        
        $("#simulator_loader").hide();
        timer.stop();
    });

}
function bindRunFlat(data) {
    if (data == 1) {
        $('#runflatno').removeAttr("disabled");
        $("#runflatno").attr("checked", true);
        $("#runflatyes").attr("disabled", true);
    }
    else if (data == 2) {
        $('#runflatyes').removeAttr("disabled");
        $("#runflatyes").attr("checked", true);
        $("#runflatno").attr("disabled", true);
    }
    else {
        $('#runflatyes').removeAttr("disabled");
        $('#runflatno').removeAttr("disabled");
        $("#runflatno").attr("checked", true);
        $("#runflatno").attr("disabled", false);
    }
}
/*************************   -----------  **************************************************/
function callExternalFunction(o/*caller*/, droplists/*all droplists*/, val/*rel in <a>*/) {


    /*
    * o : selectUI object
    *   o.select : <select> in jQuery type
    *   o.elUL : list drop down, main list <ul>
    *----------------------------------------------*
    * droplists : all selectUI droplists in page
    * val : rel value in a of each selectUI option
    */
    var id = o.select.attr("id");

    if (id == "annee") {
        anneeDropDown_Changed(droplists);
        lastAction = new Date();
    }
    if (id == "brandId") {
        brandDropDown_Changed(droplists);
        lastAction = new Date();
    }
    if (id == "modelId") {
        modelDropDown_Changed(droplists);
        lastAction = new Date();
    }
    if (id == "cylindreId") {
        cylindreIdDropDown_Changed(droplists);
        lastAction = new Date();
    }

    if (id == "tyre_type") {
        $("#displayboxes").empty();
        $("#tyres_result").hide();
        $("#advice_price").hide();
        $('#runflatno').removeAttr("disabled");
        $('#runflatyes').removeAttr("disabled");
        $("#runflatno").attr("checked", true);
        tyre_TypeDropDown_Changed(droplists);
    }

    if (id == "tyre_width") {
        $("#displayboxes").empty();
        $("#tyres_result").hide();
        $("#advice_price").hide();
        $('#runflatno').removeAttr("disabled");
        $('#runflatyes').removeAttr("disabled");
        $("#runflatno").attr("checked", true);
        tyre_WidthDropDown_Changed(droplists);
    }

    if (id == "tyre_serie") {
        $("#displayboxes").empty();
        $("#tyres_result").hide();
        $("#advice_price").hide();
        $('#runflatno').removeAttr("disabled");
        $('#runflatyes').removeAttr("disabled");
        $("#runflatno").attr("checked", true);
        tyre_SerieDropDown_Changed(droplists);
    }

    if (id == "tyre_size") {
        $("#displayboxes").empty();
        $("#tyres_result").hide();
        $("#advice_price").hide();
        $('#runflatno').removeAttr("disabled");
        $('#runflatyes').removeAttr("disabled");
        $("#runflatno").attr("checked", true);
        tyre_SizeDropDown_Changed(droplists);
    }

    if (id == "tyre_weight") {
        $("#displayboxes").empty();
        $("#tyres_result").hide();
        $("#advice_price").hide();
        $('#runflatno').removeAttr("disabled");
        $('#runflatyes').removeAttr("disabled");
        $("#runflatno").attr("checked", true);
        tyre_WeightDropDown_Changed(droplists);
    }
    
    if (id == "tyre_speed") {
        $("#displayboxes").empty();
        $("#tyres_result").hide();
        $("#advice_price").hide();
        tyre_SpeedDropDown_Changed(droplists);
    }

    if (id == "lstAccess") {
        redirect(o.select.val());
    }
    if (id == 'ctl00_plhMainContent_ddlDepartment') {
        setTimeout('__doPostBack(\'ctl00$plhMainContent$ddlDepartment\',\'\')', 100)
    }
    if (id == 'ctl00_plhMainContent_ddlSubject') {
        showExtraReclamations(o, droplists);
    }
    if (id == 'ctl00_plhMainContent_ddlDeparments') {
        ddlDeparmentsChanged(o, droplists)
        //setTimeout('__doPostBack(\'ctl00$plhMainContent$ddlDeparments\',\'\')', 100)
    }

    if (id == 'ctl00_plhMainContent_ddlCentres') {
        centeridHidden.val(o.select.val());
    }
}
//------------------------------------------------//
