var day, month, year;
var fieldInFormOfInterest; // Ha!
var cal_window;

function setDate(dateField) {
	this.inDate = dateField.value;
	fieldInFormOfInterest = dateField;

   // SET DAY MONTH AND YEAR TO TODAY'S DATE
   	var now   = new Date();
   	day   = now.getDate();
   	month = now.getMonth();
   	year  = now.getYear();
   	if (year < 100) {
   		year += 1900;
   	}
	//   else {
	//       year += 2000;
	//   }

   // IF A DATE WAS PASSED IN THEN PARSE THAT DATE
   	if (inDate.indexOf('/')) {
 
 
       var inMonth = inDate.substring(0,inDate.indexOf("/"));
          if (inMonth.substring(0,1) == "0" && inMonth.length > 1)
               inMonth = inMonth.substring(1,inMonth.length);
           inMonth = parseInt(inMonth);
       var inDay   = inDate.substring(inDate.indexOf("/") + 1, inDate.lastIndexOf("/"));
           if (inDay.substring(0,1) == "0" && inDay.length > 1)
               inDay = inDay.substring(1,inDay.length);
           inDay = parseInt(inDay);
       var inYear  = parseInt(inDate.substring(inDate.lastIndexOf("/") + 1, inDate.length));
   
	    
		
	/* 	var inYear = parseInt(inDate.substring(0,inDate.indexOf("/")));
	    	
		var inMonth = inDate.substring(inDate.indexOf("/") + 1, inDate.lastIndexOf("/"));
    	if (inMonth.substring(0,1) == "0" && inMonth.length > 1)
    	inMonth = inMonth.substring(1,inMonth.length);
    	inMonth = parseInt(inMonth);
		   
   		var inDay = inDate.substring(inDate.lastIndexOf("/") + 1, inDate.length);
	
    	if (inDay.substring(0,1) == "0" && inDay.length > 1)
    		inDay = inDay.substring(1,inDay.length);
    	inDay = parseInt(inDay);
*/
    	if (inDay) {
    		day = inDay;
    	}
    	if (inMonth) {
    	    month = inMonth-1;
    	}
    	if (inYear) {
    		year = inYear;
    	}
   }
	cal_window = window.open('','cal','WIDTH=200,HEIGHT=210,outerHeight=300,outerWidth=300')
//	cal_window = window.open('draw.html','cal','WIDTH=200,HEIGHT=250')
	displayCalendar(day, month, year);
}	

function changeDate(action) {
	if (action == "prevYear") {
		year--;							
	}
	else if (action == "nextYear") {
		year++;
	}
	else if (action == "prevMonth") {
      if (month == 0) {
			month = 11;
			if (year > 1000) {
				year--;
			}
      }
      else {
      	month--;
      }
	}
	else if (action == "nextMonth") {
      if (month == 11) {
			month = 0;
			year++;
      }
      else {
			month++;
      }
	}
	else if (action == "today") {
    var now = new Date();
    day = now.getDate();
    month = now.getMonth();
    year = now.getYear();
   if (year < 100) {
       year += 1900;
   }	
/*	
    if (year < 100) {
			year += 1900;
    }
    else {
			year += 2000;
    }
*/
	}
	displayCalendar(day, month, year);
}

function isFourDigitYear(year) {
   if (year.length != 4) {
       alert ("Sorry, the year must be four-digits in length.");
       document.calControl.year.select();
       document.calControl.year.focus();
   }
   else {
       return true;
   }
}

function returnDate(theDay) {
	day = theDay;
	
	if (day < 10) day = ("0" + day);
	month++;
	if (month < 10) month = "0" + month;
	
//	fieldInFormOfInterest.value = month + "/" + day + "/" + year;
	fieldInFormOfInterest.value = month + "/" + day + "/" + year;
	cal_window.close();
}

