function dbHideNav()
{
   $('#sidebar').animate({
      width: '24px'
   }, 400, _dbHideNavFinish );
   
   _dbSetNavFlag(0);
}

function dbShowNav()
{
   // Start by showing the content area (when hiding we hide that area after the animation)
   $('#contentarea').show();
   
   $('#sidebar').animate({
      width: '215px'
   }, 400, _dbShowNavFinish );
   
   _dbSetNavFlag(1);
}

function _dbHideNavFinish()
{
   // Need to hide old before showing new for IE6
   $('#contentarea').hide();
   $('#sidebarhide').hide();
   $('#sidebarshow').show(0); // IE6 needs the 0
   
   if (document.getElementById('dbIdContent')) {
      // Everything but IE6
      $('#dbIdContent').removeClass('dbNavOn').addClass('dbNavOff');
   }
   else {
      $('#dbIdTdNavIE6').removeClass('open').addClass('closed');
   }
}

function _dbShowNavFinish()
{
   // Need to hide old before showing new for IE6
   $('#sidebarshow').hide();
   $('#sidebarhide').show(0); // IE6 needs the 0
      
   if (document.getElementById('dbIdContent')) {
      // Everything but IE6
      $('#dbIdContent').removeClass('dbNavOff').addClass('dbNavOn');
   }
   else {
      $('#dbIdTdNavIE6').removeClass('closed').addClass('open');
   }
}
      
function _dbSetNavFlag(navon)
{
   var url = 'screening/screening.ajax.php?op=2&navon=' + navon;
   $.get(url);
}

function dbOpenNewWindow(url, id, width, height)
{
   var params = 'status=1,toolbar=1,menubar=1,resizable=1,scrollbars=1';
   if (0 < width) {
      params += ',width=' + width;
   }
   if (0 < height) {
      params += ',height=' + height;
   }
   var w = window.open(url, id, params);
   if (null != w) {
      w.focus();   
   }
   return false;
}

function dbShowProductDetails(id)
{
   dbShowUnderneath('productdetails_'+id, 'producthidebutton_'+id, 'productshowbutton_'+id, 200);
}

function dbShowUnderneath(contentId, hideButtonId, showButtonId, speed)
{
   if (null == speed) {
      speed = 400;
   }
   
   // Is the content div currently visible?
   var flag = true;
   if ('none' != document.getElementById(contentId).style.display &&
      'hidden' != document.getElementById(contentId).style.visibility) {
      flag = false;
   }
   
   if (flag) {
      // Show section
      $('#' + showButtonId).fadeOut(0);
	   $('#' + contentId).show(speed);
      $('#' + hideButtonId).fadeIn(speed);
   }
   else {
      // Hide section
      $('#' + hideButtonId).fadeOut(0);
      $('#' + contentId).hide(speed)
      $('#' + showButtonId).fadeIn(speed);
   }
}

function dbApplicantAction(menu, id, userId, tagLabel)
{
   var opt = menu.options[menu.selectedIndex];   
   var cobrandPath = _dbGetCobrandPath();
   
   if ('' == opt.value) {
      menu.selectedIndex = 0;
      return;
   }
      
   var colonIndex = opt.value.indexOf(':');
   
   if (colonIndex < 0) {
      // If no colon, treat as a URL
      window.location = opt.value;
      return;
   }
   
   var action = opt.value.substring(0, colonIndex);
   var popupUrl = '';
   var popupParams = ''; 
   
   if ('remove' == action) {
      // Remove a tag
      var key = opt.value.substr(7, opt.value.indexOf('|') - 7);
      var color = opt.value.substr(opt.value.indexOf('|') + 1, 7);
      var label = opt.value.substr(opt.value.indexOf('|') + 8);
      var url = 'screening/screening.ajax.php?op=3&u=' + userId + '&id=' + id + '&tag=remove';
      var errorMessage = 'Unable to remove applicant tag. Please refresh the page and try again.';
      $.ajax({
         type: 'GET',
         url: url,
         success: function(responseText) {
            if (responseText != '1') {
               alert(errorMessage);
            }
            else {
               _dbResetActionMenu(menu);
               $('#apptag' + id).html('');
               $('#apptag' + id).hide(); // Need this so IE doesn't leave a space where the tag was
            }
         },
         error: function() { alert(errorMessage) }
      });
   }
   else if ('tag' == action) {
      // Add a tag
      var key = opt.value.substr(4, opt.value.indexOf('|') - 4);
      var color = opt.value.substr(opt.value.indexOf('|') + 1, 7);
      var label = opt.value.substr(opt.value.indexOf('|') + 8);
      var url = 'screening/screening.ajax.php?op=3&u=' + userId + '&id=' + id + '&tag=' + key;
      var errorMessage = 'Unable to set applicant tag. Please refresh the page and try again.';
      $.ajax({
         type: 'GET',
         url: url,
         success: function(responseText) {
            if (responseText != '1') {
               alert(errorMessage);
            }
            else {
               _dbResetActionMenu(menu);
               $('#apptag' + id).show(); // In case it was hidden by a previous removal action
               $('#apptag' + id).html(label);
               $('#apptag' + id).css('color', color);
               opt.value = 'remove:' + key + '|' + color + label;
               opt.innerHTML = 'Remove ' + tagLabel;
            }
         },
         error: function() { alert(errorMessage) }
      });
   }
   else if ('alert' == action) {
      // Present an alert (not doing this currently)
      alert(opt.value.substr(6));
   }
   else if ('view' == action) {
      // View report -- keep in same window
      // Hide the flag if there is one
      var viewSpan = 'viewed' + id.toString();
      if ($('#' + viewSpan)) {
         $('#' + viewSpan).hide();
      }
      var pleaseWait = new Option('Please wait...', menu.value);
      menu.options[0] = pleaseWait;
      menu.selectedIndex = 0;
      menu.style.color = '#aaaaaa'; 
      window.location = cobrandPath + '/screening/report.php' + opt.value.substr(5);
   }
   else if ('print' == action) {
      // Print-friendly report
      popupUrl = cobrandPath + '/screening/print.php' + opt.value.substr(6);
      popupParams = 'status=1,toolbar=1,menubar=1,resizable=1,scrollbars=1,width=800,height=600';
   }
   else if ('email' == action) {
      // Email report
      popupUrl = cobrandPath + '/screening/email.php' + opt.value.substr(6);
      // Making this one shorter than multi-print as we'll never have to scroll
      popupParams = 'status=1,toolbar=0,menubar=0,resizable=1,scrollbars=0,width=600,height=280';
   }
   else if ('attach' == action) {
      // Attach file
      popupUrl = cobrandPath + '/screening/file.php' + opt.value.substr(7);
      popupParams = 'status=1,toolbar=0,menubar=0,resizable=0,scrollbars=0,width=500,height=200';
   }
   else if ('resend' == action) {
      // Resend Electronic Disclosure and Authorization
      popupUrl = cobrandPath + '/screening/report.php' + opt.value.substr(7) + '&resend=1';
      popupParams = 'status=1,toolbar=0,menubar=0,resizable=0,scrollbars=0,width=600,height=235';
   }
   else if ('blank' == action) {
      // Open URL in fresh window
      window.open(opt.value.substr(6), '_blank');
      return;
   }
   
   if ('' != popupUrl) {
      var w = window.open(popupUrl, action + id.toString(), popupParams);
      if (null != w) {
         w.focus();
      }
   }
   
   menu.selectedIndex = 0;
}

