
var valid = true;

var daymonth=[31,28,31,30,31,30,31,31,30,31,30,31];

var lastValidSatellite;

function leapYear(yr) {

  /* returns true if the current year is a leap year */

  if ((yr % 100)==0) {
    if ((yr % 400)==0) return true;
  } else if ((yr % 4)==0) return true;
  return false;
}

function setDay(yr,moIndex,dy) {
 
 /* updates the day selector in response to a change in month or year */

 var dyinmo=daymonth[moIndex];
 if ((moIndex==1) && (leapYear(yr)==true)) dyinmo++;

 if (dy>dyinmo) dy=dyinmo;

 document.plotForm.startDate_day.options.length=dyinmo;
 for (c=0;c<dyinmo;c++) {
   var num = c+1;
   var numString = num.toString();
   if( numString.length == 1 ) {
     numString = "0" + numString;
   }
   document.plotForm.startDate_day.options[c].text=numString;
   document.plotForm.startDate_day.options[c].value=c+1;
 }
 document.plotForm.startDate_day.selectedIndex = dy-1;

}


/*
function setSatelliteList(year, month, day, sat) {


    // If the users switches to a date with no data, the satellite
    // menu is cleared.
    // However, the last selected satellite is remembered
    // so that when they return to a date with data, the same
    // satellite will be selected.

    if( sat != "No Data" ) {
      lastValidSatellite = sat;
    }


	if( (satellites[year] == undefined) ||
	    (satellites[year][month] == undefined) ||
	    (satellites[year][month][day] == undefined) )
	{
      document.plotForm.satelliteName.length=0;
      document.plotForm.satelliteName.length=1;
      document.plotForm.satelliteName.options[0].text  = "No Data";
      document.plotForm.satelliteName.options[0].value = "No Data";
      document.plotForm.satelliteName.options[0].selected=true;
      valid=false;

	} else {
      var listObj = satellites[year][month][day];

      var indexToUse = -1;

      if( sat == "No Data" ) {
       if( lastValidSatellite == undefined ) {
          indexToUse = 0;
          sat = "";
        } else {
          sat = lastValidSatellite;
        }
      }

      document.plotForm.satelliteName.length=20;
      var numSats = 0;
      for( var s in listObj ) {
        document.plotForm.satelliteName.options[numSats].text  = s;
        document.plotForm.satelliteName.options[numSats].value = s;
        if( (numSats == indexToUse) || (s == sat) ) {
	        document.plotForm.satelliteName.options[numSats].selected = true;
        }
        numSats++;
      }
      document.plotForm.satelliteName.length=numSats;
      valid=true;
    }
}
*/

function changeDate() {

  var ind           = document.plotForm.startDate_year.selectedIndex
  var newYear       = document.plotForm.startDate_year.options[ind].value;

  var newMonthIndex = document.plotForm.startDate_month.selectedIndex;

  ind               = document.plotForm.startDate_day.selectedIndex;
  var newDay        = document.plotForm.startDate_day.options[ind].value;

  setDay(newYear,newMonthIndex,newDay);


  /*
  // The previous function can change the day (if we switched
  // to a shorter month), and so we need to get the
  // current day again from the form.
  ind    = document.plotForm.startDate_day.selectedIndex;
  newDay = document.plotForm.startDate_day.options[ind].value;

  var sat = document.plotForm.satelliteName.value;

  setSatelliteList(newYear, newMonthIndex, newDay, sat);
  */
}


function validate() {

  if (valid==false) {
    alert("Nothing to plot.");
    return false;
  }

  var extent=document.plotForm.plotLength_hour.value*3600+document.plotForm.plotLength_minute.value*60+document.plotForm.plotLength_second.value;

  if (extent==0) {
     alert("The length of the plot must be greater than zero");
     return false;
  }

  return true;
}
