var peakNames_0 = new Array("Elidir Fach",
                            "Elidir Fawr",
                            "Mynydd Perfedd",
                            "Foel Goch",
                            "Y Garn",
                            "Glyder Fawr",
                            "Glyder Fach",
                            "Tryfan", 
                            "Ogwen");

var peakNames_1 = new Array("Pen yr Ole Wen",
                            "Carnedd Dafydd",
                            "Carnedd Llewelyn",
                            "Pen yr Helgi Du",
                            "Pen Llithrig yr Wrach", 
                            "Capel Curig");

var peakNames_2 = new Array("Moel Siabod",
                            "Clogwyn Bwlch y Maen",
                            "Carnedd y Gribau",
                            "Cerrig Cochion",
                            "Moel Merch",
                            "Ysgafael Wen",
                            "Mynydd Llynnau'r Cwn",
                            "Unnamed",
                            "Moel Druman",
                            "Allt Fawr",
                            "Foel Ddu",
                            "Moel yr Hydd",
                            "Moelwyn Bach",
                            "Craigysgafn",
                            "Moelwyn Mawr",
                            "Cnicht", 
                            "Aberglaslyn");

var peakNames_3 = new Array("Bryn Banog",
                            "Moel Hebog",
                            "Moel yr Ogof",
                            "Moel Lefn",
                            "Yr Gyrn",
                            "Mynydd y Ddwy Elor",
                            "Trum y Ddysgl",
                            "Mynydd Drws y Coed",
                            "Y Garn",
                            "Rhyd Ddu");

var peakNames_4 = new Array("Craig Wen",
                            "Yr Aran",
                            "Cribau Tregalan",
                            "Yr Wyddfa",
                            "Carnedd Ugain",
                            "Moel Cynghorion",
                            "Foel Goch",
                            "Foel Gron",
                            "Moel Eilio", 
                            "Llanberis");

var peakNames = new Array();
peakNames[0] = peakNames_0;
peakNames[1] = peakNames_1;
peakNames[2] = peakNames_2;
peakNames[3] = peakNames_3;
peakNames[4] = peakNames_4;


// The appropriate times for each section above.
var clockTimes_0 = new Array(63,15,14,13,25,35,16,22,15);
var clockTimes_1 = new Array(55,18,30,23,25,37);
var clockTimes_2 = new Array(68,22,10,34,13,17,4,7,10,13,28,10,33,12,11,52,52);
var clockTimes_3 = new Array(55,36,20,9,22,9,27,9,8,18);
var clockTimes_4 = new Array(64,21,49,21,11,41,27,15,21,41);

var clockTimes = new Array();

clockTimes[0] = clockTimes_0;
clockTimes[1] = clockTimes_1;
clockTimes[2] = clockTimes_2;
clockTimes[3] = clockTimes_3;
clockTimes[4] = clockTimes_4;

var restTimes = new Array(10,10,15,15);

function FillOut()
{
   var monthNames = new Array("January", "February", "March", "April",
                              "May", "June", "July", "August", "September",
                              "October", "November", "December")
   
   var startLocations = new Array("Llanberis", "Ogwen", "Capel Curig", 
                                  "Aberglaslyn", "Rhyd Ddu");
   for(i = 0; i < 24; ++i)
   {
      document.pbCalc.startTime.options[i]=new Option(i);
   }
   for(i = 0; i < 12; ++i)
   {
      document.pbCalc.month.options[i]=new Option(monthNames[i])
   }
   // Set up the days for June
   for(i = 0; i < 30; ++i)
   {
      document.pbCalc.day.options[i]=new Option(i+1)
   }

   for(i = 0; i < 5; ++i)
   {
      document.pbCalc.startPoint.options[i]=new Option(startLocations[i]);
   }

   var now = new Date()
   var curYear = now.getYear()
   if(curYear < 2000)
   {
      curYear += 1900
   }
   for(i = 0; i < 5; ++i)
   {
      document.pbCalc.year.options[i]=new Option(curYear+i)
   }
   
   document.pbCalc.startPoint.selectedIndex = 0;
   document.pbCalc.schedule.selectedIndex = 0;
   document.pbCalc.month.selectedIndex=5;
   document.pbCalc.day.selectedIndex=19;
}


function populateDays(month)
{
   var monthDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
   var days = monthDays[month.selectedIndex]

   document.pbCalc.day.options.length=0

   for(i = 0; i < days ; ++i)
   {
      document.pbCalc.day.options[i]=new Option(i + 1)
   }
}

