/*
//  Title: 	trackingCookie.js
//  Decription: 
//			Cookie Tracking System
//			Allows tracking of reffering URL and Advertising Campaign.
//
//  Author:	Adrian Jones @ Internet Technique (www.internet-technique.com)
//
//  Copyright 2001 Adrian Jones / Internet Technique.  All Rights Reserved.
//
//  Added getCamp, getPageName .JBW. 1 Jan 2004
// Copied to new site by LT July 2004
*/

function trackReferrer( campaign)
{
	trackReferrerOrCampaign( campaign);
}
function trackReferrerOrCampaign( campaign)
{
	cookieName = "REFERING_URL";
	cookieVal = getCookie( cookieName);
	//	alert( "1: " + cookieVal + "2: " + campaign);
	if ( cookieVal == null || cookieVal == "www.orthoview.com")
	{
		refURL = document.referrer;
		// set it to the campaign id 1st.
		if ( campaign != "" && campaign != "undefined")
		{
			setCookie( cookieName, campaign, 50 * 24);
		}
		// otherewise set it to the referrer.
		else if ( refURL != "")
		{
			todoubledash = refURL.indexOf("//");
			end = refURL.length;
			nonhttp = refURL.substring(todoubledash + 2, end);
			todash = nonhttp.indexOf("/");		
			domainName = nonhttp.substring(0,todash);
			referrerName = domainName;
			setCookie( cookieName, referrerName, 50 * 24);
		}
		// else set it to the refering advert.
		else
		{
			cookieVal = getCookie( "REFERING_ADVERT");
			if ( cookieVal != null)
			{
				setCookie( cookieName, cookieVal, 50 * 24);				
			}
		}
	}
}

function trackCampaignAdvert( cookieValue)
{
	cookieName = "REFERING_ADVERT";
	cookieVal = getCookie( cookieName);
	if ( cookieVal == null)
	{
		setCookie( cookieName, cookieValue, 50 * 24);
	}
}

function trackCampaignAdvert_OR( cookieValue)
{
	cookieName = "REFERING_ADVERT";
	delCookie( cookieName);
	setCookie( cookieName, cookieValue, 50 * 24);
}

function trackPromotion( cookieValue)
{
	cookieName = "PROMOTION";
	delCookie( cookieName);
	setCookie( cookieName, cookieValue, 50 * 24);
}

function trackNumberOfVisits()
{
	cookieName = "VISIT_NUMBER";
	cookieVal = getCookie( cookieName);
	if ( cookieVal == null)
	{
		setCookie( cookieName, "X", 50 * 24);
	}
	else
	{
		cookieVal = cookieVal + "X";
		delCookie( cookieName);
		setCookie( cookieName, cookieVal, 50 * 24);	
	}
//	alert( cookieVal);
}

// ########################################################################################

function setCookie(cookieName, cookieValue, exphours)
{
	cookieVal = getCookie( cookieName);
//	alert("add: " + cookieName + " | " + expdays);
	if (exphours == 0)
	{
		var expDate = new Date();
		var expTime = 30 * 60 * 1000;
		expDate.setTime(expDate.getTime() + expTime );		
		document.cookie = cookieName + "=" + cookieValue + ";expires=" + expDate.toGMTString() + ";path=/"+";"
	}
	else
	{
		var expDate = new Date();
		var expTime = exphours * 60 * 60 * 1000;
		expDate.setTime(expDate.getTime() + expTime );
		document.cookie = cookieName + "=" + cookieValue + ";expires=" + expDate.toGMTString() + ";path=/"+";"
	}	
}

function getCookie( cookieName) //check if cookie exists - returns boolean
{
	subtotal = 0;
	noitems = null;
	mycookies=new Array();
	var i=0;
	var startpoint=0;
	whole_len=document.cookie.length;
	while(whole_len!=startpoint && i< 10)
	{
		if ((temlen=document.cookie.indexOf(";",startpoint))!=-1)
		{
			mycookies[i]=document.cookie.substring(startpoint,temlen);
			i++;
			startpoint=temlen+2;
		}
		else if (startpoint != whole_len)
		{
			mycookies[i]=document.cookie.substring(startpoint,whole_len);
			i++;
	       	startpoint=whole_len;
		}
	}

	for (j=0;j<i;j++)
	{
		noitems = get( mycookies[j], cookieName);
		if ( noitems != null)
		{
			break;
		}
	}
	return noitems;
}

function delCookie( cookieName, cookieValue)
{
  document.cookie = cookieName + "=" + cookieValue + "; expires=Saturday, 16-Sep-90 23:59:59 GMT; path=/"+"";
//  alert("DEL: " + cookieName);
}

function get(acookie, cookieName) 
{
	code_part = acookie.indexOf("=");
	typeValue = acookie.substring(0,code_part);
	total_len = acookie.length;
	nameValue = acookie.substring(code_part+1,total_len);

//	alert("Name:" + nameValue + ":");
//	alert("Type:" + typeValue + ":");

	if (typeValue==cookieName)
	{
		noitems = nameValue;
//		alert("Check Passed - <" + nameValue + "> Tracking Cookie Found");
	}
	else
	{
		noitmes = null;
//		alert("No Tracking Cookie Found");
	}
	return noitems;
}

var G_SHengine = "none";
var G_SHkeys = "no keys";

function getCamp()
{
  var query = unescape(location.search.substring(1));

  
  if (query.search(/engine/i) == -1 || query.search(/keywords/i) == -1)
    return "None";

  var pairs = query.split("&");
  var pos = pairs[0].indexOf('=');
  var engine = pairs[0].substring(0,pos);

  if (engine == "engine" )
  {
    G_SHengine = pairs[0].substring(pos+1); 
  }  

  pos = pairs[1].indexOf('=');

  var keyword = pairs[1].substring(0,pos);

  if (keyword == "keywords" )
  {
    G_SHkeys = pairs[1].substring(pos+1); 
  }  

  var campAD = G_SHengine + " - " + G_SHkeys;
  return campAD;
}

function getPageName()
{
  var pageName = G_SHkeys.replace (/ /g,"+");
  //alert ("pagename ="+pageName);
  return pageName;  
}
