var TrackingEnabled = true;
var TrackingErrorTime;

window.onerror = HandleScriptError;

document.onmouseover = SuppressURLStatus;


function SuppressURLStatus(e) {

   // Prevent the status bar from displaying URLs.

   if (!e) {var e = window.event;}
   var Elem=(e.target)? e.target : e.srcElement;
   while (Elem != null && Elem.tagName != "A") {
      Elem = Elem.parentNode;
      }
   if (Elem == null) {return;}
   if (e.preventDefault) {
      e.preventDefault();
      }
   else {
      window.status = "";
      e.returnValue = true;
      }
}


function HandleScriptError() {

   if (ErrorHandlerTrace == "AppendTrackingCode Function") {
      TrackingErrorTime = new Date();
      TrackingEnabled = false;
      Popup("Small","../../popups/javascript_error_AppendTrackingCode.htm");
      ErrorHandlerTrace = "";
      return true;
      }
}


function RetainTrackingCode(theLink, VendorPageID) {

      if (window.location.search != "") {
         theLink.href += window.location.search;
         }
      else {
         theLink.href += "?sid=vnd_se_" + VendorPageID;
         }
}


function AppendTrackingCode(theLink, VendorPageID, e) {

   ErrorHandlerTrace = "AppendTrackingCode Function";

   if (!TrackingEnabled) {
      ErrorHandlerTrace = "";
      return;
      }

   if (theLink.getAttribute) {
      if (theLink.getAttribute("NewWinFlag")) {
         theLink.target = "_blank";
         }
      }

   if (theLink.getAttribute) {
      if (theLink.getAttribute("NoTrackFlag")) {
         ErrorHandlerTrace = "";
         return;
         }
      }

   var BrowserIsCompatible = false;                   // Presume the browser is incompatible.
   if (navigator.appName == "Netscape") {
      if (parseFloat(navigator.appVersion) >= 4.0) {
         BrowserIsCompatible = true;                  // The browser is Netscape 4.0 or higher.
         }
      }
   else if (navigator.appName == "Microsoft Internet Explorer") {
      if (parseFloat(navigator.appVersion) >= 4) {    // The Split method in the next line requires Mozilla 4 or later MSIE.  appVersion gives Mozilla version for IE.
         var tempArray = navigator.appVersion.split("MSIE");
         if (parseFloat(tempArray[1]) >= 4.0) {
            BrowserIsCompatible = true;               // The browser is MSIE 4.0 or higher.
            }
         }
      }


   if (!BrowserIsCompatible) {
      ErrorHandlerTrace = "";
      return;
      }

   var URL = theLink.href

   var QueryName = "";
   if (URL.indexOf("qksrv.net") != -1) {QueryName = "sid";}        // CJ
   if (URL.indexOf("bfast.com") != -1) {QueryName = "bfinfo";}     // BeFree
   if (URL.indexOf("linksynergy.com") != -1) {QueryName = "u1";}   // Linkshare
   if (QueryName == "") {
      ErrorHandlerTrace = "";
      return;
      }

   if (URL.indexOf("?" + QueryName + "=") != -1 || URL.indexOf("&" + QueryName + "=") != -1) {
      ErrorHandlerTrace = "";
      return;
      }

   if (URL.indexOf("?") == -1) {
      var Delimiter = "?";
      }
   else {
      var Delimiter = "&";
      }

   var QueryString = window.location.search;
   if (QueryString != "" && QueryString.indexOf("sid=") == -1) {
      QueryString = "sid=error";
      }

   if(QueryString == "") {
      var SourceID = "";
      }
   else {
      var tempArray = QueryString.split("=");
      var SourceID = tempArray[1];
      }

   if(SourceID == "") {
      theLink.href = URL + Delimiter + QueryName + "=vnd_se_none_" + VendorPageID;
      }
   else {
      theLink.href = URL + Delimiter + QueryName + "=" + SourceID + "_" + VendorPageID;
      }

   ErrorHandlerTrace="";
   return;
}


function FollowLink (theLink) {

   if (TrackingEnabled) {
      return true;                   // Load the page normally.
      }
   else {
      ClickTime = new Date();
      if (ClickTime - TrackingErrorTime < 2000) {
         return false;               // Do not load the page at all.
         }

      theLink.target = "_blank";     // Load the page in a new window.
      return true;
      }
}


function Popup(Size, URLorMsg) {

   if (URLorMsg.indexOf("Msg:")==0) {
      var Msg = unescape(URLorMsg.substr(4));
      var URL = "";
      }
   else {
      var URL = URLorMsg;
      }

   var Day = new Date();
   var ID = Day.getTime();

   if (Size == "Small") {
      eval("Page" + ID + " = window.open(URL, '" + ID + "', 'dependent,titlebar=1,menubar=0,toolbar=0,scrollbars=1,location=0,directories=0,status=0,resizable=1,width=320,height=240,left=352,top=264,screenX=352,screenY=264');");
      }
   if (Size == "Medium") {
      eval("Page" + ID + " = window.open(URL, '" + ID + "', 'dependent,titlebar=1,menubar=0,toolbar=0,scrollbars=1,location=0,directories=0,status=0,resizable=1,width=448,height=336,left=288,top=216,screenX=288,screenY=216');");
      }
   if (Size == "Large") {
      eval("Page" + ID + " = window.open(URL, '" + ID + "', 'dependent,titlebar=1,menubar=0,toolbar=0,scrollbars=1,location=0,directories=0,status=0,resizable=1,width=576,height=432,left=224,top=168,screenX=224,screenY=168');");
      }

   if (URL == "") {
      var Doc = eval("Page"+ID).document;         // eval returns the OBJECT with the name "Page" + ID.
      Doc.write("<html>");
      Doc.write("   <head>");
      Doc.write("      <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">");
      Doc.write("      <title>Message</title>");
      Doc.write("   </head>");
      Doc.write("   <body bgcolor=\"#FFFFF0\">");
      Doc.write("      <p align=\"left\"><font size=\"1\" face=\"Verdana,Arial,Helvetica\">");
      Doc.write(Msg);
      Doc.write("      </font></p>");
      Doc.write("      <p align=\"center\"><a href=\"javascript:self.close()\">");
      Doc.write("      <font color=\"#0000FF\" size=\"1\" face=\"Verdana,Arial,Helvetica\"><strong>");
      Doc.write("      Close Window");
      Doc.write("      </strong></font></a></p>");
      Doc.write("   </body>");
      Doc.write("</html>");
      }
}

