/*
  *************************************************************************
  ***                                                                   ***
  ***   IF THIS TEXT SHOWS UP ON YOUR BROWSER, YOU DO CURRENTLY NOT     ***
  ***   HAVE JAVASCRIPT CAPABILITY, PLEASE USE TABULAR INTERFACE        ***
  ***   AT http://polar.ncep.noaa.gov/waves/historic.html               ***
  ***                                                                   ***
  *************************************************************************

   nww3_hist.js JavaScript program for interactive historical data
                web page. Functions used :

                   displayHist()  Function to display actual page
                   evalHist()     Evaluate form data
                   delayHist()    Display page with a delay

                Hendrik Tolman, July 2000, Feb. 2002, Oct. 2002,
                                Sept. 2003                              */

function displayHist(iModel, month, year, iPar) {

//
// Displays entire web page with updated graphics
//

   var modelName  = new Array(6) ;
   var modelPar   = new Array(1) ;
   var modelMonth = new Array(1) ;
   var year_0 = 1997 ;
   var year_n = 2009 ;
   var month_n = 07 ;
   var hostname, i, selected, month_i, year_i, animation ;

   hostname = window.location.host ;

//
// Display head of page including title, logo and start of form
//

   with (document) {
     writeln('<HTML>') ;
     writeln('<HEAD>') ;
     writeln('<TITLE>NOAA WAVEWATCH III historic data (interactive)</TITLE>') ;
     writeln('</HEAD>') ;
     writeln('<SCRIPT LANGUAGE="JavaScript" SRC="nww3_hist.js"></SCRIPT>') ;
     writeln('<BODY background="/mmab/images/dirbkgnd.gif">');
     writeln('<table cellspacing="0" cellpadding="0" width="670" border="0" align="center">');
     writeln('<tr align="center" valign="middle"><td colspan="3" height="20"><span class="nwslink">');
     writeln('<a href="http://www.noaa.gov/privacy.html">NOAA Privacy Policy</a> | '
       + '<a href="http://www.nws.noaa.gov/disclaimer.html">NWS Disclaimer</a></span></td></tr>');
     writeln(' ');
     writeln('<tr VALIGN="middle">');
     writeln('<td ALIGN=LEFT WIDTH="85" HEIGHT="80"><a href="http://www.noaa.gov/" target="_blank">'
       + '<img SRC="/mmab/images/waves/noaaleft.jpg" BORDER=0 height=74 width=84 alt="Link to National Oceanic and Atmospheric Agency"></a></td>');
     writeln('<td ALIGN="center" WIDTH="500">'
       + '<img SRC="/mmab/images/waves/wwhistorical.jpg" BORDER=2 height=70 width=500></td>');
     writeln('<td WIDTH="85" align="center"><a href="http://www.nws.noaa.gov/" target="_blank">'
       + '<img SRC="/mmab/images/waves/nwsright_crop.jpg" ALT="National Weather Service logo" BORDER=0 height=74 width=84 alt="Link to National Weather Service"></a>');
     writeln('</td>');
     writeln('</tr>');
     writeln('</table>');
     writeln('<HR>') ;
     writeln('<FORM NAME="selector">') ;
     writeln('<CENTER>') ;
     } ;

//
// Select box for model selection
//

   modelName[0] = "Infopage" ;
   modelName[1] = "Global model" ;
   modelName[2] = "Alaskan Waters regional model" ;
   modelName[3] = "Western North Atlantic model" ;
   modelName[4] = "North Atlantic Hurricane model" ;
   modelName[5] = "Eastern North Pacific model" ;
   modelName[6] = "North Pacific Hurricane model" ;

   if ( iModel < 0 && iModel > 6 ) { iModel = 0 } ;

   document.writeln('<SELECT SIZE=1 NAME="model">') ;
   for ( i=0 ; i<=6; i++ ) {
      if ( i == iModel ) { selected = ' SELECTED> ' }
                    else { selected = '> ' } ;
      document.writeln('<OPTION' + selected + modelName[i]) ;
      } ;
   document.writeln('</SELECT>') ;

//
// Select box for month
//

   modelMonth[0] = 'Jan.' ;
   modelMonth[1] = 'Feb.' ;
   modelMonth[2] = 'Mar.' ;
   modelMonth[3] = 'Apr.' ;
   modelMonth[4] = 'May ' ;
   modelMonth[5] = 'June' ;
   modelMonth[6] = 'July' ;
   modelMonth[7] = 'Aug.' ;
   modelMonth[8] = 'Sep.' ;
   modelMonth[9] = 'Oct.' ;
   modelMonth[10] = 'Nov.' ;
   modelMonth[11] = 'Dec.' ;

   if ( month < 1 || month > 12 ) { month = month_n } ;

   document.writeln('<SELECT SIZE=1 NAME="month">') ;
   for ( i=0 ; i<=11; i++ ) {
      month_i = i + 1 ;
      if ( month_i == month ) { selected = ' SELECTED> ' }
                         else { selected = '> ' } ;
      document.writeln('<OPTION' + selected + modelMonth[i]) ;
      } ;
   document.writeln('</SELECT>') ;

//
// Select box for year
//

   year_i=year_0 ;

   if ( year < year_0 || year > year_n ) { year = year_n } ;

   document.writeln('<SELECT SIZE=1 NAME="year">') ;
   while ( year_i <= year_n ) {
      if ( year == year_i ) { selected = ' SELECTED> ' }
                       else { selected = '> ' } ;
      document.writeln('<OPTION' + selected + year_i) ;
      year_i++ ;
      } ;
   document.writeln('</SELECT>') ;

//
// Select box for parameter
//

   modelPar[0] = "Hs" ;
   modelPar[1] = "Tp" ;

   if ( iPar < 0 && iPar > 1 ) { iPar = 0 } ;

   document.writeln('<SELECT SIZE=1 NAME="par">') ;
   for ( var i=0 ; i<=1; i++ ) {
      if ( i == iPar ) { selected = ' SELECTED> ' }
                  else { selected = '> ' } ;
      document.writeln('<OPTION' + selected + modelPar[i]) ;
      } ;

//
// Set up backward, go and foreward buttons 
//

   with (document) {
     writeln('</SELECT>'
     + '<INPUT TYPE="button" NAME="down" VALUE="<-" onClick="evalHist(-1)">'
     + '<INPUT TYPE="button" NAME="go"   VALUE="go" onClick="evalHist(0)">'
     + '<INPUT TYPE="button" NAME="up"   VALUE="->" onClick="evalHist(1)">') ;
     writeln('<BR>') ;
     } ;

//
// Display infopage if iModel = 0
//

   if ( iModel == 0 ) {
      with (document) {
         writeln('</CENTER> ') ;
         writeln('<JUSTIFY>')
    writeln('<table cellspacing="0" cellpadding="0" width="670" border="0" align="center">');
    writeln('<tr align="JUSTIFY"><td>') ;
    writeln('<BR><BR>This web page gives access to animations of continuous hindcasts with the most recent model version. For the global model data are available starting late January 1997, for the regional models starting July 1999. These hindcasts have been run after the fact with archived wind and ice fields. Presented are wave heights (Hs) and peak periods (Tp), regenerated with WAVEWATCH III version 2.22. The animations start at the cold start of the model, and thus the first month(s) show the model spin-up behavior. Particularly, the period plot for the global model for Feb. 1997, shows that the model needs approximately 2 weeks to spin up its Pacific swell properly. The corresonding binary data in GRIB format are available from our <A HREF="ftp://' + hostname + '/pub/history/waves">ftp server</A>.') ;
         writeln('<BR><BR><FONT COLOR=Red>WARNING</FONT> : The animation files are typically 4 to 6Mb large. It will take considerable time to download such a file, depending on the speed of your internet connection.') ;
         writeln("<BR><BR>To display the model, month and parameter as selected above, click the 'go' button above (select a model to replace the 'Infopage' choice above). If the data for the month and model selected are not available, the nearest month following the selected month will be displayed, rotating back to the first available month if the last month is reached. the '<-' and '->' buttons give the previous and next month, respectively.") ;
         writeln('<CENTER> ') ;
         } ;
      }

//
// Display graphix otherwise
//

   else {
      animation='/history/waves/' ;
      switch (iModel) {
         case 2 :
              animation += 'akw.' ;
              break ;
         case 3 :
              animation += 'wna.' ;
              break ;
         case 4 :
              animation += 'nah.' ;
              break ;
         case 5 :
              animation += 'enp.' ;
              break ;
         case 6 :
              animation += 'nph.' ;
              break ;
         default : 
              animation += 'nww3.' ;
         } ;
      switch (iPar) {
         case 1 :
              animation += 'tp.' ;
              break ;
         default : 
              animation += 'hs.' ;
         } ;
      animation += 'anim.' ;
      if ( month < 10 ) {
         animation += year + '0' + month ;
         }
      else {
         animation += year + month ;
         } ;
      animation += '.gif' ;
      document.writeln('<BR><IMG SRC="' + animation + '">') ;
      } ;

//
// Finish page with bottom links and end of form
//

   with (document) {
     writeln('<BR>') ;
     writeln('<P>Last update : August 04, 2009 | Last data : 2009/07</P>') ;
     writeln('<HR>') ;
     writeln('<A HREF="/Welcome.html">MMAB page</A> |') ;
     writeln('<A HREF="Welcome.html">Main wave model page</A> |') ;
     writeln('<A HREF="historic.html">Tabular interface</A>') ;
     writeln('<P ALIGN="RIGHT">Point of contact : ') ;
     writeln('<A HREF="mailto:NCEP.EMC.Waves@noaa.gov">'
      +      'waves@ncep.noaa.gov</A></P>') ;
     writeln('</CENTER>') ;
     writeln('</FORM>' ) ;
     writeln('</BODY>') ;
     writeln('</HTML>') ;
     } ;

   document.close() ;

   return true ;
   } ;

