function UpdateHaltelijst()
{
   var i;

   document.getElementById("haltenr2").options.length = 0;
   if(document.getElementById("haltenr1").selectedIndex + 1==document.getElementById("haltenr1").options.length) {
	document.getElementById("haltenr1").selectedIndex=0;
   }
   for (i = document.getElementById("haltenr1").selectedIndex + 1; i < document.getElementById("haltenr1").options.length; i++)
      document.getElementById("haltenr2").options[i - (document.getElementById("haltenr1").selectedIndex + 1)] = new Option(document.getElementById("haltenr1").options[i].text, document.getElementById("haltenr1").options[i].value);
}

function UpdateTijden(alletijden)
{
   var lijstindex = -1;
   var i;

   if (alletijden == "1")
   {
      dagindex = document.getElementById("daglijst").selectedIndex;
      document.getElementById("dagtijdform").dagindex.value = dagindex.toString();
      return true;
   }
   else
   {
      if (document.getElementById("daglijst"))
      {
         dagindex = document.getElementById("daglijst").selectedIndex;
         tijdstip = document.getElementById("tijdlijst").options[document.getElementById("tijdlijst").selectedIndex].text;
      }

      if (tijdstip != "")
      {
         for (i = 0; i < rit[0][dagindex].length - 9; i++)
            if ((rit[0][dagindex][i] >= tijdstip) && (lijstindex == -1))
               lijstindex = i - 1; 
 
         if (lijstindex == -1)
            ritindex = rit[0][dagindex].length - 9;
         else
            if (lijstindex != ritindex)
               ritindex = lijstindex;
      }
      
      ToonTijden();

      //JKA: Okt2009 Check if rittable exists (new page PRIJZEN does not have rittabel)
      if (document.getElementById("vroegerbutton")) 
      {
        document.getElementById("vroegerbutton").src = (ritindex == 0) ? "images/htmweb/knoppen/btn_vroeger_dimm.gif" : "images/htmweb/knoppen/btn_vroeger.gif";
        document.getElementById("vroegerbutton").disabled = (ritindex == 0);
        document.getElementById("laterbutton").src = (ritindex == rit[0][dagindex].length - 9) ? "images/htmweb/knoppen/btn_later_dimm.gif" : "images/htmweb/knoppen/btn_later.gif";
        document.getElementById("laterbutton").disabled = (ritindex == rit[0][dagindex].length - 9);
      }
   }
    
   return false; 
}

function ToonTijden()
{
   var starttijd, eindtijd, tijdsduur;
   var dag, uren, minuten;
   var i, j;
   // check IE
   toonvalue = (document.all && document.getElementById) ? "inline" : "table-row";

	//JKA: Okt2009 Check if rittable exists (new page PRIJZEN does not have rittabel)
	if (document.getElementById("rittabel"))
	{
	   for (i = 0; i < document.getElementById("rittabel").rows.length - 1; i++)
	   {
		  if ((i > 0) && (i < (document.getElementById("rittabel").rows.length - 2)))
			 document.getElementById("rittabel").rows[i].style.display = (showhaltes) ? toonvalue : "none";

		  for (j = 2; j < document.getElementById("rittabel").rows[i].cells.length; j++)
			 document.getElementById("rittabel").rows[i].cells[j].innerHTML = rit[i][dagindex][ritindex + (j - 2)];
	   }

	   i = document.getElementById("rittabel").rows.length - 2;
	   for (j = 2; j < document.getElementById("rittabel").rows[i].cells.length; j++)
	   {
		  starttijd = rit[0][dagindex][ritindex + (j - 2)];
		  eindtijd  = rit[i][dagindex][ritindex + (j - 2)];

		  if (starttijd != "")
		  {
			 dag = (Number(eindtijd.substr(0, 2)) < Number(starttijd.substr(0, 2))) ? 24 : 0;
			 tijdsduur = ((Number(eindtijd.substr(0, 2)) + dag) * 60 + Number(eindtijd.substr(3, 2))) - 
						 (Number(starttijd.substr(0, 2)) * 60 + Number(starttijd.substr(3, 2)));
			 uren      = Math.floor(tijdsduur / 60);
			 minuten   = tijdsduur % 60;

			 tijdsduur = ((uren.toString().length == 1) ? "0" + uren : uren) + ":"; 
			 tijdsduur += (minuten.toString().length == 1) ? "0" + minuten : minuten;
			 document.getElementById("rittabel").rows[i + 1].cells[j].innerHTML = tijdsduur;
		  }
		  else
			 document.getElementById("rittabel").rows[i + 1].cells[j].innerHTML = "&nbsp;";
	   }
	}
}

