﻿/*########## DECLARATION AJAX ##########*/
var xmlhttp;

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    try {
        xmlhttp = new XMLHttpRequest();
    } catch (e) {
        xmlhttp=false
    }
}

function myXMLHttpRequest() {
    var xmlhttplocal;
    try {
        xmlhttplocal= new ActiveXObject("Msxml2.XMLHTTP")
    } catch (e) {
        try {
            xmlhttplocal= new ActiveXObject("Microsoft.XMLHTTP")
        } catch (E) {
            xmlhttplocal=false;
        }
    }

    if (!xmlhttplocal && typeof XMLHttpRequest!='undefined') {
        try {
            var xmlhttplocal = new XMLHttpRequest();
        } catch (e) {
            var xmlhttplocal=false;
            alert('couldn\'t create xmlhttp object');
        }
    }
    return(xmlhttplocal);
}
/*########## DECLARATION AJAX ##########*/

function Charge_Select(elmt, source) {

    // on Vide la liste
    elmt.options.length=0;

    // on parse la source
    var res = source.split(';');

    // on met la valeur par defaut
    elmt.options[0] = new Option('Type de motorisation', 'Type de motorisation');

    // on remplie le select
    for (var i=0; i<(res.length-1); ++i) {
        elmt.options[i+1] = new Option(res[i], res[i]);
    }
    
    // on selectionne le premier
    elmt.options.selectedIndex = 0;
}


function Load_Motorisation() {
    xmlhttp = new myXMLHttpRequest();

    var type = 0;
    var qs = location.search.toQueryParams();
    if (qs.type) type = qs.type;
    
    url = '/vomini/ajax/Load_Motorisation.aspx?type='+type+'&Carrosserie='+ $('modele_select').value;

    if (!$('search_enegie_essence').checked || !$('search_enegie_diesel').checked) {
        if ($('search_enegie_essence').checked) url += '&Energie=1';
        if ($('search_enegie_diesel').checked) url += '&Energie=2';
    }

    xmlhttp.onreadystatechange = function() { 
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {
                // on charge la liste
                Charge_Select($('search_motorisation'), xmlhttp.responseText);
            } else {
                alert('Un problème est survenu avec la requête.');
            }
        }   
     };
    xmlhttp.open('GET', url, true);
    xmlhttp.send(null);
}


function Load_Elmt_List() {
    xmlhttp = new myXMLHttpRequest();

    var type = 0;
    
    var qs = location.search.toQueryParams();
    if (qs.type) type = qs.type;

    // on construit les criteres
    critere = new Array();
    var now = new Date();
    
    if (qs.concess) critere['concess'] = qs.concess;
    
    critere['type'] = type;
    critere['carrosserie'] = $('modele_select').value;

    if ($('km_max').value != '150000') critere['kmax'] = $('km_max').value;
    if ($('km_min').value != '0') critere['kmin'] = $('km_min').value;
    if ($('year_max').value != now.getFullYear().toString()) critere['ymax'] = $('year_max').value;
    if ($('year_min').value != '2001') critere['ymin'] = $('year_min').value;
    if ($('price_max').value != '50000') critere['pmax'] = $('price_max').value;
    if ($('price_min').value != '2000') critere['pmin'] = $('price_min').value;

    if (!$('search_enegie_essence').checked || !$('search_enegie_diesel').checked) {
        if ($('search_enegie_essence').checked) critere['energie'] = 1;
        if ($('search_enegie_diesel').checked) critere['energie'] = 2;
    }
    
    if (!$('transmission_ma').checked || !$('transmission_au').checked) {
        if ($('transmission_ma').checked) critere['transmission'] = 1;
        if ($('transmission_au').checked) critere['transmission'] = 2;
    }
    
    if ($('search_motorisation').value != 'Type de motorisation') critere['motorisation'] = $('search_motorisation').value;
    //if ($('search_finition').value != '') critere['finition'] = $('search_finition').value;

    optselect = '';
    for(i=1; i<=6; ++i) {
        if ($('search_opt_'+i).checked) {
            if (optselect != '') optselect += ',';
            optselect += $('search_opt_'+i).value;        
        }        
    }    
    if (optselect != '') critere['opt'] = optselect;

    var searchLocation = $('search_loc').getAttribute('data-value');
    if (searchLocation != '') critere['loc'] = searchLocation;
    
    critere['order'] = Search_List_Order;
    critere['orderdir'] = Search_List_Order_Dir;
    
    var dt = new Date();
    critere['time'] = dt.getTime();
    
    url = '/vomini/ajax/Load_Liste.aspx';
    
    first = true;
    
    for (key in critere) {
        if (typeof(critere[key]) != 'function') {
            if (first) {
                url += '?'; 
                first = false;
            } else {
                url += '&';
            }
            
            url += key + '=' + critere[key];
        }
    }

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {

                var res = xmlhttp.responseText.split('|');

                // on mets la liste en place
                $('scrollbar_content').innerHTML = res[0];

                if (parseInt(res[2]) > 1) {
                    $('num_result').innerHTML = res[2] + ' résultats';
                } else {
                    if (parseInt(res[2]) == 1) {
                        $('num_result').innerHTML = res[2] + ' résultat';
                    } else {
                        $('num_result').innerHTML = 'Aucun résultat';
                    }
                }
                
                View_List();
                View_Resume_Search();
            } else {
                alert('Un problème est survenu avec la requête.');
            }
        }
    };
    xmlhttp.open('GET', url, true);
    xmlhttp.send(null);
}