function pbCalculate(pbForm)
{
   calculate(pbForm);

   var monthNames = new Array("January", "February", "March", "April",
                              "May", "June", "July", "August", "September",
                              "October", "November", "December");
   var duskTimes = new Array(45, 50, 40, 45, 50, 75, 60, 40, 35, 35, 40, 45);

   var curTime = pbForm.startTime.selectedIndex * 60; /* in minutes rather than hours */

   var nightFall = getMinutes(pbForm.sunset.value);
   var sunrise = getMinutes(pbForm.sunrise.value);
   var minutesInDay = 24 * 60;
   var index = 0;
   var curHour;
   var curMin;
   var hourString;
   var minString;
   var scheduleTime = 23 - pbForm.schedule.selectedIndex ;
   var newContent = "<html><head><title>Paddy Buckley Schedule<\/title><\/head>";
   //var direction = (pbForm.direction[0].checked == true) ? "clockwise" : "anticlockwise";
   var startString = (pbForm.startTime.selectedIndex < 10) ? "0" + pbForm.startTime.selectedIndex: "" + pbForm.startTime.selectedIndex;
   var legTime;
   var lightStatus;
   var dayName = curDay(pbForm);
   var legCount = 2;
   var legAccumulator = 0;
   var legHour;
   var legMin;
   var legHourString;
   var legMinString;
   var bstTime;
   var dusk = duskTimes[pbForm.month.selectedIndex];
   var bst = inBST(pbForm);

   newContent += "<body><h1>Paddy Buckley Schedule<\/h1><h3>This is a schedule for a " + scheduleTime + " hour round beginning at " + startString + "00hrs on " + dayName + " " + (pbForm.day.selectedIndex + 1) + " " + monthNames[pbForm.month.selectedIndex]  + "<\/h3>";
   if(bst)
   {
      newContent += "<p>All times are BST.<\/p>";
   }
   else
   {
      newContent += "<p>All times are GMT.<\/p>";
   }

   newContent += "<table border=2><tr><td>Sunrise<\/td><td>" + pbForm.sunrise.value + "<\/td><\/tr><tr><td>Sunset<\/td><td>" + pbForm.sunset.value + "<\/td><\/tr>";
   newContent += "<tr><td><b>Location<\/b><\/td><td><b>State of light<\/b><\/td><td><b>Leg time<\/b><\/td><td><b>Estimated time<\/b><\/td><td><b>Your time at this location (notes)<\/b><\/td><\/tr>";

   for(leg = 0; leg < 5; ++leg)
   {
      // Get a reference to the next peak name and time arrays.
      var curSection = (pbForm.startPoint.selectedIndex + leg) % 5;
      var index = 0;

      newContent += "<tr><td><b>Leg " + (leg+1) + "</b<\/td><td colspan='4'>&nbsp<\/td><\/tr>";
      newContent += "<tr><td>Companion name<\/td><td colspan='4'>&nbsp<\/td><\/tr>";
      newContent += "<tr><td>Companion name<\/td><td colspan='4'>&nbsp<\/td><\/tr>";
   
      while(index < peakNames[curSection].length)
      {
         adjLegTime = Math.floor((clockTimes[curSection][index] * scheduleTime) / 23);
         curTime += adjLegTime;   
         legAccumulator += adjLegTime;
         curTime %= minutesInDay;
         bstTime = curTime;
         if(bst)
         {
           bstTime += 60;
           bstTime %= minutesInDay;
         }

         curHour = Math.floor(curTime / 60);
         curMin = curTime % 60;

         hourString = (curHour < 10) ? "0" + curHour: "" + curHour;
         minString = (curMin < 10) ? "0" + curMin: "" + curMin;
         legTime = (clockTimes[curSection][index] < 10) ? "0" + clockTimes[curSection][index]: "" + clockTimes[curSection][index];
        
         /* Print out lines such as "08:32  Daylight:    Steel Fell" */
         lightStatus = getLightStatus(sunrise, nightFall, bstTime, dusk);
         newContent +="<tr><td>" + peakNames[curSection][index] + "<\/td><td>" + lightStatus + "<\/td><td>" + adjLegTime + "<\/td><td>" + hourString + ":" + minString + "<\/td><td>&nbsp<\/td><\/tr>";
         
         index++;
      }
      legHour = Math.floor(legAccumulator / 60);
      legMin = legAccumulator % 60;
      legHourString = (legHour < 10) ? "0" + legHour: "" + legHour;
      legMinString = (legMin < 10) ? "0" + legMin: "" + legMin;
      newContent +="<tr><td colspan='2'>Leg time<\/td><td colspan='3'><b>" + legHourString + ":" + legMinString + "<\/b><\/td><\/tr>";
      legAccumulator = 0;
      if(leg < 4)
      {
         curTime += restTimes[leg];
         curHour = Math.floor(curTime / 60);
         curMin = curTime % 60;

         hourString = (curHour < 10) ? "0" + curHour: "" + curHour;
         minString = (curMin < 10) ? "0" + curMin: "" + curMin;
         newContent +="<tr><td>Rest<\/td><td>&nbsp<\/td><td>" + restTimes[leg] + "<\/td><td>" + hourString + ":" + minString + "<\/td><td>&nbsp<\/td><\/tr>";      
      }
   }
   
   newContent += "<\/table><br><p>Table created from http:\/\/bobwightman.co.uk\/run_pb/paddy_buckley_calculator.php (Bob Wightman)<\/body><\/html>";

   newWindow = window.open("Paddy Buckley Schedule", "aaa","status,resizable,scrollbars,toolbar,height=400,width=800");
   newWindow.focus();

   newWindow.document.write(newContent);
   newWindow.document.close();
}

addEvent(window, "load", FillOut, false);
