//alert ("Activated request.js")

//This js file inlcudes the scripts which will create and change on the fly the
// request data form.

var isNav = (navigator.appName == "Netscape")
var isIE = (navigator.appName == "Microsoft Internet Explorer")

//Date Information
var today = new myDate()

// "HASH" to convert grid number to words
var gridName = new Array()
gridName[0] = gridName["240ss"] = "Original Grid (4km)"
gridName[1] = gridName["218ss"] = "Original Grid (10km)"
gridName[2] = gridName["003"] = "Original Grid (1x1 degree)"
gridName[3] = gridName["221"] = "Original Grid (32km)"
gridName[4] = gridName["104"] = "Original Grid (90km)"
gridName[5] = gridName["5km"] = "Original Grid (5km)"
gridName[6] = gridName["221us"] = "Verification Grid (32km)"
gridName[7] = gridName["240cr"] = "Central Verification Grid (4km)"
gridName[8] = gridName["240er"] = "Eastern Verification Grid (4km)"
gridName[9] = gridName["240sr"] = "Southern Verification Grid (4km)"
gridName[10] = gridName["240wr"] = "Western Verification Grid (4km)"
gridName[11] = gridName["sju32"] = "Puerto Rico Verification Grid (32km)"
gridName[12] = gridName["sju4"] = "Puerto Rico Verification Grid (4km)"

// Current Absolute Start and End Date (end date always 5 days before today)
var sDate = new myDate()
//var endDate = new myDate()
var endDate = new myDate("sub", 6)
var firstTime = "true"
var changeboth = "false"

//GLOBAL VARIABLES
 //objects
var currentType = typeList[0]
var currentSource = currentType.sources[0]
var currentFormat = currentSource.formats[0]
var currentDate = new Array()
  currentDate["start"] = new myDate(currentFormat.startDate.year, currentFormat.startDate.month, currentFormat.startDate.day)
  currentDate["end"] = new myDate(endDate.year, endDate.month, endDate.day)
 //strings
if ( currentFormat.cycleTimes == undefined ) {
  var currentCT = ""
} else {
  var currentCT = "All"
}
if ( currentFormat.forecastHours == undefined ) {
  var currentSFH = ""
  var currentEFH = ""
} else {
  var currentSFH = "All"
  var currentEFH = "All"
}
if ( currentFormat.forecastDuration == undefined ) {
  var currentFD = ""
} else {
    var currentFD = "All"
}
var textWords = "Please use this space to describe the \nrequest and how you would like to \nreceive the data."

setStartDate()

function setStartDate() {
//alert ("Entering setStartDate()")
  sDate = new myDate (currentFormat.startDate.year, currentFormat.startDate.month, currentFormat.startDate.day)
    currentDate["start"].year = sDate.year
    currentDate["start"].day = sDate.day
    currentDate["start"].month = sDate.month
  if (firstTime == "false") { 
    //if (changeboth == "true" ) {
    //  dateOptions("end", "none")
    //  changeboth = "false"
 //   }
    dateOptions("start", "none")
    dateOptions("end", "none")
  }
  firstTime = "false"
}


//SET FUNCTIONS
function setType() {
//alert("Entering setType()")
  mySelect = document.req.type
  for (i=0;i<typeList.length;++i) {
    if ( typeList[i].name == mySelect.options[mySelect.selectedIndex].value) {
      currentType = typeList[i]
    }
  }
  sourceOptions()
}

function setSource() {
//alert("Entering setSource()")
  mySelect = document.req.source
  for (i=0;i<currentType.sources.length;++i) {
    if (currentType.sources[i].name == mySelect.options[mySelect.selectedIndex].value) {
      currentSource = currentType.sources[i]
    }
  }
  formatOptions()
}

function setFormat() {
//alert("Entering setFormat()")
  mySelect = document.req.format
  for (i=0;i<currentSource.formats.length;++i) {
    if (currentSource.formats[i].name == mySelect.options[mySelect.selectedIndex].value) {
      currentFormat = currentSource.formats[i]
    }
  }
  setStartDate()
  var need = "yes"
  if ( currentFormat.cycleTimes == undefined ) { need = "no" }
  cycleOptions(need)
  need = "yes"
  if ( currentFormat.forecastHours == undefined ) { need = "no" }
  forecastOptions(need)
  need = "yes"
  if ( currentFormat.forecastDuration == undefined ) { need = "no" }
  durationOptions(need)
  need = "yes"
  if ( currentFormat.gridSize == undefined ) { need = "no" }
  gridOptions(need)
}

