/*
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 Shawn Purtell on 8/20/2008
*/

function noPercent(x)
{
	x = unescape(x);
	return x.replace(/\+/g," ").replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

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(name)
{
	var c2 = readCookie(name); 		//This gets the cookie
	var ca2 = c2.split('|');   		//This splits the cookie into parts
	
	//temp = ca2[0].split('.');  		//This grabs the first variable together with the numerical info
	//temp2 = temp[temp.length - 1]; 	// This takes only the variable we are interested in
	temp2 = ca2[0].substring(ca2[0].indexOf('utmcsr'));
	temp2 = temp2.substring(temp2.indexOf('utmgclid'));
	ca2[0] = temp2;					// We then replace the item in the array with just the variable data									
	
	var src = ' ';					//Will conatin 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 infor, 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';
			campaign = 'google';
			cancel = true;			//We don't want to reset the source, medium, or campaign info
		}

		if (temp4[0] == 'utmcsr' && !cancel)
		{
			src = temp4[1];
		}
		if (temp4[0] == 'utmccn' && !cancel)
		{
			campaign = temp4[1];
		}
		if (temp4[0] == 'utmcmd' && !cancel)
		{
			medium = temp4[1];
		}
		if (temp4[0] == 'utmctr')
		{
			term = noPercent(temp4[1]);
		}
	}		

	if (document.getElementById('campaign')) { document.getElementById('campaign').value = campaign; }
	if (document.getElementById('source')) { document.getElementById('source').value = src; }
	if (document.getElementById('medium')) { document.getElementById('medium').value = medium; }
	if (document.getElementById('term')) { document.getElementById('term').value = term; }

	if (document.getElementById('campaign_header')) { document.getElementById('campaign_header').value = campaign; }										
	if (document.getElementById('source_header')) { document.getElementById('source_header').value = src; }
	if (document.getElementById('medium_header')) { document.getElementById('medium_header').value = medium; }
	if (document.getElementById('term_header')) { document.getElementById('term_header').value = term; }
	
	if (document.getElementById('campaign_main')) { document.getElementById('campaign_main').value = campaign; }										
	if (document.getElementById('source_main')) { document.getElementById('source_main').value = src; }
	if (document.getElementById('medium_main')) { document.getElementById('medium_main').value = medium; }
	if (document.getElementById('term_main')) { document.getElementById('term_main').value = term; }			

	if (document.getElementById('campaign_main2')) { document.getElementById('campaign_main2').value = campaign; }										
	if (document.getElementById('source_main2')) { document.getElementById('source_main2').value = src; }
	if (document.getElementById('medium_main2')) { document.getElementById('medium_main2').value = medium; }
	if (document.getElementById('term_main2')) { document.getElementById('term_main2').value = term; }

}