// Graphisoft R&D JavaScript Calendar/date picker v2.0
// written by Lehel László - llaszlo@graphisoft.hu
// 2001-11-13 v2.0
// 2000-08-22 v1.0

//Resourcestrings START

var DayStrings = new Array("H","K","SZ","Cs","P","SZ","V"),
    MonthStrings = new Array("Január","Február","Március","Április","Május","Június","Július","Augusztus","Szeptember","Október","November","December");
var sMsgInvalidYear = "Invalid year: "
var sMsgInvalidDate = "Invalid date: "

//Resourcestrings END

/////////////////////////////////////////////

function doStartSelectDateField(itemName, fieldName, onSelectStartFunction, onSelectEndFunction, sDateTimeType){
 if(onSelectStartFunction && onSelectStartFunction!="")
    eval(onSelectStartFunction);
  var leftPlusOffset = 1, topPlusOffset = 20,
      leftOffset = GetObjectLeft(document.getElementById(itemName)) + leftPlusOffset,
      topOffset = GetObjectTop(document.getElementById(itemName)) + topPlusOffset;
  var v2 = document.getElementById(itemName).value;
  var d = new Date();
  var cal = document.getElementById("tblCalendar");
  var ca = document.getElementById("dinHTML");
  if (cal != null )
  {
  		removeChildren(ca);
		return;
  }

  if(v2 != ""){
     var v = v2.substring(0,4)+'.'+v2.substring(5,7)+'.'+v2.substring(8,10);
  	 d = StringToDate(v);
     if(d == null){ alert(sMsgInvalidDate + v); return; }
  }
  var iY=d.getFullYear(), iM=d.getMonth()+1, iD=d.getDate();
  SetCalendarOptions(false, false, true);//vOPTShowOKCancel, vOPTShowNavigationButtons, vOPTShowMonthYearSelection)

  document.getElementById("dinHTML").style.top = topOffset;
  document.getElementById("dinHTML").style.left = leftOffset;
  document.getElementById("dinHTML").style.zIndex = "200";
  
	document.getElementById("dinHTML").innerHTML=
   "<TABLE NAME='tblCalendar' ID='tblCalendar' BORDER=1 BORDERCOLOR=black cellspacing=0 cellpadding=0>"
    +"<TR><TD>"
    + BuildCalendar("doEndSelectDateField(\""+ itemName +"\", true,\""+onSelectEndFunction+"\")",
                    "doEndSelectDateField(\""+ itemName +"\", false,\""+onSelectEndFunction+"\")")
    +"</TD></TR></TABLE>";
  SelectDate(iY, iM, iD);

  var oNewCal = document.getElementById("tblCalendar");
  oParent = GetObjectMostTopOffsetParent(document.getElementById(itemName));
  if(oParent.offsetHeight < oNewCal.offsetTop + oNewCal.offsetHeight)
    oNewCal.style.top = topOffset - topPlusOffset - oNewCal.offsetHeight;

  if(oParent.offsetWidth < oNewCal.offsetLeft + oNewCal.offsetWidth)
    oNewCal.style.left =
       leftOffset
     + document.getElementById(itemName).offsetWidth
     + document.getElementById("btn" + itemName).offsetWidth
     - oNewCal.offsetWidth;
  window.event.cancelBubble = true;
}

function doEndSelectDateField(itemName, onOK, onSelectEndFunction){
  if(onOK)
    document.getElementById(itemName).value = XMLDateToString(SelectedYear, SelectedMonth, SelectedDay);

  var d2=document.getElementById('dinHTML');
	// -- d2Len holds the fixed starting value of the number of childNodes
  /*var d2Len=d2.childNodes.length;

  for (i=0;i<d2Len;i++)
  {
       d2.removeChild(d2.childNodes[0]);
  }
*/
  removeChildren(d2);

   if(onSelectEndFunction && onSelectEndFunction != "")
    eval(onSelectEndFunction);
}

/////////////////////////////////////////////

