
//variables
var day = 24*60*60*1000;
var BMonth = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
var now=new Date();
var curmonth=now.getMonth();
var curday=now.getDate();
var curyear= now.getFullYear();
var curdate=BMonth[curmonth]+" "+curday+", "+curyear;
	//adjustment for FullYear Netscape
	if(navigator.appName=="Netscape"){
	curyear = curyear + 1900;
	}

// find next millennial

	function calMilDate(mil,ageindays){
	futuredate=now.getTime()+((mil-ageindays)*day);
	futuredate=new Date(futuredate);
		// handle request using today's date, ie ageindays=0
		if(Date.parse(now)==Date.parse(futuredate)){
		futuredate=(Date.parse(futuredate) + (day*1000));
		futuredate=new Date(futuredate);
		mil=mil+1000;
		}
	//convert future date month from integer to string name

	//futuredate= new Date(futuredate.getMonth()+" "+futuredate.getDate()+", "+futuredate.getFullYear());
	futuredate = (BMonth[futuredate.getMonth()]) + " " + futuredate.getDate() + ", " + futuredate.getFullYear();
	document.getElementById('mdate').value=futuredate;
	document.getElementById('nextmil').value=(mil/1000)+",000 days old";
	}

	function calResults(){
	// get birthday value segments from dropdowns
	bmonth=document.getElementById('bmonth').value;
	bdate=document.getElementById('bdate').value;
	byear=document.getElementById('byear').value;
	bday = new Date(bmonth+" "+bdate+", "+byear);
	// convert to milliseconds at noon
	bday = bday.setTime(bday)+(12*60*60*1000);

	today = new Date(now.getFullYear(),now.getMonth(),now.getDate());
	// convert to milliseconds at noon
	today = today.setTime(today)+(12*60*60*1000);
	//calculate age in days from noon to noon
	currage=Math.round((today - bday)/day);

	document.getElementById('age').value=currage;
	findnextmil=Math.ceil(currage/1000);

	//handle a date in the future, not born yet
	if(findnextmil!=0){
	calMilDate((findnextmil*1000),currage);
	}else{
	calMilDate((1*1000),currage);
	}

	document.getElementById('resultsdisplay').style.visibility="visible";
	}

	// update the number of day shown in day dropdown, handles Leap Years
	function change () {
	var el = document.getElementById('bmonth');
	var mel = document.getElementById('bdate');
	var yel = document.getElementById('byear');
	var d = new Date(yel.value,el.selectedIndex + 1, 0);
	mel.options.length = 0;
		for(var i = 1; i <= d.getDate(); i++ ) {
		mel.options[mel.options.length] = new Option(i,i);
		}
	}


	function birthMonth(){
	document.write("<select id='bmonth' onclick='change()'>");
		for (m in BMonth) {
		document.write("<option value='"+BMonth[m]+"'>"+ BMonth[m] +"</option> ");
		}
	document.write("</select>");
	}

	function birthDate(month){
	document.write("<select id='bdate'>");
		for (i=1;i<32;i++) {
		document.write("<option value='"+ i +"'>"+ i +"</option> ");
		}
	document.write("</select>");

	}

	function birthYear(){
	document.write("<select id='byear' onchange='change()'>");
	startyear=now.getFullYear()+2;
		for (i=startyear;i>startyear-102;i--) {
			if(i!=now.getFullYear()){
			document.write("<option value='"+ i +"'>"+ i +"</option> ");
			}else{
			document.write("<option value='"+ i +"' selected>"+ i +"</option> ");
			}
		}
	document.write("</select>");

	}

	function milList(){
	document.write("<select id='millist' style='text-align:center;'>");
		for (i=40;i>0;i--) {
			var displayval=i+",000";
			document.write("<option value='"+ i*1000 +"'>"+ displayval +"&nbsp;&nbsp; </option> ");
		}
	document.write("</select>");

	}

//find date for a selected future millennial milestone

	function calResults2(){
	bmonth=document.getElementById('bmonth').value;
	bdate=document.getElementById('bdate').value;
	byear=document.getElementById('byear').value;
	bday = new Date(bmonth+","+bdate+","+byear);
	// convert to milliseconds at noon
	bday = bday.setTime(bday)+(12*60*60*1000);

	var DaysToAdd=(document.getElementById("millist").value);

	var targetdate=new Date(bday);
	var targettimems=targetdate.getTime()+((DaysToAdd*day));
	targetdate.setTime(targettimems);

	var displayeddate=new Date(targetdate);
	displayeddate = (BMonth[displayeddate.getMonth()])+ " " + displayeddate.getDate() + ", " + displayeddate.getFullYear();

	document.getElementById("mdate2").value=displayeddate;

	//convert DaysToAdd to int, then reformat to insert comma
	DaysToAdd = parseInt(DaysToAdd);
	DaysToAdd = (DaysToAdd/1000) + ",000";

	document.getElementById("nextmil2").value=DaysToAdd;
	document.getElementById("resultsdisplay2").style.visibility="visible";
}