function displayCalendar(day, month, year) {
	day = parseInt(day);
	month = parseInt(month);
	year = parseInt(year);

	var monthArr = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
	var days = getDaysInMonth(month+1, year);
	var firstOfMonth = new Date(year, month, 1);
	var startingPos = firstOfMonth.getDay();

	var curDayOfWeek = 0;
	var curDayOfMonth = 1;
	var curWeek= 0;

	var now = new Date();
	var nowDay = now.getDate();
	var nowMonth = now.getMonth();
	var nowYear = now.getYear();
   if (year < 100) {
       year += 1900;
   }
/*
    if (nowYear < 100) {
			nowYear += 1900;
    }
    else {
			nowYear += 2000;
    }
*/
	var outStr = "";
		
	outStr += "<head><title>Calendar</title></head>";
	outStr += "<style>\n" 
	outStr += ".ccmButton{ "
	outStr += "background-color:#123981; "
	outStr += "background-image: hdr_bg.gif; "
	outStr += "color: #FFFFFF; "
	outStr += "border-left: 1px #6B94CD solid; "
	outStr += "border-top: 1px #6B94CD solid; "
	outStr += "border-right: 1px #173989 solid; "
	outStr += "border-bottom: 1px #173989 solid; "
	outStr += "font-size: 11px; "
	outStr += "font-weight: bold; "
	outStr += "height: 19px; "
	outStr += "text-decoration: none; }\n "
	
	outStr += ".label { "
	outStr += "	color : #123981; "
	outStr += "	font-size : 9pt; "
	outStr += "	font-family : verdana, arial; "
	outStr += "	font-weight : bold; "
	outStr += "} "

	outStr += ".altLine { "
	outStr += "	background-color: #FFFFFF;"
	outStr += "	font-size : 12px;"
	outStr += "	font-family : verdana, arial;"
	outStr += "	font-weight : normal;"
	outStr += "	padding-left: 2px;"
	outStr += "	padding-top: 1px;	"
	outStr += "	padding-bottom: 1px;"		
	outStr += "	padding-right: 2px;	"
	outStr += "}"
	
	outStr += ".altLine2 { "
	outStr += "	background-color: #DEDEDE;"
	outStr += "	font-size : 12px;"
	outStr += "	font-family : verdana, arial;"
	outStr += "	font-weight : normal;"
	outStr += "	padding-left: 2px;"
	outStr += "	padding-top: 1px;	"
	outStr += "	padding-bottom: 1px;"		
	outStr += "	padding-right: 2px;	"
	outStr += "}"
	
	outStr += ".colHead {"
	outStr += "	background-color: #BDCBD7;"
	outStr += "	color : #123981;"
	outStr += "	font-size : 10px;"
	outStr += "	font-family : verdana, arial;"
	outStr += "	font-weight : bold; "
	outStr += "	padding-left: 2px; "
	outStr += "	padding-top: 1px; "
	outStr += "	padding-bottom: 1px; "
	outStr += "	padding-right: 2px;	"
	outStr += "	border-right : 1px solid #123981;"
	outStr += "	border-bottom : 1px solid #123981;"
	outStr += "}\n"

	outStr += ".altLineLink { "
	outStr += "	color: #000000; "
	outStr += "	font-size : 12px; "
	outStr += "	font-family : verdana, arial; "
	outStr += "	font-weight : normal; "
	outStr += "	text-decoration: none; "
	outStr += "} \n"
	outStr += "</style>\n";
	
	outStr += "<body topmargin=3 marginheight=3>";
	outStr += "<div align='center' class='label'>";
	outStr += "<b>" + monthArr[month] + " " + year + "</b>";
	outStr += "<table cellpadding='0' cellspacing='1' border='0' bgcolor='#123981'><tr><td>"
	outStr += "<table border='0' cellspacing='0' cellpadding='2'>";
	outStr += "<tr align='center'><td width='20' class='colHead'>S</td><td width='20' class='colHead'>M</td><td width='20' class='colHead'>T</td><td width='20' class='colHead'>W</td><td width='20' class='colHead'>T</td><td width='20' class='colHead'>F</td><td width='20' class='colHead'>S</td></tr>";

	for (var i = 0; i < startingPos + days; i++) {
		if (curDayOfWeek == 0) outStr += "<tr align='center'>";
		if (i < startingPos) {
			outStr += "<td class='altLine'>&nbsp;</td>";
			curDayOfWeek++;
		}
		else {
			if ((nowDay == curDayOfMonth) && (nowMonth == month) && (nowYear == year))
				outStr += "<td height='25' class='altLine2'><a href='javascript:opener.returnDate(" + curDayOfMonth + ")' class='altLineLink'>" + curDayOfMonth + "</a></td>";
			else
				outStr += "<td height='25' class='altLine'><a href='javascript:opener.returnDate(" + curDayOfMonth + ")' class='altLineLink'>" + curDayOfMonth + "</a></td>";
			curDayOfWeek++;
			curDayOfMonth++;
		}
		if (curDayOfWeek % 7 == 0) {
			curWeek++;
			outStr += "</tr>";
			curDayOfWeek = 0
		}
	}
	while (curDayOfWeek % 7 != 0) {
		outStr += "<td class='altLine'>&nbsp;</td>";
		curDayOfWeek++;
	}

	outStr += "</table>";
	outStr += "</td></tr></table>";
	outStr += "<table border='0' cellspacing='0'>";
	outStr += "<tr><td>";
	outStr += "<form name='calControl' onSubmit='return false'>";
	outStr += "<input type='button' name='prevYear' value='<<' onClick='opener.changeDate(this.name);' class='ccmButton' style='width: 30px;' >";
	outStr += "<input type='button' name='prevMonth' value=' < ' onClick='opener.changeDate(this.name);' class='ccmButton' style='width: 30px;'>";
	outStr += "<input type='button' name='today' value='Today' onClick='opener.changeDate(this.name);' class='ccmButton'>";
	outStr += "<input type='button' name='nextMonth' value=' > ' onClick='opener.changeDate(this.name);' class='ccmButton' style='width: 30px;'>";
	outStr += "<input type='button' name='nextYear' value='>>' onClick='opener.changeDate(this.name);' class='ccmButton' style='width: 30px;'><br>";
	outStr += "</form>";
	outStr += "</td></tr>";
	outStr += "</table>";	
	outStr += "</div></body>";
	cal_window.document.writeln(outStr);

	cal_window.document.close();
}

function getDaysInMonth(month, year) {
   if (month==1 || month==3 || month==5 || month==7 || month==8 ||
       month==10 || month==12)  return(31);
   else if (month==4 || month==6 || month==9 || month==11) return(30);
   else if (month==2)  {
       if (isLeapYear(year)) {
           return(29);
       }
       else {
           return(28);
       }
   }
}

function isLeapYear (Year) {
	if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0)) {
		return (true);
	}
	else {
		return (false);
	}
}