//Use functions SetCalendarDisplayParameters / SetCalendarOptions to modify
var CLRMainBackground = "#D6D3C2",
    CLRButtonPNLBackground = "#D6D3C2",
    CLRActDayBackground = "white",
    CLRActDayColor = "black",
    CLRActWeekEndBackground = "#f5deb3";//#ff6347",
    CLRActWeekEndColor = "black",
    CLRPrevNextDayBackground = "LightGrey",
    CLRPrevNextDayColor = "black",
    CLRPrevNextWeekEndBackground = "#d2b48c",
    CLRPrevNextWeekEndColor = "black",
    CLRSelectedBackground = "#00bfff",
    CLRSelectedColor = "black",
    CLRHeaderBackground = "gray",
    CLRHeaderColor = "black";

    OPTShowOKCancel = true;
    OPTShowNavigationButtons = true;
    OPTShowMonthYearSelection = true;

var SelectedYear = 2000 ;
var SelectedMonth = 8;
var SelectedDay = 1;

var SelectedDayPosition = 3;
var DaysInMonths = new Array();
var FirstDay; //in current month
var strDoOnOK = "", strDoOnCancel = "";

function SetCalendarDisplayParameters
  (vCLRMainBackground, vCLRButtonPNLBackground,
   vCLRActDayBackground, vCLRActDayColor,
   vCLRActWeekEndBackground, vCLRActWeekEndColor,
   vCLRPrevNextDayBackground, vCLRPrevNextDayColor,
   vCLRPrevNextWeekEndBackground, vCLRPrevNextWeekEndColor,
   vCLRSelectedBackground, vCLRSelectedColor,
   vCLRHeaderBackground, vCLRHeaderColor){

  CLRMainBackground = vCLRMainBackground;
  CLRButtonPNLBackground = vCLRButtonPNLBackground;
  CLRActDayBackground = vCLRActDayBackground;
  CLRActDayColor = vCLRActDayColor;
  CLRActWeekEndBackground = vCLRActWeekEndBackground;
  CLRActWeekEndColor = vCLRActWeekEndColor;
  CLRPrevNextDayBackground = vCLRPrevNextDayBackground;
  CLRPrevNextDayColor = vCLRPrevNextDayColor;
  CLRPrevNextWeekEndBackground = vCLRPrevNextWeekEndBackground;
  CLRPrevNextWeekEndColor = vCLRPrevNextWeekEndColor;
  CLRSelectedBackground = vCLRSelectedBackground;
  CLRSelectedColor = vCLRSelectedColor;
  CLRHeaderBackground = vCLRHeaderBackground;
  CLRHeaderColor = vCLRHeaderColor;
}

function SetCalendarOptions(
    vOPTShowOKCancel,
    vOPTShowNavigationButtons,
    vOPTShowMonthYearSelection){
  OPTShowOKCancel = vOPTShowOKCancel;
  OPTShowNavigationButtons = vOPTShowNavigationButtons;
  OPTShowMonthYearSelection = vOPTShowMonthYearSelection;
}

function SetCalendarMonthNames(vMonthStrings){
 var i;
 for(i=0; i<vMonthStrings.length; i++)
   MonthStrings[i] = vMonthStrings[i];
}

function SetCalendarDayNames(vDayStrings){
 var i;
 for(i=0; i < vDayStrings.length; i++)
   DayStrings[i] = vDayStrings[i];
}

function SelectDate(inYear, inMonth, inDay){
  SelectedYear = inYear;
  SelectedMonth = inMonth;
  SelectedDay = inDay;
  if(OPTShowMonthYearSelection)
    this.document.getElementById("year").value = SelectedYear;
  Fill_DaysInMonths(SelectedYear);
  SelectMonth(inMonth);
}

