//
// Fetch Date 
//  Borrowed from hypergurl
//

var monthAbbr = new Array();
// MonthAbbr Labels
   monthAbbr[0]  = "Jan";
   monthAbbr[1]  = "Feb";
   monthAbbr[2]  = "Mar";
   monthAbbr[3]  = "Apr";
   monthAbbr[4]  = "May";
   monthAbbr[5]  = "Jun";
   monthAbbr[6]  = "Jul";
   monthAbbr[7]  = "Aug";
   monthAbbr[8]  = "Sep";
   monthAbbr[9]  = "Oct";
   monthAbbr[10] = "Nov";
   monthAbbr[11] = "Dec";

var monthNames = new Array();
// Month Labels
   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";


var daysNames = new Array();
// Day labels
   daysNames[0] = "Sunday";
   daysNames[1] = "Monday";
   daysNames[2] = "Tuesday";
   daysNames[3] = "Wednesday"; 
   daysNames[4] = "Thursday";
   daysNames[5] = "Friday";
   daysNames[6] = "Saturday"; 

function customDateSpring(oneDate) { 
    var theDay = daysNames[oneDate.getDay()];
    var theDate = oneDate.getDate();
    var theMonth = monthNames[oneDate.getMonth()];
    var tmpDate = new Date();
    var theYear = tmpDate.getFullYear();

    // Determine date suffix
    var dayth= "th";
    if ((theDate == 1) || (theDate == 21) || (theDate == 31)) { 
       dayth="st";
    } if ((theDate == 2) || (theDate == 22)) { 
       dayth="nd";
    } if ((theDate== 3) || (theDate == 23)) { 
       dayth="rd";
    } 
    var tmpFullDate = theDay + ", " + theMonth + " " + theDate + dayth;
    tmpFullDate += ", " + theYear + ".";
    return tmpFullDate;
  } 

document.write(customDateSpring(new Date()));

// Get the ISO week date week number

Date.prototype.getWeek = function () {
	// Create a copy of this date object
	var target = new Date(this.valueOf());

	// ISO week date weeks start on monday
	// so correct the day number
	var dayNr = (this.getDay() + 6) % 7;

	// Set the target to the thursday of this week so the
	// target date is in the right year
	target.setDate(target.getDate() - dayNr + 3);

	// ISO 8601 states that week 1 is the week
	// with january 4th in it
	var jan4 = new Date(target.getFullYear(), 0, 4);

	// Number of days between target date and january 4th
	var dayDiff = (target - jan4) / (60*60*24*1000); 

	// Calculate week number: Week 1 (january 4th) plus the 
	// number of weeks between target date and january 4th 
	var weekNr = 1 + Math.floor(dayDiff / 7); 

	document.write(weekNr); 
}


/* 
 * Get the ISO week date year number
 */
Date.prototype.getWeekYear = function () {

	// Create a new date object for the thursday of this week
	var target = new Date(this.valueOf());
	target.setDate(target.getDate() - ((this.getDay() + 6) % 7) + 3);
 
	return target.getFullYear();
}


function dayOfYear(day) {
	var jan_one = new Date(day.getFullYear(),0,0,0,0,0);
	var doy = parseInt((day - jan_one) / (1000 * 60 * 60 * 24)) ;

	document.write(doy);
}



function myCalendar(){

/* PERPETUAL GREGORIAN CALENDAR
 *
 *  ---------------------------------------------------------------
 *  APPEARED IN ASTRONOMICAL COMPUTING, SKY & TELESCOPE, JULY, 1985
 *  Modified for javascript
 *  ---------------------------------------------------------------
 *
 * th class
 *   head1: month & year
 *   head2: SMTWTFS or MTWTFSS labels
 * 
 * td class
 *   ms: mon thru sat
 *   sun: sunday only
 *
 */

var inputMonth;
var inputYear;

now= new Date();
inputMonth = now.getMonth();
inputYear = now.getFullYear();

var mondayStart = false; // false for Sun start, true for Mon start

var monthLength=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var today = new Date();
var J; // julian date
var K; // leap month flag
var W; // leap century flag
var X; // leap year flag
var Z; // leap 400-year flag
var N;
var M;
var P;

// inputMonth 1-12, JS getMonth 0-11
// inputYear same as Javascript

// inputMonth=inputMonth-1;                         //less 1
var myMonth=Math.floor(inputMonth);   //integer of input month
var myYear=Math.floor(inputYear);     //integer of input year

myMonth %= 12; // take 12 modulus, sets range 0-11
myMonth=myMonth+1;     // sets range 1-12

var quo = Math.floor(inputMonth);   // integer of input month
quo /= 12;                          // take 12 quotient
quo = Math.floor(quo);              // integer
myYear += quo;                      // add quotient to input year

J = 367*myYear;
N = (myMonth+9)/12;
M = Math.floor(N);
P= 7*(myYear+ M)/4
J -= Math.floor(P);
J += Math.floor(275*myMonth/9);
J += 1721031;
if (mondayStart) {J -= 1;}

if ((myMonth == 1) || (myMonth == 2)) {
	K = -1;
	} else {K = 0;}

Q = (myYear+K)/100;
R = Math.floor(Q);
R= R+1;
R *= .75;
S = Math.floor(R);
J -= S;
T = myMonth-1
L = monthLength[T];  

if (myMonth != 2) {

	fillin(J,L,T,myYear,mondayStart); // non-Feb months
	
	} else {

	// determine leap year

	WC = myYear/100;
	WB = Math.floor(WC);
	WB *= 100;
	WA = myYear-WB;
	W = Math.floor(WA);

	XC = myYear/4;
	XB = Math.floor(XC);
	XB *= 4;
	XA = myYear- XB;
	X = Math.floor(XA);

	ZC = myYear/400;
	ZB = Math.floor(ZC);
	ZB *= 400;
	ZA = myYear- ZB;
	Z=Math.floor(ZA);

	if (X != 0) {
		fillin(J,L,T,myYear,mondayStart);
	} else if (W == 0 && Z != 0) {
		fillin(J,L,T,myYear,mondayStart);
	} else { 
		K=29;
		fillin(J,L,T,myYear,mondayStart);
		}
	}
}

