<!--

var yearStart = 2003;
var monthStart = 4;

var year,day,month,hour,yearEnd,monthEnd,dayEnd
var re_year, re_month, re_day

year=yearEnd; month=monthEnd; day=dayEnd;


if (time_frame == "week") {
	var weekStart = weekArray[1]
	var wIdx = numwks
}


scan_refresh_timeframe();

var image_file_extension = ".png";

var satelliteNumStart=2
var pass = "m"
var nextSatelliteCounter=0
var rootDir = "../graphics/";
var data_dir = "../data/seawinds/bmaps_v03/";
var img_dir = "graphics_v03/";
var firstY, firstM, firstD, lastY, lastM, lastD

var qStr

var keep_rgn = 1; keep_size = 1; keep_date = 1;

var region_key = "global";
var image_size = "small";

url_params = window.location.search.substring(1);
params = url_params.split("&");

for (i=0; i<params.length; ++i) {
	param = params[i];
	param = param.split("=");
	name = param[0];
	val = param[1];

	switch (name) {
		case "rgn": region_key = val; break;
		case "size": image_size = val; break;
		case "year": year = val - 0; break;
		case "month": month = val - 0; break;
		case "day": day = val - 0; break;
		case "pass": pass = val; break;

		case "keep":
			keep_rgn = val.charAt(0) - 0;
			keep_size = val.charAt(1) - 0;
			keep_date = val.charAt(2) - 0;
			break;
	}
}

if (keep_date) {
	if (time_frame == "week") { date_to_week(); }
}

var form

num_satellites = 2
satellites = new createArray(num_satellites)
satellites[1] = new avail("nscat", 1)
satellites[2] = new avail("SeaWinds", 2)

satelliteNum = satelliteNumStart
satelliteName = "Satellite: " + satellites[satelliteNum].name
satelliteDir = satellites[satelliteNum].name

set_image_folder();


nsi = 2
satelliteInfo = new createArray(nsi);
satelliteInfo[1] = new range(1, 1, 1, 1997, 12, 31, 1998);
satelliteInfo[2] = new range(2, 4, 10, 2003, 10, 24, 2003);


nGap = 1
gaps = new createArray(nGap);
gaps[1] = new gap(2, 1999, 11, 18);


function init() {
	form = document.forms["select_date"];
	set_refresh_time();
	set_image_map();
}


function avail(name, TF) {
	this.name = name
	this.TF = TF
}



function year_options() {
	options = "<OPTION VALUE=" + year + ">Year&nbsp;&nbsp;"
	for (var i=yearStart; i<=yearEnd; ++i) {
		options += "<OPTION VALUE=" + i 
		if (year == i) options += " selected>"
		else options += ">"
		options += i
	}
	return options
}


function set_year() {
	ySI = form.year.selectedIndex;
	if (ySI == 0) { sync_year(); }
	else { year = parseInt(form.year.options[ySI].value); }
}


function sync_year() {
	form.year.options[year - yearStart + 1].selected = true;
}


function month_options() {
	options = "<OPTION>Month"
	for (var i=1; i<=12; ++i) {
		options += "<OPTION"
		if (month == i) options += " selected>"
		else options += ">"
		options += month_names[i].substring(0, 3);
	}
	return options
}


function set_month() {
	mSI = form.month.selectedIndex;
	if (mSI == 0) { sync_month(); }
	else { month = mSI; }
}


function sync_month() {
	form.month.options[month].selected = true;
}

function week_options() {
	options = ""
	for (var i=1; i<=numwks; ++i) {
		options += "<OPTION" 
		if (i == wIdx) options += " selected>"
		else options += ">"
		options += weekName[i]
	}
	return options
}


function set_week() {
	wIdx = form.week.selectedIndex + 1;
	display_image();
}


function sync_week() {
	form.week.options[wIdx - 1].selected = true;
}


function day_options() {
	options = "<OPTION>Day"
	for (var i=1; i<=31; ++i) {
		options += "<OPTION" 
		if (day == i) options += " selected>"
		else options += ">"
		if (i < 10) options += "0"
		options += i
	}
	return options
}


function set_day() {
	dSI = form.day.selectedIndex;
	if (dSI == 0) { form.day.options[day].selected = true; }
	else { day = dSI; }
}