function SelectMonth(inMonth){
  var i;
  var PrevMonthDayNr;
  var ia;
  SelectedMonth = inMonth;
  if(SelectedDay > DaysInMonths[SelectedMonth]) // next month for a 31 day month yy/mm/31->yy/mm+1/31?
    SelectedDay = DaysInMonths[SelectedMonth]

  if(OPTShowMonthYearSelection)
    this.document.getElementById("month").selectedIndex = SelectedMonth-1;
  FirstDay = GetFirstDayPos(SelectedYear, SelectedMonth)-1;  //  -1
  if (FirstDay == 0) FirstDay = 7;  // !!
  PrevMonthDayNr = GetPrevMonthDayNr();
  for(i=1; i < FirstDay; i++){
    //this.document.getElementById("DTP_Pos" + i).innerText = (PrevMonthDayNr - FirstDay + i + 1).toString();
	ia =PrevMonthDayNr - FirstDay + i + 1;
    this.document.getElementById("DTP_Pos" + i).childNodes[0].nodeValue =ia;
	this.document.getElementById("DTP_Pos" + i).bgColor = CLRPrevNextDayBackground;
    if(i == 7)    // 1 volt
      this.document.getElementById("DTP_Pos" + i).bgColor = CLRPrevNextWeekEndBackground;
  };
  for(i=FirstDay; i <= FirstDay+DaysInMonths[SelectedMonth] - 1; i++){
    this.document.getElementById("DTP_Pos" + i).childNodes[0].nodeValue = (i - FirstDay + 1).toString();
    this.document.getElementById("DTP_Pos" + i).bgColor = CLRActDayBackground;
    if(i % 7 == 0)
      this.document.getElementById("DTP_Pos" + i).bgColor = CLRActWeekEndBackground;
  };

  var from = FirstDay + DaysInMonths[SelectedMonth];
  for(i=from; i <= 42 ; i++){
    this.document.getElementById("DTP_Pos" + i).childNodes[0].nodeValue = i - from + 1;
    this.document.getElementById("DTP_Pos" + i).bgColor = CLRPrevNextDayBackground;
    if(i % 7 == 0)
      this.document.getElementById("DTP_Pos" + i).bgColor = CLRPrevNextWeekEndBackground;
  };

  SelectedDayPosition = FirstDay + (SelectedDay - 1);
  this.document.getElementById("DTP_Pos" + SelectedDayPosition).bgColor = CLRSelectedBackground;
}

function GetFirstDayPos(year, month){
  var d = new Date(year, month - 1, 1);
  return d.getDay() + 1;
}

function GetPrevMonthDayNr(){
  if(SelectedMonth > 1)
     return DaysInMonths[SelectedMonth - 1];
  else
     return 31;
}

function Fill_DaysInMonths(aYear){
  DaysInMonths[1] = 31;
  DaysInMonths[2] = 28;
  if((aYear % 400 == 0)||
     ((aYear % 4 == 0)&&(aYear % 100 != 0))) {
    DaysInMonths[2] = 29;
  }
  DaysInMonths[3] = 31;
  DaysInMonths[4] = 30;
  DaysInMonths[5] = 31;
  DaysInMonths[6] = 30;
  DaysInMonths[7] = 31;
  DaysInMonths[8] = 31;
  DaysInMonths[9] = 30;
  DaysInMonths[10] = 31;
  DaysInMonths[11] = 30;
  DaysInMonths[12] = 31;
}

function month_onchange(){
  SelectMonth(this.document.getElementById("month").selectedIndex + 1);
}

function year_onchange() {
  if(isNaN(this.document.getElementById("year").value)){
    this.document.getElementById("year").focus();
    alert(sMsgInvalidYear + this.document.getElementById("year").value);
    return;
  }
  SelectDate(this.document.getElementById("year").value, SelectedMonth, SelectedDay);
}

