
var cal_ID = 0;

function CALENDAR(year, month) {

//========================================================================================================
// Configuration
//========================================================================================================

  this.tFontFace = 		'Arial, Helvetica'; // title: font family (CSS-spec, e.g. "Arial, Helvetica")
  this.tFontSize = 		14;                 // title: font size (pixels)
  this.tFontColor = 	'#FFFFFF';         // title: font color
  this.tBGColor = 		'#304B90';           // title: background color

  this.hFontFace = 		'Arial, Helvetica'; // heading: font family (CSS-spec, e.g. "Arial, Helvetica")
  this.hFontSize = 		12;                 // heading: font size (pixels)
  this.hFontColor = 	'#FFFFFF';         // heading: font color
  this.hBGColor = 		'#304B90';           // heading: background color

  this.dFontFace = 		'Arial, Helvetica'; // days: font family (CSS-spec, e.g. "Arial, Helvetica")
  this.dFontSize = 		14;                 // days: font size (pixels)
  this.dFontColor = 	'#000000';         // days: font color
  this.dBGColor = 		'#FFFFFF';           // days: background color

  this.wFontFace = 		'Arial, Helvetica'; // weeks: font family (CSS-spec, e.g. "Arial, Helvetica")
  this.wFontSize = 		12;                 // weeks: font size (pixels)
  this.wFontColor = 	'#FFFFFF';         // weeks: font color
  this.wBGColor = 		'#304B90';           // weeks: background color

  this.saFontColor = 	'#0000D0';        // Saturdays: font color
  this.saBGColor = 		'#F6F6FF';          // Saturdays: background color

  this.suFontColor = 	'#D00000';        // Sundays: font color
  this.suBGColor = 		'#FFF0F0';          // Sundays: background color

  this.tdBorderColor = 	'#FF0000';      // today: border color

  this.borderColor = 	'#304B90';        // border color
  this.hilightColor = 	'#FFFF00';       // hilight color (works only in combination with link)

  this.link = 			'javascript:;';          // page to link to when day is clicked
  this.offset = 		2;                     // week start: 0 - 6 (0 = Saturday, 1 = Sunday, 2 = Monday ...)
  this.weekNumbers = 	false;            // view week numbers: true = yes, false = no

  //this.AvailableDates =	["2008-07-30#BC0000", "2008-08-01#CCFF00", "2008-08-04#33FF00", "2008-08-05#00FFFF", "2008-08-06#FF00FF"];
  this.AvailableDates = [];
  this.model = 	0;

//--------------------------------------------------------------------------------------------------------
// You should change these variables only if you want to translate them into your language:
//--------------------------------------------------------------------------------------------------------
  // weekdays: must start with Saturday because January 1st of year 1 was a Saturday
  this.weekdays = ['Sa', 'Su', 'Mo', 'Tu', 'We', 'Th', 'Fr'];

  // months: must start with January
  this.months = ['January', 'February', 'March', 'April', 'May', 'June', 'July',
                 'August', 'September', 'October', 'November', 'December'];
  // error messages
  this.error = ['Year must be 1 - 3999!', 'Month must be 1 - 12!'];

//--------------------------------------------------------------------------------------------------------
// Don't change from here:
//--------------------------------------------------------------------------------------------------------
  this.size = 0;
  this.mDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

  if(year == null && month == null) {
    var obj = new Date();
    year = obj.getYear();
    if(year < 1900) year += 1900;
    month = obj.getMonth() + 1;
  }
  else if(year != null && month == null) month = 1;
  this.year = year;
  this.month = month;
  this.specDays = {};

//========================================================================================================
// Functions
//========================================================================================================
  this.set_styles = function() {
    var html = '';
    return html;
  }

  this.leap_year = function(year) {
    return (!(year % 4) && (year < 1582 || year % 100 || !(year % 400))) ? true : false;
  }

  this.get_weekday = function(year, days) {
    var a = days;
    if(year) a += (year - 1) * 365;
    for(var i = 1; i < year; i++) if(this.leap_year(i)) a++;
    if(year > 1582 || (year == 1582 && days >= 277)) a -= 10;
    if(a) a = (a - this.offset) % 7;
    else if(this.offset) a += 7 - this.offset;

    return a;
  }

  this.get_week = function(year, days) {
    var firstWDay = this.get_weekday(year, 0);
    return Math.floor((days + firstWDay) / 7) + (firstWDay <= 3);
  }

  this.table_cell = function(content, cls, date, style) {
    var size = Math.round(this.size * 1.5);
    var clsName = cls.toLowerCase();
    var html = '<td class="' + cls + '"';

	if(content != '&nbsp;' && clsName.indexOf('day') != -1) {
      var link = this.link;

      if(this.specDays[content]) {
        html += ' class="' + this.specDays[content][1] + '"';
        if(this.specDays[content][0]) {
          style += '';
        }
        if(this.specDays[content][1]) {
          html += ' title="' + this.specDays[content][1] + '"';
        }
        if(this.specDays[content][2]) link = this.specDays[content][2];
      }
      if(link) {
        //html += ' onClick="return ShowForm(\''+ date + '\');"';
      }
    }
    if(style) html += ' style="' + style + '"';
    if(this.specDays[content] || (content == 'Mo' || content == 'Tu' || content == 'We' || content == 'Th' || content == 'Fr' || content == 'Sa' || content == 'Su' || content == '&nbsp;' )) {
    	if(content == 'Mo' || content == 'Tu' || content == 'We' || content == 'Th' || content == 'Fr' || content == 'Sa' || content == 'Su' || content == '&nbsp;' ) {
    		html += '><span class="weekday">' + content + '</span></td>';
    	} else {
    		html += '><span>' + content + '</span></td>';
    	}
    } else {
		if ((ColorValue = this.Available(content)) != '') {
			html += (' style="background-color: #' + ColorValue + '"><a href="/calendar.html?S2XEvent=IndividualModel&model=' + this.model + '&date=' + this.year + '-' + (this.month < 10 ? '0' : '') + this.month + '-' + (content < 10 ? '0' : '') + content + '">' + content + '</a></td>');
		} else {
    		html += '><span>' + content + '</span></td>';
		}
    }

    return html;
  }
  
  this.Available = function(d){
  	inp = this.year + '-' + (this.month < 10 ? '0' : '') + this.month + '-' + (d < 10 ? '0' : '') + d;
  	for (i = 0; i < this.AvailableDates.length; i++){
  		date = this.AvailableDates[i].split('#');
  		if(date[0] == inp){
  			return date[1];
  		}
  	}
  	
  	return '';
  }

  this.table_head = function(content) {
    var html, ind, wDay, i;
    var cols = this.weekNumbers ? 8 : 7;

    html = '<tr><td colspan=' + cols + ' class="cssTitle"><table cellpadding="0" cellspacing="0" border="0" width="100%"><tr><td><a href="javascript:LoadPrevMonth();" class="prev"><img src="/public/images/calendar/left.gif" alt="&lt;" /></a></td><td><strong>' + content + '</strong></td><td><a href="javascript:LoadNextMonth();" class="next"><img src="/public/images/calendar/right.gif" alt="&gt;" /></a></td></tr></table></td></tr><tr>';
    for(i = 0; i < this.weekdays.length; i++) {
      ind = (i + this.offset) % 7;
      wDay = this.weekdays[ind];
      html += this.table_cell(wDay, 'cssHeading' + cal_ID);
    }
    if(this.weekNumbers) html += this.table_cell('&nbsp;', 'cssHeading' + cal_ID);
    html += '</tr>';

    return html;
  }

  this.viewEvent = function(from, to, color, title, link) {
    if(from > to) return;
    if(from < 1 || from > 31) return;
    if(to < 1 || to > 31) return;

    while(from <= to) {
      this.specDays[from] = [color, title, link];
      from++;
    }
  }

  this.create = function() {
    var obj, html, curYear, curMonth, curDay, start, stop, title, daycount, inThisMonth, weekNr, wdays, days, ind, cls, style, content, date, i;

    this.size = (this.hFontSize > this.dFontSize) ? this.hFontSize : this.dFontSize;
    if(this.wFontSize > this.size) this.size = this.wFontSize;

    obj = new Date();
    curYear = obj.getYear();
    if(curYear < 1900) curYear += 1900;
    curMonth = obj.getMonth() + 1;
    curDay = obj.getDate();

    if(this.year < 1 || this.year > 3999) html = '<b>' + this.error[0] + '</b>';
    else if(this.month < 1 || this.month > 12) html = '<b>' + this.error[1] + '</b>';
    else {
      if(this.leap_year(this.year)) this.mDays[1] = 29;
      for(i = days = 0; i < this.month - 1; i++) days += this.mDays[i];

      start = this.get_weekday(this.year, days);
      stop = this.mDays[this.month-1];

      html = this.set_styles();
      html += '<table border="0" cellspacing="2" cellpadding="0" class="com-calendar">';
      title = this.months[this.month-1] + ' ' + this.year;
      html += this.table_head(title);
      daycount = 1;

      if((this.year == curYear) && (this.month == curMonth)) inThisMonth = true;
      else inThisMonth = false;

      if(this.weekNumbers) weekNr = this.get_week(this.year, days);

      while(daycount <= stop) {
        html += '<tr>';

        for(i = wdays = 0; i <= 6; i++) {
          ind = (i + this.offset) % 7;
          if(ind == 0) cls = 'cssSaturdays';
          else if(ind == 1) cls = 'cssSundays';
          else cls = 'cssDays';

          style = '';
          date = this.year + '-' + this.month + '-' + daycount;

          if((daycount == 1 && i < start) || daycount > stop) content = '&nbsp;';
          else {
            content = daycount;
            if(inThisMonth && daycount == curDay) {
              style = '';
              cls = 'current';
            }
            else if(this.year == 1582 && this.month == 10 && daycount == 4) daycount = 14;
            daycount++;
            wdays++;
          }
          html += this.table_cell(content, cls + cal_ID, date, style);
        }

        if(this.weekNumbers) {
          if(!weekNr) {
            if(this.year == 1) content = '&nbsp;';
            else if(this.year == 1583) content = 52;
            else content = this.get_week(this.year - 1, 365);
          }
          else if(this.month == 12 && weekNr >= 52 && wdays < 4) content = 1;
          else content = weekNr;

          html += this.table_cell(content, 'cssWeeks' + cal_ID);
          weekNr++;
        }
        html += '</tr>';
      }
      html += '</table>';
    }
    return html;
  }
}

