// WWS code
var _hbEC=0,_hbE=new Array;function _hbEvent(a,b){b=_hbE[_hbEC++]=new Object();b._N=a;b._C=0;return b;}
var hbx = _hbEvent("pv");hbx.vpc="HBX0100u";hbx.gn="a.chryslerllc.com";

// Dodge Brand and Global accounts for externally hosted partner sites
hbx.acct="DM530925P1NA;DM540330MAFN";

var brand = "dodge";
var exthostname = location.hostname;

if (exthostname == "wstest.bbdodetroit.com" || exthostname == "dev.icshq.com"){
	//test accounts
	hbx.acct = "DM54050166AN;DM540330JFCA";
}

//Get tracking parameters passed into page
var page_id 	= getParameter("pid");
var banner_id 	= getParameter("adid");
var creative_id	= getParameter("rid");
var buy_id 		= getParameter("bid");
var mktprgm 	= getParameter("mktprgm");

//Setting default values
if((page_id == "") || (page_id == null) || (page_id == "%epid!")){
	var id_val = "nothing";
}

if(page_id == null || page_id == "") page_id = " ";
if(banner_id == null || banner_id == "") banner_id = " ";
if(creative_id == null || creative_id == "") creative_id = " ";
if(mktprgm == null || mktprgm == "" || "".equals( mktprgm )) mktprgm = "unset";

// we only set the market code rid variable to zero when the pid and adid are numbers (doubleclick data) and rid is blank
if (isNaN(page_id) == false && page_id != " " && isNaN(banner_id) == false && banner_id != " "){
	if (creative_id == "" || creative_id == " ") creative_id = "0";
}

//if the bid has a value then concatenate it with the adid (delimited by an underscore)
if((buy_id != null) && (buy_id != "")){
	banner_id = banner_id + "_" + buy_id;
}
//Set cookie if needed
if(id_val != "nothing"){
  var cookieName = "mktcodes";
  var myDate = new Date();
  var value = "|" + unescape(page_id) + "|" + unescape(banner_id) + "|" + unescape(creative_id) + "|" + unescape(mktprgm) + "|" + myDate.valueOf();
  var expires = new Date(); //Cookie expires in 45 days
  expires.setDate(expires.getDate() + 45);
  var cookiepath = "/";
  // 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 = cookieName+"="+value+";expires="+expires.toGMTString()+";path="+cookiepath+";host="+cookieDomain+";false";
}

function nameCleaner(str) {
	str = str.toString();
	return str.replace(/[^a-zA-Z0-9,\s,+,-,_,\/]/g,'').replace(/[\s+]/g,"_").toLowerCase();
}

var cv=_hbEvent("cv");
var path = location.pathname;
var mlcSplit = path.split("/");
if(location.pathname.indexOf(".html") != -1 || location.pathname.indexOf(".jsp") != -1){
  var mlcSplice = mlcSplit.splice(0, mlcSplit.length -1);
}else{
  var mlcSplice = mlcSplit.splice(0, mlcSplit.length);
}
var mlcJoin = mlcSplice.join("/");
var mlc = mlcJoin.toLowerCase();
var language_check =  mlcSplice[1];

mlc = mlcSplice.splice(1, mlcSplice.length);
mlc = mlc.join("/");

hbx.pndef = "index.html"; // takes the name of the html file at the page name = *.html
//special mlc used for externally hosted sites that use the brand`s tracking info
hbx.mlc=exthostname+"/"+mlc+";/"+brand+"/"+exthostname+"/"+mlc;
//override MLC if necessary
if (typeof(newMLC) != "undefined" && newMLC) hbx.mlc = newMLC;
hbx.ctdef="full";
hbx.dcmpn="bid";
hbx.hqsp="adid";
hbx.hrf="hrf";

//Optional Variables
hbx.lt="auto";
hbx.lc="y";
hbx.dlf=".pdf,.zip,.hqx!.jsp,.htm,.html";
cv.c21=location.protocol+'//'+location.host+location.pathname;
cv.c22=location.search.substring(1, location.search.length);

////////////////////////////////////////////////////////////////////
function getParameter( argParam ) {
  var queryString = new String( location.search.substring(1, location.search.length) );
  var params = queryString.split( "&" );
  var val = "";
  if( argParam ) {
   for(i=0;i<params.length;i++) {
    if( params[i].split( "=" )[0] == argParam )
     val = params[i].split( "=" )[1];
   }
   return val;
  }
}

////////////////////////////////////////////////////////////////////
// This function goes and gets the cookie for the browser back redirect code.
function getCookie(cookiename) {
	var cookie = " " + document.cookie;
	var search = " " + cookiename + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
   return(setStr);
}

////////////////////////////////////////////////////////////////////
// This function is used when calling one of the brand sites (gets and passes necessary tracking params
function goBrand(argUrl) {
	if(page_id == " "){
		var mktCodeCookie = getCookie( "mktcodes" );
		if (mktCodeCookie){
			var splitResultsArray = mktCodeCookie.split( "|" );
			page_id = splitResultsArray[1];
			banner_id = splitResultsArray[2];
			creative_id = splitResultsArray[3];
		} else {
			page_id = location.hostname;
			banner_id = location.pathname;
			creative_id = "";
		}
	}
	argUrl = (argUrl.indexOf("?") == -1) ? argUrl	+= "?" : argUrl	+= "&";
	argUrl += "pid="+page_id+"&adid="+banner_id+"&rid="+creative_id;
	window.location = argUrl;
}

////////////////////////////////////////////////////////////////////
// This function handles page tracking from within flash modules
function pageTrack(flashPN, flashMLC) {
	var flashPN = nameCleaner(flashPN);
	var flashMLC = nameCleaner(flashMLC);
	var trackPN = hbx.pndef; // ignore the passed in Page Name
	var trackMLC = hbx.mlc.split(";");
	trackMLC = trackMLC.join(flashMLC + ";");
	trackMLC = trackMLC + flashMLC;
	trackMLC = trackMLC.replace(/\/\//g,'\/'); //stripping double slashes
	_hbPageView(trackPN, trackMLC);
	if(getParameter("showTracking")) {
		alert(	"flashPageTrack\n" +
			"--pn: " + trackPN + "\n" +
			"--mlc: " + trackMLC + "\n"
		);
	}
}

////////////////////////////////////////////////////////////////////
// This function handles link tracking from within flash modules
function linkTrack(flashLPOS, flashName) {
	var flashName = nameCleaner(flashName);
	var flashLPOS = nameCleaner(flashLPOS);
	_hbLink(flashName, flashLPOS);
	if(getParameter("showTracking")) {
		alert(	"flashLinkTrack\n" +
			"--lpos: " + flashLPOS + "\n" +
			"--name: " + trackName + "\n"
		);
	}

}

////////////////////////////////////////////////////////////////////
// This function handles link tracking for LEGACY flash modules
function flashLinkTracking(argPN, argMLC, argLid, argLpos, argHEC){
// arguments passed only for legacy flash modules.  Only lid and lpos needed
// call new tracking function to register click with hbx

  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)
}

////////////////////////////////////////////////////////////////////
// This function handles page tracking for LEGACY flash modules
function flashPageTracking(argPN, argMLC){

   var newPageName = argPN.replace(/[^a-zA-Z0-9,\s,+,-]/g,'').replace(/\s+/g,"+");
   var newMLC = argMLC.replace(/[^a-zA-Z0-9,\s,+,-]/g,'').replace(/\s+/g,"+");

   pageTrack(newPageName, newMLC)
}