function setDate(which,skip) {
//alert("Entering setDate("+which+")")
  myMSelect = new Array()
  myMSelect["start"] = document.req.smonth
  myMSelect["end"] = document.req.emonth
  myDSelect = new Array()
  myDSelect["start"] = document.req.sday
  myDSelect["end"] = document.req.eday
  myYSelect = new Array()
  myYSelect["start"] = document.req.syear
  myYSelect["end"] = document.req.eyear
  currentDate[which].month = parseInt(myMSelect[which].options[myMSelect[which].selectedIndex].value)
  if ( currentFormat.name != "Gempak Grid" ) { currentDate[which].day = parseInt(myDSelect[which].options[myDSelect[which].selectedIndex].value) }
  currentDate[which].year = parseInt(myYSelect[which].options[myYSelect[which].selectedIndex].value)
//alert("Setting current "+which+" date to "+currentDate[which].print())
  currentDate[which].newJSdate()
  dateOptions(which,skip)
}

function setCycle() {
//alert("Entering setCycle()")
  var mySelect = document.req.cycle
  currentCT = mySelect.options[mySelect.selectedIndex].value
}

function setSFH() {
//alert("Entering setSFH()")
  var mySelect = document.req.sforecast
  currentSFH = mySelect.options[mySelect.selectedIndex].value
}

function setEFH() {
//alert("Entering setEFH()")
  var mySelect = document.req.eforecast
  currentEFH = mySelect.options[mySelect.selectedIndex].value
}

function setFD() {
//alert("Entering setFD()")
  var mySelect = document.req.duration
  currentFD = mySelect.options[mySelect.selectedIndex].value
}

//function setGS() {
//alert("Entering setGS()")
//  var mySelect = document.req.gsize
//  currentGS = mySelect.options[mySelect.selectedIndex].value
//  if ( currentGS == "Both" ) {
//    currentGSname = "B"
//  } else {
//    currentGSname = gridName[currentGS].charAt(0)
//  }
//}

//OPTIONS FUNCTIONS
function sourceOptions() {
//alert ("Entering sourceOptions()")
  var wantSource = currentSource
  var mySindex = 99
  mySelect = document.req.source
  mySelect.options.length = 0
  for (i=0;i<currentType.sources.length;++i) {
    mySelect.options[i] = new Option(currentType.sources[i].name, currentType.sources[i].name, false, false)
    if ( wantSource.name == currentType.sources[i].name) { mySindex = i }
  }
  if (mySindex == 99 ) {
    alert (wantSource.name+" is not an available Data Source.  Setting Data Source to "+currentType.sources[0].name)
    mySindex=0
  }
  mySelect.selectedIndex = mySindex
  currentSource = currentType.sources[mySindex]
  formatOptions()
}

function formatOptions() {
//alert ("Entering formatOptions()") 
  var wantFormat = currentFormat
  var mySindex = 99
  mySelect = document.req.format
  mySelect.options.length = 0
  for(i=0;i<currentSource.formats.length;++i) {
    mySelect.options[i] = new Option(currentSource.formats[i].name, currentSource.formats[i].name, false, false)
    if ( wantFormat.name == currentSource.formats[i].name ) { mySindex = i }
  }
  if (mySindex == 99) {
    alert (wantFormat.name+" is not an availabel Data Format.  Setting Data Format to "+currentSource.formats[0].name)
    mySindex = 0
  }
  mySelect.selectedIndex = mySindex
  currentFormat = currentSource.formats[mySindex]
  setStartDate()
  var need = "yes"
  if ( currentFormat.cycleTimes == undefined ) { need = "no" }
  cycleOptions(need)
  need = "yes"
  if ( currentFormat.forecastHours == undefined ) { need = "no" }
  forecastOptions(need)
  need = "yes"
  if ( currentFormat.forecastDuration == undefined ) { need = "no" }
  durationOptions(need)
  need = "yes"
  if ( currentFormat.gridSize == undefined ) { need = "no" }
  gridOptions(need)
}

