/* ---------------------------------------------------------------------------------------------------
   ePages 5 - Scripts
   presentation_calendar.js $Revision: 1.15 $
   ---------------------------------------------------------------------------------------------------
   Required JavaScript Packages:
   epages_scripts.js
   presentation_event.js
   presentation_layer.js
   --------------------------------------------------------------------------------------------------- */

/* - public ------------------------------------------------------------------------------------------ */

var NamesOfMonth;
var MonthAbbreviations;
var NamesOfDay;
var DayAbbreviations;
var InitialDay;
var InitialMonth;
var InitialYear;
var MaxHours;

function ShowCalenderLayer(DisplayPattern, InputFieldID, Design) {
// DisplayPattern -> format of date output
// InputFieldID -> target input field for date output
// Design -> empty = standard dialog; "Toolbar" = layer in toolbar design

  // hide visible layer
  hideLayer();

  // initial calendar settings
  if(ShowCalenderLayer.arguments.length>3) {
     UserXshift=ShowCalenderLayer.arguments[3];
  } else {
     UserXshift=0;
  }
  if(ShowCalenderLayer.arguments.length>4) {
     UserYshift=ShowCalenderLayer.arguments[4];
  }  else {
     UserYshift=0;
  }
  if(ShowCalenderLayer.arguments.length>5) {
     LayerDispayTimeOut=ShowCalenderLayer.arguments[5];
     ResetLayerDispayTimeOut=ShowCalenderLayer.arguments[5];
  }

  LayerID="Calendar";
  CalendarPattern=DisplayPattern;
  CalendarTargetInputID=InputFieldID;
  ActualMonth=InitialMonth;
  ActualYear=InitialYear;
  setCalenderMonth(ActualMonth,ActualYear);

  // show clock in calendar
  if(CalendarPattern.match(/%H/)||
     CalendarPattern.match(/%M/)||
     CalendarPattern.match(/%S/)||
     CalendarPattern.match(/..hour_12./)||
     CalendarPattern.match(/%I/)||
     CalendarPattern.match(/%k/)||
     CalendarPattern.match(/%l/)) {
     document.getElementById("SelectTime").className="ShowElement";
  } else {
     document.getElementById("SelectTime").className="HideElement";
  }

  // hours 12/24
  if(CalendarPattern.match(/..hour_12./)||
     CalendarPattern.match(/%I/)||
     CalendarPattern.match(/%l/)) {
    document.getElementById("Hour12").style.display="inline";
    document.getElementById("Hour24").style.display="none";
    MaxHours=12;
  } else {
    document.getElementById("Hour12").style.display="none";
    document.getElementById("Hour24").style.display="inline";
    MaxHours=24;
  }

  // check am/pm
  if(CalendarPattern.match(/%p/)||
     CalendarPattern.match(/%P/)) {
     document.getElementById("AMPM").style.display="inline";
  } else {
     document.getElementById("AMPM").style.display="none";
  }

  // show layer after 0,01s
  // the coordinates of layer are set & calculated by the click event: getLayerXYCoords()
  window.setTimeout("setUpLayer('"+Design+"')",10)
}

function ShowTimeLayer(DisplayPattern, InputFieldID, Design) {
// DisplayPattern -> format of date output
// InputFieldID -> target input field for date output
// Design -> empty = standard dialog; "Toolbar" = layer in toolbar design

  // hide visible layer
  hideLayer();

  // initial calendar settings
  if(ShowTimeLayer.arguments.length>3) {
     UserXshift=ShowTimeLayer.arguments[3];
  } else {
     UserXshift=0;
  }
  if(ShowTimeLayer.arguments.length>4) {
     UserYshift=ShowTimeLayer.arguments[4];
  } else {
     UserYshift=0;
  }
  if(ShowTimeLayer.arguments.length>5) {
     LayerDispayTimeOut=ShowTimeLayer.arguments[5];
     ResetLayerDispayTimeOut=ShowTimeLayer.arguments[5];
  }

  LayerID="Clock";
  CalendarPattern=DisplayPattern;
  CalendarTargetInputID=InputFieldID;

  // hours 12/24
  if(CalendarPattern.match(/..hour_12./)||
     CalendarPattern.match(/%I/)||
     CalendarPattern.match(/%l/)) {
    document.getElementById("ClockHour12").style.display="inline";
    document.getElementById("ClockHour24").style.display="none";
    MaxHours=12;
  } else {
    document.getElementById("ClockHour12").style.display="none";
    document.getElementById("ClockHour24").style.display="inline";
    MaxHours=24;
  }

  // check am/pm
  if(CalendarPattern.match(/%p/)||
     CalendarPattern.match(/%P/)) {
     document.getElementById("ClockAMPM").style.display="inline";
  } else {
     document.getElementById("ClockAMPM").style.display="none";
  }

  // show layer after 0,01s
  // the coordinates of layer are set & calculated by the click event: getLayerXYCoords()
  window.setTimeout("setUpLayer('"+Design+"')",10)
}