function RecupInfo(id) {
    xmlhttp = new myXMLHttpRequest();
    
    var type = 0;
    var qs = location.search.toQueryParams();
    if (qs.type) type = qs.type;
    
    var dt = new Date();
    url = '/vomini/ajax/Load_Info.aspx?type='+type+'&id='+ id+'&time='+dt.getTime();

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {
                var res = xmlhttp.responseText.split(';');

                $('info_carrosserie').innerHTML = res[0];
                $('info_energie').innerHTML = res[1];
                $('info_transmission').innerHTML = res[2];
                $('info_equipements').innerHTML = res[3];
                $('info_mec').innerHTML = res[4];

                if (type == 0) {
                    $('info_photo').src = '/vomini/PhotoView.aspx?name=' + res[9] + '-' + id + '-200-150-ext.jpg';
                    $('tr_info_concessionnaire').style.display = '';
                    $('info_concessionnaire').innerHTML = res[7];
                    $('info_localisation').innerHTML = res[8];
                } else {
                    
                    $('info_photo').src = '/vomini/PhotoViewVc.aspx?name=' + id + '-200-150-.jpg';
                    $('tr_info_concessionnaire').style.display = 'none';
                    $('info_localisation').innerHTML = res[7];
                }

                $('info_prix').innerHTML = res[5] + ' &euro;';
                $('info_km').innerHTML = res[6] + ' Km';
            }
        }
    };
    xmlhttp.open('GET', url, true);
    xmlhttp.send(null);
}


