<html>
<head>
<script>
//(1) Start by getting a numeric value representing TODAY that
// you can use for comparing dates. Final format: YYYYMMDD
var today = new Date();
var todayDay = today.getDate();
//NOTE: zero-padding of single-digit days essential for making this work.
if(todayDay < 10)
todayDay = '0' + todayDay;
var todayMonth = today.getMonth()+1;
//Regarding above: months are 0-based, but easier for us if 1-based.
// Ask about this if you don't understand.
//NOTE: zero-padding of single-digit months essential for making this work.
if(todayMonth < 10)
todayMonth = '0' + todayMonth;
var todayYear = today.getYear();
var todayString = '' + todayYear + todayMonth + todayDay;
//above: string concatenation
var todayVal = parseInt(todayString, 10); //YYYYMMDD version of today
//(2) Declare global variable myList.
var myList;
//(3) Called with BODY tag's onload event, starts date selection going.
function startBallRolling()
{
myList = document.forms[0].cboWeekEnd;
//To begin with, lowest index is 0
// highest index is document.forms[0].cboWeekEnd.options.length-1.
findClosest(0, document.forms[0].cboWeekEnd.options.length-1);
}//end startBallRolling()
//(4) Function looks at middle of drop-down list, then decides if it's
// in the right place. If not, it searches either the top or bottom
// half of the list (by calling itself with different parameters) and
// the process continues, searching smaller and smaller "halves" of the
// remaining list until it finds its mark. i.e., does a binary search.
function findClosest(low, high)
{
//Go to middle of list (binary search)
var halfwayIndex = Math.floor((low+high)/2);
var dateVal = getDateVal(myList.options[halfwayIndex].text);
if(dateVal == todayVal)
{
myList.selectedIndex = halfwayIndex; //Done.
}
else if(dateVal > todayVal)
{
if(halfwayIndex == 0) //start of list. Can't go lower, so...
{
myList.selectedIndex = 0; //Done.
}//end if
else
{
var newIndex = halfwayIndex - 1;
var newDateVal = getDateVal(myList.options[newIndex].text);
if(newDateVal > todayVal) //too far away. Search bottom half of list.
{
findClosest(low, halfwayIndex);
}//end if
else //we're where we need to be. Determine which is closer.
{
var choice = whichIsCloser(halfwayIndex, dateVal, newIndex, newDateVal, todayVal);
myList.selectedIndex = choice; //Done.
}//end else
}//end else
}//end else if
else //dateVal < todayVal
{
if(halfwayIndex == myList.options.length-1) //end of list. Can't go higher, so...
{
myList.selectedIndex = myList.options.length-1; //Done.
}//end if
else
{
var newIndex = halfwayIndex + 1;
var newDateVal = getDateVal(myList.options[newIndex].text);
if(newDateVal < todayVal) //too far away. Search top half of list.
{
findClosest(halfwayIndex, high);
}//end if
else //we're where we need to be. Determine which is closer.
{
var choice = whichIsCloser(halfwayIndex, dateVal, newIndex, newDateVal, todayVal);
myList.selectedIndex = choice; //Done.
}//end else
}//end else
}//end else
}//end findClosest(var, var)
//Returns YYYYMMDD int value of date sent in MM/DD/YYYY format
function getDateVal(optDate)
{
//optDate of form: MM/DD/YYYY.
var month = parseInt(optDate.substring(0, optDate.indexOf("/")), 10);
if(month < 10) //Treating month like an int
month = '0' + month; //Now treating month like a String. Crazy, huh?
optDate = optDate.substring(optDate.indexOf("/")+1);
//Now optDate of form: DD/YYYY
var day = parseInt(optDate.substring(0, optDate.indexOf("/")), 10);
if(day < 10)
day = '0' + day;
optDate = optDate.substring(optDate.indexOf("/")+1);
//Finally optDate of form: YYYY
var year = optDate;
return parseInt('' + year + month + day);
}//end getDateVal(var)
//Returns index1 or index2, based on whether date1 or date2 (respectively)
// is closer to the dateOfInterest
function whichIsCloser(index1, date1, index2, date2, dateOfInterest)
{
var compare1 = Math.abs(dateOfInterest - date1);
var compare2 = Math.abs(dateOfInterest - date2);
if(compare1 < compare2)
return index1;
else
return index2;
}//end whichIsCloser(var, var, var, var, var)
</script>
</head>
<body onload='startBallRolling();'>
<form>
<SELECT id=select17 style="WIDTH: 115px" size=1 name=cboWeekEnd>
<OPTION selected>9/4/2005</OPTION>
<OPTION>09/11/2005</OPTION>
<OPTION>9/18/2005</OPTION>
<OPTION>9/25/2005</OPTION>
<OPTION>10/2/2005</OPTION>
<OPTION>10/9/2005</OPTION>
<OPTION>10/16/2005</OPTION>
<OPTION>10/23/2005</OPTION>
<OPTION>10/30/2005</OPTION>
<OPTION>11/6/2005</OPTION>
<OPTION>11/13/2005</OPTION>
<OPTION>11/20/2005</OPTION>
<OPTION>11/27/2005</OPTION>
<OPTION>12/4/2005</OPTION>
<OPTION>12/11/2005</OPTION>
<OPTION>12/18/2005</OPTION>
<OPTION>12/25/2005</OPTION>
<OPTION>1/1/2006</OPTION>
<OPTION>1/8/2006</OPTION>
<OPTION>1/15/2006</OPTION>
<OPTION>1/22/2006</OPTION>
<OPTION>1/29/2006</OPTION>
<OPTION>2/5/2006</OPTION>
<OPTION>2/12/2006</OPTION>
<OPTION>2/19/2006</OPTION>
<OPTION>2/26/2006</OPTION>
<OPTION>3/5/2006</OPTION>
<OPTION>3/12/2006</OPTION>
<OPTION>3/19/2006</OPTION>
<OPTION>3/26/2006</OPTION>
<OPTION>4/2/2006</OPTION>
<OPTION>4/9/2006</OPTION>
</SELECT>
</form>
</body>
</html>