
/* $File: //depot/DocRoot/js/kr_hbx_lib.js $
 * Library used for HitBox implementation
 */

// HitBox accounts
function initHbxAccounts() {
  siteAcct = new Array();
  if ( isTestEnv() ) {
    siteAcct['aberdeennews']='DM541025OACA';
    siteAcct['belleville']='DM541026GLEN';
    siteAcct['bradenton']='DM54102652EV';
    siteAcct['centredaily']='DM541026P6ZE';
    siteAcct['charlotte']='DM541025I4RM';
    siteAcct['contracostatimes']='DM541025BARE';
    siteAcct['dfw']='DM54102597FM';
    siteAcct['duluthsuperior']='DM541026EGME';
    siteAcct['fortwayne']='DM541026D3FA';
    siteAcct['grandforks']='DM541026EHMD';
    siteAcct['kansas']='DM541025B1FS';
    siteAcct['kansascity']='DM54102513RE';
    siteAcct['kentucky']='DM541026I0BE';
    siteAcct['krd_global_rollup']='DM5410251GNB';
    siteAcct['ledgerenquirer']='DM541026EPVB';
    siteAcct['macon']='DM541026KNFA';
    siteAcct['mercurynews']='DM5410253ONZ';
    siteAcct['miami']='DM5410251BVM';
    siteAcct['montereyherald']='DM541026G1AD';
    siteAcct['myrtlebeachonline']='DM541026ODSD';
    siteAcct['ohio']='DM5410250GER';
    siteAcct['philly']='DM541025EHMF';
    siteAcct['realcities']='DM5503210OBV';
    siteAcct['sanluisobispo']='DM5410267GWZ';
    siteAcct['siliconvalley']='DM5503210OBV';
    siteAcct['sunherald']='DM541026H3ZS';
    siteAcct['tallahassee']='DM5410251DNA';
    siteAcct['thatsracin']='DM5503210OBV';
    siteAcct['thestate']='DM541026JIZW';
    siteAcct['timesleader']='DM541025A6EW';
    siteAcct['twincities']='DM5410256CED';
  }
  else {
    siteAcct['aberdeennews']='DM55013089FE';
        siteAcct['belleville']='DM550130IPZV';
        siteAcct['bradenton']='DM550130NPFW';
        siteAcct['centredaily']='DM550130N3AM';
        siteAcct['charlotte']='DM541105N7SD';
        siteAcct['contracostatimes']='DM550130K0WW';
        siteAcct['dfw']='DM550130GFDD';
        siteAcct['duluthsuperior']='DM550130EJEZ';
        siteAcct['fortwayne']='DM550130FJAN';
        siteAcct['grandforks']='DM5501309PVD';
        siteAcct['kansas']='DM550130ANEE';
        siteAcct['kansascity']='DM550130CANE';
        siteAcct['kentucky']='DM5501307JNF';
        siteAcct['krd_global_rollup']='DM55013049AA';
        siteAcct['ledgerenquirer']='DM550130PANZ';
        siteAcct['macon']='DM550130PFVD';
        siteAcct['mercurynews']='DM550130JBZB';
        siteAcct['miami']='DM541105J6DD';
        siteAcct['montereyherald']='DM5501305HZZ';
        siteAcct['myrtlebeachonline']='DM55013043BF';
        siteAcct['ohio']='DM5501307DMM';
        siteAcct['philly']='DM550130HIDA';
        siteAcct['realcities']='DM550321NJBZ';
        siteAcct['sanluisobispo']='DM55013041CW';
        siteAcct['siliconvalley']='DM550321FCZE';
        siteAcct['sunherald']='DM550130P1WD';
        siteAcct['tallahassee']='DM550130BPWD';
        siteAcct['thatsracin']='DM550321GCVS';
        siteAcct['thestate']='DM550130K1WB';
        siteAcct['timesleader']='DM550130OGAR';
    	siteAcct['twincities']='DM55013092DW';
  }
}

// Check environment.
function isTestEnv() {
  testHost = document.location.host;
  if (testHost == "" || testHost.match(/(\.(int|qa|stg)\.|localhost)/)) {
    return true;
  }
  return false;
}

// Return account for site passed in.
function getHbxAccount(site) {
  return siteAcct[site];
}

/* MULTI-LEVEL CONTENT CATEGORY */
function getHbxMLC (hbxPath) {
 var mlc = hbxPath.replace(hbxPath.substring((hbxPath.lastIndexOf("/"))+1),'');
 
 //mlc for global tracking
 var globalMLC = mlc.replace('/mld/','');
 globalMLC = globalMLC.substring (globalMLC.indexOf('/'));
 
 //ae entertainment domain is to be tracked under SDP
 hbxdomain = document.domain;
 if (hbxdomain.indexOf('ae.') == 0) hbxdomain = hbxdomain.replace ('ae.', ''); 
 
 mlc = "/" + hbxdomain + mlc.replace('/mld','');
 mlc = mlc.replace('www.', ''); 
 mlc = mlc + ";" + globalMLC; 
 return mlc;
}

