

var calendarId = '';

function wShowCalendarDate(year, month, day, type, id2, obj){
  
  calendarId = id2;
  
  if(type == 'popup'){ 
    window.open('calendar.htm?year='+year+'&month='+month+'&day='+day+'&type='+type+'&id='+id2, '', 'width=200,height=200');
  }
  else if(type == 'layer'){       
    if(objCalendar.id!=''){
      var containerCalendar = document.getElementById('wCalendar' + objCalendar.id);
      containerCalendar.innerHTML = '';
    }
    objCalendar.init(parseInt(day), parseInt(month), parseInt(year), 'layer', id2, obj); 
  }
  else{    

    var divContainer = document.createElement('div');
    divContainer.id  = 'wCalendarContainer_'+id2;
    
    with (divContainer.style){
      backgroundColor = '#ff0000';
      height    = '210px';
      left      = obj.offsetLeft + obj.offsetWidth + 'px';      
      position  = 'absolute';
      textAlign = 'center';
      top       = obj.offsetTop + obj.offsetHeight + 'px';
      width     = '195px';
    }

    var iFrame  = document.createElement('iframe');
    iFrame.src  = 'calendar.htm?year='+year+'&month='+month+'&day='+day+'&type='+type+'&id='+id2;
    iFrame.frameborder = '0';
    with (iFrame.style){
      border   = '0px';
      height    = '180px';
      position  = 'relative';
      top       = '20px';
      width     = '175px';
    }
    
    divContainer.appendChild(iFrame);    
    document.body.appendChild(divContainer);
  }
}

function wShowCalendarString(dateStr, formatStr, type, id){
  if (type == 'popup'){ 
    window.open('calendar.htm?', '', 'width=200,height=200');
  }
}

function wCloseCalendar(id){
  document.body.removeChild(document.getElementById('wCalendarContainer_'+id));
}

function wHideCalendar(id){
  if(document.getElementById('wCalendar' + id)){
    document.getElementById('wCalendar' + id).style.display = "none";
  }
}





