<!--
/*Copyright 1996 - Tomer and Yehuda Shiran
Feel free to "steal" this code provided that you leave this notice as is.
Additional examples from the book can be found at http://www.geocities.com/SiliconValley/9000/
For more information contact Tomer or Yehuda Shiran <yshiran@iil.intel.com>*/

var prevMonth, nextMonth, prevYear, nextYear;
var curMonth, curYear;

function getTime() {
	// initialize time-related variables with current time settings
	var now = new Date()
	var hour = now.getHours()
	var minute = now.getMinutes()
	now = null
	var ampm = ""

	// validate hour values and set value of ampm
	if (hour >= 12) {
		hour -= 12
		ampm = "PM"
	} else
		ampm = "AM"
	hour = (hour == 0) ? 12 : hour

	// add zero digit to a one digit minute
	if (minute < 10)
		minute = "0" + minute // do not parse this number!

	// return time string
	return hour + ":" + minute + " " + ampm
}

function leapYear(year) {
	if (year % 4 == 0) // basic rule
		return true // is leap year
	/* else */ // else not needed when statement is "return"
	return false // is not leap year
}

function getDays(month, year) {
	// create array to hold number of days in each month
	var ar = new Array(12)
	ar[0] = 31 // January
	ar[1] = (leapYear(year)) ? 29 : 28 // February
	ar[2] = 31 // March
	ar[3] = 30 // April
	ar[4] = 31 // May
	ar[5] = 30 // June
	ar[6] = 31 // July
	ar[7] = 31 // August
	ar[8] = 30 // September
	ar[9] = 31 // October
	ar[10] = 30 // November
	ar[11] = 31 // December

	// return number of days in the specified month (parameter)
	return ar[month]
}

function getMonthName(month) {
	// create array to hold name of each month
	var ar = new Array(12)
	ar[0] = "January"
	ar[1] = "February"
	ar[2] = "March"
	ar[3] = "April"
	ar[4] = "May"
	ar[5] = "June"
	ar[6] = "July"
	ar[7] = "August"
	ar[8] = "September"
	ar[9] = "October"
	ar[10] = "November"
	ar[11] = "December"

	// return name of specified month (parameter)
	return ar[month]
}

function setCal(month, year, layer) {
	// standard time attributes
	var now = new Date()
	curYear = now.getYear()
	curMonth = now.getMonth()
	
	month = (month == -1)? curMonth : month;
	year = (year == -1) ? curYear : year;

	if (year < 1000)
	    year+=1900
	var monthName = getMonthName(month)
//	var date = now.getDate()
	var date = 0
	now = null

	switch (month) {
		case 0:
			prevMonth = 11;
			prevYear = year -1;
			nextMonth = 1;
			nextYear = year
			break;
		case 11:
			prevMonth = 10;
			prevYear = year;
			nextMonth = 0;
			nextYear = year+1;
			break;
		default:
			prevMonth = month-1;
			prevYear = year;
			nextMonth = month+1;
			nextYear = year;
			break;
	}

	// create instance of first day of month, and extract the day on which it occurs
	var firstDayInstance = new Date(year, month, 1)
	var firstDay = firstDayInstance.getDay()
	firstDayInstance = null

	// number of days in current month
	var days = getDays(month, year)

	// call function to draw calendar
	drawCal(firstDay + 1, days, date, monthName, year, layer)
}

