
// Call vars and functions ASP... so they dont conflict with other vars and functions from similar .js routines.

var ASPQueryString;
var ASPSearchPositionsUrl = _webRoot + "/Ajax/SearchPositions.aspx";
var ASPReq;
var ASPResponseText;
var ASPResponseErrorCode;
var ASPResponseErrorMessage;
var ASPResponseHTML;
var ASPuid;
var ASPFirstColonIdx;
var ASPSecondColonIdx;

var ASPPageNumber;
var ASPSearchEmployerSearchLookupIds;
var ASPSearchCreatedWithin;
var ASPSearchMiles;
var ASPSearchNursingStatusIds;
var ASPSearchPracticeAreaIds;
var ASPSearchScheduleIds;
var ASPSearchShiftIds;
var ASPSearchTerm;
var ASPSearchZip;
var ASPSelectItemId;

function ASPSearchPositions(pageNumber, selectItemId)
{
   // Set the default value for selectItemId.
   if ('undefined' == typeof selectItemId) {selectItemId = -1};
   
   // Set the itemId as a global var, so we can know whether we need to add it to
   // the cart once the search is complete.
   ASPSelectItemId = selectItemId;
   
   ASPPageNumber = pageNumber;
   // Display the clock icon to indicate the search has started.
   document.getElementById('PositionsList').innerHTML = "<div id=\"selectText\"><table width=\"340\" cellpadding=\"0\" cellspacing=\"0\"><tr><td class=\"searchtitle\">Jobs Listings</td></tr></table></div><table cellpadding=\"0\" cellspacing=\"0\"><tr><td width=\"360\" height=\"350\" align=\"center\" valign=\"middle\" class=\"plain\"><img src=\"/images/AjaxWait.gif\" /><BR><span style='font-size:10pt;'>TooBusyWorking</span></td></tr></table>";
   
   // Get the search criteria entered by user.
   ASPGetSearchCriteria();
   // TODO:Ad: place search criteria values on hidden fields
   document.getElementById('hdnSearchEmployerIds').value = ASPSearchEmployerSearchLookupIds;
   document.getElementById('hdnSearchMiles').value = ASPSearchMiles;
   document.getElementById('hdnSearchNursingStatusIds').value = ASPSearchNursingStatusIds;
   document.getElementById('hdnSearchPracticeAreaIds').value = ASPSearchPracticeAreaIds;
   document.getElementById('hdnSearchScheduleIds').value = ASPSearchScheduleIds;
   document.getElementById('hdnSearchShiftIds').value = ASPSearchShiftIds;
   document.getElementById('hdnSearchTerm').value = ASPSearchTerm;
   document.getElementById('hdnSearchZIP').value = ASPSearchZip;
   
   // JavaScript Async object.
   ASPCreateRequester();

   // Generate a unique Id for the querystring
   ASPuid = (new Date().getTime());

   ASPQueryString = "?searchEmployerSearchLookupIds=" + ASPSearchEmployerSearchLookupIds +
      "&searchCreatedWithin=" + ASPSearchCreatedWithin +
      "&searchMiles=" + ASPSearchMiles +
      "&searchNursingStatusIds=" + ASPSearchNursingStatusIds +
      "&searchPageNumber=" + pageNumber +
      "&searchPracticeAreaIds=" + ASPSearchPracticeAreaIds +
      "&searchScheduleIds=" + ASPSearchScheduleIds +
      "&searchShiftIds=" + ASPSearchShiftIds +
      "&searchTerm=" + ASPSearchTerm +
      "&searchZip=" + ASPSearchZip +
      "&selectItemId=" + selectItemId +
      "&uid=" + ASPuid;
   
   if (ASPReq != null)
   {
      // Connect the "ASPProcessSearchPositions" function to the
      // "onreadystatechange" event so that it can handle the HTTP response.
      ASPReq.onreadystatechange = ASPProcessSearchPositions;

      // Make HTTP request to check status page.
      ASPReq.open("GET", ASPSearchPositionsUrl + ASPQueryString, true);
      ASPReq.send(null);
   }
}