function Vroeger()
{
   if (ritindex > 0)
      ritindex--;

   ToonTijden();
   UpdateButtons();

   return false;
}

function Later()
{
   if (ritindex < rit[0][dagindex].length - 9)
      ritindex++;

   ToonTijden();
   UpdateButtons();

   return false;
}

function UpdateButtons()
{
   if (ritindex == 0)
      document.getElementById("vroegerbutton").src = "/images/htmweb/knoppen/btn_vroeger_dimm.gif";
   else
      document.getElementById("vroegerbutton").src = "/images/htmweb/knoppen/btn_vroeger.gif";
   document.getElementById("vroegerbutton").disabled = (ritindex == 0);

   if (ritindex == rit[0][dagindex].length - 9)
      document.getElementById("laterbutton").src = "/images/htmweb/knoppen/btn_later_dimm.gif";
   else
      document.getElementById("laterbutton").src = "/images/htmweb/knoppen/btn_later.gif";
   document.getElementById("laterbutton").disabled = (ritindex == rit[0][dagindex].length - 9);
}

function ToggleHaltes()
{
   showhaltes = !showhaltes;
   ToonTijden();

   document.getElementById("haltesbutton").value = (showhaltes) ? "Verberg tussenhaltes" : "Toon tussenhaltes";
   return false;
}

function ToonRouteTijden()
{
   var routeindex, ritindex;
   var datum, starttijd;
   var i, j;

   routeindex = document.getElementById("routelijst").options.selectedIndex;
   datum = new Date();
   
   starttijd =  ((datum.getHours().toString().length == 1) ? "0" + datum.getHours() : datum.getHours()) + ":";
   starttijd += ((datum.getMinutes().toString().length == 1) ? "0" + datum.getMinutes() : datum.getMinutes());

   ritindex = -1;
   if (rit[routeindex][0]) {
      if ( rit[routeindex][0]==-2) {
         ritindex = -2;
      } else {
        for (i = 0; i < rit[routeindex][0].length; i++)
           if ((rit[routeindex][0][i] > starttijd) && (ritindex == -1))
              ritindex = i;
      }
   }
   if (ritindex == -1)
   {
      document.getElementById("rittabel").style.display = "none";
      document.getElementById("rittekst").style.display = "inline";
      document.getElementById("lijnerror").style.display = "none";
   }   
   else
   {
      if (ritindex == -2) {
        document.getElementById("rittabel").style.display = "none";
        document.getElementById("rittekst").style.display = "none";
        document.getElementById("lijnerror").style.display = "inline";
      } else {
        document.getElementById("rittabel").style.display = "inline";
        document.getElementById("rittekst").style.display = "none";
        document.getElementById("lijnerror").style.display = "none";
  
        for (i = 0; i < document.getElementById("rittabel").rows.length; i++)
        {
           document.getElementById("rittabel").rows[i].cells[1].innerHTML = "<strong>" + halte[routeindex][i] + "</strong>";
        
           for (j = 0; j < document.getElementById("rittabel").rows[i].cells.length - 2; j++)
              if (ritindex + j < rit[routeindex][i].length)
                 document.getElementById("rittabel").rows[i].cells[j + 2].innerHTML = rit[routeindex][i][ritindex + j];
              else
                 document.getElementById("rittabel").rows[i].cells[j + 2].innerHTML = "&nbsp;";
        }
      }
   }
      
   return false;
}