function setTimeToTarget() {
// sets date in target input field
    var formattedDate;
    var Hour;
    var Minute=document.getElementById('ClockMinute').value;
    var Second=0;
    var AMPM=document.getElementById("ClockAMPM").value;

    if(MaxHours==12) Hour=document.getElementById("ClockHour12").value;
    else Hour=document.getElementById("ClockHour24").value;

    try {
      Second=document.getElementById('ClockSecond').value;
    } catch (Exeption){
      // Element not found
    }
    winInterface.hideLayer();
    formattedDate=replacePattern(0,0,0,0,Hour,Minute,Second,AMPM);
    var oCalendarTargetInput=winInterface.document.getElementById(winInterface.CalendarTargetInputID);
    oCalendarTargetInput.value=formattedDate;
    winInterface.ep_fireEvent("change", oCalendarTargetInput);
    oCalendarTargetInput.focus();
}


/* - private ----------------------------------------------------------------------------------------- */

var CalendarPattern;
var CalendarTargetInputID;
var ActualMonth=0;
var ActualYear=0;

function nextMonth() {
// switch to the next month
    winInterface.ActualMonth++;
    if(winInterface.ActualMonth>12) {
        winInterface.ActualYear++;
        winInterface.ActualMonth=1;
    }
    setCalenderMonth(winInterface.ActualMonth, winInterface.ActualYear);
}

function previousMonth() {
// switch to the previous month
    winInterface.ActualMonth--;
    if(winInterface.ActualMonth==0) {
        winInterface.ActualYear--;
        winInterface.ActualMonth=12;
    }
    setCalenderMonth(winInterface.ActualMonth,winInterface.ActualYear);
}

function replacePattern(Year,Month,Day,DayNo,Hour,Minute,Second,AMPM) {
  // this function replaces the pattern specifieres with real values
  // please note: this function does not support all declared specifieres

  var formattedDate=winInterface.CalendarPattern;

  // set current values for specifieres

  // century

  var C=Year.toString();
  C=C.substr(0,2)                      // only 2 digits

  //year
  var y=Year.toString();
  y=y.substr(2,3)                      // only 2 digits

  var Y=Year.toString();

  var ce_year=Year.toString();

  //month
  var m=Month.toString();
  if(m.length==1) m="0"+m;             // 2 digits

  var month=Month.toString();

  var B=winInterface.NamesOfMonth[Month-1];

  var b=winInterface.MonthAbbreviations[Month-1];
  var h=winInterface.MonthAbbreviations[Month-1];

  //day
  var d=Day.toString();
  if(d.length==1) d="0"+d;            // 2 digits

  var e=Day.toString();
  if(e.length==1) e=" "+e;            // 2 digits (fill with space)

  var day=Day.toString();
  if(day.length==1) day="0"+day;      // 2 digits

  var A=winInterface.NamesOfDay[DayNo];
  var a=winInterface.DayAbbreviations[DayNo];

  //Hour
  var H=Hour.toString();
  if(H.length==1) H="0"+H;      // 2 digits

  var I=Hour.toString();
  if(I.length==1) I="0"+I;      // 2 digits

  var k=Hour.toString();
  if(k.length==1) k="0"+k;      // 2 digits

  var l=Hour.toString();

  var hour=Hour.toString();

  var hour_12=Hour.toString();

  //Minute
  var M=Minute.toString();
  if(M.length==1) M="0"+M;      // 2 digits

  //Second
  var S=Second.toString();
  if(S.length==1) S="0"+S;      // 2 digits

  // AMPM
  var p=AMPM;
  var P=AMPM.toLowerCase();

  // relace specifieres in string with values

  // century
  formattedDate=formattedDate.replace(/%C/, C);

  //year
  formattedDate=formattedDate.replace(/%y/, y);
  formattedDate=formattedDate.replace(/%Y/, Y);
  formattedDate=formattedDate.replace(/..ce_year./, ce_year);

  //month
  formattedDate=formattedDate.replace(/%m/, m);
  formattedDate=formattedDate.replace(/..month./, month);
  formattedDate=formattedDate.replace(/%B/, B);
  formattedDate=formattedDate.replace(/%b/, b);
  formattedDate=formattedDate.replace(/%h/, h);

  //day
  formattedDate=formattedDate.replace(/%d/, d);
  formattedDate=formattedDate.replace(/%e/, e);
  formattedDate=formattedDate.replace(/..day./, day);
  formattedDate=formattedDate.replace(/%A/, A);
  formattedDate=formattedDate.replace(/%a/, a);

  //Hour
  formattedDate=formattedDate.replace(/%H/, H);
  formattedDate=formattedDate.replace(/%I/, I);
  formattedDate=formattedDate.replace(/%k/, k);
  formattedDate=formattedDate.replace(/%l/, l);
  formattedDate=formattedDate.replace(/..hour_12./, hour_12);
  formattedDate=formattedDate.replace(/..hour./, hour);

  //Minute
  formattedDate=formattedDate.replace(/%M/, M);

  //Second
  formattedDate=formattedDate.replace(/%S/, S);

  // AMPM
  formattedDate=formattedDate.replace(/%p/, p);
  formattedDate=formattedDate.replace(/%P/, P);

  return formattedDate;
}


