//  http://www.fortereview.com/date_calc.html

// ================================================
//  this is the function to calculate My Rotation Date
// ================================================
function rotation() {
	var now 			= new Date();
	var todaymonth = now.getMonth();
	var todayday 	= now.getDate(); 
	var todayyear 	= now.getFullYear();
	var todayyear2 	= now.getFullYear();

//document.write("<br>date="+now);
//document.write("<br>month="+todaymonth -1);
//document.write("<br>day="+todayday);
//document.write("<br>year="+todayyear);
//document.write("<br>year2="+todayyear2);


	if (month < todaymonth)
		{todayyear2 = todayyear2 +1;}	
		
	else if (month == todaymonth)	
		{
		if (day < todayday)	
			{todayyear2++;}	
		}	

	var includelastday;

//	    includelastday = 1;
	    includelastday = 0;

	start = new Date(todayyear,todaymonth,todayday);  // Todays Date
	end = new Date(2006,10,01);   // Roatation Date - Month is set one back because of javascript rules

	oneday = new Date(1970,1,2);
	zeroday = new Date(1970,1,1);
	onedaylength = oneday - zeroday;

	timedifference = end - start;
	//difference = Date.parse(end) - Date.parse(start);

	days_spanned = (timedifference / onedaylength)

	// checks to see if the total should count the last day.
	if (includelastday == 0) {
	    days_spanned = days_spanned;
		}
	else {days_spanned++;}
	
	// this will get rid of any decimals.
	days_spanned = roundOff(days_spanned,0);

	var num = new NumberFormat();
	num.setInputDecimal('.');
	num.setNumber(days_spanned); 
	num.setPlaces('0');
	num.setCurrencyValue('$');
	num.setCurrency(false);
	num.setCurrencyPosition(num.LEFT_OUTSIDE);
	num.setNegativeFormat(num.LEFT_DASH);
	num.setNegativeRed(false);
	num.setSeparators(true, ',', ',');
	num = num.toFormatted();

	document.write("<B><FONT COLOR='#678fc3' SIZE='-1'>ITN/B: "+num+"</font></b>");
	return false;
	}

// ================================================
//  this is the function to calculate My Retirement Date
// ================================================
function retirement(home) {
	var now 			= new Date();
	var todaymonth = now.getMonth();
	var todayday 	= now.getDate(); 
	var todayyear 	= now.getFullYear();
	var todayyear2 	= now.getFullYear();
	var homeswitch = home;

//document.write("<br>date="+now);
//document.write("<br>month="+todaymonth -1);
//document.write("<br>day="+todayday);
//document.write("<br>year="+todayyear);
//document.write("<br>year2="+todayyear2);


	if (month < todaymonth)
		{todayyear2 = todayyear2 +1;}	
		
	else if (month == todaymonth)	
		{
		if (day < todayday)	
			{todayyear2++;}	
		}	

	var includelastday;

//	    includelastday = 1;
	    includelastday = 0;

	start = new Date(todayyear,todaymonth,todayday);  // Todays Date
	end = new Date(2020,8,17);   // Retirement Date - Month is set one back because of javascript rules

	oneday = new Date(1970,1,2);
	zeroday = new Date(1970,1,1);
	onedaylength = oneday - zeroday;

	timedifference = end - start;
	//difference = Date.parse(end) - Date.parse(start);

	days_spanned = (timedifference / onedaylength)

	// checks to see if the total should count the last day.
	if (includelastday == 0) {
	    days_spanned = days_spanned;
		}
	else {days_spanned++;}
	
	// this will get rid of any decimals.
	days_spanned = roundOff(days_spanned,0);

	var num = new NumberFormat();
	num.setInputDecimal('.');
	num.setNumber(days_spanned); 
	num.setPlaces('0');
	num.setCurrencyValue('$');
	num.setCurrency(false);
	num.setCurrencyPosition(num.LEFT_OUTSIDE);
	num.setNegativeFormat(num.LEFT_DASH);
	num.setNegativeRed(false);
	num.setSeparators(true, ',', ',');
	num = num.toFormatted();

	if (homeswitch =="1")
	{
	document.write(num);
	return false;
	}
	else
	{
	document.write("<B><FONT COLOR='#678fc3' SIZE='-1'>Days Left DCX: "+num+"</font></b>");
	return false;
	}
	}

