/********** WSS_Dodge code ********************/

// WWS code

var page_id = getParameter("pid");
var banner_id = getParameter("adid");
var creative_id = getParameter("rid");
var mktprgm = getParameter("mktprgm");
var pricing = getParameter("pref");


if((page_id == "") || (page_id == null) || (page_id == "%epid!")){
  var id_val = "nothing";
  } else {
    var page_id = unescape(page_id).replace(/[^a-zA-Z0-9,\s]/g,'').replace(/\s+/g,"");
    new Number(page_id);
    var id_val = Number(page_id);
    var id_val = isNaN(id_val);
}

var path ="";
var anametrixPath = "";
var pathSplice ="";

//Set cookie if needed
if(id_val != "nothing"){
	var name = "mktcodes";
  	var myDate = new Date();
  	var value = "|" + unescape(page_id) + "|" + unescape(banner_id) + "|" + unescape(creative_id) + "|" + unescape(mktprgm) + "|" + myDate.valueOf() + "|" + unescape(pricing);
  	var expires = new Date(); //Cookie expires in 45 days
  	expires.setDate(expires.getDate() + 45);
  	path = "/";
  	// get the full url string, http://www..com/dir/page.html?x=y...
  	var curUrl = new String( window.location );
  	// strip off the leading protocol ("http://") and strip any trailing file-path and query string
  	var domain = curUrl.substring(7, curUrl.indexOf( ".com/"));
  	// strip off any preceding subdomains (www.sub.subsub...)
  	domain = domain.substring(domain.lastIndexOf(".") + 1, domain.length) + ".com";
  	//set the domain for the cookie
  	var cookieDomain = curUrl.substring(7, curUrl.indexOf( ".com/"))+".com";
  	document.cookie = name+"="+value+";expires="+expires.toGMTString()+";path="+path+";host="+cookieDomain+";false";
}

var anametrixPath ="";


if (path != "" && path != "undefined") {
    anametrixPath = "/" + path
    anametrixPath = anametrixPath.toLowerCase();
}
else 
    if (location.pathname.indexOf(".html") != -1 || location.pathname.indexOf(".jsp") != -1 || location.pathname.indexOf(".php") != -1) {
        var path = location.pathname;
        if (path.indexOf('htdocs') != -1) {
            var pathParts = path.split('htdocs');
            path = pathParts[1];
        }
        var pathSplit = path.split("/");
        if (cur_lang != "") {
            var pathSplice = pathSplit.splice(0, pathSplit.length - 1);
        }
        else {
            var pathSplice = pathSplit.splice(1, pathSplit.length - 2);
        }
        
        var pathJoin = pathSplice.join("/");
        anametrixPath = pathJoin.toLowerCase();
        anametrixPath = "/" + pathJoin;
    }
    else {
        anametrixPath = location.pathname.toLowerCase();
        anametrixPath = anametrixPath.replace(/\/$/, "");
    }
    
    if (anametrixPath == "/" || anametrixPath == "") {
        anametrixPath = "/home"
    }

var country = country.toLowerCase();
var language = language.toLowerCase();
var brand = brand.toLowerCase();

//var anametrixPN= document.title.replace(/[^a-zA-Z0-9,\s,+,-]/g,'').replace(/\s+/g,"+");
var anametrixPN= document.title;


if ((curVehicle == "brand") || (curVehicle == "") || (curVehicle == null)) {
  anametrixPath = "/" +country+ "/" + language + "/" + brand + anametrixPath;
} else {
  anametrixPath= "/" + country+"/"+language+"/"+brand+"/vehicles"+anametrixPath;
}

// Instantiate the tag
var nr_t = new nr_tag();
var nr_c = new Object();
// Make sure that the page variables object is present
if (typeof pv == 'undefined') pv = new Object();
// Cookie domain
var anaURL = location.hostname;

nr_t['domain'] = location.host;	

nr_t['dataset'] = "100361"; //catch all basin
			
var anaClean=anaURL.replace(/www|com|\-|\./g, "");

