
function positionInfo(object) {

  var p_elm = object;

  this.getElementLeft = getElementLeft;
  function getElementLeft() {
    var x = 0;
    var elm;
    if(typeof(p_elm) == "object"){
      elm = p_elm;
    } else {
      elm = document.getElementById(p_elm);
    }
    while (elm != null) {
      x+= elm.offsetLeft;
      elm = elm.offsetParent;
    }
    return parseInt(x);
  }

  this.getElementWidth = getElementWidth;
  function getElementWidth(){
    var elm;
    if(typeof(p_elm) == "object"){
      elm = p_elm;
    } else {
      elm = document.getElementById(p_elm);
    }
    return parseInt(elm.offsetWidth);
  }

  this.getElementRight = getElementRight;
  function getElementRight(){
    return getElementLeft(p_elm) + getElementWidth(p_elm);
  }

  this.getElementTop = getElementTop;
  function getElementTop() {
    var y = 0;
    var elm;
    if(typeof(p_elm) == "object"){
      elm = p_elm;
    } else {
      elm = document.getElementById(p_elm);
    }

    y = y - 200;

    while (elm != null) {
      y += elm.offsetTop;
      elm = elm.offsetParent;
    }

    return parseInt(y);
  }

  this.getElementHeight = getElementHeight;
  function getElementHeight(){
    var elm;
    if(typeof(p_elm) == "object"){
      elm = p_elm;
    } else {
      elm = document.getElementById(p_elm);
    }
    return parseInt(elm.offsetHeight);
  }

  this.getElementBottom = getElementBottom;
  function getElementBottom(){
    return getElementTop(p_elm) + getElementHeight(p_elm);
  }
}