function pass_options() {
	str = "<option value='e'>Passes";
 
	str += "<option value='m'"; if (pass == "m") { str += " selected"; } str += ">Morning";
	str += "<option value='e'"; if (pass == "e") { str += " selected"; } str += ">Evening";

	return(str);
}


function set_pass() {
	pSI = form.pass.selectedIndex
	if (pSI == 0) {
		if (pass == "m") { opt_num = 1; }
		else { opt_num = 2; }
		form.pass.options[opt_num].selected = true;
	}
	else { pass = form.pass.options[pSI].value; }
}



function satellite_options() {
	options = "<OPTION VALUE=F" + satelliteDir + ">Satellite #"
	for (var i=1; i<=6; ++i) {
		options += "<OPTION VALUE=F" + satellites[i].name 
		if (satelliteNum == i) options += " selected>"
		else options += ">"
		options += "Satellite F" + satellites[i].name
	}
	return options
}

function set_satellite() {
	sNSI = form.satellite.selectedIndex;
	if (sNSI == 0) {
		form.satellite.options[satelliteNum].selected = true;
	}
	else {
		satelliteNum = sNSI;
		satelliteName = form.satellite.options[satelliteNum].text;
		satelliteDir = form.satellite.options[satelliteNum].value;
	}
}



function region_options() {
	
	str = "<option value=region_map>Region Map"
	
	for (i=0; i<rgn_keys.length; i++) {
		rgn = rgn_keys[i];
		str += "<option value=" + rgn;
		if (rgn == region_key) str += " selected";
		str += ">" + rgn_names[i];
	}
	return str
}


function set_region() {
	regionSI = form.region.selectedIndex;
	region_key = form.region.options[regionSI].value;
	
	set_image_map();
	
	display_image();
}


function region(p) { // called by image map

	region_key = p;
	
	idx = get_po(rgn_keys, p) + 1;
	form.region.options[idx].selected = 1;
	
	display_image();
	set_image_map();
}


function set_image_map() {

	switch (region_key) {
	case "global" :
	case "region_map": str_map = "#region_image_map_" + image_size; break;
	default: str_map = "#region_image_map_not_global";
	}
	document.images["wind"].useMap = str_map;
}


function image_size_options() {
	syzs = ["small", "medium", "large"];
	lbls = ["Image Size: Small", "Image Size: Medium", "Image Size: Large"]
	str = "";

	for (i=0; i<syzs.length; i++) {
		syz = syzs[i];
		str += "<option value=" + syz;
		if (syz == image_size) {str += " selected";}
		str += ">" + lbls[i];
	}

	return(str);
}


function set_image_size() {
	sizeSI = form.image_size.selectedIndex;
	image_size = form.image_size.options[sizeSI].value;
	
	set_image_map();
	
	set_image_folder();
	display_image();
}


function set_image_folder() {
	image_folder = rootDir + satelliteDir + "/" + img_dir + image_size + "/";
}


function createArray(n) {
	this.length = n;
	return this;
}

function gap(gapSatellite, gapYear, gapMonth, gapDay) {
	this.gapSatellite = gapSatellite;
	this.gapYear = gapYear;
	this.gapMonth = gapMonth;
	this.gapDay = gapDay;
}

function is_gap() {
	for (var i = 1; i <= nGap; ++i) {
		if ((satelliteNum == gaps[i].gapSatellite) && (year == gaps[i].gapYear) && (month == gaps[i].gapMonth) && (day == gaps[i].gapDay)) return true;
	}
	return false;
}

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 exist_data(idx) {
	count = 0
	for (var i = 1; i <=nsi; ++i) {
		if (idx == satelliteInfo[i].num) {
			count++ 
			if (within_range(i)) return true
			else {
				endIdx = i
				if (count == 1) bgnIdx = i
			}
		}
	}
	firstY = satelliteInfo[bgnIdx].beginYear
	firstM = satelliteInfo[bgnIdx].beginMonth
	firstD = satelliteInfo[bgnIdx].beginDay
	lastY = satelliteInfo[endIdx].endYear
	lastM = satelliteInfo[endIdx].endMonth
	lastD = satelliteInfo[endIdx].endDay
	return false
}