function ASPGetSearchCriteria()
{
   // Get the user entered search criteria from the page form elements.
   ASPSearchCreatedWithin = createdWithinRcb.get_selectedItem().get_value();
   ASPSearchMiles = document.getElementById('MilesTxt').value;
   ASPSearchTerm = document.getElementById('SearchTermTxt').value;
   ASPSearchZip = document.getElementById('MyZipTxt').value;
   if (ASPSearchZip == 'ZIP code')
   {
      ASPSearchZip = '';
   }
   
   // Get the list of checkBoxes and hidden divs (which contain the employerSearchLookupIds) in the employerSearchLookup List.
   checkBoxes = employerSearchLookupCbl.getElementsByTagName("input");
   divs = employerSearchLookupCbl.getElementsByTagName("div");
   ASPSearchEmployerSearchLookupIds = '';
   // Loop on each checkbox in the list.
   for (var i = 0; i < checkBoxes.length; i++)
   {
      // If the checkbox is selected, add the employerSearchLookupId to the list.
      if (checkBoxes[i].checked)
      {
         ASPSearchEmployerSearchLookupIds = ASPSearchEmployerSearchLookupIds + divs[i].innerHTML + ',';
      }
   }
   // Remove the last comma.
   if (ASPSearchEmployerSearchLookupIds.length > 0)
   {
      ASPSearchEmployerSearchLookupIds = ASPSearchEmployerSearchLookupIds.substr(0, ASPSearchEmployerSearchLookupIds.length - 1);
   }
   
   // Get the list of checkBoxes and hidden divs (which contain the practiceAreaIds) in the practiceArea List.
   var checkBoxes = practiceAreaCbl.getElementsByTagName("input");
   var divs = practiceAreaCbl.getElementsByTagName("div");
   ASPSearchPracticeAreaIds = '';
   // Loop on each checkbox in the list.
   for (var i = 0; i < checkBoxes.length; i++)
   {
      // If the checkbox is selected, add the practiceAreaId to the list.
      if (checkBoxes[i].checked)
      {
         ASPSearchPracticeAreaIds = ASPSearchPracticeAreaIds + divs[i].innerHTML + ',';
      }
   }
   // Remove the last comma.
   if (ASPSearchPracticeAreaIds.length > 0)
   {
      ASPSearchPracticeAreaIds = ASPSearchPracticeAreaIds.substr(0, ASPSearchPracticeAreaIds.length - 1);
   }
   
   // Get the list of checkBoxes and hidden divs (which contain the scheduleIds) in the schedule List.
   checkBoxes = scheduleCbl.getElementsByTagName("input");
   divs = scheduleCbl.getElementsByTagName("div");
   ASPSearchScheduleIds = '';
   // Loop on each checkbox in the list.
   for (var i = 0; i < checkBoxes.length; i++)
   {
      // If the checkbox is selected, add the scheduleId to the list.
      if (checkBoxes[i].checked)
      {
         ASPSearchScheduleIds = ASPSearchScheduleIds + divs[i].innerHTML + ',';
      }
   }
   // Remove the last comma.
   if (ASPSearchScheduleIds.length > 0)
   {
      ASPSearchScheduleIds = ASPSearchScheduleIds.substr(0, ASPSearchScheduleIds.length - 1);
   }
   
   // Get the list of checkBoxes and hidden divs (which contain the nursingStatusIds) in the schedule List.
   checkBoxes = searchNursingStatusCbl.getElementsByTagName("input");
   divs = searchNursingStatusCbl.getElementsByTagName("div");
   ASPSearchNursingStatusIds = '';
   // Loop on each checkbox in the list.
   for (var i = 0; i < checkBoxes.length; i++)
   {
      // If the checkbox is selected, add the nursingStatusId to the list.
      if (checkBoxes[i].checked)
      {
         ASPSearchNursingStatusIds = ASPSearchNursingStatusIds + divs[i].innerHTML + ',';
      }
   }
   // Remove the last comma.
   if (ASPSearchNursingStatusIds.length > 0)
   {
      ASPSearchNursingStatusIds = ASPSearchNursingStatusIds.substr(0, ASPSearchNursingStatusIds.length - 1);
   }
   
   // Get the list of checkBoxes and hidden divs (which contain the shiftIds) in the shift List.
   checkBoxes = shiftCbl.getElementsByTagName("input");
   divs = shiftCbl.getElementsByTagName("div");
   ASPSearchShiftIds = '';
   // Loop on each checkbox in the list.
   for (var i = 0; i < checkBoxes.length; i++)
   {
      // If the checkbox is selected, add the shiftId to the list.
      if (checkBoxes[i].checked)
      {
         ASPSearchShiftIds = ASPSearchShiftIds + divs[i].innerHTML + ',';
      }
   }
   // Remove the last comma.
   if (ASPSearchShiftIds.length > 0)
   {
      ASPSearchShiftIds = ASPSearchShiftIds.substr(0, ASPSearchShiftIds.length - 1);
   }
}

function ASPProcessSearchPositions()
{
   if (ASPReq.readyState == 4)
   {
      // Only if HTTP Response is "OK"
      if (ASPReq.status == 200)
      {
         // Set the HTML of the positions list to the results from the ajax call.
         ASPResponseText = ASPReq.responseText;
         
         ASPFirstColonIdx = ASPResponseText.indexOf(':');
         ASPResponseErrorCode = ASPResponseText.substring(0, ASPFirstColonIdx);
         
         ASPSecondColonIdx = ASPFirstColonIdx + 1 + ASPResponseText.substr(ASPFirstColonIdx + 1).indexOf(':');
         ASPResponseErrorMessage = ASPResponseText.substring(ASPFirstColonIdx + 1, ASPSecondColonIdx);
         
         ASPResponseHTML = ASPResponseText.substr(ASPSecondColonIdx + 1);
         
         // No error
         if (ASPResponseErrorCode == '0')
         {
            // Display the search results in the Positions panel.
            document.getElementById('PositionsList').innerHTML = ASPResponseHTML;
            
            // Now that we have completed the search, check if an itemId was sent
            // indicating it needs to be added to the cart.
            if (ASPSelectItemId != -1)
            {
               addPositionToCart(ASPSelectItemId);
            }
         }
         // Validation error
         else if (ASPResponseErrorCode == '-1')
         {
            displayErrorPopup(ASPResponseHTML);
            document.getElementById('PositionsList').innerHTML = "";
         }
         // Unknown error
         else if (ASPResponseErrorCode == '-2')
         {
            displayErrorPopup('We were unable to complete your search. Please try again.');
            document.getElementById('PositionsList').innerHTML = "";
         }
      }
   }
}


/*
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 ASPCreateRequester()
{
   try
   {
      ASPReq = new ActiveXObject("Msxml2.XMLHTTP");
   }
   catch(e)
   {
      try
      {
         ASPReq = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(oc)
      {
         ASPReq = null;
      }
   }
   if(!ASPReq && typeof XMLHttpRequest != "undefined")
   {
      ASPReq = new XMLHttpRequest();
   }
}