function dateOptions(which,skip) {
//alert("Entering dateOptions("+which+", "+skip+")")
  var myMSelect = new Array()
  myMSelect["start"] = document.req.smonth
  myMSelect["end"] = document.req.emonth
  myDSelect = new Array()
  myDSelect["start"] = document.req.sday
  myDSelect["end"] = document.req.eday
  myYSelect = new Array()
  myYSelect["start"] = document.req.syear
  myYSelect["end"] = document.req.eyear
  var wantDate = new myDate (currentDate[which].year, currentDate[which].month, currentDate[which].day)
  if ( skip == "none" && which == "start") { 
    wantDate = new myDate (sDate.year, sDate.month, sDate.day)
  }
  var mySindex = new Array (99,99,99)
  var warn = "false"
  if ( skip != "month") {
//alert ("Entering Month Section")
    myMSelect[which].options.length = 0
    if ( currentDate[which].year == sDate.year ) {
      var ibegin = sDate.month
      var iend = 12 
    } else if ( currentDate[which].year == endDate.year ) {
      var ibegin = 1
      var iend = endDate.month
    } else {
      var ibegin = 1
      var iend = 12
    }
    var myI = 0
    for (i=ibegin;i<=iend;++i) {
      myMSelect[which].options[myI] = new Option (monthList[i], i, false, false)
      if (i == wantDate.month ) { mySindex[0] = myI }
      ++myI
    }
    if (mySindex[0] == 99 ) { mySindex[0] = 0; warn = "true"}
    myMSelect[which].selectedIndex = mySindex[0]
    currentDate[which].month = parseInt(myMSelect[which].options[myMSelect[which].selectedIndex].value)
  }
  
  if ( skip != "day" ) {
  // Day Section
    if ( sDate.day != 0 ) {
      myDSelect[which].options.length = 0
      if ( currentDate[which].year == endDate.year && currentDate[which].month == endDate.month ) {
        var ibegin = 1
        var iend = endDate.day
      } else if ( currentDate[which].year == sDate.year && currentDate[which].month == sDate.month ) {
        var ibegin = sDate.day
        var iend = currentDate[which].daysInMonth()
      } else {
        var ibegin = 1
        var iend = currentDate[which].daysInMonth()
      }
      var myIndex = 0
      for (i=ibegin;i<=iend;++i) {
        myDSelect[which].options[myIndex] = new Option (i, i, false, false)
        if ( i == wantDate.day ) { mySindex[1] = myIndex }
        ++myIndex
      }
      if (mySindex[1] == 99 ) { mySindex[1] = 0; warn = "true"}
      if (mySindex[1] == 99 ) { mySindex[1] = 0; warn = "true"}
      myDSelect[which].selectedIndex = mySindex[1]
    } else {
      myDSelect["start"].options.length=0
      myDSelect["end"].options.length=0
      changeboth = "true"
    }
    if (mySindex[1] != 99) {currentDate[which].day = parseInt(myDSelect[which].options[myDSelect[which].selectedIndex].value)}
  }
  if ( skip != "year") {
  // Year Section
    myYSelect[which].options.length = 0
    var myI = 0
    for (i=sDate.year;i<=endDate.year;++i) {
      myYSelect[which].options[myI] = new Option (i, i, false, false)
      if ( i == wantDate.year ) { mySindex[2] = myI }
      ++myI
    }
    if (mySindex[2] == 99 ) { mySindex[2] = 0; warn = "true"}
    myYSelect[which].selectedIndex = mySindex[2]
    currentDate[which].year = parseInt(myYSelect[which].options[myYSelect[which].selectedIndex].value)
  }
  if ( warn == "true") { 
    alert (which+" date "+wantDate.print()+" is not available setting date to "+currentDate[which].print())
  }
//alert ("Setting date to "+currentDate[which].print())
  currentDate[which].newJSdate()
}

