
window._TgN_Track = new function () {

this.cookie = function (name)
{
	if (document.cookie.length > 0)
	{
		var from = document.cookie.indexOf(name + '=');
		if (from != -1)
		{
			from += name.length + 1;
			var to = document.cookie.indexOf(';', from);
			if (to == -1)
			{
				to = document.cookie.length;
			}
			return unescape(document.cookie.substring(from, to));
		}
	}
	return null;
}

this.tags = function (name)
{
	return document.getElementsByTagName(name);
}

this.found = null;

this.text = function (el, limit)
{
	this.found = "";
  if (el)
  {
    if (el.length)
    {
      for (var i in el)
      {
        this.itext(el[i]);
        if (this.found.length > limit)
        {
          break;
        }
      }
    }
    else
    {
      this.itext(el);
    }
  }
	return this.found;
}

this.itext = function (el, limit)
{
  var childs = el.childNodes;
  if (childs)
  {
    for (var i = 0; i < childs.length; i++)
    {
      if (childs[i].nodeValue != null)
      {
        var words = childs[i].nodeValue.replace(/\s+/g, " ").split(" ");
        for (var w in words)
        {
          this.found = this.found + " " + words[w];
          if (this.found.length > limit)
          {
            break;
          }
        }
      }
      else
      {
        this.itext(childs[i]);
      }
      if (this.found.length > limit)
      {
        break;
      }        
    }
  }
}

this.track = function (entry, callback)
{
  var head= document.getElementsByTagName("head")[0];
  var script = document.createElement("script");
  script.type = "text/javascript";
  var params = "";
  if (this.client)
  {
    params += (params ? "&" : "") + "client=" + encodeURIComponent(this.client);
  }
  if (this.test)
  {
    params += (params ? "&" : "") + "test=" + encodeURIComponent(this.test);
  }
  if (this.goal)
  {
    params += (params ? "&" : "") + "goal=" + encodeURIComponent(this.goal);
  }
  if (this.name)
  {
    params += (params ? "&" : "") + "name=" + encodeURIComponent(this.name);
  }
  if (this.data)
  {
    params += (params ? "&" : "") + "data=" + encodeURIComponent(this.data);
  }
  if (params)
  {
    params += "&";
  }
  if (typeof(callback) == "function")
  {
    script.onload = callback;
  }
  script.src = "http://www.mrswing.com/targetnext/public/web/" + entry + "?" + params + "user=" + encodeURIComponent(this.cookie("_TgN_Track")) + "&rand=" + Math.random() + 
  "&referrer=" + encodeURIComponent(document.referrer) + "&url=" + encodeURIComponent(document.URL) + "&title=" + encodeURIComponent(this.text(this.tags("title"), 256)) +
	"&h1=" + encodeURIComponent(this.text(this.tags("h1"), 256)) + "&h2=" + encodeURIComponent(this.text(this.tags("h2"), 256))
  head.appendChild(script);
}

this.view = function (callback)
{
  this.track('ViewHelper.php', callback);
}

this.convert = function (callback)
{
  this.track('ConvertHelper.php', callback);
}

this.client = null;

this.setClient = function (client)
{
  this.client = client;
}

this.test = null;

this.setTest = function (test)
{
  this.test = test;
}

this.goal = null;

this.setGoal = function (goal)
{
  this.goal = goal;
}

this.name = null;

this.setName = function (name)
{
  this.name = name;
}

this.data = null;

this.setData = function (data)
{
  this.data = data;
}

}

window.getTgNTrack = new function ()
{
  return window._TgN_Track;
}

window.getTgNTrack.track('ViewHelper.php');