function onDayClick(inPosition){
  // selected day
  if(inPosition == FirstDay+SelectedDay-1){
    if(!OPTShowOKCancel)
      eval(strDoOnOK);
  }  //do nothing
  //day of current month
  else if(inPosition >= FirstDay &&  inPosition < FirstDay + DaysInMonths[SelectedMonth]){
    if(SelectedDayPosition % 7 == 1)
      this.document.getElementById("DTP_Pos" + SelectedDayPosition).bgColor = CLRActWeekEndBackground;
    else
      this.document.getElementById("DTP_Pos" + SelectedDayPosition).bgColor = CLRActDayBackground;
    this.document.getElementById("DTP_Pos" + inPosition).bgColor = CLRSelectedBackground;
    SelectedDay = this.document.getElementById("DTP_Pos" + inPosition).childNodes[0].nodeValue - 0 ; //  force to integer
    SelectedDayPosition = inPosition;
    if(!OPTShowOKCancel)
      eval(strDoOnOK);
  }
  // prev month
  else if(inPosition < 15){
    if(!OPTShowOKCancel)
      eval(strDoOnCancel);
    else
      PrevMonth(this.document.getElementById("DTP_Pos" + inPosition).childNodes[0].nodeValue - 0);
  }
  // next month
  else
    if(!OPTShowOKCancel)
      eval(strDoOnCancel);
    else
      NextMonth(this.document.getElementById("DTP_Pos" + inPosition).childNodes[0].nodeValue - 0);
}

function PrevMonth(otherDay){
  if(otherDay) SelectedDay = otherDay;
  if(SelectedMonth > 1)
    SelectMonth(SelectedMonth - 1);
  else
    SelectDate(SelectedYear - 1, 12, SelectedDay);
}

function NextMonth(otherDay){
  if(otherDay) SelectedDay = otherDay;
  if(SelectedMonth < 12)
    SelectMonth(SelectedMonth + 1);
  else
    SelectDate(SelectedYear + 1, 1, SelectedDay);
}

function PrevYear(){
  SelectDate(SelectedYear - 1, SelectedMonth, SelectedDay);
}

function NextYear(){
  SelectDate(SelectedYear + 1, SelectedMonth, SelectedDay);
}

function Today(){
  var todayDate = new Date();
  SelectDate(todayDate.getYear(), todayDate.getMonth() + 1, todayDate.getDate());
}

