
/* $File: //depot/DocRoot/js/kr_hbx_lib.js $
 * Library used for HitBox implementation
 */

/* 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;
}

// Separate function to produce hbx.mlc variable for partner (cobrand) pages.
function getHbxPartnerMLC (hbxPath) {
 var mlc = hbxPath;
 // Strip out any special characters from both local and global mlc.
  mlc = mlc.substring(mlc.indexOf('/'));
  mlc = stripHbxRestSpChar(mlc);
 // mlc for global tracking is same as regular mlc in the case of cobrands.
 var globalMLC = mlc;

 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 */
/* strips all special charaters for title(/article id) */
function stripHbxSpChar(str){
   str = str.split("|").join("");
   str = stripHbxRestSpChar(str);
   str = str.split("/").join("-");
   return str;
}
/* partner_group keeps "|" in order to get 2nd dimension */
function stripHbxRestSpChar(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("+");

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