//
// Main logic
//

var g_bDebug = true;

var g_sCookieName = "tk";
var g_sCookieDomain = "radialpoint.net"; // this helps for cookie collector finding the cookie
var g_sCookiePath = "";                  // this helps for cookie collector finding the cookie

var g_sRedir;
var g_sDefaultRedir = "www.verizon.net";

// ----------------------------------------------
function DropCookie(){
   // parse url params
   var mapQueryParams = GetQueryParamsMap();

   var iTid = mapQueryParams["id"];
   g_sRedir = mapQueryParams["redir"] || g_sDefaultRedir;

   if( ! iTid ){
      return;
   }

   // read current cookie
   var hCookie = new Cookie(g_sCookieName);
   var sTidList = hCookie.GetAttribute("tid");
   var sDateList = hCookie.GetAttribute("date");

   // build and drop cookie 
   var iDate = GetTimestamp();
   
   var arrTid = sTidList ? sTidList.split(",") : [];
   var arrDate = sDateList ? sDateList.split(",") : [];
   
   var iPos = arrTid.getIndex(iTid);

   // - if param not present, add it
   if( iPos == -1 ){
      arrTid.push(iTid);
      arrDate.push(iDate);
   }
   // - if param already present, update its associated value
   else{
      arrDate[iPos] = iDate;
   }

   hCookie.SetDomain( g_sCookieDomain );
   hCookie.SetPath( g_sCookiePath );
   hCookie.SetAttribute("tid",arrTid.join(","));
   hCookie.SetAttribute("date",arrDate.join(","));
   hCookie.Save();
}

// ----------------------------------------------
function Debug( sMethod, sMsg ){
   if( g_bDebug ){
      alert("DEBUG\n" + sMsg );
   }
}

// ----------------------------------------------
function Error( sMethod, sMsg ){
   if( g_bDebug ){
      alert("-ERR-\n" + sMsg );
   }
}

// ----------------------------------------------
function OnBeforePageLoad(){
   try{
      DropCookie();
   }
   catch(e){
      Error("OnBeforePageLoad","exception caught: " + e.description );
   }

   // redir
   document.location = g_sRedir;
}