function Recup_Present(id) {

    var type = 0;
    var qs = location.search.toQueryParams();
    if (qs.type) type = qs.type;

    xmlhttp = new myXMLHttpRequest();
    var dt = new Date();
    url = '/vomini/ajax/Load_Present.aspx?type='+type+'&id='+ id+'&time='+dt.getTime();

    xmlhttp.onreadystatechange = function() { 
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {
                var res = xmlhttp.responseText.split(';');
                
                $('concess_select').value = res[28];
                $('vehicule_select').value = res[29];                
                
                $('td_present_modele').innerHTML = res[0];
                $('td_present_finition').innerHTML = res[1];
                
                Affich_Info('carrosserie', res[2]);
                Affich_Info('energie', res[3]);
                Affich_Info('couleur', res[4]);
                Affich_Info('couleur_int', res[5]);
                Affich_Info('pf', res[6]);
                Affich_Info('mec', res[7]);
                Affich_Info('km', res[8]);
                Affich_Info('provenance', res[9]);
                Affich_Info('moteur', res[10]);
                Affich_Info('din', res[11]);
                Affich_Info('couple', res[12]);
                Affich_Info('vitesse', res[13]);
                Affich_Info('conso_eu', res[14]);
                Affich_Info('conso_u', res[15]);
                Affich_Info('conso_e', res[16]);
                Affich_Info('reservoir', res[17]);
                Affich_Info('poids', res[18]);
                Affich_Info('coffre', res[19]);
                
                $('td_present_prix').innerHTML = res[20];                
                $('prix_value').value = res[30];
                $('finan_nom_concess').innerHTML = res[31];
                $('finan_loc_concess').innerHTML = res[32];
                $('produit_select').value = res[33];                
                
                if (type == 0) {                
                    Affich_Info('garantie', res[21], 'Garantie : ');
                    Affich_Info('garantie_duree', res[22], 'Durée : ', ' mois');

                    if (res[21] == '' || res[22] == '') {
                        $('tr_present_head_garantie').style.display = 'none';
                    } else {
                        $('tr_present_head_garantie').style.display = '';
                    }

                    $('td_present_concess').innerHTML = res[23];
                } else {
                    Affich_Info('garantie', res[21], 'Garantie : ');
                    
                    if (res[21] == '') {
                        $('tr_present_head_garantie').style.display = 'none';
                    } else {
                        $('tr_present_head_garantie').style.display = '';
                    }
                    
                    $('td_present_concess').innerHTML = res[23];
                }
                
                $('td_present_options').innerHTML = res[24];
                
                var count_img = 0;
                $$('.ongletIss').invoke('removeClassName', 'active');
                $('ongletIss1').addClassName('active');
                $('Iss1').style.display = '';
                $('Iss2').style.display = 'none';
                $('Iss3').style.display = 'none';                
                
                // on detruit le slide si deja lancer
                if (Iss) {
                    Iss.DestroySlide();
                } else {
                    Iss = new ImagesSlideShow();
                }
                
                if (res[25] != '') {
                    ++count_img;
                    if (type == 0) {
                        $('img_present_1').src = '/vomini/PhotoView.aspx?name='+res[25]+'-286-213-ext.jpg';
                        $('img_present_print_1').src = '/vomini/PhotoView.aspx?name='+res[25]+'-286-213-ext.jpg';
                        $('img_present_min_1').src = '/vomini/PhotoView.aspx?name='+res[25]+'-82-62-ext.jpg';
                    } else {
                        $('img_present_1').src = '/vomini/PhotoViewVc.aspx?name='+id+'-286-213-.jpg';
                        $('img_present_print_1').src = '/vomini/PhotoViewVc.aspx?name='+id+'-286-213-.jpg';
                        $('img_present_min_1').src = '/vomini/PhotoViewVc.aspx?name='+id+'-82-62-.jpg';
                    }                    
                    $('Iss1').addClassName('Iss');
                } else {
                    $('Iss1').addClassName('NoIss');
                    $('img_present_1').src = '/vomini/commun/img/default.png';
                    $('img_present_print_1').src = '/vomini/commun/img/default.png';
                    $('img_present_min_1').src = '/vomini/commun/img/default_min.png';
                }
                
                if (res[26] != '') {
                    ++count_img;
                    if (type == 0) {
                        $('img_present_2').src = '/vomini/PhotoView.aspx?name='+res[26]+'-286-213-int.jpg';
                        $('img_present_print_2').src = '/vomini/PhotoView.aspx?name='+res[26]+'-286-213-int.jpg';
                        $('img_present_min_2').src = '/vomini/PhotoView.aspx?name='+res[26]+'-82-62-int.jpg';
                    } else {
                        $('img_present_2').src = '/vomini/PhotoViewVc.aspx?name='+id+'-286-213-int.jpg';
                        $('img_present_print_2').src = '/vomini/PhotoViewVc.aspx?name='+id+'-286-213-int.jpg';
                        $('img_present_min_2').src = '/vomini/PhotoViewVc.aspx?name='+id+'-82-62-int.jpg';
                    } 
                    
                    $('Iss2').addClassName('Iss');
                } else {
                    $('Iss2').addClassName('NoIss');
                    $('img_present_2').src = '/vomini/commun/img/default.png';
                    $('img_present_print_2').src = '/vomini/commun/img/default.png';
                    $('img_present_min_2').src = '/vomini/commun/img/default_min.png';
                }
                
                if (res[27] != '') {
                    ++count_img;
                    if (type == 0) {
                        $('img_present_3').src = '/vomini/PhotoView.aspx?name='+res[27]+'-286-213-ext_arr.jpg';
                        $('img_present_print_3').src = '/vomini/PhotoView.aspx?name='+res[27]+'-286-213-ext_arr.jpg';
                        $('img_present_min_3').src = '/vomini/PhotoView.aspx?name='+res[27]+'-82-62-ext_arr.jpg';
                    } else {
                        $('img_present_3').src = '/vomini/PhotoViewVc.aspx?name='+id+'-286-213-ext.jpg';
                        $('img_present_print_3').src = '/vomini/PhotoViewVc.aspx?name='+id+'-286-213-ext.jpg';
                        $('img_present_min_3').src = '/vomini/PhotoViewVc.aspx?name='+id+'-82-62-ext.jpg';
                    }
                    
                    $('Iss3').addClassName('Iss');
                } else {
                    $('Iss3').addClassName('NoIss');
                    $('img_present_3').src = '/vomini/commun/img/default.png';
                    $('img_present_print_3').src = '/vomini/commun/img/default.png';
                    $('img_present_min_3').src = '/vomini/commun/img/default_min.png';
                }
                
                if (count_img != 0) Iss.initSlide();
                
                if (!Load_Present) {
                    scrollbarpresent = new Control.ScrollBar('scrollbar_present_content','scrollbar_present_track'); 
                    Load_Present = true;
                } else {
                    scrollbarpresent.onWindowResize();
                }
            } else {
                alert('Un problème est survenu avec la requête.');
            }
        }   
     };
    xmlhttp.open('GET', url, true);
    xmlhttp.send(null);
}

