/**
 * pageLogger
 *
 * This source is part of pageLogger. pageLogger is free software;
 * you can redistribute it and/or modify it under the terms of the 
 * GNU General Public License as published by the Free Software 
 * Foundation; either version 3 of the License, or any later version.
 *
 * The GNU General Public License can be found at
 * http://www.gnu.org/copyleft/gpl.html.
 * A copy is found in the textfile
 * pageLogger/licenses/pageLogger/License.txt
 *
 * @category   pageLogger
 * @package    pageLogger_base
 * @license    http://www.gnu.org/copyleft/gpl.html  GPL
 */
 
function _my_stat_write(sitename, installdir, trackname) {
	var winref=self.document.referrer;
	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	try {
		if (self!=top) {
			var wintref=top.location.href;
			if(wintref==winref) {
				winref=top.document.referrer;
			}
		}
		var bsuniquevid=_my_stat_getCookie("bsuniquevid");
		if(bsuniquevid == null) {
			_my_stat_setCookie("bsuniquevid",0,0,"/","",0);
			bsuniquevid=_my_stat_getCookie("bsuniquevid");
			if(bsuniquevid == null){
				bsuniquevid = "";
			}
		}
		var randnum=Math.random()*1000000000000000;
		var final_query = 	installdir + "/connector.php?pn="+ escape(sPage)+
		"&fp="+escape(self.location.href)+
		"&ref="+escape(winref)+
		"&websitename="+escape(sitename)+
		"&logitmode=ALWAYS&trackname="+((trackname!= null) ? escape(trackname) : "DETECT")+"&trackmode=DYNAMIC&bsuniquevid="+
		escape(bsuniquevid)+"&randnum="+randnum+
		"&mode=JSMODE"+_my_stat_getData(sitename, installdir);
		document.write("<img width='1' height='1' alt='"+randnum+"' border='0' src='"+final_query+"'>");
	} catch (e) {
		
	}


}

