
/* Create a new XMLHttpRequest object to talk to the Web server */
var xmlHttp = false;
/* color array with black(0), green(1), red(2), yellow(3), blue(4), orange(5), white(6) */
var colors = ["#000000","#00BF08","#BF0008","#FDFF00","#0050AF", "#FF7200","#FFFFFF"];
var flashTime= {"VQ" : [250,0], 
                "Q": [500,0], 
                "FL":[1000,0], 
                "ISO(2)":[1000,0], 
                "ISO(4)":[2000,0], 
                "ISO(6)":[3000,0], 
                "ISO(8)":[4000,0],
                "LFL":[2000,0],
                "Mo(A)":[1000,2500],
                "Oc(4)":[3000,0],
                "Oc(6)":[4500,0],
                "VQ(6)LFL":[250,2000],
                "Q(6)LFL":[500,2000]};
var flasher = null;
var flashState = null;
var buoyFlash = null;
var curFlash=0;
var curSide=0;
var curShipList;
var flashPeriod=0;
var darkPeriod=0;
var lightType = "";
var timer=0;
var counter=0;
var isIE = false;
/* lightpattern is represented as a 5x5 array 0=light
*  0 . . . 0
*  0 0 0 0 0
*  . 0 0 0 .
*  . 0 0 0 .
*  . 0 . 0 .
*/
var shipsFront = new Array();
shipsFront["Maak en keuze"]="";

var shipSide = new Array();
shipSide["Maak en keuze"]="";

var bridgeSide = new Array();
bridgeSide["Maak en keuze"]="";

var buoySIGNI = new Array();
buoySIGNI["Maak en keuze"]="";
var buoyIALA = new Array();
buoyIALA["Maak en keuze"]="";

// Set-up a multi div-tag direction function
function handleDivTag(divtag)
{
  var divtag;
  return divtag;
}



// Create the Divtag Handler -- Mainly an IE 6 Fix
var divhandler = new handleDivTag(null);

