//
// VERIDIAN CORE JAVASCRIPT
//
// These functions apply to all Veridian installations, regardless of the interface being used.
// Editing these functions is not recommended as all Veridian installations will be affected!
//
// Copyright (C) 2006-2010  DL Consulting Ltd.
//


// ----------------------------------------------------------------------------------------------
//  LOGICAL SECTION HIGHLIGHTING
// ----------------------------------------------------------------------------------------------

var currentlyHighlightedLogicalSectionOID = -1;
var currentlySelectedLogicalSectionOID = -1;

function logicalSectionOnClick(logicalSectionOID)
{
  // If the logical section is currently selected we don't need to do anything
  if (logicalSectionOID == currentlySelectedLogicalSectionOID)
  {
    return true;
  }
  // Unselect the previous selected logical section, if necessary
  if (currentlySelectedLogicalSectionOID != -1)
  {
    selectLogicalSection(currentlySelectedLogicalSectionOID, false);
    currentlySelectedLogicalSectionOID = -1;
  }

  // Otherwise select the logical section
  selectLogicalSection(logicalSectionOID, true);
  currentlySelectedLogicalSectionOID = logicalSectionOID;

  // Return something
  return true;
}

function logicalSectionOnMouseOver(logicalSectionOID)
{
  // If the logical section is currently highlighted or selected we don't need to do anything
  if (logicalSectionOID == currentlyHighlightedLogicalSectionOID || logicalSectionOID == currentlySelectedLogicalSectionOID)
  {
    return true;
  }
  // Otherwise highlight the logical section
  highlightLogicalSection(logicalSectionOID, true);
  currentlyHighlightedLogicalSectionOID = logicalSectionOID;

  // Return something
  return true;
}

function logicalSectionOnMouseOut(logicalSectionOID)
{
  // Unhighlight the logical section, unless it is selected
  if (logicalSectionOID != currentlySelectedLogicalSectionOID)
  {
    highlightLogicalSection(logicalSectionOID, false);
    currentlyHighlightedLogicalSectionOID = -1;
  }

  // Return something
  return true;
}


function highlightLogicalSection(logicalSectionOID, highlight)
{
  for (var i = 0; i < logicalSectionAreas[logicalSectionOID].length; i++)
  {
    var logicalSectionArea = document.getElementById(logicalSectionAreas[logicalSectionOID][i].id);
    if (logicalSectionArea == undefined) continue;

    if (highlight)
    {
      logicalSectionArea.className = 'veridianlogicalsectionhighlighted';
    }
    else
    {
      logicalSectionArea.className = 'veridianlogicalsectionarea';
    }
  }
}

function selectLogicalSection(logicalSectionOID, select)
{
  for (var i = 0; i < logicalSectionAreas[logicalSectionOID].length; i++)
  {
    var logicalSectionArea = document.getElementById(logicalSectionAreas[logicalSectionOID][i].id);
    if (logicalSectionArea == undefined) continue;

    if (select)
    {
      logicalSectionArea.className = 'veridianlogicalsectionselected';
    }
    else
    {
      logicalSectionArea.className = 'veridianlogicalsectionarea';
    }
  }
}


// ----------------------------------------------------------------------------------------------
//  QUERY VALIDATION
// ----------------------------------------------------------------------------------------------

// Returns true if the search parameters are valid, false otherwise
function validateSearch()
{
  // Check that the "from" date isn't after the "to" date
  if (checkDates() == false) {
    alert(badDatesMessage);
    return false;
  }

  // Everything is fine, continue with the search
  return true;
}


// Returns false if the "from" date is after the "to" date, true otherwise
function checkDates()
{
  // Check year
  var arg_dafyq = ((document.AdvancedQueryForm.dafyq.value != "") ? document.AdvancedQueryForm.dafyq.value : "0000");
  var arg_datyq = ((document.AdvancedQueryForm.datyq.value != "") ? document.AdvancedQueryForm.datyq.value : "9999");
  if (arg_dafyq < arg_datyq) return true;
  if (arg_dafyq > arg_datyq) return false;

  // Check month (years are the same)
  var arg_dafmq = ((document.AdvancedQueryForm.dafmq.value != "") ? document.AdvancedQueryForm.dafmq.value : "00");
  var arg_datmq = ((document.AdvancedQueryForm.datmq.value != "") ? document.AdvancedQueryForm.datmq.value : "99");
  if (arg_dafmq < arg_datmq) return true;
  if (arg_dafmq > arg_datmq) return false;

  // Check day (years and months are the same)
  var arg_dafdq = ((document.AdvancedQueryForm.dafdq.value != "") ? document.AdvancedQueryForm.dafdq.value : "00");
  var arg_datdq = ((document.AdvancedQueryForm.datdq.value != "") ? document.AdvancedQueryForm.datdq.value : "99");
  if (arg_dafdq < arg_datdq) return true;
  if (arg_dafdq > arg_datdq) return false;

  // Days are the same also; this is OK
  return true;
}

