//Get parameters
function getParameter(aP){var qS = new String(location.search.substring(1,location.search.length));var p = qS.split("&");var val = "";if(aP){for(i=0;i<p.length;i++){if(p[i].split( "=" )[0] == aP){val = p[i].split( "=" )[1];}}return val;}}
var page_id 	= getParameter("pid");
var banner_id 	= getParameter("adid");
var creative_id	= getParameter("rid");
var buy_id 		= getParameter("bid");
var mktprgm 	= getParameter("mktprgm");
var pricing 	= getParameter("pref");

//Code that runs before PageView request is built
/*
function _hbOnPrePVR(req){
	if(typeof(dealerInfo) != "undefined") {
		if(dealerInfo.id != null) {
			pop_group = "Preffered Dealer";
		} else if(Cookie.get('zipcode')) {
			pop_group = "Known Zip";
		} else {
			pop_group = "Unknown Zip";
		}
		
		cv.c99 = pop_group;
	}
}
*/
//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";
if(pricing == null || pricing == "") pricing = " ";

// 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() + "|" + unescape(pricing);
  var expires = new Date(); //Cookie expires in 45 days
  expires.setDate(expires.getDate() + 45);
  var 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 = cookieName+"="+value+";expires="+expires.toGMTString()+";path="+path+";host="+cookieDomain+";false";
}

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

var cleanPN = nameCleaner(document.title);
var cv=_hbEvent("cv");

// Get the IWOV dev out of the path
var path = location.pathname;
if (path.indexOf('htdocs') != -1) {
	var parts = path.split('htdocs');
	path = parts[1];
}
  
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];

//if(language_check != "en"){
	mlc = mlcSplice.splice(1, mlcSplice.length);
/*}else{
	mlc = mlcSplice.splice(2, mlcSplice.length);
}
*/
mlc = mlc.join("/");


//hbx.pn=cleanPN;
hbx.pndef = "index.html"; // takes the name of the html file at the page name = *.html
hbx.mlc="/"+mlc+";/"+brand+"/"+mlc;
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);


if(getParameter("showTracking")) {
	alert(	"hbx\n" + 
			"--pn: " + hbx.pn + "\n" +
			"--mlc: " + hbx.mlc + "\n" +
			"--eof: eof\n"
		);

}


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

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

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
 //alert("you are in wss_funs in root "+argPN+" "+argMLC+" "+argLid+" "+argLpos+" "+argHEC);
  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 hbxRecSearch(keys,res,at1,at2,at3,at4)
{
    /* add a search event to the data sent to hitbox
    that contains details about the search result set */
    
    var ev1;
    hbx.pn = keys;
    ev1 = _hbEvent("search");
    ev1.keywords = keys;
    ev1.results = res;
    
    if(at1 != null && at1.length > 0) {ev1.attr1=at1;}
    if(at2 != null && at2.length > 0) {ev1.attr2=at2;}
    if(at3 != null && at3.length > 0) {ev1.attr3=at3;}
    if(at4 != null && at4.length > 0) {ev1.attr4=at4;}
}