	// VKI - Erik Vold, Brian Katz 080319
	// Required to segment visitors for Google Analytics
	// Called from GA script blocks on all pages.
	// Removing this file will cause Javascript errors on most pages

var strLocation = document.location.toString();
var strCookies  = unescape(document.cookie);
var bolDebugging = ( (strCookies.indexOf('vki_debugging') >= 0) && (strLocation.indexOf('vki_debugging=0') == -1) ||
				     (strLocation.indexOf('vki_debugging=1') >= 0) 
				   ); 
 
var segmentHelperObj = {};
segmentHelperObj.visitorSegmentationLvls = new Array(
'No Action',
'Submitted Form',
'Submitted Instaquote',
'Added to Quote',
'Added to Cart',
'Requested Quote',
'New Customer - Never Purchased',
'New Shopper - Never Purchased',
'Casual Purchaser',
'Purchased',
'New Customer - Has Purchased',
'New Shopper - Has Purchased'
);

	// ###########################################################################
	// Once development is completed and before the changes go live, this must be set to false
	// If it is not set to false and an unknown segment level is requested on the page, 
	// 	an alert box will display to the client.
segmentHelperObj.debuggingMode = bolDebugging;
	// ###########################################################################
	
	
segmentHelperObj.getCookie = function(cookie_name){
  	var results = document.cookie.match( '(^|;) ?'+cookie_name+'=([^;]*)(;|$)' );
	
  	if (results)
    	return ( unescape(results[2]) );
  	else
    	return null;
};

segmentHelperObj.cleanGoogle_utmvCookie = function(){
  	var results = segmentHelperObj.getCookie('__utmv');

	if (results)
	{
		return results.replace(/^\.?(([A-Za-z]|[0-9])*\.)*/,"");
	}
	else
	{
		return null;
	}
	
};
	
segmentHelperObj.trimStr = function(str){
		var whitespace=' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
		for(var i=0;i<str.length;i++){
			if(whitespace.indexOf(str.charAt(i)) === -1){
				str = str.substring(i);
				break;
			}
		}
		for(i=str.length-1;i>=0;i--){
			if(whitespace.indexOf(str.charAt(i)) === -1){
				str = str.substring(0, i + 1);
				break;
			}
		}
		return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
};

var segmentVisitor = function(inputSegmentationLvl)
{
	// grab the GA cookie var that stores the custom visitors segment type, and trim whitespace
	var currentSegmentationLvl = segmentHelperObj.cleanGoogle_utmvCookie(); //'__utmv'

	// ###########################################################################
	// Added this to see the cookie set by segmentVisitor
	// REMOVE WHEN YOU ARE DONE TESTING
if (segmentHelperObj.debuggingMode) // && (inputSegmentationLvl != 'No Action' || inputSegmentationLvl != 'null'))
	alert("__utmv segmentVisitor=" + segmentHelperObj.cleanGoogle_utmvCookie() + ". Trying to set it to:" + inputSegmentationLvl); 
	// ###########################################################################

	
	
	// if the current value is null (ie: not found) then set currentSegmentationLvl to empty string
	if(currentSegmentationLvl==null) currentSegmentationLvl = "";

	var numOfLevels = segmentHelperObj.visitorSegmentationLvls.length;
	var currentSegmentationIndex = numOfLevels;
	var newSegmentationIndex = numOfLevels;

	var tempSegmentationLvlLabel = "";
	
	//trim whitespace and covert to all upper case
	currentSegmentationLvl = (segmentHelperObj.trimStr(currentSegmentationLvl)).toUpperCase();
	var newSegmentationLvl = (segmentHelperObj.trimStr(inputSegmentationLvl)).toUpperCase();
	
	// loop through the segmentation lvls from lowest to highest
	for(var i=0;i<numOfLevels;i++){
		tempSegmentationLvlLabel = (segmentHelperObj.visitorSegmentationLvls[i]).toUpperCase();
		
		// is the current lvl equal to this lvl?
		if(currentSegmentationLvl === tempSegmentationLvlLabel){
			currentSegmentationIndex = i;
		}
		
		// is the new lvl equal to this lvl?
		if(newSegmentationLvl === tempSegmentationLvlLabel){
			newSegmentationIndex = i;
		}
		
		// are we done yet?
		if(currentSegmentationIndex < numOfLevels && newSegmentationIndex < numOfLevels){
			break;
		}
	}
	
	if(newSegmentationIndex == numOfLevels)
	{
		// check if we are in debugging mode
		if(segmentHelperObj.debuggingMode){
			alert('Segment Error: requested visitor level not in list of levels!. Please continue');
		}
		
		// make sure that the function isDefined first
		if(pageTracker._setVar){
			pageTracker._setVar('ERROR');
		}
	}
	// if the new lvl is greater than the current lvl OR the new level was found but the current level could not be found,
	// then call _setVar()
	else if(newSegmentationIndex < numOfLevels && (newSegmentationIndex > currentSegmentationIndex || currentSegmentationIndex == numOfLevels))
	{
		// make sure that the function isDefined first
		if(pageTracker._setVar){
			pageTracker._setVar(inputSegmentationLvl);
		}
	}

	// ###########################################################################
	// Added this to see the cookie set by segmentVisitor
	// REMOVE WHEN YOU ARE DONE TESTING
	if (segmentHelperObj.debuggingMode && 
			(segmentHelperObj.cleanGoogle_utmvCookie() != 'No Action' || segmentHelperObj.cleanGoogle_utmvCookie() != 'null') 
	   )	
			alert("__utmv segmentVisitor cookie is now:" + segmentHelperObj.cleanGoogle_utmvCookie()); 
		// ###########################################################################
	};

