
var queryString;
var addToCartUrl = _webRoot + "/JobSeeker/AjaxAddToCart.aspx";
var getCartCountUrl = _webRoot + "/JobSeeker/AjaxGetCartCount.aspx";
var removeFromCartUrl = _webRoot + "/JobSeeker/AjaxRemoveFromCart.aspx";
var req;
var responseText;
var responseErrorCode;
var responseErrorMessage;
var responseHTML;
var uid;
var firstColonIdx;
var secondColonIdx;
var itemCount = -1;
var addRemovePositionDivId;

function addPositionToCart(itemId)
{
   // JavaScript Async object.
   createRequester();

   // Generate a unique Id for the querystring
   uid = (new Date().getTime());

   queryString = "?itemId=" + itemId + "&uid=" + uid;
   
   if (req != null)
   {
      // Connect the "processAdd" function to the "onreadystatechange" event so that the
      // "processAdd" function will handle the HTTP response.
      req.onreadystatechange = processAdd;

      // Make HTTP request to check status page.
      req.open("GET", addToCartUrl + queryString, true);
      req.send(null);
   }
}

function getCartCount()
{
   // JavaScript Async object.
   createRequester();

   // Generate a unique Id for the querystring
   uid = (new Date().getTime());

   queryString = "?cartType=JobSeeker&uid=" + uid;
   
   if (req != null)
   {
      // Connect the "processCartCount" function to the "onreadystatechange" event so that the
      // "processCartCount" function will handle the HTTP response.
      req.onreadystatechange = processCartCount;

      // Make HTTP request to check status page.
      req.open("GET", getCartCountUrl + queryString, true);
      req.send(null);
   }
}

function removePositionFromCart(itemId)
{
   // JavaScript Async object.
   createRequester();

   // Generate a unique Id for the querystring
   uid = (new Date().getTime());

   queryString = "?itemId=" + itemId + "&uid=" + uid;
   
   if (req != null)
   {
      // Connect the "processRemove" function to the "onreadystatechange" event so that the
      // "processRemove" function will handle the HTTP response.
      req.onreadystatechange = processRemove;

      // Make HTTP request to check status page.
      req.open("GET", removeFromCartUrl + queryString, true);
      req.send(null);
   }
}

function processAdd()
{
   if (req.readyState == 4)
   {
      // Only if HTTP Response is "OK"
      if (req.status == 200)
      {
         // Set the HTML of the shopping cart to the results from the ajax call.
         responseText = req.responseText;
         
         firstColonIdx = responseText.indexOf(':');
         responseErrorCode = responseText.substring(0, firstColonIdx);
         
         secondColonIdx = firstColonIdx + 1 + responseText.substr(firstColonIdx + 1).indexOf(':');
         responseErrorMessage = responseText.substring(firstColonIdx + 1, secondColonIdx);
         
         responseHTML = responseText.substr(secondColonIdx + 1);
         
         if (responseErrorCode == '0')
         {
            // The first piece from the ResponseHTML is the Id of the addRemove div
            // for the position added. We use this id to reset its html with
            // updated addRemove links.
            addRemovePositionDivId = responseHTML.substr(0, responseHTML.indexOf(':'));
            responseHTML = responseHTML.substr(responseHTML.indexOf(':') + 1);
            
            // Increment the # of items in the cart.
//            itemCount++;
            //document.getElementById('positionsSelected').innerHTML = itemCount;
//            updateViewStep3Mask();
            
            // Replace the add remove links with the new html returned. This allows
            // us to activate the remove link and deactivate the add link.
//            if (document.getElementById(addRemovePositionDivId) != null)
//            {
//               document.getElementById(addRemovePositionDivId).innerHTML = responseHTML;
//            }
            
            // Open the Connect form popup.
            openRadW(radWConnectClientId);
         }
         else if (responseErrorCode == '-1')
         {
            // TODO: display error
         }
         else if (responseErrorCode == '-2')
         {
            // TODO: display error
         }
      }
   }
}

function processCartCount()
{
   if (req.readyState == 4)
   {
      // Only if HTTP Response is "OK"
      if (req.status == 200)
      {
         // Set the HTML of the shopping cart to the results from the ajax call.
         responseText = req.responseText;
         
         firstColonIdx = responseText.indexOf(':');
         responseErrorCode = responseText.substring(0, firstColonIdx);
         
         secondColonIdx = firstColonIdx + 1 + responseText.substr(firstColonIdx + 1).indexOf(':');
         responseErrorMessage = responseText.substring(firstColonIdx + 1, secondColonIdx);
         
         responseHTML = responseText.substr(secondColonIdx + 1);
         
         if (responseErrorCode == '0')
         {
            itemCount = responseHTML;
            //document.getElementById('positionsSelected').innerHTML = itemCount;
            updateViewStep3Mask();
         }
         else if (responseErrorCode == '-1')
         {
            // TODO: display error
         }
         else if (responseErrorCode == '-2')
         {
            // TODO: display error
         }
      }
   }
}

function processRemove()
{
   if (req.readyState == 4)
   {
      // Only if HTTP Response is "OK"
      if (req.status == 200)
      {
         // Set the HTML of the shopping cart to the results from the ajax call.
         responseText = req.responseText;
         
         firstColonIdx = responseText.indexOf(':');
         responseErrorCode = responseText.substring(0, firstColonIdx);
         
         secondColonIdx = firstColonIdx + 1 + responseText.substr(firstColonIdx + 1).indexOf(':');
         responseErrorMessage = responseText.substring(firstColonIdx + 1, secondColonIdx);
         
         responseHTML = responseText.substr(secondColonIdx + 1);
         
         if (responseErrorCode == '0')
         {
            // The first piece from the ResponseHTML is the Id of the addRemove div
            // for the position removed. We use this id to reset its html with
            // updated addRemove links.
            addRemovePositionDivId = responseHTML.substr(0, responseHTML.indexOf(':'));
            responseHTML = responseHTML.substr(responseHTML.indexOf(':') + 1);
            
            // Decrease the # of items in the cart.
            itemCount--;
            //document.getElementById('positionsSelected').innerHTML = itemCount;
            updateViewStep3Mask();
            
            // Replace the add remove links with the new html returned. This allows
            // us to activate the add link and deactivate the remove link.
            document.getElementById(addRemovePositionDivId).innerHTML = responseHTML;
         }
         else if (responseErrorCode == '-1')
         {
            // TODO: display error
         }
         else if (responseErrorCode == '-2')
         {
            // TODO: display error
         }
      }
   }
}

function updateViewStep3Mask()
{
   if (itemCount > 0)
   {
      document.getElementById('step3Mask').style.display = 'none';
   }
   else
   {
      document.getElementById('step3Mask').style.display = 'block';
   }
}


/*
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 createRequester()
{
   try
   {
      req = new ActiveXObject("Msxml2.XMLHTTP");
   }
   catch(e)
   {
      try
      {
         req = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(oc)
      {
         req = null;
      }
   }
   if(!req && typeof XMLHttpRequest != "undefined")
   {
      req = new XMLHttpRequest();
   }
}