function drawCal(firstDay, lastDate, date, monthName, year, layer) {
	// constant table settings
	var headerHeight = 20 // height of the table's header cell
	var border = 0 // 3D height of table's border
	var cellspacing = 1 // width of table's border
	var headerColor = "midnightblue" // color of table's header
	var headerSize = "1" // size of tables header font
	var colWidth = 20 // width of columns in table
	var dayCellHeight = 25 // height of cells containing days of the week
	var dayColor = "darkblue" // color of font representing week days
	var cellHeight = 20 // height of cells representing dates in the calendar
	var todayColor = "red" // color specifying today's date in the calendar
	var timeColor = "purple" // color of font representing current time

	// create basic table structure
	var text = "" // initialize accumulative variable to empty string
	text += '<CENTER>'
	text += '<table border=0 cellspacing=1 cellpadding=0><tr><td class=datatablebg>'
	text += '<TABLE BORDER=' + border + ' CELLSPACING=' + cellspacing + '>' // table settings
	text += '<TH class=formbg COLSPAN=7 HEIGHT=' + headerHeight + '>' // create table header cell
	text += '<FONT COLOR="' + headerColor + '" SIZE=' + headerSize + '>' // set font for table header
	text += '<a href=javascript:setCal(' + prevMonth + ',' + prevYear + ',\'' +  layer + '\')><<</a>    ' + monthName + ' ' + year + '    <a href=javascript:setCal(' + nextMonth + ',' + nextYear + ',\'' + layer + '\')>>></a>'
	text += '</FONT>' // close table header's font settings
	text += '</TH>' // close header cell

	// variables to hold constant settings
	var openCol = '<TD WIDTH=' + colWidth + ' HEIGHT=' + dayCellHeight + '>'
	openCol += '<FONT COLOR="' + dayColor + '">'
	var closeCol = '</FONT></TD>'

	// create array of abbreviated day names
	var weekDay = new Array(7)
	weekDay[0] = "Sun"
	weekDay[1] = "Mon"

	weekDay[2] = "Tues"
	weekDay[3] = "Wed"
	weekDay[4] = "Thu"
	weekDay[5] = "Fri"
	weekDay[6] = "Sat"

	// create first row of table to set column width and specify week day
	text += '<TR ALIGN="center" VALIGN="center" class=frameMain>'
	for (var dayNum = 0; dayNum < 7; ++dayNum) {
		text += openCol + weekDay[dayNum] + closeCol
	}
	text += '</TR>'

	// declaration and initialization of two variables to help with tables
	var digit = 1
	var curCell = 1

	for (var row = 1; row <= Math.ceil((lastDate + firstDay - 1) / 7); ++row) {
		text += '<TR ALIGN="right" VALIGN="top" class=frameMain>'
		for (var col = 1; col <= 7; ++col) {
			if (digit > lastDate)
			break
			if (curCell < firstDay) {
				text += '<TD></TD>';
				curCell++
			}
			else {
				if (digit == date) { // current cell represent today's date
				text += '<TD HEIGHT=' + cellHeight + '>'
				text += '<FONT COLOR="' + todayColor + '">'
				text += digit
				text += '</FONT><BR>'
				//text += '<FONT COLOR="' + timeColor + '" SIZE=2>'
				//text += '<CENTER>' + getTime() + '</CENTER>'
				//text += '</FONT>'
				text += '</TD>'
			} else
				text += '<TD HEIGHT=' + cellHeight + '>' + digit + '</TD>'
				digit++
		}
	}
	text += '</TR>'
	}

	// close all basic table tags
	text += '</TABLE></td></tr></table>'
	text += '</CENTER>'

	MM_setTextOfLayer(layer,'',text);
}


function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_setTextOfLayer(objName,x,newText) { //v3.0
  if ((obj=MM_findObj(objName))!=null) with (obj)
    if (navigator.appName=='Netscape') {document.write(unescape(newText)); document.close();}
    else innerHTML = unescape(newText);
}

