<!--
// JavaScript to support the SINTEX-F Seasonal forecasts home pages
// sintex_f1_forecast.html.en and sintex_f1_forecast.html.ja
// Sergey Varlamov, December 2008
//
// May, 2009 - if last map is not available - shifts backward (now - 1 month only).
// February 16, 2010: corrected bug in the shift one month back if there is no images,
//                    bug impacted the late February update case.
//
// ******* Main parameters to update if production schedule is changing
// Do not need to change map_delay if real delay is less then one month,
// last production date is shifted one month back automaticly. But images
// created earlier then "map_delay" day of current month would not be shown
// until this date

var last_map_delay_days=7;
var base_name="http://www.jamstec.go.jp/frcgc/research/d1/iod/2007/forecast/";

// ******* End of list

var jpn = 0;
var param_is_index = 0;
var use_prev_month = 0;
var num_init = 0;

window.onload = initialize;
function initialize () { if (self.init) self.init();}
window.onunload = remove;
function remove () { if (self.exit) self.exit(); }

function init ()
{
  num_init = num_init + 1;
  if(use_prev_month == 0){
    is_jpn();
    hide_show_div(document.getElementById("iform1"),"flip");
  }
  adj_seasons_param (); /* fill date and season lists for first parameter */
  adj_seasons_date();
  display_map();
  if(use_prev_month == 0){
    var x = document.forms[0].elements;
    x['base_date'].onchange = function(){adj_seasons_date();display_map();}
    x['parameter'].onchange = function(){adj_seasons_param();display_map();}
    x['season'].onchange = display_map;
//    x['update'].onclick = display_map; //vsm: remove this control from page, Sep. 2009
  }
}
function adj_seasons_date () {
  if(jpn){
//    var sname = new Array("冬","春","夏","秋");
    var sname = new Array("12~2月","3~5月","6~8月","9~11月");
  }
  else {
//    var sname = new Array("Winter","Spring","Summer","Autumn");
    var sname = new Array("Dec-Feb","Mar-May","Jun-Aug","Sep-Nov");
  }
  var sval = new Array("DJF","MAM","JJA","SON");
  var x = document.forms[0].elements;
  var month = x['base_date'].value.substr(0,3);
  var selIndex = x['season'].selectedIndex;
  var selSeason = x['season'].options[selIndex].value;

  x['season'].options.length=0;
  var is=0, is_selected=0;
  var iss=0;
  if (month == "jan"||month == "feb"||month == "mar") iss=1;
  if (month == "apr"||month == "may"||month == "jun") iss=2;
  if (month == "jul"||month == "aug"||month == "sep") iss=3;
  if (month == "oct"||month == "nov"||month == "dec") iss=0;
  for (var i=0; i<3; i++){
    is = iss + i;
    if (is > 3) is = is - 4;
    if (sval[is] == selSeason){
      x['season'].options[i]=new Option(sname[is], sval[is], false, true)
      is_selected=1;
    }
    else
      x['season'].options[i]=new Option(sname[is], sval[is], false, false)
  }
  if (is_selected == 0)x['season'].options[0].selected = true;
}
function adj_seasons_param () {
  var x = document.forms[0].elements;
  if( x['parameter'].value == "ssta.nino3.4.fcst" ||
      x['parameter'].value == "ssta.eio.fcst" ||
      x['parameter'].value == "ssta.nino3.4.fcst.2-yr" ||
      x['parameter'].value == "ssta.ninomdk.fcst") {
    x['season'].disabled = true;
  }
  else {
    x['season'].disabled = false;
  }
  if( x['parameter'].value == "ssta.nino3.4.fcst" ) {
    manage_base_date(last_map_delay_days,2006,9);
  } else if( x['parameter'].value == "ssta.eio.fcst" ) {
    manage_base_date(last_map_delay_days,2006,7);
  }else if( x['parameter'].value == "ssta.nino3.4.fcst.2-yr" ) {
    manage_base_date(last_map_delay_days,2008,9);
  }else if( x['parameter'].value == "tprep.glob" ) {
    manage_base_date(last_map_delay_days,2007,8);
  }else if( x['parameter'].value == "temp2.glob" ) {
    manage_base_date(last_map_delay_days,2007,8);
  }else if( x['parameter'].value == "ssta.ninomdk.fcst" ) {
    manage_base_date(last_map_delay_days,2009,8);
  }else {
    manage_base_date(last_map_delay_days,2006,3);
  }
}
function display_map() {
  /* file name examples used now for plots of different parameters */
  /* ssta.glob.JJA08.1may2008.gif */
  /* tprep.glob.DJF09.1oct2008.gif */
  /* ssta.nino3.4.fcst.1nov2008.gif */
  /* ssta.eio.fcst.1jul2006.gif */
  /* ssta.nino3.4.fcst.2-yr.1sep2008.gif */

  var x = document.forms[0].elements;
  var base_date = x['base_date'].value;
  var syear  = base_date.substr(5,2)*1;
  var scent  = base_date.substr(3,2);
  var month  = base_date.substr(0,3);
  var season = x['season'].value;
  var parameter = x['parameter'].value;

  if (parameter == "ssta.glob" ||
      parameter == "tprep.glob" ||
      parameter == "temp2.glob") {
    if (month == "apr" ||
        month == "may" ||
        month == "jun" ) {
      if (season == "DJF") syear = syear*1 + 1;
    }
    if (month == "jul" ||
        month == "aug" ||
        month == "sep" ) {
      if (season == "DJF") syear = syear*1 + 1;
      if (season == "MAM") syear = syear*1 + 1;
    }
    if (month == "oct" ||
        month == "nov" ||
        month == "dec" ) {
      syear = syear*1 + 1;
    }
    if (syear < 10)syear = "0"+syear;
    /* the format for the target season in file name
       was changed by Luo-san to full year. */
    if (syear >= 10)syear = ""+scent+syear;
    var sz= season + syear + ".";
  }
  else {
    var sz= "";
  }
  document.images["Graphic0"].width = 660;
  document.images["Graphic0"].height = 450;
  var img_URL = base_name+parameter+"."+sz+"1"+base_date+".gif";
//  document.images["Graphic0"].onerror = isBad;
  document.images["Graphic0"].onerror = PrevMonth;
  document.images["Graphic0"].src = img_URL;
  is_index();
  display_comment("Index_Comment",1);
  display_comment("Anomaly_Comment",0);
  return true;
}
function is_index (){
  var x = document.forms[0].elements;
  if ( x['parameter'].value == "ssta.nino3.4.fcst" ||
       x['parameter'].value == "ssta.nino3.4.fcst.2-yr" ||
       x['parameter'].value == "ssta.eio.fcst" ||
       x['parameter'].value == "ssta.ninomdk.fcst") {
    param_is_index = 1;
  }
  else {
    param_is_index = 0;
  }
}
function display_comment(id,indx){
  hide_show_div(document.getElementById(id),"none");
  if (param_is_index == indx)
    hide_show_div(document.getElementById(id),"flip");
}
function hide_show_div(field, sVisibility){
  try{
    if((field) && (field.style)){
       if (sVisibility=='flip'){
         if (field.style.display == 'none'){
           sVisibility = 'block';
         }
         else {
           sVisibility = 'none';
         }
       }
      field.style.display = sVisibility;
    }
  }
  catch(exception){
    //no handling - just preventing page explosions
  }
}
function is_jpn(){
  jpn = 0;
  if(typeof(window.use_language) !== "undefined" || typeof(use_language) !== "undefined"){
    if(use_language == "ja" || use_language == "jp") jpn = 1;
  }
}
//function is_jpn(){
//  var url_len = document.URL.length;
//  var url_ext = document.URL.substr(url_len-3,3);
//  if(url_ext == ".ja" || url_ext == ".jp") jpn = 1;
//  else                                     jpn = 0;
//}
function manage_base_date(delay_in_days,firstYear,firstMonth) {
  var mnth=new Array("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");
  if(jpn) {
//    var year_prefix = "平成";
//    var year_zero = 1988*1;
    var year_prefix = "";
    var year_zero = 0*1;
    var year_suffix = "年";
    var MNTH = new Array("1月","2月","3月","4月","5月","6月",
                         "7月","8月","9月","10月","11月","12月");
  }
  else {
    var MNTH=new Array("January","February","March","April","May","June",
                       "July","August","September","October","November","December");
  }
  var cur_date = new Date();
  //Set 1 day in milliseconds
  var one_day=1000*60*60*24;
  wrk_date = new Date(cur_date.getTime()-one_day*delay_in_days);

  var selIndex = document.forms[0].base_date.selectedIndex;
  document.forms[0].base_date.options.length=0;
  var lastYear=wrk_date.getUTCFullYear();
  var lastMonth=wrk_date.getUTCMonth();
  if(use_prev_month != 0){
    lastMonth = lastMonth-1;
    if(lastMonth < 0){
      lastMonth=lastMonth+12;
      lastYear=lastYear-1;
    }
  }
  var i=0;
/*
  new Option(text, value, defaultSelected, selected)
*/
  var date_str = "";
  for (var yr=lastYear; yr>=firstYear; yr--){
    if (yr==lastYear) m2=lastMonth;
    else              m2=11;
    if (yr==firstYear)m1=firstMonth-1;
    else              m1=0;
    for (var mn=m2; mn>=m1; mn--){
      if (jpn){
        date_str = year_prefix+(yr-year_zero)+year_suffix+MNTH[mn];
      }
      else {
        date_str = MNTH[mn]+" "+yr;
      }
      if (i == selIndex)
        document.forms[0].base_date.options[i]=new Option(date_str, mnth[mn]+yr, false, true)
      else
        document.forms[0].base_date.options[i]=new Option(date_str, mnth[mn]+yr, false, false)
      i++;
    }
  }
}
function isBad() {
  this.width = 0;
  this.height = 0;
  alert('Sorry, requested image file does not exist!');
}
function PrevMonth() {
  var parameter = document.forms[0].elements['parameter'].value;
  if(use_prev_month != 0 || (num_init == 1 && parameter != "ssta.glob")){
    isBad();
  }
  else{
    if (parameter == "ssta.glob" && num_init == 1){
      use_prev_month = 1;
      init();
    }
  }
}

// -->


