// rwtag.js
// Version 0.9.2

// Declare (global) setup object.
var rwSetup = new rwSetup();
var rwImage = new Image;

// Routine to pick up exisiting PPC parameters. Adapt and uncomment code as necessary.
// Regular expression matches referrersiteID=xxx and referreriste=xxx (typo)
var rwTagPrefix
rwGrep = new RegExp("referrer..teID=([^&]+)");
if (rwGrep.test(location.href)) {
	matches = location.href.match(rwGrep);
	rwTagPrefix="rw.cm="+matches[1]+",PPC,";
}


// rwSetup - setup variables change for each implementation.
function rwSetup() {
	this.rwVersion = "0.9.2";
	this.rwProfile = "iglu";
	// Do not put in http:// protocol for the tag gif.
	// GIF is required in root of website.
	this.rwTagServerGIF = "www.sptag.com/rwtag.gif";
	this.rwCheckMetaTags = true;
	this.rwCookieName = "";
	this.rwCreateCookie = false;
	this.rwCookieDays = 0;
	this.rwCookieEnabledCheck = true;
	// If using first party cookies across subdomains, set this.rwCookieDomain = ".domain.com";
	this.rwCookieDomain = null;
	this.rwSendTitle = false;
	this.rwDebug = false;
}

// rwAddTag - Creates an additonal tag for extra downloads, events etc.
// Pass in page name as parameter: rwAddTag('document.prf').
function rwAddTag( rwPage, rwPrefix ) {
	if (typeof(rwPage)=="string" && rwPage.length>0){
		// Tag has url tag determined from current path and referrer is the currnent page.
		var rwString = "http"+(window.location.protocol.indexOf('https:')==0?'s':'')+"://" + rwSetup.rwTagServerGIF;
		if (typeof(rwPrefix)=="string" && rwPrefix.length>0){
			rwString = rwString + "?" + rwPrefix + "&" + rwParameters();
		} else {
			rwString = rwString + "?" + rwParameters();
		}
		rwString = rwString + "&rw.url=" + escape(location.href.substring(0, location.href.lastIndexOf("/")+1) + rwPage) + "&rw.rf=" + escape(location.href);
		
		// Now call the tag server.
		return rwTagServer(rwString);
	}
}


// rwCookieTag - Returns cookie string value as RW.CK tag according to RWCOOKIENAME.
// RWCREATECOOKIE and RWCOOKIEDAYS determine whether a cookie is created if absent.
function rwCookieTag(){
	var rwCookieTagString = "";
	var rwCookieName = rwSetup.rwCookieName
	if (rwCookieName.length > 0){
		rwCookieTagString = rwGetCookie(rwCookieName);
		if (typeof(rwCookieTagString)=="string"){
			// Cookie OK so setup tag value.
			rwCookieTagString = "rw.ck=" + rwCookieTagString; 
		} else {
			if (rwSetup.rwCreateCookie == false){
				// Cookie should be there but is absent and creation not specified.
				rwCookieTagString = "rw.ck=";
			} else {
				// Create new cookie.
				var rwNowString = new Date().getTime();
				var rwCookieString = "rwtag." + location.href.length + "." + navigator.userAgent.length + "." + rwNowString;
				if (rwSetup.rwCookieDays > 0) {
					rwSetCookie( rwCookieName, rwCookieString, rwGetExpDate(rwSetup.rwCookieDays, 0, 0), null, rwSetup.rwCookieDomain, null);
				} else {
					// Sets cookie for session only (useless really).
					rwSetCookie( rwCookieName, rwCookieString, null, null, null, null);
				}
				// Check that cookie has been created.
				rwCookieTagString = rwGetCookie(rwCookieName);
				if (typeof(rwCookieTagString)=="string"){
					rwCookieTagString = "rw.ck=" + rwCookieTagString + "&rw.ckcreated=" +  rwNowString;
				} else {
					// Cannot create new cookie.
					rwCookieTagString = "rw.ck=&rw.ckstatus=disabled";
				}
			}
		}
	}
	return rwCookieTagString;
}
	
// rwMetaTags - return all meta tags with prefix rw. in an ampersand delimited string.
function rwMetaTags(){		
	var rwMT = "";
	var rwMTags;
	if (document.all){
		rwMTags = document.all.tags("meta");
	} else if (document.documentElement){
		rwMTags = document.getElementsByTagName("meta");
	}
	if (typeof(rwMTags)!="undefined"){
		for (var i=1;i<=rwMTags.length;i++){
			var rwMTag=rwMTags.item(i-1);
			if (rwMTag.name){
				if (rwMTag.name.indexOf('rw.')==0){
					rwMT=rwMT+"&"+rwMTag.name+"="+escape(rwMTag.content);
				}
			}
		}
	}
	return rwMT;
}
	
// rwParameters - return profile tag, cookie information for identifying user, and cache busting parameters.
function rwParameters(){
	var rwNow = new Date();
	var rwString = "rw.pf=" + rwSetup.rwProfile + "&rw.nw="+ rwNow.getTime();	
	if (rwSetup.rwCookieName.length>0){
		// User defined cookie name (should be a persistent and not a session cookie).
		// May also create the cookie if required.
		rwString = rwString + "&" + rwCookieTag();
	}			
	if (rwSetup.rwCookieEnabledCheck == true) {
		var rwEnabled = rwIsCookieEnabled();
		if (rwEnabled == false){
			rwString = rwString + "&rw.ckstatus=disabled";
		}
	}	
	return rwString;	
}

