/*
Date:    2011-10-26
 
Written by:  Ecomerce Intelligence.  www.ecomiq.com
 
Updates: 
	2010-08-23:
		- Added updates to track different virtual pageviews for the Rquest a Quote thankyou page based on the referrer.  
		This is to help in the creation of different funnels that use the same thankyou page.
		- Updated code to respect global JS variables pageCategoryGA and pageNameGA.
	2010-12-22:
		- Added tracking code entries for 2 new sites:
		shopkonicaminolta.us
		colorfultomorrow.org
	2011-10-26:
		- Added tracking code entries for commerce.mykonicaminolta.com
	2012-01-15:
		- Added accounts for kmbscontent.konicaminolta.us and wapps.mykonicaminolta.com
 
	Usage: 	This file should be included in each page of each website that is to 
 				be tracked in Google Analytics.  It should be included towards the top of each page 
 				just after the closing head tag </head>
 
	Purpose: This script does the following:
		- 	Determiness the correct Google Analytics account ID and makes all 
 			appropriate page tracking calls for the various konica minolta web properties. 
 			The account id is determined by the hostname the user's session begins on. 
*/

// The names of the cookies that store the primary and secondary Google Analytics account IDs.
var cookieNames 		= {'primary': 'primaryAcc', 'secondary' : 'secondaryAcc'};

// catchAllAcc is to be used if we can not get a primary account based on either the hostname.
// This is intened to be used to find traffic that is slipping through the cracks.  A properly functioning tracking stragegy would mean this proile remains empty.
var catchAllAcc 			= 'UA-7525673-10';

// Mapping of domain to GA Account Ids.  
var Domain2Accs = {

	// KM Properties
	'kmbs.konicaminolta.us'			:{'primary'	:'UA-7525673-5',	'secondary'	:''},
	'kmbsapps.konicaminolta.us'		:{'primary'	:'UA-7525673-6',	'secondary'	:''},
	'mykonicaminolta.com'			:{'primary'	:'UA-7525673-7',	'secondary'	:''},
	'participant.mykonicaminolta.com'	:{'primary'	:'UA-7525673-8',	'secondary'	:''},
	'util.mykonicaminolta.com'		:{'primary'	:'UA-7525673-9',	'secondary'	:''},
	'shopkonicaminolta.us'			:{'primary'	:'UA-7525673-11',	'secondary'	:''},
	'colorfultomorrow.org'			:{'primary'	:'UA-7525673-12',	'secondary'	:''},
	'commerce.mykonicaminolta.com'		:{'primary'	:'UA-7525673-14',	'secondary'	:''},
	'kmbscontent.konicaminolta.us'		:{'primary'	:'UA-7525673-15',	'secondary'	:''},
	'wapps.mykonicaminolta.com'		:{'primary'	:'UA-7525673-16',	'secondary'	:''}
};

var _gaq = _gaq || [];


//=============================================================================
//===================== Do Not Edit Below This ================================
//=============================================================================

var primaryAcc 	= false;
var primaryTracker;

primaryAcc = getAcc('primary');

if(! primaryAcc){
	primaryAcc = catchAllAcc;
}

// Set Custom Var and/or call virtual pageview if the javascript variables pageCategoryGA and/or pageNameGA are set.
if(primaryAcc){
	_gaq.push(['_setAccount', primaryAcc]);

	if(typeof(pageCategoryGA) != "undefined" && pageCategoryGA.length){
		_gaq.push(['_setCustomVar',1,'Page Category',pageCategoryGA,3]);
	}

	if(typeof(pageNameGA) != "undefined" && pageNameGA.length){
		_gaq.push(['_trackPageview',pageNameGA]);
	}else{
		_gaq.push(['_trackPageview']);
	}
}


// Set virtual page views for tracking request a quote funnel.
var cDomain = getCurrentDomain();
 if(cDomain == 'kmbs.konicaminolta.us'){
	if(window.location.toString().match('/content/rfq/rfq_conf.html')){
		if(document.referrer.match('zip=')){
			_gaq.push(['_trackPageview','/content/rfq/fromsaleslocator/rfq_conf.html']);
		}else{
			_gaq.push(['_trackPageview','/content/rfq/fromnonsaleslocator/rfq_conf.html']);
		}
	}
}


function getCurrentDomain(){
        var url = window.location.toString();
        url = cleanURL(url);
        return url;
}

function cleanURL(url){
        url = url.replace(/http[s]?:\/\//,'');
        url = url.replace(/www\./,'');
        if(url.indexOf('?')>=0){
           url = url.substring(0,url.indexOf('?'));
        }
        if(url.indexOf('/')>=0){
           url = url.substring(0,url.indexOf('/'));
        }
        url = url.toLowerCase();
        return url;
}

function createCookie(domain,name,value,days) {
        if (days) {
                var date = new Date();
                date.setTime(date.getTime()+(days*24*60*60*1000));
                var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/; domain=" + domain ;
}

function getCookieVal(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 false;
}


// type = ['primary' | 'secondary' ];
function getAcc(type){

	// See if cookie is set
	var accountId =  getCookieVal(cookieNames[type]);

	// Get from Current Domain name
	if (! accountId){
		var cDomain 	= getCurrentDomain();
		var data 	= Domain2Accs[cDomain];
		if(data){
			accountId = data[type];
		}
		if (accountId){
			createCookie(cDomain,cookieNames[type],accountId);
		}
	}
	return accountId;
}

(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

