/*
  Original:  Kedar R. Bhave (softricks@hotmail.com)
  Web Site:  http://www.softricks.com
  This script and many more are available free online at
  The JavaScript Source!! http://javascript.internet.com

  modifications and customizations to work with the "overLIB" library:
  Author:   James B. O'Connor (joconnor@nordenterprises.com)
  Web Site: http://www.nordenterprises.com
  developed for use with http://home-owners-assoc.com
  Note: while overlib works fine with Netscape 4, this function does not work very well, since portions of the "over" div
    end up under other fields on the form and cannot be seen.  If you really want to use this with NS4,
    you'll need to change the positioning in the overlib() call to make sure the "over" div gets positioned
    away from all other form fields
  you can get overLIB from:
  overLIB 3.50  --  This notice must remain untouched at all times.
  Copyright Erik Bosrup 1998-2001. All rights reserved.
  By Erik Bosrup (erik@bosrup.com).  Last modified 2001-08-28.
  Portions by Dan Steinman (dansteinman.com). Additions by other people are
  listed on the overLIB homepage.
  Get the latest version at http://www.bosrup.com/web/overlib/
*/

var weekend = [0,6];

//var gNow = new Date();
var gNow = getDateObject(today); // select the server date
var ggWinContent;
var ggPosX = -1;
var ggPosY = -1;

// Non-Leap year Month days..
Calendar.DOMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// Leap year Month days..
Calendar.lDOMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

function Calendar(p_item, p_month, p_year, p_format) {
  if ((p_month == null) && (p_year == null))
    return;

  if (p_month == null) {
    return;
  }
  else {
    this.gMonthName = Calendar.get_month(p_month);
    this.gMonth = new Number(p_month);
  }

  this.gYear = p_year;
  this.gFormat = p_format;
  this.gBGColor = "white";
  this.gFGColor = "black";
  this.gReturnItem = p_item;
} // Calender

Calendar.get_month = Calendar_get_month;
Calendar.get_daysofmonth = Calendar_get_daysofmonth;
Calendar.calc_month_year = Calendar_calc_month_year;

function Calendar_get_month(monthNo) {
  return js_nameofMonths[monthNo];
} // Calendar_get_month

function Calendar_get_daysofmonth(monthNo, p_year) {
  /* 
    Check for leap year ..
    1.Years evenly divisible by four are normally leap years, except for... 
    2.Years also evenly divisible by 100 are not leap years, except for... 
    3.Years also evenly divisible by 400 are leap years. 
  */
  if ((p_year % 4) == 0) {
    if ((p_year % 100) == 0 && (p_year % 400) != 0)
      return Calendar.DOMonth[monthNo];
  
    return Calendar.lDOMonth[monthNo];
  } else
    return Calendar.DOMonth[monthNo];
} // Calendar_get_daysofmonth

function Calendar_calc_month_year(p_Month, p_Year, incr) {
  /* 
    Will return an 1-D array with 1st element being the calculated month 
    and second being the calculated year 
    after applying the month increment/decrement as specified by 'incr' parameter.
    'incr' will normally have 1/-1 to navigate thru the months.
  */
  var ret_arr = new Array();
  
  if (incr == -1) {
    // B A C K W A R D
    if (p_Month == 0) {
      ret_arr[0] = 11;
      ret_arr[1] = parseInt(p_Year) - 1;
    }
    else {
      ret_arr[0] = parseInt(p_Month) - 1;
      ret_arr[1] = parseInt(p_Year);
    }
  } else if (incr == 1) {
    // F O R W A R D
    if (p_Month == 11) {
      ret_arr[0] = 0;
      ret_arr[1] = parseInt(p_Year) + 1;
    }
    else {
      ret_arr[0] = parseInt(p_Month) + 1;
      ret_arr[1] = parseInt(p_Year);
    }
  }
  
  return ret_arr;
} // Calendar_calc_month_year

// This is for compatibility with Navigator 3, we have to create and discard one object before the prototype object exists.
new Calendar();

Calendar.prototype.getMonthlyCalendarCode = function() {
  var vCode = "";
  var vHeader_Code = "";
  var vData_Code = "";

  // Begin Table Drawing code here..
  vCode += ("<div align=center><table cellspacing='1' cellpadding='0' bgcolor='" + this.gBGColor + "'><tr><td height='15px'>&nbsp;</td></tr>");

  vHeader_Code = this.cal_header();
  vData_Code = this.cal_data();
  vCode += (vHeader_Code + vData_Code);
  vCode += "<tr><td height='15px'>&nbsp;</td></tr></table></div>";

  return vCode;
}

