/////////////////////////////////////////////////////////////////////////////
//  $Workfile: Calendar.js $ $Revision: 1.2 $ $Author: biden $ $Date: 2006/06/05 14:06:38 $
//
//
// *Note: This code has been radically modified for ledgibility and maintenence
//    but is largely based on the original pupdate.js from:
//
// PopUp Calendar v2.1 © PCI, Inc.,2000 • Freeware
// webmaster@personal-connections.com +1 (925) 955 1624
// Permission granted for unlimited use so far as the copyright notice above remains intact.
//
/////////////////////////////////////////////////////////////////////////////

var ppcDF = "m/d/Y";
var ppcMN = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var ppcWN = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var ppcER = new Array(4);
ppcER[0] = "Required DHTML functions are not supported in this browser.";
ppcER[1] = "Target form field is not assigned or not accessible.";
ppcER[2] = "Sorry, the chosen date is not acceptable. Please read instructions on the page.";
ppcER[3] = "Unknown error occured while executing this script.";

var ppcUC = false;
var ppcUX = 4;
var ppcUY = 4;
var bIsIE=(navigator.appName == "Microsoft Internet Explorer");
var bIsNN=((navigator.appName == "Netscape")&&(document.layers));
var ppcTT="<table width=\"200\" cellspacing=\"1\" cellpadding=\"2\" border=\"1\" bordercolorlight=\"#000000\" bordercolordark=\"#000000\">\n";
var ppcCD=ppcTT;
var ppcFT="<font face=\"MS Sans Serif, sans-serif\" size=\"1\" color=\"#000000\">";
var ppcFC=true;
var ppcTI=false;
var g_eltTarget=null;
var objRules=null;
var posX=null;
var posY=null;
var ppcML=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var ppcWE=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var g_dateSelection=new Date();


//////////////////////////////////////////////////
// Setup Event Handlers for Netscape
//////////////////////////////////////////////////
if(bIsNN)
{
	//////////////////////////////////////////////////
	// Setup Window.Resize Event Handler for Netscape
	//////////////////////////////////////////////////
	window.captureEvents(Event.RESIZE);
	window.onresize = restoreLayers;

	//////////////////////////////////////////////////
	// Setup Mouse click Event Handler for Netscape
	//////////////////////////////////////////////////
	document.captureEvents(Event.MOUSEDOWN|Event.MOUSEUP);
	document.onmousedown = mouseDownHandler;
	document.onmouseup = confirmXY;
}

///////////////////////////////////////////////////////////////////////////////
// Window.Resize Event Handler for Netscape
//
// @param e Event object reference
//
///////////////////////////////////////////////////////////////////////////////
function restoreLayers(e)
{
	if (bIsNN)
	{
		window.document.open("text/html");
		window.document.write("<html><head><title>Restoring the layer structure...</title></head>");
		window.document.write("<body bgcolor=\"#FFFFFF\" onLoad=\"history.go(-1)\">");
		window.document.write("</body></html>");
		window.document.close();
	}
}

///////////////////////////////////////////////////////////////////////////////
// Event.MOUSEDOWN Handler used in Netscape Navigator
//
// @param e Event object reference
//
///////////////////////////////////////////////////////////////////////////////
function mouseDownHandler(e)
{
	recordXY(e);
}

///////////////////////////////////////////////////////////////////////////////
// Store event location location
//
// @param e Event object reference
//
///////////////////////////////////////////////////////////////////////////////
function recordXY(e)
{
	if(bIsNN)
	{
		posX = e.x;
		posY = e.y;
		document.routeEvent(e);
	}
}

///////////////////////////////////////////////////////////////////////////////
// Confirm location
//
// @param e Event object reference
//
///////////////////////////////////////////////////////////////////////////////
function confirmXY(e)
{
 if(bIsNN)
 {
  posX = (posX == e.x) ? e.x : null;
  posY = (posY == e.y) ? e.y : null;
  document.routeEvent(e);
 }
}

///////////////////////////////////////////////////////////////////////////////
// Displays Calendar layer for specified target using optional rules.
//
// @param eltTarget Target element to write date text
//
///////////////////////////////////////////////////////////////////////////////
function getCalendarFor(eltTarget,rules)
{
	g_eltTarget = eltTarget;
	var nMoveUnderInputXAdjustment = 200;
	var nMoveUnderInputYAdjustment = 25;

	if(rules)
	{
		objRules = rules;
	}

	if (ppcFC)
	{
		setCalendar();
		ppcFC = false;
	}

 if ((g_eltTarget != null)&&(g_eltTarget))
 {
  if (bIsIE)
  {
   var obj = document.all['PopUpCalendar'];
   obj.style.left = document.body.scrollLeft+event.clientX-nMoveUnderInputXAdjustment;
   obj.style.top  = document.body.scrollTop+event.clientY;
   obj.style.visibility = "visible";
  }
  else if (bIsNN)
  {
   var obj = document.layers['PopUpCalendar'];
   obj.left = posX - nMoveUnderInputXAdjustment;
   obj.top  = posY + nMoveUnderInputYAdjustment;
   obj.visibility = "show";}
  else
  {
  	// Calendar functions not supported.
  	showError(ppcER[0]);
  }
 }
 else
 {
  	// Target Element not set.
		showError(ppcER[1]);
 }
}

