////////////////////
//
//    Filename: ezDateDrop.js
//
//    Description: A quick and easy solution for Combo Drop Down date selection.
//                 Just fill out a couple settings and paste the HTML code!
//
//    Created: 04-19-02
//    Last Modified: 11-02-04
//    Copyright (c) 2002-2004 James Delcamp
//    License: Free for personal use
//
//    Website: http://www.Tools4Affiliates.com
//    Support: http://www.Tools4Affiliates.com/support
//
//    Questions or Comments?
//    Please leave feedback on Planet-Source-Code.com
//    ALL Feedback is GREATLY appreciated! Thank you!
//
//    Compatible With: N6+ and IE5+
//
////////////////////

/*
////////////////////

 Note:
 You would only need set the variables to something other than '0' if you
 need to set a specific date. Otherwise, it will display the current date.

 Usage Header - Place this code within your HTML header
     <script type="text/javascript" src="ezDate_Drop.js"></script>


 Usage Body - Place this code within your HTML body:

     <script language="JavaScript" type="text/javascript">
	<!--
		setD = 0;  // The 'day' variable
		setM = 0;  // The 'month' variable
		setY = 0;  // The 'year' variable

		showDropDisplay(setD, setM, setY);

	//-->
     </script>

Please see the included example for a working page: ezDD_example.html

////////////////////
*/

/// Begin Manual Settings ///

/**
***	Basic Settings
**/

yearRange = 0;		// Set number of years to display before and after "Set" year (Required) - Leaving '0' will default to 10
minYear = 0;		// Set earliest year for drop down to display (Optional - Enter '0' for current year or 4 digit year - ie '2002')
maxYear = 0;		// Set maximum year for drop down to display (Optional - Enter '0' for no max or 4 digit year - ie '2010')


/**
***	Form/Element Settings
**/

useForm = 0;					// Set whether or not to add form tag - Enter '0' if ezDate_Drop will be placed within another form
frmName	= '';					// Set form name (optional)
frmClass = '';					// Set the 'Form' class (Optional - Use if your will use a CSS)
frmStyle = 'margin: 0px';			// Set the 'Form' style (Optional)
selClass = '';					// Set the 'Form Select' class (Optional - Use if your page is using CSS)
selStyle = 'border: 1px groove;color:#333365';	// Set the 'Form Select' style (Optional)
dSelect_name = 'myDay';				// Set the 'Form Select' name for days drop down
mSelect_name = 'myMonth';			// Set the 'Form Select' name for months drop down
ySelect_name = 'myYear';			// Set the 'Form Select' name for years drop down
imgPath = 'images/setToday.gif';			// Set the image path for 'Todays Date' - Leave blank if not using option or image
imgStyle = 'cursor:hand'			// Set the 'Image' style (Optional)

/// End Manual Settings ///



// Do NOT change anything below if you don't know what you're doing!!

// Checking and Setting the main drop down year range
if(yearRange == 0 || yearRange == '0') yearRange = 10;

function showDropDisplay(setDay, setMonth, setYear)
{
	var myForm, dDisplay, mDisplay, yDisplay, monthDays, i;
	var allMonths, setDrop, rangeStart, mnYear, mxYear;

// Set current date or preset date
//
// Check to see if we need to set current date

	if(setDay == '' || setMonth == '' || setYear == '')
	{
		d = new Date();
	}else{
		d = new Date(setYear, (setMonth - 1), setDay);
		setDrop = 1;
	}

	curDay = d.getDate();
	curMonth = d.getMonth() + 1;
	curYear = d.getFullYear();

// Checking and Setting year minimum limit
	mnYear = (minYear == 0 || minYear == '0') ? curYear : minYear;

// Checking and Setting year maximum limit
	mxYear = (maxYear == 0 || maxYear == '0') ? Math.abs(curYear) + 100 : maxYear;

// Verify our yearRange doesn't take a wrong turn 
	if((yearRange * 2) > (mxYear - mnYear)) yearRange = (mxYear - mnYear);
	mainRange = ((yearRange * 2) > (mxYear - mnYear)) ? (mxYear - mnYear) : (yearRange * 2);

// Need to adjust for year drop down range (uses Min/Max Settings)
	rangeStart = (setDrop != 1 && setYear <= (mnYear + yearRange)) ? mnYear : (setYear - yearRange);
	if(setDrop == 1 && setYear >= (mxYear - yearRange)) rangeStart = (mxYear - mainRange);
	if(rangeStart <= 0) rangeStart = (curYear - yearRange);

// Pulling number of days available in the 'curMonth'
	monthDays = checkDays(curMonth, curYear);
	allMonths = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

// Build Drop Down Code For Days
	dDisplay = '<select name="' + dSelect_name + '" class="' + selClass + '" style="' + selStyle + '">';

	for(i = 0; i < monthDays; i++)
	{
		selected = (i == (curDay - 1) ? 'selected' : '');
		dDisplay += '<option value="' + (i + 1) + '" ' + selected + '>' + (i + 1) + '</option>';
	}

	dDisplay += '</select>';


// Build Drop Down Code For Months
	mDisplay = '<select name="' + mSelect_name + '" class="' + selClass + '" style="' + selStyle + '" onChange="changeDropDisplay(0);">';

	for(i = 0; i < 12; i++)
	{
		selected = (i == (curMonth - 1) ? 'selected' : '');
		mDisplay += '<option value="' + (i + 1) + '" ' + selected + '>' + allMonths[i] + '</option>';
	}

	mDisplay += '</select>';


// Building Drop Down Code For Years
	yDisplay = '<select name="' + ySelect_name + '" class="' + selClass + '" style="' + selStyle + '" onChange="changeDropDisplay(1);">';

	for(i = 0; i < mainRange + 1; i++)
	{
		selected = (rangeStart == curYear ? 'selected' : '');
		yDisplay += '<option value="' + rangeStart + '" ' + selected + '>' + rangeStart + '</option>';
		rangeStart++;
	}

	yDisplay += '</select>';

	if(imgPath != '') setImg = '&nbsp; <img style="' + imgStyle + '" onClick="showToday()" src="' + imgPath + '" align="absmiddle" alt="Click To Set Todays Date" border="0">';

// Set Form If Needed
	if(useForm == 1)
	{
		myForm = '<form name="' + frmName + '" method="POST" class="' + frmClass + '" style="' + frmStyle + '">' + dDisplay + '  ' + mDisplay + ' ' + yDisplay + setImg + '</form>';
	}else{
		myForm = dDisplay + '  ' + mDisplay + ' ' + yDisplay + setImg;
	}

// Displaying Drop Down And/Or Form
	document.write(myForm);

	return;
}



