<!--

var yearStart=1997
var monthStart=12

var defaultYearIdx=yearEnd-yearStart +1
var year=yearEnd
var month=monthEnd
var imgWin, instrWin
var rootDir="../graphics/";
var satelliteDir="tmi";

var rootLarge = rootDir + satelliteDir + "/graphics_v04/gif/";
var rootSmall = rootDir + satelliteDir + "/graphics_v04/small/";

var cloudFile = rootLarge + get_gif("l");
var rainFile = rootLarge + get_gif("r");
var vaporFile = rootLarge + get_gif("v");
var windFile = rootLarge + get_gif("w");
var sstFile = rootLarge + get_gif("s");
var windzFile = rootLarge + get_gif("z");

var firstY = yearStart
var firstM = monthStart  
var lastY = yearEnd
var lastM = monthEnd

var form = document.forms[0]

var otherDisplay=false

function year_options() {
	options = "<OPTION VALUE=" + year + ">Year"
	for (var i=yearStart; i<=yearEnd; ++i) {
		options += "<OPTION VALUE=" + i 
		if (i == yearEnd) options += " selected>"
		else options += ">"
		options += i
	}
	return options
}

function month_options() {
	options = "<OPTION>Month"
	for (var i=1; i<=12; ++i) {
		options += "<OPTION" 
		if (i == monthEnd) options += " selected>"
		else options += ">"
		options += month_names[i]
	}
	return options
}


function within_range() {
	if ((year < yearStart) || (year > yearEnd)) return false;
	if ((year == yearStart) && (month < monthStart)) return false;
	if ((year == yearEnd) && (month > monthEnd)) return false;
	return true;
}

function set_year() {
	si = form.year.selectedIndex;
	if (si == 0) {form.year.options[defaultYearIdx-(yearEnd-year)].selected = true; }
	else { year = parseInt(form.year.options[si].value); }
}

function set_month() {
	mSI = form.month.selectedIndex;
	if (mSI == 0) {form.month.options[month].selected = true; }
	else { month = mSI }
}

function valid_select() {
	if ( !exist_data()) {
		rangeMsg = "Monthly averaged data are available\n";
		rangeMsg += "from " + monthStart + "/" + yearStart + " to " + monthEnd + "/" + yearEnd +".\n"
		rangeMsg += "Please check your selection.";
		alert(rangeMsg);
		return false
	}
	return true
}

function reset_index() {
	form = document.forms[0];
	form.year.options[defaultYearIdx].selected = true;
	form.month.options[monthEnd].selected = true;
}

function get_dataFile() {
	if (valid_select()) {
		dataFile = "../data/tmi/bmaps_v04/y" + year + "/m" + convert(month) + "/"
		dataFile += "TMI_" + year + convert(month) + "v4.gz" 
		window.location = dataFile;
	}
}

function convert(inStr) {
	if (inStr < 10) return ("0" + inStr);
	return inStr;
}

function get_gif(par) {
	gifName = "y" + year +"/m"+convert(month)+"/" + par + year;
	gifName += convert(month);
	gifName += ".gif";
	return gifName;
}

function display_image() {
	form = document.forms[0];
	if (form.year.selectedIndex == 0) 
		form.year.options[defaultYearIdx].selected = true;
	if (form.month.selectedIndex == 0) 
		form.month.options[monthEnd].selected = true;
	if (otherDisplay || valid_select()) {
		outputText = year + "/" + month_names[month].substring(0, 3) + " - monthly average";
		cloudFile = rootLarge + get_gif("l");
		rainFile = rootLarge + get_gif("r");
		vaporFile = rootLarge + get_gif("v");
		windFile = rootLarge + get_gif("w");
		windzFile = rootLarge + get_gif("z");
		sstFile = rootLarge + get_gif("s");

		form.output.value = outputText;
		document.images["cloud"].src = rootSmall + get_gif("l");
		document.images["rain"].src = rootSmall + get_gif("r");
		document.images["vapor"].src = rootSmall + get_gif("v");
		document.images["wind"].src = rootSmall + get_gif("w");
		document.images["windz"].src = rootSmall + get_gif("z");
		document.images["sst"].src = rootSmall + get_gif("s");

		otherDisplay = false 
	}
}

function display_next() {
	otherDisplay = true
	form = document.forms[0];
	yIdx = form.year.selectedIndex
	month = (month + 1) % 13;
	if (month == 0) {
		year++;
		yIdx++
		month = 1;
	}
	if ( !exist_data()) {
		if ((year > lastY) || (year == lastY && month > lastM)) {
			msg = "Sorry.  Data are not available "
			msg += "after " + lastM +"/" + lastY + ".\n"
			msg += "Images for " + lastM +"/" + lastY + " will be shown."
			alert(msg)
			display_last()
		}
		else if ((year < firstY) || (year == firstY && month < firstM)) display_first()
	}
	else {
		form.year.options[yIdx].selected = true;
		form.month.options[month].selected = true;
		display_image();
	}
}

function display_prev() {
	otherDisplay = true
	form = document.forms[0];
	yIdx = form.year.selectedIndex
	month--;
	if (month == 0) {
		year--;
		yIdx--
		month = 12;
	}
	if ( !exist_data()) {
		if ((year < firstY) || (year == firstY && month < firstM)) {
			msg = "Sorry.  Data are not available "
			msg += "before " + firstM +"/" + firstY + ".\n"
			msg += "Images for " + firstM +"/" + firstY + " will be shown."
			alert(msg)
			display_first()
		}
		else if ((year > lastY) || (year == lastY && month > lastM)) display_last()
	}
	else {
		form.year.options[yIdx].selected = true;
		form.month.options[month].selected = true;
		display_image();
	}
}

function display_first() {
	form = document.forms[0];
	form.year.options[defaultYearIdx-(yearEnd-yearStart)].selected = true
	year = firstY
	month = firstM
	form.month.options[month].selected = true;
	display_image()
}



function display_last() {
	form = document.forms[0];
	year = lastY
	form.year.options[defaultYearIdx].selected = true
	month = lastM
	form.month.options[month].selected = true
	display_image()
}


function get_help() {
	spawn_window("tmi_averaged_help.html","help");
}


function data_may_need_refreshing() {
	return(0);
}


// -->