///////////////////////////////////////////////////////////////////////////////
// Displays Calendar layer for specified target using optional rules.
//
// @param eltTarget Target element to write date text
//
///////////////////////////////////////////////////////////////////////////////
function switchMonth(param)
{
	var tmp = param.split("|");
	setCalendar(tmp[0],tmp[1]);
}

///////////////////////////////////////////////////////////////////////////////
// Displays Calendar layer for specified target using optional rules.
//
// @param eltTarget Target element to write date text
//
///////////////////////////////////////////////////////////////////////////////
function moveMonth(dir)
{
	var obj = null;
	var limit = false;
	var tmp,dptrYear,dptrMonth;
	if (bIsIE)
	{
		obj = document.ppcMonthList.sItem;
	}
	else if (bIsNN)
	{
		obj = document.layers['PopUpCalendar'].document.layers['monthSelector'].document.ppcMonthList.sItem;
	}
	else
	{
		showError(ppcER[0]);
	}

	if (obj != null)
	{
		if ((dir.toLowerCase() == "back")&&(obj.selectedIndex > 0))
		{
			obj.selectedIndex--;
		}
		else if ((dir.toLowerCase() == "forward")&&(obj.selectedIndex < 12))
		{
			obj.selectedIndex++;
		}
		else
		{
			limit = true;
		}
	}
	if (!limit)
	{
		tmp = obj.options[obj.selectedIndex].value.split("|");
		dptrYear  = tmp[0];
		dptrMonth = tmp[1];
		setCalendar(dptrYear,dptrMonth);
	}
	else
	{
		if (bIsIE)
		{
		 obj.style.backgroundColor = "#FFE003";
		 window.setTimeout("document.ppcMonthList.sItem.style.backgroundColor = '#FFFFFF'",50);
		}
	}
}

///////////////////////////////////////////////////////////////////////////////
// Displays Calendar layer for specified target using optional rules.
//
// @param eltTarget Target element to write date text
//
///////////////////////////////////////////////////////////////////////////////
function selectDate(param)
{
	var arr   = param.split("|");
	var year  = arr[0];
	var month = arr[1];
	var date  = arr[2];
	var ptr = parseInt(date);
	g_dateSelection.setDate(ptr);

	if ((g_eltTarget != null)&&(g_eltTarget))
	{
		if (validDate(date))
		{
			g_eltTarget.value = dateFormat(year,month,date);hideCalendar();
		}
		else
		{
			showError(ppcER[2]);
			if (ppcTI)
			{
				clearTimeout(ppcTI);
				ppcTI = false;
			}
		}
	}
	else
	{
		showError(ppcER[1]);
		hideCalendar();
	}
}

///////////////////////////////////////////////////////////////////////////////
// Initializes global date objects.
//
// @param year Year
// @param month Month
//
///////////////////////////////////////////////////////////////////////////////
function setCalendar(year,month)
{
	var dateNow = new Date();

	if((year == null) || (!year))
	{
		year = getFullYear(dateNow);
	}

	if((month == null) || (!month))
	{
		month = dateNow.getMonth();
		setSelectList(year,month);
	}

	if (month == 1)
	{
		ppcML[1]  = (isLeap(year)) ? 29 : 28;
	}

	g_dateSelection.setYear(year);
	g_dateSelection.setMonth(month);
	g_dateSelection.setDate(1);
	updateContent();
}

function updateContent()
{
	generateContent();
	if (bIsIE)
	{
		document.all['monthDays'].innerHTML = ppcCD;
	}
	else if(bIsNN)
	{
		with (document.layers['PopUpCalendar'].document.layers['monthDays'].document)
		{
		 open("text/html");
		 write("<html>\n<head>\n<title>DynDoc</title>\n</head>\n<body bgcolor=\"#FFFFFF\">\n");
		 write(ppcCD);
		 write("</body>\n</html>");
		 close();
		}
	}
	else
	{
		showError(ppcER[0]);
	}
	ppcCD = ppcTT;
}

function generateContent()
{
	var year  = getFullYear(g_dateSelection);
	var month = g_dateSelection.getMonth();
	var date  = 1;
	var day   = g_dateSelection.getDay();
	var len   = ppcML[month];
	var bgr,cnt,tmp = "";
	var j,i = 0;
	for (j = 0; j < 7; ++j) {
	if (date > len) {break;}
	for (i = 0; i < 7; ++i) {
	 bgr = ((i == 0)||(i == 6)) ? "#FFFFFF" : "#FFFFFF";
	 if (((j == 0)&&(i < day))||(date > len)) {tmp  += makeCell(bgr,year,month,0);}
	 else {tmp  += makeCell(bgr,year,month,date);++date;}}
	ppcCD += "<tr align=\"center\">\n" + tmp + "</tr>\n";tmp = "";}
	ppcCD += "</table>\n";
}