function setDate(Day,Month,Year) {
// sets date in target input field
    var myMinute, myHour;
    var formattedDate;
    var unformattedDate=new Date(Year,Month-1,Day)
    var DayNo=unformattedDate.getDay();
    var AMPM=document.getElementById("AMPM").value;
    // start with monday
    if(DayNo == 0) DayNo = 6;
    else DayNo--;

    winInterface.hideLayer();
    try {
      myMinute=document.getElementById("Minute").value;
      if(MaxHours==12) myHour=document.getElementById("Hour12").value;
      else myHour=document.getElementById("Hour24").value;
    }catch(exception) {
        // node wasn't found
    }
    formattedDate=replacePattern(Year,Month,Day,DayNo,myHour,myMinute,0,AMPM);
    var oCalendarTargetInput=winInterface.document.getElementById(winInterface.CalendarTargetInputID);
    oCalendarTargetInput.value=formattedDate;
    winInterface.ep_fireEvent("change", oCalendarTargetInput);
    oCalendarTargetInput.focus();
}

function setCalenderMonth(Month,Year) {
// this functions displays days of a month in a div with the id "Days"
// a div with the id NameOfMonth is also required
    var DisplayDate = new Date(Year,Month-1,1);
    var NumberOfWeekDay = DisplayDate.getDay();
    var MaximumDays=31;

    // recalculate NumberOfWeekDay (start with monday not sunday)
    if(NumberOfWeekDay == 0) NumberOfWeekDay = 6;
    else NumberOfWeekDay--;

    //calculate number of days in month
    if(Month==4 || Month==6 || Month==9 || Month==11 ) MaximumDays--;
    if(Month==2) {
     MaximumDays = MaximumDays - 3;
     if(Year%4==0) MaximumDays++;
     if(Year%100==0) MaximumDays--;
     if(Year%400==0) MaximumDays++;
    }

    // throw away existing matrix of days
    try {
        ThrowAwayNode=document.getElementById("Days").firstChild;
        document.getElementById("Days").removeChild(ThrowAwayNode);
    } catch(exception) {
        // no node was found
    }

    // create new matrix of days and append it to the div with the id "Days"
    NodeDaysMatrix=document.createElement("div");
    document.getElementById("Days").appendChild(NodeDaysMatrix);

    // create day-nodes and append them to the first child element (matrix of days) of the div with the id "Days"
    for(i=0; i<42; i++) {
      if(i-NumberOfWeekDay < 0) {
        MonthDay="";
      } else {
        MonthDay=i-NumberOfWeekDay+1;
      }
      if(i-NumberOfWeekDay >= MaximumDays) {
        MonthDay="";
      }

      NodeDayLink=document.createElement("a");
      NodeDay=document.createTextNode(MonthDay);
      NodeDayLink.appendChild(NodeDay);
      if(MonthDay!="") NodeDayLink.setAttribute("href", "javascript:setDate("+MonthDay+","+Month+","+Year+")");

      // grey out sundays
      if(i%7==6) NodeDayLink.className="Sunday";

      // highlight current day (today)
      initalDate=InitialYear+" "+InitialMonth+" "+InitialDay;
      loopedDate=Year+" "+Month+" "+MonthDay;
      if(initalDate==loopedDate) {
        NodeDayLink.className="HiglightDate";
      }

      document.getElementById("Days").firstChild.appendChild(NodeDayLink);
    }

    // remove existing node with month name from the div with the id NameOfMonth
    try {
        ThrowAwayNode=document.getElementById("NameOfMonth").firstChild;
        document.getElementById("NameOfMonth").removeChild(ThrowAwayNode);
    } catch(exception) {
        // no node was found
    }

    // insert name of month and year as new text node the div with the id NameOfMonth
    NodeNameOfMonth = document.createTextNode(winInterface.NamesOfMonth[DisplayDate.getMonth()]+" "+Year);
    document.getElementById("NameOfMonth").appendChild(NodeNameOfMonth);
}
