///////////////////////////////////////////////////////////////////////////////////////////////////
//
// $Id: CommonFunctions.js,v 1.3 2008/11/25 13:33:25 markr Exp markr $
//
///////////////////////////////////////////////////////////////////////////////////////////////////


function reDisplay()
{
   for (i=0; i < g_markerIndex; ++i)
   {
      if ((g_theColours[g_markers[i].colourIndex].isChecked == true)
            &&
            (g_markers[i].matchSearch == true)
            &&
            (g_markers[i].inRange == true)) {
         g_markers[i].theMarker.show();
      } else {
         g_markers[i].theMarker.hide();
      }
   }
}

function checkClick() {
   for (i=0; i < g_NUM_COLOURS; ++i) {
      g_theColours[i].isChecked = document.SelectColour[i].checked;
   }
   reDisplay();
}

function searchKeyWords() {
   var haystack = "";
   for (i=0; i < g_markerIndex; ++i) {
      haystack = g_markers[i].theIssue;
      if (haystack.search(document.KeyWordSearch.TextSearch.value.toUpperCase()) == -1) {
         g_markers[i].matchSearch = false;
      } else {
         g_markers[i].matchSearch = true;
      }
   }
   reDisplay();
}


function clearKeyWords()
{
   document.KeyWordSearch.TextSearch.value = "";
   for (i=0; i < g_markerIndex; ++i) {
      g_markers[i].matchSearch = true;
   }
   reDisplay();
}


function addCountToSelectRange(newSum) {
   for (var i = 0; i < g_counts.length; ++i) {
      if (g_counts[i] == newSum) {
         return;
      }
   }
   g_counts[g_counts.length] = newSum;
}


function populateCountBoxes() {
   g_counts.sort(function(a,b){return a - b});
   for (var i = 0; i < g_counts.length; ++i) {
      document.SelectCount.minCountSelect[document.SelectCount.minCountSelect.length]
         = new Option(g_counts[i],g_counts[i]);
      document.SelectCount.maxCountSelect[document.SelectCount.maxCountSelect.length]
         = new Option(g_counts[i],g_counts[i]);
   }
   document.SelectCount.maxCountSelect[document.SelectCount.maxCountSelect.length-1].selected = true;
}


function changeCountSelection() {
   var minNum = 1;
   var maxNum = 1;
   minNum = document.SelectCount.minCountSelect.value;
   maxNum = document.SelectCount.maxCountSelect.value;

   for (var i=0; i < g_markerIndex; ++i) {
      if ((g_markers[i].theCount >= minNum)
            &&
         (g_markers[i].theCount <= maxNum)) {
         g_markers[i].inRange = true;
      } else {
         g_markers[i].inRange = false;
      }
   }
   reDisplay();
}



function addStreetToSource(newStreetName) {
   for (var i = 0; i < g_sourceStreetArray.length; ++i) {
      if (g_sourceStreetArray[i] == newStreetName) {
         return;
      }
   }
   g_sourceStreetArray[g_sourceStreetArray.length] = newStreetName;
}

function populateSourceStreetBox() {
   g_sourceStreetArray.sort();
   for (var i = 0; i < g_sourceStreetArray.length; ++i) {
      document.SelectStreets.sourceStreets[document.SelectStreets.sourceStreets.length]
         = new Option(g_sourceStreetArray[i]);
   }
}

function includeSelectedStreet(streetName) {
   var LeftListBox = document.SelectStreets.sourceStreets;

   for(var i=(LeftListBox.options.length - 1);i>=0;i--)
      if (LeftListBox.options[i].text == streetName) {
         LeftListBox.options[i].selected = true;
         break;
      }
}


function OnTransferBtnClick(blnFromLeft)
{
   var LeftListBox = document.SelectStreets.sourceStreets;
   var RightListBox = document.SelectStreets.targetStreets;

   var ListItems = new Array();
   FromList = (blnFromLeft ? LeftListBox : RightListBox);
   ToList = (blnFromLeft ? RightListBox : LeftListBox);
   for(var i=(FromList.options.length - 1);i>=0;i--)
      if(FromList.options[i].selected)
      {
         ListItems[ListItems.length] = new Option(FromList.options[i].text);
         FromList.options[i] = null;
      }
   for(var i=ListItems.length - 1;i>=0;i--)
      ToList.options[ToList.options.length] = ListItems[i];
}

function OnBtnSubmitClick()
{
   ListBox = document.SelectStreets.targetStreets;
   if(ListBox.options.length==0)
      alert("You did not select any item/items. Please 'Include' item(s) from the top list box and transfer them to the bottom list box");
   else
   {
      for (var i=0; i < g_markerIndex; ++i) {
         // All markers should be cleared before checking against streets
         g_markers[i].matchSearch = false;

         for(var j=0;j<ListBox.options.length;++j) {
            if (g_markers[i].theLocation == ListBox.options[j].text) {
               g_markers[i].matchSearch = true;
               // If there is a match, ensure that the "colour" is also ticked
               g_theColours[g_markers[i].colourIndex].isChecked = true;
               document.SelectColour[g_markers[i].colourIndex].checked = true;

               // Break out of the current loop and go straight to the next marker
               break;
            }
         }
      }
      reDisplay();
   }
}

function getColour(statusField)
{
   var returnColour = "white";

   if       ("TODO"     == statusField) { returnColour = "red";      }
   else if  ("ONGOING"  == statusField) { returnColour = "yellow";   }
   else if  ("DONE"     == statusField) { returnColour = "green";    }

   return returnColour;
}

