/*  Copyright © 2001, 2002 OGMA Consulting Corp. */

var weekend = [0,6];
var weekendColor = "#e0e0e0";
var fontface = "Arial";
var fontsize = '12pt';

var gNow = new Date();
var ggWinCal;

var todayStr;
var selectedLanguage;

function setLanguage(p_language) {

  if ((p_language == null) || (p_language == '')) {
    selectedLanguage = document.forms[0].language.value;
  } else {
    selectedLanguage = p_language;
  };

  if ((selectedLanguage != null) && (selectedLanguage != '')) {
    selectedLanguage = selectedLanguage.toUpperCase();
  }
}

if (selectedLanguage == "FR") {
  todayStr = "Aujourd'hui"
} else {
  todayStr = "Today"
}

if (selectedLanguage == "FR") {
  Calendar.Months = ['janvier', 'février', 'mars', 'avril', 'mai', 'juin',
                           'juillet', 'aout', 'septembre', 'octobre', 'novembre', 'décembre'];
} else {
  Calendar.Months = ["January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"];
}

if (selectedLanguage == "FR") {
  Calendar.Days = ['Dim', 'Lun', 'Mar', 'Mec', 'Jeu', 'Ven', 'Sam'];
} else {
  Calendar.Days = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
}


// 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_WinCal, p_isChanged, p_setChanged, p_sDay, p_sMonth, p_sYear, p_month, p_year) {
  if ((p_month == null) && (p_year == null))  return;

  if (p_WinCal == null)
    this.gWinCal = ggWinCal;
  else
    this.gWinCal = p_WinCal;

  if (p_month == null) {
    this.gMonthName = null;
    this.gMonth = null;
    this.gYearly = true;
  } else {
    this.gMonthName = Calendar.get_month(p_month);
    this.gMonth = new Number(p_month);
    this.gYearly = false;
  }

  if (p_day == null) {
    this.sDay = '';
    this.gMonth = null;
    this.gYearly = true;
  } else {
    this.sDay   = p_sDay;
    this.sMonth = p_sMonth;
    this.sYear  = p_sYear;

  }

  this.gYear = p_year;
  this.gFormat = "yyyy/mm/dd";
  this.gReturnItem = p_item;
  this.gIsChanged  = p_isChanged;
  this.gSetChanged = p_setChanged;

}

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 Calendar.Months[monthNo];
}

function Calendar_get_day(dayNo) {
  return Calendar.Days[dayNo];
}

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];
}

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;
}

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;
}

// This is for compatibility with Navigator 3, we have to create and discard one object before the prototype object exists.
new Calendar();


//-------------------------------------
// Gets the code for the calender table
//-------------------------------------
Calendar.prototype.getMonthlyCalendarCode = function(selectedDay) {

  return "<center><table border=1 bgcolor=\"#FFFFFF\">"
    + this.cal_header() + this.cal_data(selectedDay)
    + "</table></center>";
}