function fillin(a,b,e,y,flagMonday){
	var a;
	var b;
	var e;
	var y;
	var flagMonday;
	var monthAbbr=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	var cell=new Array(43);  // blank cells to be populated
	var tiles=new Array(32);  // cell tiles (dates)

	tiles[1]="&nbsp;1";
	tiles[2]="&nbsp;2";
	tiles[3]="&nbsp;3";
	tiles[4]="&nbsp;4";
	tiles[5]="&nbsp;5";
	tiles[6]="&nbsp;6";
	tiles[7]="&nbsp;7";
	tiles[8]="&nbsp;8";
	tiles[9]="&nbsp;9";
	tiles[10]="10";

	tiles[11]="11";
	tiles[12]="12";
	tiles[13]="13";
	tiles[14]="14";
	tiles[15]="15";
	tiles[16]="16";
	tiles[17]="17";
	tiles[18]="18";
	tiles[19]="19";
	tiles[20]="20";

	tiles[21]="21";
	tiles[22]="22";
	tiles[23]="23";
	tiles[24]="24";
	tiles[25]="25";
	tiles[26]="26";
	tiles[27]="27";
	tiles[28]="28";
	tiles[29]="29";
	tiles[30]="30";

	tiles[31]="31";

	// subst jkt for abe
	
	d = a/7;
	d = Math.floor(d);
	d *= 7;
		
	var c = a - d;
	
	// reset array
	for(var i = 1; i <= 42; i++){
		cell[i] = "&nbsp;&nbsp;";
		}

	// assign tiles into array
	for(var i = 1; i <= b; i++){
		cell[i+c]=tiles[i];
		}

	document.write("<table class='greg' cellspacing='0' cellpadding='2'>");
	document.write("<tr class='heada'><th colspan='7' class='head1'>" + monthAbbr[e] + " " + y);
	document.write("</th></tr>");
	
	if (flagMonday){
		document.write("<tr class='headb'>");
		document.write("<th class='head2'>M</th><th class='head2'>T</th>");
		document.write("<th class='head2'>W</th><th class='head2'>T</th>");
		document.write("<th class='head2'>F</th><th class='head2'>S</th>");
		document.write("<th class='head2'>S</th></tr>");
	}else{
		document.write("<tr class='headb'>");
		document.write("<th class='head2'>S</th><th class='head2'>M</th>");
		document.write("<th class='head2'>T</th><th class='head2'>W</th>");
		document.write("<th class='head2'>T</th><th class='head2'>F</th>");
		document.write("<th class='head2'>S</th></tr>");
	}

	// print rows
	for (var i = 1; i<= 6; i++){
		a = 7 * i;
	if (flagMonday){
		document.write("<tr class='dts'>");
		document.write("<td class='ms'>"+cell[a-6]+"</td>");
		document.write("<td class='ms'>"+cell[a-5]+"</td>");
		document.write("<td class='ms'>"+cell[a-4]+"</td>");
		document.write("<td class='ms'>"+cell[a-3]+"</td>");
		document.write("<td class='ms'>"+cell[a-2]+"</td>");
		document.write("<td class='ms'>"+cell[a-1]+"</td>");
		document.write("<td class='sun'>"+cell[a]+"</td>");
		document.write("</tr>");
	    }else{
		document.write("<tr class='dts'>");
		document.write("<td class='sun'>"+cell[a-6]+"</td>");
		document.write("<td class='ms'>"+cell[a-5]+"</td>");
		document.write("<td class='ms'>"+cell[a-4]+"</td>");
		document.write("<td class='ms'>"+cell[a-3]+"</td>");
		document.write("<td class='ms'>"+cell[a-2]+"</td>");
		document.write("<td class='ms'>"+cell[a-1]+"</td>");
		document.write("<td class='ms'>"+cell[a]+"</td>");
		document.write("</tr>");
	    }
	}
	//close table
	document.write("</table>");
}





