//<SCRIPT LANGUAGE=javascript>

//=================================================
// Global Variables and Switches
//
// These variable may be altered to change the 
// behavior of the StatScript to better suit your
// web site. Default settings will work in nearly
// all situations.
//=================================================

// Optional domain for cookie required in cases where
// multiple domain prefixes are used.

var DMdomain = ""; 

// File extensions required for a file to be considered
// a download. This variable is used by the Link Tracking
// feature. Only external links (to outside web sites)
// and Downloads (generally media files with extensions 
// matching those below) are recorded.
// Note: This list is Space (' ') separated.

var DMDownloadExtensions = ".arc .arj .asf .asx .avi .bin .doc .exe .gz .gzip .hqx .mov .movie .mp2 .mp3 .mpeg .mpg .msi .pdf .ppt .qt .ra .ram .sea .sit .tar .z .tgz .wav .wmv .zip .msp .rar .wpd .qtm";

// The location where statistical transactions are sent
// in LiveStats.NET and LiveStats.BIZ hosted this is
//
//        //collector.deepmetrix.com/dm.gif?
//
// for LiveStats.NET installable this will likely be
// your web server, or a designated server.
//
//        //www.yourserver.com/dm.gif?

var DMsendTo = "//collector.deepmetrix.com/dm.gif?";


//-------------------------------------------------
// Record an Event
//    Name  = Name of Event (Display Name)
//    Value = ####.## format, monetary or points value 
//  
// Call this function BEFORE calling DMPage. This 
// allows you to set an event with page statistics
//
// Value is an optional parameter and is only used 
// for Valued Goals
//-------------------------------------------------

function DMEvent( Name, Value )
{

  if ( ( ! Name ) || ( Name == "" ) )
  {
    if (__DMDEBUG != 0)  
  		alert( 'LIVESTATS ERROR \n\n DMEvent requires a Name' );
      
    return;
  }
  
  var ProdVar = "type=event&name=" + escape( Name );
  
  if ( ( Value ) && ( Value != "" ) )
    ProdVar += "&value=" + escape( Value );

  __DMEVENT = "&crt=" + escape(ProdVar);

}

//-------------------------------------------------
// Record a PAGE VIEW
//-------------------------------------------------

function DMPage( URLOverride )
{

  if ((URLOverride) && (URLOverride != "" ))
    __DMOVERRIDE = URLOverride;
    
  __DMStat();
  
}

//-------------------------------------------------
// Record a PRODUCT VIEW
//
// Set Product and Catalog (ProductName and 
//     CatalogName are optional parameters)
//-------------------------------------------------

function DMProduct( ProductCode, Catalog, ProductName, CatalogName )
{

  if ((!ProductCode) || (ProductCode == ""))
  {
    if (__DMDEBUG != 0)  
		alert( 'LIVESTATS ERROR \n\n DMSetProduct requires a ProductCode and a CatalogID' );
    return;
  }

  if ((!Catalog) || (Catalog == ""))
  {
    if (__DMDEBUG != 0)  
		alert( 'LIVESTATS ERROR \n\n DMSetProduct requires a CatalogID' );
    return;
  }

  if ((!ProductName) || (ProductName == ""))
    ProductName = "";

  if ((!CatalogName) || (CatalogName == ""))
    CatalogName = "";

  var ProdVar = "type=prod&prodid=" + escape( ProductCode ) +
                  "&catalog=" + escape( Catalog ) +
                  "&prodname=" + escape( ProductName ) +
                  "&catname=" + escape( CatalogName );

  var URLExtra = "&crt=" + escape(ProdVar);
  
  __DMStat( URLExtra );

}


//-------------------------------------------------
// Track CRM values from a Form
//-------------------------------------------------

function DMCRMForm(formObj, mappings, OnSubmit )
{
  var TagString = "";
  var arrLen = mappings.length;
  var val;
  var c;
  for(c=0;c<arrLen;c++)
    {
      var parts = mappings[c].split( '=' );
      val = "";
      if(eval('formObj.'+parts[1]))
        {
          val = eval('formObj.'+parts[1]+'.value');
        }
      if (val != '')
        {
          val = parts[0] + '+' + val;
          TagString += escape(val);
          if (c < arrLen-1)
            TagString += ',';
        }
    }

  var dmURL = "v=94&id=" + __DMGETID() + "&tag=" + escape(TagString) + "&rnd="  + new Date().getTime();

  if ((!OnSubmit) || (OnSubmit == ""))
  {
    __DMFORM = formObj;

    OnSubmit = "__DMFORM.submit();true;";
  }

  __DMXIMG( dmURL, OnSubmit );
  return false;
}