function within_range(idx) {
	bYear = satelliteInfo[idx].beginYear;
	eYear = satelliteInfo[idx].endYear;
	if ((year < bYear) || (year > eYear)) return false;
	if (year == bYear) {
		bMonth = satelliteInfo[idx].beginMonth;
		if (month < bMonth) return false;
		if (month == bMonth) {
			bDay = satelliteInfo[idx].beginDay;
			if (day < bDay) return false;
		}
	}
	if (year == eYear) {
		eMonth = satelliteInfo[idx].endMonth;
		if (month > eMonth) return false;
		if (month == eMonth) {
			eDay = satelliteInfo[idx].endDay;
			if (day > eDay) return false;
		}
	}
	return true;
}


function exist_satellite() {
	noDataMsg = "Currently available satellites are:\n";
	if (satellites[satelliteNum].TF == 0) {
		for (var i = 1; i <= 6; i++) 
			if (satellites[i].TF != 0) noDataMsg += satellites[i].name + "\n";
		noDataMsg += "Please select an available satellite.";
		alert(noDataMsg);
		return false
	}
	return true
}


function valid_select() {
	if (time_frame == "day") return valid_select_day();
	if (time_frame == "week") return(1);
	if (time_frame == "month") return valid_select_month();
}

function valid_select_month() {

	out_of_range = month_out_of_range();

	if ( out_of_range > 0 ) { out_of_range_msg(1, "hang"); return(0); }
	if ( out_of_range < 0 ) { out_of_range_msg(-1, "hang"); return(0); }

	return(1);
}


function valid_select_day() {
	if ( !valid_day(day)) {
		msg = "By my calculations, the day you selected does not seem to exist.\n";
		msg += "Please check your calendar and try again.";
		alert(msg);
		return false
	}
	if ( !exist_satellite())  return false
	if (is_gap()) {
		alert("No " + satelliteDir + " data are available for this particular day.")
		return false
	}
	if ( !exist_data(satelliteNum)) {
		rangeMsg = satelliteDir + " data are available for\n" + "the following time periods:\n";
		for (var i = 1; i <= nsi; i++) {
			if (satelliteNum == satelliteInfo[i].num) { 
				rangeMsg += satelliteInfo[i].beginYear + "/";
				rangeMsg += satelliteInfo[i].beginMonth + "/";
				rangeMsg += satelliteInfo[i].beginDay + " --- ";
				tempY = satelliteInfo[i].endYear
				tempM = satelliteInfo[i].endMonth
				tempD = satelliteInfo[i].endDay
				if (tempY>yearEnd || (tempY==yearEnd && tempM>monthEnd) || (tempY==yearEnd && tempM==monthEnd && tempD>dayEnd)) {
					rangeMsg += yearEnd + "/" + monthEnd + "/" + dayEnd + "\n"
				} else {
					rangeMsg += tempY + "/" + tempM + "/" + tempD + "\n";
				}
			}
		}
		rangeMsg += "Please select an appropriate date.";
		alert(rangeMsg);
		return false
	}
	return true
}


function get_dataFile() {

	yyyy = "" + year;
	mm = pad(month);
	dd = pad(day);
	
	fyl = data_dir;

	switch (time_frame) {
	case "week": fyl += "weeks/"; break;
	default: fyl += "y" + yyyy + "/" + "m" + mm + "/";
	}

	switch (time_frame) {
	case "day": fyl += yyyy + mm + dd; break;
	case "week": fyl += weekArray[wIdx]; break;
	case "month": fyl += yyyy + mm; break;
	}

	if (valid_select()) { window.location = fyl + ".gz"; }
}


function pad(p) {
	if (p < 10) { return("0" + p); }
	return("" + p);
}


function get_image_path(par) {

	if (region_key == "region_map") return(image_folder + region_key + "/map.png");

	if (time_frame == "week") { week_to_date(); }
	yyyy = "" + year; mm = pad(month); dd = pad(day);

	img_name = region_key + "/"

	switch (time_frame) {
	case "week": { img_name += "weeks/"; break; }
	default: img_name += "y" + yyyy + "/m" + mm + "/";
	}

	switch (time_frame) {
	case "day":		{ img_name += yyyy + "_" + mm + "_" + dd + "_" + pass; break; }
	case "week":	{ img_name += yyyy + "_" + mm + "_" + dd + "_week"; break; }
	case "month":	{ img_name += yyyy + "_" + mm + "_month"; break; }
	}

	return image_folder + img_name + image_file_extension;
}



function update_display() {
	if ( valid_select() ) { display_image(); }
}

function display_image() {

	if ( data_may_need_refreshing() ) { reloadImages() }
	else { document.images['wind'].src = get_image_path("w"); }

}


