	// Based on querystring.js by Adam Vandenberg http://adamv.com/dev/javascript/qslicense.txt

	 // optionally pass a this.cookiesString to parse
	 // adapted specifically for GA cookies to extract specific values from them
function gaVKIcookies(intGoogDomainHash, strCookies, strDefaultDomainName) {
	this.params = {};
	this.cookiesString = strCookies;
	
	if (strDefaultDomainName) this.strDefaultDomainName = strDefaultDomainName; // Added all strDefaultDomainName to object 3:17 PM 12/02/09
	
	if (this.cookiesString == null) this.cookiesString = document.cookie;
	if (this.cookiesString.length == 0) return;

		// extract only those cookies for this domain
			// pattern to find only the __utm cookies with the appropriate domain hash
	var rePattern = new RegExp('(__utm[a-z]{1,2}=' + intGoogDomainHash + '[^;]*)', "g"); 
			// recreate cookiestring by creating an array of selected cookies and joining with ; delimiter
	var aryMatches = this.cookiesString.match(rePattern,"$1");
	if (aryMatches) 
		this.cookiesString = aryMatches.join(';'); 
	else {
		this.cookiesString = '';
		return;
	}
	
		// change components of  __utmz to independant name=value pairs
	this.cookiesString = this.cookiesString.replace(/(\.|\|)utm/g,';utm'); 

		// parse out name/value pairs separated via ;
	var args = this.cookiesString.split(';'); 				
	
		// split out each name=value pair
	for (var i = 0; i < args.length; i++) {
	
		var pair = args[i].match(/([^=]*)=(.*)/) ;  // Don't use: pair = args[i].split('=') since value could contain a 2nd =
													// Thanks Danny Ng of www.firstrate.com.au
		var name = decodeURIComponent(pair[1]);
		name = name.replace(/^\s+|\s+$/g, '')
		var value = (pair.length>=2)
			? decodeURIComponent(pair[2])
			: '';
		
			// 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
			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':
					if (subValues.length > 1) {
						this.params['trafficsource'] = '';
						for (subValIdx = 0; subValIdx < subValues.length; subValIdx++) {
							if (subValIdx > 0)
								this.params['trafficsource'] += '.';
								
							this.params['trafficsource'] += subValues[subValIdx];
						}
					}
					else
						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.writeCookie = function(strCookieName, value, strDomainName, strPath, hours) {
	var d = new Date();
	strDomainName = strDomainName || this.strDefaultDomainName;
	
	d.setTime(d.getTime() + (hours*60*60*1000));
	var strExpiry = (hours == 0) ? 0 : d.toGMTString();
	
	var strCookie = strCookieName + '=' + value + 
		  ';expires=' + strExpiry + ';path=' + strPath + 
		  ';domain=' + strDomainName;
	document.cookie = strCookie;
} 

gaVKIcookies.prototype.set = function(strCookieName, value, strDomainName, strPath, hours) {
	this.writeCookie(strCookieName, value, strDomainName, strPath, hours);
}

gaVKIcookies.prototype.contains = function(key) {
	var value = this.params[key];
	return (value != null);
}

gaVKIcookies.prototype.formatDateTime = function(strSession) {
	var value = this.params[strSession]
	var d = new Date(value*1000);

	strDate = d.toLocaleDateString();
	strDate = strDate.replace(/[a-z]{3}.*[, ]+([a-z]{3})[^, ]*/i,'$1');
	strDate += ' ' + d.toLocaleTimeString();
	
	return strDate;
}

gaVKIcookies.prototype.dumpDebug = function(isDebug) {

	var strDump = '';
	
	if (document.location.href.indexOf('php') !==-1 || isDebug || (document.location.search.indexOf('debug=y') !== -1) && document.cookie) {
		var aryCookies = document.cookie.split(';');
		aryCookies.sort();
		strDump  = ('<br /><hr /><b>document.cookie:</b><br />' + unescape(aryCookies.join('<br />')) );
		strDump += ('<br /><br /><b>gaVKIcookies string. Relevant Domain Hash only:</b><br />' + unescape(this.cookiesString.replace(/;/g, ';<br />')) + '<hr />');
	}
	return strDump;
}

//-----------------------------------
getCookie = function(strCookieName, strDefault) {
	try {
		var strCookieValue = document.cookie.match(strCookieName + '=' + '(([^;])*)')[1];
	} 
	catch (e) {
		var strCookieValue = strDefault;
	}
	
	return strCookieValue;
}

deleteCookie = function(strCookieName, strDomainName, strPath) {
/*
	var strCookie = strCookieName + '=' + 
		  ';expires=1' + //(new Date().getTime()-1000) + 
		  ';path=' + strPath + 
		  ';domain=' + strDomainName;
	document.cookie = strCookie;
*/
		writeCookie(strCookieName, '', strDomainName, strPath, -1); 
}

deleteAllCookies = function(strDomainName){
    var new_date = new Date();
	var aryCookie;
	var strCookie;
    new_date.setTime(1);//.toGMTString();
	new_date = new_date.toGMTString()

    var thecookie = document.cookie.split(";");
    for (var i = 0;i < thecookie.length;i++) 
    {
		aryCookie = thecookie[i].split('=');
		deleteCookie(aryCookie[0], strDomainName, '');
		// strCookie = aryCookie[0] + "=;expires=" + new_date + ';domain=';// + strDomainName;
        // document.cookie = strCookie;
	}
}

writeCookie = function(strCookieName, value, strDomainName, strPath, hours) {
	var d = new Date();
	d.setTime(d.getTime() + (hours*60*60*1000));
	var strExpiry = (hours == 0) ? 0 : d.toGMTString();
	
	var strCookie = strCookieName + '=' + value + 
		  ';expires=' + strExpiry + ';path=' + strPath + 
		  ';domain=' + strDomainName;
	document.cookie = strCookie;
}

/*
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.
*/


