/**
 * Function displays calendar for certain month and year.
 */
function displayCalendar(doc, mon, realYear) 
{
    var i,oneDay,day;
    myPage = "";
    monthsList = ["January", "February", "March", "April", 
                  "May", "June", "July", "August", "September", 
                  "October", "November", "December"];

    // Here we apply doom' day rule to find day of the week
    // See doom's day rule - the most popular algorithm

    myPage += "<html><head><title>Calendar</title>\n";      

    myPage += "</head>\n<body>";
    myPage += "<center> <table border=\"0\" width=\"120\" cellpadding=\"2\" cellspacing=\"1\"> \n";

    myPage += "<tr BGCOLOR=\"#6699FF\">";
    myPage += "<td ALIGN=CENTER COLSPAN=\"7\"><font face=\"Arial, helv\"><font size=-1>" + monthsList[mon-1];
    myPage += ", " + realYear + "</font></font></td>";
    myPage += "</tr>";

    myPage += "<tr ALIGN=CENTER>";
    myPage += "<td WIDTH=\"25\" BGCOLOR=\"#FFFF99\"><b><font face=\"Arial, helv\"><font size=-1>S</font></font></b></td>";
    myPage += "<td WIDTH=\"25\" BGCOLOR=\"#FFFF99\"><b><font face=\"Arial, helv\"><font size=-1>M</font></font></b></td>";
    myPage += "<td WIDTH=\"25\" BGCOLOR=\"#FFFF99\"><b><font face=\"Arial, helv\"><font size=-1>T</font></font></b></td>";
    myPage += "<td WIDTH=\"25\" BGCOLOR=\"#FFFF99\"><b><font face=\"Arial, helv\"><font size=-1>W</font></font></b></td>";
    myPage += "<td WIDTH=\"25\" BGCOLOR=\"#FFFF99\"><b><font face=\"Arial, helv\"><font size=-1>T</font></font></b></td>";
    myPage += "<td WIDTH=\"25\" BGCOLOR=\"#FFFF99\"><b><font face=\"Arial, helv\"><font size=-1>F</font></font></b></td>";
    myPage += "<td WIDTH=\"25\" BGCOLOR=\"#FFFF99\"><b><font face=\"Arial, helv\"><font size=-1>S</font></font></b></td>";
    myPage += "</tr>";

    for(oneDay = 1, day = 0; day != -1; )
    {
        myPage += "<tr ALIGN=CENTER>\n";
        for(i = 0; i < 7; i++)
        {
            day = dayOfWeek(oneDay, mon, realYear); 
  
            if(i == day && day != -1)
            { 
                myPage += "<td WIDTH=\"25\" BGCOLOR=\"#99CCFF\"> " + "<a href=\"#\" " + "onClick=\"javascript:top.self.opener.vs()\">" + oneDay + "</a>" + " </td>\n";
                oneDay++;
            } else {
                myPage += "<td WIDTH=\"25\" BGCOLOR=\"#99CCFF\"> &nbsp; </td>\n";
            }
        }
        myPage += "</tr>\n";
     }

    myPage += " </table>\n";
    myPage += "<P><form>\n";
    myPage += "<INPUT TYPE=\"button\" VALUE=\"Close\" onClick=\"javascript:top.self.opener.newWind = null; top.window.close()\">\n";
    myPage += "<P></form></Center>\n";
    myPage += "</body></html>\n";

    doc.clear();
    doc.write(myPage);
    doc.close();
}

/**
 * Given month and year function returns day od the week.
 */
function dayOfWeek(day, mon, realYear)
{
    var days = 31; // For January - default.
    var month = mon;
    var year = realYear - 1900;

    // Here we apply doom' day rule to find day of the week
    // See doom's day rule - the most popular algorithm

    var doomDay = 3 + year + Math.floor(year/4) + Math.floor((year + 300)/400) - Math.floor(year/100);
    if(month == 9 || month == 5) month = 14 - month;
    else
        if(month == 7 || month == 11) month = 18 - month;
        else 
            if(month == 3) month = 7;
            else
                if(month == 1 || month == 2)
                {  
                    if(realYear%4==0 && (realYear%100 != 0 || realYear%400 ==0)) month = 29 + (month%2)*3;
                    else month = 28 + (month%2)*3;
                }

    // Find the number of days in a month
    if(mon > 2 && mon < 12) // for March, ..., December
        days = 31 - ((mon + 2)%5)%2;
    else
        if(mon == 2)
        {
            // Is it leap year?
            if(realYear%4==0 && (realYear%100 != 0 || realYear%400 ==0)) days = 29;
            else days = 28;
        }
    
    if(realYear < 1900 || day < 1 || day > days || mon < 1 || mon > 12) return -1; // Conditions do not hold

    return Math.abs(doomDay + day - month) % 7; 
}

/**
 * Create a global variable so the window object can be accessed elsewhere
 * in the page if necessary
 */
newWind = null
/**
 * The function opens new window.
 */
function createWindow() 
{
   var m = calRef[0][0].selectedIndex;
   var y = calRef[0][1].selectedIndex;
   if (calRef[0][0].options[m].value == -1 || calRef[0][1].options[y].value == -1)  
   {
       alert("Please specify month and year at first");
       return false;
   }
   if (newWind == null)
   {
      newWind = window.open("./html/frameCalendar.html",calRef[0][0].options[m].value +
                "_" + calRef[0][1].options[y].value,"top=200,left=200,height=270,width=250,resizable=no");
   }
   else {
      newWind.focus();
   }
   return true;
}

/**
 * Auxiliary function to transfer date
 */
function vs() 
{
    window.focus();
    newWind.close(); // Close the date choice window
    newWind = null;
}

/**
 * Function returns full year.
 */
function getFullYear()
{
    var date = new Date
    var year = date.getYear();
    return year < 1000 ? year + 1900 : year
}

/**
 * Function gets year in the current century.
 */
function getCenturyYear()
{
    var date = new Date
    return date.getYear() % 100;
}