function _my_stat_getData(sitename,installdir) {
	// step #1: set some variables to FALSE
	java 	    = "FALSE";
	cookies     = "FALSE";
	flash 	    = "FALSE";
	mediaplayer = "FALSE";
	shockwave   = "FALSE";
	realplayer  = "FALSE";
	quicktime   = "FALSE";
	acrobat     = "FALSE";
	svg 	    = "FALSE";

	// step #2: detect colordepth
	visColordepth = screen.colorDepth;

	// step #3: detect screen resolution
	visScreenX = window.screen.width;
	visScreenY = window.screen.height;

	// step #4: detect cookie-support
	if (window.navigator.cookieEnabled) cookies = "TRUE"; else cookies = "FALSE";

	// step #5: detect browser and version
	var visUserAgent = navigator.userAgent.toLowerCase();

	// browser engine name
	isGecko       = (visUserAgent.indexOf('gecko') != -1 && visUserAgent.indexOf('safari') == -1);
	isAppleWebKit = (visUserAgent.indexOf('applewebkit') != -1);
	isKonqueror   = (visUserAgent.indexOf('konqueror') != -1);
	isSafari      = (visUserAgent.indexOf('safari') != - 1);
	isOmniweb     = (visUserAgent.indexOf('omniweb') != - 1);
	isOpera       = (visUserAgent.indexOf('opera') != -1);
	isIcab        = (visUserAgent.indexOf('icab') != -1);
	isAol         = (visUserAgent.indexOf('aol') != -1);
	isIE          = (visUserAgent.indexOf('msie') != -1 && !isOpera && (visUserAgent.indexOf('webtv') == -1) );
	isMozilla     = (isGecko && visUserAgent.indexOf('gecko/') + 14 == visUserAgent.length);
	isFirebird    = (visUserAgent.indexOf('firebird/') != -1);
	isFirefox     = (visUserAgent.indexOf('firebird/') != -1);
	isNS          = ((isGecko) ? (visUserAgent.indexOf('netscape') != -1) : ( (visUserAgent.indexOf('mozilla') != -1) && !isOpera && !isSafari && (visUserAgent.indexOf('spoofer') == -1) && (visUserAgent.indexOf('compatible') == -1) && (visUserAgent.indexOf('webtv') == -1) && (visUserAgent.indexOf('hotjava') == -1) ) );
	isWin 	      = ((visUserAgent.indexOf('win')!=-1) || (visUserAgent.indexOf('32bit')!=-1));

	// browser version number
	geckoVersion = ( (isGecko) ? visUserAgent.substring( (visUserAgent.lastIndexOf('gecko/') + 6), (visUserAgent.lastIndexOf('gecko/') + 14) ) : -1 );
	equivalentMozilla = ( (isGecko) ? parseFloat( visUserAgent.substring( visUserAgent.indexOf('rv:') + 3 ) ) : -1 );
	appleWebKitVersion = ( (isAppleWebKit) ? parseFloat( visUserAgent.substring( visUserAgent.indexOf('applewebkit/') + 12) ) : -1 );

	// browser version
	versionMinor = parseFloat(navigator.appVersion);
	if (isGecko && !isMozilla){versionMinor = parseFloat( visUserAgent.substring( visUserAgent.indexOf('/', visUserAgent.indexOf('gecko/') + 6) + 1 ) );}
	else if (isMozilla) {versionMinor = parseFloat( visUserAgent.substring( visUserAgent.indexOf('rv:') + 3 ) );}
	else if (isIE && versionMinor >= 4) {versionMinor = parseFloat( visUserAgent.substring( visUserAgent.indexOf('msie ') + 5 ) );}
	else if (isKonqueror) {versionMinor = parseFloat( visUserAgent.substring( visUserAgent.indexOf('konqueror/') + 10 ) );}
	else if (isSafari) {versionMinor = parseFloat( visUserAgent.substring( visUserAgent.lastIndexOf('safari/') + 7 ) );}
	else if (isOmniweb) {versionMinor = parseFloat( visUserAgent.substring( visUserAgent.lastIndexOf('omniweb/') + 8 ) );}
	else if (isOpera) {versionMinor = parseFloat( visUserAgent.substring( visUserAgent.indexOf('opera') + 6 ) );}
	else if (isIcab) {versionMinor = parseFloat( visUserAgent.substring( visUserAgent.indexOf('icab') + 5 ) );}
	versionMajor = parseInt(versionMinor);

	// dom support
	isDOM1 = (document.getElementById);
	isDOM2Event = (document.addEventListener && document.removeEventListener);

	// step #8: detect plugins
	if (isIE && isWin) 
	{ 
	pluginlist = _my_stat_detectIE("Adobe.SVGCtl","SVG Viewer") + _my_stat_detectIE("SWCtl.SWCtl.1","Shockwave Director") + _my_stat_detectIE("ShockwaveFlash.ShockwaveFlash.1","Shockwave Flash") + _my_stat_detectIE("rmocx.RealPlayer G2 Control.1","RealPlayer") + _my_stat_detectIE("QuickTimeCheckObject.QuickTimeCheck.1","QuickTime") + _my_stat_detectIE("MediaPlayer.MediaPlayer.1","Windows Media Player") + _my_stat_detectIE("Pdf.PdfCtrl.5","Acrobat Reader"); }
	else if(isNS || !isWin) {nse = ""; for (var i=0;i<navigator.mimeTypes.length;i++) nse += navigator.mimeTypes[i].type.toLowerCase();
	pluginlist = _my_stat_detectNS("image/svg-xml","SVG Viewer") + _my_stat_detectNS("application/x-director","Shockwave Director") + _my_stat_detectNS("application/x-shockwave-flash","Shockwave Flash") + _my_stat_detectNS("audio/x-pn-realaudio-plugin","RealPlayer") + _my_stat_detectNS("video/quicktime","QuickTime") + 
	_my_stat_detectNS("application/x-mplayer2","Windows MediaA Player") + _my_stat_detectNS("application/pdf","Acrobat Reader");}
	else { nse = ""; for (var i=0;i<navigator.mimeTypes.length;i++) nse += navigator.mimeTypes[i].type.toLowerCase();
	pluginlist = _my_stat_detectNS("image/svg-xml","SVG Viewer") + _my_stat_detectNS("application/x-director","Shockwave Director") + _my_stat_detectNS("application/x-shockwave-flash","Shockwave Flash") + _my_stat_detectNS("audio/x-pn-realaudio-plugin","RealPlayer") + _my_stat_detectNS("video/quicktime","QuickTime") + _my_stat_detectNS("application/x-mplayer2","Windows MediaA Player") + _my_stat_detectNS("application/pdf","Acrobat Reader"); }


	function _my_stat_detectIE(ClassID,name)
	{
		result = false;
		
	
		var hasPlugin = WM_easyDetect(name);
        if(hasPlugin) 
		{
              
			   return name+',';
        } 
	    else 
	    {
	   
              return '';
        }
		
		
		
		
		
		//document.writeln('<SCRIPT LANGUAGE=VBScript>');
		//document.writeln('on error resume next');
		//document.writeln('If ScriptEngineMajorVersion >= 2 then');
		//document.writeln('result = False');
		//document.writeln('result = IsObject(CreateObject("' + ClassID + '"))');
		//document.writeln('If (err) then');
		//document.writeln('result = False');
		//document.writeln('End If');
		//document.writeln('Else');
		//document.writeln('result = False');
		//document.writeln('End If');
		//document.writeln('</SCRIPT>');
		if (result) return name+','; else return '';
	}

	function _my_stat_detectNS(ClassID,name)
	{
		n = "";
		
		if (nse.indexOf(ClassID) != -1) if (navigator.mimeTypes[ClassID].enabledPlugin != null) n = name+","; return n;
	}
	
	
	
	
	
	

	pluginlist += navigator.javaEnabled() ? "Java," : "";

	if (pluginlist.length > 0) pluginlist = pluginlist.substring(0,pluginlist.length-1);
	//detect Flash
	if (pluginlist.indexOf("Flash")!=-1) flash = "TRUE";
	//detect Windows Media Player
	if (pluginlist.indexOf("Windows Media Player")!=-1) mediaplayer = "TRUE";
	//detect Java
	if (pluginlist.indexOf("Java")!=-1) java = "TRUE";
	//detect Shockwave
	if (pluginlist.indexOf("Shockwave Director")!=-1) shockwave = "TRUE";
	//detect RealPlayer
	if (pluginlist.indexOf("RealPlayer")!=-1) realplayer = "TRUE";
	//detect QuickTime
	if (pluginlist.indexOf("QuickTime")!=-1) quicktime = "TRUE";
	//detect Acrobat Reader
	if (pluginlist.indexOf("Acrobat Reader")!=-1) acrobat = "TRUE";
	//detect SVG Viewer
	if (pluginlist.indexOf("SVG Viewer")!=-1) svg = "TRUE";

	// save collected information for this visitor in querystring
	var querystring = "&width=" + visScreenX + "&height=" + visScreenY + "&colordepth=" + visColordepth +
	"&java=" + java + "&cookies=" + cookies + "&flash=" + flash + "&mediaplayer=" + mediaplayer +
	"&shockwave=" + shockwave + "&realplayer=" + realplayer + "&quicktime=" + quicktime +
	"&acrobat=" + acrobat + "&svg=" + svg;

	// finalize the query string
	return querystring;
}
// cookie & random string functions
function _my_stat_getCookie(name) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) &&
	( name != document.cookie.substring( 0, name.length ) ) )
	{
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}
function _my_stat_randomString() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 40;
	var randomstring = '';
	for (var i=0; i<string_length; i++)
	{
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	var ret = new Date().getTime() + randomstring;
	return ret.substring(0,40);

}