function BuildCalendar(doOnOK, doOnCancel){
var strResult = "";
strResult += "<TABLE WIDTH=100 height=100 BGCOLOR='"+CLRMainBackground+"' ALIGN=center BORDER=0 cellspacing=0 cellpadding=0 >";
if(OPTShowNavigationButtons){
  strResult += "<TR>";
  strResult += " <TD ALIGN=right NOWRAP>";
  strResult += "  <TABLE WIDTH='100%' BORDER=0 BGCOLOR='"+CLRButtonPNLBackground+"' CELLSPACING=1 CELLPADDING=0 style='FONT-SIZE: 10px;'>";
  strResult += "   <TR align=center>";
  strResult += "    <TD WIDTH='10%'><INPUT type='button' value='|<' id=btnPrevYear name=button1 onClick='javascript:PrevYear();return true;' CLASS='FMStyleButton'></TD>";
  strResult += "    <TD WIDTH='10%'><INPUT type='button' value='<' id=btnPrevMonth name=button2 onClick='javascript:PrevMonth();return true;' CLASS='FMStyleButton'></TD>";
  strResult += "    <TD><INPUT type='button' value='Today' id=button3 name=btnToday style='width: 50;' CLASS='FMStyleButton' onClick='javascript:Today();return true;'></TD>";
  strResult += "    <TD WIDTH='10%'><INPUT type='button' value='>' id=btnNextMonth name=button4 onClick='javascript:NextMonth();return true;' CLASS='FMStyleButton'></TD>";
  strResult += "    <TD WIDTH='10%'><INPUT type='button' value='>|' id=btnNextYear name=button5 onClick='javascript:NextYear();return true;' CLASS='FMStyleButton'></TD>";
  strResult += "   </TR>";
  strResult += "  </TABLE>";
  strResult += " </TD>";
  strResult += "</TR>";
}
if(OPTShowMonthYearSelection){
  strResult += "<TR>";
  strResult += " <TD ALIGN=right NOWRAP>";
  strResult += "  <TABLE WIDTH='100%' BORDER=0 CELLSPACING=0 CELLPADDING=0 BGCOLOR='"+CLRButtonPNLBackground+"'VALIGN='top'>";
  strResult += "   <TR>";
  strResult += "    <TD>"
  strResult += "     <SELECT id=month name=month LANGUAGE=javascript onchange='return month_onchange()' style='FONT-SIZE: 10px;'>";
  for(i=0; i<MonthStrings.length; i++)
    strResult += "<OPTION>"+MonthStrings[i]+"</OPTION>";
  strResult += "     </SELECT>";
  strResult += "    </TD>";
  strResult += "    <TD><INPUT TYPE=TEXT id=year name=year STYLE='width:35px; height:20px;FONT-SIZE: 10px;' value=2000 onchange='javascript:return year_onchange();'>";
  strResult += "    </TD>";
  strResult += "   </TR>";
  strResult += "  </TABLE>";
  strResult += " </TD>";
  strResult += "</TR>";
}
strResult += "<TR>";
strResult += " <TD ALIGN=right NOWRAP>";
strResult += "  <TABLE WIDTH='100%' ALIGN=center BORDER=0 CELLSPACING=0 CELLPADDING=0 height=100%  style='FONT-SIZE: 8px;'>";
strResult += "   <TR align=center bgcolor="+CLRHeaderBackground+" valign=middle height=10%> ";
for(i=0; i<DayStrings.length; i++)
  strResult += "<TD NOWRAP WIDTH='14%' valign=center><FONT color=White><B>"+DayStrings[i]+"</B></FONT></TD>";
strResult += "   </TR>";
strResult += "  </TABLE>	";
strResult += " </TD>	";
strResult += "</TR>	";
strResult += "<TR>";
strResult += " <TD ALIGN=right NOWRAP>";
strResult += "  <TABLE WIDTH='100%' HEIGHT='100%' ALIGN=center BORDER=0 CELLSPACING=0 CELLPADDING=0   style='FONT-SIZE: 10px;cursor: pointer'>";
var i, j, N;
for(i=0; i<6; i++){
  strResult += "<TR align=right valign=middle>";
  for(j=1; j<8; j++){
    N = i*7+j
    strResult += "<TD ID='DTP_Pos"+N.toString()+"' NOWRAP onClick='javascript:onDayClick("+N.toString()+");return true;' WIDTH='14%'>"+N+"</TD>";
  }
  strResult += "</TR>";
}
strResult += "  </TABLE>";
strResult += " </TD>";
strResult += "</TR>";
if(OPTShowOKCancel){
  strResult += "<TR>";
  strResult += " <TD ALIGN=right NOWRAP>";
  strResult += "  <TABLE WIDTH='100%' BORDER=0 CELLSPACING=1 CELLPADDING=0 BGCOLOR='"+CLRButtonPNLBackground+"'>";
  strResult += "   <TR align=center>";
  strResult += "	  <TD><INPUT type='button' value='OK' name=btnOk style='width: 50;height:20;' CLASS='FMStyleButton' onClick='javascript:"+ doOnOK +";return true;'></TD>";
  strResult += "	  <TD><INPUT type='button' value='Cancel' name=btnCancel style='width: 50;height:20;' CLASS='FMStyleButton' onClick='javascript:"+ doOnCancel +";return true;'></TD>";
  strResult += "	 </TR>";
  strResult += "	</TABLE>";
  strResult += " </TD>";
  strResult += "</TR>";
}
strResult += "</TABLE>";

strDoOnOK = doOnOK;
strDoOnCancel = doOnCancel;

return strResult;
}
function updateText(id,value) {
  document.getElementById(id).childNodes[0].nodeValue = value;
  // - note that for this to work, the div must already have a child node.  
  // - Otherwise, you'll get an error.
}
function removeChildren(d2)
{
  var d2Len=d2.childNodes.length;
  for (i=0;i<d2Len;i++)
  {
       d2.removeChild(d2.childNodes[0]);
  }

}

