
//Entry point
//Parse input string and extract affiliate
//Input assumed to be in form #Affiliate=yyyy

var passedData = self.location.href;
var index1 = passedData.indexOf("Affiliate=");
var affiliate = "";
if ( index1 > -1) {
   affiliate = passedData.substring(index1+10).toUpperCase();
}
setAffiliate(affiliate);

function GMTDate(n) {
    var today = new Date();
    var expDate = today.getTime() + (n * 24 * 60 * 60 * 1000);
    today.setTime(expDate);
    return today.toGMTString();
}

function setAffiliate(x) {
   if (isNull(x)) {
	   if (isNull(getBookmark("Affiliate"))) {
		   affiliate = "";
	   } else {
		   affiliate=getBookmark("Affiliate");
	   }
   } else {
      if (isNull(getBookmark("Affiliate"))) {
	       writeBookmark("Affiliate",affiliate,"30");
      } else {
         deleteBookmark("Affiliate");
         writeBookmark("Affiliate",affiliate,"30");
      }
   }
}

function isNull(x) {
    if ((x == 'undefined') || (x == "") || (x == null) || (x == "null") || !x) {
        return true;
    } else {
        return false;
    }
}

function deleteBookmark(name) {
    if (getBookmark(name)) {
      document.cookie = name + "=" + "; expires=" + GMTDate(-2);
    }
}

function getBookmark(name)  {
    var arg = name + "=";
    var argLength = arg.length;
    var cLength  = document.cookie.length;
    var i = 0;
    while (i < cLength) {
       var j = i + argLength;
       if (document.cookie.substring(i,j) == arg) {
          var endstr = document.cookie.indexOf(";",j);
          if (endstr == -1)
            endstr = document.cookie.length;
          return unescape(document.cookie.substring(j, endstr));
       }
       i = document.cookie.indexOf(" ", i) + 1;
       if (i == 0) break;
    }
    return null;
 }

function writeBookmark(name,value,retention) {
    document.cookie = name + "=" + escape(value) + "; expires=" + GMTDate(retention);
}
function glossary()  {
	  var GlsWindow = window.open("manta/glossary.htm","_blank","height=500,width=600");
}

//This stuff is for Google analytics
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-19849536-1']);
_gaq.push(['_trackPageview']);
(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