//-------------------------------------
//
//-------------------------------------
Calendar.prototype.show = function() {
  var vCode = "";

  this.gWinCal.document.open();

  // Setup the page...
  this.wwrite("<html>\n");
  this.wwrite("<head><title>Calendar</title>\n");
  this.wwrite("<script language=\"javascript\" type=\"text/javascript\" src=\"../js/setChanged.js\"></script>");
  this.wwrite("<style type='text/css'>\n<!--");
  this.wwrite("body {");
  this.wwrite("  color: #000000;");
  this.wwrite("  background-color: #ffffff;");
  this.wwrite("  font-family: arial;");
  this.wwrite("}");

  this.wwrite("a:link {");
  this.wwrite("  text-decoration: none;");
  this.wwrite("  color: #000000;");
  this.wwrite("}");

  this.wwrite("a:visited {");
  this.wwrite("  text-decoration: none;");
  this.wwrite("  color: #000000;");
  this.wwrite("}");

  this.wwrite("a:active {");
  this.wwrite("  text-decoration: none;");
  this.wwrite("  color: #000000;");
  this.wwrite("}");

  this.wwrite("a:hover {");
  this.wwrite("  text-decoration: underline;");
  this.wwrite("  color: #cc0000;");
  this.wwrite("}");

  this.wwrite("--></style>");
  this.wwrite("</head>\n");

  this.wwrite("<body "
   + "text=\"#000000\">");

  this.wwriteA("<center>");

  this.wwrite("<select onChange=\"" +
    "javascript:window.opener.Build(" +
    "'" + this.gReturnItem + "', '" + this.sDay + "', '" + this.sMonth + "', '" + this.sYear + "', this.value, '" + this.gYear + "', '" + this.gFormat + "'" +
    ", '" + this.gIsChanged + "', '" + this.gSetChanged + "')\">");
  for (var i = 0; i < Calendar.Months.length; i++) {
    if (i == this.gMonth)
      this.wwrite("<option value=" + i + " selected>" + Calendar.Months[i]);
    else
      this.wwrite("<option value=" + i + ">" + Calendar.Months[i]);
  }
  this.wwrite("</select>");

  this.wwrite("<input type=text size=4 maxlength=4 value='" + this.gYear + "' onChange=\"" +
    "javascript:window.opener.Build(" +
    "'" + this.gReturnItem + "', '" + this.sDay + "', '" + this.sMonth + "', '" + this.sYear + "', '" + this.gMonth + "', this.value, '" + this.gFormat + "'" +
    ", '" + this.gIsChanged + "', '" + this.gSetChanged + "')\">");
  this.wwrite("<br>");

  // 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];


  this.wwrite("<input type=button name='previousYear' value='<<' onClick=\"" +
    "javascript:window.opener.Build(" +
    "'" + this.gReturnItem + "', '" + this.sDay + "', '" + this.sMonth + "', '" + this.sYear + "', '" + this.gMonth + "', '" + (parseInt(this.gYear)-1) + "', '" + this.gFormat + "'" +
    ", '" + this.gIsChanged + "', '" + this.gSetChanged + "')\">");

  this.wwrite("<input type=button name='previousYear' value=' < ' onClick=\"" +
    "javascript:window.opener.Build(" +
    "'" + this.gReturnItem + "', '" + this.sDay + "', '" + this.sMonth + "', '" + this.sYear + "', '" + prevMM + "', '" + prevYYYY + "', '" + this.gFormat + "'" +
    ", '" + this.gIsChanged + "', '" + this.gSetChanged + "')\">");

  this.wwrite("<input type=button name='previousYear' value=\"" + todayStr + "\" onClick=\"" +
    "javascript:window.opener.Build(" +
    "'" + this.gReturnItem + "', '" + this.sDay + "', '" + this.sMonth + "', '" + this.sYear + "', '" + gNow.getMonth() + "', '" + gNow.getFullYear() + "', '" + this.gFormat + "'" +
    ", '" + this.gIsChanged + "', '" + this.gSetChanged + "')\">");

  this.wwrite("<input type=button name='previousYear' value=' > ' onClick=\"" +
    "javascript:window.opener.Build(" +
    "'" + this.gReturnItem + "', '" + this.sDay + "', '" + this.sMonth + "', '" + this.sYear + "', '" + nextMM + "', '" + nextYYYY + "', '" + this.gFormat + "'" +
    ", '" + this.gIsChanged + "', '" + this.gSetChanged + "')\">");

  this.wwrite("<input type=button name='previousYear' value='>>' onClick=\"" +
    "javascript:window.opener.Build(" +
    "'" + this.gReturnItem + "', '" + this.sDay + "', '" + this.sMonth + "', '" + this.sYear + "', '" + this.gMonth + "', '" + (parseInt(this.gYear)+1) + "', '" + this.gFormat + "'" +
    ", '" + this.gIsChanged + "', '" + this.gSetChanged + "')\"></center><br>");

  // Get the complete calendar code for the month..
  vCode = this.getMonthlyCalendarCode(this.gDay);
  this.wwrite(vCode);

  this.wwrite("</font></body></html>");
  this.gWinCal.document.close();
}

