// rwtag.js
// Version 0.8

	// Declare (global) setup object.
	var rwSetup = new rwSetup();

	// rwSetup - setup variables change for each implementation.
	function rwSetup() {
		this.rwVersion = "0.8";
		this.rwProfile = "evergreensuk"
		// Do not put in http:// for the tag gif.
		// GIF is required in root of website.
		this.rwTagServerGIF = "www.sptag2.com/rwtag.gif";
		this.rwCookieName = "rwtag";
		this.rwCreateCookie = true;
		this.rwCookieDays = 3650;
		this.rwCookieEnabledCheck = true;
		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( rwURL, rwPrefix ) {
		if (typeof(rwURL)=="string"){
			// Use current path and add document onto the end.
			var rwString = "rw.url=" + escape(location.href.substring(0, location.href.lastIndexOf("/")+1) + rwURL);
			// Current page is the referrer.
			rwString = rwString + "&rw.rf=" + escape(location.href);
			if (typeof(rwPrefix)=="string"){
				rwString = rwPrefix + "&" + rwString;
			}
			// Now call the tag server.
			rwTag(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, null, 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 rwMetaTagString = "";
		var rwMetaTags;
		if (document.all){
			rwMetaTags=document.all.tags("meta");
		} else if (document.documentElement){
			rwMetaTags=document.getElementsByTagName("meta");
		}
		if (typeof(rwMetaTags)!="undefined"){
			for (var i=1;i<=rwMetaTags.length;i++){
				rwMeta=rwMetaTags.item(i-1);
				if (rwMeta.name){
					if (rwMeta.name.indexOf('rw.')==0){
						rwMetaTagString = rwMetaTagString + "&";
						rwMetaTagString = rwMetaTagString + rwMeta.name+"="+ escape(rwMeta.content);
					}		
				}
			}
		}
		return rwMetaTagString;
	}
	
	// rwParameters - return parameter string with unique value and the referrer.
	// Unique value required to pass through proxy server.
	function rwParameters(){
		var rwParameters = "";
		var rwNow = new Date();
		rwParameters = rwParameters + "rw.nw="+ rwNow.getTime();
		rwParameters = rwParameters + "&rw.rf=" + escape(window.document.referrer);		
		return rwParameters;
	}

	// rwTag - Generate full URL and request image so hit is made onto tag server.
	function rwTag( rwTagPrefixString ){		
		var rwTagHTML = rwURL( rwTagPrefixString );
		if (rwTagHTML.length>2048&&navigator.userAgent.indexOf('MSIE')>=0){
			rwTagHTML =rwTagHTML.substring(0,2040)+"&rwEnd=1";
		}
		

			
		if (document.images){
			// Get image into variable without displaying on form.
			var rwImage = new Image();
			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( rwTagPrefixString ){
		
		var rwPrefix = "";
		if (typeof rwTagPrefixString == "string"){
			rwPrefix = rwTagPrefixString;
		} 
		// Check for global parameter (comes before prefix).
		if (typeof rwTagPrefix== "string") {
			rwPrefix = rwTagPrefix + "&" + rwPrefix;
		}
		// Protocol is http or https.
		var rwURL = "http"+(window.location.protocol.indexOf('https:')==0?'s':'')+"://";		
		rwURL = rwURL + rwSetup.rwTagServerGIF;		
		if (rwPrefix.length > 0) {
			rwURL = rwURL + "?" + rwPrefix + "&rw.pf=" + rwSetup.rwProfile + "&" + rwParameters() + rwMetaTags();
		} else {
			rwURL = rwURL + "?rw.pf=" + rwSetup.rwProfile + "&" + rwParameters() + rwMetaTags();
		}
		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.
			rwURL = rwURL + "&" + rwCookieTag();
		}			
		if (rwSetup.rwCookieEnabledCheck == true) {
			if (typeof document.cookie == "string"){
				if (document.cookie.length == 0){
					var rwEnabled = rwCheckCookieEnabled();
					if (rwEnabled == false){
						rwURL = rwURL + "&rw.ckstatus=disabled";
					}
				}
			}
		}		
		// 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 enabled (also from o'reilly).
	function rwCheckCookieEnabled() {
		var rwCookieFlag = false;
		if (typeof document.cookie == "string"){
			if (document.cookie.length == 0){
				document.cookie = "rwCheckCookie";
				if (document.cookie == "rwCheckCookie"){
					rwCookieFlag = true;
				}
				document.cookie = "";
			} else {
				rwCookieFlag = true;
			}
		}
		return rwCookieFlag;	
	}
	
	/* 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";
		}
	}