function MM_dragLayer(objName,x,hL,hT,hW,hH,toFront,dropBack,cU,cD,cL,cR,targL,targT,tol,dropJS,et,dragJS) { //v 3.6
  //Copyright 1998 Macromedia, Inc. All rights reserved.
  var i,j,aLayer,retVal,curDrag=null,NS=document.layers, MZ=(!document.all && document.getElementById),curLeft, curTop;
  if (!document.all && !document.layers && !document.getElementById) return false;
  retVal = true; if(!NS && !MZ && event) event.returnValue = true;
  if (MM_dragLayer.arguments.length > 1) {
    curDrag = (MZ)?document.getElementById(objName):MM_findObj(objName); if (!curDrag) return false;
    if (!document.allLayers) { document.allLayers = new Array();
      with (document) if (NS) { for (i=0; i<layers.length; i++) allLayers[i]=layers[i];
        for (i=0; i<allLayers.length; i++) if (allLayers[i].document && allLayers[i].document.layers)
          with (allLayers[i].document) for (j=0; j<layers.length; j++) allLayers[allLayers.length]=layers[j];
      } else if(MZ){
            var mzall = getElementsByTagName("div");
            for (i=0;i<mzall.length;i++) if (mzall[i].style&&mzall[i].style.position) allLayers[allLayers.length]=mzall[i];
          } else {
                for (i=0;i<all.length;i++) if (all[i].style&&all[i].style.position) allLayers[allLayers.length]=all[i];
          }}
    curDrag.MM_dragOk=true; curDrag.MM_targL=targL; curDrag.MM_targT=targT;
    curDrag.MM_tol=Math.pow(tol,2); curDrag.MM_hLeft=hL; curDrag.MM_hTop=hT;
    curDrag.MM_hWidth=hW; curDrag.MM_hHeight=hH; curDrag.MM_toFront=toFront;
    curDrag.MM_dropBack=dropBack; curDrag.MM_dropJS=dropJS;
    curDrag.MM_everyTime=et; curDrag.MM_dragJS=dragJS;
    curDrag.MM_oldZ = (NS)?curDrag.zIndex:curDrag.style.zIndex;
    curLeft= (NS)?curDrag.left:(MZ)?curDrag.style.left.replace("px","")/1:curDrag.style.pixelLeft;
        curDrag.MM_startL = curLeft;
    curTop = (NS)?curDrag.top:(MZ)?curDrag.style.top.replace("px","")/1:curDrag.style.pixelTop;
        curDrag.MM_startT = curTop;
    curDrag.MM_bL=(cL<0)?null:curLeft-cL; curDrag.MM_bT=(cU<0)?null:curTop -cU;
    curDrag.MM_bR=(cR<0)?null:curLeft+cR; curDrag.MM_bB=(cD<0)?null:curTop +cD;
    curDrag.MM_LEFTRIGHT=0; curDrag.MM_UPDOWN=0; curDrag.MM_SNAPPED=false; //use in your JS!
    document.onmousedown = MM_dragLayer; document.onmouseup = MM_dragLayer;
    if (NS||MZ) document.captureEvents(Event.MOUSEDOWN|Event.MOUSEUP);
  } else {
    var theEvent = ((NS||MZ)?objName.type:event.type);
    if (theEvent == 'mousedown') {
      var mouseX = (NS||MZ)?objName.pageX : event.clientX + document.body.scrollLeft;
      var mouseY = (NS||MZ)?objName.pageY : event.clientY + document.body.scrollTop;
      var maxDragZ=null; document.MM_maxZ = 0;
      for (i=0; i<document.allLayers.length; i++) { aLayer = document.allLayers[i];
        var aLayerZ = (NS)?aLayer.zIndex:aLayer.style.zIndex/1;
        if (aLayerZ > document.MM_maxZ) document.MM_maxZ = aLayerZ;
        var isVisible = (((NS)?aLayer.visibility:aLayer.style.visibility).indexOf('hid') == -1);
        if (aLayer.MM_dragOk != null && isVisible) with (aLayer) {
          var parentL=0; var parentT=0;
          if (!NS) { parentLayer = (MZ)?aLayer.parentNode:aLayer.parentElement;
            while (parentLayer != null && parentLayer.style.position) {
              parentL += parentLayer.offsetLeft/1; parentT += parentLayer.offsetTop/1;
              parentLayer = (MZ)?parentLayer.parentNode:parentLayer.parentElement;
          } }
          var tmpX=mouseX-(((NS)?pageX:((MZ)?style.left.replace("px","")/1:style.pixelLeft)+parentL)+MM_hLeft);
          var tmpY=mouseY-(((NS)?pageY:((MZ)?style.top.replace("px","")/1:style.pixelTop) +parentT)+MM_hTop);
          var tmpW = MM_hWidth;  if (tmpW <= 0) tmpW += ((NS)?clip.width :offsetWidth);
          var tmpH = MM_hHeight; if (tmpH <= 0) tmpH += ((NS)?clip.height:offsetHeight);
          if ((0 <= tmpX && tmpX < tmpW && 0 <= tmpY && tmpY < tmpH) && (maxDragZ == null
              || maxDragZ <= aLayerZ)) { curDrag = aLayer; maxDragZ = aLayerZ; } } }
      if (curDrag) {
        document.onmousemove = MM_dragLayer; if (NS) document.captureEvents(Event.MOUSEMOVE);
        curLeft = (NS)?curDrag.left:(MZ)?curDrag.style.left.replace("px","")/1:curDrag.style.pixelLeft;
        curTop = (NS)?curDrag.top:(MZ)?curDrag.style.top.replace("px","")/1:curDrag.style.pixelTop;
        MM_oldX = mouseX - curLeft; MM_oldY = mouseY - curTop;
        document.MM_curDrag = curDrag;  curDrag.MM_SNAPPED=false;
        if(curDrag.MM_toFront) {
          eval('curDrag.'+((NS)?'':'style.')+'zIndex=document.MM_maxZ'+((MZ)?'':'+1') );
          if (!curDrag.MM_dropBack) document.MM_maxZ++; }
        retVal = false; if(!NS&&!MZ) event.returnValue = false;
    } } else if (theEvent == 'mousemove') {
      if (document.MM_curDrag) with (document.MM_curDrag) {
        var mouseX = (NS||MZ)?objName.pageX : event.clientX + document.body.scrollLeft;
        var mouseY = (NS||MZ)?objName.pageY : event.clientY + document.body.scrollTop;
        newLeft = mouseX-MM_oldX; newTop  = mouseY-MM_oldY;
        if (MM_bL!=null) newLeft = Math.max(newLeft,MM_bL);
        if (MM_bR!=null) newLeft = Math.min(newLeft,MM_bR);
        if (MM_bT!=null) newTop  = Math.max(newTop ,MM_bT);
        if (MM_bB!=null) newTop  = Math.min(newTop ,MM_bB);
        MM_LEFTRIGHT = newLeft-MM_startL; MM_UPDOWN = newTop-MM_startT;
        if (NS) {left = newLeft; top = newTop;}
                else if (MZ){style.left = newLeft; style.top = newTop;}
        else {style.pixelLeft = newLeft; style.pixelTop = newTop;}
        if (MM_dragJS) eval(MM_dragJS);
        retVal = false; if(!NS&&!MZ) event.returnValue = false;
    } } else if (theEvent == 'mouseup') {
      document.onmousemove = null;
      if (NS||MZ) document.releaseEvents(Event.MOUSEMOVE);
      if (NS||MZ) document.captureEvents(Event.MOUSEDOWN); //for mac NS
      if (document.MM_curDrag) with (document.MM_curDrag) {
        if (typeof MM_targL =='number' && typeof MM_targT == 'number' &&
            (Math.pow(MM_targL-((NS)?left:(MZ)?style.left.replace("px","")/1:style.pixelLeft),2)+
             Math.pow(MM_targT-((NS)?top:(MZ)?style.top.replace("px","")/1:style.pixelTop),2))<=MM_tol) {
          if (NS) {left = MM_targL; top = MM_targT;}
                  else if(MZ){style.left = MM_targL; style.top = MM_targT;}
          else {style.pixelLeft = MM_targL; style.pixelTop = MM_targT;}
          MM_SNAPPED = true; MM_LEFTRIGHT = MM_startL-MM_targL; MM_UPDOWN = MM_startT-MM_targT; }
        if (MM_everyTime || MM_SNAPPED) eval(MM_dropJS);
        if(MM_dropBack) {if (NS) zIndex = MM_oldZ; else style.zIndex = MM_oldZ;}
        retVal = false; if(!NS&&!MZ) event.returnValue = false; }
      document.MM_curDrag = null;
    }
    if (NS||MZ) document.routeEvent(objName);
  } return retVal;
}
