/*---- Init Javascript ------*/ var dealerCode = '270c16c500d02cccc6c75a4f27347a43'; var vin = '{vin}'; var currencyFormat = function (num, decimals) { var numString = num.toString().replace(/,/g, ''); return "$" + parseFloat(numString).toFixed(decimals).replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,"); }; var populate = function (results) { if (results.lease_payment) { updateVal('ds_lease_payment', currencyFormat(results.lease_payment)); updateVal('ds_lease_das', currencyFormat(results.lease_das)); updateVal('ds_lease_down', currencyFormat(results.lease_down)); updateVal('ds_lease_down_text', results.lease_down_text); updateVal('ds_lease_down_or_das', currencyFormat(results.lease_down_or_das)); updateModal('ds_lease_link', results.lease_frameurl, results.vin); } if (results.finance_lowapr) { updateVal('ds_finance_lowapr', (results.finance_lowapr)); updateVal('ds_finance_lowapr_max_term', (results.finance_lowapr_max_term)); updateModal('ds_finance_lowapr_link', results.finance_lowapr_frameurl, results.vin); } else { jQuery(".ds_vin_" + results.vin).find(".dsp-banner-footer").hide(); } if (results.finance_payment) { updateVal('ds_finance_payment', currencyFormat(results.finance_payment)); updateVal('ds_finance_das', currencyFormat(results.finance_das)); updateVal('ds_finance_down', currencyFormat(results.finance_down)); updateVal('ds_finance_down_text', results.finance_down_text); updateVal('ds_finance_down_or_das', currencyFormat(results.finance_down_or_das)); updateModal('ds_finance_link', results.finance_frameurl, results.vin); } }; var updateVal = function (key, value) { var match = jQuery('.' + key).html(value); match.removeClass(key); }; var updateModal = function (key, frame_url, vin) { var tag = jQuery("a." + key); var newClass = key + vin; tag.removeClass(key).addClass(newClass).attr('href', frame_url); jQuery("a." + newClass).fancybox({ 'transitionIn': 'none', 'transitionOut': 'none', 'type': 'iframe', 'href': frame_url }); }; var GetURLParameter = function (sParam) { var sPageURL = window.location.search.substring(1); var sURLVariables = sPageURL.split('&'); for (var i = 0; i < sURLVariables.length; i++) { var sParameterName = sURLVariables[i].split('='); if (sParameterName[0] == sParam) { return sParameterName[1]; } } }; var setSigninCookie = function () { if (document.cookie.search('vrp-form=visited') < 0) { jQuery.getScript("https://cdnjs.cloudflare.com/ajax/libs/js-cookie/2.1.4/js.cookie.min.js", function (data, textStatus, jqxhr) { Cookies.set('vrp-form', 'visited', { expires: 7, path: '' }); }); } }; var requireSigninIfDesired = function (results, signed_in, dealerCode, vin) { var sign_in_required = parseInt(results.sign_in_required); if (sign_in_required && !signed_in) { /* * checks to see if sign in is required and then signs the user in if sign_in_user is set. * */ var sign_in_user = GetURLParameter('sign_in_user'); if (sign_in_user) { setSigninCookie(); signed_in = true; } else if (!results.payment_is_a_special || !results.show_specials_without_signing_in) { /* * if sign in required and the user is not signed in, add code to alert the user to sign in instead of payments * */ var sign_in_url = ('https://www.dealerscience.com/iframe.php?target=signin' + '&dealerCode=' + dealerCode + '&vin=' + vin + '&reloadtopframe=' + encodeURIComponent(window.top.location.href.replace(window.top.location.hash, ""))); var lease_sign_in_url = (sign_in_url + '&monthly=' + results.lease_payment + '&down=' + results.lease_down_or_das + '&url=' + encodeURIComponent(results.lease_url)); var finance_sign_in_url = (sign_in_url + '&monthly=' + results.finance_payment + '&down=' + results.finance_down_or_das + '&url=' + encodeURIComponent(results.finance_url)); results.finance_payment = null; results.lease_payment = null; results.finance_lowapr = null; jQuery('.das').html(""); jQuery('.payment').html("

Sign in to
View Payments

"); updateModal('ds_lease_link', lease_sign_in_url, results.vin); updateModal('ds_finance_link', finance_sign_in_url, results.vin); } } return results; }; var populateSRP = function (lease_template, finance_template, signed_in) { /*========================================================================================================== // if this page has no finance blocks then lets add new ones. If it does then it doesn't need any more. The // finance block is added after the vehicle price section. //========================================================================================================== */ if (jQuery(".price-block.finance").length == 0) { jQuery(".vehicle-price").each(function () { var url = jQuery(this).find('a').attr('href'); jQuery("
").appendTo(this); }); } /*============================================================================================================ // dealerScienced is a class added to let us know we already built a block for this vehicle, otherwise when // the page is turned and then returned to it would generate duplicate blocks resulting in duplicate buttons. // this way we know that if the block contains a class dealerscienced then it can be ignored. //============================================================================================================ */ jQuery(".price-block.finance:not(.dealerScienced)").each(function () { var thisBlock = this; jQuery(thisBlock).addClass('dealerScienced'); var url = jQuery(thisBlock).children('a').attr('href'); var vinAttr = jQuery(thisBlock).children('a').attr('href'); var vinMatch = null; if (vinAttr) { vinMatch = vinAttr.match(/-([a-zA-Z0-9]{17})/i); } if (!vinMatch) { vinAttr = jQuery(thisBlock).parents('.vehicle-overview').attr('id'); if (vinAttr) { vinMatch = vinAttr.match(/([a-zA-Z0-9]{17})/i); } } /*======================================================================================================== // We get a vin number from the parent container, we need the vin number to query the webservice and // retrieve the appropriate pricing data. As of this writing include paymenttype = 2 to return both // finance and lease data, if it is omitted then you only get lease data. // // Once we get a reply from the api we create a static button with no data, place it in the section for // for the current vehicle, then we populate the empty section with the specific details. //======================================================================================================== */ if (vinMatch && vinMatch.length) { var vin = vinMatch[1]; var apiurl = 'https://www.dealerscience.com/webservices/script/?v=0.2&key=FJA12D159&details&paymenttype=2&dealerCode=' + dealerCode + '&vin=' + vin; var templates = ""; jQuery.getJSON(apiurl, function (results) { jQuery(thisBlock).parents('.price-leaseandfinance').removeClass('price-leaseandfinance'); if (results.show_lease) { templates += lease_template; } if (results.show_finance) { templates += finance_template; } jQuery(thisBlock).html(templates); jQuery(thisBlock).addClass('ds_vin_' + vin.toUpperCase()); var resultsAfterSignin = requireSigninIfDesired(results, signed_in, dealerCode, vin); populate(resultsAfterSignin); /* some dealers may require specific actions to the page. The scripts themselves would be defined in the // dealer specific template, so if it is defined in the demplate then execute it here */ if (typeof dealerSpecificLeaseAction === 'function') { dealerSpecificLeaseAction(resultsAfterSignin.vin); } if (typeof dealerSpecificFinanceAction === 'function') { dealerSpecificFinanceAction(resultsAfterSignin.vin); } }); } }); }; jQuery(document).ready(function () { var apiurl = 'https://www.dealerscience.com/webservices/ds/jstemplate/?v=0.2&key=FJA12D159&dealerCode=' + dealerCode + '&category=script&subcategory[]=lease_finance_header&subcategory[]=lease&subcategory[]=finance'; jQuery.getJSON(apiurl, function (results) { var lease_template = results['lease']; var finance_template = results['finance']; var header_template = results['lease_finance_header']; var signed_in = (document.cookie.search('vrp-form=visited') >= 0 ? true : false); populateSRP(lease_template, finance_template, signed_in); jQuery("body").live("vrp-ready vrp-ajax-complete", function () { populateSRP(lease_template, finance_template, signed_in); }); jQuery(header_template).appendTo("head"); }); });