/// copyright 2003 Maurici Carbo Jordi for Math Cats (www.mathcats.com)
/// All rights reserved. This script may not be reproduced. 
/// Questions? Contact Wendy Petti of Math Cats (wpetti@mathcats.com) 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///MIN_YEAR MUST BE UPDATED EACH YEAR ONLY FOR ALERT PURPOSE////////
MIN_YEAR=2003;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function bodyobject()
{
this.run=0;
this.birthtime=new Object();
this.calculatebutton=1;
this.cakebutton=0;
this.cakewnd=new Object();
this.showcake=0;
this.IsyourBirthday=0;
this.Cakesize=0;
}
function dateobject(second,minute,hour,day,month,year)
{
this.m_second=second;
this.m_minute=minute;
this.m_hour=hour;
this.m_day=day;
this.m_month=month;
this.m_year=year;

this.m_internal_months=0;
this.m_internal_days=0;

for (ycount=1582; ycount<this.m_year; ycount++)
  {
  if (leapyear(ycount)==1)
    {this.m_internal_days+=366;}
  else
    {this.m_internal_days+=365;}
  this.m_internal_years++;
  this.m_internal_months+=12;
  }
for (mcount=1; mcount<this.m_month; mcount++)
  {
  this.m_internal_months++;
  this.m_internal_days+=longmonth(mcount,this.m_year);
  } 
for (dcount=1;dcount<this.m_day;dcount++)
  {
  this.m_internal_days++;
  }
this.m_internal_seconds=this.m_hour*3600+this.m_minute*60+this.m_second;
}

function leapyear(year)
{
if(year%4==0)
   {
   if ((year%100==0)&&(year%400!=0))
   {return 0;}
   else
   {return 1;}		
   }
return 0;
}

function subtract_years(a,b)
{
if(a.m_month<b.m_month)
  {
  return (a.m_year-b.m_year-1);
  }
else
  {
  if(a.m_month==b.m_month)
    {
    if(a.m_day<b.m_day)
      {
      return (a.m_year-b.m_year-1);
      }
    else
      {
      return (a.m_year-b.m_year);
      }
    }
  }
return (a.m_year-b.m_year);
}

function subtract_months(a,b)
{
if(a.m_day<b.m_day)
  {
  return (a.m_internal_months-b.m_internal_months-1);
  }
return (a.m_internal_months-b.m_internal_months);
}

function subtract_weeks(a,b)
{
return (Math.floor((a.m_internal_days-b.m_internal_days)/7));
}

function subtract_days(a,b)
{
return (a.m_internal_days-b.m_internal_days);
}
function subtract_hours(a,b)
{
ahours=Math.floor(a.m_internal_seconds/3600);
bhours=Math.floor(b.m_internal_seconds/3600);

if(ahours<bhours)
  {
  return (ahours-bhours+25+((a.m_internal_days-b.m_internal_days)-1)*24);
  }
return (ahours-bhours+(a.m_internal_days-b.m_internal_days)*24);
}

function subtract_minutes(a,b)
{
aminutes=Math.floor(a.m_internal_seconds/60);
bminutes=Math.floor(b.m_internal_seconds/60);

if(aminutes<bminutes)
  {
  return (aminutes-bminutes+1441+((a.m_internal_days-b.m_internal_days)-1)*1440);
  }
return (aminutes-bminutes+(a.m_internal_days-b.m_internal_days)*1440);
}

function subtract_seconds(a,b)
{
aseconds=Math.floor(a.m_internal_seconds);
bseconds=Math.floor(b.m_internal_seconds);

if(aseconds<bseconds)
  {
  return (aseconds-bseconds+86401+((a.m_internal_days-b.m_internal_days)-1)*86400);
  }
return (aseconds-bseconds+(a.m_internal_days-b.m_internal_days)*86400);
}



function longmonth(month,year)
{
if ((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12))
   {return 31;}
else
  {
  if (month==2)
    {
    if(leapyear(year)==1)     
      {return 29;}
    else
      {return 28;}
    }
  else
    {
    return 30;
    }  
  }	
}


function heart()
{
cal();
}
function init()
{
flyngtdy=new Date();
flyngyr=parseInt(flyngtdy.getFullYear());
if(flyngyr<MIN_YEAR){alert("Your computer is not updated\nActual year is:"+flyngyr+"  ??????");}
true_bodyobject=new bodyobject();
validatebutton();
}

