
// Call vars and functions ARC... so they dont conflict with other vars and functions from similar .js routines.

var ARCQueryString;
var ARCRecordClickthroughUrl = _webRoot + "/Ajax/RecordClickthrough.aspx";
var ARCReq;
var ARCResponseText;
var ARCResponseErrorCode;
var ARCResponseErrorMessage;
var ARCResponseHTML;
var ARCuid;
var ARCFirstColonIdx;
var ARCSecondColonIdx;

function ARCRecordClickthrough(positionId, sponsoredAdId, type)
{  
   // JavaScript Async object.
   ARCCreateRequester();

   // Generate a unique Id for the querystring
   ARCuid = (new Date().getTime());

   ARCQueryString = "?positionId=" + positionId +
      "&sponsoredAdId=" + sponsoredAdId +
      "&type=" + type +
      "&uid=" + ARCuid;
   
   if (ARCReq != null)
   {
      // Connect the "ARCProcessRecordClickthrough" function to the
      // "onreadystatechange" event so that it can handle the HTTP response.
      ARCReq.onreadystatechange = ARCProcessRecordClickthrough;

      // Make HTTP request to check status page.
      ARCReq.open("GET", ARCRecordClickthroughUrl + ARCQueryString, true);
      ARCReq.send(null);
   }
}

function ARCProcessRecordClickthrough()
{
   if (ARCReq.readyState == 4)
   {
      // Only if HTTP Response is "OK"
      if (ARCReq.status == 200)
      {
         // Set the HTML of the responses list to the results from the ajax call.
         ARCResponseText = ARCReq.responseText;
         
         ARCFirstColonIdx = ARCResponseText.indexOf(':');
         ARCResponseErrorCode = ARCResponseText.substring(0, ARCFirstColonIdx);
         
         ARCSecondColonIdx = ARCFirstColonIdx + 1 + ARCResponseText.substr(ARCFirstColonIdx + 1).indexOf(':');
         ARCResponseErrorMessage = ARCResponseText.substring(ARCFirstColonIdx + 1, ARCSecondColonIdx);
         
         ARCResponseHTML = ARCResponseText.substr(ARCSecondColonIdx + 1);
         
         if (ARCResponseErrorCode == '0')
         {
            // Do nothing.
         }
         else if (ARCResponseErrorCode == '-1')
         {
            displayErrorPopup(ARCResponseHTML);
         }
         else if (ARCResponseErrorCode == '-2')
         {
            displayErrorPopup(ARCResponseErrorMessage);
         }
      }
   }
}


/*
Note that this tries several methods of creating the XmlHttpRequest object,
depending on the browser in use. Also note that as of this writing, the
Opera browser does not support the XmlHttpRequest.
*/
function ARCCreateRequester()
{
   try
   {
      ARCReq = new ActiveXObject("Msxml2.XMLHTTP");
   }
   catch(e)
   {
      try
      {
         ARCReq = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(oc)
      {
         ARCReq = null;
      }
   }
   if(!ARCReq && typeof XMLHttpRequest != "undefined")
   {
      ARCReq = new XMLHttpRequest();
   }
}