if (anaURL.indexOf("53.28.38.192") != -1 || anaURL.indexOf("test") != -1 || anaURL.indexOf("uat") != -1){
  // test account
    anaType = "test"; 
 	anaClean = "inttest"; 
  } else {
  // live account
    anaType = "international";
}
var accountBase = anaURL.split(".");
var fullLocation = document.location.href;
var accountBrand = accountBase[1];
var accountURL = accountBase[0];

if(accountBrand == "sapientem"){

accountBrand = fullLocation.split("main/US/")[1].split("/us/")[0].toLowerCase();
}

(accountURL != "www")?accountURL = 'testing':accountBase;
 
nr_c = new Object();
 
nr_c.account = {
		 
	    international:{
	        dodgear:"100373",
			dodgeau:"100377",
			dodgeat:"100387",
			dodgebe:"100393",
			dodgecl:"100399",
			dodgecn:"100405",
			dodgeco:"100411",
			dodgecz:"100417",
			dodgedk:"100423",
			dodgefr:"100429",
			dodgede:"100435",
			dodgeit:"100441",
			dodgejapan:"100447",
			dodgekz:"100453",
			dodgelv:"100459",
			dodgelt:"100465",
			dodgelu:"100471",
			dodgenl:"100477",
			dodgepe:"100483",
			dodgeph:"100489",
			dodgepl:"100495",
			dodgept:"100501",
			dodgepr:"100507",
			dodgero:"100513",
			dodgeru:"100519",
			dodgesg:"100525",
			dodgecoza:"100531",
			dodgecokr:"100539",
			dodgees:"100545",
			dodgese:"100551",
			dodgeswissch:"100557",
			mideastdodge:"100563",
			dodgecouk:"100569",
			dodgeve:"100575"

	    },
		test:{
			inttest:"100370"
		}
}
 
//nr_t['dataset'] = nr_c.account[accountBrand][accountURL];

try {
	nrcTagID = eval(nr_c.account[anaType][anaClean]);
	if(nrcTagID != "" && nrcTagID != undefined) nr_t['dataset'] = nrcTagID;
			}
	catch(err) {
		nr_t['dataset'] = "100361";
}


// Set the gateway hosts
nr_t['gwlist'] = ['gw.anametrix.net'];

// Automatic link click tracking: 1 = on, 0 = off
nr_t['link_tracking'] = 1;

// Automatic form tracking: 1 = on, 0 = off
nr_t['form_tracking'] = 1;

// Define any additional plugin scripts we'd like to load. Leave this blank unless
// you have a special script to load.
nr_t.init();
//nr_t.load([]);

function nameCleaner(str) {
	str = str.toString();
	return str.replace(/[^a-zA-Z0-9,\s,+,-,_,\/]/g,'').replace(/[\s+]/g,"_").toLowerCase();
}

var fullURL = location.href;
var pageViewVariables = new Object();

//anametrixPN = anametrixPN.replace(/[^a-zA-Z0-9,\s,\+,-]/g,'').replace(/\+/g,' '); //stripping '+' from page name;
		