function next() {

	if (pass == "m") { pass = "e"; form.pass.options[2].selected = true; }
	else { pass = "m"; form.pass.options[1].selected = true; day++; }
	
	if ( !valid_day(day)) {
		day = 1;
		month = (month + 1) % 13;
		if (month == 0) {
			year++; form.year.options[form.year.selectedIndex+1].selected = true;
			month = 1;
		}
		form.month.options[month].selected = true;
	}
	
	form.day.options[day].selected = true;
	
	if (exist_satellite()) {
		if (is_gap()) next()
		else if ( !exist_data(satelliteNum)) {
			if ((year > lastY) || (year == lastY && month > lastM) || (year == lastY && month == lastM && day > lastD)) {
				msg = "Data are not available "
				msg += "after " + lastY +"/" + lastM + "/" + lastD + " for " + satelliteDir + ".\n"
				msg += "Images for " + lastY +"/" + lastM + "/" + lastD + " will be shown."
				alert(msg)
				display_last()
			}
			else if ((year < firstY) || (year == firstY && month < firstM) || (year == firstY && month == firstM && day < firstD)) display_first()
			else next();
		}
		else display_image();
	}
}


function previous_week() { step_week(-1); }
function next_week() { step_week(1); }

function step_week(d) {
	new_wIdx = wIdx + d;
	if ( new_wIdx > numwks ) { alert("This is currently the last available week."); return; }
	if ( new_wIdx < 1 ) { alert("This is the earliest available week."); return; }
	wIdx = new_wIdx; sync_week(); display_image();
}


function previous_month() { step_month(-1); }
function next_month() { step_month(1); }

function step_month(d) {

	if (d < 0) {
		month--; if (month == 0) { month = 12; year--; }
	}

	if (d > 0) {
		month++; if (month == 13) { month = 1; year++; }
	}
		
	out_of_range = month_out_of_range();
	
	if ( out_of_range > 0 ) { out_of_range_msg(1, "nearest");  set_last_month(); }
	if ( out_of_range < 0 ) { out_of_range_msg(-1, "nearest"); set_first_month(); }
	
	sync_month(); sync_year();
	display_image();
}


function month_out_of_range() {
	if (year < yearStart) return(-1);
	if (year > yearEnd) return(1);
	
	if ((year == yearStart) && (month < monthStart)) return(-1);
	if ((year == yearEnd) && (month > monthEnd)) return(1);
	
	return(0);
}


function out_of_range_msg(side, mode) {

	msg = "Data are not available ";

	if ( side < 0 ) { msg += "before "; y = yearStart; m = pad(monthStart); }
	if ( side > 0 ) { msg += "after "; y = yearEnd; m = pad(monthEnd); }

	msg += y + "/" + m + ".\n";

	switch (mode) {
	
	case "nearest": msg += "Images for " + y + "/" + m + " will be shown."; break;
	
	case "hang":
		y1 = yearStart; m1 = pad(monthStart); y2 = yearEnd; m2 = pad(monthEnd);
		msg += "Please select a month between " + y1 + "/" + m1 + " and " + y2 + "/" + m2 + ".";
		break;
	}

	alert(msg);
}