function intro()
{
true_bodyobject.run=1;
byear=parseInt(1582+document.agecalc.fyear.selectedIndex);
bmonth=document.agecalc.fmonth.selectedIndex +1;
bdate=document.agecalc.fdate.selectedIndex +1;
bsecond=0;
bminute=document.agecalc.fminute.selectedIndex;
bhour=document.agecalc.fhour.selectedIndex;
true_bodyobject.birthtime=new dateobject(bsecond,bminute,bhour,bdate,bmonth,byear);
}

function invalidate()
{
true_bodyobject.run=0;
byear=parseInt(1582+document.agecalc.fyear.selectedIndex);
bmonth=document.agecalc.fmonth.selectedIndex +1;
bdate=document.agecalc.fdate.selectedIndex +1;
bsecond=0;
bminute=document.agecalc.fminute.selectedIndex;
bhour=document.agecalc.fhour.selectedIndex;
local_birthtime=new dateobject(bsecond,bminute,bhour,bdate,bmonth,byear);
daynumber=local_birthtime.m_internal_days;
day_of_week=((daynumber+5)%7);
textdayofweek="";

if(day_of_week==1){textdayofweek="Monday.";}
if(day_of_week==2){textdayofweek="Tuesday.";}
if(day_of_week==3){textdayofweek="Wednesday.";}
if(day_of_week==4){textdayofweek="Thursday.";}
if(day_of_week==5){textdayofweek="Friday.";}
if(day_of_week==6){textdayofweek="Saturday.";}
if(day_of_week==0){textdayofweek="Sunday.";}

document.agecalc.dayofweek.value="You were born on a "+textdayofweek;
true_bodyobject.calculatebutton=1;
true_bodyobject.cakebutton=0;


if(((bmonth==2)||(bmonth==4)||(bmonth==6)||(bmonth==9)||(bmonth==11))&&(bdate==31))
	{
	document.agecalc.dayofweek.value="This month never has 31 days.";
	true_bodyobject.calculatebutton=0;
    true_bodyobject.cakebutton=0;
	}   
if((bmonth==2)&&(bdate==30))
	{
	document.agecalc.dayofweek.value="February never has 30 days.";
	true_bodyobject.calculatebutton=0;
    true_bodyobject.cakebutton=0;
	}   
if(((bmonth==2)&&(bdate==29))&&(leapyear(byear)==0))
	{
	document.agecalc.dayofweek.value="February 29 did not occur in this year.";
	true_bodyobject.calculatebutton=0;
    true_bodyobject.cakebutton=0;
	}   


validatebutton();
validatecake();
invalidatepopup();
}
function validatebutton()
	{
	if(true_bodyobject.calculatebutton==1) 
		{
		document.agecalc.calculatebutton.value="calculate age";
		}
	else
	    {
		document.agecalc.calculatebutton.value=" ";
	    }
	}
function validatecake()
	{
	if(true_bodyobject.cakebutton==1) 
		{
		document.agecalc.showcakebutton.value="";
		document.agecalc.showcakebutton.text="";
		}
	else
	    {
		document.agecalc.showcakebutton.value="";
		document.agecalc.showcakebutton.text="";
	    }
	}

function invalidatepopup()
{
if(true_bodyobject.showcake==1)
	{
	if(!true_bodyobject.cakewnd.closed)
		{
		true_bodyobject.cakewnd.close();
		true_bodyobject.showcake=0;
		}
	else
		{
		true_bodyobject.showcake=0;
		}
	}

}