Calendar.prototype.show = function() {
  var vCode = "";

  // Show navigation buttons
  var prevMMYYYY = Calendar.calc_month_year(this.gMonth, this.gYear, -1);
  var prevMM = prevMMYYYY[0];
  var prevYYYY = prevMMYYYY[1];

  var nextMMYYYY = Calendar.calc_month_year(this.gMonth, this.gYear, 1);
  var nextMM = nextMMYYYY[0];
  var nextYYYY = nextMMYYYY[1];

  // build content into global var ggWinContent
  ggWinContent += ("<table width='100%' CELLSPACING=0 CELLPADDING=0 style='text-underline: none;'>");
  // Ein Jahr zurueck
  ggWinContent += ("<tr><td class='bgGrey' align='left'>");
  ggWinContent += ("&nbsp;<a href=\"javascript:void(0);\" style='text-decoration:none;' " +
    "onMouseOver=\"window.status='"+js_aYear+" "+js_back+"'; return true;\" " +
    "onMouseOut=\"window.status=''; return true;\" " +
    "onClick=\"Build(" + 
    "'" + this.gReturnItem + "', '" + this.gMonth + "', '" + (parseInt(this.gYear)-1) + "', '" + this.gFormat + "'" +
    ");" +
    "\">&lt;<\/A></TD>");
  // Jahr
  ggWinContent += ("<td class='bgGrey' align='center'><b>");
  ggWinContent += (this.gYear);
  ggWinContent += "</b></td>";
  // Ein Jahr vorwaerts
  ggWinContent += ("<td class='bgGrey' align='right'><a href=\"javascript:void(0);\" style='text-decoration:none;'" +
    "onMouseOver=\"window.status='"+js_aYear+" "+js_forward+"'; return true;\" " +
    "onMouseOut=\"window.status=''; return true;\" " +
    "onClick=\"Build(" + 
    "'" + this.gReturnItem + "', '" + this.gMonth + "', '" + (parseInt(this.gYear)+1) + "', '" + this.gFormat + "'" +
    ");" +
    "\">&gt;<\/A>&nbsp;</TD></tr>");
  // Ein Monat zurueck
  ggWinContent += ("<tr><td class='bgGrey' align='left'>&nbsp;<a href=\"javascript:void(0);\" style='text-decoration:none;'" +
    "onMouseOver=\"window.status='"+js_aMonth+" "+js_back+"'; return true;\" " +
    "onMouseOut=\"window.status=''; return true;\" " +
    "onClick=\"Build(" + 
    "'" + this.gReturnItem + "', '" + prevMM + "', '" + prevYYYY + "', '" + this.gFormat + "'" +
    ");" +
    "\">&lt;<\/A></TD>");
  // Monat
  ggWinContent += ("<td class='bgGrey' align='center'><b>");
  ggWinContent += (this.gMonthName);
  ggWinContent += "</b></td>";
  // Ein Monat vorwaerts
  ggWinContent += "<td class='bgGrey' align='right'>";
  ggWinContent += ("<A HREF=\"javascript:void(0);\" style='text-decoration:none;' " +
    "onMouseOver=\"window.status='"+js_aMonth+" "+js_forward+"'; return true;\" " +
    "onMouseOut=\"window.status=''; return true;\" " +
    "onClick=\"Build(" + 
    "'" + this.gReturnItem + "', '" + nextMM + "', '" + nextYYYY + "', '" + this.gFormat + "'" +
    ");" +
    "\">&gt;<\/a>&nbsp;</td></tr>");
  ggWinContent += "<tr><td ></td></tr></table>";

  // Get the complete calendar code for the month, and add it to the
  //  content var
  vCode = this.getMonthlyCalendarCode();
  ggWinContent += vCode;
  
  // Close-Button
  ggWinContent += "<table width='100%' CELLSPACING=0 CELLPADDING=0>";
  ggWinContent += "<tr><td class='bgGrey' align='right' height='24px' valign='middle'>";
  ggWinContent += "<A HREF=\"/\" onClick=\"return cClick();\" style=\"text-decoration:none;\">" + js_close + "&nbsp;&gt;</A>&nbsp;";
  ggWinContent += "</td></tr>";
  ggWinContent += "</table>";
}

Calendar.prototype.cal_header = function() {
  var vCode = "";

  vCode = vCode + "<tr>";
  for (k=0; k<js_shortNameofDays.length; k++) {
  	vCode = vCode + "<td class='tableHeader' WIDTH='14%'>" + js_shortNameofDays[k] + "</td>";
  }
  vCode = vCode + "</tr>";

  return vCode;
}

