// MAXSEPublishing & Default Basic Configuration
var MAXSEConfiguration = MAXSEConfiguration || {
VendorID: "website",
CustomCssClass: "maxSePdfLink customCssClass",
ElementImage: "http://maxinsights.biz/images/btn_GoodDeal.png",
ElementText: "Is this a good deal? CLICK TO FIND OUT",
OutputType: "image", // ["link", "button", "image", "binding"]
ShowNoPdfMessage: false,
PlaceholderCssClass: "MaxSeLinkPlaceholder",
UseIFrame: true
};
MAXSEPublishing = function(){};
MAXSEPublishing.prototype = {
Placeholders: null,
ErrorPrefix: "MAX for Selling & Email error: ",
Errors: {
config: "MAXSEConfiguration object not found.",
service: "We are unable to access the consumer value analysis report for this vehicle. Please contact the dealer directly for more information.",
vinElementId: "Required property 'VinElementId' not found.",
vinElement: "VIN element not found.",
vin: "VIN not found"
},
DestinationDomain: "https://maxdigitalshowroom.com",
DestinationDomainRegex: /^https:\/\/maxdigitalshowroom\.com/,
DestinationPath: "/?gooddeal=web/",
Loaded: false,
Interval: 250,
MaximumElapsed: 60*1000*0.5, /* half a minute of polling */
InstallationAttemptElapsedTime: 0,
/// Adds event handler to window onload event
AddLoadEvent: function ( func ) {
var oldOnLoad = window.onload;
if (window.addEventListener) {
window.addEventListener('load', func, false);
} else if (window.attachEvent) {
window.attachEvent('onload', func, false);
} else if (typeof window.onload !== 'function') {
window.onload = func;
} else {
window.onload = function() {
if ( oldOnLoad ) {
oldOnLoad();
}
func();
};
}
},
///
FindPlaceholders: function( options ) {
// placeholders for each of which VIN-related content (a link or image) will be inserted as child HTML
this.Placeholders = document.getElementsByName( options.PlaceholderCssClass );
},
FindCobaltSrpPlaceholders: function ( options ){
var self = this;
var attribute = "data-href";
var matchingElements = [];
var allElements = document.getElementsByTagName('li');
for (var i = 0, n = allElements.length; i < n; i++)
{
var thisAttribute = allElements[i].getAttribute(attribute);
if (thisAttribute !== null && self.DestinationDomainRegex.test(thisAttribute))
{
matchingElements.push(allElements[i]);
}
}
this.Placeholders = matchingElements;
},
FindCobaltVdpPlaceholders: function ( options ){
var self = this;
var attribute = "href";
var matchingElements = [];
var allElements = document.getElementsByTagName('a');
for (var i = 0, n = allElements.length; i < n; i++)
{
var thisAttribute = allElements[i].getAttribute(attribute);
if (thisAttribute !== null && self.DestinationDomainRegex.test(thisAttribute))
{
matchingElements.push(allElements[i]);
}
}
this.Placeholders = matchingElements;
},
///
GetPlaceholdersForURL: function( url ) {
// find the placeholder for which the given URL is intended
var url_upper, i, vin_upper, found;
var matchingPlaceholders = new Array();
url_upper = url.toUpperCase();
for( i = 0; i < this.Placeholders.length; ++i ) {
vin_upper = this.Placeholders[i].attributes["data-vin"].value.toUpperCase();
if( url_upper.search( vin_upper ) != -1 ) // case-insensitive search
{
//return this.Placeholders[i];
found = true;
matchingPlaceholders.push(this.Placeholders[i]);
}
}
if (found)
return matchingPlaceholders;
else return null;
},
InstallAnchorIframes: function(self, options) {
self.FindCobaltVdpPlaceholders(options);
var installedAnchorEvents = false;
for( i = 0; i < self.Placeholders.length; i++ ) {
placeholder = self.Placeholders[i];
var destination = placeholder.getAttribute("href");
if (self.DestinationDomainRegex.test(destination)) {
placeholder.removeAttribute("href");
placeholder.setAttribute("data-href", destination);
var new_element = placeholder.cloneNode(true);
placeholder.parentNode.replaceChild(new_element, placeholder);
var fn = function(dst){
return function(event) {
MAXSEPublishing.prototype.ClickHandler(dst);
event.stopPropagation();
return false;
};
}(destination);
new_element.onclick = fn;
new_element.style.cursor = "pointer";
installedAnchorEvents = true; // we installed this one but there still may be more, let's not break out of the loop just yet
} else {
// nothing found but it may be added to the page by a script in a moment
}
}
return installedAnchorEvents;
},
/// Outputs markup for link/button on window load
OnReady: function(e) {
var self = this;
if (!window.jQuery) {
if (this.Loaded) return;
this.Loaded = true;
}
var options, placeholder, request, i;
// Make sure we have configuration object
try {
options = MAXSEConfiguration;
} catch(error) {
throw new Error(this.ErrorPrefix + this.Errors.config);
}
function replacePlaceholders() {
self.FindPlaceholders(options);
if (self.Placeholders.length > 0) {
for( i = 0; i < self.Placeholders.length; i++ ) {
placeholder = self.Placeholders[i];
request = self.DestinationDomain + self.DestinationPath + placeholder.attributes["data-vin"].value;
if (placeholder.getAttribute('data-bu')) request += '/' + placeholder.attributes["data-bu"].value;
self.WritePdfLink(request);
}
} else {
// Didn't find anything, let us see if we can match cobalt's SRP pattern
//
self.FindCobaltSrpPlaceholders(options);
if (self.Placeholders.length > 0) {
for( i = 0; i < self.Placeholders.length; i++ ) {
placeholder = self.Placeholders[i];
var destination = placeholder.getAttribute("data-href");
if (self.DestinationDomainRegex.test(destination)) {
// is this our URL?
placeholder.removeAttribute("data-href");
var new_element = placeholder.cloneNode(true);
placeholder.parentNode.replaceChild(new_element, placeholder);
var fn = function(dst){
return function(event) {
MAXSEPublishing.prototype.ClickHandler(dst);
event.stopPropagation();
return false;
};
}(destination);
new_element.onclick = fn;
new_element.style.cursor = "pointer";
} else {
// no
}
}
} else {
// Didn't find SRP pattern, check for a href pattern (can be any anchor with our domain)
//
self.ClickEventInstallerHandle = setInterval(function(){
self.InstallationAttemptElapsedTime += self.Interval;
if (self.InstallAnchorIframes(self, options) || self.InstallationAttemptElapsedTime >= self.MaximumElapsed) {
clearInterval( self.ClickEventInstallerHandle );
}
},self.Interval);
}
}
}
if (!options.BUCode) {
// if no bucode specified, display links
replacePlaceholders();
} else {
this.ShowroomEnabled(options.BUCode, replacePlaceholers);
}
},
///
WriteError: function() {
if( !this.ErrorHasRun && MAXSEConfiguration.ShowNoPdfMessage !== false ) {
document.getElementById("MaxSeLinkPlaceholder").innerHTML = this.Errors.service;
this.ErrorHasRun = true;
}
},
///
WritePdfLink: function( url ) {
var linkHtml, options, placeholders;
placeholders = this.GetPlaceholdersForURL( url );
if( !placeholders )
return; // no placeholder could be found
// Make sure we have configuration object
try {
options = MAXSEConfiguration;
} catch(error) {
throw new Error(this.ErrorPrefix + this.Errors.config);
}
// If PDF URL is not found return empty string
if (!url || url === "") { return ""; }
for (i = 0; i < placeholders.length; ++i)
{
placeholder = placeholders[i];
// Assign relevant markup
switch( options.OutputType ) {
case "button":
placeholder.innerHTML = "";
break;
case "image":
placeholder.href = url;
placeholder.className = options.CustomCssClass;
placeholder.onclick = function() { return MAXSEPublishing.prototype.ClickHandler(url); }
placeholder.target = "_blank";
placeholder.innerHTML = "";
break;
case "link":
placeholder.href = url;
placeholder.className = options.CustomCssClass;
placeholder.target = "_blank";
placeholder.onclick = function() { return MAXSEPublishing.prototype.ClickHandler(url); }
placeholder.innerHTML = options.ElementText;
break;
case "binding":
placeholder.onclick = function() { return MAXSEPublishing.prototype.ClickHandler(url); }
break;
}
}
},
ClickHandler: function(url) {
if (window.innerWidth <= 1040 || !MAXSEConfiguration.UseIFrame) {
window.open(url);
} else {
this.OpenIFrame(url);
}
return false;
},
ShowroomEnabled: function(bucode, callback) {
if (!bucode) return false;
this.LoadDealer(bucode, function(xhr) {
var resp = JSON.parse(xhr.responseText);
if (!resp.hits || !resp.hits.found || resp.hits.found <= 0) {
callback(); // if 404 or error, call callback anyway
return false;
}
if (resp.hits.hit[0].fields.maxforwebsite2_enabled == '1') {
callback();
return true;
} else {
// bypass callback only if maxforwebsite2_enabled != '1'
return false;
}
});
},
LoadDealer: function(bucode, callback) {
var xhr;
var url = 'http://api.maxdigitalshowroom.com/dealer-search?q.parser=lucene&q=businessunitcode%3A%22' + bucode + '%22';
// http://api.beta.maxdigitalshowroom.com/dealer-search?
// q.parser=lucene&q=ownerhandle%3A%2241FB6750-7248-E211-AAA7-0022198DC5E2%22+and+showroom_enabled%3A%221%22&sort=businessunitname+asc
var isXDomainRequest = typeof XDomainRequest != 'undefined';
if (isXDomainRequest) {
xhr = new XDomainRequest();
} else {
xhr = new XMLHttpRequest();
}
function ensureReadiness() {
if (xhr.readyState < 4) return;
if (xhr.status !== 200) return;
if (xhr.readyState === 4) {
callback(xhr);
}
}
if (isXDomainRequest) {
xhr.onload = function( ){
callback(xhr);
};
} else {
xhr.onreadystatechange = ensureReadiness;
}
xhr.open('GET', url, true);
xhr.send();
},
OpenIFrame: function(url) {
var mfwId = 'mfw2';
var oldframe = document.getElementById(mfwId);
if (oldframe) {
oldframe.parentElement.removeChild(oldframe);
}
var iframe = document.createElement('iframe');
iframe.setAttribute('src', url);
var iframeWrap = document.createElement('div');
var frameWidth = 1100;
var frameHeight = 640;
var frameTop = '50%';
var frameLeft = '50%';
var marginLeft = (frameWidth / -2),
marginTop = (frameHeight / -2);
if (window.innerWidth / 2 + marginLeft < 0) marginLeft = window.innerWidth / -2;
if (window.innerHeight / 2 + marginTop < 0) marginTop = window.innerHeight / -2;
var wrapStyles = {
width: frameWidth + 'px',
height: frameHeight + 'px',
position: 'fixed',
top: '50%',
left: '50%',
border: '3px solid #ccc',
borderRadius: '5px',
boxShadow: 'rgba(0, 0, 0, 0.2) 0 3px 6px',
backgroundColor: '#fff',
marginTop: marginTop + 'px',
marginLeft: marginLeft + 'px',
maxWidth: '100%',
paddingLeft: '1px'
};
var frameStyles = {
width: '100%',
height: '100%',
border: 'none'
};
for (var prop in wrapStyles) {
iframeWrap.style[prop] = wrapStyles[prop];
}
for (var prop in frameStyles) {
iframe.style[prop] = frameStyles[prop];
}
var closeButton = document.createElement('div');
closeButton.innerHTML = '×';
closeButton.onclick = function() {
var el = document.getElementById(mfwId);
if (el) el.parentElement.removeChild(el);
};
var closeStyles = {
position: 'absolute',
width: '24px',
height: '24px',
borderRadius: '50%',
border: '2px solid #ccc',
backgroundColor: '#fff',
boxShadow: 'rgba(0, 0, 0, 0.3) 0 3px 4px',
textAlign: 'center',
lineHeight: '24px',
fontFamily: 'Arial',
fontSize: '25px',
top: '-14px',
right: '-14px',
color: '#ccc',
cursor: 'pointer'
};
for (var prop in closeStyles) {
closeButton.style[prop] = closeStyles[prop];
}
var overlay = document.createElement('div');
var overlayStyles = {
position: 'fixed',
top: 0,
left: 0,
width: '100%',
height: '100%',
background: 'rgba(0, 0, 0, 0.4)',
zIndex: 10000
};
for (var prop in overlayStyles) {
overlay.style[prop] = overlayStyles[prop];
}
overlay.id = mfwId;
overlay.onclick = function() {
var el = document.getElementById(mfwId);
if (el) el.parentElement.removeChild(el);
};
iframeWrap.appendChild(iframe);
iframeWrap.appendChild(closeButton);
overlay.appendChild(iframeWrap);
document.body.appendChild(overlay);
return false;
}
};
/// ----------------------------------
/// Setup page load actions
/// ----------------------------------
/// Instantiate publishing object and add window.onload event handler
var MAXPublishPage = MAXPublishPage || new MAXSEPublishing();
if (window.jQuery) {
$(document)
.ajaxComplete(function() {
MAXPublishPage.OnReady();
});
}
MAXPublishPage.AddLoadEvent(
function() {
MAXPublishPage.OnReady();
}
);