// ================================================
//   General Array Function
// ================================================
    function MakeArray2(n) 
	{   
      this.length=n;   
      for (var i=1; i<=n; i++) this[i]=0;   
    	}

// ================================================
//  this is the function to calculate Card and Meeting Dates
// ================================================
var firstimethrough="Y"
function bday(namex,eventx,yearx,monthx,dayx,leadx)
{	
	var now 			= new Date();
	var todaymonth = now.getMonth() ;
	var todayday 	= now.getDate(); 
	var todayyear 	= now.getFullYear();
	var todayyear2 	= now.getFullYear();

	var year = yearx;
	var month = monthx -1;
	var day = dayx;
	var name=namex;
	var event=eventx;
	var lead=leadx;
	var ext1="";

	var dayname="";
	var monthname="";

    //Initialize Days of Week Array
    days=new MakeArray2(7);
    days[0]="(Sat)"; days[1]="(Sun)"; days[2]="(Mon)";
    days[3]="(Tues)"; days[4]="(Wed)"; days[5]="(Thur)";
    days[6]="(Fri)";

    //Initialize Months Array
    months=new MakeArray2(12);
    months[1]="January";   months[2]="February"; 
    months[3]="March";     months[4]="April";
    months[5]="May";       months[6]="June";
    months[7]="July";      months[8]="August";
    months[9]="September"; months[10]="October";
    months[11]="November"; months[12]="December";

    //Day of Week Function
      var val1=dayx;
      var val2 = monthx;

      var val2x = parseInt(val2, 10)   
      var val3 = parseInt(todayyear, 10)   
      if (val2 == 1)    { val2x=13;  val3=val3-1;    }   
      if (val2 == 2)    { val2x=14;  val3=val3-1;    }   
      var val4 = parseInt(((val2x+1)*3)/5, 10)   
      var val5 = parseInt(val3/4, 10)   
      var val6 = parseInt(val3/100, 10)   
      var val7 = parseInt(val3/400, 10)   
      var val8 = val1+(val2x*2)+val4+val3+val5-val6+val7+2   
      var val9 = parseInt(val8/7, 10)   
      var val0 = val8-(val9*7)   

     monthname=months[val2]+" "+dayx+ ", "+todayyear;   
	dayname=days[val0];
// document.write("<br>"+namex+"     Month name:"+monthname+"    Day Name:"+dayname+"<br>");


	if (month < todaymonth)
		{todayyear2 = todayyear2 +1;}	
		
	else if (month == todaymonth)	
		{
		if (day < todayday)	
			{todayyear2++;}	
		}	

	var age = todayyear2 - yearx;

	var includelastday;

//	    includelastday = 1;
	    includelastday = 0;

	start = new Date(todayyear,todaymonth,todayday);  // Todays Date
	end = new Date(todayyear2,month,day);   // Birthdate

//	document.write("<br><br>start:"+start+"<br>end:"+end+"<br><br>");

	oneday = new Date(1970,1,2);
	zeroday = new Date(1970,1,1);
	onedaylength = oneday - zeroday;

	timedifference = end - start;

	days_spanned = (timedifference / onedaylength)
//	document.write("<br><br>Days Spanned:"+days_spanned+"<br>");
     days_spanned = days_spanned;
//	document.write("<br><br>Days Spanned:"+days_spanned+"<br>");	
	// this will get rid of any decimals.
	days_spanned = roundOff(days_spanned,0);
//	document.write("<br><br>Days Spanned:"+days_spanned+"<br>");


// document.write("<br>date="+now);
// document.write("<br>month="+todaymonth);
// document.write("<br>day="+todayday);
// document.write("<br>year="+todayyear);
// document.write("<br>year2="+todayyear2);
// document.write("<br>in month="+month);
// document.write("<br>in day="+day);
// document.write("<br>in year="+year);
// document.write("<br>in lead="+lead);
// document.write("<br><br>start:"+start+"<br>end:"+end+"<br><br>");

	if(age < 9)
	{
		if (age == 1)
			{ ext1="st";}
		else if (age == 2)
			{ ext1="nd";}
		else if (age == 3)
			{ ext1="rd";}
		else 
			{ ext1="th";}
	}
	else
	{
		if (age == 21 || age == 31 || age == 41 || age == 51 || age == 61 || age == 71 || age == 81 || age == 91 || age == 101)
			{ ext1="st";}
		else if (age == 22 || age == 32 || age == 42 || age == 52 || age == 62 || age == 72 || age == 82 || age == 92 || age == 102)
			{ ext1="nd";}
		else if (age == 23 || age == 33 || age == 43 || age == 53 || age == 63 || age == 73 || age == 83 || age == 93 || age == 103)
			{ ext1="rd";}
		else 
			{ ext1="th";}
	}	

// document.write("str:"+age.substring(1,2))
		
	if (event == "R")
	{
		if (days_spanned == 0)
			{
			if (firstimethrough=="Y")
			{document.write("<b class=blue10><br>Reminders:</b><br>"); firstimethrough="N"}	
			document.write("<b class=red12>"+name+" Today </b>"+ dayname +"<br>");
			}
		else if ((days_spanned > 0) && (days_spanned < lead))
			{
			if (firstimethrough=="Y")
			{document.write("<b class=blue10><br>Reminders:</b><br>"); firstimethrough="N"}	
			document.write("<font color='#006633'>"+name+" in " + days_spanned + " days "+ dayname +"</font><br>");
			}
	}
	else
	{
		if (days_spanned == 0)
			{
			document.write("<b class=red12>"+name+"'s "+age+ext1+" "+event+" is Today </b>"+ dayname +"<br>");
			}
		else if ((days_spanned > 0) && (days_spanned < 15))
			{
			document.write("<font color='red'>"+name+"'s "+ age+ext1+" "+event+" is in " + days_spanned + " days "+ dayname +"</font><br>");
			}
		else if ((days_spanned > 15) && (days_spanned < lead))
			{
			document.write(name+"'s "+ age+ext1+" "+event+" is in " + days_spanned + " days "+ dayname +"<br>");
			}
		else if (days_spanned < 0)
			{
			document.write(name+"'s "+ age+ext1+" "+event+" was "  + days_spanned + " days ago "+ dayname +"<br>");
			}
	}
}

