var wiredminds = {
  
  getlist : "",
  
  numberOfCalls : 0,
  wm_track_url : "",
  wm_track_alt : "",
  wm_cvid : 0,
  
  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 '';
  },
  
  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+'//ctsde17.wiredminds.de/track/ctin.php?'+getlist;
    //alert(WMCT_URL);
	
	//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 ) {
		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+'" 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);
  },
  
  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);
    
    // auto-detect page name, if necessary
    if ( typeof(wm_page_name) == 'undefined' ) {
      wm_page_name_value = document.location.pathname
    } else {
      wm_page_name_value = wm_page_name;
    }
    if (typeof(ext) == 'string') {
      wm_page_name_value+='|'+ext;
    }
    wiredminds.add_to_list('pagename',wm_page_name_value);
    
    // 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);
    
    
    // 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_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); }

// 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); }
	
    wiredminds.count_URL(wiredminds.getlist);
  },
  
  
  //This function is called by the counting code and sets the default parameter for wm_track_url and wm_track_alt
  count : 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.wiredminds.de';
	  }
	  
	  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 );
  },
  
  //This function is called by the counting code and sets the default parameter for wm_track_url and wm_track_alt
  counter : 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 );
  },
  
  getVisitorLink : function () {
		var destinationDomain = '//www4.wiredminds.de';
		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)");
		
  }



};
