<!--

var yearStart=1997
var monthStart=12
var dayStart=07

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 otherDisplay=false

var year,month,day
var re_year, re_month, re_day

var firstY,firstM,firstD
var lastY,lastM,lastD

var defaultYearIdx
var douttext

var passStart="a"
var pass=passStart

var yearEnd = yearEnd_nrt
var monthEnd = monthEnd_nrt
var dayEnd = dayEnd_nrt

year=yearEnd; month=monthEnd; day=dayEnd;

firstY = yearStart; firstM = monthStart; firstD = dayStart;
lastY = yearEnd; lastM = monthEnd; lastD = dayEnd;

defaultYearIdx = yearEnd - yearStart + 1;

douttext=convert(month) + "/" + convert(day) + "/" + year + "-" + pass
	
scan_refresh_timeframe();



function year_options() {
	options = "<OPTION VALUE=" + year + ">Year"
	for (var i=yearStart; i<=yearEnd; ++i) {
		options += "<OPTION VALUE=" + i 
		if (year == i) 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].substring(0, 3);
	}
	return options
}


function day_options() {
	options = "<OPTION>Day"
	for (var i=1; i<=31; ++i) {
		options += "<OPTION" 
		if (i == dayEnd) options += " selected>"
		else options += ">"
		if (i < 10) options += "0"
		options += i
	}
	return options
}


function range(num, beginMonth, beginDay, beginYear, endMonth, endDay, endYear) {
	this.num = num;
	this.beginMonth = beginMonth;
	this.beginDay = beginDay;
	this.beginYear = beginYear;
	this.endMonth = endMonth;
	this.endDay = endDay;
	this.endYear = endYear;
}


function valid_day(inDay) {
	if (month == 2) {
		if (inDay > get_Feb_length()) return false;
	}
	else if (inDay > month_lengths[month]) return false;
	return true;
}


function reset_index() {
	form = document.forms["select_date"];
	form.year.options[defaultYearIdx].selected = true;
	form.month.options[monthEnd].selected = true;
	form.day.options[dayEnd].selected = true;
	form.pass.options[1].selected = true;
}


function within_range() {

	if ((year == yearStart) && (month == monthStart) && (day < dayStart)) return false;
	if ((year == yearStart) && (month <  monthStart)) return false;
	if ((year <  yearStart)) return false;
	
	if ((year == yearEnd) && (month == monthEnd) && (day > dayEnd)) return false
	if ((year == yearEnd) && (month > monthEnd)) return false;
	if ((year >  yearEnd)) return false
	return true;
}


function set_year() {
	form = document.forms["select_date"];
	ySI = form.year.selectedIndex
	if (ySI == 0) { form.year.options[defaultYearIdx-(yearEnd-year)].selected = true; }
	else year = parseInt(form.year.options[ySI].value);
}


function set_month() {
	form = document.forms["select_date"];
	mSI = form.month.selectedIndex;
	if (mSI == 0) { form.month.options[month].selected = true; }
	else month = mSI
}


function set_day() {
	form = document.forms["select_date"];
	dSI = form.day.selectedIndex;
	if (dSI == 0) { form.day.options[day].selected = true; }
	else day = dSI
}


function set_pass() {
	form = document.forms["select_date"]
	pSI = form.pass.selectedIndex
	if (pSI == 0) {
		if (pass == "a") { si = 1 } else { si = 2 }
		form.pass.options[si].selected = true;
	}	
	else pass = form.pass.options[pSI].value;
}


// Replaced fn valid_select with that of monthly for now:
function valid_select() {
	if ( !exist_data()) {
		rangeMsg = "TMI daily data is available\n";
		rangeMsg += " from " + monthStart + "/"
		rangeMsg += dayStart + "/" + yearStart + " to " + monthEnd + "/" + dayEnd + "/" + yearEnd +".\n"
		rangeMsg += "Please check your selection.";
		alert(rangeMsg);
		return false
	}
	return true
}


/*
function valid_select() {
	if ( !valid_day(day)) {
		msg = "The day you selected does not exist.\n";
		msg += "Please check your calendar and select an appropriate day.";
		alert(msg);
		return false
	}
return true
} 
*/


function convert(inStr) {
	if (inStr < 10) return ("0" + inStr);
	return inStr;
}


