var cookieVersion = "1.0";
var uRCookie = getCCookieVal('value');
var uRIdCookieUserId = getCookieKeyValue(uRCookie, 'user_id', 'ZZ');
var uRIdCookieEmail = getCookieKeyValue(uRCookie, 'email', 'ZZ');
var userIdCookie = getCCookieVal('userIdCookie');
var userIdCookieUserId = getCookieKeyValue(userIdCookie, 'userId', 'ZZ');
var userIdEmail = getCookieKeyValue(userIdCookie, 'email', 'ZZ');
var primaryDomain = getPrimaryDomain();

var now = new Date();
now.setTime(now.getTime());
expire_days = 10000 * 1000 * 60 * 60 * 24;
var expiration_dt = new Date(now.getTime() + (expire_days));

//if the userId cookie does not exist
if (userIdCookieUserId == null) {
	
	// and UR cookie exists
	if (uRIdCookieUserId != null) {

		//set userId with that value and current create date
		var cookieVal = 'userIdZZ' + uRIdCookieUserId + 'ZZemailZZ' + uRIdCookieEmail + 'ZZcreateDateZZ' + now + 'ZZcookieVersionZZ' + cookieVersion + 'ZZ';

	// else UR cookie does not exist
	} else {

		//so create anonymous id and current create date
		var anonId = "-" + getRandNumber(10);
		var cookieVal = 'userIdZZ' + anonId + 'ZZemailZZ' + uRIdCookieEmail + 'ZZcreateDateZZ' + now + 'ZZcookieVersionZZ' + cookieVersion + 'ZZ';
	}

	setCCookie('userIdCookie', cookieVal, expiration_dt, '/', primaryDomain, '');

// else userId cookie already exists
} else {
	
	//so check if UR cookie exists 
	if (uRIdCookieUserId != null) {
		
		//but if userId value does not match UR user_id value or userIdEmail is not set
		if ((userIdCookieUserId != uRIdCookieUserId) || (userIdEmail != uRIdCookieEmail)) {
		
			// then set the userId cookie to use UR value
			var cookieVal = 'userIdZZ' + uRIdCookieUserId + 'ZZemailZZ' + uRIdCookieEmail + 'ZZcreateDateZZ' + now + 'ZZcookieVersionZZ' + cookieVersion + 'ZZ';
			setCCookie('userIdCookie', cookieVal, expiration_dt, '/', primaryDomain, '');	
		}
	}

}

var userIdCookie = getCCookieVal('userIdCookie');
//Check cookie again and declare final values:
var userIdCookieUserId = getCookieKeyValue(userIdCookie, 'userId', 'ZZ');
var userIdEmail = getCookieKeyValue(userIdCookie, 'email', 'ZZ');
var userIdCookieCreateDt = getCookieKeyValue(userIdCookie, 'createDate', 'ZZ');
var userIdCookieVersion = getCookieKeyValue(userIdCookie, 'cookieVersion', 'ZZ');

function GetUserId() {
	var userIdCookie = getCCookieVal('userIdCookie');
}

function getRandNumber(numDigits) {
	var randNum = "";
	var thisDigit = "";
	for (var i=0; i<numDigits; i++) {
		thisDigit = Math.floor(Math.random()*10);
		randNum = randNum + thisDigit;		
	}
	return randNum;
}

function getPrimaryDomain() {
	var theUrl = document.domain;
	var urlLength = theUrl.length;
	var firstDot = theUrl.lastIndexOf(".");
	var secondDot = theUrl.lastIndexOf(".", firstDot - 1);
	var primaryDomain = theUrl.substr(secondDot);
	return primaryDomain;
}

function setCCookie(cookie_name, cookie_value, expiration, path, domain, secure) {

	document.cookie = cookie_name + "=" + escape( cookie_value ) +
	( ( expiration ) ? ";expires=" + expiration.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure=" : "" );

}

function getCookieKeyValue(cookie_value, key, delimeter) {
	if (cookie_value != null) {
		var keystring = key + delimeter;
		var thiscookie_start = cookie_value.indexOf(keystring) + keystring.length;
		var thiscookie_end = cookie_value.indexOf(delimeter, thiscookie_start);
		var keyvalue = cookie_value.substring(thiscookie_start, thiscookie_end);
		return keyvalue; 
	} else {
		return null;
	}
}

function getCCookieVal(cookie_name) {
	if (document.cookie.length>0) {
		cookie_start = document.cookie.indexOf(cookie_name + "=")
			if (cookie_start != -1) {
				cookie_start = cookie_start + cookie_name.length + 1;
				cookie_end = document.cookie.indexOf(";", cookie_start);
				if (cookie_end == -1) {
					cookie_end = document.cookie.length;
					return unescape(document.cookie.substring(cookie_start, cookie_end));
				} else {
					return unescape(document.cookie.substring(cookie_start, cookie_end));
				}
			} 
	} else {
		return null;
	}
}