function cycleOptions(need) {
//alert("Entering cycleOptions("+need+")")
  var mySelect = document.req.cycle
  mySelect.options.length = 0
  if ( need == "yes") {
    var wantCycle = currentCT
    var mySindex = 99
    mySelect.options[0] = new Option ("All", "All", false, false)
    if ( wantCycle == "All" ) { mySindex = 0 }
    for (i=1;i<=currentFormat.cycleTimes.length;++i) {
      mySelect.options[i] = new Option (currentFormat.cycleTimes[i-1], currentFormat.cycleTimes[i-1], false, false)
      if ( wantCycle == currentFormat.cycleTimes[i-1] ) { mySindex = i }
    }
    if ( mySindex == 99 ) {
      mySindex = 0
      if ( wantCycle != "" ) { alert(wantCycle+" not available.  Setting cycle to 'All'") }
    }
    mySelect.selectedIndex = mySindex
    currentCT = mySelect.options[mySindex].value
  }
}

function forecastOptions(need) {
//alert ("forecastOptions("+need+")")
// for Start forecast hour
  var mySelect = document.req.sforecast
  mySelect.options.length = 0
  if ( need == "yes" ) {
    var wantFore = currentSFH
    var mySindex = 99
    mySelect.options[0] = new Option ("All", "All", false, false)
    if ( wantFore == "All" ) { mySindex = 0 }
    for ( i=1;i<=currentFormat.forecastHours.length;++i) {
      mySelect.options[i] = new Option (currentFormat.forecastHours[i-1], currentFormat.forecastHours[i-1], false, false)
      if ( wantFore == currentFormat.forecastHours[i-1] ) { mySindex = i }
    }
    if ( mySindex == 99 ) {
      mySindex = 0
      if ( wantFore != "" ) { alert(wantFore+" not available.  Setting starting forecast hour to 'All'") }
    }
    mySelect.selectedIndex = mySindex
    currentSFH = mySelect.options[mySindex].value
  }
// for End forecast hour
  var mySelect = document.req.eforecast
  mySelect.options.length = 0
  if ( need == "yes" ) {
    var wantFore = currentEFH
    var mySindex = 99
    mySelect.options[0] = new Option ("All", "All", false, false)
    if ( wantFore == "All" ) { mySindex = 0 }
    for ( i=1;i<currentFormat.forecastHours.length;++i) {
      mySelect.options[i] = new Option (currentFormat.forecastHours[i], currentFormat.forecastHours[i], false, false)
      if ( wantFore == currentFormat.forecastHours[i] ) { mySindex = i }
    }
    if ( mySindex == 99 ) {
      mySindex = 0
      if ( wantFore != "" ) { alert(wantFore+" not available.  Setting ending forecast hour to 'All'") }
    }
    mySelect.selectedIndex = mySindex
    currentEFH = mySelect.options[mySindex].value
  }
}

function durationOptions(need) {
//alert("durationOptions("+need+")")
  mySelect = document.req.duration
  mySelect.options.length = 0
  if ( need == "yes" ) {
    var wantDur = currentFD
    var mySindex = 99
    mySelect.options[0] = new Option ("All", "All", false, false)
    if ( wantDur == "All" ) { mySindex = 0 }
    for (i=1;i<=currentFormat.forecastDuration.length;++i) {
      mySelect.options[i] = new Option (currentFormat.forecastDuration[i-1], currentFormat.forecastDuration[i-1], false, false)
      if ( wantDur == currentFormat.forecastDuration[i-1] ) { mySindex = i }
    }
    if ( mySindex == 99 ) {
      mySindex = 0 
      if ( wantDur != "" ) { alert(wantDur+" no availble.  Setting forecast duration to 'All'") }
    }
    mySelect.selectedIndex = mySindex
    currentFD = mySelect.options[mySindex].value
  }
}