function Affich_Info(type, valeur, txt_before, txt_after) {
    if (!txt_before) txt_before = '';
    if (!txt_after) txt_after = '';

    if (valeur == '') {
        if ($('tr_present_'+type+'_sep')) $('tr_present_'+type+'_sep').style.display = 'none';
        $('tr_present_'+type).style.display = 'none';
    } else {
        if ($('tr_present_'+type+'_sep')) $('tr_present_'+type+'_sep').style.display = '';
        $('tr_present_'+type).style.display = '';
        $('td_present_'+type).innerHTML = txt_before + valeur + txt_after;
    }
}


function Create_XML_Essai(info) {
    xmlhttp = new myXMLHttpRequest();
    url = '/vomini/ajax/Create_Flux.aspx?type=1&info='+ info;

    xmlhttp.onreadystatechange = function() { 
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {
                if (xmlhttp.responseText == '') {
                    Close_Pop('Pop_Essai');
                    $('td_txt_valid').innerHTML = 'Votre demande d\'essai à été envoyé avec succés !';
                    View_Pop('Pop_Valid');                
                } else {
                    Close_Pop('Pop_Essai');
                    $('td_txt_valid').innerHTML = 'Une erreur c\'est produite lors de l\'envoie de votre demande.<br>Veuillez recommencer.';
                    View_Pop('Pop_Valid');
                }
            } else {
                Close_Pop('Pop_Essai');
                $('td_txt_valid').innerHTML = 'Une erreur c\'est produite lors de l\'envoie de votre demande.<br>Veuillez recommencer.';
                View_Pop('Pop_Valid');
            }
        }   
     };
    xmlhttp.open('GET', url, true);
    xmlhttp.send(null);
}

function Create_XML_Tel(info) {
    xmlhttp = new myXMLHttpRequest();
    url = '/vomini/ajax/Create_Flux.aspx?type=2&info='+ info;

    xmlhttp.onreadystatechange = function() { 
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {
                if (xmlhttp.responseText == '') {
                    Close_Pop('Pop_Tel');
                    $('td_txt_valid').innerHTML = 'Votre demande de rappel à été envoyée avec succés !';
                    View_Pop('Pop_Valid');                
                } else {
                    Close_Pop('Pop_Tel');
                    $('td_txt_valid').innerHTML = 'Une erreur c\'est produite lors de l\'envoie de votre demande.<br>Veuillez recommencer.';
                    View_Pop('Pop_Valid');
                }
            } else {
                Close_Pop('Pop_Tel');
                $('td_txt_valid').innerHTML = 'Une erreur c\'est produite lors de l\'envoie de votre demande.<br>Veuillez recommencer.';
                View_Pop('Pop_Valid');
            }
        }   
     };
    xmlhttp.open('GET', url, true);
    xmlhttp.send(null);
}

function Create_Mail(info) {

    var type = 0;
    var qs = location.search.toQueryParams();
    if (qs.type) type = qs.type;
    
    xmlhttp = new myXMLHttpRequest();
    url = '/vomini/ajax/Create_Flux.aspx?tp='+ type +'&type=3&info='+ info;

    xmlhttp.onreadystatechange = function() { 
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {
                if (xmlhttp.responseText == '') {
                    Close_Pop('Pop_Mail');
                    $('td_txt_valid').innerHTML = 'Votre email à été envoyé avec succés !';
                    View_Pop('Pop_Valid');                
                } else {
                    Close_Pop('Pop_Mail');
                    $('td_txt_valid').innerHTML = 'Une erreur c\'est produite lors de l\'envoie de l\'email.<br>Veuillez recommencer.';
                    View_Pop('Pop_Valid');
                }
            } else {
                Close_Pop('Pop_Mail');
                $('td_txt_valid').innerHTML = 'Une erreur c\'est produite lors de l\'envoie de l\'email.<br>Veuillez recommencer.';
                View_Pop('Pop_Valid');
            }
        }   
     };
    xmlhttp.open('GET', url, true);
    xmlhttp.send(null);
}
