//********************************************************************
//
//              Copyright (c) 2002-2007 MindLab GmbH
//                     All rights reserved.
//
//====================================================================
//
// Project    : Postbank
// Filename   : pb_tracking_js.js
//
//====================================================================
//
/**
 * @file
 * This script can be used to integrate Microsites, which are not on
 * on a tracked website, in a tracking environment. 
 *
 * @author   $Felix Engemann$
 * @version  $0.0.1$
 * @date     $31.08.2006$

 *@listofchanges cha adaption for postbank help-div 2007/09/07
 *@listofchanges cha adaption for postbank clientStat trackin Pixelhelp-div 2007/11/13
 */
//
//********************************************************************




//the servername to get the pixel from (no protocol here, and
//no path here), you can append a port or prepend an authendication
//like: user:passwort@myserver.mydomain.com:12345
//where "user:password@" is the authentication
//      "myserver.mydomain.com" is the fully qualified servername
//      ":12345" is a port-Number
//donnot forget the "/" at the end!!!
//SETTING
PixelServer           = "www.postbank.de/";

//the path to request from the given server, this path could of
//course include CGI-Parameters
//example: "/mywebbug?thisismyparameter=value"
//SETTING
PixelPath             = "pbmedia/pb_leer.gif";




//this will always be appended as first Parameter "partner"
PartnerName          = "";

//Send PartnerName by default
SendPartnerName      = false;

//Send Url by default
SendUrl              = false;

//Send Referrer by default
SendReferrer         = false;


//shall we try to use cookies or rely only on some javascript-features
//
//note: IE6 completly ignores browser cookie-settings. If you do not 
//      want to use cookies set this option to false!
//
//      Some other browsers do not support writing in navigator-object
//      Know browsers are IE5 and Opera 9
//      Using these browsers you have to set this option to true       
UseCookie             = true;





//#########################################
//
// SESSION
//
//#########################################

//We will treat a session as timed out if more than this amount
//of seconds has passed between two clicks
NetMindSessionTimeout = "1800";



//********************************************************************
//Session-Management
//********************************************************************
//********************************************************************
//Session-Object 
//********************************************************************

//Constructor for Session-Object
function cSession()
{
  //this is a flag signaling if Timout has occured 
  bTimeout = false
  
  //get SessionID from URL (with -snm- -enm-)
  var strSessionIDURL="/";
  
  this.strOriginalID= strSessionIDURL.substr(1);
  this.strNakedSID = strSessionIDURL.substr(6,54);
  
  
  //slice new Session out of URL
  strNewNetMindSID = this.strNakedSID;
  
  //this is the SessionID returned at the end
  this.m_strNetMindSID = "";
  //get current Timestamp
  strNewTimestamp = strNewNetMindSID.substr(44);
  //m_strOldTimestamp is evaluated later from cookie or navigator-object
  strOldTimestamp = "";
  //are cookies enabled ?
  bIsCookieEnabled = ((navigator.cookieEnabled) && UseCookie);
  //m_strOldNetMindSID is evaluated later from cookie or navigator-object
  strOldNetMindSID = "";
  

  //get SID from cookie if enabled and allowed by UseCookie 
  if ((bIsCookieEnabled) && (document.cookie.length > 0))
  {
    // get cookie string
    strCookie = document.cookie

    //split into cookies
    var arCookies = strCookie.split("; ");
    //loop through cookies and get NetMind-Cookie
    var nLoopCount = 0;
    var strCookieSID = "";
    var strActCookie = arCookies[nLoopCount];
    var arActCookie = strActCookie.split("=");
    var strActCookieName = arActCookie[0];
    while ((strActCookieName != "NETMIND_SID") && (nLoopCount+1 < arCookies.length)) {
      nLoopCount++;
      strActCookie = arCookies[nLoopCount];
      arActCookie = strActCookie.split("=");
      strActCookieName = arActCookie[0];
    }//end while ((strActCookieName != "NETMIND_SID") && (nLoopCount < arCookies.length))

    //Extract SID from cookie if there was a cookie
    if (strActCookieName == "NETMIND_SID") {
      strCookieSID = arActCookie[1].substring(0,54);
      strOldURL = arActCookie[1].substring(43);
    }//end if (strActCookieName == "NETMIND_SID")
    strOldNetMindSID = strCookieSID;
  }//end if (... we use a cookie ...)

  //try to get SessionID from navigator-object 
  else
  {
    // if navigator-object is defined then read navigator-object ?
    if (navigator.NetMindSID != null)
    {
      //read SessionID from navigator-object
      strOldNetMindSID = navigator.NetMindSID; 
    }//end if (navigator.NetMindSID != null)
  }//end else (... we use a cookie ...) 
  
  //do we have a SessionID ?
  if (strOldNetMindSID != "")
  {
    //ok we have a SessionID
    //evaluate Timestamp from m_strOldNetMindSID
    strOldTimestamp = strOldNetMindSID.substr(44);
    //if evaluated Timestamp is timed out set a new one
    if (eval(strOldTimestamp) + eval(NetMindSessionTimeout) < eval(strNewTimestamp))
    { 
      strOldNetMindSID = "";
      //bTimeout = true; 
    }//end if (... timed out ...)
    else
    {
      //we should use the new timestamp with our active Session!
      this.m_strNetMindSID = strOldNetMindSID.substr(0,43) + "-" + strNewTimestamp;
    }//end else (... timed out ...)
  }//end if (strOldNetMindSID != "")
  
  //we didn't find a sessionID or we discarded it because of timeout
  if (strOldNetMindSID == "")
  {
    //ok we use the new one :-)
    this.m_strNetMindSID = strNewNetMindSID;
  }//end if (strOldNetMindSID == "")  

  //push SessionID in cookie if enabled
  if (bIsCookieEnabled)
  {
    //push SessionID in cookie
    document.cookie = "NETMIND_SID="+this.m_strNetMindSID+"; path=/";
  }
  else
  {
    //cookies not enabled or not allowed push in navigator-object
    navigator.NetMindSID = this.m_strNetMindSID;
  }//end else 

}//end function cSession()