Calendar = function(){
  var doc            = document;
  
  this.day            = 0;
  this.id             = '';
  this.month          = 0;
  this.type           = '';
  this.year           = 0;
  this.obj            = null;
  
  this.CELLID_DAYS    = 'cal_day_';
  this.CELLID_HEAD    = 'cal_head'
  this.CELLID_WEEKS   = 'cal_week_';
  this.DEVMODE        = true; //Fehler anzeigen!
  this.LANG_DAYS      = new Array('Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So');
  this.LANG_MONTHS    = new Array('Januar', 'Februar', 'Maerz', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember');
  this.LANG_TODAY     = 'Heute';
  this.LIMITMAX_YEAR  = 2050;
  this.LIMITMIN_YEAR  = 1970;
  this.MSG_MAXYEAR    = 'Sie haben das maximale Jahr erreicht.';
  this.MSG_MINYEAR    = 'Sie haben das minimale Jahr erreicht.';
  
  
   this.calcCalendarWeek = function(day, month, year){
    var tmpDate = new Date(year, month - 1, day);

    year += (year < 1900) ? 1900 : 0;
    year++;
    
    var week = this.calcCalendarWeek_(tmpDate, year);
    while(week < 1){
      year--;
      week = this.calcCalendarWeek_(tmpDate, year);
    }
      
    return week;
  
  }
   this.calcCalendarWeek_ = function(dateObj,year) {
    var doomDay = new Date(year, 0, 4);
    
    return Math.floor(1.05 + (dateObj.getTime() - doomDay.getTime()) / 6048e5 + ((doomDay.getDay() + 6) % 7) / 7);
  }
  
  
   this.calcDaysPerMonth = function(month, year){

    if (month == 2){
      return this.isLeapYear(year) ? 29 : 28;
    }
    else{

      if (month < 8){
        return (month % 2 == 1) ? 31 : 30;
      }
      else{
        return (month % 2 == 0) ? 31 : 30;
      }
    }
  }
  
  
   this.calcWeekday = function(day, month, year){
    var century = parseInt(year / 100);
    var decade  = (year % 100);

    if (month < 3){
      month += 10;
      if (decade == 0){
        decade = 99;
        century--;
      }
      else{
        decade--;
      }
    }
    else{
      month -= 2;
    }

    var res = (day + Math.floor(2.61 * month - 0.2) + decade + Math.floor(decade / 4) + Math.floor(century / 4) - 2 * century);

    return (res % 7 >= 0) ? (res % 7) : (7 - Math.abs(res % 7));
  }
    
  
   this.correctYear = function(year){
    if (year < 1000){
      return (year + 1900);
    }
    else{
      return year;
    }
  }
      
  
   this.error = function(txt){
    if (this.DEVMODE){
      alert('ERROR\n' + txt);
    }
  }
  
  
   this.init = function(day, month, year, type, id, obj){
    var objTable = doc.createElement('table'); 
    
    var objTd    = null;
    var objTh    = null;
    var objTr    = null;
    
    this.day     = day;
    this.id      = id;
    this.month   = month;
    this.type    = type;
    this.year    = year;
    this.obj     = obj;
    
    objTable.className = 'wCalendar';
    objTable.style.marginLeft = '-99px';
    
    if(ie){
     objTable.style.marginTop = '5px'; 
    }
    else{
      objTable.style.marginTop = '6px';
    }

    var objTBody = doc.createElement('tbody');
    objTable.appendChild(objTBody);

    var cellspacingAttribute = document.createAttribute("cellspacing");
    cellspacingAttribute.nodeValue = "0";
    objTable.setAttributeNode(cellspacingAttribute);    
    
    var cellpaddingAttribute = document.createAttribute("cellpadding");
    cellpaddingAttribute.nodeValue = "0";
    objTable.setAttributeNode(cellpaddingAttribute);   
          
    var widthAttribute = document.createAttribute("width");
    widthAttribute.nodeValue = "192";
    objTable.setAttributeNode(widthAttribute);      

    objTr = doc.createElement('tr');
      objTh = doc.createElement('th');
      with (objTh){
        id = this.CELLID_HEAD;

        var colspanAttribute = document.createAttribute("colspan");
        colspanAttribute.nodeValue = "8";
        objTh.setAttributeNode(colspanAttribute);   
        
        if(ie){
          objTh.style.height = "17px";
          objTh.style.paddingTop = "2px";
        }
        else{
          objTh.style.height = "20px"; 
        }
        
        objTh.style.color = "#000000";
        objTh.style.fontWeight = "normal";
        objTh.style.paddingLeft = "5px";
        objTh.style.borderBottom = "1px solid #999999";
        objTh.style.backgroundImage = "url('/weblication/grid/gui/wImages/style/wSheetElementHeaderBg.gif')";
        objTh.style.backgroundRepeat = "repeat-x";
      }         
      
      objTr.appendChild(objTh);
    objTBody.appendChild(objTr);

    objTr = doc.createElement('tr');
      objTd = doc.createElement('td');
      with (objTd){
        
        var colspanAttribute = document.createAttribute("colspan");
        colspanAttribute.nodeValue = "3";
        objTd.setAttributeNode(colspanAttribute);         
                
        innerHTML  = '<a class="wButton" style="float:left; margin-left:1px; margin-top:1px; display:block; text-align:center; text-decoration:none; width:23px; height:16px; line-height:16px;" href="javascript:objCalendar.lastYear();"><img src="/weblication/grid/gui/wImages/icon_small/calendarPrevYear.gif" border="0"/></a><a class="wButton" style="float:left; margin-left:1px; margin-top:1px; display:block; text-align:center; text-decoration:none; width:18px; height:16px; line-height:16px;" href="javascript:objCalendar.lastMonth();"><img src="/weblication/grid/gui/wImages/icon_small/calendarPrevMonth.gif" border="0"/></a>';
        
        objTd.style.height = "21px";
        objTd.style.backgroundColor = "#DDDDDD";      
      }        
      objTr.appendChild(objTd);
      
      

      objTh = doc.createElement('th');
      with (objTh){
        innerHTML = '<a class="wButton" style="color:#000000; margin-top:1px; display:block; text-align:center; text-decoration:none;  height:16px; line-height:16px;" href="javascript:objCalendar.setDateCurrent();objCalendar.show();">'+this.LANG_TODAY+'</a>';
        var colspanAttribute = document.createAttribute("colspan");
        colspanAttribute.nodeValue = "2";
        objTh.setAttributeNode(colspanAttribute); 
        
        objTh.style.color = "#000000";
        objTh.style.fontWeight = "normal";        
        objTh.style.height = "21px";
        objTh.style.backgroundColor = "#DDDDDD";         
      }
      objTr.appendChild(objTh);
      
     
      
      objTd = doc.createElement('td');
      with (objTd){
        var colspanAttribute = document.createAttribute("colspan");
        colspanAttribute.nodeValue = "3";
        objTd.setAttributeNode(colspanAttribute);           
        
        innerHTML = '<a class="wButton" style="float:right; margin-right:1px; margin-top:1px; display:block; text-align:center; text-decoration:none; width:23px; height:16px; line-height:16px;" href="javascript:objCalendar.nextYear();"><img src="/weblication/grid/gui/wImages/icon_small/calendarNextYear.gif" border="0"/></a><a class="wButton" style="float:right; margin-right:1px; margin-top:1px; display:block; text-align:center; text-decoration:none; width:18px; height:16px; line-height:16px;" href="javascript:objCalendar.nextMonth();"><img src="/weblication/grid/gui/wImages/icon_small/calendarNextMonth.gif" border="0"/></a>';
        objTd.style.height = "21px";
        objTd.style.backgroundColor = "#DDDDDD";  
      }
          
      objTr.appendChild(objTd);
      
     
    objTBody.appendChild(objTr);

    objTr = doc.createElement('tr');

      objTd = doc.createElement('td');
      objTd.style.backgroundColor = "#EEEEEE";      
      
      objTr.appendChild(objTd);
      for (var i = 0; i < this.LANG_DAYS.length; i++){
        objTd = doc.createElement('td');
        objTd.innerHTML = this.LANG_DAYS[i];      
        objTd.style.textAlign = "center";
        objTd.style.color = "#7F7F7F";
        var widthAttribute = document.createAttribute("width");
        widthAttribute.nodeValue = "24";
        objTd.setAttributeNode(widthAttribute);         
        
        objTr.appendChild(objTd);     
      }
    
    objTr.style.height = "24px";
    objTr.style.fontWeight = "bold";    
      
    objTBody.appendChild(objTr);

    for (var i = 0; i < 6; i++){
      objTr = doc.createElement('tr');
      for (var j = 0; j < 8; j++){  
        objTd = doc.createElement('td');

        if (j > 0){
          objTd.id = this.CELLID_DAYS + (i * 7 + j - 1);
          objTd.innerHTML = '.';
        }
        else{
          objTd.id = this.CELLID_WEEKS + i;
          objTd.style.textAlign = "center";
          objTd.style.fontWeight = "bold";
          objTd.style.color = "#777777";
          objTd.style.height = "18px";
          objTd.style.lineHeight = "16px";
          objTd.style.backgroundColor = "#EEEEEE";
        }

        objTr.appendChild(objTd);
      }
      objTBody.appendChild(objTr);
    }

    objTable.appendChild(objTBody);
    var containerCalendar = document.getElementById('wCalendar' + id);

    containerCalendar.innerHTML = '';
    containerCalendar.appendChild(objTable);
    
    this.show();
  }
  
  
   this.isLeapYear = function(year){
    return (((year % 4 == 0) & ~(year % 100 == 0)) | (year % 400 == 0))
  }
        
  
   this.isToday = function(day){
    var dateObj = new Date();

    return ((day == dateObj.getDate()) && (this.month == (dateObj.getMonth() + 1)) && (this.year == this.correctYear(dateObj.getYear())))
  }
    
  
   this.lastMonth = function(){  
    if (this.month == 1){
      this.month = 12;
      this.year--;
    }
    else{
      if (this.year == this.LIMITMIN_YEAR){
        alert(this.MSG_MINYEAR);
        return;
      }
      this.month--;
    }
    
    this.show();
  }
    
  
   this.lastYear = function(){  
    if (this.year == this.LIMITMIN_YEAR){
      alert(this.MSG_MINYEAR);
      return;
    }
    
    this.year--;
    this.show();
  }
  
  
   this.modulo = function(num, mod){
    var tmp = num % mod;
    
    return (tmp < 0) ? (mod + tmp) : tmp;
  }
  
  
   this.nextMonth = function(){  
    if (this.month == 12){
      this.month = 1;
      this.year++;
    }
    else{
      if (this.year == this.LIMITMAX_YEAR){
        alert(this.MSG_MAXYEAR);
        return;
      }
      this.month++;
    }

    this.show();
  }
    
  
   this.nextYear = function(){  
    if (this.year == this.LIMITMAX_YEAR){
      alert(this.MSG_MAXYEAR);
      return;
    }
    
    this.year++;
    this.show();
  }
    
  


   this.close = function(){

    
    if(this.type == 'popup'){
      self.close();
    }
    else if(this.type == 'layer'){
      var containerCalendar = document.getElementById('wCalendar' + this.id);
      containerCalendar.innerHTML = '';
    }    
    else{
      parent.wCloseCalendar(this.id);
    }
  }
    
  
  
   this.onSelect = function(day){
    
    var prefix = (this.type == 'layer') ? '' : 'opener.';
    
    if(typeof eval(prefix + 'wSetDateCalendar' + this.id + '') == 'function'){
      var year  = String(this.year);
      var month = String(this.month);  
      day       = String(day);  
      
      if(month.length == 1){
        month = '0' + month;
      }  

      if(day.length == 1){
        day = '0' + day;
      }  
      var evalStr = prefix + "wSetDateCalendar" + this.id + "('" + year + "', '" + month + "', '" + day + "', this.obj)";
      eval(evalStr);
    }    
    else if(typeof eval(prefix + 'calendar_' + this.id + '_onSelect') == 'function'){
      var year  = String(this.year);
      var month = String(this.month);  
      day       = String(day);  
      
      if(month.length == 1){
        month = '0' + month;
      }  

      if(day.length == 1){
        day = '0' + day;
      }  
      var evalStr = prefix + "calendar_" + this.id + "_onSelect('" + year + "', '" + month + "', '" + day + "', this.obj)";
      eval(evalStr);
    }
    else{
      this.error('Keine Event-Funktion (onSelect) gefunden.\n\'' + this.id + '\'-Modus.');
    }

    
    if(this.type == 'popup'){
      self.close();
    }
    else if(this.type == 'layer'){
      var containerCalendar = document.getElementById('wCalendar' + this.id);
      containerCalendar.innerHTML = '';
    }    
    else{
      parent.wCloseCalendar(this.id);
    }
  }
  
  
   this.setDateCurrent = function(){
    var dateObj = new Date();
    
    this.day   = dateObj.getDate();
    this.month = dateObj.getMonth() + 1;
    this.year  = this.correctYear(dateObj.getYear());
  }
  
  
   this.show = function(){

    
    if(this.year > 1970){
      var daysMonth = this.calcDaysPerMonth(this.month, this.year);
      var firstDay  = this.calcWeekday(1, this.month, this.year);

      firstDay = (firstDay == 0) ? 6 : (firstDay - 1);

      var weekLimit = ((firstDay + daysMonth - 1) < 35) ? 5 : 6;

      for (var i = 0; i < 6; i++){        
        if (i < weekLimit){
          doc.getElementById(this.CELLID_WEEKS + i).innerHTML = this.calcCalendarWeek((i * 7) + 1, this.month, this.year);
        }
        else{
          doc.getElementById(this.CELLID_WEEKS + i).innerHTML = '';
        }
      }
      
      var obj = null;

      for (var i = 0; i < 42; i++){
        
        obj = doc.getElementById(this.CELLID_DAYS + i)
        
        if ((i >= firstDay) && (i < (firstDay + daysMonth))){

          if ((i % 7 == 5) || (i % 7 == 6)){
            obj.className = 'dayWeekend';
          }
          else{
            obj.className = 'day';
          }

          if (this.isToday(i - firstDay + 1)){
            obj.className = 'dayToday';
          }

    
          obj.innerHTML = '<a style="display:block; height:16px; cursor:pointer; text-decoration:none;" href="javascript:objCalendar.onSelect(' + (i - firstDay + 1) + ');">' + (i - firstDay + 1) + '</a>';
        }
        else{ 
          obj.className = '';
          obj.innerHTML = '';
        }
      }
    }
    else{
      this.error('Only years after 1970 allowed!\n\nWrong date:\n- Day: ' + this.day + '\n- Month: ' + this.month + '\n- Year: ' + this.year);
      return;
    }

    
    doc.getElementById(this.CELLID_HEAD).innerHTML = "";
    
    var objHeaderTitle = document.createElement('div');

    if (ie){                                                                                             
      objHeaderTitle.innerHTML = "<div style='float:left; margin-top:2px;'>"+this.LANG_MONTHS[this.month - 1] + ', ' + this.year+"</div><div style='float:right;cursor:pointer;'><a class='wButton' style='margin-right:2px; margin-top:1px; color:#E60003; display:block; text-align:center; text-decoration:none; width:14px; height:13px; line-height:10px; font-size:10px; font-weight:bold; overflow:hidden;' onclick='objCalendar.close();'><img src='/weblication/grid/gui/wImages/icon_small/calendarClose.gif' border='0'/></a></div><div style='clear:both'></div>"; 
    }
    else{
      objHeaderTitle.innerHTML = "<div style='float:left; margin-top:3px;'>"+this.LANG_MONTHS[this.month - 1] + ', ' + this.year+"</div><div style='float:right;cursor:pointer;'><a class='wButton' style='margin-right:2px; margin-top:2px; color:#E60003; display:block; text-align:center; text-decoration:none; width:14px; height:13px; line-height:10px; font-size:10px; font-weight:bold; overflow:hidden;' onclick='objCalendar.close();'><img src='/weblication/grid/gui/wImages/icon_small/calendarClose.gif' border='0'/></a></div><div style='clear:both'></div>"; 
    }

    doc.getElementById(this.CELLID_HEAD).appendChild(objHeaderTitle);
  }
}

var objCalendar = new Calendar();









function wTimestamp(timestamp, type){
	if(type == 'js'){
		return timestamp/1000;
	}
	else if(type == 'php'){
		return timestamp*1000;
	}
	else{
		return timestamp;
	}
}