Calendar.prototype.wwrite = function(wtext) {
  this.gWinCal.document.writeln(wtext);
}

Calendar.prototype.wwriteA = function(wtext) {
  this.gWinCal.document.write(wtext);
}

Calendar.prototype.cal_header = function() {
  var vCode = "";

  vCode = vCode + "<tr>";
  vCode = vCode + "<td width='14%'><font size='2' color='#000000'><b>" + Calendar_get_day(0) + "</b></font></td>";
  vCode = vCode + "<td width='14%'><font size='2' color='#000000'><b>" + Calendar_get_day(1) + "</b></font></td>";
  vCode = vCode + "<td width='14%'><font size='2' color='#000000'><b>" + Calendar_get_day(2) + "</b></font></td>";
  vCode = vCode + "<td width='14%'><font size='2' color='#000000'><b>" + Calendar_get_day(3) + "</b></font></td>";
  vCode = vCode + "<td width='14%'><font size='2' color='#000000'><b>" + Calendar_get_day(4) + "</b></font></td>";
  vCode = vCode + "<td width='14%'><font size='2' color='#000000'><b>" + Calendar_get_day(5) + "</b></font></td>";
  vCode = vCode + "<td width='16%'><font size='2' color='#000000'><b>" + Calendar_get_day(6) + "</b></font></td>";
  vCode = vCode + "</tr>";

  return vCode;
}