function KoledarO() {

  var calendarId = 'KoledarO';
  var currentYear = 0;
  var currentMonth = 0;
  var currentDay = 0;

  var selectedYear = 0;
  var selectedMonth = 0;
  var selectedDay = 0;

  var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
  this.setmonths = setmonths;
  function setmonths(p_months) {
    months = p_months;
    return p_months
  }

  var days = ['S','M','T','W','T','F','S'];
  this.setdays = setdays;
  function setdays(p_days) {
    days = p_days;
    return p_days
  }

  var appointments = '';
  this.setappointments = setappointments;
  function setappointments(p_appointments) {
    appointments = p_appointments;
    return p_appointments
  }

  var buttons = ['Clear','Close','Free termins for the day:','Chose your termin','Department'];
  this.setbuttons = setbuttons;
  function setbuttons(p_buttons) {
    buttons = p_buttons;
    return p_buttons
  }

  var message = '';
  this.setmessage = setmessage;
  function setmessage(p_message) {
    message = p_message;
    return p_message
  }

  var dateField = null;
  var SUDField = null;
  var APTERField = null;
  var DField = null;
  var TField = null;
  var noteField = null;
  var SDField = null;

  function getProperty(p_property){
    var p_elm = calendarId;
    var elm = null;

    if(typeof(p_elm) == "object"){
      elm = p_elm;
    } else {
      elm = document.getElementById(p_elm);
    }
    if (elm != null){
      if(elm.style){
        elm = elm.style;
        if(elm[p_property]){
          return elm[p_property];
        } else {
          return null;
        }
      } else {
        return null;
      }
    }
  }


  function setElementProperty(p_property, p_value, p_elmId){
    var p_elm = p_elmId;
    var elm = null;

    if(typeof(p_elm) == "object"){
      elm = p_elm;
    } else {
      elm = document.getElementById(p_elm);
    }
    if((elm != null) && (elm.style != null)){
      elm = elm.style;
      elm[ p_property ] = p_value;
    }
  }

  function setProperty(p_property, p_value) {
    setElementProperty(p_property, p_value, calendarId);
  }

  function getDaysInMonth(year, month) {
    return [31,((!(year % 4 ) && ( (year % 100 ) || !( year % 400 ) ))?29:28),31,30,31,30,31,31,30,31,30,31][month-1];
  }

  function getDayOfWeek(year, month, day) {
    var date = new Date(year,month-1,day)
    return date.getDay();
  }

  this.clearDate = clearDate;
  function clearDate() {
    dateField.value = '';
    SUDField.value = '';
    SDField.value = '';
    APTERField.value = '';
    DField.value = '';
    TField.value = '';
    noteField.value = '';
  }

  this.setDate = setDate;
  function setDate(year, month, day) {
    if (dateField) {
      if (month < 10) {month = "0" + month;}
      if (day < 10) {day = "0" + day;}

      var dateString = day+"."+month+"."+year;
      dateField.value = dateString;
    }
    return;
  }

  this.setDateTime = setDateTime;
  function setDateTime(year, month, day, time) {
    if (dateField) {
      if (month < 10) {month = "0" + month;}
      if (day < 10) {day = "0" + day;}

      var dateString = day+"."+month+"."+year+" "+time;
      dateField.value = dateString;
    }
    return;
  }

  this.setDateTimeSA = setDateTimeSA;
  function setDateTimeSA(year, month, day, time, SUD, APTER, des,SD) {
    if (dateField) {
      if (month < 10) {month = "0" + month;}
      if (day < 10) {day = "0" + day;}

      var dateString = day+"."+month+"."+year+" "+time;
      dateField.value = dateString;
      
      SUDField.value = SUD;
      APTERField.value = APTER;
      DField.value = year+"-"+month+"-"+day;
      TField.value = time;
      noteField.value = des;
      SDField.value = SD;
    }
    return;
  }

  this.changeMonth = changeMonth;
  function changeMonth(change) {
    currentMonth += change;
    currentDay = 0;
    if(currentMonth > 12) {
      currentMonth = 1;
      currentYear++;
    } else if(currentMonth < 1) {
      currentMonth = 12;
      currentYear--;
    }

    calendar = document.getElementById(calendarId);
    calendar.innerHTML = calendarDrawTable();
  }

  this.changeYear = changeYear;
  function changeYear(change) {
    currentYear += change;
    currentDay = 0;
    calendar = document.getElementById(calendarId);
    calendar.innerHTML = calendarDrawTable();
  }

  function getCurrentYear() {
    var year = new Date().getYear();
    if(year < 1900) year += 1900;
    return year;
  }

  function getCurrentMonth() {
    return new Date().getMonth() + 1;
  } 

  function getCurrentDay() {
    return new Date().getDate();
  }

  function getDayAppointment(p_year,p_month,p_day){
    for(var app in appointments) {
      if (appointments[app][0]==p_year
          && appointments[app][1]==p_month
          && appointments[app][2]==p_day
          ) {
          return appointments[app][3];
          }
    }
    return '';
  }


  function calendarDrawTable() {

    var dayOfMonth = 1;
    var validDay = 0;
    var startDayOfWeek = getDayOfWeek(currentYear, currentMonth, dayOfMonth);
    var daysInMonth = getDaysInMonth(currentYear, currentMonth);
    var css_class = null; //CSS class for each day
    var apphours = '';

    var table = "<table id='koledartable'>";
    table = table + "<tr class='header'>";
    table = table + "  <td colspan='2' class='previous'><a href='javascript:changeKoledarOMonth(-1);'>&lt;</a> <a href='javascript:changeKoledarOYear(-1);'>&laquo;</a></td>";
    table = table + "  <td colspan='3' class='title'>" + months[currentMonth-1] + "<br>" + currentYear + "</td>";
    table = table + "  <td colspan='2' class='next'><a href='javascript:changeKoledarOYear(1);'>&raquo;</a> <a href='javascript:changeKoledarOMonth(1);'>&gt;</a></td>";
    table = table + "</tr>";
    
    if (message == '') {
    table = table + "<tr><th>"+days[0]+"</th><th>"+days[1]+"</th><th>"+days[2]+"</th><th>"+days[3]+"</th><th>"+days[4]+"</th><th>"+days[5]+"</th><th>"+days[6]+"</th></tr>";
    for(var week=0; week < 6; week++) {
      table = table + "<tr>";
      for(var dayOfWeek=0; dayOfWeek < 7; dayOfWeek++) {
        if(week == 0 && startDayOfWeek == dayOfWeek) {
          validDay = 1;
        } else if (validDay == 1 && dayOfMonth > daysInMonth) {
          validDay = 0;
        }

        if(validDay) {
          if (dayOfMonth == selectedDay && currentYear == selectedYear && currentMonth == selectedMonth) {
            css_class = 'current';
          } else if (dayOfWeek == 0 || dayOfWeek == 6) {
            css_class = 'weekend';
          } else {
            css_class = 'weekday';
          }

          apphours = getDayAppointment(currentYear,currentMonth,dayOfMonth);
          if (apphours) {
            css_class = 'appointment';
          }

          if (css_class == 'appointment') {
          table = table + "<td><a class='"+css_class+"' href=\"javascript:clearAppointments('AppHours'); showAppointments('AppHours',"+currentYear+","+currentMonth+","+dayOfMonth+","+apphours+",'"+buttons[1]+"','"+buttons[2]+"','"+buttons[3]+"','"+buttons[4]+"');\" >"+dayOfMonth+"</a></td>";
            } else {
          table = table + "<td><a class='"+css_class+"' onclick=\"javascript:clearAppointments('AppHours')\">"+dayOfMonth+"</a></td>";
          }

          dayOfMonth++;
        } else {
          table = table + "<td class='empty'>&nbsp;</td>";
        }
      }
      table = table + "</tr>";
    }
    } else {
    table = table + "<tr><td class='koledartablemessage' colspan='7'>" + message +  "</td></tr>";
    }
    table = table + "<tr class='header'><th colspan='7' style='padding: 3px;'><a href='javascript:clearKoledarO();'>"+buttons[0]+"</a> | <a href='javascript:hideKoledarO();'>"+buttons[1]+"</a> </td></tr>";
    table = table + "</table><div id=\"AppHours\" class=\"PopUpWindow\"></div>";

    return table;
  }

  this.show = show;
  function show(field, Sfield, Afield, Dfi, Tfi, nfi,SDfi) {

    can_hide = 0;
    SUDField = Sfield;
    APTERField = Afield;
    DField = Dfi;
    TField = Tfi;
    noteField = nfi;
    SDField = SDfi;

    // If the calendar is visible and associated with
    // this field do not do anything.
    

    if (dateField == field) {
      return;
    } else {
      dateField = field;
    }

    if(dateField) {
      try {
        var dateString = new String(dateField.value);
        var dateParts = dateString.split("-");

        selectedMonth = parseInt(dateParts[0],10);
        selectedDay = parseInt(dateParts[1],10);
        selectedYear = parseInt(dateParts[2],10);
      } catch(e) {}
    }

    if (!(selectedYear && selectedMonth && selectedDay)) {
      selectedMonth = getCurrentMonth();
      selectedDay = getCurrentDay();
      selectedYear = getCurrentYear();
    }

    currentMonth = selectedMonth;
    currentDay = selectedDay;
    currentYear = selectedYear;

    if(document.getElementById){


      calendar = document.getElementById(calendarId);
      calendar.innerHTML = calendarDrawTable(currentYear, currentMonth);

      setProperty('display', 'block');

      var fieldPos = new positionInfo(dateField);
      var calendarPos = new positionInfo(calendarId);

     // var x = fieldPos.getElementLeft();
     // var y = fieldPos.getElementBottom();

     // setProperty('left', x + "px");
     // setProperty('top', y + "px");

      //if (document.all) {
      //  setElementProperty('display', 'block', 'KoledarOIFrame');
      ////  setElementProperty('left', x + "px", 'KoledarOIFrame');
      ////  setElementProperty('top', y + "px", 'KoledarOIFrame');
      ////  setElementProperty('width', calendarPos.getElementWidth() + "px", 'KoledarOIFrame');
      ////  setElementProperty('height', calendarPos.getElementHeight() + "px", 'KoledarOIFrame');
      //}
    }
  }

  this.hide = hide;
  function hide() {
    if(dateField) {
      setProperty('display', 'none');
      setElementProperty('display', 'none', 'KoledarOIFrame');
      dateField = null;
      SUDField = null;
      SDField = null;
      APTERFiels = null;
      DField = null;
      TField = null;
      noteField = null;
    }
  }

  this.visible = visible;
  function visible() {
    return dateField
  }

  this.can_hide = can_hide;
  var can_hide = 0;
}

