Index: trunk/FACT++/www/shift/calendar.js
===================================================================
--- trunk/FACT++/www/shift/calendar.js	(revision 13748)
+++ trunk/FACT++/www/shift/calendar.js	(revision 13773)
@@ -380,9 +380,59 @@
             };
 
+            var tab = document.createElement("table");
+            tab.width="100%";
+            tab.style.border = 0;
+            tab.style.padding = 0;
+            tab.style.margin = 0;
+            tab.style.fontSize = "1.5em";
+            tab.style.backgroundColor = "transparent";
+            var tr0 = document.createElement("tr");
+            var td0 = document.createElement("td");
+            var td1 = document.createElement("td");
+            td0.style.textAlign = "left";
+            td1.style.textAlign = "right";
+            td1.style.textWeight= "normal";
+            td0.style.color = "lightgray";
+            td0.style.border=0;
+            td1.style.border=0;
+            td0.style.padding=0;
+            td1.style.padding=0;
+            tab.appendChild(tr0);
+            tr0.appendChild(td0);
+            tr0.appendChild(td1);
+            //td0.appendChild(document.createTextNode(txt));
+
+            td1.appendChild(td.firstChild);
+            td.appendChild(tab);
+
+            var IP    = this.getMoonPhase(this.yy, this.mm, i);
+            var str   = this.getMoonPhaseStr(IP);
+            var phase = 100-Math.abs(IP-0.5)*200;
+            var txt   = parseInt(phase+0.5,10)+"%";
+            if (phase>50)
+                td0.style.color = "gray";
+            if (phase<3.4)
+            {
+                txt = "o";
+                td0.style.textWeight = "bolder";
+                td0.style.fontSize = "0.7em";
+                td0.style.color = "darkgreen";
+            }
+            if (phase>96.6)
+            {
+                txt = "&bull;";
+                td0.style.textWeight = "bolder";
+                td0.style.fontSize = "0.8em";
+                td0.style.color = "darkred";
+            }
+            tab.title = str;
+            td0.innerHTML = txt;
+
             var sp = document.createElement("span");
             sp.appendChild(document.createTextNode("*"));
             sp.style.color="darkred";
             sp.style.display="none";
-            td.appendChild(sp);
+            td1.appendChild(sp);
+
 
             tr.appendChild( td );
@@ -460,5 +510,5 @@
                 return;
 
-            while (td.childNodes.length>2)
+            while (td.childNodes.length>1)
                 td.removeChild(td.lastChild);
 
@@ -482,5 +532,5 @@
             }
 
-            if (td.childNodes.length>2)
+            if (td.childNodes.length>1)
                 td.className += " enabled";
             else
@@ -533,13 +583,13 @@
 
             var td = document.getElementById(m+"-"+d);
-            alert(td.childNodes[1]+"/"+v);
+            var sp = td.firstChild.firstChild.lastChild.lastChild;
             if (v=="")
             {
-                td.childNodes[1].style.display="none";
+                sp.style.display="none";
                 td.title="Click to select this date.";
             }
             else
             {
-                td.childNodes[1].style.display="";
+                sp.style.display="";
                 td.title=v;
             }
@@ -573,17 +623,21 @@
 
             var td = document.getElementById(mm+"-"+dd);
-
-            c.color="#888";
-            if (xmlHttp.responseText=="")
-            {
-                c.value="enter comment here";
-                td.childNodes[1].style.display="none";
-                td.title="";
-            }
-            else
-            {
-                c.value = xmlHttp.responseText;
-                td.childNodes[1].style.display="";
-                td.title=xmlHttp.responseText;
+            var sp = td.firstChild.firstChild.lastChild.lastChild;
+
+            if (sp!=undefined)
+            {
+                c.color="#888";
+                if (xmlHttp.responseText=="")
+                {
+                    c.value="enter comment here";
+                    sp.style.display="none";
+                    td.title="";
+                }
+                else
+                {
+                    c.value = xmlHttp.responseText;
+                    sp.style.display="";
+                    td.title=xmlHttp.responseText;
+                }
             }
 
@@ -677,5 +731,6 @@
                 {
                     var td = document.getElementById(mm+"-"+dd);
-                    td.childNodes[1].style.display="";
+                    var sp = td.firstChild.firstChild.lastChild.lastChild;
+                    sp.style.display="";
                     td.title=com;
                 }
@@ -784,4 +839,43 @@
         this.show();
     }
+
+    this.getMoonPhase = function(Y, M, D)
+    {
+        // calculate the Julian date at 12h UT
+        var YY = Y - Math.floor( ( 12 - M ) / 10 );
+        var MM = ( M + 9 ) % 12;
+
+        var K1 = Math.floor( 365.25 * ( YY + 4712 ) );
+        var K2 = Math.floor( 30.6 * MM + 0.5 );
+        var K3 = Math.floor( Math.floor( ( YY / 100 ) + 49 ) * 0.75 ) - 38;
+
+        var JD = K1 + K2 + D + 59;  // for dates in Julian calendar
+        if ( JD > 2299160 )         // for Gregorian calendar
+            JD = JD - K3;
+
+        // calculate moon's age in days
+        var IP = ( ( JD - 2451550.1 ) / 29.530588853 ) % 1;
+        return IP;
+
+        // Moon's age
+        //var AG = IP*29.53;
+
+    }
+
+    this.getMoonPhaseStr = function(IP)
+    {
+        var phase = " ("+(100-Math.abs(IP-0.5)*200).toPrecision(2)+"%)";
+
+        if (IP*16 < 1) return "New moon" + phase;
+        if (IP*16 < 3) return "Evening crescent" + phase;
+        if (IP*16 < 5) return "First quarter" + phase;
+        if (IP*16 < 7) return "Waxing gibbous" + phase;
+        if (IP*16 < 9) return "Full moon" + phase;
+        if (IP*16 <11) return "Waning gibbous" + phase;
+        if (IP*16 <13) return "Last quarter" + phase;
+        if (IP*16 <15) return "Morning crescent" + phase;
+
+        return "New moon"+phase;
+    }
 }
 
