//alert("JavaScript activated")

//Which Web Browser is Being Used?
isNav = (navigator.appName == "Netscape")
isIE = (navigator.appName == "Microsoft Internet Explorer")

var monthList = new Array(" ", "January", "February", "March", "April", "May",
                          "June", "July", "August", "September", "October",
                          "November", "December") 
var daysInMonth = new Array( " ", "31", "28", "31", "30", "31",
                             "30", "31", "31", "30", "31",
                             "30", "31")

var currentYear = lastYear
var currentMonth = lastMonth
var currentDay = lastDay
var currentCycle = lastCycle
var currentFor = 06

isLeapYear()
var firstYear = lastYear
var firstMonth = lastMonth - 1
var firstDay = lastDay
checkFirstDay()

function isLeapYear() {
  var diff = 1
  diff = currentYear - (Math.round(currentYear / 4) * 4)
  if ( diff == 0 ) {
    daysInMonth[2] = 29
  } else {
    daysInMonth[2] = 28
  }
}

function checkFirstDay() {
  if ( firstMonth < 1 ) { firstMonth = 12 }
  if ( firstMonth == 12 ) { firstYear = lastYear -1 }
  if ( firstDay > daysInMonth[firstMonth] ) {
    ++firstMonth
    firstDay = 1
  }
//alert("Month "+firstMonth+" "+lastMonth)
//alert("Day "+firstDay+" "+lastDay)
//alert("Year "+firstYear+" "+lastYear)
}

function setCurrentYear () {
  var mySelect = document.hpc.year
  currentYear = mySelect.options[mySelect.selectedIndex].value
  monthOptions()
}

function monthOptions () {
  mySelect = document.hpc.month
  mySelect.options.length = 0
  if ( currentYear == lastYear ) { currentMonth  = lastMonth }
  if ( currentYear == firstYear ) { currentMonth = firstMonth }
  var printValue = currentMonth.toString()
  if ( currentMonth < 10 ) { printValue = "0" + currentMonth.toString() }
  mySelect.options[0] = new Option(monthList[currentMonth], printValue, false, false)
  dayOptions()
}

function setCurrentMonth() {
  var mySelect = document.hpc.month
  currentMonth = mySelect.options[mySelect.selectedIndex].value
  var controlValue = 09
  var results = controlValue & currentMonth
  if ( currentMonth == "08" || currentMonth == "09") { currentMonth = results }
  if ( currentMonth < 10 ) currentMonth = parseInt(currentMonth)
  dayOptions()
}

function dayOptions() {
  var wantDay = currentDay
  var mySelect = document.hpc.day
  mySelect.options.length = 0
  if ( currentMonth == lastMonth ) {
    var mySindex = -99
    var startDay = 1
    var endDay = lastDay
  } else {
    var mySindex = -98
    var startDay = firstDay
    var endDay = daysInMonth[currentMonth]
  }
  var myIndex = 0
  for ( var i=startDay; i<=endDay; ++i) {
    currentDay = i
    myIndex = i - startDay
    var printValue = currentDay.toString()
    if ( currentDay < 10 ) { printValue = "0" + currentDay.toString() }
    mySelect.options[myIndex] = new Option(currentDay, printValue, false, false)
    if ( currentDay == wantDay ) { mySindex = myIndex }
  }
  currentDay = wantDay
  if ( mySindex == -99 ) {
    alert (wantDay+" not available.  Setting day to "+lastDay)
    currentDay = lastDay
    mySindex = lastDay - 1
  } else if ( mySindex == -98) {
    alert (wantDay+" not available.  Setting day to "+firstDay)
    currentDay = firstDay
    mySindex = 0
  }
  mySelect.selectedIndex = mySindex
  cycleOptions()
}

function setCurrentDay() {
  var mySelect = document.hpc.day
  currentDay = mySelect.options[mySelect.selectedIndex].value
  cycleOptions()
}

function cycleOptions() {
  var wantCycle = currentCycle
  var mySindex = -99
  var mySelect = document.hpc.hour
  mySelect.options.length = 0
  if ( currentMonth == lastMonth && currentDay == lastDay ) {
    var endCycle = lastCycle
  } else {
    var endCycle = 18
  }
  var myIndex = -1
  for ( var i=0; i<=endCycle; i=i+6) {
    ++myIndex
    currentCycle = i
    var printValue = i.toString()
    if ( i < 10 ) { printValue = "0" + i.toString() }
    mySelect.options[myIndex] = new Option(printValue, printValue, false, false)
    if ( currentCycle == wantCycle ) { mySindex = myIndex }
  }
  currentCycle = wantCycle
  if ( mySindex == -99) {
    alert (wantCycle+" not available for "+monthList[currentMonth]+" "+currentDay+", "+currentYear+" setting hour to "+endCycle)
    currentCycle = endCycle
    mySindex = myIndex
  }
  mySelect.selectedIndex = mySindex
  forOptions()
}

function setCurrentCycle() {
  var mySelect = document.hpc.hour
  currentCycle = mySelect.options[mySelect.selectedIndex].value
  forOptions()
}

function forOptions() {
  var wantFor = currentFor
  var mySindex = -99
  var mySelect = document.hpc.dur
  mySelect.options.length = 0
  if ( currentCycle == 00 || currentCycle == 12 ) {
  var endFor = 72
  } else {
  var endFor = 54
  }
  var myIndex = -1
  for ( var i=6; i<= endFor; i=i+6) {
    ++myIndex
    var printValue = i.toString()
    if ( i < 10 ) { printValue = "0"+i.toString() }
    var printText = "f0"+printValue
    mySelect.options[myIndex] = new Option(printText, printValue, false, false)
    if ( printValue == wantFor ) { mySindex = myIndex }
  }
  currentFor = wantFor
  if ( mySindex == -99) {
    alert(wantFor+" not available for "+currentCycle+" setting forecast hour to f006")
    currentFor = 06
    mySindex = 0
  }
  mySelect.selectedIndex = mySindex
}

function setCurrentFor() {
  var mySelect = document.hpc.dur
  currentFor = mySelect.options[mySelect.selectedIndex].value
}

function checkSelect() {
//  alert ("currentRFC: "+currentRFC+"\ncurrentYear: "+currentYear+"\ncurrentMonth: "+currentMonth+"\ncurrentIntType: "+currentIntType+"\ncurrentStatType: "+currentStatType+"\ncurrentFor: "+currentFor+"\ncurrentDisp: "+currentDisp)

  myRFC = document.rfc.rfc.options[document.rfc.rfc.selectedIndex].value
  myYEAR = document.rfc.year.options[document.rfc.year.selectedIndex].value
  myMONTH = document.rfc.month.options[document.rfc.month.selectedIndex].value
  myTYPE = document.rfc.type.options[document.rfc.type.selectedIndex].value
  myINTER = document.rfc.inter.options[document.rfc.inter.selectedIndex].value
  mySTAT = document.rfc.stat.options[document.rfc.stat.selectedIndex].value
  myPERIOD = document.rfc.period.options[document.rfc.period.selectedIndex].value
  myOPTION = document.rfc.option.options[document.rfc.option.selectedIndex].value
  myOPTTYPE = document.rfc.opttype.options[document.rfc.opttype.selectedIndex].value
  alert ("myRFC: "+myRFC+"\nmyYEAR: "+myYEAR+"\nmyMONTH: "+myMONTH+"\nmyTYPE: "+myTYPE+"\nmyINTER: "+myINTER+"\nmySTAT: "+mySTAT+"\nmyPERIOD: "+myPERIOD+"\nmyOPTION: "+myOPTION+"\nmyOPTTYPE: "+myOPTTYPE)
}
