// MyDatePicker...   You are free to use this
// script. Please keep this header info in tact.
// Erich Knaak, erichk@earthlink.net
// 01.10.2001: ///o-o\\\ organized & modified
// 01.10.2002: \/\/ick completely re-written, added day & checking against dealer hours

var futureDays; // how many days to add
var hoursArray;

function renderDateSelector( future,range ){
	futureDays = (future != null)?future:0;
	range = (range != null)?range:14;

	today      = new Date;
	thisYear   = today.getYear();
	thisMonth  = today.getMonth();
	thisDate   = today.getDate();
	thisDay   = today.getDay();

	myYear = new Array();
	myMonth = new Array();
	myDate = new Array();
	myDay = new Array();

	dayName = new Array('Sun','Mon','Tues','Wed','Thurs','Fri','Sat');
	monthName = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
	monthMax = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

	// handle leap year
	if ((thisMonth == 1) && ((thisYear / 4) == parseInt(thisYear / 4))) { monthMax[1] = 29 };

	// set up day of the week
	thisDay = ((thisDay + futureDays) % 7);

	// determine start date
	if ((thisDate + futureDays) > monthMax[thisMonth]) {
		thisDate = ((thisDate + futureDays) - monthMax[thisMonth]);
		if (thisMonth == 11) {
			thisMonth = 0;
			thisYear++;
		}
		else { thisMonth++; }
	}
	else { thisDate = (thisDate + futureDays); }

	// load arrays with date data
	myYear[0]  = thisYear;
	myMonth[0] = thisMonth;
	myDate[0]  = thisDate;
	myDay[0]  = thisDay;

	// determine proper year, month, date & day to put into each array element
	for (i = 1; i < range; i++) {
		if (myDate[i-1] >= monthMax[myMonth[i-1]]) { // month end?
			myDate[i] = 1;
			if (myMonth[i-1] == 11) { // manage end of year
				myMonth[i] = 0;
        myYear[i] = (myYear[i-1] + 1);
			}
			else { // manage end of month
				myMonth[i] = (myMonth[i-1] + 1);
				myYear[i] = myYear[i-1];
			}
		}
		else { // normal increment
			myDate[i] = (myDate[i-1] + 1);
			myMonth[i] = myMonth[i-1];
			myYear[i] = myYear[i-1];
		}
		myDay[i] = ((thisDay + i) % 7);
	}
	writeToPage();
}

function writeToPage() {
	for (i = 0; i < myDay.length; i++) {
		if ((hoursArray[myDay[i]].toLowerCase().search(/\d/) > -1) && (hoursArray[myDay[i]])) {
			writeoption(dayName[myDay[i]],monthName[myMonth[i]],myDate[i],myYear[i]);
		}
/*
		myDate[i] = "" + myDate[i];
		if (myDate[i].length == 1) {
			myDate[i] = '0' + myDate[i];
		}
*/
	}
}

function writeoption(optday,optmonth,optdate,optyear) {
	document.write('<option value=\"'+optday+' '+optmonth+' '+optdate+'\">');
	document.writeln(optday+' '+optmonth+' '+optdate+'</option>');
}


function test(){
	// testing variable values
	// delete this section to use script

	document.write("<!--<B> Testing variable values </B> <BR>");
	document.write("today = " + today + "<BR>");
	document.write("thisYear = " + thisYear + "<BR>");
	document.write("thisMonth = " + thisMonth + "<BR>");
	document.write("month[thisMonth] = " + month[thisMonth] + "<BR>");
	document.write("thisDate = " + thisDate + "<BR>");
	document.write("maxDays = " + maxDays + "<BR>");
	document.write("<BR>");

	document.write("<B> Date values to choose from </B> <BR>");
	for(k = 0 ; k < 14 ; k++){
		document.write(myDate[k] + "-" + month[myMonth[k]] + "-" + myYear[k] + "<BR>");
	}
	document.write("-->");
}