// rwTag - Called by end user to generate log record on tag server.
function rwTag( rwPrefix ){			
	return rwTagServer( rwURL( rwPrefix ));
}
	
// rwTagServer - Request URL from tag server.
function rwTagServer( rwURLString ){	
	var rwTagHTML = rwURLString;
	if (rwTagHTML.length>2048&&navigator.userAgent.indexOf('MSIE')>=0){
		rwTagHTML=rwTagHTML.substring(0,2040)+"&rw.End=1";
	}
	if (document.images){
		// Get image into variable without displaying on form.
		// Make sure image variable is global.
		rwImage.src = rwTagHTML;
	} else {
		// Alternatively write an image request in HTML to the page.
		// Note that IMG string must be split to prevent browser problems.
		rwTagHTML = '<I' + 'MG BORDER=\"0\" NAME=\"rwtag\" WIDTH=\"1\" HEIGHT=\"1\" SRC=\"' + rwTagHTML + ' \" >' ;
		document.write( rwTagHTML );
	}
	// 0.8 Debug.
	if (rwSetup.rwDebug == true) {
		//alert(rwTagHTML);
		document.write("<br/><b>"+rwTagHTML+"<b/>");
	}
	return rwTagHTML;
}	


// rwURL - return the tag URL for HTTP or HTTPS.
function rwURL( rwPrefix ){
	var rwURLPrefix = "";
	// Check for global parameter (comes before prefix).
	if (typeof rwTagPrefix== "string") {
		rwURLPrefix = rwTagPrefix;
	}
	if (typeof rwPrefix == "string"){
		rwURLPrefix = rwURLPrefix + "&" + rwPrefix;
	} 
	// Protocol is http or https.
	var rwURL = "http"+(window.location.protocol.indexOf('https:')==0?'s':'')+"://" + rwSetup.rwTagServerGIF;		
	if (rwURLPrefix.length > 0) {
		rwURL = rwURL + "?" + rwURLPrefix + "&" + rwParameters() + "&rw.rf=" + escape(window.document.referrer);
	} else {
		rwURL = rwURL + "?" + rwParameters() + "&rw.rf=" + escape(window.document.referrer);
	}
	
	if (rwSetup.rwCheckMetaTags == true){
		// No need for & seperator.
		rwURL = rwURL + rwMetaTags();
	}	
	// Optionally send the title as the last element (it may be truncated).
	if (rwSetup.rwSendTitle == true) {
		rwURL = rwURL + "&rw.ti=" + escape(document.title);
	}
	return rwURL;
}
	
	
// Return true if cookies are disabled (also from o'reilly).
function rwIsCookieEnabled() {
	var rwEnabled = false;
	if (typeof document.cookie == "string"){
		if (document.cookie.length == 0){
			document.cookie = "rwCheckCookie";
			if (document.cookie == "rwCheckCookie"){
				rwEnabled = true;
			}
			document.cookie = "";
		} else {
			rwEnabled = true;
		}
	}
	return rwEnabled;	
}
	
	/* cookies.js */
	/*
		 Example File From "JavaScript and DHTML Cookbook"
		 Published by O'Reilly & Associates
		 Copyright 2003 Danny Goodman
	*/
	// getCookierVal renames to rwGetCookie.
	// getCookie renamed to rwGetCookieString.
	
	// utility function to retrieve a future expiration date in proper format;
	// pass three integer parameters for the number of days, hours,
	// and minutes from now you want the cookie to expire; all three
	// parameters required, so use zeros where appropriate
	function rwGetExpDate(days, hours, minutes) {
		var expDate = new Date();
		if (typeof days == "number" && typeof hours == "number" && typeof hours == "number") {
			expDate.setDate(expDate.getDate() + parseInt(days));
			expDate.setHours(expDate.getHours() + parseInt(hours));
			expDate.setMinutes(expDate.getMinutes() + parseInt(minutes));
			return expDate.toGMTString();
		}
	}	
	// utility function called by rwGetCookieVal()
	function rwGetCookieString(offset) {
		var endstr = document.cookie.indexOf (";", offset);
		if (endstr == -1) {
			endstr = document.cookie.length;
		}
		return unescape(document.cookie.substring(offset, endstr));
	}	
	// primary function to retrieve cookie by name
	function rwGetCookie(name) {
		var arg = name + "=";
		var alen = arg.length;
		var clen = document.cookie.length;
		var i = 0;
		while (i < clen) {
			var j = i + alen;
			if (document.cookie.substring(i, j) == arg) {
				return rwGetCookieString(j);
			}
			i = document.cookie.indexOf(" ", i) + 1;
			if (i == 0) break; 
		}
		return null;
	}	
	// store cookie value with optional details as needed
	function rwSetCookie(name, value, expires, path, domain, secure) {
			document.cookie = name + "=" + escape (value) +
			((expires) ? "; expires=" + expires : "") +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			((secure) ? "; secure" : "");
	}
	// remove the cookie by setting ancient expiration date
	function rwDeleteCookie(name,path,domain) {
		if (rwGetCookieVal(name)) {
			document.cookie = name + "=" +
				((path) ? "; path=" + path : "") +
				((domain) ? "; domain=" + domain : "") +
				"; expires=Thu, 01-Jan-70 00:00:01 GMT";
		}
	}