//method for object Session: returns the Session ID 
function session_getSessionID()
{
  return this.m_strNetMindSID;
}//end function session_getSessionID()

//method for object Session: returns the Session ID with old timestamp and without "/" 
function session_getOriginalSessionID()
{
  return this.strOriginalID;
}//end function session_getSessionID()

//method for object Session: returns the Session ID with old timestamp and without "/" and without -snm--enm-
function session_getNakedSessionID()
{
  return this.strNakedSID;
}//end function session_getSessionID()



//add methods to class cSession 
cSession.prototype.getSID = session_getSessionID;

//add methods to class cSession 
cSession.prototype.getOriginalSID = session_getOriginalSessionID;

//add methods to class cSession 
cSession.prototype.getNakedSID = session_getNakedSessionID;


//********************************************************************
//Session-instance creation
//********************************************************************
//oNetMindSession = new cSession();


//#########################################
//
// END SESSION
//
//#########################################





//#########################################
//
// CLIENTSTAT
//
//#########################################

//********************************************************************
//Pixel-Object
//********************************************************************
//Contstructor for Pixel-Object
function cPixel()
{
  //pixel object constructor
  this.m_oImage = new Image();
  this.m_arParameterList = new Array();
  this.m_strParameterString = "";
  this.m_nParameterCount = 0;
  
  //get the Protocoll from the current location
  this.nEndOfProtocoll = location.href.indexOf("://");
  this.strProtocoll = location.href.substr(0, this.nEndOfProtocoll);
 
  this.m_strPixelURLPrefix = this.strProtocoll+"://"+PixelServer;
  
  this.m_strPixelURLPrefix += "-snm-";
  
  
  if (PixelPath.charAt(0) == "/")
  {
    this.m_strPixelURLSuffix = "-enm-"+PixelPath.substr(1);
  }//end if (PixelPath.charAt(0) == "/")
  else
  {
    this.m_strPixelURLSuffix = "-enm-"+PixelPath; 
  }//end else (PixelPath.charAt(0) == "/")
  
  //make sure we use the right concatenation-Char 
  //for appending our parameters
  if (PixelPath.indexOf("?") != -1)
  {
    this.m_cConcatChar = "&";
  }//end if (PixelPath.indexOf("?") != -1)
  else
  {
    this.m_cConcatChar = "?";
  }//end else (PixelPath.indexOf("?") != -1)

}//ENd function cPixel()

//methode fuer Objekt Pixel: Fuegt einen Parameter hinzu
function pixel_addParameter(key, value)
{
  arPair = new Array(key, value);
  bfound = false;
  if (this.m_arParameterList.length > 0) 
  {
    for (i=0; i < this.m_arParameterList.length; i++)
      {
        if (this.m_arParameterList[i][0] == key)
        {
          bfound = true;
          this.m_arParameterList[i] = arPair;
        }   
      }
    }
   if (bfound == false) 
   {
     this.m_arParameterList[this.m_nParameterCount] = arPair;
     this.m_nParameterCount++;
   }
}//end function pixel_add_parameter()

//methode fuer Objekt Pixel: Fuegt einen Parameterstring hinzu
function pixel_addParameterString(strParameters)
{

  if (this.m_strParameterString == "") 
  {
	this.m_strParameterString = strParameters;
	
  }
  else{
  
  		this.m_strParameterString = this.m_strParameterString+"&"+strParameters;
  }


}//end function pixel_add_parameterstring()


//method for object Pixel: add affiliate
function pixel_addAffiliate(name)
{
  this.addParameter("affiliate", name)  
}//end function pixel_addAffiliate(name)

//method for object Pixel: add url
function pixel_addUrl(url)
{
  this.addParameter("url", url);
}//end function pixel_addAffiliate(name)

//method for object Pixel: add referrer
function pixel_addReferrer(referrer)
{
  this.addParameter("referrer", referrer);
}//end function pixel_addAffiliate(name)



//methode for Object Pixel: send the Pixel
function pixel_sendPixel(oSession)
{

  var strSID = oSession.getSID();
  
  
  var strURL = this.m_strPixelURLPrefix+strSID+this.m_strPixelURLSuffix;


  
  for(i=0; i<this.m_arParameterList.length; i++)
  {

	strURL = strURL + this.m_cConcatChar + escape(this.m_arParameterList[i][0]) + "=" + escape(this.m_arParameterList[i][1]);    

  	if(i == 0){
		this.m_cConcatChar = "&";
  	}
    
    
  }//end for(i=0; i<this.m_arParameterList.length; i++)
  


  if (this.m_strParameterString != "") 
  {
  
	strURL = strURL + this.m_cConcatChar + this.m_strParameterString;
	
  }

  //now set URL on Pixel
  this.m_oImage.src=strURL;
  
  
}//end function pixel_sendPixel()

//methoden zur Klasse hinzufuegen
cPixel.prototype.addParameter = pixel_addParameter;
cPixel.prototype.addParameterString = pixel_addParameterString;
cPixel.prototype.addAffiliate = pixel_addAffiliate;
cPixel.prototype.addUrl       = pixel_addUrl;
cPixel.prototype.addReferrer  = pixel_addReferrer;
cPixel.prototype.sendPixel    = pixel_sendPixel;





function getSessionIDFromTracker()
{
     //   var strSessionIDURL="/";
      //  return strSessionIDURL.substring(1);
}











//#########################################
//
// ADDSIDTOURL
//
//#########################################

//we need to specify the domains, that are tracked or not. 
//The urls of tracked domains are enriched by a netmind-session ID, the others not. 
//If the url is relative, the netmind-session ID is included by default (because the 
//target must be tracked).

//The modified or not modified Url is returned.




function addSidToURL(strURL, oSession) {	

//whitelist for domain specification. 
//You donnot need to !
//All Urls are checked, if they start with one of those Strings
//SETTING
var aWhiteList		= new Array (
					"http://www.postbank.de",
					"http://www.postbank.com",
					"http://www.dslbank.de",
					"http://www.dsl-bank.de",
					"https://produktabschluss.postbank.de",
					"https://produktverkauf.postbank.de",
					"http://localhost"
					);
					



	oSession.getSID();

  var strSessionIDURL="/";
  
  var strSessionID = strSessionIDURL.substring(1);
    
  var y = strURL.lastIndexOf ("-enm-");
  	
  	
  if (y == -1) 
  {
    var x = strURL.indexOf ("://");		
    
    //Protocoll in the url
    //do we have a Protocol-Section (http or something like that)?
    if (x != -1) 
    {
      //we have a protocol
	  //so check the whitelist

		bNonefound = true;
		
		//check if Url starts with one of the whitelist domains
  		for(var i = 0; i < aWhiteList.length; i++){
	  		var nStartsWith = strURL.indexOf (aWhiteList[i]);	
	  	
	  		//we got a hit and include the id 
			if( nStartsWith != -1){
				
				//we got a hit
				bNonefound = false;
				
			    var x2 = strURL.indexOf ("://");	
	  	
	  	    	var strPre = strURL.substring (0, x2 + 3);
     			var strPost = strURL.substring (x2 + 3, strURL.length);

				var i = strPost.indexOf('/');
      
				//only host or host and path?
				if (i != -1)
				{
					//host and path, so insert sid
					return strPre + strPost.substring(0, i+1)+strSessionID+strPost.substring(i+1);
      
      			}//end if (i != -1)
			    else
      			{
        			//only host, so append sid at end
			        return strPre + strPost + "/" + strSessionIDURL;
      			}//end else ( i != -1 )
   			 }//end if (x != -1)
   		
	  		
	  	}//end for  var i = 0; i < aWhiteList.length; i++)
	  	
	  	if (bNonefound){
	   			 return strURL;
   			 }
	  		
	  	
	}
	
  
  	
  	


    
    //No Protocoll in the url
    //the id is included 
    else 
    {
      //we don't have a protocol
      
      //search for a "/"
      var i = strURL.indexOf('/');
      if (i==-1) 
      {
        //no path, so just put it in front of the file/url
        return strSessionID+strURL;		
      }//end if (i==-1)
      else
      {
        //there is a path, so insert it directly after first "/"
        return strURL.substring(0, i+1)+strSessionID+strURL.substring(i+1);
      }//end else (i==-1)
    } //end else if
  
  
  //there is already an ID
  }//end if (y == -1)
  
  else 
  {
    return strURL.substring(0, y+5)+strSessionID+strURL.substring(y+5);
    
  }//end else (y == -1)


}//end addSidToUrl()

//#########################################
//
// END ADDSIDTOURL
//
//#########################################


