/*
Name: Google Website Optimizer Google Analytics Integration Tool
Version: 1.2a for ga.js and urchin.js users
Original Author: Shawn Purtell
Algorithm: Ophir Prusak
Created: May 12, 2008
Description: Grabs data from Google Website Optimizer tracking cookie (__utmx) and returns the combination
			 number for use in the Google Analytics tracking functions.
			 Now features Error Handling for paused or completed experiments.
			 
Implementation:
--------------------------------------------------
FOR GA.JS ONLY
--------------------------------------------------
Simply add the following code immediately after the Google Analytics code (ga.js version) on the page:

GA CODE WOULD BE HERE
<!-- Begin Google Website Optimizer - Google Analytics Integration for ga.js !-->
<script src="http://www.yoursite.com/path/to/ga_gwo.js" type="text/javascript"></script>
<script>
var gwoComboTracker = _gat._getTracker("UA-XXXXXX-X");
getcombo_ga(experiment_num, experiment_name, "a-b-c-...");
</script>
<!-- End Google Website Optimizer - Google Analytics Integration for ga.js !-->

- Make sure the ga_gwo.js line is pointing to the right place.
- Replace the UA-XXXXXX-X string with your actual Google Analytics account number.

To properly use the getcombo function, you need to pass in the experiment number from GWO, your name for the experiment and
a string with the number of variations in each section, separated with dashes.
For example, if the first section has three variations, the second section has four variations and the third section has two variations you'd call the function as such:

getcombo_ga("4444455555", "ex1.1", "3-4-2");

If you do not pass in any parameter, it will use the same calculations as version 1.0, which did not require a parameter
-----------------------------------------------------
Modified by: Shawn Purtell on May 12, 2008
 Guy Atkinson Apr 23 2009  supporting multiple experiments on a site; added tests

*/

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 getcombo_ga(experiment_num, experiment_name, variations)
{
	if (document.cookie.indexOf("__utmx=") != -1)
	{
		var param = createcombo_ga(experiment_num, experiment_name, variations, readCookie('__utmx'));
		if (param) {
			var sPath = window.location.pathname;
			//var sPage = sPath.substring(sPath.lastIndexOf('\\') + 1);
			var sPage = sPath.substring(sPath.lastIndexOf('//') + 1);
			if (sPage.indexOf('?') == -1) {
				sPage +=  '?' + param; 
			} else {
				sPage +=  '&' + param;
			}
			//insert var gwoComboTracker = _gat._getTracker("UA-XXXXX-X"); before the getcombo_ga call;
			gwoComboTracker._initData();
			gwoComboTracker._trackPageview(sPage);
		}
	}
}

function createcombo_ga(experiment_num, experiment_name, variations, utmx_cookie_value)
{
		var cookie_data_array = utmx_cookie_value.split('.');	
		for (var i=1, l=cookie_data_array.length; i<l; i++) {
			var experiment_data_array = []
			var experiment_data_array = cookie_data_array[i].split(':');
			if (experiment_data_array[0].indexOf(experiment_num) != -1) {
				var combination_id = experiment_data_array[2];
				var ids = combination_id.split('-');
				var len = ids.length;
				var multiplier = [];
				var factor = 1;
				var combo = 0;
				if (variations != undefined) {
					multiplier = variations.split('-');
				}
	
				for(i=0; i<len; i++){
					combo += ids[i] * factor;
					factor = (multiplier[i] > 0) ? factor * multiplier[i] : Math.pow(len,i+1) ;
				}
				return "combo=" + experiment_name + ":" + combo + ":" + combination_id;
			}
		}
		return null;
}


/*
function test_createcombo_ga()
{
        document.writeln("<br>test_createcombo_ga");
	var param = createcombo_ga("4444455555", "ex1.1", "3-1-4", "1.00003926004444455555:4:1-0-0.00003433871597082510:2:0-0-1-0");
	document.writeln("<br>param=" + param + "; result=" + (param == "combo=ex1.1:1:1-0-0"));
	param = createcombo_ga("00003926004444455555", "ex1.1", "3-1-4", "1.00003433871597082510:2:0-0-1-0.00003926004444455555:4:0-0-1");
	document.writeln("<br>param=" + param + "; result=" + (param == "combo=ex1.1:3:0-0-1"));
	param = createcombo_ga("0000011111", "ex1.1", "3-1-4", "1.00003433871597082510:2:0-0-1-0.00003926004444455555:4:0-0-1");
	document.writeln("<br>param=" + param + "; result=" + (param == null));
}
*/