//Used for normal page Tracking		
function setPageViewVariables(){
		
	//this code has been addded to get anametrixPath from MLC incase it 
	// is being sent as MLC from some page (anametrixPath;anametrixPath)
	if (anametrixPath.indexOf(';') != -1) {
		anametrixPath = anametrixPath.split(';')[1];
	}	
		
	var pageName = anametrixPath + "/" + anametrixPN;	
	
	pageName= pageName.toLowerCase().replace(/\/\//g,'\/'); //stripping double slashes; 
	
	pageViewVariables['name'] = pageName;	   
		
	// this method will set the folder names to pageview attributes object of Anametrix.
	setPageViewVariablesForTracking(pageName);
	
	//These items are goals/conversions in Anametrix.  The values (v:), are arbitrary as of this writing but still useful.	  	
	if (fullURL.indexOf("summary.do") != -1) { //Build my Own Conversion
		pageViewVariables['goal'] = {id:"BMO", v:"5", cmp:"last", a_1: "Build My Own"};
	}	
	if (fullURL.indexOf("findDealers.do") != -1){ //Find a Dealer Conversion
		pageViewVariables['goal'] = {id:"FAD", v:"5", cmp:"last", a_1: "Find a Dealer"};
	}
	if (fullURL.indexOf("inventorySearch.do") != -1){ //Search New Inventory Conversion
		pageViewVariables['goal'] = {id:"SNI", v:"5", cmp:"last", a_1: "Search New Inventory"};
	}
	if (fullURL.indexOf("/dma/") != -1){ //Viewed Incentive Conversion
		pageViewVariables['goal'] = {id:"Viewed Incentive", v:"5", cmp:"last", a_1: "Viewed Incentive"};
	}
	if (fullURL.indexOf("/en/gaq/") != -1){ //Get a Quote Conversion
		pageViewVariables['goal'] = {id:"GAQ", v:"5", cmp:"last", a_1: "Get a Quote"};
	}	
	(typeof nr_t != 'undefined')?nr_t.pageView(pageViewVariables):"";	
	
}
	
//Used for FlashTracking
function pageTrack(argPNCleaned, argPathCleaned) {
	
	var argPath = nameCleaner(argPathCleaned);
	
	//this code has been addded to get argPath from MLC incase it 
	// is being sent as MLC from some page (argPath;argPath)
	if (argPath.indexOf(';') != -1) {
		argPath = argPath.split(';')[1];
	}
	
	//this code has been addded to get anametrixPath from MLC incase it 
	// is being sent as MLC from some page (anametrixPath;anametrixPath)
	if (anametrixPath.indexOf(';') != -1) {
		anametrixPath = anametrixPath.split(';')[1];
	}
	
   	var pageNameData = "";
	
	var trackPath = anametrixPath + argPath;
	trackPath = trackPath.replace(/\/\//g,'\/'); //stripping double slashes

	var pageTrackVariables = new Object();  
	
	pageNameData = trackPath + "/" + anametrixPN;
	
	pageNameData = pageNameData.toLowerCase();
	
	pageNameData = pageNameData.replace(/\/\//g,'\/');//stripping double slashes; 
			
	pageViewVariables['name'] = pageNameData; 
	
	// this method will set the folder names to page track attributes object of Anametrix.
	setTrackingVariablesForFlash(pageNameData,pageTrackVariables);
		    	    
	//These items are goals/conversions in Anametrix.  The values (v:), are arbitrary as of this writing but still useful.	  	
	if (fullURL.indexOf("summary.do") != -1) { //Build my Own Conversion
		pageViewVariables['goal'] = {id:"BMO", v:"5", cmp:"last", a_1: "Build My Own"};
	}	
	if (fullURL.indexOf("findDealers.do") != -1){ //Find a Dealer Conversion
		pageViewVariables['goal'] = {id:"FAD", v:"5", cmp:"last", a_1: "Find a Dealer"};
	}
	if (fullURL.indexOf("inventorySearch.do") != -1){ //Search New Inventory Conversion
		pageViewVariables['goal'] = {id:"SNI", v:"5", cmp:"last", a_1: "Search New Inventory"};
	}
	if (fullURL.indexOf("/dma/") != -1){ //Viewed Incentive Conversion
		pageViewVariables['goal'] = {id:"Viewed Incentive", v:"5", cmp:"last", a_1: "Viewed Incentive"};
	}
	if (fullURL.indexOf("/en/gaq/") != -1){ //Get a Quote Conversion
		pageViewVariables['goal'] = {id:"GAQ", v:"5", cmp:"last", a_1: "Get a Quote"};
	}	
	(typeof nr_t != 'undefined')?nr_t.pageView(pageViewVariables):"";	
	
}

//this method will split the path and set it into the Page Track Variables for Pages 
function setPageViewVariablesForTracking(anametrixPath){
		
	try {
	//Split the path to get the all the folder names and set them in an array
	var anamarray = anametrixPath.split("/");
	//Get number of items in the array
	var analength = anamarray.length;          
	
	//This switch is intended to set the folder names to the Page View Variables 
	//There is no case 1 because anamarray[0] is always empty [a '/' at the begining of path]	
	switch(analength)     
	{
		case 10:
		pageViewVariables['p.a_9'] = anamarray[9];	
		case 9:
		pageViewVariables['p.a_8'] = anamarray[8];	
		case 8:
		pageViewVariables['p.a_7'] = anamarray[7];	
		case 7:
		pageViewVariables['p.a_6'] = anamarray[6];
		case 6:
		pageViewVariables['p.a_5'] = anamarray[5];
		case 5:
		pageViewVariables['p.a_4'] = anamarray[4];
		case 4:
		pageViewVariables['p.a_3'] = anamarray[3];
		case 3:
		pageViewVariables['p.a_2'] = anamarray[2];
		case 2:
		pageViewVariables['p.a_1'] = anamarray[1];
		
	}
	
	} catch (e) {
		console.log(e);
	}
	
		
}

// this method will split the path and set it into the Track View Variables for Flash Tracking
function setTrackingVariablesForFlash(anametrixPath,pageTrackVariables){
		
	try {
	//Split the path to get the all the folder names and set them in an array
	var anamarray = anametrixPath.split("/");
	//Get number of items in the array   
	var analength = anamarray.length;          
	
	//This switch is intended to set the folder names to the Page View Variables 
	//There is no case 1 because anamarray[0] is always empty [a '/' at the begining of path]	
	switch(analength)     
		{
		case 10:
		pageTrackVariables['p.a_9'] = anamarray[9];	
		case 9:
		pageTrackVariables['p.a_8'] = anamarray[8];
		case 8:
		pageTrackVariables['p.a_7'] = anamarray[7];	
		case 7:
		pageTrackVariables['p.a_6'] = anamarray[6];
		case 6:
		pageTrackVariables['p.a_5'] = anamarray[5];
		case 5:
		pageTrackVariables['p.a_4'] = anamarray[4];
		case 4:
		pageTrackVariables['p.a_3'] = anamarray[3];
		case 3:
		pageTrackVariables['p.a_2'] = anamarray[2];
		case 2:
		pageTrackVariables['p.a_1'] = anamarray[1];
		
		}
	} catch (e) {
	console.log(e);
	}
	
		
}
	
function linkTrack(flashLPOS, flashName) {
	var flashName = nameCleaner(flashName);
	var flashLPOS = nameCleaner(flashLPOS);
	
    	
	var linkTrackVariables = new Object();

	linkTrackVariables['lc.n'] = flashName;
	linkTrackVariables['lc.a_1'] = flashLPOS; 
	//linkTrackVariables['lc.u'] = LINK_URL;  -- We should try to pass in the link URL here

	(typeof nr_t != 'undefined')?nr_t.sendReq(linkTrackVariables):"";
	 
}

function linkTrackGoURL(goURL,flashLPOS, flashLID) {
	linkTrack(flashLPOS, flashLID);
	window.location = goURL;
}

function flashLinkTracking(argPN, argPath, argLid, argLpos, argHEC){
  // arguments passed only for legacy flash modules.  Only lid and lpos needed
  var newLid = argLid.replace(/[^a-zA-Z0-9,\s,+,-]/g,'').replace(/\s+/g,"+");
  var newLpos = argLpos.replace(/[^a-zA-Z0-9,\s,+,-]/g,'').replace(/\s+/g,"+");

  linkTrack(newLpos, newLid)
}

function flashPageTracking(argPN, argPath, argGP){

   var argPNCleaned = argPN.replace(/[^a-zA-Z0-9,\s,+,-]/g,'').replace(/\s+/g,"+");
   var argPathCleaned = argPath.replace(/[^a-zA-Z0-9,\s,+,-]/g,'').replace(/\s+/g,"+");
 
   pageTrack(argPNCleaned, argPathCleaned)
}

try {
	
	onload_register('setPageViewVariables()');
	
} catch (e) {
	
	console.log(e);
	
}