/* get Article  */
function getHbxArticleID(hbxPath) {
 var pageName = hbxPath.substring((hbxPath.lastIndexOf("/"))+1);
 var articleID = pageName.substring(0, pageName.indexOf('.htm'));
 if (articleID) 
 	articleID = articleID + ' - ';
 if (articleID.indexOf(".") >= 0) 
 	articleID = ""; 
 if (articleID) 
 	articleID = stripHbxSpChar(articleID + document.title);
 else 
 	articleID= "";
 return articleID;
}

/* this method strips out special charaters from the string */
function stripHbxSpChar(a){
 a = a.split("|").join("");
 a = a.split("&").join("");
 a = a.split("'").join("");
 a = a.split("\"").join("");
 a = a.split("#").join("");
 a = a.split("$").join("");
 a = a.split("%").join("");
 a = a.split("^").join("");
 a = a.split("*").join("");
 a = a.split(":").join("");
 a = a.split("!").join("");
 a = a.split("<").join("");
 a = a.split(">").join("");
 a = a.split("~").join("");
 a = a.split(";").join("");
 a = a.split(",").join("");
 a = a.split(" ").join("+");
 a = a.split("++").join("+");
 a = a.split("/").join("-");
 return a;    
}

/* Is this search results page? */
function isSearch(hbxPath) {
  if (document.location.pathname.indexOf("search_results.htm") != -1) return true;
  return false;
}

/* Is this more news page? */
function isMoreNews () {
  if (document.location.pathname.indexOf("more_news.htm") != -1) return true;
  return false;
}

/* Is this ask the expert page? */
function isATE () {
  if (document.location.pathname.indexOf("ask_expert_front.htm") != -1) return true;
  return false;
}

/* Is this QA Forum page? */
function isQAForum () {
  if (document.location.pathname.indexOf("qa_forum.htm") != -1) return true;
  return false;
}

/* Get the value of a specific parameter from search part of url */
function getParamValue (paramName) {
  var searchstr = document.location.search;
  var paramLoc = searchstr.indexOf("&" + paramName + "=");
  if (paramLoc == -1)
  	paramLoc = searchstr.indexOf("?" + paramName + "=");
  //not found
  if (paramLoc == -1) return "";
  
  var paramVal = searchstr.substring(paramLoc);
  paramVal = paramVal.split ("&" + paramName + "=").join("");
  paramVal = paramVal.split ("?" + paramName + "=").join("");
  
  if (paramVal.indexOf("&") != -1) {
  	paramVal = paramVal.substring(paramVal.indexOf("&"), "");
  }
  return paramVal;
}

/* Get the result count. From span we get <B>1-10</B> of 100 where 100.
 */
function getHitCount() {
  var hitCount;
  if (gotHits() && document.getElementById('number_of_hits')) {
  	hitCount=document.getElementById('number_of_hits').innerHTML;
  } else hitCount = 0;
  return hitCount;
}

/* Did we make any hits in search results */
function gotHits() {
  if (document.getElementById('no_hits'))  return false;
  return true;
}

/* Identify a URL as being from the entertainment application */
function isEntertainmentUrl(url) {
  aeUrlPattern = new RegExp ('ae\.(int\.|qa\.|stg\.)?(.+?)\.com\/entertainment\/ui\/.+?\/(.+?)(Search|Results)?\.html');
  kr_hbx_aeSite = "";
  kr_hbx_aePath = "";
  if ( aeUrlPattern.test(url) ) {
    kr_hbx_aeSite = RegExp["$2"];
    kr_hbx_aePath = RegExp["$3"];
    // Translate to SDP paths.
    kr_hbx_aePath = kr_hbx_aePath.replace('restaurant', 'dining');
    kr_hbx_aePath = kr_hbx_aePath.replace('stage', 'performing_arts');
    kr_hbx_aePath = kr_hbx_aePath.replace('movie', 'movies');
    if (kr_hbx_aePath.substring(0,4) == 'misc') {
      kr_hbx_aePath = '';
    }
    return true;
  } 
  return false;
}

/* Return ForumId and PageNum, return only the params that have values */
function getForumParam() {
 var forumId = getParamValue("forumId");
 var pageNum = getParamValue("pageNo");
 var forumParam = "";
 
 if (forumId != "") forumParam = '|' + forumId;
 
 if (forumId == "" && pageNum != "") forumParam = '|PageNum=' +  pageNum;
 else if (forumId != "" && pageNum != "") forumParam = forumParam + '+-+PageNum=' + pageNum; 
 
 return forumParam;
}
