var tsx=0;

function tsinit(x)
{
	tsx=x;
	s = document.cookie + ";";
	i = s.indexOf("P14TS");
	if (i < 0) return;
	j = s.indexOf(";", i);
	if (j < 0) return;
	n = parseFloat(s.substr(i+6, j-i-6));
	if (n < 2 || n > 4) return;
	tschange(n);
}

function tschange(n)
{
	e = document.getElementById("tscontent");
	if (e == null) return;
	document.cookie = "P14TS=" + n + "; expires=Sun, 01 Dec 2030 23:59:59 GMT; path=/; domain=open.ac.uk;";
	if (document.getElementById("ts1"))
	{
		if (n == 1) document.getElementById("ts1").style.borderColor='#547FBD'; else document.getElementById("ts1").style.borderColor='#ccc';
		if (n == 2) document.getElementById("ts2").style.borderColor='#547FBD'; else document.getElementById("ts2").style.borderColor='#ccc';
		if (n == 3) document.getElementById("ts3").style.borderColor='#547FBD'; else document.getElementById("ts3").style.borderColor='#ccc';
		if (n == 4) document.getElementById("ts4").style.borderColor='#547FBD'; else document.getElementById("ts4").style.borderColor='#ccc';
	}
	if (n == 4)
	n = 140;
	else
	n = 90 + n * 10;
	e.style.fontSize = n + "%";
	for (i=1 ; i <= tsx ; i++)
	{
		e = document.getElementById("tsx" + i);
		if (e) e.style.fontSize = n + "%";
	}
	e = document.getElementById("tsheader");
	if (e) e.style.fontSize = n + "%";
	e = document.getElementById("tsfooter");
	if (e) e.style.fontSize = n + "%";
}

function tschangesc(n)
{
	
	// Grab the area we want to resize
	e = document.getElementById("tscontent");
	if (e == null) return;
	
	// Read the cookie
	document.cookie = "P14TS=" + n + "; expires=Sun, 01 Dec 2030 23:59:59 GMT; path=/; domain=open.ac.uk;";
	
	// Set the display to show the cookie info
	if (document.getElementById("ts1"))
	{
		if (n == 1) document.getElementById("ts1").style.borderColor='#547FBD'; else document.getElementById("ts1").style.borderColor='#ccc';
		if (n == 2) document.getElementById("ts2").style.borderColor='#547FBD'; else document.getElementById("ts2").style.borderColor='#ccc';
		if (n == 3) document.getElementById("ts3").style.borderColor='#547FBD'; else document.getElementById("ts3").style.borderColor='#ccc';
		if (n == 4) document.getElementById("ts4").style.borderColor='#547FBD'; else document.getElementById("ts4").style.borderColor='#ccc';
	}
	
	// Figure out the font size based on the option chosen
	if (n == 4)
		n = 140;
	else
		n = 90 + n * 10;
		
	// Set anything that can obviously be resized
	e.style.fontSize = n + "%";
		
	// See if we were told we need to resize id tags
	if (tsx > 0)
	{
		// find each id and resize it
		for (i=1 ; i <= tsx ; i++)
		{
			e = document.getElementById("tsx" + i);
			if (e) e.style.fontSize = n + "%";
		}
	}
	else
	{
		// Find all elements with tsx in the id or the classname
		// saves having to code an incrementor for the id, and
		// keeps compatibilty with older sites that already use the incrementor method
		var inc=0;
		var alltags= e.getElementsByTagName("span")
		var elemList=new Array();
		// build a list of tags that need resizing
		for (i=0; i<alltags.length; i++)
		{
			if (alltags[i].id.indexOf("tsx") != -1 || alltags[i].className.indexOf("tsx") != -1)
				elemList[inc++]=alltags[i]
		}
	
		// resize the tags
		for (i=0; i < elemList.length; i++)
			elemList[i].style.fontSize = n + "%";
	}
	
	// Now resize the header
	
	e = document.getElementById("tsheader");
	if (e) 
		e.style.fontSize = n + "%";
		
	// Now resize the footer
	e = document.getElementById("tsfooter");
	if (e) 
		e.style.fontSize = n + "%";
	
}

// Change these to match your requirements
var siTrackerUrl    = "/wwwcommon/si-tracker.gif";
var siCookieName    = "SIVISITOR";
var siCookieQPName  = "simigvis";
var siCookiePath    = "/";
var siCookieTimeout = 315360000000;
var siDomainList    = new Array(".ac.uk",".net",".co.uk");
var siAutoTracer    = true;
// These will be set by functions below - note image set is needed to stop variables going out of scope
var siCookieValue   = "";
var siPageID        = "";
var siTracerPath    = "";
var siTracerQuery   = "";
var siImageSet      = new Array(1);

function EncodeString(Input)
{
  var TransChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  var Output     = "";
  for (var i = 0; i < Input.length; i += 3)
  {
    var NumBytesLeft = Input.length - i;
    var Value        = 0;
    Value  = (Input.charCodeAt(i) << 16) & 0x00ff0000;
    Value |= (NumBytesLeft > 1)? (Input.charCodeAt(i + 1) << 8) & 0x0000ff00 : 0;
    Value |= (NumBytesLeft > 2)? Input.charCodeAt(i + 2) & 0x000000ff : 0;
    Output += TransChars.charAt((Value & 0x00fC0000) >> 18);
    Output += TransChars.charAt((Value & 0x0003f000) >> 12);
    Output += (NumBytesLeft > 1)? TransChars.charAt((Value & 0x00000fc0) >> 6) : '_';
    Output += (NumBytesLeft > 2)? TransChars.charAt((Value & 0x0000003f)) : '_';
  }
  return Output;
}

