// Sets the referrer to the current value of the _tag.DCS.dcsuri
function setReferrer() {
  if (!(navigator.appName=="Microsoft Internet Explorer"&&parseInt(navigator.appVersion)<4)){
    _tag.DCS.dcsref = self.location.href.substring(0,self.location.href.indexOf(self.location.pathname)) + _tag.DCS.dcsuri;
  }  
}

// Returns the page path starting from the web root directory.
function getFolder(){
  return thisFolder = self.location.pathname.substring(0,self.location.pathname.lastIndexOf('/')+1);
}


// objectReference must be a relative path: e.g "test.pdf" or "downloads/test.pdf".
// A path outside the current directory is NOT allowed: e.g. "../download/test.pdf".
// If the referrer for a new function call should not be changed, the value of the attribute "keepLastReferrer" must be "1" or "true".
// Otherwise the attribute should not be used.
// The referrer should not be changed, if an event should be tracked without taking the User to a new page:
// e.g. when tracking a download file (*.pdf, *.zip, etc.).
/* OLD V7. 
function trackRelative(objectReference,objectTitle,keepLastReferrer){

  setReferrer();
  currentUri = DCS.dcsuri;
  if (objectReference.indexOf('?') != -1) {
    DCS.dcsqry = objectReference.substring(objectReference.indexOf('?'));
    objectReference = objectReference.substring(0, objectReference.indexOf('?'));
  }
  path = getFolder() + objectReference;
  dcsMultiTrack('DCS.dcsuri',path,'WT.ti',objectTitle);
  if (keepLastReferrer) {
    DCS.dcsuri = currentUri;
  }
}
*/
/**
V8. from BMW.at
*/
function trackRelative(objectReference,objectTitle,keepLastReferrer){
  //alert("trackRelative: objectReference:"+objectReference+" objectTitle: "+objectTitle+ " keepLastReferrer:"+keepLastReferrer);
  setReferrer();
  currentUri = _tag.DCS.dcsuri;
  if (objectReference.indexOf('?') != -1) {
    _tag.DCS.dcsqry = objectReference.substring(objectReference.indexOf('?'));
    objectReference = objectReference.substring(0, objectReference.indexOf('?'));
  }
  path = getFolder() + objectReference;
  dcsMultiTrack('DCS.dcsuri',path,'WT.ti',objectTitle);
  if (keepLastReferrer) {
    _tag.DCS.dcsuri = currentUri;
  }
  thisFolder = self.location.pathname.substring(0,self.location.pathname.lastIndexOf('/')+1);
  path = thisFolder + objectReference;
  dcsMultiTrack('DCS.dcsuri',path,'WT.ti',objectTitle);
}

// objectReference must be an absolute path starting from the web root directory: e.g "/com/en/downloads/test.pdf".
// The path must begin with a slash: e.g. "/".
// If the referrer for a new function call should not be changed, the value of the attribute "keepLastReferrer" must be "1" or "true".
// Otherwise the attribute should not be used.
// The referrer should not be changed, if an event should be tracked without taking the User to a new page:
// e.g. when tracking a download file (*.pdf, *.zip, etc.).

/* V 7.
function trackAbsolute(objectReference,objectTitle,keepLastReferrer){
  setReferrer();
  currentUri = DCS.dcsuri;
  if (objectReference.indexOf('?') != -1) {
    DCS.dcsqry = objectReference.substring(objectReference.indexOf('?'));
    objectReference = objectReference.substring(0, objectReference.indexOf('?'));
  }
  path = objectReference;
  dcsMultiTrack('DCS.dcsuri',path,'WT.ti',objectTitle);
  if (keepLastReferrer) {
    DCS.dcsuri = currentUri;
  }
}
*/

/* legacy call */
function trackAbsolute(objectReference,objectTitle,keepLastReferrer){
	//alert("trackAbsolute: objectReference:"+objectReference+" objectTitle: "+objectTitle+ " keepLastReferrer:"+keepLastReferrer);
	trackAbsolute(objectReference,objectTitle);
}

/* V8.5 from BMW.at */
function trackAbsolute(objectReference,objectTitle){
   //alert("trackAbsolute: objectReference:"+objectReference+" objectTitle: "+objectTitle);
  if(objectReference.substr(0,4) == "http") {
    objectReference = objectReference.replace(/\/\//,"");
    objectReference = objectReference.substring(objectReference.indexOf("/"));
  }

  if(objectReference.indexOf('?') != -1){
    var splitted = objectReference.split('?');
    var path = splitted[0];
    if (splitted[1].substr(splitted[1].length - 1)!="&") {
      splitted[1] += "&";
    }
    //BMW specific splitted[1] += "highbandUser=" + highbandUser;

    queryString = "?" + splitted[1];

    dcsMultiTrack('DCS.dcsuri',path,'WT.ti','-', 'DCS.dcsqry', queryString);
  }
  else {
   //BMW specific queryString = "?" + "highbandUser=" + highbandUser;
    queryString = "";
    dcsMultiTrack('DCS.dcsuri', objectReference, 'WT.ti', '-', 'DCS.dcsqry', queryString);
  }
}

// objectReference must be a URL to an external website that is not part of your website tracking: e.g "www.microsoft.com".
// The URL protocol, e.g. "http://", is NOT allowed as part of the reference.
function trackExternal(objectReference,objectTitle){
   //alert("trackExternal: objectReference:"+objectReference+" objectTitle: "+objectTitle+ " keepLastReferrer:"+keepLastReferrer);
  setReferrer();
  currentUri = _tag.DCS.dcsuri;
  if (objectReference.indexOf('?') != -1) {
    _tag.DCS.dcsqry = objectReference.substring(objectReference.indexOf('?'));
    objectReference = objectReference.substring(0, objectReference.indexOf('?'));
  }
  path = getFolder() + "external/" + objectReference;
  dcsMultiTrack('DCS.dcsuri',path,'WT.ti',objectTitle);
  _tag.DCS.dcsuri = currentUri;
}