function makeCell(bgr,year,month,date)
{
	var dateNow=new Date();

 var param = "\'"+year+"|"+month+"|"+date+"\'";
 var td1 = "<td width=\"20\" bgcolor=\""+bgr+"\" ";
 var td2 = (bIsIE) ? "</font></span></td>\n" : "</font></a></td>\n";
 var evt = "onMouseOver=\"this.style.backgroundColor=\'#FFE003\'\" onMouseOut=\"this.style.backgroundColor=\'"+bgr+"\'\" onMouseUp=\"selectDate("+param+")\" ";
 var ext = "<span Style=\"cursor: hand\">";
 var lck = "<span Style=\"cursor: default\">";
 var lnk = "<a href=\"javascript:selectDate("+param+")\" onMouseOver=\"window.status=\' \';return true;\">";
 var cellValue = (date != 0) ? date+"" : "&nbsp;";
 if ((dateNow.getDate() == date)&&(dateNow.getMonth() == month)&&(getFullYear(dateNow) == year)) {
  cellValue = "<b>"+cellValue+"</b>";}
 var cellCode = "";
 if (date == 0) {
  if (bIsIE) {cellCode = td1+"Style=\"cursor: default\">"+lck+ppcFT+cellValue+td2;}
  else {cellCode = td1+">"+ppcFT+cellValue+td2;}}
 else {
  if (bIsIE) {cellCode = td1+evt+"Style=\"cursor: hand\">"+ext+ppcFT+cellValue+td2;}
  else {
   if (date < 10) {cellValue = "&nbsp;" + cellValue + "&nbsp;";}
   cellCode = td1+">"+lnk+ppcFT+cellValue+td2;}}
 return cellCode;
}

function setSelectList(year,month) {
 var i = 0;
 var obj = null;
 if (bIsIE) {obj = document.ppcMonthList.sItem;}
 else if (bIsNN) {obj = document.layers['PopUpCalendar'].document.layers['monthSelector'].document.ppcMonthList.sItem;}
 else {/* NOP */}
 while (i < 13) {
  obj.options[i].value = year + "|" + month;
  obj.options[i].text  = year + " • " + ppcMN[month];
  i++;
  month++;
  if (month == 12) {year++;month = 0;}}}

function hideCalendar() {
 if (bIsIE) {document.all['PopUpCalendar'].style.visibility = "hidden";}
 else if (bIsNN) {document.layers['PopUpCalendar'].visibility = "hide";window.status = " ";}
 else {/* NOP */}
 ppcTI = false;
 setCalendar();
 g_eltTarget = null;
 if (bIsIE) {var obj = document.ppcMonthList.sItem;}
 else if (bIsNN) {var obj = document.layers['PopUpCalendar'].document.layers['monthSelector'].document.ppcMonthList.sItem;}
 else {/* NOP */}
 obj.selectedIndex = 0;}
function showError(message)
{
 window.alert("[ PopUp Calendar ]\n\n" + message);
}

function isLeap(year) {
 if ((year%400==0)||((year%4==0)&&(year%100!=0))) {return true;}
 else {return false;}}

///////////////////////////////////////////////
// Fix for netscape year?
///////////////////////////////////////////////
function getFullYear(obj)
{
	if (bIsNN)
	{
		return obj.getYear() + 1900;
	}
	else
	{
		return obj.getYear();
	}
}

function validDate(date) {
 var reply = true;
 if (objRules == null)
 {/* NOP */}
 else
 {
  var arr = objRules.split(":");
  var mode = arr[0];
  var arg  = arr[1];
  var key  = arr[2].charAt(0).toLowerCase();
  if (key != "d")
  {
   var day = g_dateSelection.getDay();
   var orn = isEvenOrOdd(date);
   reply = (mode == "[^]") ? !((day == arg)&&((orn == key)||(key == "a"))) : ((day == arg)&&((orn == key)||(key == "a")));
  }
  else {reply = (mode == "[^]") ? (date != arg) : (date == arg);
  }
 }
 return reply;
}

function isEvenOrOdd(date) {
 if (date - 21 > 0) {return "e";}
 else if (date - 14 > 0) {return "o";}
 else if (date - 7 > 0) {return "e";}
 else {return "o";}}

function dateFormat(year,month,date) {
 if (ppcDF == null) {ppcDF = "m/d/Y";}
 var day = g_dateSelection.getDay();
 var crt = "";
 var str = "";
 var chars = ppcDF.length;
 for (var i = 0; i < chars; ++i) {
  crt = ppcDF.charAt(i);
  switch (crt) {
   case "M": str += ppcMN[month]; break;
   case "m": str += (month<9) ? ("0"+(++month)) : ++month; break;
   case "Y": str += year; break;
   case "y": str += year.substring(2); break;
   case "d": str += ((ppcDF.indexOf("m")!=-1)&&(date<10)) ? ("0"+date) : date; break;
   case "W": str += ppcWN[day]; break;
    default: str += crt;}}
 return unescape(str);}