function _my_stat_setCookie(name, value, expires, path, domain, secure) {

	// create a random, unique ID
	value = _my_stat_randomString();

	// set cookie expire date
	var today = new Date(); // get todays date
	today.setTime( today.getTime() ); // set time
	expires = 365 * 1000 * 60 * 60 * 24; // cookie expires in 1 year (365 days)
	var expires_date = new Date( today.getTime() + (expires) ); // set expire date

	// save cookie to visitors browser
	document.cookie= name + "=" + escape(value) +
	((expires) ? "; expires=" + expires_date.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}

//new PlugInSystem for IE7


var WM_startTagFix = '</';
var msie_windows = 0;
if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1)){
  msie_windows = 1;
  document.writeln('<script language="VBscript">');
  document.writeln('\'This will scan for plugins for all versions of Internet Explorer that have a VBscript engine version 2 or greater.');
  document.writeln('\'This includes all versions of IE4 and beyond and some versions of IE 3.');
  document.writeln('Dim WM_detect_through_vb');
  document.writeln('WM_detect_through_vb = 0');
  document.writeln('If ScriptEngineMajorVersion >= 2 then');
  document.writeln('  WM_detect_through_vb = 1');
  document.writeln('End If');
  document.writeln('Function WM_activeXDetect(activeXname)');
  document.writeln('  on error resume next');
  document.writeln('  If ScriptEngineMajorVersion >= 2 then');
  document.writeln('     WM_activeXDetect = False');
  document.writeln('     WM_activeXDetect = IsObject(CreateObject(activeXname))');
  document.writeln('     If (err) then');
  document.writeln('        WM_activeXDetect = False');
  document.writeln('     End If');
  document.writeln('   Else');
  document.writeln('     WM_activeXDetect = False');
  document.writeln('   End If');
  document.writeln('End Function');
  document.writeln(WM_startTagFix+'script>');
}

