// tagging.js
// Developed on behalf of Marketwave.
// October 2007
// Use this var to identify the tag version for internal purposes. This will be available for reporting.
var hl_tagcodebase="tng4_20071005-1200";
/* New in tng4_20071005-1200
  added functionality to set the tagurl based on domain
    Logic: For Production: use metrics.stjude.org for all else, use a8.dev.alsac.stjude.org
  added logic to support http or https based on current protocol
  broke down the hl_tagurl into multiple vars (protocol, domain and path) for better management
*/
var hl_tagversion=1; // DO NOT CHANGE

var tagurlProd="metrics.stjude.org";
var tagurlnonProd="a8.dev.alsac.stjude.org";
var tagurlPath="/HLTag/tng/HLTag.gif";

var currentDomain=document.location.hostname;
var currentProto=document.location.protocol;

switch (currentDomain) {
  case "www.tg.stjude.org": // Production
    tagurlDomain=tagurlProd;
    break;
  default: // all else
    tagurlDomain=tagurlnonProd;
    break;
}

var hl_tagurl=currentProto+'//'+tagurlDomain+tagurlPath;
function hl_tag(){
	/* First, check if parameters are being passed and set the values
	Param 1 = uri (no domain) for the page that will be displayed - start from root (/)
	Param 2 = title
	Param 3 = query paramters (param=value&param=value...) - THIS WILL OVERRIDE EXISTING QUERY STRINGS
	Param 4 = URL for current page (including domain) for referrer. Leave blank to capture the normal referrer
	*/
	if (arguments.length!=0) {
		var hl_url=arguments[0];
		var hl_title=arguments[1];
		var hl_query=arguments[2];
		// if no referrer is passed, use document.referrer
		var hl_referrer=(arguments[3]=="")?document.referrer:arguments[3]; 
	}
	//alert(hl_title);


/*
	//this needs to be changed to the location of the server where
	//the tag logging is to occur
	var hl_tagurl='http://a8.dev.alsac.stjude.org/HLTag/tng/HLTag.gif'; // XXX - NO SUPPORT FOR SSL YET XXX
*/
	var hl_referrer=(hl_referrer)?hl_referrer:document.referrer;
	var hl_domain=document.location.hostname;

	//url won't always be useful if dynamically generated
	var hl_url=(hl_url)?hl_url:document.location.pathname;
	var hl_port=document.location.port;

	//get universal time information for tag
	var now = new Date();
	var dMonth=String(now.getUTCMonth()+1);
	var dDay=String(now.getUTCDate());
	var dHours=String(now.getUTCHours());
	var dMinutes=String(now.getUTCMinutes());
	var dSeconds=String(now.getUTCSeconds());

	if (dMonth.length==1) {dMonth='0'+dMonth};
	if (dDay.length==1) {dDay='0'+dDay};
	if (dHours.length==1) {dHours='0'+dHours};
	if (dMinutes.length==1) {dMinutes='0'+dMinutes};
	if (dSeconds.length==1) {dSeconds='0'+dSeconds};

	//we don't use the client time by default, but here is the format that would be passed in the application argument
	//this is the standard format we like: yyyy-mm-dd hh:nn:ss
	var hl_requestdate= String(now.getUTCFullYear())+'-'+dMonth+'-'+dDay+' '+dHours+':'+dMinutes+':'+dSeconds;
	var hl_uniqueid = String(now.getTime());

	//various unused default values (they could be appended as part of hl_query in parameter values)
	var hl_screenresolution=screen.width+'*'+screen.height;
	var hl_title=(hl_title)?hl_title:document.title;

	//if you don't want to keep the original query values change this
	if (typeof(hl_query)=="undefined") {
		var hl_query='';
		if (document.location.search.length>0){hl_query=document.location.search.substring(1)};
		if (hl_query.length==0)
		{hl_query=hl_meta().substring(1);}
		else
			{hl_query+=hl_meta();}
		}
	//this is a good place to implement a visitor cookie
	//we default to create a unique sessionid to make sure all tagged requests have a way to resolve the visit
	//note:  if we aren't allowing cookies this won't affect anything...
	var hl_sessionid=GetCookie("hl_sessionid");
	if (hl_sessionid == null)
	{
		hl_sessionid=NewSessionID();
		//set the cookie to expire at end of session for entire site
		// SetCookie ("hl_sessionid", hl_sessionid, "", "/", "", false);
		var expirationdate=new Date(); 
		expirationdate.setFullYear("2025"); 
		SetCookie ("hl_sessionid", hl_sessionid, expirationdate, "/", ".stjude.org", false);
	}

	//note:  this variable passed in the query to HitList will override the cookie value passed in normal logging
	//	 that means you could add information (if necessary) directly to the cookie by adding it to hl_cookie
	var hl_cookie=document.cookie;

	//this is the tagging information that will be sent
	//note: by default we defer to the server time, but the client time may be passed by appending the hl_requestdate parameter
	//      '&hl_requestdate='+HLescape(hl_requestdate)
	var hl_tagsource=hl_tagurl+'?hl_tagversion='+hl_tagversion+'&hl_tagcodebase='+hl_tagcodebase+'&hl_title='+HLescape(hl_title)+'&hl_screenresolution='+hl_screenresolution+'&hl_uniqueid='+hl_uniqueid+'&hl_domain='+HLescape(hl_domain)+'&hl_url='+HLescape(hl_url)+'&hl_port='+hl_port+'&hl_query='+HLescape(hl_query)+'&hl_referrer='+HLescape(hl_referrer)+'&hl_cookie='+HLescape(hl_cookie);

	//send the tagging information by loading 1x1 gif
	hl_tag2=new Image();
	hl_tag2.src=hl_tagsource;

}

function hl_meta(){
	var hl_elements;
	var hl_metaquery='';
	if (document.all){
		hl_elements=document.all.tags("meta");
	}
	else if (document.documentElement){
		hl_elements=document.getElementsByTagName("meta");
	}
	if (typeof(hl_elements)!="undefined"){
		for (var i=1;i<=hl_elements.length;i++){
			var hl_meta=hl_elements.item(i-1);
			if (hl_meta.name)
			{
				if (hl_meta.name.indexOf('HL.')==0)
				{
					hl_metaquery+='&'+hl_meta.name.substring(3)+'='+hl_meta.content;
				}
			}
		}
	}
	return(hl_metaquery);
}

function NewSessionID()
{
	var TodaysDate=new Date();
	var newid = Math.floor(999999*Math.random()+1)+"-"+TodaysDate.getFullYear()+(TodaysDate.getMonth()+1)+TodaysDate.getDate()+TodaysDate.getHours()+TodaysDate.getMinutes()+TodaysDate.getSeconds();
	return newid;
}

function HLescape(value)
{

	if (value != null)
	{
		//a little extra paranoia doesn't hurt, since we don't want the parameters to have bad character
		//that would cause problems parsing the values later
		value=escape(value);
		value = value.replace(/\//g,"%2F");
		value = value.replace(/\?/g,"%3F");
		value = value.replace(/=/g,"%3D");
		value = value.replace(/&/g,"%26");
		value = value.replace(/@/g,"%40");
	}
	else
		{
			value="";
		}
	return value
}