var KoledarO = new KoledarO();

function showKoledarO(textField, textFieldSUD, textFieldAPTER, tD, tT, tn, tSD) {
  KoledarO.show(textField,textFieldSUD, textFieldAPTER, tD, tT, tn, tSD);
}


function clearKoledarO() {
  KoledarO.clearDate();
}

function hideKoledarO() {
  KoledarO.hide();
}

function setKoledarODate(year, month, day) {
  KoledarO.setDate(year, month, day);
}

function setKoledarODateTime(year, month, day, time) {
  KoledarO.setDateTime(year, month, day, time);
}

function setKoledarODateTimeSA(year, month, day, time, SUD, APTER, des, SD) {
  KoledarO.setDateTimeSA(year, month, day, time, SUD, APTER, des, SD);
}


function setElementProperty(p_elm, p_property, p_value){
	var elm = null;
	if(typeof(p_elm) == "object"){
		elm = p_elm;
	} else {
		elm = document.getElementById(p_elm);
	}
	if((elm != null) && (elm.style != null)){
		elm = elm.style;
		elm[ p_property ] = p_value;
	}
}
/*

var IE = document.all?true:false
// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)
// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;
// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0


function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}
  return true
}
*/
function showAppointments(objectid, year, month, day, hours, closebtn, text1, text2,text3) {

  showobj = document.getElementById(objectid);


 // var x = showobj.offsetLeft+showobj.offsetWidth;
  //var y = showobj.offsetTop;
  var tip = '';

 //   x = tempX-585;
 //   y = showobj.offsetTop; //tempY-440;
 //  positionInfo(showobj);


    setElementProperty(showobj, 'display', 'block');
   // setElementProperty(showobj, 'right', '-390px');
   // setElementProperty(showobj, 'top', '-120px');
   // setElementProperty(showobj, 'width', '390px');
   // setElementProperty(showobj, 'height', '290px');
    setElementProperty(showobj, 'overflow-y', 'scroll');

  printshow = '<div align=\"center\" ><b>'+text1+' '+day+'.'+month+'.'+year+'</b></div><hr>';
  printshow = printshow + text2 +'<hr><table>';
  for (oddind in hours) {

  //printshow = printshow + hours[oddind][0]+'</BR>';
  if (hours[oddind][0] != '')  {
  printshow = printshow + '<tr><th colspan=\"2\">'+text3+' '+hours[oddind][0]+'</th></tr>';   
  } 
  userdes = hours[oddind][2];

  for (userind in userdes) {
  tip = '';
  //printshow = printshow + userdes[userind][0]+'</BR>';
  if (userdes[userind][0] != '')  { 
  printshow = printshow + '<tr><th colspan=\"2\">'+userdes[userind][0]+'</th></tr>';   
  }
  daydes = userdes[userind][1];

  printshow = printshow + '<tr><td>';
  for (hoursind in daydes) {
     if (tip != (daydes[hoursind][1]) ) {
         //printshow = printshow + (daydes[hoursind][1]) +': '
         printshow = printshow + '</td></tr><tr><td nowrap=\"nowrap\"><b>' +(daydes[hoursind][1]) +'</b></td><td > ';        
    }
    tip = (daydes[hoursind][1]);
////  printshow = printshow + '<TR><TD><A href=\"javascript:setKoledarODateTime('+year+','+month+','+day+',\''+hours[hourind]+'\')\">'+ hours[hourind] +'</A></TD></TR>';
// printshow = printshow + '<A href=\"javascript:setKoledarODateTimeSA('+year+','+month+','+day+',\''+ (daydes[hoursind][0]) +'\','+(daydes[hoursind][2])+','+(daydes[hoursind][3])+',\''+ (daydes[hoursind][1]) +'\',\''+ (hours[oddind][1]) +'\');clearAppointments(\'AppHours\');hideKoledarO();\">'+ (daydes[hoursind][0]) +' '+(daydes[hoursind][1])+'</A>';
  
 printshow = printshow + ' <A href=\"javascript:setKoledarODateTimeSA('+year+','+month+','+day+',\''+ (daydes[hoursind][0]) +'\','+(daydes[hoursind][2])+','+(daydes[hoursind][3])+',\''+ (daydes[hoursind][1]) +'\',\''+ (hours[oddind][1]) +'\');clearAppointments(\'AppHours\');hideKoledarO();\">'+ (daydes[hoursind][0]) +'</A> ';
//  alert(daydes[hoursind][0]);
  }
  printshow = printshow + '</td></tr>';

    }
  }
  printshow = printshow +  '</table><hr />';
  
  printshow = printshow +  '<table class=\"zapriapphours\"><tr><td onclick=\"javascript:clearAppointments(\'AppHours\');\"> <b><a href=\"javascript:clearAppointments(\'AppHours\');\" >'+closebtn+'</a></b> </td></tr></table>';
  
  
  //  showobj.innerHTML = '<TABLE>' + printshow + '</TABLE>';
  showobj.innerHTML = printshow ;
}


function clearAppointments(objectid) {
  showobj = document.getElementById(objectid);
  setElementProperty(showobj, 'display', 'none');
}

function changeKoledarOYear(change) {
  KoledarO.changeYear(change);
}

function changeKoledarOMonth(change) {
  KoledarO.changeMonth(change);
}