Calendar.prototype.cal_data = function(selectedDay) {
  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       = "";

  var vSetChanged = '';

  if (this.gSetChanged == 'setChanged2') {
    vSetChanged = this.gSetChanged + "('" + this.gReturnItem + "', '" + this.gIsChanged + "', '', 'self.opener.');"
  } else if (this.gSetChanged == 'setChanged1') {
    vSetChanged = this.gSetChanged + "('" + this.gIsChanged + "', '', 'self.opener.');"
  } else if (this.gSetChanged != '') {
    vSetChanged = this.gSetChanged + "('" + this.gIsChanged + "', '', '', 'self.opener.');"
  }

  /*
  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>";
  for (i=0; i<vFirstDay; i++) {
    vCode = vCode + "<td width='14%'" + this.write_weekend_string(i) + "><font size='2' face='" + fontface + "'> </font></td>";
  }

  // Write rest of the 1st week
  for (j=vFirstDay; j<7; j++) {
    vCode = vCode + "<td width='14%'" + this.write_weekend_string(j) + "><font size='2' face='" + fontface + "'>" +
      "<a href='#' " +
        "onClick=\"self.opener.document.forms[0]." + this.gReturnItem + ".value='" +
        this.format_data(vDay) +
        //"';self.opener.document.forms[0]." + this.gIsChanged + ".value='yes';window.close();\">" +
        "';" + vSetChanged + "window.close();\">" +
        this.format_day(vDay) +
      "</a>" +
      "</font></td>";
    vDay=vDay + 1;
  }
  vCode = vCode + "</tr>";

  // Write the rest of the weeks
  for (k=2; k<7; k++) {
    vCode = vCode + "<tr>";

    for (j=0; j<7; j++) {
      vCode = vCode + "<td width='14%'" + this.write_weekend_string(j) + "><font size='2' face='" + fontface + "'>" +
        "<a href='#' " +
          "onClick=\"self.opener.document.forms[0]." + this.gReturnItem + ".value='" +
          this.format_data(vDay) +
          "';" + vSetChanged + "window.close();\">" +
          //"';self.opener.document.forms[0]." + this.gIsChanged + ".value='yes';window.close();\">" +
        this.format_day(vDay) +
        "</a>" +
        "</font></td>";
      vDay=vDay + 1;

      if (vDay > vLastDay) {
        vOnLastDay = 1;
        break;
      }
    }

    if (j == 6)
      vCode = vCode + "</tr>";
    if (vOnLastDay == 1)
      break;
  }

  // Fill up the rest of last week with proper blanks, so that we get proper square blocks
  for (m=1; m<(7-j); m++) {
    if (this.gYearly)
      vCode = vCode + "<td width='14%'" + this.write_weekend_string(j+m) +
      "><font size='2' face='" + fontface + "' color='gray'> </font></td>";
    else
      vCode = vCode + "<td width='14%'" + this.write_weekend_string(j+m) +
      "><font size='2' face='" + fontface + "' color='gray'>" + m + "</font></td>";
  }

  return vCode;
}

Calendar.prototype.format_day = function(vday) {
  var vNowDay;
  var vNowMonth;
  var vNowYear;

  if (this.sDay == '') {
    var vNowDay   = gNow.getDate();
    var vNowMonth = gNow.getMonth();
    var vNowYear  = gNow.getFullYear();
  } else {
    var vNowDay   = this.sDay;
    var vNowMonth = this.sMonth;
    var vNowYear  = this.sYear;
  }

  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 (" bgcolor=\"" + weekendColor + "\"");
  }

  return "";
}

Calendar.prototype.format_data = function(p_day) {
  var vMonth = 1 + this.gMonth;
  vMonth = (vMonth.toString().length < 2) ? "0" + vMonth : vMonth;
  var vY4 = new String(this.gYear);
  var vDD = (p_day.toString().length < 2) ? "0" + p_day : p_day;

  return vY4 + "\/" + vMonth + "\/" + vDD;
}

function Build(p_item, p_sDay, p_sMonth, p_sYear, p_month, p_year, p_gFormat, p_isChanged, p_setChanged) {
  // p_gFormat -> not used, just a placeholder
  var p_WinCal = ggWinCal;
  gCal = new Calendar(p_item, p_WinCal, p_isChanged, p_setChanged, p_sDay, p_sMonth, p_sYear, p_month, p_year);

  gCal.show();
}

function show_calendar() {
  /*
    p_month : 0-11 for Jan-Dec; 12 for All Months.
    p_year  : 4-digit year
    p_item  : Return Item.
  */

  p_item = arguments[0];

  if (arguments[1] == "" || arguments[1] == null)
    p_day = '';
  else
    p_day = arguments[1];

  if (arguments[2] == "" || arguments[2] == null)
    p_month = new String(gNow.getMonth());
  else
    p_month = arguments[2];

  if (arguments[3] == "" || arguments[3] == null)
    p_year = new String(gNow.getFullYear().toString());
  else
    p_year = arguments[3];

  var p_isChanged  = arguments[4];
  var p_setChanged = arguments[5];

  vWinCal = window.open("", "Calendar",
    "width=250,height=250,status=no,resizable=no,top=200,left=200");
  vWinCal.opener = self;
  ggWinCal = vWinCal;

  Build(p_item, p_day, p_month, p_year, p_month, p_year, '', p_isChanged, p_setChanged);
}

/*
function Run_Calendar
*/

function Run_Calendar(fieldnum, fieldName, isChanged, setChanged) {
  var currYear  = '';
  var currMonth = '';
  var currDay   = '';
  var value     = '';

  if (isIE) {

    Run_Calendar_ie(fieldnum, fieldName, isChanged, setChanged)

  } else {


    if (arguments.length == 1) {
      fieldName  = 'field' + fieldnum
      isChanged  = 'field_ischanged' + fieldnum
      setChanged = 'setChanged'
      value      = getValueByName('field' + fieldnum)


    } else if (arguments.length < 4) {

      setChanged = '';
      value = getValueByName(fieldName)

    } else {

      value = getValueByName(fieldName)

    }

    if (value != '') {
      currYear = value.substring(0, 4)

      if (value.substring(5, 6) == 0) {
        currMonth  = value.substring(6, 7);
      } else {
        currMonth  = value.substring(5, 7);
      }

      if (value.substring(8, 9) == 0) {
        currDay  = value.substring(9, 10);
      } else {
        currDay  = value.substring(8, 10);
      }


      currMonth = parseInt(currMonth) - 1;
    }
    show_calendar(fieldName, currDay, currMonth, currYear, isChanged, setChanged)
  }
}
