//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")

//List of Variables for each RFC
var rfcList = new Array("ABRFC", "CBRFC", "CNRFC", "LMRFC", "MARFC", "MBRFC", 
                        "NCRFC", "NERFC", "NWRFC", "OHRFC", "SERFC", "WGRFC",
                        "CONUS")
var rfcRegionList = new Array ("east", "west", "west", "east", "east", "east",
                               "east", "east", "west", "east", "east", "east",
                               "all")                            

var currentRFC = "ABRFC"
var currentRFCindex = 0
var currentYear = lastYear
var currentMonth = lastMonth
var currentDay = 1
var currentFor = "006"
var currentDur = "06"
var textShow = new Boolean(true)

isLeapYear()

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

function setCurrentRFC() {
  var myRSelect = document.cp.rfc
  currentRFC = myRSelect.options[myRSelect.selectedIndex].value
  for (var i=0; i<12; ++i) {
    if (currentRFC == rfcList[i]) {
      currentRFCindex = i
      forOptions()
    }
  }
}

function setCurrentYear() {
  var myYSelect = document.cp.year
  currentYear = myYSelect.options[myYSelect.selectedIndex].value
  isLeapYear()
  monthOptions()
  durOptions()
}

function setCurrentMonth() {
  var myMSelect = document.cp.month
  currentMonth = myMSelect.options[myMSelect.selectedIndex].value
  dayOptions()
}

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

function setCurrentFor() {
  //alert ("Entering setCurrentFor()")
  var mySelect = document.cp.fhr
  currentFor = mySelect.options[mySelect.selectedIndex].value
  durOptions()
}

function setCurrentDur() {
  var mySelect = document.cp.dur
  currentDur = mySelect.options[mySelect.selectedIndex].value
}


function monthOptions () {
  var wantMonth = currentMonth
  var mySindex = -99
  mySelect = document.cp.month
  mySelect.options.length = 0
  if ( currentYear == lastYear) {
    for (var i=01; i<=lastMonth; ++i) {
      mySelect.options[(i-1)] = new Option(monthList[i], i, false, false)
      if ( i == wantMonth ) { mySindex = (i-1) }
      if ( i == lastMonth ) { myLindex = (i-1) }
    }
  } else {
    for (var i=lastMonth; i<=12; ++i) {
      mySelect.options[(i-lastMonth)] = new Option(monthList[i], i, false, false)
      if ( i == wantMonth ) { mySindex = (i-lastMonth) }
      if ( i == lastMonth ) { myLindex = (i-lastMonth) }
    }
  } 
  currentMonth = wantMonth
  if ( mySindex == -99 ) {
    alert ( monthList[wantMonth]+" not available for "+currentYear+" setting month to "+monthList[lastMonth])
    mySindex = myLindex
    currentMonth = lastMonth
  }
  mySelect.selectedIndex = mySindex
}

function dayOptions() {
  var wantDay = currentDay
  var mySindex = -99
  mySelect = document.cp.day
  mySelect.options.length = 0
  for (var i=1; i<=daysInMonth[currentMonth]; ++i) {
    mySelect.options[(i-1)] = new Option(i, i, false, false)
    if ( i == wantDay ) { mySindex = (i-1) }
  }
  currentDay = wantDay
  if ( mySindex == -99 ) {
    alert ( wantDay+" is not available for "+monthList[currentMonth]+" setting day to 1")
    currentDay = 1
    mySindex = 0
  }
  mySelect.selectedIndex = mySindex
}

function forOptions() {
  var wantFor = currentFor
  var mySindex = -99
  var rfcRegion = rfcRegionList[currentRFCindex] 
  mySelect = document.cp.fhr
  var endFor = 72
  if (rfcRegion == "east") { endFor = 24 }
  mySelect.options.length = 0 
  var myIndex = -1
  for ( var i=6; i<= endFor; i=i+6 ) {
    ++myIndex
    var printValue = i.toString()
    if ( i < 10 ) { printValue = "0"+printValue }
    if ( i < 100 ) { printValue = "0"+printValue }
    var printText = "f"+printValue
    mySelect.options[myIndex] = new Option(printText, printValue, false, false)
    if ( printValue == wantFor ) { mySindex = myIndex }
  }
  currentFor = wantFor
  if ( mySindex == -99 ) {
    alert ( "f"+wantFor+" is not available for "+currentRFC+" setting forecast to f006")
    currentFor = 006
    mySindex = 0
    durOptions()
  }
  mySelect.selectedIndex = mySindex
}

function durOptions() {
  //alert ("Entering durOptions()")
  //alert ("curerntFor: "+currentFor)
  var wantDur = currentDur
  var mySindex = -99
  mySelect = document.cp.dur
  mySelect.options.length = 0
  mySelect.options[0] = new Option("06-hours", "06", false, false)
  if ( wantDur == 06 ) { mySindex = 0 }
  if ( (currentFor == "024" || currentFor == "048" || currentFor == "072") && currentYear > 2003 ) {
    //alert("Adding the 24-hour option")
    mySelect.options[1] = new Option("24-hours", 24, false, false)
    if ( wantDur == 24 ) { mySindex = 1 }
    textShow = false
  } else {
    textShow = true
  }
  currentDur = wantDur
  if ( mySindex == -99 ) {
    alert ("24-hours is only available at forecast hours f024, f048 or f072 from Jan 2004 to present.\nSetting Durration to 06-hours")
    currentDur = 06
    mySindex = 0
  }
  mySelect.selectedIndex = mySindex
}   
  
function warnText() {
  //alert ("Entering warnText()")
  //alert ("currentFor: "+currentFor)
  //alert ("textShow: "+textShow)
  if ( (currentFor != "024" && currentFor != "048" && currentFor != "072") || currentYear < 2003) {
    if ( textShow ) {
      alert ("If you want to view the 24-hour plots you will need to first select a forecast hour of f024, f048 or f072 for January 2004 to present.")
      textShow=false
    }
  }
} 


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

  myRFC = document.cp.rfc.options[document.cp.rfc.selectedIndex].value
  myYEAR = document.cp.year.options[document.cp.year.selectedIndex].value
  myMONTH = document.cp.month.options[document.cp.month.selectedIndex].value
  myDAY = document.cp.type.options[document.cp.type.selectedIndex].value
  myHOUR = document.cp.inter.options[document.cp.inter.selectedIndex].value
  myFHOUR = document.cp.stat.options[document.cp.stat.selectedIndex].value
  myDURAT = document.cp.period.options[document.cp.period.selectedIndex].value
  myTYPE = document.cp.option.options[document.cp.option.selectedIndex].value

  alert ("myRFC: "+myRFC+"\nmyYEAR: "+myYEAR+"\nmyMONTH: "+myMONTH+"\nmyDAY: "+myDAY+"\nmyHOUR: "+myHOUR+"\nmyFHOUR: "+myFHOUR+"\nmyDURAT: "+myDURAT+"\nmyTYPE: "+myTYPE)
}
