/*
* Relevancy Usage Monitor Logging for BfS
* Version: 0.3
* Date: 2008-03-17
*/

/*
*  Start Configuration
*/
var sep = '::';
var RUMUrlPrefix = 'http://www.nutzungsmonitor.com/bfs-log/';
// either configure the session cookie name or the session get parameter name
var sessionCookieName = 'JSESSIONID';
var sessionParam = '';
// url parameters 
// configure the names of the url parameters if they exists or leave them empty otherwise
var queryIdParam = '';
var queryParam = 'queryString';
var restrictionParam = 'site';
var pageParam = '';
var typeParam = 'mode'; 
var nofParam = '';
var docIdParam = '';
var rankParam = '';
var collectionIdParam = ''; 
var mimeParam = '';
var docLengthParam = '';
var renderTypeParam = '';
/*
*  End Configuration
*/

_d = document;
_dl = _d.location;
_dls = _dl.search;

// sessionId
if (sessionCookieName && sessionCookieName != '') sessionId = _readCookie(sessionCookieName);
else if (sessionParam && sessionParam !='') sessionId = _getParamValue(_dls, sessionParam+"=", "&", '');
else sessionId = '';
if (sessionId == null) /* if cookies are disabled, the session ID is put into the URL */
{
   var strHref = window.location.href;
   if (strHref != null) {
      var i = strHref.indexOf("id=");
      if (i > 0) sessionId = strHref.substr(i + 3);
   }
}
// queryString
if (queryParam && queryParam != '') queryString = _getParamValue(_dls, queryParam+"=", "&", ''); //query + '======';
else queryString = '';
// queryId
if (queryIdParam && queryIdParam != '') queryId = _getParamValue(_dls, queryIdParam+"=", "&", _str2hex(queryString));
else queryId = _str2hex(queryString);
// queryRestriction
if (restrictionParam && restrictionParam != '') queryRestriction = _getParamValue(_dls, restrictionParam+"=", "&", '-');
else queryRestriction = '-';
if (queryRestriction == 'all') queryRestriction = '-';
// searchPage
if (pageParam && pageParam != '') searchPage = _getParamValue(_dls, pageParam+"=", "&", 1);
else searchPage = 1;
// queryType
if (typeParam && typeParam != '') queryType = _getParamValue(_dls, typeParam+"=", "&", 'simple_search'); 
else queryType = 'simple_search';

function PageLog(nofRanks, quer, restrict) {
  // pagelog
  // <sessionId>::<queryId>::<queryString>::<queryRestriction>::<searchPage>::<queryType>::<nofRanks>::<emptyField>::<cookieValues>

  quer = quer.replace(/\+/g, " "); 
  queryString = unescape(unescape(quer)) + "======";
  if (queryIdParam && queryIdParam != '') queryId = _getParamValue(_dls, queryIdParam+"=", "&", _str2hex(queryString));
  else queryId = _str2hex(queryString);
  if (restrict != '') {
     queryType = 'advanced_search';
     queryRestriction = restict;
  }
  var pageparam = sessionId + sep + queryId + sep + queryString + sep + queryRestriction + sep + searchPage + sep + queryType + sep + nofRanks + sep + '-' + sep + '-';
//  alert('querystring='+queryString);
  purl = RUMUrlPrefix+"pagelog.php?v="+escape(pageparam).replace(/\+/g, "%2b");
//  alert('url='+purl);
  if (_d.images) {
    i = new Image(1,1);
    // uncomment the following lines to enable logging
    i.src=purl;
    i.onload=function() {_uVoid();}
  }
  return true;
}

function DocumentLog(rank,docid) {
  // documentId
  if (docIdParam && docIdParam != '') documentId =  _getParamValue(_dls, docIdParam+"=", "&", docid); 
  else documentId = docid;
  var ii = documentId.indexOf(";jsession"); //if cookies disabled
  if (ii > 0) documentId = documentId.substr(0, ii);
  // 
  if (collectionIdParam && collectionIdParam != '') collectionId =  _getParamValue(_dls, collectionIdParam+"=", "&", '-'); 
  else collectionId = '-';
  // rank
  if (rankParam && rankParam != '') rank =  _getParamValue(_dls, rankParam+"=", "&", rank); 
  else rank = rank;

  // documentlog
  // <sessionId>::<queryId>::<documentId>::<rank>::<collectionId>::<mimeType>::<docLength>::<renderType>
  var docparam = sessionId + sep + queryId + sep + documentId + sep + rank + sep + '-' + sep + '-' + sep + '-' + sep + '-';
  durl = RUMUrlPrefix+"documentlog.php?v="+escape(docparam);
  //alert(durl);
  if (_d.images) {
    i2 = new Image(1,1);
    // uncomment the following lines to enable logging
    i2.src=durl;
    i2.onload=function() {_void();}
  }
  return true;
}

function _void() { return; }

/*
* Returns the value of a cookie or null.
* @param name The name of the cookie
*/
function _readCookie(name) {
        var nameEQ = name + "=";
        var ca = _d.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
}

/*
* Converts a string to hex.
* @param s The string to convert to hex
*/
function _str2hex(s) {
   var a,b,d;
   var hexStr = '';
   for (var i=0; i < s.length; i++) {
      d = s.charCodeAt(i);
      a = d % 16;
      b = (d - a)/16;
      hexStr += "0123456789abcdef".charAt(b) + "0123456789abcdef".charAt(a);
   }
   return hexStr;
}

function _getParamValue(str,param,sep,def) {
  if (!str || str=="" || !param || param=="" || !sep || sep=="") return def;
  var i,i2,i3,c=def;
  i=str.indexOf(param);
  i3=param.indexOf("=")+1;
  if (i > -1) {
    i2=str.indexOf(sep,i); if (i2 < 0) { i2=str.length; }
    c=str.substring((i+i3),i2);
  }
  return c;
}