function evalHist(increment) {

   var firstYearMonth, lastYearMonth, yearMonth ;
   var modelIndex, monthIndex, month, yearIndex, year, parIndex ;
   var i, missingIt ;

//
// Extract form information
//

   modelIndex=document.selector.model.selectedIndex ;

   monthIndex=document.selector.month.selectedIndex ;
        month=1+monthIndex ;

    yearIndex=document.selector.year.selectedIndex ;
         year=document.selector.year.options[yearIndex].text ;

     parIndex=document.selector.par.selectedIndex ;

//
// Set year / month constants
//

   switch ( modelIndex ) {
      case 2  : firstYearMonth = 199907 ;
                lastYearMonth = 200711 ;
                var missing = new Array (  ) ;
                var numMissing = 0 ;
                break ;
      case 3  : firstYearMonth = 199907 ;
                lastYearMonth = 200711 ;
                var missing = new Array (  ) ;
                var numMissing = 0 ;
                break ;
      case 4  : firstYearMonth = 200008 ;
                lastYearMonth = 200907 ;
                var missing = new Array ( '200011' , '200012' , '200101' , '200102' , '200103' , '200104' , '200105' , '200107' , '200201' , '200202' , '200203' , '200204' , '200205' , '200206' , '200211' , '200212' , '200301' , '200302' , '200303' , '200304' , '200305' , '200401' , '200402' , '200403' , '200404' , '200405' , '200501' , '200502' , '200503' , '200504' , '200505' , '200601' , '200602' , '200603' , '200604' , '200605' , '200612' , '200701' , '200702' , '200703' , '200704' , '200801' , '200802' , '200803' , '200804' , '200805' , '200812' , '200901' , '200902' , '200903' , '200904' , '200905' ) ;
                var numMissing = 52 ;
                break ;
      case 5  : firstYearMonth = 200208 ;
                lastYearMonth = 200711 ;
                var missing = new Array (  ) ;
                var numMissing = 0 ;
                break ;
      case 6  : firstYearMonth = 200308 ;
                lastYearMonth = 200907 ;
                var missing = new Array ( '200804' ) ;
                var numMissing = 1 ;
                break ;
      default : firstYearMonth = 199701 ;
                lastYearMonth = 200907 ;
                var missing = new Array (  ) ;
                var numMissing = 0 } ;

//
// Increment if wanted
//

   if ( increment < 0 ) {
      month = month - 1 ;
      if ( month < 1 ) {
         month = 12  ;
         year = parseInt(year) - 1 ;
         year = '' + year ;
         } ;
      } ;

   if ( increment > 0 ) {
      month = month + 1 ;
      if ( month > 12 ) {
         month = 1  ;
         year = parseInt(year) + 1 ;
         year = '' + year ;
         } ;
      } ;

//
// Check year and month
//

   if ( month < 10 ) { yearMonth = year + '0' + month }
                else { yearMonth = year + month } ;

   if ( yearMonth > lastYearMonth ) {
       year  = parseInt(firstYearMonth/100,10) ;
       month = firstYearMonth % 100 ;
      } ;

   if ( yearMonth < firstYearMonth ) {
       year  = parseInt(lastYearMonth/100,10) ;
       month = lastYearMonth % 100 ;
      } ;

//
// Missing months, find month that is there
//

   if ( numMissing != 0 ) {
       missingIt = 1 ;
       while ( missingIt != 0 ) {
           missingIt = 0 ;
           for ( i = 0; i < numMissing ; i++ ) {
               if ( missing[i] == yearMonth ) {
                   missingIt = missingIt + 1 ;
                   } ;
               } ;
           if ( missingIt != 0 ) {
              if ( increment < 0 ) {
                 month = month - 1 ;
                 if ( month < 1 ) {
                    month = 12  ;
                    year = parseInt(year) - 1 ;
                    year = '' + year ;
                    } ;
                 }
                else {
                 month = month + 1 ;
                 if ( month > 12 ) {
                    month = 1  ;
                    year = parseInt(year) + 1 ;
                    year = '' + year ;
                    } ;
                 } ;
              if ( missingIt != 0 ) {
                  if ( month < 10 ) { yearMonth = year + '0' + month }
                               else { yearMonth = year + month } ;
                  } ;
              } ;
           } ;
      } ;

//
// Display new page / image
//

   displayHist(modelIndex, month, year, parIndex ) ;

   return true ;
   } ;

function delayHist(delaytime) {

//
// Delay first display of page by several seconds
//

   setTimeout("displayHist(0,0,0,0)",delaytime) ;

   return true ;
   } ;
