// START RAcdn
var debug = false;
if (debug) {
	var a = "";
}
var raver = "1.0";	// RAcdn version number
var rarc = "";		// Referral Code
var rarcd = "";		// Referral Code DateTime
var raor = "";		// Original Referrer
var raord = "";		// Original Referrer DateTime
var rarr = "";		// Recent Referrer
var rarrd = "";		// Recent Referrer DateTime
var raol = "";		// Original Landing Page
var raold = "";		// Original Landing Page DateTime
var rarl = "";		// Recent Landing Page
var rarld = "";		// Recent Landing Page DateTime
var time_now = new Date();
var time_now_str = time_now.toLocaleString();
time_now.setFullYear(time_now.getFullYear() + 1);
var expires_utc = time_now.toUTCString();
// Searching the cookies
var obj_cookies = {};
var arr_cookies = document.cookie.split(";");
for (i = 0; i < arr_cookies.length; i++) {
	arr_pair = arr_cookies[i].split("=");
	if (arr_pair.length == 1) {
		// name present with empty value
		key = arr_pair[0].replace(/\s/g, "");
		obj_cookies[key] = "";
		if (key.search(/^__ra/) === 0) {
			eval(key.substr(2)+" = \"\"");
		}
	}
	else if (arr_pair.length == 2) {
		// name present with escaped value
		key = arr_pair[0].replace(/\s/g, "");
		obj_cookies[key] = arr_pair[1];
		if (key.search(/^__ra/) === 0) {
			eval(key.substr(2)+" = \""+unescape(arr_pair[1])+"\"");
		}
	}
	else if (arr_pair.length > 2) {
		// name present with unescaped value containing other "=" characters
		key = arr_pair[0].replace(/\s/g, "");
		val = "";
		for (j = 1; j < arr_pair.length; j++) {
			val += ((j > 1) ? "="+arr_pair[j] : arr_pair[j]);
		}
		obj_cookies[key] = val;
		if (key.search(/^__ra/) === 0) {
			eval(key.substr(2)+" = \""+val+"\"");
		}
	}
}
if (debug) {
	for (var k in obj_cookies) {
		if (k.search(/^__ra/) === 0) {
			a += "COOKIE "+k+"="+obj_cookies[k]+"\n";
		}
	}
	a += "\n";
}
/*  
	__rapid		Partner ID
	__rarc		Referral Code
	__rarcd		Referral Code DateTime
	__raor		Original Referrer
	__raord		Original Referrer DateTime
	__rarr		Recent Referrer
	__rarrd		Recent Referrer DateTime
	__raol		Original Landing Page
	__raold		Original Landing Page DateTime
	__rarl		Recent Landing Page
	__rarld		Recent Landing Page DateTime
*/
if (typeof obj_cookies.__raver == "undefined") {
	if (debug) {
		a += "SET __raver="+raver+"\n";
	}
	document.cookie = "__raver=" + raver + ";expires=" + expires_utc;
}
var rx = new RegExp("(http|https):\/\/" + window.location.host + "\/", "i");
var ext_site = ((document.referrer !== "") && (document.referrer.search(rx) == -1));
if (debug) {
	a += (ext_site) ? "EXTERNAL\n" : "INTERNAL\n";
}
// If the partner did not define the local rapid var, then check for the corresponding cookie, and assign any existing value to it
if ((typeof rapid == "undefined") && (typeof obj_cookies.__rapid != "undefined") && (obj_cookies.__rapid !== "")) {
	if (debug) {
		a += "SET rapid="+obj_cookies.__rapid+"\n";
	}
	rapid = obj_cookies.__rapid;
}
// If the __rapid cookie isn't there, but the local rapid var is, then create the cookie
if ((typeof obj_cookies.__rapid == "undefined") && (typeof rapid != "undefined") && (rapid !== "")) {
	if (debug) {
		a += "SET __rapid="+rapid+"\n";
	}
	document.cookie = "__rapid=" + rapid + ";expires=" + expires_utc;
}
if (debug) {
	a += "document.referrer "+((document.referrer === "") ? "EMPTY\n" : "NOT EMPTY\n");
	a += "raor "+((raor === "") ? "EMPTY\n" : "NOT EMPTY\n");
	a += "__raor "+((typeof obj_cookies.__raor == "undefined") ? "UNDEFINED\n" : "DEFINED\n");
}
if ((document.referrer !== "") && (raor === "") && (typeof obj_cookies.__raor == "undefined") && ext_site) {
	// We have a referrer, have NOT yet created the original referrer cookie, and we're coming from outside
	raor = document.referrer;
	raord = time_now_str;
	document.cookie = "__raor=" + escape(raor) + ";expires=" + expires_utc;
	document.cookie = "__raord=" + escape(raord) + ";expires=" + expires_utc;
	if (debug) {
		a += "SET raor="+document.referrer+"\n";
		a += "SET raord="+time_now_str+"\n";
		a += "SET __raor="+escape(raor)+"\n";
		a += "SET __raord="+escape(raord)+"\n";
	}
}
if ((document.referrer !== "") && ext_site) {
	// We have a referrer, and we're coming from outside, so ALWAYS set the recent referrer
	rarr = document.referrer;
	rarrd = time_now_str;
	document.cookie = "__rarr=" + escape(rarr) + ";expires=" + expires_utc;
	document.cookie = "__rarrd=" + escape(rarrd) + ";expires=" + expires_utc;
	if (debug) {
		a += "SET rarr="+document.referrer+"\n";
		a += "SET rarrd="+time_now_str+"\n";
		a += "SET __rarr="+escape(rarr)+"\n";
		a += "SET __rarrd="+escape(rarrd)+"\n";
	}
}
if (debug) {
	a += "raol "+((raol === "") ? "EMPTY\n" : "NOT EMPTY\n");
	a += "__raol "+((typeof obj_cookies.__raol == "undefined") ? "UNDEFINED\n" : "DEFINED\n");
}
if ((raol === "") && (typeof obj_cookies.__raol == "undefined") && ext_site) {
	// We have NOT yet set the original landing page cookie, and we're coming from outside
	raol = window.location.href;
	raold = time_now_str;
	document.cookie = "__raol=" + escape(raol) + ";expires=" + expires_utc;
	document.cookie = "__raold=" + escape(raold) + ";expires=" + expires_utc;
	if (debug) {
		a += "SET raol="+window.location.href+"\n";
		a += "SET raold="+time_now_str+"\n";
		a += "SET __raol="+escape(raol)+"\n";
		a += "SET __raold="+escape(raold)+"\n";
	}
}
if (ext_site) {
	// We're coming from outside, so ALWAYS set the recent landing page cookie
	rarl = window.location.href;
	rarld = time_now_str;
	document.cookie = "__rarl=" + escape(rarl) + ";expires=" + expires_utc;
	document.cookie = "__rarld=" + escape(rarld) + ";expires=" + expires_utc;
	if (debug) {
		a += "SET rarl="+window.location.href+"\n";
		a += "SET rarld="+time_now_str+"\n";
		a += "SET __rarl="+escape(rarl)+"\n";
		a += "SET__rarld="+escape(rarld)+"\n";
	}
}
// Searching the URL
var obj_urlargs = {};
var url_split = window.location.href.split("?");
if (url_split.length == 2) {
	arr_querystr = url_split[1].split("&");
	for (i = 0; i < arr_querystr.length; i++) {
		arr_pair = arr_querystr[i].split("=");
		if (arr_pair.length == 2) {
			obj_urlargs[arr_pair[0]] = arr_pair[1];
		}
		else {
			obj_urlargs[arr_pair[0]] = "";
		}
	}
}
if (debug) {
	a += "URL.ref "+((typeof obj_urlargs.ref == "undefined") ? "UNDEFINED\n" : "DEFINED\n");
	a += "URL.ref "+((obj_urlargs.ref === "") ? "EMPTY\n" : "NOT EMPTY\n");
}
var ref_in_url = false;
if ((typeof obj_urlargs.ref != "undefined") && (obj_urlargs.ref !== "")) {
	// We have a referral code in the URL, and it is not empty, so ALWAYS set the referral code cookie
	ref_in_url = true;
	rarc = obj_urlargs.ref;
	rarcd = time_now_str;
	document.cookie = "__rarc=" + escape(rarc) + ";expires=" + expires_utc;
	document.cookie = "__rarcd=" + escape(rarcd) + ";expires=" + expires_utc;
	if (debug) {
		a += "SET rarc="+obj_urlargs.ref+"\n";
		a += "SET rarcd="+time_now_str+"\n";
		a += "SET __rarc="+escape(rarc)+"\n";
		a += "SET __rarcd="+escape(rarcd)+"\n";
	}
}
if (debug) {
	alert(a);
}
if ((typeof rapid != "undefined") && (rapid !== "")) {
	if (ref_in_url === true && rarc !== "") {
		var imgTag = '<img style=\"display:none\" src=\"https://www.revagency.net/AP5/vendors/RAcdn/trackhit.cfm?rapid='+rapid+'&rarc='+rarc+'\" width=\"1\" height=\"1\" border=\"0\">';
		document.write(imgTag);
	} 
}
// END RAcdn