function get_gif(par) {
	gifName = "y" + year + "/m" + convert(month) + "/" + par + year;
	if (month == 10) gifName += "a";
	else if (month == 11) gifName += "b";
	else if (month == 12) gifName += "c";
	else gifName += month;
	gifName += convert(day);
	gifName += pass + ".gif"
	return gifName;
}


function display_image() {
	form = document.forms["select_date"];
	
	if (otherDisplay || valid_select()) {
		outputText = year + "/" + convert(month) + "/" + convert(day) + ", ";

		if (pass == "a") outputText += "Ascending passes";
		else outputText += "Descending passes";
		
		rootLarge = rootDir + satelliteDir + "/gif/";
		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;

		if ( data_may_need_refreshing() ) {
			reloadImages()
		} else {
			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["select_date"]

	if (pass == "a") 
		{pass = "d"; form.pass.options[2].selected = true}
	else 
		{pass = "a"; form.pass.options[1].selected = true; day++}

	if (!valid_day(day)) {day = 1;	  month++}
	
	if (month == 13) {year++; month = 1}

	if (!exist_data())  {
		if ((year > lastY) || (year == lastY && month > lastM) || (year == lastY && month==lastM && day > lastD)) {
			msg ="Sorry. Data are not available"
			msg +=" after "+ lastM + "/" + lastD + "/" + lastY + ".\n"
			msg += "Images for " + lastM + "/" +lastD +"/" +lastY + " will be displayed"
			alert(msg)
			display_last()
		}
		else if ((year < firstY) || (year == firstY && month < firstM)) display_first()
	}

	else {
		form.year.options[year-yearStart+1].selected = true
		form.month.options[month].selected = true
		form.day.options[day].selected = true
		display_image()
	}
}


function display_prev() {
	otherDisplay = true
	form = document.forms["select_date"];

	if (pass == "d") {pass = "a"; form.pass.options[1].selected = true;}
	else {pass = "d"; form.pass.options[2].selected = true; day--;}

	if(day == 0) {
		month--
		if(month == 0) {year--; month = 12}
		if (month == 2) {day = get_Feb_length()}
		else {day = month_lengths[month]}
	}

	if (!exist_data()) {
		if ((year < firstY) || (year == firstY && month < firstM) || (year == firstY && month == firstM && day <firstD)) {
			msg ="Sorry. Data are not available"
			msg +=" before "+ firstM + "/" + firstD + "/" + firstY + ".\n"
			msg += "Images for " + firstM + "/" +firstD +"/" +firstY + " will be displayed"
			alert(msg)
			display_first()
		}
		else if ((year > lastY) || (year == lastY && month > lastM)) display_last()
	}

	else {
		form.year.options[year-yearStart+1].selected = true
		form.month.options[month].selected = true
		form.day.options[day].selected = true
		display_image()
	}
}


function display_first() {
	year = firstY
	form.year.options[year-yearStart+1].selected = true
	month = firstM
	form.month.options[month].selected = true
	day = firstD
	form.day.options[day].selected = true
	pass = "a"
	form.pass.options[1].selected = true
	display_image()
}


function display_last() {
	year = lastY
	form.year.options[year-yearStart+1].selected = true
	month = lastM
	form.month.options[month].selected = true
	day = lastD
	form.day.options[day].selected = true
	pass = "d"
	form.pass.options[2].selected = true
	display_image()
}


function get_help() {
	spawn_window("tmi_daily_help.html","help");
}


function refresh() {
	reloadImages();
}


function reloadImages() {
	var now = new Date();
	qStr = now.getTime();

	form = document.forms["select_date"];

	document.images["cloud"].src = rootSmall + get_gif("l") + "?" + qStr;
	document.images["rain"].src = rootSmall + get_gif("r") + "?" + qStr;
	document.images["vapor"].src = rootSmall + get_gif("v") + "?" + qStr;
	document.images["wind"].src = rootSmall + get_gif("w") + "?" + qStr;
	document.images["windz"].src = rootSmall + get_gif("z") + "?" + qStr;
	document.images["sst"].src = rootSmall + get_gif("s") + "?" + qStr;
}


function scan_refresh_timeframe() {
	re_year = year; re_month = month; re_day = day;
}


function data_may_need_refreshing() {
	if (year > re_year) return(1);
	if (year < re_year) return(0);

	// year must be same

	if (month > re_month) return(1);
	if (month < re_month) return(0);

	// month must be same

	if (day >= re_day) return(1);
	return(0);

}


// -->