var hash = getUrlVars();
var redirect = "http://www.pmi.org/community/Practitioner.html";
var path = "http://analyst.deardorffassociates.com/tracking/logvisit.aspx";
var gc = "PR001XX01";
var ct = "";
var visitorCode = "";
var date = new Date();
var ticks = "";
var dmn = "";
			
function init() {
	ticks = date.getTime();
	
	// clicktag is present so use that to make the cookie
	if (hash['ct'] != '' && hash['ct'] != null) {
		ct = hash['ct'];
		vc = ticks;
		dmn = baseDomainString();
		createCookie();
	// clicktag is not present so look for a cookie
	} else {
		ct = readCookie("ct");
		vc = readCookie("vc");
		dmn = readCookie("dmn");
		
		// cookie is present - just check if the domains match
		if (ct != "" && vc != "" && ct != null && vc != null) {
			// domains do not match so create a new cookie with generic values
			if (dmn != baseDomainString()) {
				ct = gc;
				vc = ticks;
				dmn = baseDomainString();
				createCookie();
			// domains match so do nothing
			} else {
			
			}
		// cookie is not present so setup generic values	
		} else {
			ct = gc;
			vc = ticks;
			dmn = baseDomainString();
			createCookie();
		}
	}
	
	getUrl(path, redirect, ct, vc);
}

function createCookie() {
	document.cookie = "ct=" + ct + "; expires=''; path=/";
	document.cookie = "vc=" + ticks + "; expires=''; path=/";
	document.cookie = "dmn=" + dmn + "; expires=''; path=/";
}

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 getUrlVars() {
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('#') + 1).split('&');
	for(var i = 0; i < hashes.length; i++) {
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}

function getUrl(path, redirect, ct, vc) {
	window.location.href = path + "?rd=" + redirect + "&ct=" + ct + "&vc=" + vc; 
}

function baseDomainString(){
  e = document.domain.split(/\./);
  if(e.length > 1) {
    return("domain=" + e[e.length-2] + "." +  e[e.length-1]) + ";"  ;
  }else{
    return("");
  }
}

init();