/*

 #
 # Javascript file for index. js
 #

*/

 var Months = new Array(12);

 Months[0]  = "January";
 Months[1]  = "February";
 Months[2]  = "March";
 Months[3]  = "April";
 Months[4]  = "May";
 Months[5]  = "June";
 Months[6]  = "July";
 Months[7]  = "August";
 Months[8]  = "September";
 Months[9]  = "October";
 Months[10] = "November";
 Months[11] = "December";

 var CurrYear;
 var CurrMonth;
 var StartCol;

 var LatestDay;
 var LatestMonth;
 var LatestYear;

 var CalendarCreated = false;

 var OldHours    = -1;
 var OldMinutes  = -1;
 
 var Firefox;

 var OriginalSidebarHeight;

 var Captcha_SHA1;
 var CaptchaVerified;

 function init(ff, month, year)
 {
          Firefox = ff;
          CurrMonth = month - 1;
          CurrYear = year;

          window.onresize = position;
          position();
 }

 function position()
 {
          var div_maintitle = document.getElementById("div_maintitle");
          var div_sidebar = document.getElementById("div_sidebar");
          var doc_body = (Firefox) ? document.body.parentNode : document.body;

          if(!CalendarCreated)
          {
             makeCalendar();
          }

          if(parseInt(doc_body.clientWidth) >= 1024)
          {
             div_maintitle.style.width = doc_body.clientWidth + "px";
          }

          else
          {
             div_maintitle.style.width = "1024px";
          }

          if(document.getElementById("obj_journal"))
          {
             init_journal();
          }

          if(document.getElementById("obj_project"))
          {
             init_project();
          }

          div_sidebar.style.height = (doc_body.scrollHeight - 50) + "px";
          OriginalSidebarHeight = div_sidebar.style.height;
 }

 function makeCalendar()
 {
          CalendarCreated = true;

          var i;
          var j = 0;
          var calendar = document.getElementById("calendar").getElementsByTagName("tbody")[0];

          for(i = 0; i <= 6; i++)
          {
              var row = document.createElement("tr");
              row.id = "tr_daterow" + i;
              calendar.appendChild(row);

              for(j = 0; j <= 6; j++)
              {
                  var cell = document.createElement("td");
                  cell.id = "td_day" + i + "" + j;
                  cell.align = "center";
                  cell.style.color = "#777777";
                  cell.style.backgroundColor = "#ffffff";

                  row.appendChild(cell);
              }
          }

          populateCalendar(CurrMonth, CurrYear);
 }

 function populateCalendar(Month, Year)
 {
          var i;
          var j;
          var row = 0;
          var col = 0;
          var days = MaxDays[Month];
          var date;

          document.getElementById("td_MonthYear").firstChild.data = MonthNames[Month] + ", " + Year;

          days = ((Month == 1) && (Year % 4 == 0)) ? days + 1 : days;

          date = new Date(Year, Month, 1);
          col = date.getDay();
          StartCol = col;

          for(i = 0; i <= 6; i++)
          {
              for(j = 0; j <= 6; j++)
              {
                  var cell = document.getElementById("td_day" + i + "" + j);

                  if(cell.firstChild)
                  {
                     cell.removeChild(cell.firstChild);
                  }
             }
          }

          for(i = 1; i <= days; i++)
          {
              var cell = document.getElementById("td_day" + row + "" + col);

              cell.appendChild(document.createTextNode(i));

              col++;
              row = (col > 6) ? row + 1 : row;
              col = (col > 6) ? 0 : col;
          }

          addJournalEntriesToCalendar(CurrMonth, CurrYear);
 }

 function addJournalEntriesToCalendar(Month, Year)
 {
          var Request = getXMLHttpRequestObject();
          var FormData = "action=addJournalEntriesToCalendar&month=" + Month + "&year=" + Year;

          sendXMLHttpRequest(Request, "http://vivin.net/action.php", FormData, evalHttpResponse);
 }

 function generateCaptcha()
 {
          var Request = getXMLHttpRequestObject();
          var FormData = "action=generateCaptcha";
        
          sendXMLHttpRequest(Request, "http://vivin.net/action.php", FormData, evalHttpResponse);
 }

 function verifyCaptcha(captcha, responseHandler)
 { 
          var Request = getXMLHttpRequestObject();
          var FormData = "action=verifyCaptcha&captcha=" + captcha + "&hash=" + Captcha_SHA1;

          sendXMLHttpRequest(Request, "http://vivin.net/action.php", FormData, responseHandler);
 }

 function evalHttpResponse(code)
 {
          eval(code);
 }

 function decMonth()
 {
          CurrYear = (CurrMonth - 1 < 0) ? CurrYear - 1 : CurrYear;
          CurrMonth = (CurrMonth - 1 < 0) ? 11 : CurrMonth - 1;

          populateCalendar(CurrMonth, CurrYear);
 }

 function decYear()
 {
          CurrYear--;

          populateCalendar(CurrMonth, CurrYear);
 }

 function incMonth()
 {
          CurrYear = (CurrMonth + 1 > 11) ? CurrYear + 1 : CurrYear;
          CurrMonth = (CurrMonth + 1 > 11) ? 0 : CurrMonth + 1;

          populateCalendar(CurrMonth, CurrYear);
 }

 function incYear()
 {
          CurrYear++;

          populateCalendar(CurrMonth, CurrYear);
 }

 function highlightDate(date)
 {
          var col;
          var row;
          var day;
          var span;

          date--;
          col = (StartCol + date) % 7;
          row = Math.floor((StartCol + date) / 7);

          day = document.getElementById("td_day" + row + "" + col);
          span = document.createElement("span");
          span.className = "clickable";
          span.onclick = showJournalEntry;
          span.style.fontWeight = "bold";
          span.style.color = "#000731";
          span.style.textDecoration = "underline";
          span.appendChild(document.createTextNode(date + 1));
          span.data = date + 1;

          day.removeChild(day.firstChild);
          day.appendChild(span);       
 }

 function showJournalEntry(anevent)
 {
          var element;

          anevent = (anevent) ? anevent : event;

          if(anevent)
          {
             element = (anevent.target) ? anevent.target : anevent.srcElement;

             if(LatestMonth == (CurrMonth + 1) &&
                LatestDay == element.data &&
                LatestYear == CurrYear)
             {
                document.location = "http://vivin.net/journal/" + (CurrMonth + 1) + "/" + element.data + "/" + CurrYear + "/";
             }

             else
             {
                document.location = "http://vivin.net/archives/journal/" + (CurrMonth + 1) + "/" + element.data + "/" + CurrYear + "/";
             }
          }
 }

 function dropShadow(img_name, shadowHeightExtend, shadowWidthExtend)
 {
          var shadow = document.getElementById("sh_" + img_name);
          var img = document.getElementById("img_" + img_name);

          shadow.width = img.width + shadowWidthExtend;
          shadow.height = img.height + shadowHeightExtend;
 }

 function getXMLHttpRequestObject()
 {
          if(Firefox)
          {
             return new XMLHttpRequest();
          }

          else
          {
             return new ActiveXObject('Microsoft.XMLHTTP');
          }
 }

 function sendXMLHttpRequest(Request, uri, POSTdata, responseHandler)
 {
          Request.open("POST", uri, true);
          Request.setRequestHeader(
                                   'Content-Type',
                                   'application/x-www-form-urlencoded'
                                   );

          Request.onreadystatechange = function()
                                       {
                                                if(Request.readyState == 4)
                                                {
                                                   if(Request.status == 200)
                                                   {
                                                      responseHandler(Request.responseText);
                                                   }

                                                   else
                                                   {
                                                      alert("There was a problem retrieving data: " + Request.statusText);
                                                   }
                                                }
                                       }

          Request.send(POSTdata);
 }

 function setDateTime()
 {
          var span_welcome = document.getElementById("span_welcome");
          var Now          = new Date();
 
          var Hours   = Now.getHours();
          var Minutes = Now.getMinutes();
          var Seconds = Now.getSeconds();
 
          var ampm = "am";

          if(Hours > 12)
          {
             Hours -= 12;
             ampm = "pm";
          }
      
          else if(Hours == 12)
          {
               if(Minutes > 0)
               {
                  ampm = "pm";
               }

               else
               {
                  ampm = "noon";
               }
          }

          if(Minutes < 10)
          {
             Minutes = "0" + new String(Minutes);
          }

          if(Hours == 0)
          {
             Hours = 12;
          }

          if(Hours < 10)
          {
             Hours = "0" + new String(Hours);
          }

          if(Hours != OldHours || Minutes != OldMinutes)
          { 
             span_welcome.innerHTML = "Welcome to vivin.net. Today is " + Weekdays[Now.getDay()] + ", " + MonthNames[Now.getMonth()] + " " + Now.getDate() + 
                                      ", " + Now.getFullYear() + ". The time is " + Hours + ":" + Minutes + " " + ampm + ".";

             OldHours = Hours;
             OldMinutes = Minutes;
          }
 }