//-------------------------------------------------
// Track ad hoc CRM values
//-------------------------------------------------

function DMCRMValue( Param, Value )
{
  var TagString = "";

  var dmURL = "v=94&id=" + __DMGETID() + 
              "&tag=" + escape( escape( Param ) + '+' + escape( Value ) ) + 
              "&rnd="  + new Date().getTime();

  __DMXIMG( dmURL );
  return false;
}

//-------------------------------------------------
// Call this for each item in a shopping cart
//-------------------------------------------------

function DMCartItem(prodID, prodQuantity, totalPrice)
{
  if (prodID)
    {
      __DMPRODUCTS[__DMPRODUCTS.length] = prodID;

      if(prodQuantity)
          __DMQUANTITY[__DMQUANTITY.length] = prodQuantity;
      else
          __DMQUANTITY[__DMQUANTITY.length] = '1';

      if(totalPrice)
          __DMPRICE[__DMPRICE.length] = totalPrice;
      else

          __DMPRICE[__DMPRICE.length] = '0';
    }
}


//-------------------------------------------------
// Call this to send LiveStats the updated cart info.
//      - Call after all calls to DMCartItem have 
//        been made.
//-------------------------------------------------

function DMCartUpdate()
{
  var cartStr = "+";

  var len = __DMPRODUCTS.length; // We assume that the other 2 arrays have the same length

  for(var i=0; i < len; i++)
    cartStr += __DMPRODUCTS[i] + ',' + __DMQUANTITY[i] + ',' + __DMPRICE[i] + "+";

  var URLExtra = "&crt=" + escape(cartStr);
  
  __DMStat( URLExtra );  

}


//-------------------------------------------------
// Enables Link Tracking
//      - Call at end of page or on Body onLoad
//-------------------------------------------------

function DMEnableLinkTracking()
{

  for (var i=0; i < document.links.length; i++)
  {

	    if(document.layers)
  	      document.links[i].onmousedown= function() { return __DMINTERCEPT( this ); };
      else
          document.links[i].onmouseup= function() { return __DMINTERCEPT( this ); };
  }

}


//=================================================
// INTERNAL FUNCTIONS - DO NOT EDIT. DO NOT CALL.
// These functions are 'helpers' called by the 
// functions listed above.
//=================================================

function __DMGETLINKEDID()
{

  var URL = window.location.toString(); 

  var start = URL.indexOf( "_dm_x=" );

  if (start > -1)
  {
    var end = URL.indexOf("&",start);      

    if (end == -1) end = URL.indexOf("#",start);      

    if (end == -1) end = URL.length;  

    start += 6;

    var DMsession = unescape(URL.substring(start,end));

    return DMsession;
  }

  return "";

}

function __DMXIMG( URL, onStat )
{

  __DMSTATIMG[ __DMSTATIDX ] = new Image();

   __DMSTATIMG[ __DMSTATIDX ].src = location.protocol + DMsendTo + URL;

  if ((onStat) && (onStat != ""))
  {
    __DMSTATIMG[ __DMSTATIDX ].onStat = onStat;
    __DMSTATIMG[ __DMSTATIDX ].onload = function() { __DMHOLD( this.onStat) ; };//onStat; //"__DMHOLD();"+onStat+"";
  }
  
  __DMSTATIDX++;
}

function __DMHOLD( onStat )
{

  eval( onStat );

};

function __DMIDGEN()
{
  var vals = new Array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f');  
  var key  = "";
  var last = -1;
  var idx  = -1;
  
  for (var i = 0; i < 8; i++)
  {
  
    while (last == idx)
      idx = Math.floor( Math.random() * 16 );    
      
    last = idx;

    key += vals[ idx ];
  }
  
  var x = 0; 
   
 if (screen)
   x = (screen.width + screen.height) / screen.colorDepth;    
   
 if (x == 0)
   x = Math.random() * 65536;
 
  x = Math.floor( x % 256 );
     
  key += '_' + vals[ Math.floor(x / 16) ] + vals[ x % 16 ];
  

  var s = new Date();  
  x = s.getTime() % 16777216;
  x = x.toString();   
  
  while (x.length < 8) x = '0' + x;
  
  key += '_' + x;
   
  return key;

};