Calendar.prototype.cal_data = function() {
  var vDate = new Date();
  vDate.setDate(1);
  vDate.setMonth(this.gMonth);
  vDate.setFullYear(this.gYear);

  var vFirstDay=vDate.getDay();
  var vDay=1;
  var vLastDay=Calendar.get_daysofmonth(this.gMonth, this.gYear);
  var vOnLastDay=0;
  var vCode = "";

  /*
    Get day for the 1st of the requested month/year..
    Place as many blank cells before the 1st day of the month as necessary. 
  */
  vCode = vCode + "<tr>";
  if ( vFirstDay > 0 ) {
    for (i=1; i<vFirstDay; i++) {
      vCode = vCode + "<td width='14%'" + this.write_weekend_string(i) + ">&#160;</td>";
    }

    // Write rest of the 1st week
    for (i=vFirstDay; i<8; i++) {
      j = i % 7;
      vCode = vCode + "<td width='14%'" + this.write_weekend_string(j) + ">" + 
        "<a href='javascript:void(0);' style='text-decoration:none; color:#59819F;' " + 
          "onMouseOver=\"window.status='"+js_setDate+" " + this.format_data(vDay) + "'; return true;\" " +
          "onMouseOut=\"window.status=' '; return true;\" " +
          "onClick=\"document." + this.gReturnItem + ".value='" + 
          this.format_data(vDay) + 
          "';ggPosX=-1;ggPosY=-1;nd();nd();\">" + 
          this.format_day(vDay) + 
        "</a>" + 
        "</td>";
      vDay=vDay + 1;
    }

  }
  else {
    for (i=1; i<7; i++) {
      vCode = vCode + "<td width='14%'" + this.write_weekend_string(i) + "> </td>";
    }
    vCode = vCode + "<td width='14%'" + this.write_weekend_string(0) + ">" + 
      "<a href='javascript:void(0); style='text-decoration:none; color:#59819F;'' " + 
        "onMouseOver=\"window.status='"+js_setDate+" " + this.format_data(vDay) + "'; return true;\" " +
        "onMouseOut=\"window.status=' '; return true;\" " +
        "onClick=\"document." + this.gReturnItem + ".value='" + 
        this.format_data(vDay) + 
        "';ggPosX=-1;ggPosY=-1;nd();nd();\">" + 
        this.format_day(vDay) + 
      "</a>" + 
      "</td>";
    vDay=vDay + 1;
  }

  vCode = vCode + "</TR>";

  // Write the rest of the weeks
  for (k=2; k<7; k++) {
    vCode = vCode + "<tr>";

    for (i=1; i<8; i++) {
      j = i % 7;
      vCode = vCode + "<td width='14%'" + this.write_weekend_string(j) + ">" + 
        "<a href='javascript:void(0);' style='text-decoration:none; color:#59819F;' " +
          "onMouseOver=\"window.status='"+js_setDate+" " + this.format_data(vDay) + "'; return true;\" " +
          "onMouseOut=\"window.status=' '; return true;\" " +
          "onClick=\"document." + this.gReturnItem + ".value='" + 
          this.format_data(vDay) + 
          "';window.scroll(0,ggPosY);ggPosX=-1;ggPosY=-1;nd();nd();\">" + 
        this.format_day(vDay) + 
        "</a>" + 
        "</td>";
      vDay=vDay + 1;

      if (vDay > vLastDay) {
        vOnLastDay = 1;
        break;
      }
    }

    if (j == 0)
      vCode = vCode + "</tr>";
    if (vOnLastDay == 1)
      break;
  }

  // Fill up the rest of last week with proper blanks, so that we get proper square blocks
  if ( j != 0 ) {
    for (m=1; m<(7-j); m++) {
      vCode = vCode + "<td width='14%'" + this.write_weekend_string(j+m) + ">&#160;</td>";
    }
    vCode = vCode + "<td width='14%'" + this.write_weekend_string(0) + ">&#160;</td>";
  }

  return vCode;
}

Calendar.prototype.format_day = function(vday) {
  var vNowDay = gNow.getDate();
  var vNowMonth = gNow.getMonth();
  var vNowYear = gNow.getFullYear();

  if (vday == vNowDay && this.gMonth == vNowMonth && this.gYear == vNowYear)
    return ("<font color=\"red\"><b>" + vday + "</b></font>");
  else
    return (vday);
}

Calendar.prototype.write_weekend_string = function(vday) {
  var i;

  // Return special formatting for the weekend day.
  for (i=0; i<weekend.length; i++) {
    if (vday == weekend[i])
      return (" class='tableLightGrey'");
  }
  
  return " class='tableWhite'";
}