// ###########################################################################
// Auto Tagging downloads
// ###########################################################################

//	This javascript tags file downloads and external links in Google Analytics.
//	You need to be using the Google Analytics New Tracking Code (ga.js) 
//	for this script to work.
//	To use, call this file on all pages just above the closing body tag
//	(since all hrefs must have loaded before this script starts looking for them)

//	All outbound links and links to non-html files should now be automatically tracked.
//
//	This script has been provided by Goodwebpractices.com
//	Thanks to ShoreTel, MerryMan and Colm McBarron
//
//	www.goodwebpractices.com
//	VKI has made changes as indicated below.
function InvokeAutoTagging()
{
	if (document.getElementsByTagName) {
	        // Initialize external link handlers
	        var hrefs = document.getElementsByTagName("a");
	        for (var l = 0; l < hrefs.length; l++) {
					// try {} catch{} block added by erikvold VKI
				try{
		                //protocol, host, hostname, port, pathname, search, hash
		                if (hrefs[l].protocol == "mailto:") {
		                        startListening(hrefs[l],"click",trackMailto);
		                } else if (hrefs[l].hostname == location.host) {
		                        var path = hrefs[l].pathname + hrefs[l].search;
														// updated by erikvold VKI added \? in second group:
		                        var isDoc = path.match(/\.(?:pdf|doc)($|\&|\?)/);
		                        if (isDoc) {
		                                startListening(hrefs[l],"click",trackExternalLinks);
		                        }
		                } else {
		                        startListening(hrefs[l],"click",trackExternalLinks);
		                }
				}
				catch(e){
						continue;
				}
	        }
	}
	
	if (bolDebugging)	alert('auto-tagging invoked');
}
function startListening (obj,evnt,func) {
        if (obj.addEventListener) {
                obj.addEventListener(evnt,func,false);
        } else if (obj.attachEvent) {
                obj.attachEvent("on" + evnt,func);
        }
}

function trackMailto (evnt) {
        var href = (evnt.srcElement) ? evnt.srcElement.href : this.href;
        var mailto = "/mailto/" + href.substring(7);
        if (typeof(pageTracker) == "object") pageTracker._trackPageview(mailto);
}

function trackExternalLinks (evnt) {
        var e = (evnt.srcElement) ? evnt.srcElement : this;
        while (e.tagName != "A") {
                e = e.parentNode;
        }
        var lnk = (e.pathname.charAt(0) == "/") ? e.pathname : "/" + e.pathname;
        if (e.search && e.pathname.indexOf(e.search) == -1) lnk += e.search;
        if (e.hostname != location.host) lnk = "/external/" + e.hostname + lnk;
        if (typeof(pageTracker) == "object") pageTracker._trackPageview(lnk); 

		if (bolDebugging)	alert('onClickEvent' + lnk);

} 

/*
      var isDoc = path.match(/\.(?:doc|eps|jpg|png|svg|xls|ppt|pdf|xls|zip|txt|vsd|vxd|rar|exe|wma|mov|avi|wmv|mp3)($|\&)/);
*/