function cal()
{

if ((true_bodyobject.run==1)&&(true_bodyobject.calculatebutton==1))
  {
  today=new Date();
  ayear=today.getFullYear();
  amonth=today.getMonth();
  amonth++;
  amnday=today.getDate();
  amnhour=today.getHours();
  amin=today.getMinutes();
  asec=today.getSeconds();
 
  actualtime=new dateobject(asec,amin,amnhour,amnday,amonth,ayear);
  
  elapsedyears= subtract_years(actualtime,true_bodyobject.birthtime);
  elapsedmonths= subtract_months(actualtime,true_bodyobject.birthtime);
  elapsedweeks=subtract_weeks(actualtime,true_bodyobject.birthtime);
  elapseddays= subtract_days(actualtime,true_bodyobject.birthtime);
  elapsedhours=subtract_hours(actualtime,true_bodyobject.birthtime);
  elapsedminutes=subtract_minutes(actualtime,true_bodyobject.birthtime);
  elapsedseconds= subtract_seconds(actualtime,true_bodyobject.birthtime);

  if(Math.abs(elapsedyears)==1){document.agecalc.result.value=""+elapsedyears;}
  else{document.agecalc.result.value=""+commatized(elapsedyears);}

  if(Math.abs(elapsedmonths)==1){document.agecalc.result.value+="."+commatized(elapsedmonths-(elapsedyears*12))+" years\n";} 
  else{document.agecalc.result.value+="."+commatized(elapsedmonths-(elapsedyears*12))+" years\n";}

  /*if(Math.abs(elapsedweeks)==1){document.agecalc.result.value+="    or  "+commatized(elapsedweeks)+" week old\n";} 
  else{document.agecalc.result.value+="    or  "+commatized(elapsedweeks)+" weeks old\n";}
  if(Math.abs(elapseddays)==1){document.agecalc.result.value+="    or  "+commatized(elapseddays)+" day old\n";} 
  else{document.agecalc.result.value+="    or  "+commatized(elapseddays)+" days old\n";}
  if(Math.abs(elapsedhours)==1){document.agecalc.result.value+="    or  "+commatized(elapsedhours)+" hour old\n";} 
  else{document.agecalc.result.value+="    or  "+commatized(elapsedhours)+" hours old\n";}
  if(Math.abs(elapsedminutes)==1){document.agecalc.result.value+="    or "+commatized(elapsedminutes)+" minute old\n";}  
  else{document.agecalc.result.value+="    or  "+commatized(elapsedminutes)+" minutes old\n";}
  if(Math.abs(elapsedseconds)==1){document.agecalc.result.value+="    or "+commatized(elapsedseconds)+" second old\n";}  
  else{document.agecalc.result.value+="    or  "+commatized(elapsedseconds)+" seconds old\n";}*/
  
 if((actualtime.m_month==true_bodyobject.birthtime.m_month)&&(actualtime.m_day==true_bodyobject.birthtime.m_day))
 	{
 	document.agecalc.result.value+="\nToday is your birthday!"; 
	/*true_bodyobject.IsyourBirthday=1;
	true_bodyobject.Cakesize=elapsedyears;
	true_bodyobject.cakebutton=1;
	showmycake();
	validatecake();*/
 	}
else
	{ 	
 	document.agecalc.result.value+="\nYour next birthday is in: \n"; 
	/*true_bodyobject.IsyourBirthday=0;
	true_bodyobject.cakebutton=0;
	validatecake();*/
	if((actualtime.m_month< true_bodyobject.birthtime.m_month)||((actualtime.m_month== true_bodyobject.birthtime.m_month)&&(actualtime.m_day< true_bodyobject.birthtime.m_day)))
		{
        fbsec=0;
        fbmin=true_bodyobject.birthtime.m_minute;
        fbhour=true_bodyobject.birthtime.m_hour;
        fbday=true_bodyobject.birthtime.m_day;
        fbmonth=true_bodyobject.birthtime.m_month;
        fbyear=actualtime.m_year;
        futurebirthday=new dateobject(fbsec,fbmin,fbhour,fbday,fbmonth,fbyear);
        tstb=subtract_seconds(futurebirthday,actualtime);
        daystobirthday=Math.floor(tstb/86400); 
        hourstobirthday=Math.floor((tstb-daystobirthday*86400)/3600);
        minutestobirthday=Math.floor((tstb-daystobirthday*86400-hourstobirthday*3600)/60);
        secondstobirthday=Math.floor(tstb-daystobirthday*86400-hourstobirthday*3600-minutestobirthday*60)

		document.agecalc.result.value+=daystobirthday;
		if(daystobirthday==1)
			{
			document.agecalc.result.value+=" day ";
			}
		else
			{
			document.agecalc.result.value+=" days ";
			}
		document.agecalc.result.value+=hourstobirthday;
		if(hourstobirthday==1)
			{
			document.agecalc.result.value+=" hr ";
			}
		else
			{
			document.agecalc.result.value+=" hrs ";
			}
		document.agecalc.result.value+=minutestobirthday;
		if(minutestobirthday==1)
			{
			document.agecalc.result.value+=" min ";
			}
		else
			{
			document.agecalc.result.value+=" mins ";
			}
		document.agecalc.result.value+=secondstobirthday;
		if(secondstobirthday==1)
			{
			document.agecalc.result.value+=" sec. ";
			}
		else
			{
			document.agecalc.result.value+=" secs. ";
			}
		}
    else
    	{
        fbsec=0;
        fbmin=true_bodyobject.birthtime.m_minute;
        fbhour=true_bodyobject.birthtime.m_hour;
        fbday=true_bodyobject.birthtime.m_day;
        fbmonth=true_bodyobject.birthtime.m_month;
        fbyear=actualtime.m_year+1;
        futurebirthday=new dateobject(fbsec,fbmin,fbhour,fbday,fbmonth,fbyear);
        tstb=subtract_seconds(futurebirthday,actualtime);
        daystobirthday=Math.floor(tstb/86400); 
        hourstobirthday=Math.floor((tstb-daystobirthday*86400)/3600);
        minutestobirthday=Math.floor((tstb-daystobirthday*86400-hourstobirthday*3600)/60);
        secondstobirthday=Math.floor(tstb-daystobirthday*86400-hourstobirthday*3600-minutestobirthday*60)

		document.agecalc.result.value+=daystobirthday;
		if(daystobirthday==1)
			{
			document.agecalc.result.value+=" day ";
			}
		else
			{
			document.agecalc.result.value+=" days ";
			}
		document.agecalc.result.value+=hourstobirthday;
		if(hourstobirthday==1)
			{
			document.agecalc.result.value+=" hr ";
			}
		else
			{
			document.agecalc.result.value+=" hrs ";
			}
		document.agecalc.result.value+=minutestobirthday;
		if(minutestobirthday==1)
			{
			document.agecalc.result.value+=" min ";
			}
		else
			{
			document.agecalc.result.value+=" mins ";
			}
		document.agecalc.result.value+=secondstobirthday;
		if(secondstobirthday==1)
			{
			document.agecalc.result.value+=" sec. ";
			}
		else
			{
			document.agecalc.result.value+=" secs. ";
			}
    	}
	true_bodyobject.cakebutton=0;
	validatecake();
    }
  }
else
  {
  document.agecalc.result.value="";
  }
}

