//Calendar Functions

//initialize busyView for current month
function busyInit(cId) {
  if (cId.getItem('hideInput') != 'false') { BusyHideCalendarInput(cId); }
  var myToday=busyHeute();
  cId.setItem('month',myToday.substr(3,2));
  cId.setItem('year',myToday.substr(6,4));
  ajax(cId.getItem('file'),busyShow,cId);  
}

//hide calender input field 
function BusyHideCalendarInput(cId) {
  oInput = bb.evaluate('./div[1]',document.getElementById(cId.getItem('id')));
  oInput[0].className="none";
  oInput = bb.evaluate('./div[1]/div',document.getElementById(cId.getItem('id')));
  oInput[0].className="none";
  oInput = bb.evaluate('./div[1]/div/input',document.getElementById(cId.getItem('id')));
  oInput[0].style.border="0 solid white";
  oInput[0].style.color="white";
}

//show busy after calendar other month selected 
function busyShow(xmlDoc,cId) {
  // get the targets 
  var target=bb.evaluate('./div[2]/table/tbody/tr//td',document.getElementById(cId.getItem('id')));

  // get the currently selected day 
  var myObj=bb.evaluateSmart("id('" + cId.getItem('id') + "')");
  myObj.setProperty('value',btl.calendar.getFormattedValue(myObj.getProperty('selectedDate'),myObj.getAttribute('format'),myObj.getAttribute('language')));
  var date=myObj.getProperty("value");

  // find days of current month only and highlight if we have a description
  var dayofcurmonth=0;
  for(var i=0;i<target.length; i++) {
    if (dayofcurmonth == 1) {
      if ( target[i].innerHTML == "1") { dayofcurmonth=0; }       // END
    } else {
      if ( target[i].innerHTML == "1") { dayofcurmonth=1; }        // START
    }
    if ((i+1)%7 == 0) {         // Sunday red font
      target[i].style.color='#FF0000';
      if (dayofcurmonth) { target[i].style.fontWeight='bold'; } 
    }
    if ((i+2)%7 == 0) {         // Saturday bold
      if (dayofcurmonth) { target[i].style.fontWeight='bold'; } 
    }
    if (dayofcurmonth == 1 ) {
      day=((target[i].innerHTML<10) ? "0" : "")+ target[i].innerHTML;
      var datum=day + "." + cId.getItem('month') + "." + cId.getItem('year');

      var busy=cId.getItem('output')(xmlDoc,cId,day + "." + cId.getItem('month') + "." + cId.getItem('year'));
      if (busy == 0) {
        target[i].style.backgroundColor = 'white';
      }
      if (busy == 1) {
        target[i].style.backgroundColor = '#C0FFC0';
      }
      if (busy == 2) {
        if (datum == date) {
          bb.html.removeClass(target[i], 'btl-chameleon-highlightBackground');
          target[i].style.backgroundColor = '#FF0000';
        } else {
          target[i].style.backgroundColor = '#FFC0C0';
        }
      }
    }
  }
  if (date == "") {                                 // as long as no date is selected show today
    cId.getItem('output')(xmlDoc,cId,busyHeute());
  } else {
    cId.getItem('output')(xmlDoc,cId,date);
  }
}

// Functions called from outside

//day changed 
function busyDayChange(cId) {
  // daychange isnt called by the eventhandler DOMAttrModified if year or month switched
  ajax(cId.getItem('file'),busyShow,cId);  
}

//Month changed (by clicking one of the prev/next month days in daytable
function busyMonthChange(cId,offset) {
  var myMonth=cId.getItem('month');
  myMonth = (Number(myMonth) + Number(offset));
  myMonth=((myMonth<10) ? "0" : "")+ myMonth;
  cId.setItem('month',myMonth);
  ajax(cId.getItem('file'),busyShow,cId);   
}

//Year changed
function busyYearChange(cId,offset) {
  var myYear=cId.getItem('year');
  myYear=Number(myYear) + Number(offset);
  cId.setItem('year',myYear);
  ajax(cId.getItem('file'),busyShow,cId);   
}

function busyHeute() {
  var Heute=new Date();
  var DieserMonatstag = Heute.getDate();
  var DieserMonatNummmer = Heute.getMonth()+1;
  var DiesesJahr4stellig = Heute.getFullYear();

  // zweistellige Darstellung
  var DieserMonatstag2 = ((DieserMonatstag<10) ? "0" : "")+ DieserMonatstag;
  var DieserMonatNummmer2 = ((DieserMonatNummmer<10) ? "0" : "")+ DieserMonatNummmer;

  return DieserMonatstag2 + "." + DieserMonatNummmer2 + "." + DiesesJahr4stellig;
}

