/****************************************************/ /** Make-model-trim AJAX requests */ function refreshChild(parNode, childNode, onSuccess, cmd) { var transaction = YAHOO.util.Connect.asyncRequest("GET", "/site/services/aeeq/scripts.spy?cmd="+cmd+"¶m=" + escape(parNode.options[parNode.selectedIndex].value), {success:refreshChild_callback_success, failure:refreshChild_callback_failure, argument:[childNode, onSuccess] }, null); } function refreshChild_callback_success(o){ try { var entries = getresponsetext_no_marker(o) var childNode = o.argument[0]; var onSuccess = o.argument.length > 1 ? o.argument[1] : null; if(childNode != null) { while(childNode.options.length > 1) childNode.options[1] = null; childNode.selectedIndex = 0; for(var i = 0; i < entries.length; i++) { childNode.options[childNode.options.length] = new Option(entries[i][0], entries[i][1]); } } if(typeof(onSuccess) == "function") { onSuccess(); } }catch(e) {} } function refreshChild_callback_failure(o) { try { var entries = getresponsetext(o) var childNode = o.argument[0]; if(childNode != null) { while(childNode.options.length > 1) childNode.options[1] = null; childNode.selectedIndex = 0; } } catch(e) {} } /** / Make-model-trim AJAX requests */ /****************************************************/ var marker = "------>"; getresponsetext = function(o) { var textIdx = o.responseText.indexOf(marker); var res = null; if(textIdx != -1) { textIdx += marker.length; r = o.responseText.substring(textIdx); r = r.replace(/\s+$/,""); eval("res = "+r); } return res; } function getresponsetext_no_marker(o) { try { r = o.responseText; r = r.replace(/\s+$/,""); eval("res = "+r); return res; } catch(e) {} } /***********************************************************/ /*** Compare related functions *****************************/ var MAX_CARS_TO_COMPARE = 5; var SCALE_WIDTH_PIXELS = 250; function is_on_compare_list(carid){ try{ var stack = document.getElementById('compare_stack'); var stack_els = stack.getElementsByTagName('DIV'); var stack_cnt = 0; for(var i = 0; i < stack_els.length; i++){ if(stack_els[i].id && stack_els[i].id == 'car2compare_'+carid){ return true; } } return false; }catch(e) { handle_exception('is_on_compare_list', e); return false; } } /** Add another vehicle to compare stack */ function add_to_compare(carid){ try { var stack = Y.get('#compare_stack'); var main_list = Y.get('#main_compare_list'); if(!stack || !main_list){return;} var stack_els = Y.all('#compare_stack div'); // stack.getElementsByTagName('DIV'); var stack_cnt = 0; //var sort_field = 'emissions'; var sort_field = 'annual_cost'; var keep_adding = true; var max_reached = false; stack_els.each(function(el){ var id = el.get('id'); if(id && id == 'car2compare_'+carid){ remove_from_compare(carid); keep_adding = false; return; } if(el.hasClass('car2compare')){ stack_cnt++; } if(stack_cnt >= MAX_CARS_TO_COMPARE){ keep_adding = false; max_reached = true; return; } }); if(max_reached){ alert('Votre sélection ne peut contenir plus de %MAX% modèles'.replace('%MAX%',MAX_CARS_TO_COMPARE)); return; } if(!keep_adding){ return; } // Hide "empty list" message var el = Y.get('#compare_stack .empty_list'); el.replaceClass('visible', 'hidden'); var new_car = Y.Node.create('
'); var car_data = cars['id'+carid]; new_car.addClass('car2compare'); new_car.set('id', 'car2compare_'+carid); new_car.set(sort_field,car_data[sort_field]); el_code = ''; el_code += '
'; el_code += '
'+[car_data.make, car_data.modelf, car_data.submodelf, car_data.year].join(' ')+'
'; el_code += '
('+format_number(car_data.annual_cost, 'fr', 0)+' $ /an)
'; el_code += "
"+ "" + "Afficher la description" + ""+ "" + "Supprimer" + ""+ "
"; el_code += '
'; var tmp_el = Y.Node.create(el_code); new_car.appendChild(tmp_el); stack.appendChild(new_car); /** **************************** */ /** Add to the main compare list */ var new_car = Y.Node.create('
'); new_car.addClass('car2compare_main_list car_info'); new_car.set('id', 'car2compare_'+carid+'_main_list'); var tmp_el = Y.Node.create(build_car_info(carid, 'compare', true)); new_car.appendChild(tmp_el); new_car.setAttribute(sort_field, car_data[sort_field]); var ins_flag = false; // If 'sort_field' value is 0, append the car to the end of the list if(parseInt(car_data[sort_field])){ main_list.get('childNodes').each(function(n){ //if(!ins_flag && parseInt(n.get(sort_field)) > parseInt(car_data[sort_field])){ if(!ins_flag && parseInt(n.getAttribute(sort_field)) > parseInt(car_data[sort_field])){ main_list.insertBefore(new_car, n); ins_flag = true; } }); } if(!ins_flag){ main_list.appendChild(new_car); } toggle_highlight(carid, true); toggle_add_links(carid, 'on'); // Update srchparams Array srchparams.compare_list.push(carid); return true; }catch(e) { handle_exception('add_to_compare', e); return false; } } /** The function was switched to show ranking based on "rang centile". */ function get_scale_percentile(category, value){ try{ var ARROW_WIDTH = 7; // The width of arrow image var min_val = 0; var max_val = 10000; var step = (max_val - min_val)/SCALE_WIDTH_PIXELS; var offset = parseInt((value - min_val)/step); // Make sure offset is in the right range if(offset < 0){ offset = 0; } if(offset > (SCALE_WIDTH_PIXELS - ARROW_WIDTH)){ offset = SCALE_WIDTH_PIXELS - ARROW_WIDTH; } var scale_title = category == 'general' ? "Classement général" : "Classement dans sa catégorie"; var res = '
'+ scale_title+ '
'+ '
 
' + '
'+ 'moins polluant'+ 'plus polluant'+ '

'; return res; }catch(e) { handle_exception('get_scale', e); return false; } } function get_scale(category, transmission, value){ try{ if(!cons_ranges[category]){ return ''; } var ARROW_WIDTH = 7; // The width of arrow image var min_val = cons_ranges[category][transmission]['min']; var max_val = cons_ranges[category][transmission]['max']; var step = (max_val - min_val)/SCALE_WIDTH_PIXELS; var offset = parseInt((value - min_val)/step); // Make sure offset is in the right range if(offset < 0){ offset = 0; } if(offset > (SCALE_WIDTH_PIXELS - ARROW_WIDTH)){ offset = SCALE_WIDTH_PIXELS - ARROW_WIDTH; } var scale_title = category == 'general' ? "Classement général" : "Classement dans sa catégorie"; var res = '
'+ scale_title+ '
'+ '
 
' + '
'+ 'moins polluant'+ 'plus polluant'+ '

'; return res; }catch(e) { handle_exception('get_scale', e); return false; } } /** Remove vehicle from compare stack */ function remove_from_compare(carid){ try { var stack = Y.get('#compare_stack'); var main_list = Y.get('#main_compare_list'); if(!stack || !main_list){return;} var el = Y.get('#car2compare_'+carid); if(!el){return;} stack.removeChild(el); var el = Y.get('#car2compare_'+carid+'_main_list'); if(!el){return;} main_list.removeChild(el); if(is_compare_list_empty()){ // Show "empty list" message var el = Y.get('#compare_stack .empty_list'); el.replaceClass('hidden', 'visible'); } // IE6 hack redraw(stack); toggle_highlight(carid, false); toggle_add_links(carid, 'off'); // Update srchparams Array var tmp_list = srchparams.compare_list; srchparams.compare_list = new Array(); for (var i in tmp_list){ if(tmp_list[i] != carid){ srchparams.compare_list.push(tmp_list[i]); } } } catch(e) { handle_exception('remove_from_compare', e); return false; } } function is_compare_list_empty(){ try{ var stack_el = Y.get('#compare_stack'); var is_empty = true; stack_el.get('childNodes').each(function(n){ if(n.hasClass('car2compare')){ is_empty = false; } }); return is_empty; }catch(e){ handle_exception('is_compare_list_empty', e); return false; } } /** (Un)Highlight the car on the lists */ function toggle_highlight(carid, value){ try { for(i in tabs){ var car_on_list = document.getElementById('car_list_'+tabs[i]+'_'+carid); if(car_on_list){ if(value){ car_on_list.className += " highlight"; }else{ car_on_list.className = car_on_list.className.replace(/ highlight/gi, ''); } } } }catch(e) { handle_exception('toggle_highlight', e); return false; } } /** @param int carid @param str val 'on'/'off' */ function toggle_add_links(carid, val){ try{ var display = null; var links = Y.all('a'); var class_on = 'car_info_toggl_comp_on'; var class_off = 'car_info_toggl_comp_off'; links.each(function(link) { if(!link.hasClass('car_info_toggl_comp_'+carid)){ return; } if(val == 'on'){ link.replaceClass(class_on, class_off); }else{ link.replaceClass(class_off, class_on); } //redraw(link); }); return true; }catch(e) { handle_exception('toggle_add_links', e); return false; } } // hide and then show again the node // to make the browser to re-draw it function redraw(node){ try{ old_display = node.getStyle('display'); node.setStyle('display', 'none'); node.setStyle('display', old_display); return true; }catch(e) { handle_exception('redraw', e); return false; } } function build_compare_link(carid){ try{ var link_code = " "+ "Ajouter à votre sélection" + "Retirer de votre sélection" + " "; return link_code; }catch(e){ return ''; } } /***********************************************************/ function switch_tab(tab_name){ try { var tab_els = Y.all('.frame'); tab_els.each(function(tab_content){ var tab_suffix = tab_content.get('id').replace(/frame_f_/gi, ''); var display = tab_name == tab_suffix ? 'block': 'none'; var old_class = tab_name == tab_suffix ? 'tab_off': 'tab_on'; var new_class = tab_name == tab_suffix ? 'tab_on': 'tab_off'; var tab_header = Y.get('#frame_h_' + tab_suffix); //var tab_content = Y.get('#frame_f_' + tab_suffix); var tab_search_params = Y.get('#frame_sp_' + tab_suffix); if(!tab_content || !tab_header){return;} tab_header.setStyle('display', display); tab_content.setStyle('display', display); if(tab_search_params) tab_search_params.setStyle('display', display); var tab_el = Y.get('#tab_' + tab_suffix); if(tab_el){ tab_el.replaceClass(old_class, new_class); } }) var tabs = Y.get('#tabs'); var display = 'none'; if(tab_name == 'cost' || tab_name == 'co2' || tab_name == 'cons'){ var display = 'block'; } tabs.setStyle('display', display); // Hide/show "Comparer ces modeles" link display_compare = 'compare' == tab_name ? 'none' : 'block'; display_modify = 'compare' != tab_name ? 'none' : 'block'; Y.get('#comparison_block_comrare_link a#show_compare_list').setStyle('display', display_compare); Y.get('#comparison_block_comrare_link a#modify_compare_list').setStyle('display', display_modify); // Update print link update_print_link(); // Update srchparams Array srchparams.tab = tab_name; // Google Analytics stuff var pageTracker = _gat._getTracker("UA-5549663-5"); pageTracker._trackPageview('/show_cars/'+tab_name); } catch(e) { handle_exception('switch_tab', e); return false; } } /** Show the page number "page" under tab named "tab" @param string tab ('cost'/'cons'/'co2') @param int page */ function show_page(tab, page){ try { var open_id = 'cars_outer_list_'+tab+'_'+page; var el_open = Y.get('#'+open_id); if(!el_open){ if('list_mode' == tab){ el_open = build_list_mode_page(open_id); }else{ el_open = build_page(open_id); } } if(el_open){ var ancNode = el_open.get('parentNode'); //.parentNode; var pages = ancNode.get('childNodes'); pages.each( function(page){ if(page.hasClass('tab_page')){ page.setStyle('display', 'none'); } }); el_open.setStyle('display', 'block'); } show_paginator(tab, page); // Update print link update_print_link(); // Update srchparams Array srchparams[tab + 'page'] = page; }catch(e) { handle_exception('show_page', e); return false; } } /** Update the "other" input, accordingly to the one, that was modified. Make sure the values are legal and the sum is 100. @param str ratio - 'city'/'hwy' - the one that is being modified */ function update_ratio(ratio){ try { var el_city = document.getElementById('city_ratio'); var el_hwy = document.getElementById('hwy_ratio'); var value = ratio == 'city' ? el_city.value : el_hwy.value; value = parseInt(value); if(!(value >= 0 && value <= 100)){ //alert('illegal ratio'); if(ratio == 'hwy'){ update_ratio('city', el_city.value); }else{ update_ratio('hwy', el_hwy.value); } return; } if(ratio == 'hwy'){ el_city.value = 100 - value; }else{ el_hwy.value = 100 - value; } } catch(e) { handle_exception('update_ratio', e); return false; } } function validate_prices(pr){ try { var el_from = document.getElementById('price_from'); var el_to = document.getElementById('price_to'); var val_from = parseInt(el_from.options[el_from.selectedIndex].value); var val_to = parseInt(el_to.options[el_to.selectedIndex].value); if(val_from >= val_to){ if(pr == 'price_from'){ el_to.selectedIndex = el_from.options.length - 1 > el_from.selectedIndex ? el_from.selectedIndex + 1 : el_from.selectedIndex; }else{ el_from.selectedIndex = el_to.selectedIndex > 0 ? el_to.selectedIndex - 1 : el_to.selectedIndex; } return; } } catch(e) { handle_exception('validate_prices', e); return false; } } /***********************************************************/ function prompt_survey(){ try{ if(_prompt_survey){ _prompt_survey = false; // Display this only once turn_off_survey_prompt(); //location.replace('?cmd='); //location.assign('?cmd='); location.assign('survey.spy'); return "prompt survey"; } }catch(e) { handle_exception('prompt_survey', e); return false; } } /** Disable survey prompt for all clickable elements on the page */ function disable_prompt_survey(){ try{ var els = Y.all('a'); var new_onclick = "_prompt_survey=false;" els.each(function(n){ var href = n.get('href'); // If there is a href, it must be some external link. // Unless it is the link to the survey if(href.indexOf('p://') != -1 && href.indexOf('survey.spy') == -1 ){ return; } plant_onclick(n, new_onclick) }); }catch(e) { handle_exception('disable_prompt_survey', e); return false; } } function turn_off_survey_prompt(){ try{ var DO_NOT_BUG_FOR = 365; // days setCookie('survey_done', true, DO_NOT_BUG_FOR); }catch(e) { handle_exception('turn_off_survey_prompt', e); return false; } } /** Make all the expernal links open a new window/tab */ function set_links_target(){ try{ var els = Y.all('a'); els.each(function(n){ if(n.hasClass('external')){ n.set('target', '_blank'); } }); }catch(e) { handle_exception('set_links_target', e); return false; } } function plant_onclick(el, script){ try{ if(el.hasClass('ban_survey')){ stop='STOP'; } // Keep the old onclick value (if applicable) //var old_onclick = el.get('onclick'); // Generate the new onclick value var new_onclick = function(){eval(script)}; if(typeof(old_onclick) == 'function'){ //el.set('onclick', function(){if(old_onclick){old_onclick();} new_onclick(); }); el.on('click', function(){if(old_onclick){old_onclick();} new_onclick(); }); }else{ el.on('click', new_onclick); } return; }catch(e) { //handle_exception('plant_onclick', e); return false; } } /***********************************************************/ /** Change 'display' value @param string/object @param string ('none'/'block'/etc.) */ function toggle_display(el_id){ try{ var n = Y.get('#'+el_id); if(n){ n.toggleClass('hidden'); } return false; }catch(e) { handle_exception('toggle_display', e); return false; } } /** Format number accordingly to language. En: 20,123.99 Fr: 20 123,99 @param string/numeric amount @param string lang 'en'/'fr' */ function format_number(amount, lang, decimalPlaces){ try{ if(typeof(amount) == 'string'){ amount = amount.replace(/ /gi, ''); if('fr' == lang){ // && typeof(amount) == 'string' // 1,038 -> 1.038 amount = amount.replace(/,/gi, '.'); }else{ // 20,000 -> 20000 amount = amount.replace(/,/gi, ''); } } var num = Y.DataType.Number.parse(amount); if(typeof(decimalPlaces) != 'number'){ decimalPlaces = 0; if(num%1){ decimalPlaces = 2; } } if('fr' == lang){ num = Y.DataType.Number.format(num, {decimalPlaces:decimalPlaces, decimalSeparator: ',',thousandsSeparator:' ' }); }else{ num = Y.DataType.Number.format(num, {decimalPlaces:decimalPlaces, decimalSeparator: '.',thousandsSeparator:',' }); } return num; }catch(e) { handle_exception('format_number ('+amount+')', e); return amount; } } /** Format currency accordingly to the language En: $999 Fr: 999 $ @param string/numeric amount @param string lang 'en'/'fr' */ function format_currency(amount, lang, decimalPlaces){ try { var num = format_number(amount, lang, decimalPlaces); if('fr' == lang){ num = num + ' $'; }else{ num = '$' + num; } return num; }catch(e) { handle_exception('format_currency', e); return amount; } } /* function format_thousands(amount, lang){ try{ var delimiter = lang == 'fr' ? ' ' : ','; var a = String(amount).split('.',2) var d = a[1]; d = parseInt(d); d = isNaN(d) ? '' : String(d).substring(0,2); var i = parseInt(a[0]); if(isNaN(i)) { return ''; } var minus = i < 0 ? '-' : ''; i = Math.abs(i); var n = new String(i); var a = []; while(n.length > 3){ var nn = n.substr(n.length-3); a.unshift(nn); n = n.substr(0,n.length-3); } if(n.length > 0) { a.unshift(n); } n = a.join(delimiter); amount = d.length < 1 ? n : (n + '.' + d); amount = minus + amount; return amount; }catch(e) { return amount; } } format_thousands = format_number; */ function average(){ try{ var items = average.arguments.length; if(!items) return 0; var sum = 0 for (i = 0; i < items;i++){ sum += average.arguments[i] } return (sum/items) }catch(e){ return 0; } } /** This function will gather the currently displayed cars and update the "Print" link accordingly */ function update_print_link(){ try{ // Find visible frame var frames = Y.all('div.frame'); var vis_frame = null; frames.each(function(n){ if(n.getStyle('display') != 'none'){ vis_frame = n; } }); if(!vis_frame){ return; } var frame_id = vis_frame.get('id'); var tab = frame_id.replace('frame_f_', ''); if('compare' != tab){ var vis_page; var pages = Y.all('#'+frame_id+' div.tab_page'); pages.each(function(n){ if(n.getStyle('display') != 'none'){ vis_page = n; } }); if(!vis_page){ return; } var page_id = vis_page.get('id'); var page_ind = page_id.replace('cars_outer_list_'+tab+'_', ''); }else{ var page_id = frame_id; var page_ind = 0; } var cars_els = Y.all('#'+page_id+' div.car'); var carids = new Array(); var carids_proposed = new Array(); var carids_exact = new Array(); cars_els.each(function(n){ var carid = n.get('id').replace('car_list_'+tab+'_',''); if(carid){ carids.push(carid); } var cl = n.getAttribute('class'); if(cl){ if(cl.indexOf('proposed') != -1){ carids_proposed.push(carid); } if(cl.indexOf('exact') != -1){ carids_exact.push(carid); } } }); //alert(carids.join(',')); //alert(carids_proposed.join(',')); //alert(carids_exact.join(',')); var transmission = 'best'; if(request.transmissions && request.transmissions.length == 1){ transmission = request.transmissions[0]; } //alert (transmission); var url = 'printable.spy?tab='+tab+ '&carids_proposed='+carids_proposed.join(',')+ '&carids_exact='+carids_exact.join(',')+ '&carids='+carids.join(',')+ '&rng='+page_ind+'|'+cars.length+ '&city_ratio='+city_ratio+ '&yearly_mileage='+yearly_mileage+ '&duration='+duration+ '&transmission='+transmission; //'&fuel_price_gas=1.12&fuel_price_diesel=1.12'; var print_links = Y.all('a.print_link'); print_links.each(function(n){ n.setAttribute('href', url); }); }catch(e) { handle_exception('update_print_link', e); return false; } } function handle_exception(fn, e){ try{ return; }catch(e) {} } function setCookie(name, val, days) { if (days) { var date = new Date(); date.setTime(date.getTime() + (days*24*60*60*1000)); // days to ms var expires = "expires="+date.toGMTString(); }else{ expires = ""; } document.cookie = name + "=" + val + "; " + expires + "; path=/"; } function getCookie(name) { var full_name = name + "="; var cks = document.cookie.split(';'); for(var i = 0; i < cks.length; i++) { var c = cks[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(full_name) == 0){ return c.substring(full_name.length, c.length); } } return null; } /** The function will build the GET link, leading to the current page in its current state (current tab, current page etc.) It will use the global "srchparams" Array (must exist!) @param string addr_link - this is the address link (e.g. English or French domain name - osez-un-vehicule-plus-...com) */ function build_get_link(addr_link){ try{ var url = ''; var params = new Array(); for(var i in srchparams){ if(i){ params.push(i + '=' + srchparams[i]); } } url = params.join('&'); url = addr_link + '?' + url; return url; }catch(e) {} } /** The function will try to restore the state of a page in case if 'tab', 'page' etc. parameters were passed like in case of language switching. */ function restore_state(){ try{ if(srchparams.tab){ switch_tab(srchparams.tab); if(srchparams[srchparams.tab + 'page']){ show_page(srchparams.tab, srchparams[srchparams.tab + 'page']); } } // Restore compare list (if applicable) // Save the list to be restored. var tmp_list = srchparams['compare_list']; // Empty the srchparams.compare_list srchparams.compare_list = new Array(); for(var i in tmp_list){ var add_flag = true; // Do not over-fill the list if(srchparams.compare_list.length >= MAX_CARS_TO_COMPARE){ break; } // Do not try to add carids that already on the list // Otherwise they will be removed instead for(var j in srchparams.compare_list){ if(srchparams.compare_list[j] == tmp_list[i]){ add_flag = false; } } if(add_flag){ add_to_compare(tmp_list[i]); } } }catch(e) {} }