function _dbResetActionMenu(menu)
{
   for (var i = 0; i < menu.options.length; ++i) {
      if (menu.options[i].value.indexOf('remove:') == 0) {
         menu.options[i].value = 'tag:' + menu.options[i].value.substr(7);
         menu.options[i].innerHTML = 'Mark as ' + menu.options[i].value.substr(menu.options[i].value.indexOf('|') + 8);
      }
   }
}

function dbShowAdvancedOptions()
{
   dbShowUnderneath('divAdvanced', 'aHideAdvanced', 'aShowAdvanced');
}

function dbCheckAllBoxes(checkAll)
{
   if (checkAll.checked) {
      $('.multiactioncb').attr('checked', 'checked');
      $('.multiactiondisabled').hide(0);
      $('.multiactionenabled').show(0);
   }
   else {
      $('.multiactioncb').removeAttr('checked');
      $('.multiactionenabled').hide(0);
      $('.multiactiondisabled').show(0);
   }
}
function dbCheckOneBox()
{
   var cbTotalCount = $('.multiactioncb').length;
   var cbCheckedCount = $('.multiactioncb:checked').length;
   
   // See if the "check all" box now needs to be checked or unchecked
   if (cbCheckedCount == cbTotalCount) {
      $('#cb_all').attr('checked', 'checked');
   }
   else {
      $('#cb_all').removeAttr('checked');
   }

   // Enable or disable the multi-action links
   if (0 < cbCheckedCount) {
      $('.multiactiondisabled').hide(0);
      $('.multiactionenabled').show(0);
   }
   else {
      $('.multiactionenabled').hide(0);
      $('.multiactiondisabled').show(0);
   }
}

function dbApplicationActionMulti(url)
{
   var cobrandPath = _dbGetCobrandPath();
   
   var popupUrl = cobrandPath + url;
   // Append applicant IDs
   $('.multiactioncb:checked').each( function() {
      popupUrl = popupUrl + '&ApplicantID[]=' + this.value;
   });

   var action = '';
   var popupParams = '';
   if (-1 < url.indexOf('screening/print')) {
      // Print-friendly report
      action = 'print';
      popupParams = 'status=1,toolbar=1,menubar=1,resizable=1,scrollbars=1,width=800,height=600';
   }
   else if (-1 < url.indexOf('screening/email')) {
      // Email report
      action = 'email';
      popupParams = 'status=1,toolbar=0,menubar=0,resizable=1,scrollbars=1,width=600,height=600';
   }
   
   if ('' != popupUrl) {
      var w = window.open(popupUrl, action + 'multi', popupParams);
      if (null != w) {
         w.focus();   
      }
   }
}

function _dbGetCobrandPath()
{
   var url = window.location.href;
   var UrlParts = url.split('/');
   
   // If in a cobrand we need to include that in links
   var cobrandPath = '';
   if (UrlParts && UrlParts.length >= 3 && UrlParts[3] != 'screening') {
      cobrandPath = '/' + UrlParts[3];
   }
   return cobrandPath;
}
