Skip to content

Instantly share code, notes, and snippets.

@LarryBarker
Created January 4, 2019 14:30
Show Gist options
  • Save LarryBarker/b8f93e8034c7605fbd0e9c72f316577c to your computer and use it in GitHub Desktop.
Save LarryBarker/b8f93e8034c7605fbd0e9c72f316577c to your computer and use it in GitHub Desktop.
Script used to process VIN numbers on NHTSA
$(document.vhr).submit(function (e) {
$vin = $(this).find('input[name="decodeVIN"]');
if (!$vin.val() || $vin.val().length <= 10) {
$('.digits-error').show();
$('.triangle-border').hide();
return false;
}
else {
e.preventDefault();
$('.miniBox ul:eq( 0 )').after('<div class="loader"></div>');
$('.digits-error').hide();
$('.triangle-border').hide();
var $vinResponse = $('#vinResponse'),
url = 'https://vpic.nhtsa.dot.gov/api/vehicles/decodevinextended/' + $vin.val() + '?format=json',
v = 0; // count how many non-empty values
$vinResponse.hide();
$.get(url, function(data) {
// success
if (data && data.Message.indexOf('successfully') >= 0 && data.Count > 0) {
$vinResponse.empty();
var year, make, model, trim, body_style, fuel_type, type, // Krux vars
blacklist = [143, // error
97, // ncsa make
96, // ncsa body
98, // ncsa model
157]; // manufacturer id
for(var i=0; i<data.Results.length; i++) {
if (blacklist.indexOf(data.Results[i].VariableId)>-1) {
continue; // skip blacklisted ids
}
if (data.Results[i].Value) {
$vinResponse.append('<p><strong>' + data.Results[i].Variable + ':</strong> ' + data.Results[i].Value +'</p>');
v++;
// assign Krux vars
if (data.Results[i].VariableId === 29) {
year = data.Results[i].Value;
}
if (data.Results[i].VariableId === 26) {
make = data.Results[i].Value;
}
if (data.Results[i].VariableId === 28) {
model = data.Results[i].Value;
}
if (data.Results[i].VariableId === 38) {
trim = data.Results[i].Value;
}
if (data.Results[i].VariableId === 5) {
body_style = data.Results[i].Value; // BODY CLASS
}
if (data.Results[i].VariableId === 24) {
fuel_type = data.Results[i].Value;
}
if (data.Results[i].VariableId === 39) {
type = data.Results[i].Value;
}
}
}
// Krux event
window.Krux||((Krux=function(){Krux.q.push(arguments);}).q=[]);
Krux('ns:dmv','admEvent', 'Lcu4-9HU', {
vehicle_year: year,
vehicle_make: make,
vehicle_model: model,
vehicle_trim: trim,
vehicle_body_style: body_style,
vehicle_fuel_type: fuel_type,
vehicle_type: type
});
if (v === 0) {
$vinResponse.html('<p class="errorResponse">We couldn\'t decode your VIN Number. Please confirm the number and try again.</p>');
$('#needMore').hide();
$('#ctaLogo').hide();
}
else {
$('#needMore').show();
$('#ctaLogo').show();
$("#VHRVIN").val($vin.val());
ac(this,'misc_prod_vin_7042');
window.ic('misc_prod_vin_7042');
window.ic('7042');
$("#CFForm button").click(function() {
ac(this, '7042');
});
$('#moreHeading').html('More information for ' + $vin.val() +' is available<br> on a CARFAX Report.' );
}
} else {
$vinResponse.html('<p><strong>Query Failed.</strong></p>');
}
}).fail(function() {
$vinResponse.html('<p><strong>Query Failed.</strong></p>');
});
setTimeout(function(){
$('.ExpandedForm').removeClass('shrinkForm');
$('.loader').addClass('shrinkForm');
$vinResponse.show();
}, 3000);
}
});
$( "#clickable" ).click(function() {
$( ".triangle-border" ).toggle();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment