//******************************************************************************************
//
//              Copyright (c) 2002-2008 MindLab GmbH
//                     All rights reserved.
//
//==========================================================================================
//
// Project    : netmind techaddon
// Filename   : nm_tr_cookie.js
//
//==========================================================================================
//
/**
 * @file
 * This script determines technical user data und sends data with pixel
 *
 * @author   $Alexander Schreiber$
 * @version  $Name: V1_0_0 $
 * @date     $Date: 2009-03-17 09:38:31 $
 *

 * $Log: nm_tr_cookie.js,v $
 * Revision 1.4  2009-03-17 09:38:31  pohl
 * Added per-plugin configuration
 * Added configuration class
 *
 * Revision 1.3  2008-10-01 11:53:53  pohl
 * *** empty log message ***
 *
 * Revision 1.2  2008-10-01 11:43:41  pohl
 * Fixed IE7 problem with MediaPlayer plugin
 * 
 */
//
//*****************************************************************************************


//********************************************************************
//configuration
//********************************************************************

//********************************************************************
//configuration
//********************************************************************


function cConfigTechAddon()
{
    //set to true, if you want to get the params automatically
    this.m_bAutoparams = true;

    //set to true, if you want to send the pixel automatically
    this.m_bAutosend = true;

    //Object to configure the browser plugins to be detected:
    this.m_oTechPlugins = new Object();
    this.m_oTechPlugins['Director'] = false;
    this.m_oTechPlugins['MediaPlayer'] = false;
    this.m_oTechPlugins['Flash'] = true;
    this.m_oTechPlugins['RealPlayer'] = false;
    this.m_oTechPlugins['AcroRead'] = false;
}

//==========================================================
// getSendTechPlugin
// strPlugin = 'Director', 'MediaPlayer', 'Flash', 
//             'RealPlayer', 'AcroRead'
//==========================================================
function configTech_getSendTechPlugin( strPlugin )
{
    if ( this.m_oTechPlugins )
    {
        return this.m_oTechPlugins[strPlugin];
    }
    return false;
}

cConfigTechAddon.prototype.getSendTechPlugin     = configTech_getSendTechPlugin;

//********************************************************************
//Configuration-Instance
//********************************************************************
oTechConfig = new cConfigTechAddon();


//********************************************************************
//cookie handling
//********************************************************************

//constructor cookie object
function cCookie() {
  this.m_arParameterList = new Array();
  this.m_nParameterCount = 0;
}

//methode fuer Objekt Cookie: Fuegt einen Parameter hinzu
function cookie_addParameter(key, value)
{
  var arPair = new Array(key, value);
  this.m_arParameterList[this.m_nParameterCount] = arPair;
  this.m_nParameterCount++;
}//end function pixel_add_parameter()

//methode für Objekt Cookie: setzt cookies
function cookie_sendData() {

  var strActCookie;
  var arActCookie;
  var strActCookieName;
  
  var bFound = false;
  // get cookie string
  var strCookie = document.cookie
  //split into cookies
  var arCookies = strCookie.split("; ");
  

  //every parameter will be saved in seperate cookie
  for(i=0; i<this.m_arParameterList.length; i++) {
  //loop through cookies and get NetMind-Cookie
    for (j=0; j < arCookies.length; j++) {
      strActCookie = arCookies[j];
      arActCookie = strActCookie.split("=");
      strActCookieName = arActCookie[0];
	
      //if cookie already exists, it will not be saved
      if (strActCookieName == this.m_arParameterList[i][0]) {
        bFound = true;
      	break;
      }//end if (strActCookieName == this.m_arParameterList[i][0])
      
      else bFound = false;
    }//end for (j=0; j < arCookies.length; j++)
      
    //cookie not found, it will be saved
    if(!bFound) {
      document.cookie = "" + this.m_arParameterList[i][0] + "="+escape(this.m_arParameterList[i][1])+"; path=/;";  
    }//end if(!bFound)      
  }//end for(i=0; i<arParameterList.length; i++)

}//end function cookies_sendData()

//methode objekt cookie: liest techdaten aus
function cookie_getTechData () {

  //--------------------------------------------------------
  // variables for general settings
  //--------------------------------------------------------
  var javaOK       = "unknown";
  var cookiesOK    = "unknown";
  var browsLang    = "unknown";
  var availheight  = screen.availHeight;
  var availwidth   = screen.availWidth;
  var colordepth   = screen.colorDepth + " bit";
  var height       = screen.height;
  var width        = screen.width;
  var title        = document.title;
  
  //--------------------------------------------------------
  // variables for plugins
  //--------------------------------------------------------
  var flashPlugin      = "-";
  var flashVersion     = "0";
  var directorPlugin   = "-";
  var wmplayerPlugin   = "-";
  var realplayerPlugin = "-";
  var quicktimePlugin  = "-";
  var adobe_svgPlugin  = "-";
  var adobe_pdfPlugin  = "-";
  
  var agt = navigator.userAgent.toLowerCase();
  var ie  = (agt.indexOf("msie") != -1);
  //accept only higher than MSIE 4
  //otherwise the detectIE() function would not work
  if(ie){
  	ie  = (agt.indexOf("msie 4") == -1) && (agt.indexOf("msie 3") == -1);
  	ie7 = (agt.indexOf("msie 7") > -1);
  }//end if(ie)
  
  var win = ((agt.indexOf("win")!=-1) || (agt.indexOf("32bit")!=-1));
    
  //--------------------------------------------------------
  // Java and Cookies
  //--------------------------------------------------------
  if (navigator.javaEnabled()==true){javaOK="yes"}else{javaOK="no"}
  
  //--------------------------------------------------------
  //-- cookieEnabled only works in MSIE.
  //--------------------------------------------------------
  if ((navigator.cookieEnabled) && (navigator.cookieEnabled==true)) {cookiesOK="yes"} else {cookiesOK="no"}
  
  //--------------------------------------------------------
  // language
  //--------------------------------------------------------
  if (navigator.language) browsLang = navigator.language;
  
  //--------------------------------------------------------
  // Plugins
  // 
  // SVG and Quicktime are not certified Plugins and cause a warning
  // in MSIE 7. Because of this fact those two plugins are not detected.
  //--------------------------------------------------------
  if (ie && win) {
  
    if ( oTechConfig.getSendTechPlugin('Director') )
    {
        directorPlugin = detectIE("SWCtl.SWCtl.1");
    }
    if ( oTechConfig.getSendTechPlugin('Flash') ) 
    {
        flashPlugin = detectIE("ShockwaveFlash.ShockwaveFlash.1");
    }
    if ( oTechConfig.getSendTechPlugin('RealPlayer') )
    {
        realplayerPlugin = detectIE("rmocx.RealPlayer G2 Control.1");
    }

    if ( oTechConfig.getSendTechPlugin('MediaPlayer') )
    {
        wmplayerPlugin = detectIE("MediaPlayer.MediaPlayer.1"); 
    }
    if ( oTechConfig.getSendTechPlugin('AcroRead') ) 
    {
        adobe_pdfPlugin = detectIEAcrobat("Acrobat Reader");
    }     
  }//end if (ie && win) 

  else {
    nse = ""; 
    for (var i=0;i<navigator.mimeTypes.length;i++) nse += navigator.mimeTypes[i].type.toLowerCase();

    if ( oTechConfig.getSendTechPlugin('Director') )
    {
        directorPlugin = detectNS(nse, "application/x-director");
    }
    if ( oTechConfig.getSendTechPlugin('Flash') )
    {
        flashPlugin = detectNS(nse, "application/x-shockwave-flash");
    }
    if ( oTechConfig.getSendTechPlugin('RealPlayer') )
    {
        realplayerPlugin = detectNS(nse, "audio/x-pn-realaudio-plugin");
    }
    if ( oTechConfig.getSendTechPlugin('MediaPlayer') )
    {
        wmplayerPlugin = detectNS(nse, "application/x-mplayer2");     
    }
    if ( oTechConfig.getSendTechPlugin('AcroRead') )
    {
        adobe_pdfPlugin = detectNS(nse, "application/pdf");  
    }
  }//end else

  // Save techdata
  this.addParameter("nm_java", javaOK);
  this.addParameter("nm_cookies", cookiesOK);
  this.addParameter("nm_screen", width + "x" + height);
  this.addParameter("nm_colordepth", colordepth);
  this.addParameter("nm_plugin_flash", flashPlugin);
  this.addParameter("nm_plugin_director", directorPlugin);
  this.addParameter("nm_plugin_wmplayer", wmplayerPlugin);
  this.addParameter("nm_plugin_realplayer", realplayerPlugin);
  this.addParameter("nm_plugin_acroread", adobe_pdfPlugin);
}//end function cookie_getTechData ()


//methoden zur Klasse hinzufuegen
cCookie.prototype.addParameter = cookie_addParameter;
cCookie.prototype.sendData    = cookie_sendData;
cCookie.prototype.getTechData  = cookie_getTechData;



//*****************************************************************************************
//
/**
 * checks for installed acrobat plugins
 */
//
//*****************************************************************************************

function detectIEAcrobat(name) {

   var acrobat=new Object();

   if (window.ActiveXObject) {
     for (x=2; x<10; x++) {
       try {
         oAcro=eval("new ActiveXObject('PDF.PdfCtrl."+x+"');");
         if (oAcro) {
           return 'yes';
         }//end if (oAcro)
       }// try
       catch(e) {}
    }//end for (x=2; x<10; x++)

    try {
      oAcro4=new ActiveXObject('PDF.PdfCtrl.1');
      if (oAcro4) {
        return 'yes';
      }//end if (oAcro4)
    }//end try
    catch(e) {}

    try {
      oAcro7=new ActiveXObject('AcroPDF.PDF.1');
      if (oAcro7) {
        return 'yes';
      }//end if (oAcro7)
    }//end try
    catch(e) {}
  }//end if (window.ActiveXObject)

  return 'no';	
}//end function detectIEAcrobat(name)

//*****************************************************************************************
//
/**
 * checks for installed IE plugins
 */
//
//*****************************************************************************************

function detectIE(ClassID) { 
  try{
    new ActiveXObject(ClassID);
    return 'yes'; 
  }//end try
  
  catch(e) {
    return 'no';
  }//end catch(e)   
}//end function detectIE(ClassID)

//*****************************************************************************************
//
/**
 * checks for installed Firefox plugins
 */
//
//*****************************************************************************************

function detectNS(strMimetypes, ClassID) { 
  if (strMimetypes.indexOf(ClassID) != -1) 
    if (navigator.mimeTypes[ClassID].enabledPlugin != null) 
      return 'yes'; 
  return 'no';
}//end function detectNS(strMimetypes, ClassID)


//**********************************************************
// initialisation and starting function
//**********************************************************

oNMTechCookie = new cCookie();

if(oTechConfig.m_bAutoparams) {
  oNMTechCookie.getTechData();
}

if(oTechConfig.m_bAutosend) {
  oNMTechCookie.sendData();
}