// Make the XMLHttpRequest object
function createRequestObject() 
{
  
  var req;
  
  if(window.XMLHttpRequest)
    {
      // Firefox, Safari, Opera...
      req = new XMLHttpRequest();
    } else if(window.ActiveXObject) 
    {
      // Internet Explorer 5+
      req = new ActiveXObject("Microsoft.XMLHTTP");
      isIE = true;
    } else 
    {
      // There is an error creating the object,
      // just as an old browser is being used.
      alert('There was a problem creating the XMLHttpRequest object');
    }
    
    return req;
    
  }
  
  xmlHttp = createRequestObject();
  
  // This function makes the HTTP request to the server
  function callServer( tempURL, divtag ) 
  {
    var url = tempURL;
    // Build the URL to connect to
    
    // Open a connection to the server
    xmlHttp.open("GET", url, true);
    
    // Setup a function for the server to run when it's done
    xmlHttp.onreadystatechange = handleResponse;
    
    // Setup the taghandler to handle the different tags
    divhandler.divtag = divtag;
    
    // Send the request
    xmlHttp.send(null);
  }
  
  // This function handles the response in text-format
  function handleResponse() 
  {
    // check if response is received and is valid
    if (xmlHttp.readyState == 4  && xmlHttp.status == 200) 
      {
        var response = xmlHttp.responseText;
        document.getElementById(divhandler.divtag).style.visibility = "visible";
        document.getElementById(divhandler.divtag).innerHTML = response; 
      }
    }
    
    // Just for demonstration purposes a separate HHTP request 
    // returning XML files
    function callXMLServer( tempURL, divtag ) 
    {
      var url = tempURL+"?ms="+new Date().getTime();
      // Build the URL to connect to
      
      // Open a connection to the server
      xmlHttp.open("GET", url, true);
      
      // Setup a function for the server to run when it's done
      xmlHttp.onreadystatechange = handleXMLResponse;
      
      // Setup the taghandler to handle the different tags
      divhandler.divtag = divtag;
      
      // Send the request
      xmlHttp.send(null);
    }
    
    // The specific XML response handler
    function handleXMLResponse() 
    {
      // check if response is received and is valid
      if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
        {
          var xmlDoc =xmlHttp.responseXML;
          switch( divhandler.divtag )
          {
            case "Lightsource" :
            handleLightsourceItems(xmlDoc);
            callXMLServer("bridges.xml","Bridgesource");
            break;
            case "Bridgesource" :
            handleBridgesourceItems(xmlDoc);
            callXMLServer("buoys.xml","Buoysource");
            break;
            case "Buoysource" :
            handleBouysourceItems(xmlDoc);
            break;
            default:
            break;
          }
        }
      }
      
      
      
      
      function handleLightsourceItems(xmlDoc)
      {
        var shipsBB = xmlDoc.documentElement.getElementsByTagName("frontside");  
        var shipsSB = xmlDoc.documentElement.getElementsByTagName("backside");  
        for( i=0; i< shipsBB[0].childNodes.length; i++)
          {
            if( shipsBB[0].childNodes[i].nodeType == 1 ){
              var name = shipsBB[0].childNodes[i].firstChild.nodeValue;
              var img = shipsBB[0].childNodes[i].childNodes[1].firstChild.nodeValue;
              shipsFront[name] = new Array(img);
            }
          }
          for( i=0; i< shipsSB[0].childNodes.length; i++)
            {
              if( shipsSB[0].childNodes[i].nodeType == 1 ){
                var name = shipsSB[0].childNodes[i].firstChild.nodeValue;
                var img = shipsSB[0].childNodes[i].childNodes[1].firstChild.nodeValue;
                shipSide[name] = new Array(img);
              }
            }
          }
          
          function handleBridgesourceItems(xmlDoc)
          {
            var bridges = xmlDoc.documentElement.getElementsByTagName("bruggen");  
            for( i=0; i< bridges[0].childNodes.length; i++)
              {
                if( bridges[0].childNodes[i].nodeType == 1 ){
                  var name = bridges[0].childNodes[i].firstChild.nodeValue;
                  var img = bridges[0].childNodes[i].childNodes[1].firstChild.nodeValue;
                  bridgeSide[name] = new Array(img);
                }
              }
            }
            

            function handleBouysourceItems(xmlDoc)
            {
              var buoysBW = xmlDoc.documentElement.getElementsByTagName("boeien");  
              var p = 1;
              checkIE()  ? j=1 : j=2;
              for( i=0; i< buoysBW[0].childNodes.length; i++)
                {
                  p=1;
                  if( buoysBW[0].childNodes[i].nodeType == 1 ){
                    var name = buoysBW[0].childNodes[i].firstChild.nodeValue;
                    var img = buoysBW[0].childNodes[i].childNodes[1].firstChild.nodeValue;
                    var top = buoysBW[0].childNodes[i].childNodes[p+=j].firstChild.nodeValue;
                    var left =  buoysBW[0].childNodes[i].childNodes[p+=j].firstChild.nodeValue;
                    var light = buoysBW[0].childNodes[i].childNodes[p+=j].firstChild.nodeValue;
                    var sequence =  buoysBW[0].childNodes[i].childNodes[p+=j].firstChild.nodeValue;
                    var duration=  buoysBW[0].childNodes[i].childNodes[p+=j].firstChild.nodeValue;
                    var regulation =  buoysBW[0].childNodes[i].childNodes[p+=j].firstChild.nodeValue;
                    var example = buoysBW[0].childNodes[i].childNodes[p+=j].firstChild.nodeValue;
                    buoySIGNI[name] = new Array(img,regulation,top,left,light,sequence,duration, example, name);
                  }
                }
             }
                
                
                function setLights( boat )
                {
                  var html="";
                  if( flasher )
                    {
                      clearTimeout( flasher );
                      flasher = null;
                      document.getElementById("MaskFront1").style.visibility="hidden";
                      document.getElementById("MaskFront2").style.visibility="hidden";
                      document.getElementById("MaskBack").style.visibility="hidden";
                      document.getElementById("MaskBack2").style.visibility="hidden";
                    }
                    if( buoyFlash )
                      {
                        clearTimeout( buoyFlash );
                        buoyFlash = null;
                        document.getElementById("BuoyMask").style.visibility="hidden";
                      }
                      var img = curShipList[boat][0];
                      if( img!="") html = "<img src='images/"+img+"'>";
                      if( boat.indexOf("BPR snelschip")>=0)
                        {
                          flasher = setTimeout( "flash('hidden')",500);
                        }
                        if( curShipList[boat].length>1 )
                          {
                            switch( curShipList[boat][1])
                            {
                              case "SIGNI":
                              case "IALA":
                              document.getElementById("BuoyMask").style.top = curShipList[boat][2];
                              document.getElementById("BuoyMask").style.left = curShipList[boat][3];
                              
                              counter = curShipList[boat][5];
                              flashPeriod = counter;
                              darkPeriod = curShipList[boat][6]*1000;
                              timer=0;
                              flashOn(curShipList[boat][4]);
                              break;
                              default:
                              break;
                            }
                            lightType = curShipList[boat][4];
                            html += "<br>Stelsel: "+curShipList[boat][1]+", Lichtkaraker "+
                            
                            curShipList[boat][8]+"<br><img src= 'images/"+curShipList[boat][7]+"'>";
                          }
                          if( html!="") document.getElementById("Lightsource").innerHTML= html;
                        }
                        
                        function flashOn( lType )
                        {
                          //set light on by setting BuoyMask to hidden
                          document.getElementById( "BuoyMask" ).style.visibility="hidden";
                          
                          if( counter>0) {
                            timer+=flashTime[ lType][0];
                            buoyFlash=setTimeout( "flashOff('"+ lType+"')",flashTime[ lType][0]);
                            } else {
                              //timer+=flashTime[ lType][1];
                              buoyFlash=setTimeout( "flashOff('"+ lType+"')",flashTime[ lType][1]);
                            }
                        }
                        
                        function flashOff( lType )
                        {
                          counter--;
                          //set light off by setting BuoyMask to visible
                          document.getElementById( "BuoyMask" ).style.visibility="visible";
                          if( counter>0) {
                            timer+=flashTime[ lType][0];
                            buoyFlash=setTimeout( "flashOn('"+ lType+"')",flashTime[ lType][0]);
                            } else {
                              if( counter==0 && flashTime[lType][1]>0)
                                {
                                  buoyFlash=setTimeout( "flashOn('"+ lType+"')",flashTime[ lType][0]);
                                }else {
                                  counter = flashPeriod;
                                  //timer+=flashTime[ lType][0];
                                  var dark = darkPeriod - timer;
                                  timer=0;
                                  buoyFlash=setTimeout( "flashOn('"+ lType+"')",dark);
                                 }
                            }
                        }
                        
                      function flash( state )
                      {
                        curFlash = state;
                        (curFlash == "visible" ? curFlash="hidden": curFlash = "visible");
                        switch(curSide){
                          case "front":
                          document.getElementById("MaskFront1").style.visibility=curFlash;
                          document.getElementById("MaskFront2").style.visibility=curFlash;
                          break;
                          case "back":
                          document.getElementById("MaskBack").style.visibility=curFlash;
                          document.getElementById("MaskBack2").style.visibility=curFlash;
                          break;
                          default:
                          
                          break;
                        }
                        flasher = setTimeout( "flash(curFlash)", 500);
                      }
                      
                      function lightOut( tag )
                      {
                        
                        document.getElementById(tag).innerHTML="";
                      }
                      // Initialization routine when the body if an HTML page is loaded
                      function onLoad()
                      {
                        callXMLServer("ships.xml","Lightsource");
                        lightOut("Lightsource");
                      } 
                      
                      function buildForm( selection )
                      {
                        var i=0;
                        curShipList=null;
                        curSide=selection;
                        switch( selection ){
                          case "front":
                          curShipList = shipsFront;
                          
                          break;
                          case "back":
                          curShipList = shipSide;
                          break;
                          case "bruggen":
                          curShipList = bridgeSide;
                          break;
                          case "boeien":
                          curShipList = buoySIGNI;
                          break;
                          default:
                          curShipList ="";
                        }
                        if( selection == "front" || selection =="back")
                        {
                          i=0;
                          while( document.selectForm.shipList.options.length>0)
                            {
                              i = document.selectForm.shipList.options.length-1;
                              document.selectForm.shipList.options[i]=null;
                            }
                            i=0;  
                            for(key in curShipList)
                              {
                                var name = key;
                                if( name!="indexOf")
                                {
                                document.selectForm.shipList.options[i] = new Option(name,name);
                                i++;
                                }
                              }
                              if( i> 0 )
                                document.getElementById("selectShip").style.visibility = "visible";
                          }
                            if( selection == "boeien" )
                            {
                              i=0;
                              while( document.selectForm3.buoyList.options.length>0)
                                {
                                  i = document.selectForm3.buoyList.options.length-1;
                                  document.selectForm3.buoyList.options[i]=null;
                                }
                                i=0;  
                                for(key in curShipList)
                                  {
                                    var name = key;
                                    if( name!="indexOf")
                                      {
                                      document.selectForm3.buoyList.options[i] = new Option(name,name);
                                      i++;
                                    }
                                  }
                              }
                              if( selection == "bruggen" )
                              {
                                i=0;
                                while( document.selectForm2.bridgeList.options.length>0)
                                  {
                                    i = document.selectForm2.bridgeList.options.length-1;
                                    document.selectForm2.bridgeList.options[i]=null;
                                  }
                                  i=0;  
                                  for(key in curShipList)
                                    {
                                      var name = key;
                                      if( name!="indexOf")
                                      {
                                      document.selectForm2.bridgeList.options[i] = new Option(name,name);
                                      i++;
                                      }
                                    }
                                  }
                                }
                                        
                                function hide( tag )
                                {
                                  lightOut("Lightsource");
                                  document.getElementById( tag ).style.visibility = "hidden";
                                }

                                function show( tag )
                                {
                                  document.getElementById( tag ).style.visibility = "visible";
                                }

                                function showCategory( cat )
                                {
                                  lightOut("Lightsource");
                                  hide("BridgeSelection" );

                                  hide("BuoySelection");
                                  //hide("selectBuoy");
                                  hide("ShipSelection");
                                  hide("selectShip");
                                  switch( cat){
                                    case "schepen":
                                    show("ShipSelection");
                                    break;
                                    case "bruggen":
                                    show("BridgeSelection" );
                                    break;
                                    case "boeien":
                                    show("BuoySelection");
                                    break;
                                    default:
                                    break;
                                  }
                                }
                                
function checkIE()
{
  if (navigator.appName == 'Microsoft Internet Explorer') 
	{
	  return true;
	} else return false;
}                                
                                        