wm_indiv_stats = new Object();  // object to add the injdividual parameters

var wiredminds = {

  getlist : "",

  www_server : "www.innominate.com",
  app_server : "qs.de.phoenixcontact.com:8100",
  cnt_server : "qs.innominate.com",
  numberOfCalls : 0,
  wm_track_url : "",
  wm_track_alt : "",
  wm_cvid : 0,
  placeOverlay : false,
  placeHeatmap : false,
  placeOverlayMenu : false,
  wm_page_name_value : null,
  ovk : null,
  overlayScriptUrl : '',
  validateUrl            : '',
  still_alive_interval : 5000,  // msecs!
  still_alive_time_max : 30*60*1000,  // msecs!
  still_alive_time_remaining : 0,
  still_alive_request : false,
  use_image_container : false,
  getOverlayScriptUrl : function (){
                  proto = wiredminds.getProto();
                  return proto+'//' + wiredminds.app_server + '/app/frontend/Ajax/overlay/getExt.php';
  },
  getValidateUrl : function (){
                  proto = wiredminds.getProto();
                  return proto+'//' + wiredminds.app_server + '/app/frontend/Ajax/overlay/validateKey.php';
  },
  createRandomKey : function () {
                var keyLenght = 16;
                var now = new Date();
                var outputStr = '';
                var inputNum = now.getTime();
                inputNum = inputNum.toString();
                var newInputNum = 0;
                var conStr = '';
                var partition = 3;

                while (outputStr.length < keyLenght) {
                        // Add random value
                        if (inputNum.length==0) {
                                do {
                                        newInputNum = Math.round(Math.random()*123);
                                } while (newInputNum<48 || newInputNum>57 && newInputNum<65 || newInputNum>80 && newInputNum<97 || newInputNum>122);
                                inputNum = newInputNum.toString();
                        }
                        // Separate numbers out of string
                        partition = 3;
                        do {
                                conStr = parseInt(inputNum.substr(0, partition));
                                partition--;
                        } while ((conStr<48 || conStr>57 && conStr<65 || conStr>80 && conStr<97 || conStr>122) && partition>0);
                        inputNum = inputNum.substr(partition+1);
                        // Convert and add new character
                        if (conStr<48 || conStr>57 && conStr<65 || conStr>80 && conStr<97 || conStr>122) {
                                outputStr += conStr.toString();
                        } else {
                                outputStr += String.fromCharCode(conStr);
                        }
                }
                if (outputStr.length > keyLenght) {
                        outputStr = outputStr.substr(0, keyLenght);
                }
                return outputStr;
  },

  get_GET_value:function ( get_param_name ) {
    if (!window) { return ''; }
    if (!window.location) { return ''; }
    if (!window.location.search) { return ''; }
    var search=window.location.search.substr(1);
    var assignments=search.split('&');
    for( var i=0; i<assignments.length; i++ ) {
      var assignment_splitted=assignments[i].split('=');
      var param_name=assignment_splitted[0];
      var param_value=assignment_splitted[1];
      if (param_name == get_param_name) {
        return param_value;
      }
    }
    return '';
  },

  get_HASH_value:function (hashName){

        var hashValue = null;
        if (self.document.location){
                hashValue = unescape(self.document.location.hash.substring(1));
        }
        if ((hashValue != null) && (hashValue != '')){
                var parts = hashValue.split('=');

                if (parts.length == 2){
                        hashValue = parts[1];
                }
        }
        return hashValue;
  },

  add_to_list : function( key, value) {
    wiredminds.getlist += '&' + key + '=' + escape(value);
  },

  //Get protocol, returns protocol string
  getProto : function () {
        var proto = 'http:';
    if ( typeof (document.location) != 'undefined') {
      if ( typeof (document.location.protocol) != 'undefined') {
        if ( document.location.protocol != 'file:') {
          proto = document.location.protocol;
        }
      }
    }
        return proto;
  },

  count_URL : function (getlist) {

    proto = wiredminds.getProto();
    var WMCT_URL=proto+'//'+this.cnt_server+'/track/ctin.php?'+getlist;

        //Check if it is the first time the pixel is called (numberOfCalls==0)
        //if it is the first then write pixel, this is required to show the webcounter on the website
        if ( (wiredminds.numberOfCalls > 0) || wiredminds.use_image_container ) {
                var CT_container=new Image();
        CT_container.src=WMCT_URL;
        } else {
                document.write('<div><a target="_blank" href="'+wiredminds.wm_track_url+'"><img src="'+WMCT_URL+'" alt="'+wiredminds.wm_track_alt+'" border="0" /></a></div>');
        }
        wiredminds.numberOfCalls++;
  },

  countpix : function (pixcode) {
    wm_pixelcode = pixcode;
    wiredminds.count();
  },

  count_flash : function (flashArgs) {
    if ( typeof(flashArgs) != 'string') { return; }
    if ( flashArgs == '') { return; }

    if ( flashArgs.substr(0,1) == '&') { flashArgs='t2=1'+flashArgs; }

    wiredminds.count_URL(flashArgs);
  },

  getPageName : function (ext){

          // auto-detect page name, if necessary
    if ( typeof(wm_page_name) == 'undefined' ) {
      wm_page_name_value = document.location.pathname
          // replace '/' with '|'
          wm_page_name_value = wm_page_name_value.replace(/^\/*/, '');
          wm_page_name_value = wm_page_name_value.replace(/\/*$/, '');
          wm_page_name_value = wm_page_name_value.replace(/\//g, '|');
    } else {
      wm_page_name_value = wm_page_name;
    }
    if (typeof(ext) == 'string') {
      wm_page_name_value+='|'+ext;
    }

    return wm_page_name_value;
  },

  internalCount : function (ext) {
        myDate = new Date();
    wiredminds.getlist="t="+myDate.getTime(); // Clear/Initialize the get list.
                                                                                          // NOTE: Clearing the getlist is a must!
                                                                                          //       Otherwise subsequent calls to count
                                                                                          //       (e.g. by using onClick="") would
                                                                                          //       append their values to the old get list.

        // First the required parameters
    if ( typeof(wm_custnum) != 'undefined') {
      wiredminds.add_to_list('custnum',wm_custnum);
    } else {
      return; // no customer number given, do nothing
    }


    // auto-detect sname, if necessary
    if ( typeof(wm_sname) == 'undefined' ) {
      wm_sname = document.location.hostname;
    }
    wiredminds.add_to_list('sname',wm_sname);

    wiredminds.add_to_list('pagename',this.getPageName(ext));

    // auto-detect group name, if necessary
    if ( typeof(wm_group_name) == 'undefined') {
      wm_group_name = document.location.pathname.substring(0,document.location.pathname.lastIndexOf('/'));
    }
    wiredminds.add_to_list('group',wm_group_name);

        if ( this.still_alive_request == true) { wiredminds.add_to_list('still_alive',1); }

    // Then the optional parameters
    // Note: The optional parameters first get collected and added
    //       afterwards to the getlist to better control their order.
    var wm_version = "3.0";
    var wm_resolution = screen.width + "x" + screen.height;
    var wm_color_depth = navigator.appName != 'Netscape' ? screen.colorDepth : screen.pixelDepth;

    var wm_plugin_list = "";
    for( var i=0; i<navigator.plugins.length; i++ ) {
      wm_plugin_list += navigator.plugins[i].name + ';';
    }

    var wm_jv = 0;
    if (navigator.javaEnabled()) { wm_jv=1; };

    var wm_js_value = 1;
    if ((typeof wm_js) != 'undefined') { wm_js_value=wm_js; }

    // auto-detect referrer, if necessary
    if ( typeof(wm_ref) == 'undefined' ) {
      if ( typeof(wm_referrer) == 'undefined' ) {
        try {
          if( typeof( top.document ) == "object" ) { wm_referrer_value = top.document.referrer; }
        } catch (e) {
          wm_referrer_value = document.referrer;
        }
      } else {
        wm_referrer_value = wm_referrer;
      }
    } else {
      wm_referrer_value = wm_ref;
    }

    if ((typeof wm_campaign) != 'undefined') {
      wm_camp_value = wm_campaign;
    } else if ((typeof wm_campaign_key) != 'undefined') {
      wm_camp_value = wiredminds.get_GET_value(wm_campaign_key);
    }

        //Call Center
        if (typeof(wm_useccapplet)!="undefined") {
                if (wm_useccapplet==1) {
                         wm_cvid = wiredminds.createRandomKey();
                         wiredminds.wm_cvid =  wm_cvid;
                }
        }
        //END Call Center

        if ( typeof (wm_version) != 'undefined')     { wiredminds.add_to_list('version',wm_version); }
    if ( typeof (wm_js_value) != 'undefined')    { wiredminds.add_to_list('js',wm_js_value); }
    if ( typeof (wm_jv) != 'undefined')          { wiredminds.add_to_list('jv',wm_jv); }
    if ( typeof (wm_resolution) != 'undefined')  { wiredminds.add_to_list('resolution',wm_resolution); }
    if ( typeof (wm_color_depth) != 'undefined') { wiredminds.add_to_list('color_depth',wm_color_depth); }
    if (wiredminds.numberOfCalls == 0) {
                //Only request webcounter if it is the first call (performance issues)
                if ( typeof (wm_wc) != 'undefined')          { wiredminds.add_to_list('wc',wm_wc); }
        }
    if ( typeof (wm_camp_value) != 'undefined')  { wiredminds.add_to_list('campaign',wm_camp_value); }
    if ( typeof (wm_milestone) != 'undefined')   { wiredminds.add_to_list('milestone',wm_milestone); }
    if ( typeof (wm_sales) != 'undefined')       { wiredminds.add_to_list('sales',wm_sales); }
    if ( typeof (wm_order_id) != 'undefined')    { wiredminds.add_to_list('order_id',wm_order_id); }
    if ( typeof (wm_basket) != 'undefined')      { wiredminds.add_to_list('basket',wm_basket); }
    if ( typeof (wm_connspeed) != 'undefined')   { wiredminds.add_to_list('speed',wm_connspeed); }
    if ( typeof (wm_txid) != 'undefined')        { wiredminds.add_to_list('txid',wm_txid); }
    if ( typeof (wm_pixelcode) != 'undefined')   { wiredminds.add_to_list('pixel',wm_pixelcode); }
    if ( typeof (wm_referrer_value) != 'undefined')  { wiredminds.add_to_list('referrer',wm_referrer_value); }
    if ( typeof (wm_ivw) != 'undefined')         { wiredminds.add_to_list('ivw',wm_ivw); }
    if ( typeof (wm_ab_data) != 'undefined')     { wiredminds.add_to_list('ab_data',wm_ab_data); }
    if ( typeof (wm_ud1) != 'undefined')         { wiredminds.add_to_list('ud1',wm_ud1); }
        if ( typeof (wm_ud2) != 'undefined')         { wiredminds.add_to_list('ud2',wm_ud2); }
    if ( typeof (wm_cd1) != 'undefined')         { wiredminds.add_to_list('cd1',wm_cd1); }
        if ( typeof (wm_cvid) != 'undefined')        { wiredminds.add_to_list('cvid',wm_cvid); }
        if ( typeof (wm_score) != 'undefined')            { wiredminds.add_to_list('score',wm_score); }
        // ecommerce tracking
        if ( typeof (wm_ec_action) != 'undefined')                     { wiredminds.add_to_list('wm_ec_action',wm_ec_action); }
        if ( typeof (wm_ec_basket_id) != 'undefined')            { wiredminds.add_to_list('wm_ec_basket_id',wm_ec_basket_id); }
        if ( typeof (wm_ec_product_id) != 'undefined')            { wiredminds.add_to_list('wm_ec_product_id',wm_ec_product_id); }
        if ( typeof (wm_ec_product_name) != 'undefined')            { wiredminds.add_to_list('wm_ec_product_name',wm_ec_product_name); }
        if ( typeof (wm_ec_category_name) != 'undefined')    { wiredminds.add_to_list('wm_ec_category_name',wm_ec_category_name); }
        if ( typeof (wm_ec_order_id) != 'undefined')                     { wiredminds.add_to_list('wm_ec_order_id',wm_ec_order_id); }
        if ( typeof (wm_ec_customer_id) != 'undefined')            { wiredminds.add_to_list('wm_ec_customer_id',wm_ec_customer_id); }
        if ( typeof (wm_ec_currency) != 'undefined')                     { wiredminds.add_to_list('wm_ec_currency',wm_ec_currency); }
        if ( typeof (wm_ec_unit_price) != 'undefined')            { wiredminds.add_to_list('wm_ec_unit_price',wm_ec_unit_price); }
        if ( typeof (wm_ec_unit_price_in_currency) != 'undefined')  { wiredminds.add_to_list('wm_ec_unit_price_in_currency',wm_ec_unit_price_in_currency); }
        if ( typeof (wm_ec_total_price) != 'undefined')                             { wiredminds.add_to_list('wm_ec_total_price',wm_ec_total_price); }
        if ( typeof (wm_ec_total_price_in_currency) != 'undefined') { wiredminds.add_to_list('wm_ec_total_price_in_currency',wm_ec_total_price_in_currency); }
        if ( typeof (wm_ec_quantity) != 'undefined')                                    { wiredminds.add_to_list('wm_ec_quantity',wm_ec_quantity); }
        if ( typeof (wm_ec_basket_special) != 'undefined')                    { wiredminds.add_to_list('wm_ec_basket_special',wm_ec_basket_special); }
        if ( typeof (wm_ec_product_special) != 'undefined')                    { wiredminds.add_to_list('wm_ec_product_special',wm_ec_product_special); }
        if ( typeof (wm_ec_payment_method) != 'undefined')                    { wiredminds.add_to_list('wm_ec_payment_method',wm_ec_payment_method); }

        // Page Url - put it at the (near) end, because it may overflow the GET list.
        wiredminds.add_to_list('page_url', encodeURIComponent(document.location));

        // The plugin list always goes last, because it can become very long and is not that required.
    if ( typeof (wm_plugin_list) != 'undefined') { wiredminds.add_to_list('plugins',wm_plugin_list); }

        // check for individual parameters
        for (var key in wm_indiv_stats) {
                if (wm_indiv_stats[key]){
                        wiredminds.add_to_list(key, wm_indiv_stats[key]);
                }
        }

        // end individual parameters
    wiredminds.count_URL(wiredminds.getlist);
  },

   ReadCookie : function (name) {

                var nameEQ = name + "=";
                var ca = document.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;
        },

        SetCookie : function (name,value,nHours, path) {
                 var today = new Date();
                 var expire = new Date();
                 var pathStr = "/";
                 if (nHours==null || nHours==0) nHours=1;
                 if ((typeof(path) != 'undefined') && (path != null)) { pathStr = path; }
                 expire.setTime(today.getTime() + 3600000 * nHours);
                 document.cookie = name+"="+escape(value) + ";expires="+expire.toGMTString() + ";path=" + pathStr;

        },

        eraseCookie : function(name) {
                this.SetCookie(name,"",-1);
        },

        loadExternalScript : function (id, url){

          var oScript  = document.createElement('script');
          oScript.id = id;
          oScript.type = "text/javascript";
          oScript.src  = url;
          document.getElementsByTagName("head")[0].appendChild(oScript);
  },

  setOverlayCookies : function(){

                wiredminds.eraseCookie('WMOverlayMenu');
                wiredminds.eraseCookie('WMOverlayKey');
                  wiredminds.SetCookie('WMOverlayMenu', '1', 24);
                  wiredminds.SetCookie('WMOverlayKey', wiredminds.ovk, 24);

                  if (this.checkOverlayCookies()){
                          this.startOverlay();
                  }
  },

  checkOverlayCookies : function(){

           wiredminds.placeOverlay = (wiredminds.ReadCookie('WMOverlay') == '1') ? true : false;
           wiredminds.placeHeatmap = (wiredminds.ReadCookie('WMHeatmap') == '1') ? true : false;
           wiredminds.placeOverlayMenu = (wiredminds.ReadCookie('WMOverlayMenu') == '1') ? true : false;

                   if (wiredminds.placeOverlayMenu || wiredminds.placeOverlay || wiredminds.placeHeatmap) {
                           return true;
                   }
                   return false;
  },

  // load the extra javascripts for the overlay/heatmap
  startOverlay : function(){

                  if (this.checkOverlayCookies()){
                          this.overlayScriptUrl = wiredminds.getOverlayScriptUrl();
                          this.overlayScriptUrl += '?key=' +  wiredminds.ovk;
                      setTimeout("wiredminds.loadExternalScript('wmOverlay','" + this.overlayScriptUrl + "')", 1000);
                  }
  },

  doCountWiredminds : function( ext ){

            if (typeof (wm_track_url) != 'undefined') {
                  wiredminds.wm_track_url = wm_track_url;
          } else {
                  proto = wiredminds.getProto();
                  wiredminds.wm_track_url = proto+'//'+this.www_server;
          }

          if (typeof (wm_track_alt) != 'undefined') {
                  wiredminds.wm_track_alt = wm_track_alt;
          } else {
                  wiredminds.wm_track_alt = 'WiredMinds eMetrics tracking with WiredMinds Enterprise Edition';
          }
          wiredminds.internalCount( ext );
  },

  doCountHottracker : function( ext ){

            if (typeof (wm_track_url) != 'undefined') {
                  wiredminds.wm_track_url = wm_track_url;
          } else {
                  proto = wiredminds.getProto();
                  wiredminds.wm_track_url = proto+'//www.hottracker.de';
          }

          if (typeof (wm_track_alt) != 'undefined') {
                  wiredminds.wm_track_alt = wm_track_alt;
          } else {
                  wiredminds.wm_track_alt = 'eMetrics tracking with HotTracker';
          }
          wiredminds.internalCount( ext );
  },
  // for Wiredminds
  count : function ( ext ) {

    var getKey = wiredminds.get_HASH_value('WMOverlayKey');

    if (getKey.length == 32){
            wiredminds.ovk = getKey;
    }else{
            wiredminds.ovk = wiredminds.ReadCookie('WMOverlayKey');
    }
           if ((wiredminds.ovk) && (wiredminds.ovk.length == 32)){

            // - if the key was passed and it has a length of 32 charts, it might be valid.
            // - check the key in the backend
            // - if the key is valid. it will autocaticly call the functions to load the extra javascript,
            //   and show the overlay menu.
                   wiredminds.validateUrl = wiredminds.getValidateUrl();
            wiredminds.validateUrl += '?key=' + wiredminds.ovk;
            this.loadExternalScript('wmOverlayKeyValidation', wiredminds.validateUrl);

    } else {
            wiredminds.eraseCookie('WMOverlayKey');
            this.doCountWiredminds( ext );
    }
  },

  // for Hottracker
  counter : function ( ext ) {

     var getKey = wiredminds.get_HASH_value('WMOverlayKey');

    if (getKey.length == 32){
            wiredminds.ovk = getKey;
    }else{
            wiredminds.ovk = wiredminds.ReadCookie('WMOverlayKey');
    }
           if ((wiredminds.ovk) && (wiredminds.ovk.length == 32)){

            // - if the key was passed and it has a length of 32 charts, it might be valid.
            // - check the key in the backend
            // - if the key is valid. it will autocaticly call the functions to load the extra javascript,
            //   and show the overlay menu.
                   wiredminds.validateUrl = wiredminds.getValidateUrl();
            wiredminds.validateUrl += '?key=' + wiredminds.ovk;
            this.loadExternalScript('wmOverlayKeyValidation', wiredminds.validateUrl);

    } else {
            wiredminds.eraseCookie('WMOverlayKey');
            this.doCountHottracker( ext );
    }
  },

  getVisitorLink : function () {
                var destinationDomain = '//'+this.app_server;
                var relPath = '/app/ct/index.php?mode=last_visitors_detail&websitesel='+wm_custnum+'&cvid='+wiredminds.wm_cvid;
                proto = wiredminds.getProto();
                var url = proto + destinationDomain + relPath;
                return url;
  },

  setVisitorLink : function (flashObjectId) {
                var visitor_link = wiredminds.getVisitorLink();
                eval("document."+flashObjectId+".SetVariable('wm_visitor_link', visitor_link)");

  },

  sendStillAlive : function() {
    this.still_alive_request=true;
        wiredminds.internalCount();
    this.still_alive_request=false; // Immediatly remove it, so onClicks are counted correctly, for example!

    this.still_alive_time_remaining -= this.still_alive_interval;
    if (this.still_alive_time_remaining > 0) {
      setTimeout("wiredminds.sendStillAlive()", this.still_alive_interval);
    }
  },

  activateStillAlive : function() {
    this.still_alive_time_remaining=this.still_alive_time_max;
    setTimeout("wiredminds.sendStillAlive()", this.still_alive_interval);
  },

  /**
   * Event Tracking
   * @param string eventStr
   */
  trackEvent: function(eventStr){
          if ((typeof(eventStr) == 'undefined') && (eventStr != '')){
                  return; // no event string given, do nothing
          }

          myDate = new Date();
    wiredminds.getlist = "t="+myDate.getTime(); // Clear/Initialize the get list.
                                                                                            // NOTE: Clearing the getlist is a must!
                                                                                            //       Otherwise subsequent calls to count
                                                                                            //       (e.g. by using onClick="") would
                                                                                            //       append their values to the old get list.
        // First the required parameters
    if ( typeof(wm_custnum) != 'undefined') {
      wiredminds.add_to_list('custnum',wm_custnum);
    } else {
      return; // no customer number given, do nothing
    }

        wiredminds.add_to_list('wm_event', eventStr);
    wiredminds.count_URL(wiredminds.getlist);
  }

};