function __DMGETID()
{

  var LinkedID = __DMGETLINKEDID();

  var s = new Date();
  
  if (navigator.userAgent.indexOf('Mac') >= 0 && s.getTimezoneOffset() >= 720)
    s.setTime (s.getTime() - 1440*60*1000);

  var cookieString = document.cookie.toString();

  var DMsession = "";

  if ((LinkedID != "") || (cookieString.indexOf("_dmid") == -1))
  {

    if (LinkedID != "")
      DMsession = LinkedID;
    else
      DMsession = __DMIDGEN();

    var domStr = "";

    if( DMdomain != "" )
    {
      domStr = "domain="+ DMdomain +";";
    }

    document.cookie = "_dmid=" + DMsession + 
                      ";expires=Mon, 31-Dec-2008 00:00:00 GMT;"+
                      domStr+"path=/;";

  }

  cookieString = document.cookie.toString();

  if(cookieString.length < 1)
  {
    DMsession = "";
  }
  else
  {
    if(cookieString.indexOf(';') == -1)
      cookieString = cookieString.replace(/_dm/g, ';_dm'); // NS6 issue

    var start = cookieString.indexOf("_dmid=") + 6;
    var end = cookieString.indexOf(";",start);

    if (end == -1)
      end = cookieString.length;
    DMsession = unescape(cookieString.substring(start,end));
  }
  return DMsession;

}

function __DMINTERCEPT( link )
{

   var url = link.href;
   var parts = DMDownloadExtensions.split( ' ' );

   var match = false;

   for (var i = 0; i < parts.length; i++)
   {

     if ((!parts[i]) || (parts[i] == "")) continue;

     if (url.indexOf( parts[ i ] ) != -1)
     {
       match = true;
       break;
     }

   }

   if ((match == false) && (url.indexOf( location.host ) != -1))
   {
     return true;
   }

   var dmURL = "v=94&id=" + __DMGETID() + 
               "&lnk=" + escape(url)+"&rnd=" + new Date().getTime();

   __DMXIMG( dmURL );

   var endAt = new Date().getTime() + 250;
   var rightNow;

   while(true)
   {
     rightNow = new Date();
     if(rightNow.getTime() >= endAt)
       break;
   }

   return true;

}

// Track a page (set DMAltURL to record a different page URL than the one in window.location)
function __DMStat( urlAppend )
{

  __DMPAGE = window.location; 

  if (__DMOVERRIDE != "")
    __DMPAGE = __DMOVERRIDE;

  if (__DMREQUESTED != 0)
  {
    if (__DMDEBUG != 0)
      alert( 'LIVESTATS ERROR \n\nDuplicate Call to Statistic Function' );
    return;
  }

  var dmURL = "v=94&id=" + __DMGETID() + "&url=" + escape(__DMPAGE) + 
              "&ref=" + escape(document.referrer) +
              "&lng=" + ((!document.all || navigator.userAgent.match('Opera')) ? 
                 navigator.language : navigator.userLanguage) +             
              "&tz=" + (Math.round(new Date('dec 1, 2002').getTimezoneOffset()/60)*-1);

  if (screen)
     dmURL += "&scr=" + escape( screen.width + "x" + screen.height + " " + screen.colorDepth + "bpp" );
     
   if ((urlAppend) && (urlAppend != ""))
     dmURL += urlAppend;

   if ((__DMEVENT) && (__DMEVENT != ""))
     dmURL += __DMEVENT;

  dmURL += "&rnd=" + new Date().getTime();

  __DMXIMG( dmURL );

  __DMREQUESTED = 1;

}

// Constant values used by StatScript
var __DMSTATIMG   = new Array();
var __DMSTATIDX   = 0;

var __DMDEBUG     = 1;
var __DMREQUESTED = 0;
var __DMOVERRIDE  = "";
var __DMEVENT     = "";

var __DMPAGE      = "";
var __DMFORM;

var __DMPRODUCTS  = new Array(); // List of product ids
var __DMQUANTITY  = new Array(); // List of product quantitys.
var __DMPRICE     = new Array(); // List of product prices.