function checkDays(newMonth, newYear)
{
	var daysInMonth, isLeap;

// Set array for num days in each month
	daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

	if(newMonth != 2)
	{
// If not February, send back total days in month
		return daysInMonth[newMonth-1];
	}else{
// Month selected is February - Determine If Leap Year
		isLeap = (newYear % 4 == 0 && newYear % 100 != 0) || (newYear % 400 == 0) ? 29 : 28;
		return isLeap;
	}
}


function changeDropDisplay(chkYear)
{
	var xd = document.getElementById(dSelect_name);
	var xm = document.getElementById(mSelect_name);
	var xy = document.getElementById(ySelect_name);

	var monthDays, selYear, rangeStart, rangeEnd, mnYear, mxYear;

	monthDays = checkDays(xm.options[xm.selectedIndex].value, xy.options[xy.selectedIndex].value);

// Checking and Setting year minimum
	if(minYear == 0 || minYear == '0')
	{
		d = new Date();
		mnYear = d.getFullYear();
	}else{
		mnYear = minYear;
	}

// Re-adjust day drop down to match days in selected month
	xd.options.length = monthDays;

	for(i = 0; i < monthDays; i++)
	{
		xd.options[i].value = (i + 1);
		xd.options[i].text = (i + 1);
	}

// Re-adjust year drop down to move forward/back from the year selected
	if(chkYear == 1)
	{

		selYear = xy.options[xy.selectedIndex].value;

// Checking and Setting year maximum limit
		mxYear = (maxYear == 0 || maxYear == '0') ? Math.abs(selYear) + 100 : maxYear;

// Need to adjust for year drop down range (uses Min/Max Settings)
		rangeStart = (selYear <= (mnYear + yearRange)) ? mnYear : (selYear - yearRange);
		if(selYear >= (mxYear - yearRange)) rangeStart = (mxYear - mainRange);

		for(i = 0; i < mainRange + 1; i++)
		{
			xy.options[i].value = rangeStart;
			xy.options[i].text = rangeStart;
			rangeStart++;
		}
		xy.options.value = selYear;
	}
	return;
}


function showToday()
{
	var xd = document.getElementById(dSelect_name);
	var xm = document.getElementById(mSelect_name);
	var xy = document.getElementById(ySelect_name);

// Pull today's date
	d = new Date();
	tDay = d.getDate();
	tMonth = d.getMonth() + 1;
	tYear = d.getFullYear();

	var monthDays, selYear, rangeStart, rangeEnd, mnYear, mxYear;

	monthDays = checkDays(tMonth, tYear);

// Re-adjust day drop down to match days in selected month
	xd.options.length = monthDays;

	for(i = 0; i < monthDays; i++)
	{
		xd.options[i].value = (i + 1);
		xd.options[i].text = (i + 1);
	}

// Checking and Setting year minimum
	mnYear = (minYear == 0 || minYear == '0') ? d.getFullYear() : minYear;

// Checking and Setting year maximum limit
	mxYear = (maxYear == 0 || maxYear == '0') ? Math.abs(tYear) + 100 : maxYear;

// Verify our yearRange doesn't take a wrong turn 
	if((yearRange * 2) > (mxYear - mnYear)) yearRange = (mxYear - mnYear);
	mainRange = ((yearRange * 2) > (mxYear - mnYear)) ? (mxYear - mnYear) : (yearRange * 2);

// Need to adjust for year drop down range (uses Min/Max Settings)
	rangeStart = (tYear <= (mnYear + yearRange)) ? mnYear : (tYear - yearRange);
	if(tYear >= (mxYear - yearRange)) rangeStart = (mxYear - mainRange);

	for(i = 0; i < mainRange + 1; i++)
	{
		xy.options[i].value = rangeStart;
		xy.options[i].text = rangeStart;
		rangeStart++;
	}
	xd.options.value = tDay;
	xm.options.value = tMonth;
	xy.options.value = tYear;

	return;
}