function WM_pluginDetect(plugindescription, pluginxtension, pluginmime, activeXname){

  //This script block will test all user agents that have a real plug-in array

  //(i.e. Netscape) and set the variables, otherwise it directs the routine

  // to WM_activeXDetect to detect the activeX control.

  

  // First define some variables
  var i,plugin_undetectable=0,detected=0, daPlugin=new Object();


  // Then we check to see if it's an MSIE browser that you can actually

  // check for the plugin in question. 

  if (msie_windows && WM_detect_through_vb){
      plugin_undetectable = 0;
  } else {
      plugin_undetectable = 1;
  }



  

  if(navigator.plugins) {
      numPlugins = navigator.plugins.length;
      if (numPlugins > 1) {
	  if (navigator.mimeTypes && navigator.mimeTypes[pluginmime] && navigator.mimeTypes[pluginmime].enabledPlugin && (navigator.mimeTypes[pluginmime].suffixes.indexOf(pluginxtension) != -1)) { 
	      if ((navigator.appName == 'Netscape') && (navigator.appVersion.indexOf('4.0') != -1)) { 
		  for(i in navigator.plugins) {
		      if ((navigator.plugins[i].description.indexOf(plugindescription) != -1) || (i.indexOf(plugindescription) != -1)) { 
			  detected=1;
			  break;
		      }
		  }
	      } else {
		  for (i = 0; i < numPlugins; i++) {
		      daPlugin = navigator.plugins[i];
		      if ((daPlugin.description.indexOf(plugindescription) != -1) || (daPlugin.name.indexOf(plugindescription) != -1)) {
			  detected=1;
			  break;
		      }
		  }
	      }

	    
      if (navigator.mimeTypes[pluginmime] == null) {
		  detected = 0;
	      }
	  }
	  return detected;
      } else if((msie_windows == 1) && !plugin_undetectable){
	  return WM_activeXDetect(activeXname);
      } else { 		
	  return 0;
      }
  } else {
      return 0;
  }
}


// this next function just makes it easy to detect the common plugins

function WM_easyDetect(whichPlugin) {

    // this function just makes it easy to do basic plug-in detection without

    // knowing all the mimetypes and activeX names and such
    var isItThere = 0;
    if( (whichPlugin == 'flash') || (whichPlugin == 'Shockwave Flash') ) 
	{
		isItThere = WM_pluginDetect('Flash', 'swf', 'application/x-shockwave-flash', 'ShockwaveFlash.ShockwaveFlash');
    } 
    else 
	if( (whichPlugin == 'director') || (whichPlugin == 'Shockwave Director') ) 
	{
		isItThere = WM_pluginDetect('Shockwave', 'dcr', 'application/x-director', 'SWCtl.SWCtl.1');
    } 
	else if( (whichPlugin == 'quicktime') || (whichPlugin == 'Quicktime') || (whichPlugin == 'QuickTime') ) 
	{
		isItThere = WM_pluginDetect('QuickTime', 'mov', 'video/quicktime', '');
    } 
    else if( (whichPlugin == 'Acrobat') || (whichPlugin == 'AdobeAcrobat') || (whichPlugin == 'Acrobat Reader') ) 
	{
		isItThere = WM_pluginDetect('Acrobat Reader', 'pdf', 'application/pdf', 'Pdf.PdfCtrl.5');
    } 
    	else if( (whichPlugin == 'Windows Media Player')  ) 
	{
		//isItThere = WM_pluginDetect('Windows Media Player', 'wmp', 'application/x-mplayer2', 'MediaPlayer.MediaPlayer.1');
    } 
	else if( (whichPlugin == 'realaudio') || (whichPlugin == 'Realaudio') || (whichPlugin == 'RealPlayer') ) 
	{
		isItThere = (WM_pluginDetect('RealPlayer', 'rpm', 'audio/x-pn-realaudio-plugin', 'RealPlayer.RealPlayer(tm) ActiveX Control (32-bit)')) || (WM_pluginDetect('RealPlayer', 'rpm', 'audio/x-pn-realaudio-plugin','rmocx.RealPlayer G2 Control')) || (WM_pluginDetect('RealPlayer', 'rpm', 'audio/x-pn-realaudio-plugin','RealPlayer.RealPlayer(tm) ActiveX Control (32-bit)')) || (WM_pluginDetect('RealPlayer', 'rpm', 'audio/x-pn-realaudio-plugin','RealVideo.RealVideo(tm) ActiveX Control (32-bit)'))
    } else {


    }
    return isItThere;
}






