// Based on querystring.js by Adam Vandenberg http://adamv.com/dev/javascript/qslicense.txt

// optionally pass a cookiesString to parse
// adapted specifically for GA cookies to extract specific values from them
function gaVKIcookies(intGoogDomainHash, cookiesString) {
	this.params = {};

	if (cookiesString == null) cookiesString = document.cookie;
	if (cookiesString.length == 0) return;

	cookiesString = cookiesString.replace(/(\.|\|)utm/g,';utm'); // change components of  __utmz to independant name=value pairs
	var args = cookiesString.split(';'); 				// parse out name/value pairs separated via ;
	
		// split out each name=value pair
	for (var i = 0; i < args.length; i++) {
		var pair = args[i].split('=');

		var name = decodeURIComponent(pair[0]);
		name = name.replace(/^\s+|\s+$/g, '')
		var value = (pair.length=2)
			? decodeURIComponent(pair[1])
			: name;
		
			// in case page has access to > 1 domain that has __utm* cookies, we need to ensure we look only at the
			// cookies for the tracked (sub-)domain. 
			// Since JS has no access to read a cookie's domain, we need to matching on the required Google Domain Hash.  
			// All __utm* cookies begin with the domain hash = position 0
		if (value.indexOf(intGoogDomainHash) == 0)   {
			this.params[name] = value;
			var subValues = value.split('.');
		
			switch (name) {
				case '__utma': //domainhash.anonymizedVisitorID.ftime.ltime.stime.sessioncount
					this.params['domainhash'] = subValues[0];
					this.params['visitorId'] = subValues[1];
					this.params['ftime'] = subValues[2];
					this.params['ltime'] = subValues[3];
					this.params['stime'] = subValues[4];
					this.params['sessioncount'] = subValues[5];
					break;
				case '__utmb':	// eg .45.10.1218592192
					this.params['gif_hits'] = subValues[1];
					break;
				case '__utmv':
					this.params['custom'] = subValues[1];
					break;
				case '__utmz':	//nsession.nresponses
					this.params['nsession'] = subValues[2];
					this.params['nresponses'] = subValues[3];
					break;
				case 'utmcsr':
					this.params['trafficsource'] = subValues[0];
					break;
				case 'utmccn':	
					this.params['campaignname'] = subValues[0];
					break;
				case 'utmcmd':	
					this.params['campaignmedium'] = subValues[0];
					break;
				case 'utmctr':	
					this.params['campaignterm'] = subValues[0];
					break;
				case 'utmcct':	
					this.params['campaigncontent'] = subValues[0];
					break;
				case 'utmcid':	
					this.params['campaignid'] = subValues[0];
			}
		}
	}
}

gaVKIcookies.prototype.get = function(key, default_) {
	var value = this.params[key];
	return (value != null) ? value : default_;
}


gaVKIcookies.prototype.contains = function(key) {
	var value = this.params[key];
	return (value != null);
}
/*
Copyright (c) 2008, Adam Vandenberg
All rights reserved.

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, 
      this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright 
      notice, this list of conditions and the following disclaimer in the 
      documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/

function deleteCookie(strCookieName, strDomainName, strPath) {
	var strCookie = strCookieName + '=' + 
		  ';expires=1;path=' + strPath + 
		  ';domain=' + strDomainName;
	document.cookie = strCookie;
}