function roundOff(value, precision)
{
// from http://developer.netscape.com/docs/examples/javascript/rounding.html
        value = "" + value //convert value to string
        precision = parseInt(precision);
        var whole = "" + Math.round(value * Math.pow(10, precision));
        var decPoint = whole.length - precision;
        if(decPoint != 0)
        {
                result = whole.substring(0, decPoint);
				// Josh Kutz commented next line cause only need whole numbers.
                // result += ".";
                result += whole.substring(decPoint, whole.length);
        }
        else
        {
                result = whole;
        }
        return result;
}


// Day / Date function
function makeArray(len)
{       for (var i = 0; i < len; i++)
        {       this[i] = null
        }
        this.length = len
}

// array of day names
        var daynames = new makeArray(7)
        daynames[0] = "Sunday"
        daynames[1] = "Monday"
        daynames[2] = "Tuesday"
        daynames[3] = "Wednesday"
        daynames[4] = "Thursday"
        daynames[5] = "Friday"
        daynames[6] = "Saturday"

// array of month names
        var monthnames = new makeArray(12)
        monthnames[0] = "January"
        monthnames[1] = "February"
        monthnames[2] = "March"
        monthnames[3] = "April"
        monthnames[4] = "May"
        monthnames[5] = "June"
        monthnames[6] = "July"
        monthnames[7] = "August"
        monthnames[8] = "September"
        monthnames[9] = "October"
        monthnames[10] = "November"
        monthnames[11] = "December"

// define date variables
        var now = new Date()
        var day = now.getDay()
        var month = now.getMonth()
        var year = now.getFullYear()
        var date = now.getDate()
        var hour=now.getHours()
        var minutes=now.getMinutes()

// write date
function writeDate()
{       document.write("<B><FONT COLOR='#678fc3' SIZE='+1'>" +  daynames[day] + ",&nbsp;" + monthnames[month] + " " + date + ", " + year +"  "      +"</FONT></B>")
}
