(function () {
//todo
//does qs url have a zipcode *** already does this
//if yes load iframe noramlly
//if not does bat have a zipcode in a cookie
//if yes load in that zip and then gather the zip into the iframe url
//if not show a madel asking for zip
//once submitted load iframe with zipCide in the qs
//if cancelled then load iframe normally
// setInterval(checker, 500);
// function checker() {
// // console.log(window.isExternalLoaded)
// // console.log(isExternalLoaded)
// }
// var script = document.createElement('script');
// script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js';
// script.type = 'text/javascript';
// document.getElementsByTagName('head')[0].appendChild(script);
//https://tcuv.datcdn.com/tcuviframe2.css
//todo, move this history stuff to tda_global
//if (window.location.href.indexOf("national-preview.aws") > 0) {
var askZip='
'+
'
'+
''+
'
SET YOU LOCATION
'+
'
Enter your Zip Code to find dealers, inventory, and special offers located near you.
'+
'
'+
''+
''
'
'+
"
"+
"
";
// createCookie("pageHistory", "https://national-preview.aws.buyatoyota.com/newengland/");
// window.history.pushState({ prevUrl: window.location.href }, null, "/new/path/in/your/app")
// console.log("00000000000000000000000000000000000000000000 " + document.referrer)
// if (window.location.hash.substring(1).includes("loaded")) {
// function readCookie(name) {
// var nameEQ = name + "=";
// var ca = document.cookie.split(";");
// for (var i = 0; i < ca.length; i++) {
// var c = ca[i];
// while (c.charAt(0) == " ") c = c.substring(1, c.length);
// if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
// }
// return null;
// }
// var caughtNavState = 0;
// // console.log("00000000000000000000000000000000000000000000 " + document.referrer)
// window.onpopstate = function (event) {
// event.preventDefault();
// // history.pushState("", document.title, window.location.pathname + window.location.search);
// //get what is in cookie
// fullList = readCookie("pageHistory");
// //split to array
// listArr = fullList.split(",");
// // console.log("history 4 = " + listArr[4])
// // console.log("history 3 = " + listArr[3])
// // console.log("history 2 = " + listArr[2])
// // console.log("history 1 = " + listArr[1])
// // console.log("history 0 = " + listArr[0])
// // console.log("current = " + window.location.protocol + "//" + window.location.host + window.location.pathname + window.location.search)
// //get the url to go back to
// for (var i = 4; i >= 0; i--) {
// console.log("i " + i);
// console.log("listArr element var " + listArr[i]);
// if (
// listArr[i] !=
// window.location.protocol +
// "//" +
// window.location.host +
// window.location.pathname +
// window.location.search
// ) {
// console.log("found a nav to nav to");
// //window.location.href = listArr[i]
// history.pushState("", "", listArr[i]);
// location.reload();
// caughtNavState = 1;
// break;
// }
// }
// //fail, falback
// if (caughtNavState == 0) {
// console.log(
// "failure to find previous page " +
// window.location.protocol +
// "//" +
// window.location.host
// );
// window.location.href =
// window.location.protocol + "//" + window.location.host;
// }
// //window.location.href = "https://national-preview.aws.buyatoyota.com/newengland/";
// //window.location.href = window.history.go(-2);
// // window.history.go(-2);
// // document.getElementById("tcuviframe").remove()
// //location.reload();
// };
// // }
// //alert(window.location.hash.substring(1))
// // history.back(); // alerts "location: http://example.com/example.html?page=1, state: {"page":1}"
// // history.back(); // alerts "location: http://example.com/example.html, state: null
// // ; // alerts "location: http://example.com/example.html?page=3, state: {"page":3}
// // window.onload = function() {
// // if(!window.location.hash) {
// // window.location = window.location + '#loaded';
// // window.location.reload();
// // }
// // }
// if (!document.getElementById("tcuviframe2css")) {
// console.log("attempting to load tcuv iframe css");
// var head = document.getElementsByTagName("head")[0];
// var externalFile = document.createElement("link");
// externalFile.id = "tcuviframe2css";
// externalFile.rel = "stylesheet";
// externalFile.type = "text/css";
// externalFile.href = "https://tcuv.datcdn.com/tcuviframe2.css";
// externalFile.media = "all";
// head.appendChild(externalFile);
// console.log("load of tcuv iframe css complete");
// }
// } else {
// console.log("2222222222222222222222");
// }
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(";");
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == " ") c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
function getParameterByName(name, url) {
if (!url) url = window.location.href.toLowerCase();
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return "";
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
function cleanSeriesName(seriesname) {
seriesname = seriesname.replace("hybrid", " hybrid");
seriesname = seriesname.replace("prius", "prius ");
seriesname = seriesname.replace("plugin", "plug-in");
seriesname = seriesname.replace("fjcruiser", "fj cruiser");
seriesname = seriesname.replace("landcruiser", "land cruiser");
seriesname = seriesname.replace("corollaim", "corolla im");
seriesname = seriesname.replace("yarisia", "yaris ia");
return seriesname;
}
function buildQAVars(varName) {
var strQS = "";
var varValue = "";
varValue = getParameterByName(varName);
if (varValue) {
//alert(varName+"="+varValue);
strQS = varName + "=" + varValue;
}
return strQS;
}
function removeURLParameter(url, parameter) {
//prefer to use l.search if you have a location/link object
var urlparts = url.split("?");
if (urlparts.length >= 2) {
var prefix = encodeURIComponent(parameter) + "=";
var pars = urlparts[1].split(/[&;]/g);
//reverse iteration as may be destructive
for (var i = pars.length; i-- > 0; ) {
//idiom for string.startsWith
if (pars[i].lastIndexOf(prefix, 0) !== -1) {
pars.splice(i, 1);
}
}
url = urlparts[0] + (pars.length > 0 ? "?" + pars.join("&") : "");
return url;
} else {
return url;
}
}
function char_count(str, letter) {
var letter_Count = 0;
for (var position = 0; position < str.length; position++) {
if (str.charAt(position) == letter) {
letter_Count += 1;
}
}
return letter_Count;
}
/*function iframeLoaded() {
var iFrameID = document.getElementById('tcuvframe');
if(iFrameID) {
iFrameID.height = "";
iFrameID.height = iFrameID.contentWindow.document.body.scrollHeight + "px";
$("body").css("overflow", "hidden");
}
}
*/
//get qs vars
//siteid = getParameterByName('siteid');
//alert(siteid);
var myElements = document.querySelectorAll(".footer-secondary-wrap");
for (var i = 0; i < myElements.length; i++) {
myElements[i].style.display = "none";
}
var s = document.createElement("script");
s.src = "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js";
document.head.appendChild(s);
s.onload = function (e) {
e.preventDefault();
/* now that its loaded, do something */
$(document).ready(function () {
if(getParameterByName("testing")=='on'){
console.log("zip popup on")
// document.write(askZip);
}
//set static css
$("", {
rel: "stylesheet",
type: "text/css",
href: "https://tcuv.datcdn.com/tcuviframe2.css",
}).appendTo("head");
//12-month
//$('body').on('blur change click dblclick error focus focusin focusout hover keydown keypress keyup load mousedown mouseenter mouseleave mousemove mouseout mouseover mouseup resize scroll select submit', function(){
//$("body").css("overflow", "hidden");
//});
//on iframe evnts
console.log(
"---------------------------------------------------------------------"
);
$("#tcuvframe").on(
"blur change click dblclick error focus focusin focusout hover keydown keypress keyup load mousedown mouseenter mouseleave mousemove mouseout mouseover mouseup resize scroll select submit",
function (e) {
// console.log("+111111111111111_+_+_+_+_")
// console.log($("#tcuvframe").attr('src'))
console.log(e);
e.preventDefault();
$("body").css("overflow", "hidden");
$("body").css("overflow", "auto");
//console.log("-------------- all");
$window = $(window);
//$("#tcuvframe").css("overflow", "hidden");
$("#tcuvframe").css("overflow", "auto");
$("#tcuvframe").height(function () {
return $window.height() - $(this).offset().top;
});
$("#tcuvframe").width(function () {
return $window.width(); //-$(this).offset().left;
});
//console.log(this.history);
window.parent.document.body.style.overflow = "hidden";
//alert($('#tcuvframe').attr('src'));
//alert("data point");
//clean zip from iframe src
//var zipCheck = $('#tcuvframe').attr('src')
//if(zipCheck.indexOf('?ZipCode=98362') !== -1){
//console.log("-----------contains zip");
//zipCheck = zipCheck.replace('?ZipCode=98362','');
//history.pushState(null, null, zipCheck)
//$('#tcuvframe').attr('src', zipCheck)
//}
var curlocation = $("#tcuvframe").attr("src");
//alert(curlocation);
//if(curlocation.indexOf(";") == -1){
//curlocation = removeURLParameter(curlocation, "location");
//curlocation = curlocation.replace('?ZipCode=98362', '');
//curlocation = curlocation.replace('https://www.toyotacertified.com/?','');
//curlocation = curlocation.replace('https://www.toyotacertified.com/','');
//curlocation = curlocation.replace('https://www.toyotacertified.com','');
//history.pushState(null, null, '?location='+curlocation);
//console.log("-------------- " + curlocation);
//console.log("-------------- " + this.contentWindow.href);
//}
//if($curlocation=="certification"){
//history.pushState(null, null, 'https://www.westernwashington.buyatoyota.com/en/tcuv?location='+curlocation);
//}
//https://www.toyotacertified.com/certification
}
);
$(window).on("message", function (e) {
e.preventDefault();
// console.log("iframe message");
// var data = e.originalEvent.data;
// if (data.indexOf('iframe_page_url') !== -1) {
// $("#tcuvframe").css("display", "none");
// $(window).scrollTop(0);
// var removePoint = "iframe_page_url";
// var theURL = data.substring(data.indexOf(removePoint) + removePoint.length, data.length);
// var triggerReload = false;
// theURL = theURL.replace(":", "");
// theURL = theURL.replace("}", "");
// theURL = theURL.replace(" ", "");
// theURL = theURL.replace(/['"]+/g, '');
// console.log("1--------- " + theURL);
// if (theURL.indexOf("radius") == -1 && theURL.indexOf("inventory") > 0 && theURL.indexOf("vin") == -1) {
// console.log("2-------------- radius index" + theURL.indexOf("radius") + " ---------------- inventory index: " + theURL.indexOf("inventory"))
// theURL = theURL + ";radius=50";
//
// triggerReload = true;
// }
// console.log("3--------- " + theURL);
//
// console.log("4--------- " + window.location.href);
// history.pushState(null, null, window.location.href.split('?')[0] + '?location=' + theURL);
// if (triggerReload) {
// console.log("4 ---------------- reloading");
// document.getElementById('tcuvframe').src = theURL;
// }
//
// $("#tcuvframe").css("display", "block");
// }
});
window.onbeforeunload = function (e) {
//return "Dude, are you sure you want to leave? Think of the kittens!";
e.preventDefault();
};
//on bat site events
$(window).on("beforeunload", function (e) {
e.preventDefault();
});
$(window).on("load resize click", function () {
e.preventDefault();
//$("body").css("overflow", "hidden");
//$("body").css("overflow", "auto");
// $window = $(window);
// $(this).show();
// alert('load the iframe2')
$("body").css("overflow", "hidden");
$("body").css("overflow", "auto");
$window = $(window);
//$("#tcuvframe").css("overflow", "hidden");
$("#tcuvframe").css("overflow", "auto");
$("#tcuvframe").height(function () {
return $window.height() - $(this).offset().top;
});
$("#tcuvframe").width(function () {
return $window.width(); //-$(this).offset().left;
});
window.parent.document.body.style.overflow = "hidden";
//$("#shopNewCarsLink").css("left",$window.width()-$("#shopNewCarsLink").width()+"px");//$window.width()-200);
//$("#shopNewCarsLink").css("top",$window.width()-$("#shopNewCarsLink").width()+"px");//$window.width()-200);
});
//setTimeout(function () {
// $("#tcuvframe").css("style","block");
//}, 2000);
});
};
var strQS = "";
var strQSStructured = "";
var isite = "";
var shopToyota = "";
var lang = "";
var theLocation = "";
theLocation = getParameterByName("location");
//get the language
if (theLocation == "" || theLocation == null) {
if (window.location.href.indexOf("/es/") < 0) {
//isite = "toyotacertified.com";
isite = "https://www.toyotacertified.com";
shopToyota = "Shop New Toyotas";
lang = "en";
document.title =
"Certified Used Toyota Vehicles | Pre-Owned Cars, Trucks, SUVs & Hybrids";
} else {
//isite = "toyotacertificados.com";
isite = "https://www.toyotacertificados.com";
shopToyota = "Compras Nuevos Toyotas";
lang = "es";
//document.title="Autos Usados Certificados Toyota | Autos, Camionetas, SUV e HÃbridos Usados";
}
// arrBATNames = ["vin", "dealerCode", "series", "zipCode", "retargeting", "usercookie", "ActivityID", "keyword", "utm%5fcampaign", "utm%5fcontent", "utm%5fmedium", "utm%5fsource", "location"];
// arrTCUVNames = ["vin", "dealerCode", "model", "zipCode", "retargeting", "usercookie", "ActivityID", "keyword", "utm%5fcampaign", "utm%5fcontent", "utm%5fmedium", "utm%5fsource", "location"];
arrBATNames = [
"vin",
"dealercode",
"series",
"zipcode",
"ActivityID",
"utm%5fcampaign",
"utm%5fcontent",
"utm%5fmedium",
"utm%5fsource",
"location",
];
arrTCUVNames = [
"vin",
"dealer",
"model",
"zipCode",
"ActivityID",
"utm%5fcampaign",
"utm%5fcontent",
"utm%5fmedium",
"utm%5fsource",
"location",
];
var vin = getParameterByName("vin");
//get all expected qs vars
for (i = 0; i < arrBATNames.length; i++) {
var varVal = "";
varVal = getParameterByName(arrBATNames[i]);
// if var is there add to qs for iframe
if (varVal != "" && varVal != null) {
if (arrBATNames[i] == "series") {
varVal = cleanSeriesName(varVal);
}
// console.log("*************** " + arrBATNames[i] + " " + varVal);
//alert (arrBATNames[i]);
strQSStructured += "%3B" + arrTCUVNames[i] + "=" + varVal;
}
}
//if dealerCode but no series make series null
if (
getParameterByName(arrBATNames[1]) &&
(getParameterByName(arrBATNames[2]) == null ||
getParameterByName(arrBATNames[2]) == "")
) {
//alert (arrBATNames[i]);
strQSStructured += "%3B" + arrTCUVNames[2] + "=null";
}
//special for vin
if (getParameterByName(arrBATNames[0])) {
//strQSStructured += "%3Bview=details";
}
//if there is any qs
if (strQSStructured) {
//remove first seperator
strQSStructured = "/vdp?" + strQSStructured;
strQSStructured=strQSStructured.replace("?%3B","?");
}
//Count the number in the strQSStructured. if only one and that one is series
//special for series by itself
//if number of ; is 1 then check to see if that one is the series
//, then change the strQSStructured to go to series page
if (
char_count(strQSStructured, "%3B") == 1 &&
strQSStructured.includes(arrTCUVNames[2])
) {
console.log("series only page");
strQSStructured = "/" + getParameterByName(arrBATNames[2]);
if (!strQSStructured.includes("zip")) {
console.log("no zip in qs");
if (readCookie("zip") && !varVal) {
//no zip if qs but we use cookie zip instead
strQSStructured += "?" + arrTCUVNames[3] + "=" + readCookie("zip");
console.log("update zip by cookie");
}
}
}else{
//not a series only clickthrough
//no zip in clickthrough
if (!strQSStructured.includes("zip")) {
console.log("no zip in qs");
if (readCookie("zip") && !varVal) {
//no zip in qs but we use cookie zip instead
strQSStructured += ";" + arrTCUVNames[3] + "=" + readCookie("zip");
console.log("update zip by cookie");
}
}
}
//if zip is in cookie but not qs, pass it along to tc
} else {
isite = theLocation;
if (window.location.href.indexOf("/es/") < 0) {
//isite = "toyotacertified.com";
shopToyota = "Shop New Toyotas";
lang = "en";
} else {
//isite = "toyotacertificados.com";
shopToyota = "Compras Nuevos Toyotas";
lang = "es";
}
}
/*console.log("3--------- "+isite + strQSStructured);
if(!isite + strQSStructured.includes("radius")){
strQSStructured=strQSStructured+";radius=50";
}
console.log("4--------- "+isite + strQSStructured);*/
//series specific page
//https://www.toyotacertified.com/inventory;model=4Runner
console.log("------iframe--------");
console.log(strQSStructured);
var ifrm =
'';
//var ifrm = '';
$('').appendTo('#frameHolder');
//var dynIframe = '';
//$("#frameHolder").append(dynIframe)
console.log("*************************");
//document.write(ifrm);
/* document.write(
'' +
shopToyota +
""
);*/
setTimeout(function () {
document.getElementById("tcuvframe").style.display = "block";
}, 5000);
//console.log("---------------"+ document.cookie.zip); // "do not tell you"
})();