Calendar.prototype.format_data = function(p_day) {
  var vData;
  var vMonth = 1 + this.gMonth;
  vMonth = (vMonth.toString().length < 2) ? "0" + vMonth : vMonth;
  var vMon = Calendar.get_month(this.gMonth).substr(0,3).toUpperCase();
  var vFMon = Calendar.get_month(this.gMonth).toUpperCase();
  var vY4 = new String(this.gYear);
  var vY2 = new String(this.gYear.substr(2,2));
  var vDD = (p_day.toString().length < 2) ? "0" + p_day : p_day;

  switch (this.gFormat) {
    case "DD.MM.YYYY" :
      vData = vDD+ "." + vMonth + "." + vY4;
      break;
    default :
      vData = vDD+ "." + vMonth + "." + vY4;
  }

  return vData;
}

function Build(p_item, p_month, p_year, p_format) {
  gCal = new Calendar(p_item, p_month, p_year, p_format);

  // Customize your Calendar here..
  gCal.gBGColor="white";
  gCal.gLinkColor="black";

  // initialize the content string
  ggWinContent = "";

  // Choose appropriate show function
  gCal.show();

  // if this is the first calendar popup, use autopositioning with an offset
  if (ggPosX == -1 && ggPosY == -1) {
    overlib(ggWinContent, AUTOSTATUSCAP, STICKY, CLOSECLICK, CSSSTYLE,
      TEXTSIZEUNIT, "pt", TEXTSIZE, 8, CAPTIONSIZEUNIT, "pt", CAPTIONSIZE, 8, CLOSESIZEUNIT, "pt", CLOSESIZE, 8,
      CAPTION, "&nbsp;"+js_kalender_title, OFFSETX, 20, OFFSETY, -20);
    // save where the 'over' div ended up; we want to stay in the same place if the user
    //  clicks on one of the year or month navigation links
    if ( (ns4) || (ie4) ) {
      ggPosX = parseInt(over.left);
      ggPosY = parseInt(over.top);
    }
    else if (ns6) {
      ggPosX = parseInt(over.style.left);
      ggPosY = parseInt(over.style.top);
    }
  }
  else {
    // we have a saved X & Y position, so use those with the FIXX and FIXY options
    overlib(ggWinContent, AUTOSTATUSCAP, STICKY, CLOSECLICK, CSSSTYLE,
      TEXTSIZEUNIT, "pt", TEXTSIZE, 8, CAPTIONSIZEUNIT, "pt", CAPTIONSIZE, 8, CLOSESIZEUNIT, "pt", CLOSESIZE, 8,
      CAPTION, "&nbsp;"+js_kalender_title, FIXX, ggPosX, FIXY, ggPosY);
  }
  window.scroll(ggPosX, ggPosY);
}

function show_calendar() {
  /* 
    p_month : 0-11 for Jan-Dec; 12 for All Months.
    p_year  : 4-digit year
    p_format: Date format (mm/dd/yyyy, dd/mm/yy, ...)
    p_item  : Return Item.
  */

  p_item = arguments[0];
  if (arguments[1] == null)
    p_month = new String(gNow.getMonth());
  else
    p_month = arguments[1];
  if (arguments[2] == "" || arguments[2] == null)
    p_year = new String(gNow.getFullYear().toString());
  else
    p_year = arguments[2];
  if (arguments[3] == null)
    p_format = "DD.MM.YYYY";
  else
    p_format = arguments[3];

  Build(p_item, p_month, p_year, p_format);
}

// Zusaetzliche Methode fuer die relative Positionierung
//

function showCalendar(element, offsetX, offsetY ) {
  ggPosX = DL_GetElementLeft(element)+offsetX;
  ggPosY = DL_GetElementTop(element)+offsetY;
  show_calendar(document.forms[0].name+"."+element.name);
}

function DL_GetElementLeft(eElement) {
  var nLeftPos = eElement.offsetLeft; // initialize var to store calculations
  var eParElement = eElement.offsetParent; // identify first offset parent element
  while (eParElement != null) { // move up through element hierarchy
    nLeftPos += eParElement.offsetLeft; // appending left offset of each parent
    eParElement = eParElement.offsetParent; // until no more offset parents exist
  }
  return nLeftPos; // return the number calculated 
} 

function DL_GetElementTop(eElement) { 
  var nTopPos = eElement.offsetTop; // initialize var to store calculations 
  var eParElement = eElement.offsetParent; // identify first offset parent element 
  while (eParElement != null) { // move up through element hierarchy 
    nTopPos += eParElement.offsetTop; // appending top offset of each parent 
    eParElement = eParElement.offsetParent; // until no more offset parents exist 
  }
  return nTopPos; // return the number calculated 
}