function gridOptions(need) {
//alert ("Entering gridOptions("+need+")")
  mySelect = document.req.gsize
  mySelect.options.length = 0
  if ( need == "yes" ) {
    mySelect.options[0] = new Option ("Please Select...", "", true, true)
    mySelect.options[1] = new Option ("All", "All", false, false)
    for (i=0;i<currentFormat.gridSize.length;i++) {
      mygrid = currentFormat.gridSize[i]
      if ( mygrid == "221us" && currentSource.name == "HPC" ) {
        mygridname = "Original/Verification Grid (32km)"
      } else if (mygrid == "240ss" && currentSource.name == "Mosaic of RFCs") {
        mygridname = "Original Grid (10km)"
      } else if (mygrid == "240ss" && currentType.name == "QPE" ) {
        mygridname = "Original/Verification Grid (4km)"
      } else { 
        mygridname = gridName[mygrid]
      }
      mySelect.options[i+2] = new Option (mygridname, mygrid, false, false)
    }
  }
  mySelect.selectedIndex = 0
}

//SELECT TEXT BOX
function txtBox() {
  myArea = document.req.comment
  if ( myArea.value == "" ) { 
    myArea.value = textWords 
    return
  }
  if ( myArea.value.substring(0,20) == textWords.substring(0,20) ) { 
    myArea.value = "" 
    return
  }
}

// Submit Check
function checkForm() {
//alert ("checkForm()")
  var myForm = document.req
  if ( myForm.name.value == "" ) {
    alert ("Please Enter your Name")
    myForm.name.focus()
    return false
  }
  if (myForm.email.value.indexOf("@") == -1 ) {
    alert ("Please Enter your e-mail address")
    myForm.email.focus()
    return false
  }
  if (currentDate['start'].asJSdate.getTime() > currentDate['end'].asJSdate.getTime() ) {
    alert ("Starting Date is after ending date.")
    return false
  } 
  if ( currentEFH != "All" ) {
    if (currentSFH.substring(1,4) > currentEFH.substring(1,4)){
      alert("Starting forecast hour is after ending forecast hour.")
      return false
    }
  }
  if ( myForm.format.value == "Gempak Grid" && myForm.gsize.options[myForm.gsize.selectedIndex].value == "" ) {
    alert("Please select a grid size.")
    return false
  }
  if ( myForm.comment.value.substring(0,20) == textWords.substring(0,20) ) {
    alert ("Please fill in the text box.")
    return false
  }
  if (!confirm("Please verify that "+myForm.email.value+" is correct.")) {
    myForm.email.focus()
    return false
  }
  addZeros()
  window.open("","thanks","toolbar=no,location=no,directoryies=no,status=no,menubar=no,scrollbars=no,resizable=no,width=600,height=400,alwaysRaised=yes")
  document.req.target="thanks"
  return true
}

function addZeros() {
  var mySelect = document.req.smonth
  if ( parseInt(mySelect.options[mySelect.selectedIndex].value) < 10 && mySelect.options[mySelect.selectedIndex].value.charAt(0) != "0") {
    var newValue = "0"+mySelect.options[mySelect.selectedIndex].value
    mySelect.options[mySelect.selectedIndex].value = newValue
  }
  var mySelect = document.req.emonth
  if ( parseInt(mySelect.options[mySelect.selectedIndex].value) < 10 && mySelect.options[mySelect.selectedIndex].value.charAt(0) != "0") {
    var newValue = "0"+mySelect.options[mySelect.selectedIndex].value
    mySelect.options[mySelect.selectedIndex].value = newValue
  }
  if ( currentFormat.name != "Gempak Grid" ) {
    var mySelect = document.req.sday
    if ( parseInt(mySelect.options[mySelect.selectedIndex].value) < 10 && mySelect.options[mySelect.selectedIndex].value.charAt(0) != "0") {
      var newValue = "0"+mySelect.options[mySelect.selectedIndex].value
      mySelect.options[mySelect.selectedIndex].value = newValue
    }
    var mySelect = document.req.eday
    if ( parseInt(mySelect.options[mySelect.selectedIndex].value) < 10 && mySelect.options[mySelect.selectedIndex].value.charAt(0) != "0") {
      var newValue = "0"+mySelect.options[mySelect.selectedIndex].value
      mySelect.options[mySelect.selectedIndex].value = newValue
    }
  }
}

function resetpage() {
// This is used especially for Netscape/Mozilla where refresh does not change selections but does reset the javascript variables.
  document.req.type.selectedIndex = 0
  document.req.source.selectedIndex = 0
  document.req.source.selectedIndex = 0
  setStartDate()
}