function commatized(numericvalue)
{

if(Math.abs(numericvalue)<1000){return (""+numericvalue);}

textstring=""+numericvalue;
sit=0;
while(textstring.charAt(sit)!="")
	{
	sit++
	}
textdone="";
for (x=sit-1;x>=0;x--)
	{
	comingchar=textstring.charAt(x)
	if(((((sit-1)-x)%3)==0)&&(((sit-1)-x)>0))
	{
	if((comingchar=="1")||(comingchar=="2")||(comingchar=="3")||(comingchar=="4")||
	(comingchar=="5")||(comingchar=="6")||(comingchar=="7")||(comingchar=="8")||(comingchar=="9")||(comingchar=="0"))
		{
		textdone=","+textdone;
		}
	}
	textdone=comingchar+textdone;
	}
return(textdone);
}

function buttoncake()
{
if(true_bodyobject.IsyourBirthday==1)
	{
	if(true_bodyobject.showcake==0)
		{
		showmycake();
		}
	else
		{
		if(true_bodyobject.showcake==1)
			{
	        if(true_bodyobject.cakewnd.closed)
	        	{
				true_bodyobject.showcake=0;
				showmycake();
	        	}
			else
				{
		        if(!true_bodyobject.cakewnd.closed)
		        	{
					true_bodyobject.cakewnd.close();
					true_bodyobject.showcake=0;
					showmycake();
		        	}
				}
		    }
		}
	}
}
function showmycake()
{
if((true_bodyobject.IsyourBirthday==1)&&(true_bodyobject.showcake==0))
	{
	true_bodyobject.showcake=1;	
	true_bodyobject.cakewnd=window.open("","Birthday","width=205, height=270,toolbar=0,menubar=0,scrollbars=0");
	true_bodyobject.cakewnd.document.write("<html>");
	true_bodyobject.cakewnd.document.write("<head>");
	true_bodyobject.cakewnd.document.write("<title>......!!!!!!!!!!!!!!!!!!!!!!!!.......</title>");
	true_bodyobject.cakewnd.document.write("</head>");
	true_bodyobject.cakewnd.document.write("<body bgcolor=yellow>");
	true_bodyobject.cakewnd.document.write("<font size=+1>");
	true_bodyobject.cakewnd.document.write("<font face=arial>");
	true_bodyobject.cakewnd.document.write("<center>");
	true_bodyobject.cakewnd.document.write("Happy Birthday!");
	true_bodyobject.cakewnd.document.write("</center>");
	true_bodyobject.cakewnd.document.write("<center>");
	true_bodyobject.cakewnd.document.write("You are ");
	true_bodyobject.cakewnd.document.write(true_bodyobject.Cakesize);
	true_bodyobject.cakewnd.document.write(" years old.");
	true_bodyobject.cakewnd.document.write("</center>");
	true_bodyobject.cakewnd.document.write("</font>");
	true_bodyobject.cakewnd.document.write("</font>");

	true_bodyobject.cakewnd.document.write("<center>");
	true_bodyobject.cakewnd.document.write("<table border=0 cellspacing=0 cellpadding=0>");
	if (true_bodyobject.Cakesize<10)
		{
		true_bodyobject.cakewnd.document.write("<tr><td>");
		true_bodyobject.cakewnd.document.write("<img src=http://www.mathcats.org/explore/age/cake0xx.gif>");
		true_bodyobject.cakewnd.document.write("</td></tr>");
		true_bodyobject.cakewnd.document.write("<tr><td>");
		true_bodyobject.cakewnd.document.write("<img src=http://www.mathcats.org/explore/age/cake00x.gif>");
		true_bodyobject.cakewnd.document.write("</td></tr>");
		true_bodyobject.cakewnd.document.write("<tr><td>");
		true_bodyobject.cakewnd.document.write("<img src=http://www.mathcats.org/explore/age/cake00"+true_bodyobject.Cakesize+".gif>");
		true_bodyobject.cakewnd.document.write("</td></tr>");
	    }
	else
		{
		if(true_bodyobject.Cakesize<100)
			{
			true_bodyobject.cakewnd.document.write("<tr><td>");
			true_bodyobject.cakewnd.document.write("<img src=http://www.mathcats.org/explore/age/cake0xx.gif>");
			true_bodyobject.cakewnd.document.write("</td></tr>");
			true_bodyobject.cakewnd.document.write("<tr><td>");
			tensvalue=Math.abs(Math.floor(true_bodyobject.Cakesize/10));
			unityvalue=true_bodyobject.Cakesize-(tensvalue*10);
			true_bodyobject.cakewnd.document.write("<img src=http://www.mathcats.org/explore/age/cake0"+tensvalue+"x.gif>");
			true_bodyobject.cakewnd.document.write("</td></tr>");
			true_bodyobject.cakewnd.document.write("<tr><td>");
			true_bodyobject.cakewnd.document.write("<img src=http://www.mathcats.org/explore/age/cakexx"+unityvalue+".gif>");
			true_bodyobject.cakewnd.document.write("</td></tr>");
            }
		else
			{
			if(true_bodyobject.Cakesize<1000)
				{
				true_bodyobject.cakewnd.document.write("<tr><td>");
				hundredsvalue=Math.abs(Math.floor(true_bodyobject.Cakesize/100));
				true_bodyobject.cakewnd.document.write("<img src=http://www.mathcats.org/explore/age/cake"+hundredsvalue+"xx.gif>");
				true_bodyobject.cakewnd.document.write("</td></tr>");
				true_bodyobject.cakewnd.document.write("<tr><td>");
				auxiliarvar=true_bodyobject.Cakesize-(hundredsvalue*100);
				tensvalue=Math.abs(Math.floor(auxiliarvar/10));
				unityvalue=auxiliarvar-(tensvalue*10);
				true_bodyobject.cakewnd.document.write("<img src=http://www.mathcats.org/explore/age/cakex"+tensvalue+"x.gif>");
				true_bodyobject.cakewnd.document.write("</td></tr>");
				true_bodyobject.cakewnd.document.write("<tr><td>");
				true_bodyobject.cakewnd.document.write("<img src=http://www.mathcats.org/explore/age/cakexx"+unityvalue+".gif>");
				true_bodyobject.cakewnd.document.write("</td></tr>");
			    }
			}
		}
	true_bodyobject.cakewnd.document.write("</table>");
	true_bodyobject.cakewnd.document.write("</center>");
	true_bodyobject.cakewnd.document.write("<center>");
	true_bodyobject.cakewnd.document.write("<input type=button value=close  onClick='self.close();'>");
	true_bodyobject.cakewnd.document.write("</center>");
	true_bodyobject.cakewnd.document.write("</body>");
	true_bodyobject.cakewnd.document.write("</html>");
	true_bodyobject.cakewnd.document.close();
	}
}

