//This javascript dynamically populates the drop down menus for archived
//drought index selection and displays the selected file.
//--script history--
//July 8, 2005 began script
//July 11, 2005 completed date structure
//July 13, 2005 completed map display and web implementation
//edited: December 1, 2005 // removed unused data type

var d = new Date();

var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

var dIndex = new Array("cmi", "pdsi", "field_capacity", "z_index");
var filePrefix = new Array("cmi", "pdsi", "field", "zindex");

var ext = ".png"; //extenstion for img files

var buttonActive = 0;

function getDaysInMonth(month, yr)
{

	if(month == 3 || month == 5 || month == 8 || month == 10)
		return 30;	
	if(month == 1)
	{
		if( checkLeapYear(yr) )
			return 29;
		else
			return 28;
	}
	return 31;
}

function checkLeapYear(year)
{
	
	if ((year/4)   != Math.floor(year/4))
		return false;
    if ((year/100) != Math.floor(year/100))
		return true;
    if ((year/400) != Math.floor(year/400))
		return false;
    
	return true;
}

function getSats(yy, mm, dd)
{
	var c = new Date(yy,mm,dd);
	var dayOfWeek = c.getDay();

	return dayOfWeek;
}

//populate the days drop down list on the web page
function setOptions()
{
	var cdate; //current date in loop
	var y;
	var selectBox = document.selectFile.DFiles;
	y = document.selectFile.DYear.selectedIndex;
	yr = document.selectFile.DYear.options[y].value;

	if(yr != "null")
		buttonActive = 1;
	else
		buttonActive = 0;
	
	selectBox.options.length = 0;  //removes all from drop down box
	
	//the below loop populates the days drop down box on the web with
	//all saturdays in selected year
	for(x=0; x <= 11; x++)
	{
		for(j=1; j <= getDaysInMonth(x, yr); j++)
		{
			if( (cdate = new Date(yr, x, j)) >= d) //break if loop date >= todays date
				break;

				dx = getSats(yr, x, j); //sets dx to current day of week (0 to 6)
				if(dx == 6)             //if saturday, add an option to the drop down box
				{
					var ds = getDateString(yr, x, j);  //creates a string var with the date
					selectBox.options[selectBox.options.length] = new Option(months[x] + ' ' + j, ds);
				}
		}
	}
	
}

//build a URL string and pass it to the web browser to display file for
//selected date
function viewFile()
{
	var i, ival; //drought index
	var t, tval; //type (map or data)
	var y, yval; //year
	var m, mval; //month
	var newWindow;

	i = document.selectFile.DIndex.selectedIndex;
	ival = dIndex[i];

	y = document.selectFile.DFiles.selectedIndex;
	yval = document.selectFile.DFiles.options[y].value;

	//document.selectFile.tBox.value = '../' + dIndex[i] + '/' + dIndex[i] + yval + ext;

	if(buttonActive != 0)
	{
			newWindow = window.open(dIndex[i] + '/' + filePrefix[i] + yval + ext);
	}
}


function getDateString(y, m, d)
{

		m += 1; //this sets month scale to 1 to 12
		var yrString = y.toString();
		var sYrString = yrString.substring(2,4);
		
		if(m<10)
			var mString = '0' + m.toString();
		else
			var mString = m.toString();

		if(d<10)
			var dString = '0' + d.toString();
		else
			var dString = d.toString();

		var dateString = new String(sYrString + mString + dString);

		return dateString;
}