/*
Name: Google Analytics Cookie Scraper
Author: Shawn Purtell
Created: 11/29/2006
Description: Grabs data from Google Analytics tracking cookies and inserts into hidden form field for easy lead submission.
~~~~~~
Last modified by Nick Schoonens on 16 July 2009 
 - fixed getting source of referrals
 - added variables for form field names
 - fixed so it doesn't display an error if the fields do not exist
*/

function readCookie(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;
}

function parseCookie_source(name)
{
  var source_field = 'source';
  var medium_field = 'medium';
  var keyword_field = 'keyword';

  if (document.getElementById(source_field) != undefined) {
    if (document.cookie.indexOf('__utmz=') != -1)
    {
      var c2 = readCookie(name);     // This gets the cookie
      var ca2 = c2.split('|');       // This splits the cookie into parts
    
      // This was modified by Nick Schoonens on 16 July 2009
      // We can no longer split this by '.' because it breaks if it's a referral
      // So we split it at the '=' and then join it back again without the numeric info at the front
      temp = ca2[0].split('=');      // This splits it between the first variable and the value
      temp2 = temp[0].split('.'); // This splits the first variable from the numeric info at the front
      ca2[0] = temp2[temp2.length - 1] + '=' + temp[temp.length - 1];   // Combine the variable name with the value and put it back in the array
      // End modification by Nick
    
      var src = ' ';          // Will contain the source, if there is one
      var campaign = ' ';        // Will contain the campaign, if there is one
      var medium = ' ';        // Will contain the medium, if present
      var term = ' ';          // Will contain keyword info, if present
      var cancel = false;        // Used to check for AdWords ID
    
      for (i = 0; i < ca2.length; i++)
      {  
        temp3 = ca2[i];        //First, take each variable (ex. utmcsr=sourcename)
        temp4 = temp3.split('=');   //Splits into an array, with temp4[0] = 'utmcsr, and temp4[1] = 'sourcename' using our above example
      
        if (temp4[0] == 'utmgclid')  //Identifies the varaible and replaces appropriate items for Google Adwords Campaigns
        {
          src = 'google';
          medium = 'cpc';
          document.getElementById(source_field).value = src;
          document.getElementById(medium_field).value = medium;
          cancel = true;      //We don't want to reset the source, medium, or campaign info
        }
      
        if (temp4[0] == 'utmcsr' && !cancel)
        {
          src = temp4[1];
          document.getElementById(source_field).value = src;
        }
        if (temp4[0] == 'utmcmd' && !cancel)
        {
          medium = temp4[1];
          document.getElementById(medium_field).value = medium;
        }
        if (temp4[0] == 'utmctr')
        {
          term = temp4[1];
          document.getElementById(keyword_field).value = term;
        }
      }
    }  
  }
}



/*************************************************************
* Window Onload Manager (WOM) v1.0
* Author: Justin Barlow - www.netlobo.com
*
* Description:
* The WOM library of functions allows you to easily call
* multiple javascript functions when your page loads.
*
* Usage:
* Add functions to WOM using the womAdd() function. Pass the
* name of your functions (with or without parameters) into
* womAdd(). Then call womOn() like this:
*     womAdd('hideDiv()');
*     womAdd('changeBg("menuopts","#CCCCCC")');
*     womOn();
* WOM will now run when your page loads and run all of the
* functions you have added using womAdd()
*************************************************************/
/*************************************************************
* The womOn() function will set the window.onload function to
* be womGo() which will run all of your window.onload
* functions.
*************************************************************/
function womOn(){
  window.onload = womGo;
}
/*************************************************************
* The womGo() function loops through the woms array and
* runs each function in the array.
*************************************************************/
function womGo(){
  for(var i = 0;i < woms.length;i++)
    eval(woms[i]);
}
/*************************************************************
* The womAdd() function will add another function to the woms
* array to be run when the page loads.
*************************************************************/
function womAdd(func){
  woms[woms.length] = func;
}
/*************************************************************
* The woms array holds all of the functions you wish to run
* when the page loads.
*************************************************************/
if (woms == undefined) {
	var woms = new Array();
}




// Parse the cookie on the page load using Windows Onload Manager (WOM)
womAdd('parseCookie_source("__utmz")');
womOn();

