// return 2 digit number from 1/2 digit integer
function NN(x) { return (x<0||x>=10?"":"0") + x }


// return dd.MM.yy from date object (assumes 2000<year<2099 since intended for
// current/future date
function ddMMyy(adate) {
  with (adate) {
    return NN(getDate()) + "." + NN(getMonth()+1) + "." + NN((getYear()-2000))
  } 
}


// return Dy dd.MM.yy from date object (same assumptions)
function DyddMMyy(adate) {
  var DA = "SuMoTuWeThFrSa"
  var d1=2*adate.getDay()
  return DA.substring(d1, d1+2) + " " + ddMMyy(adate)
}


// return a date object if a date can be found listed in [xx] dd.MM.yy form
function ParseDateField(aString) {
  with (aString) {
    var p1 = indexOf(".")
    var dd = parseInt(substr(p1-2, 2), 10)
    var MM = parseInt(substr(p1+1, 2), 10)-1
    var yy = parseInt(substr(p1+4, 2), 10)+2000
    var aDate = new Date(yy, MM, dd)
    return aDate
  }
}

// return next Monday or Wednesday after date listed in [xx] dd.MM.yy form
function DateUp(aField) {
  var aDate = ParseDateField(aField.value)
  with (aDate) {
    var dow = getDay()
    // dow=0 for Sunday, etc
    var offset = ( (dow==0)?1 : ((dow>=1)&&(dow<=2))?(3-dow) : ((dow>=3)&&(dow<7))?(8-dow): 0 )
    setDate(getDate()+offset)
  }
  aField.value=DyddMMyy(aDate)
  return true
}

// return previous Monday or Wednesday to date listed in [xx] dd.MM.yy form
function DateDown(aField) {
  var aDate = ParseDateField(aField.value)
  with (aDate) {
    var dow = getDay()
    var offset = ( (dow<=1)?(4+dow) : ((dow==2)|(dow==3))?(dow-1) : ((dow>=4)&&(dow<7))?(dow-3): 0 )
    setDate(getDate()-offset)
  }
  aField.value=DyddMMyy(aDate)
  return true
}


// add rows corresponding to list of runs found in passed array
// (intended to be "runs", set up by runlist.js)
// first array index: consecutive run numbering as array set up
// second array index: 0=date; 1=place; 2=map url; 3=closest stop
function WriteRuns(theRuns, DateField, LocField) {

  var i = 0
  var CommentTypes = 0
  var DateTest= new Date()
  DateTest.setDate(DateTest.getDate()-1)

  document.write("</tr>")

  while (i<runs.length){
    if (ParseDateField(theRuns[i][0])>DateTest) {
      // write in row if run date has not yet passed
      // start row and first column
      document.write('<tr><td>')
      // first column entry - button with run date
      document.write('<input type="button" style="width:85px"  value="'+theRuns[i][0]+'" name="bt"'+i)
      document.write(' onClick="document.formular.'+DateField+'.value=')
      document.write("'"+theRuns[i][0]+"';")
      if (theRuns[i][4]!=0) {
        document.write(' document.formular.'+LocField+'.value=')
        document.write("'"+theRuns[i][3]+"';")
      }
      document.write('"')
      document.write(' style="font-family:Arial, Helvetica, sans-serif; font-size:8pt; background-color:#E9E9E9; height:18px; color:#ff0000; border:solid 1px #cccccc; padding:0; margin:0; cursor:pointer; line-height:8pt; vertical-align:middle;">')
      document.write('<\/input>')
      document.write('<\/td><td>')
      // second column entry - run location with link to map url, if given
      if (theRuns[i][2]=='') {
        document.write(theRuns[i][1])
      }
      else {
        document.write('<a target="_blank" href="'+theRuns[i][2]+'">')
        document.write(theRuns[i][1])
        document.write('<\/a>')
      }
      document.write('<\/td><td>')
      // third column entry - closest bus/train stop
      if ((theRuns[i][4]&63)|(theRuns[i][4]==0)) {
        // open generic formatting for dodgy stops
        document.write('<font color="#ff0000"><i>')
      }

      document.write(theRuns[i][3])
      if (theRuns[i][4]&1) {
        // indicate footnote for specific dodgy stop type
        document.write('<sup>&nbsp;&dagger;<\/sup>')
        CommentTypes|=1
      }
      if (theRuns[i][4]&2) {
        // indicate footnote for specific dodgy stop type
        document.write('<sup>&nbsp;&Dagger;<\/sup>')
        CommentTypes|=2
      }
      if (theRuns[i][4]&4) {
        // indicate footnote for specific dodgy stop type
        document.write('<sup>&nbsp;*<\/sup>')
        CommentTypes|=4
      }
      if (theRuns[i][4]&8) {
        // indicate footnote for specific dodgy stop type
        document.write('<sup>&nbsp;#<\/sup>')
        CommentTypes|=8
      }

      if ((theRuns[i][4]&63)|(theRuns[i][4]==0)) {
        // close generic formatting for dodgy stops
        document.write('<\/i><\/font>')
      }
      document.write('<\/td><\/tr>')
    }
    i++
  }

  // add footnotes if any of the run location comments have been set
  if (CommentTypes&63) {
    document.write('<tr><td colspan="3"><font color="#ff0000">Notes:')
    if (CommentTypes&1) {
      document.write('<br><sup>&dagger;</sup>&nbsp;Stop is a long way from run site')
    }
    if (CommentTypes&2) {
      document.write('<br><sup>&Dagger;</sup>&nbsp;Lifts likely to be available from stop to run site')
    }
    if (CommentTypes&4) {
      document.write('<br><sup>*</sup>&nbsp;Public transport possible in principle (outbound at least...), but terrible times')
    }
    if (CommentTypes&8) {
      document.write('<br><sup>#</sup>&nbsp;Indicated stop is an unconfirmed guess')
    }
    document.write('</font><\/td><\/tr>')
  }
}