//---------------------------------------------------------------------------------------------
// Encode all the relevent detals into a string
//---------------------------------------------------------------------------------------------
function EncodeDetails(Format)
{
   var Output = "";
   for (var i = 0; i < Format.length; i++)
   {
      var Data;
      switch (Format.charAt(i))
      {
      case 'r':
         Data = document.referrer;
         break;
      case 'p':
         Data = document.URL;      
         break;
      case 'd':
         Data = screen.availWidth+"x"+screen.availHeight+"x"+screen.colorDepth+"."+navigator.javaEnabled();
         if (navigator.plugins) Data += "."+navigator.plugins.length;
         break;
      case 'c':
         Data = siCookieValue;
         break;
      case 'u':
         Data = window.history.length+"."+(Math.random()*1000)+"."+(new Date()).getTime();
         break;
      case 't':
         Data = siPageID;
         break;
      case 'f':
         Data = siTracerPath;
         break;
      case 'q':
         Data = siTracerQuery;
         break;
      }
      //alert("Format="+Format.charAt(i)+", data: "+Data);
      Output += EncodeString(Data)+"*";
   }  
   return Output;
}

function GetDomain()
{
  var DomainValue = null;
  var firstDot;
  var secondDot;
  var lastDot;
  var useHostname=document.location.hostname;
  if (useHostname != null)
  {
     var arIndex;
     for (arIndex = 0; (arIndex < siDomainList.length) && (DomainValue == null); arIndex++)
     {
        var tldIndex = useHostname.lastIndexOf(siDomainList[arIndex]);
        if (tldIndex > 0)
        {
           var nextDot = useHostname.lastIndexOf('.',tldIndex-1);
           if (nextDot >= 0)
           {
              DomainValue = useHostname.substring(nextDot);
           }
           else
           {
              DomainValue = "." + useHostname;
           }
        }
     }
  }
  return DomainValue;
}

function SetCookie(Name,Value,Path) 
{
  var Expiry=new Date;
  Expiry.setTime(Expiry.getTime()+siCookieTimeout);
  var Domain=GetDomain();
  var CookieDetails=Name+"="+Value+"; expires=\""+Expiry.toGMTString()+"\" "+((siCookiePath) ? "; path=" + siCookiePath : "")+((Domain) ? "; domain="+Domain : "");
  document.cookie=CookieDetails;
}

function GetCookie(Name,DataSource,DataEndChar) 
{
  var Prefix = Name+"=";
  var Value  = null;
  var Begin  = DataSource.indexOf(Prefix);
  if ((Begin != -1) && (Name.length > 0))
  {
    var End = DataSource.indexOf(DataEndChar,Begin);
    if (End == -1) End = DataSource.length;
    Value = DataSource.substring(Begin+Prefix.length,End);
  } 
  return Value;
}

function BuildCookie()
{
   var CookieValue=GetCookie(siCookieQPName,document.URL,"&");
   if (CookieValue != null) 
   {
      SetCookie(siCookieName,CookieValue);
   }
   else
   {
      CookieValue=GetCookie(siCookieName,document.cookie,";");
      if (CookieValue == null)
      {
         CookieValue=EncodeDetails("u");
         SetCookie(siCookieName,CookieValue);
      }
   }
   CookieValue = GetCookie(siCookieName,document.cookie,";");
   return CookieValue;
}

function SiMigrateCookie(link)
{
   var CookieValue=GetCookie(siCookieName,document.cookie,";");
   if (CookieValue != null) link.href=link.href+((link.href.indexOf('?') > 0) ? "&" : "?")+siCookieQPName+"="+CookieValue;
   return true;
}

function SiSendTracer(cookieForm,noCookieForm)
{
   var Tracker = new Image();
   Tracker.src = (siCookieValue == null) ? siTrackerUrl+"?f="+noCookieForm+"&d="+EncodeDetails(noCookieForm) : siTrackerUrl+"?f="+cookieForm+"&d="+EncodeDetails(cookieForm);
   siImageSet[siImageSet.length] = Tracker;
}

function SiTrackForm(filePath,form,list)
{
   siTracerPath = filePath;
   siTracerQuery = "";
   if (form.elements && (form.elements.length > 0))
   {
      var doamp=false;
      for (var i=0; i<form.elements.length; i++)
      {
         var el=form.elements[i];
         if (el.name)
         {
            var capture=(list == null);
            if (list != null)
            {
               for (var j=0; !capture && (j<list.length); j++)
               {
                  if (el.name == list[j]) capture = true;      
               }
            }
            if (capture && (el.type == "radio")) capture = el.checked;
            if (capture) 
            {
               if (doamp) siTracerQuery += "&";
               siTracerQuery += escape(el.name)+"="+escape(el.value);
               doamp=true;
            }
         }
      }
   } 
   SiSendTracer("fqcrt","fqdrt");
   return true;
}

function SiTrackTracer(filePath,queryString)
{
   siTracerPath = filePath;
   siTracerQuery = queryString;
   SiSendTracer("fqcrt","fqdrt");
}

function SiTrackPage()
{
   siCookieValue = BuildCookie();
   siPageID = "t"+(new Date()).getTime()+"h"+window.history.length;
   if (siAutoTracer) SiSendTracer("pcrt","pdrt");
}

SiTrackPage();