function previous() {

	if (pass == "e") {
		pass = "m";
		form.pass.options[1].selected = true; 
	}
	else {
		pass = "e"
		form.pass.options[2].selected = true; 
		day--;
		if (day == 0) {
			month--;
			if (month == 0) {
				year--;
				form.year.options[form.year.selectedIndex-1].selected = true;
				month = 12;
			}
			if (month == 2) day = get_Feb_length();
			else day = month_lengths[month];
			form.month.options[month].selected = true;
		}
		form.day.options[day].selected = true;
	}
	if ( !valid_day(day)) {
		day--;
		form.day.options[day].selected = true;
		pass = "e";
		form.pass.options[2].selected = true;
		previous();
	}
	else if (exist_satellite()) {
		if (is_gap()) previous()
		else if ( !exist_data(satelliteNum)) {
			if ((year < firstY) || (year == firstY && month < firstM) || (year == firstY && month == firstM && day < firstD)) {
				msg = "Data are not available "
				msg += "before " + firstY +"/" + firstM + "/" + firstD + " for " + satelliteDir + ".\n"
				msg += "Images for " + firstY +"/" + firstM + "/" + firstD + " will be shown."
				alert(msg)
				display_first()
			}
			else if ((year > lastY) || (year == lastY && month > lastM) || (year == lastY && month == lastM && day > lastD)) display_last()
			else previous();
		}
		else 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 = "m"
	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 = "e"
	form.pass.options[2].selected = true
	display_image()
}


function set_first_month() { year = yearStart; month = monthStart; }

function set_last_month() { year = yearEnd; month = monthEnd; }


function next_satellite() {
	satelliteNum = (satelliteNum + 1) % 7;
	if (satelliteNum == 0) satelliteNum = 1;
	if ((satellites[satelliteNum].TF == 0) || !exist_data(satelliteNum) || is_gap()) {
		nextSatelliteCounter++;
		if (nextSatelliteCounter == 6) {
			alert("No satellite data available for this selection.\nPlease select another day or satellite")
			nextSatelliteCounter = 0
		}
		else next_satellite();
	}
	else {
		nextSatelliteCounter = 0;
		document.forms["select_date"].satellite.options[satelliteNum].selected = true;
		satelliteName = document.forms["select_date"].satellite.options[satelliteNum].text;
		satelliteDir = document.forms["select_date"].satellite.options[satelliteNum].value;
		display_image();
	}
}


function get_help() {
	spawn_window("qscat_help.html", "help");
}


function refresh() {
	reloadImages();
}


function reloadImages() {
	document.images['wind'].src = get_image_path("w") + "?" + qStr;
}


function set_refresh_time() {
	now = new Date();
	qStr = now.getTime();
	setTimeout('set_refresh_time()', 30 * 60 * 1000);
}

function scan_refresh_timeframe() {
	re_year = year; re_month = month; re_day = day;
}


function data_may_need_refreshing() {
	if (time_frame != "day") return(0);

	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);

}



function small_img(p) {

	tag = "<img src='" + get_image_path(p) + "' name="

	if (p == "w") tag += "'wind'"
 
	tag += " border=0"
	
	tag += ">"
	
	return tag
}


function img_table(p_time) {

str = "<table border='4' align='center' cellpadding='6'>"

str += "<tr>"

str += "<td align='center'>"

str += small_img("w")

str += "</td>"

str += "</tr>"

str += "</table>"

return(str)

}


function keep_options() {
	str = "";
	
	str += "<input type='checkbox' name='keep_rgn'";
	if (keep_rgn) {str += " checked"; }
	str += ">Region ";
	
	str += "<input type='checkbox' name='keep_size'";
	if (keep_size) {str += " checked"; }
	str += ">Size ";
	
	str += "<input type='checkbox' name='keep_date'";
	if (keep_date) {str += " checked"; }
	str += ">Date ";
	
	return(str);
}

function view_daily() { switch_time_frame("seawinds", "daily"); }
function view_weekly() { switch_time_frame("seawinds", "weekly"); }
function view_monthly() { switch_time_frame("seawinds", "monthly"); }


function switch_time_frame(inst, tym) {
	switch (inst) {
	case "qscat": url = "scatterometer"; break;
	default: url = inst
	}

	url += "_data_" + tym + ".html?"

	if (form.keep_rgn.checked)  { rgn = 1; } else { rgn = 0; }
	if (form.keep_size.checked) { syz = 1; } else { syz = 0; }
	if (form.keep_date.checked) { dayt = 1; } else { dayt = 0; }

	if (rgn) { url += "&rgn=" + region_key; }
	if (syz) { url += "&size=" + image_size; }

	if (dayt) {
		if (time_frame == "week") { week_to_date(); }
		url += "&year=" + year;
		url += "&month=" + month;
		if (day > 0) url += "&day=" + day;
		url += "&pass=" + pass;
	}

	url += "&keep=" + rgn + syz + dayt;

	window.location = url;
}


function view_instrument(inst) {
	switch (time_frame) {
	case "day": tym = "daily"; break;
	case "day3": tym = "3day"; break;
	default: tym = time_frame + 'ly'
	}
	switch_time_frame(inst, tym)
}


function week_to_date() {
	ymd = weekArray[wIdx];
	year =	ymd.substr(0,4) - 0;
	month =	ymd.substr(4,2) - 0;
	day = 	ymd.substr(6,2) - 0;
}


function date_to_week() {
	ymd = (year * 10000) + (month * 100) + day;
	
	for (i=1; i<=numwks; i++) {
		if (weekArray[i] >= ymd) { wIdx = i; return; }
	}
}


// -->
