// This is used by cliwatch/watch.htm
// updated 1/27/2011 to wait until noon on first day of month to change recent maps and seasonal maps
// updated 2/4/2011 to wait until tenth of month to change Monthly Climate Overviews menu

var now = new Date(); // date for recent maps and seasonal maps
var less12 = new Date(now.getTime());
less12.setHours(less12.getHours()-12);
now = less12;
		 
year0 = now.getYear();
//alert("year0="+year0);

if (year0 < 1000)
	year0 = year0 + 1900;
	
month0 = now.getMonth();
date0 = now.getDate()-1;
 
month = month0;
year = year0;
date = date0;

month--;  // display last month and older data
if (date<0)  // only until past 10th of this month
	month--;

if (month<0) {
	month = month + 12;
	year--;
}

var tdy = new Date(); // date for Midwest Climate Overviews menu
var less10days = new Date(tdy.getTime());
less10days.setHours(less10days.getHours()-120);
tdy = less10days;
		 
year = tdy.getYear();
//alert("year0="+year0);

if (year < 1000)
	year = year + 1900;
	
monthCO = tdy.getMonth();
dateCO = tdy.getDate()-1;
 
monthCO--;  // display last month and older data
if (dateCO<0)  // only until past 10th of this month
	monthCO--;

if (monthCO<0) {
	monthCO = monthCO + 12;
	year--;
}
		 

//alert("year="+year+" month="+month+" date="+date);
// for recent maps and seasonal maps
Years = new Array(); 
Years.length = 0;
for(count = 0; count < year-1999; count++)
	Years[count] = 2000 + count;

Months = new Array("January","February","March","April","May","June","July",
 "August","September","October","November","December");      


// browser specific
if (navigator.appName == "Netscape") {
	mnhili = document.monthlyhighlights;
	rcntmaps = document.recentmaps;
} else {
	mnhili = document.all.monthlyhighlights;
	rcntmaps = document.all.recentmaps;
}
		
initializeForm()
//getlink()

function initializeForm() {

	// fill first form
	mnhili.Year.length = 0;
//alert("Years.length="+Years.length);
	for(count = 0; count < Years.length; count++) {
		if (count == year - 2000) {  // set current year as default
			mnhili.Year.options[count] = new Option(Years[count],"",1,1);
			mnhili.Year.options[count].selected = 1;
//alert("default Year="+Years[count]);
		} else {
			mnhili.Year.options[count] = new Option(Years[count],"",0,0);
			mnhili.Year.options[count].selected = 0;
//alert("Year="+Years[count]);
		}
	}
//alert("mnhili.Year.selectedIndex="+mnhili.Year.selectedIndex);
	fillMonths();
	
	// fill second form
	rcntmaps.rcntmn.length = 0;
	for(count = 0; count < 6; count++) {
		mm = month0 - 1 - count;
		yy = year0;
		if ( mm < 0 ) {
			mm = mm + 12;
			yy = yy - 1;
		}
		if (count == 0)
			rcntmaps.rcntmn.options[count] = new Option(Months[mm]+" "+yy,"",1,1)
		else
			rcntmaps.rcntmn.options[count] = new Option(Months[mm]+" "+yy,"")
	}
}

function fillMonths()	{
	mnhili.Month.length=0;
//alert("Months.length="+Months.length);
//alert("mnhili.Year.selectedIndex="+mnhili.Year.selectedIndex);
	for(count = 0; count < Months.length; count++) {
		if ( mnhili.Year.selectedIndex == year - 2000)
			if ( monthCO == count) {  // show recent month as default for current year
				mnhili.Month.options[count] = new Option(Months[count],"",1,1);
				mnhili.Month.options[count].selected = 1;
				break;
			} else
				mnhili.Month.options[count] = new Option(Months[count],"");
		else
			if ( count == 0 ) { // show first month as default for past years
				mnhili.Month.options[count] = new Option(Months[count],"",1,1);
				mnhili.Month.options[count].selected = 1;
			} else
				mnhili.Month.options[count] = new Option(Months[count],"");
	}
}
		
function getlink()
{
	var linkot;
	
	if (mnhili.Year.selectedIndex<10)
		yy = "0" + mnhili.Year.selectedIndex;
	else
		yy = mnhili.Year.selectedIndex;
		
	if (mnhili.Month.selectedIndex+1<10)
		mm = "0" + (mnhili.Month.selectedIndex + 1);
	else
		mm = mnhili.Month.selectedIndex + 1;
			
	linkto = ""+yy+mm+"/climwatch."+yy+mm+".htm";

	return linkto;
}


function getlink2()
{
	var linkto;
	
	count = rcntmaps.rcntmn.selectedIndex;
	
	mm = month0 - 1 - count;
	yy = year0;
	if ( mm < 0 ) {
		mm = mm + 12;
		yy = yy - 1;
	}

	yy = yy - 2000;
	if (yy<10)
		yy = "0" + yy;
		
	if (mm+1<10)
		mm = "0" + (mm + 1);
	else
		mm = mm + 1;
		
	linkto = "months/m"+mm+"_"+yy;
	linkto = linkto + "." + rcntmaps.datatype.options[rcntmaps.datatype.selectedIndex].value + ".png";
				
	return linkto;
}

function lastSeason() {
	var now2 = new Date();
	thisMonth = now2.getMonth();
	if (thisMonth == 11 || thisMonth == 0 || thisMonth == 1) {
		document.seasonalmaps.season.options.selectedIndex = 3; }
	if (thisMonth == 2 || thisMonth == 3 || thisMonth == 4) {
		document.seasonalmaps.season.options.selectedIndex = 0; }
	if (thisMonth == 5 || thisMonth == 6 || thisMonth == 7) {
		document.seasonalmaps.season.options.selectedIndex = 1; }
	if (thisMonth == 8 || thisMonth == 9 || thisMonth == 10) {
		document.seasonalmaps.season.options.selectedIndex = 2; }
}

function goseasonalmaps() {

	var theURL;

	pickseason=document.seasonalmaps.season.options[document.seasonalmaps.season.selectedIndex].value;

	dat=document.seasonalmaps.datatype.options[document.seasonalmaps.datatype.selectedIndex].value;

	theURL="seasons/"+pickseason+"."+dat+".png";	

	return theURL;

}


