﻿function immatkeypress(immat) {
    //$("#validerSimulerImmat").bind("click", validatestep1());
}
function nomkeypress(nom) {

}
function validatestep1() {
    var correctimmat = $("#txtimmat").val();

    var intIndexOfMatch = correctimmat.indexOf(" ");

    while (intIndexOfMatch != -1) {
        correctimmat = correctimmat.replace(" ", "")
        intIndexOfMatch = correctimmat.indexOf(" ");
    }


    //Evol -
    intIndexOfMatch = correctimmat.indexOf("-");
    while (intIndexOfMatch != -1) {
        correctimmat = correctimmat.replace("-", "")
        intIndexOfMatch = correctimmat.indexOf("-");
    }

    //alert(correctimmat);
    if (correctimmat.length <= 8) {
        var query = "annee=-1&brandId=-1&modelId=-1&cylindreId=-1&immat=" + correctimmat;
        // Xiti
        //xt_med('F', '', 'Home_Page::Revision_Simulateur_Etape1_Validation');
        var url = FolderRacine + "/Simulator/plan-de-revision-etap.aspx?" + query;
        document.location = url;
    }
    else {
        return false;        
    }
}

function validatestep1page() {
    var correctimmat = $("#txtimmat").val();

    var intIndexOfMatch = correctimmat.indexOf(" ");

    while (intIndexOfMatch != -1) {
        correctimmat = correctimmat.replace(" ", "")
        intIndexOfMatch = correctimmat.indexOf(" ");
    }


    //Evol -
    intIndexOfMatch = correctimmat.indexOf("-");
    while (intIndexOfMatch != -1) {
        correctimmat = correctimmat.replace("-", "")
        intIndexOfMatch = correctimmat.indexOf("-");
    }

    //alert(correctimmat);
    if (correctimmat.length >= 2 && correctimmat.length <= 8) {
        var query = "annee=-1&brandId=-1&modelId=-1&cylindreId=-1&immat=" + correctimmat + "&provenance=page";
        // Xiti
        //xt_med('F', '', 'Home_Page::Revision_Simulateur_Etape1_Validation');
        var url = FolderRacine + "/Simulator/plan-de-revision-etap.aspx?" + query;
        document.location = url;
    }
    else {
        return false;
    }
}
function validateetape8() {
    var opt = document.getElementsByName("modele");

    var val = "";

    for (i = 0; i < opt.length; i++) {
        if (opt[i].checked == true)
            val = opt[i].value;
    }
    if (val != "") {
        //Split + call
        
        retval = val.split('|');
        /*
        0 - Marque
        1 - Modele
        2 - Cylindre
        3 - Annee
        4 - Ktypes 
        */
        marqueimmat = retval[0];
        modeleimmat = retval[1];
        cylindreimmat = retval[2];
        anneeimmat = retval[3];
        ktypeimmat = retval[4];

        //Appelle au webservice de midas ktype + annee

        performSearchButtonImmat(ktypeimmat, anneeimmat, marqueimmat, modeleimmat, cylindreimmat); // Method de sim.js
    }
    else {
        return false;
    }
}

function validateit() {

    //Verifiez s'il y a bien 3 caracters
    nom = $("#txtnom").val();
    var length = nom.length;
    nom = cleanAccents(nom);
    nom = nom.toUpperCase();
    if (length != nom.length) {
        nom = nom + " ";
    }
    nom = nom.substr(0, 3);

    if (nom.length != 3) {
        alert("Merci de rentre les 3 premiers caractéres du nom sur la carte grise");
        $("#txtnom").focus();
    }
    else {        
        $("#ajaxetape7").show();
        immat = getParameterByName("immat");
        $.ajax(
            {
                type: "GET",
                url: "/simulateuraaaredirector.aspx",
                data: "immat=" + immat + "&nom=" + nom,
                success: function(data) {
                    if (data == "-1") {
                        // Pas de ligne du webservice --> Voiture non reconnue
                        ShowEtape9(); //sim.js
                    }
                    else if (data.substr(0, 2) == "-2") {
                        //Erreur
                        //alert(data.replace("-2|", ""));
                        ShowEtape9(); //sim.js
                    }
                    else if (data.substr(0, 2) == "-3") {
                        //Pas de IMMAT
                        ShowEtape9(); //sim.js
                    }
                    else {


                        //Test plusieur Vechicule
                        retvalktype = data.split('$');

                        if (retvalktype.length >= 2) {
                            //plusiers kypes
                            ShowEtape8(retvalktype); //sim.js
                        }
                        else {

                            //Recuperer les data
                            retval = data.split('|');
                            /*
                            0 - Marque
                            1 - Modele
                            2 - Cylindre
                            3 - Annee
                            4 - Ktypes 
                            */
                            marqueimmat = retval[0];
                            modeleimmat = retval[1];
                            cylindreimmat = retval[2];
                            anneeimmat = retval[3];
                            ktypeimmat = retval[4];
                            //alert(anneeimmat);

							
							//xiti
                            var ispage = getParameterByName("provenance");
                            if (ispage == "") {
                                //from HP
                                xt_med('F', '', 'Home_Page::Revision_Simulateur_Etape1_Validation_Immat');
                            }
                            else {
                                //from page
                                xt_med('F', '', 'Metiers_et_Services::Revision_Simulateur_Etape1_Validation_Immat');

                            }
							
                            //Appelle au webservice de midas ktype + annee

                            performSearchButtonImmat(ktypeimmat, anneeimmat, marqueimmat, modeleimmat, cylindreimmat); // Method de sim.js
                        }
                    }

                },
                error: function() {
                    alert("Une erreur c'est produit, merci de re-essayer");
                    $("#ajaxetape7").hide();
                }
            });
    }
    //alert("test");
    return false;
}
