/******
 * Author: Sebastian Haller
 * Date: 2005
 * Calendar Class
 */

function calendar(name, objname)
{
	// hide it when user clicks
  var oldfunc = document.onclick;
  if (typeof oldfunc != 'function')
  {
		document.onclick = function() {eval(name+'.hide();');};
  }
  else
  {
    document.onclick = function()
	    {
	      oldfunc();
	      eval(name+'.hide();');
	    };
  }

	this.name = name; // name of object you are creating
	this.objname = objname;
	this.obj = null;
	
	this.op = window.opera;
	this.ie = document.all && !this.op;
	this.ns = document.getElementById && !this.ie && !this.op;
	
	if (this.ie)
	{
		document.write('<iframe id="iframe_'+objname+'" name="iframe_'+objname+'" src="/img/blank.gif"  style="position: absolute; left:0; top:0; width:0; height:0; visibility:hidden; filter:alpha(opacity=0); z-index: 1"></iframe>');
		this.iframeobjname = 'iframe_'+objname;
		this.iframeobj = null;
	}

	this.cancelbubble =
		function(e)
		{
			if (window.event)
				event.cancelBubble=true;
			else if (e.stopPropagation)
				e.stopPropagation();

			return false;
		};

	this.hide =
		function()
		{
			if (this.obj == null)
				return false;
		
			this.obj.style.left = this.obj.style.top = -500;
			this.obj.style.visibility = 'hidden';
		
			if (this.ie)
				this.iframeobj.style.visibility = 'hidden';

			return false;
		};

	this.swap =
		// Opens, closes calendar window.
		// @param   string      object
		// @param   string      event
		// @param   string      field name
		// @param   string      min date
		// @param   string      allowed weekdays
		function(obj, e, field, min, days)
		{
			this.obj = document.getElementById(this.objname);
			if (this.ie)
				this.iframeobj = document.getElementById(this.iframeobjname);
		
			this.cancelbubble(e);

			if (e.type=="click" && (this.obj.style.visibility=='hidden' || this.obj.style.visibility=='') || e.type=="mouseover")
				this.show(obj, field, min, days, 0);
			else if (e.type=="click")
				this.hide();
		
			return false;
		};

	this.geturl =
		// Opens, closes calendar window.
		// @param   string      object
		// @param   string      event
		// @param   string      field name
		// @param   string      min date
		// @param   string      allowed weekdays
		function(obj, e, field, min, days)
		{
			this.obj = document.getElementById(this.objname);
			if (this.ie)
				this.iframeobj = document.getElementById(this.iframeobjname);
		
			this.cancelbubble(e);

			if (e.type=="click" && (this.obj.style.visibility=='hidden' || this.obj.style.visibility=='') || e.type=="mouseover")
				this.show(obj, field, min, days, 1);
			else if (e.type=="click")
				this.hide();
		
			return false;
		};

	this.show =
		// Opens calendar window.
		// @param   string      object
		// @param   string      field name
		// @param   string      min date
		// @param   string      allowed weekdays
		function(obj, field, min, days, link)
		{
			this.obj = document.getElementById(this.objname);
			if (this.ie)
				this.iframeobj = document.getElementById(this.iframeobjname);
		
			this.dateField_y = document.getElementById(field+'_y');
			this.dateField_m = document.getElementById(field+'_m');
			this.dateField_d = document.getElementById(field+'_d');
			this.minDate = min;
			this.daysInWeek = days;
			this.iyear = NaN;
			this.imonth = NaN;
			this.year = NaN;
			this.month = NaN;
			this.day = NaN;
			this.myear = 0;
			this.mmonth = 0;
			this.mday = 0;
			this.url = link;
	
			this.obj.style.left = this.obj.style.top = -500;
			this.obj.style.left = this.getoffset(obj, 'x') - this.obj.offsetWidth + obj.offsetWidth + "px";
			this.obj.style.top = this.getoffset(obj, 'y') + obj.offsetHeight + "px";
		
			this.obj.style.visibility = 'visible';
			
			this.initcalendar();
		
			if (this.ie)
			{
				this.iframeobj.style.left = this.obj.style.left;
				this.iframeobj.style.top = this.obj.style.top;
				this.iframeobj.style.width = this.obj.offsetWidth;
				this.iframeobj.style.height = this.obj.offsetHeight;
				this.iframeobj.style.visibility = 'visible';
			}

			return false;
		};

	this.iecompat =
		function()
		{
			return (document.compatMode && document.compatMode != 'BackCompat') ? document.documentElement : document.body;
		};

	this.getoffset =
		function(obj, which)
		{
			var offset = (which == 'x') ? obj.offsetLeft : obj.offsetTop;
			var parent = obj.offsetParent;
			while (parent!=null)
			{
				offset += (which == 'x') ? parent.offsetLeft : parent.offsetTop;
				parent = parent.offsetParent;
			}
			
			// check, if there is enough space in the broser
			if (which == 'x')
			{
				var windowedge = this.ie ? this.iecompat().scrollLeft+this.iecompat().clientWidth-15 : window.pageXOffset+window.innerWidth-15;
				if (windowedge-offset < this.obj.offsetWidth)
					offset -= this.obj.offsetWidth - obj.offsetWidth;
			}
			else
			{
				var topedge = this.ie ? this.iecompat().scrollTop : window.pageYOffset;
				var windowedge = this.ie ? this.iecompat().scrollTop+this.iecompat().clientHeight-15 : window.pageYOffset+window.innerHeight-18;
				if (windowedge-offset < this.obj.offsetHeight)
				{
					offset -= this.obj.offsetHeight + obj.offsetHeight;
					if (offset-topedge < this.obj.offsetHeight)
						offset = topedge - obj.offsetHeight;
				}
			}
		
			return offset;
		};

	this.formatnum2 =
		// Formats number to two digits.
		// @param   int number to format.
		function(i, valtype)
		{
			f = (i < 10 ? '0' : '') + i;
			if (valtype && valtype != '')
			{
				switch(valtype)
				{
					case 'month':
						f = (f > 12 ? 12 : f);
						break;
		
					default:
					case 'day':
						f = (f > 31 ? 31 : f);
						break;
				}
			}
		
			return f;
		};

	this.formatnum4 =
		// Formats number to four digits.
		// @param   int number to format.
		function(i)
		{
			return (i < 1000 ? i < 100 ? i < 10 ? '000' : '00' : '0' : '') + i;
		};

	// Initializes calendar window.
 this.initcalendar =
		function() {
			if (this.minDate && (!this.myear || !this.mmonth || !this.mday))
			{
				date = this.minDate.split("-");
				this.mday = parseInt(date[2],10);
				this.mmonth = parseInt(date[1],10) - 1;
				this.myear = parseInt(date[0],10);
			}
		
			if (!this.year && !this.month && !this.day)
			{
				/* Called for first time */
				if (this.dateField_y.value)
					this.iyear = parseInt(this.dateField_y.value,10);
				if (this.iyear<100 && this.iyear>=70)
					this.iyear += 1900;
				if (this.iyear<100 && this.iyear)
					this.iyear += 2000;
				if (this.dateField_m.value)
					this.imonth = parseInt(this.dateField_m.value,10) - 1;
				if (this.dateField_d.value)
					this.day = parseInt(this.dateField_d.value,10);
				if (isNaN(this.iyear) || isNaN(this.imonth) || isNaN(this.day) || !this.day)
				{
					dt = new Date();
					this.iyear = dt.getFullYear();
					this.imonth = dt.getMonth();
					this.day = dt.getDate();
				}
				this.year = this.iyear;
				this.month = this.imonth;
			}
			else
			{
				/* Moving in calendar */
				if (this.month > 11)
				{
					this.month = 0;
					this.year++;
				}
				if (this.month < 0)
				{
					this.month = 11;
					this.year--;
				}
			}
		
			var tmday = 0;
			if (this.year<this.myear)
				this.year = this.myear;
			if (this.year==this.myear)
			{
				if (this.month<this.mmonth)
					this.month = this.mmonth;
				if (this.month==this.mmonth)
					tmday=this.mday;
			}
		
			str = '';
		
			//heading table
			str += '<table class="calendar"><tr><td class="top" width="100%">';
		//	str += '<form method="NONE" onsubmit="return 0">';
			str += '<a href="javascript:'+this.name+'.month--; '+this.name+'.initcalendar();">&laquo;</a> ';


/*
			str += '<select class="dropdown" id="select_month" name="monthsel" onchange="'+this.name+'.month = parseInt(document.getElementById(\'select_month\').value); '+this.name+'.initcalendar();">';
			for (i =0; i < 12; i++)
			{
				if (i == this.month)
					selected = ' selected="selected"';
				else
					selected = '';
				str += '<option value="' + i + '" ' + selected + '>' + month_names[i] + '</option>';
			}
			str += '</select>';
*/

			str += '' + month_names[ this.month ] + ' ';

			str += '' + this.year + '';

			str += ' <a href="javascript:'+this.name+'.month++; '+this.name+'.initcalendar();">&raquo;</a>';
/*
			str += '</form>';
			str += '</th><td class="top" width="50%">';
			str += '<form method="NONE" onsubmit="return 0">';
			str += '<a href="javascript:'+this.name+'.year--; '+this.name+'.initcalendar();">&laquo;</a> ';
			str += '<select class="dropdown" id="select_year" name="yearsel" onchange="'+this.name+'.year = parseInt(document.getElementById(\'select_year\').value); '+this.name+'.initcalendar();">';
			for (i = this.year - 25; i < this.year + 25; i++)
			{
				if (i == this.year)
					selected = ' selected="selected"';
				else
					selected = '';
				str += '<option value="' + i + '" ' + selected + '>' + i + '</option>';
			}
			str += '</select>';
			str += ' <a href="javascript:'+this.name+'.year++; '+this.name+'.initcalendar();">&raquo;</a>';
			str += '</form>';
*/
			str += '</th></tr></table>';
		
			str += '<table class="calendar"><tr>';
			for (i = 0; i < 7; i++)
				str += '<th>' + day_names[i] + '</th>';
			str += '</tr>';
		
			var firstDay = new Date(this.year, this.month, 1).getDay();
			// first day of week is Monday (complies with ISO standard 8601, section 3.17)
			firstDay = (firstDay+6) % 7;
			var lastDay = new Date(this.year, this.month + 1, 0).getDate();
		
			str += "<tr>";
		
			dayInWeek = 0;
			for (i = 0; i < firstDay; i++)
			{
				str += '<td class="empty">&nbsp;</td>';
				dayInWeek++;
			}
			for (i = 1; i <= lastDay; i++)
			{
				if (dayInWeek == 7)
				{
					str += "</tr><tr>";
					dayInWeek = 0;
				}
		
				dispmonth = 1 + this.month;
		
				actVal = "'" + this.formatnum4(this.year) + "', '" + this.formatnum2(dispmonth, 'month') + "', '" + this.formatnum2(i, 'day') + "'";
				if (this.year==this.iyear && this.month==this.imonth && i == this.day)
					style = ' class="selected"';
				else
					style = '';
				str += '<td' + style + '>';
				if (this.daysInWeek.indexOf(dayInWeek+1)>=0 && i>=tmday) {
					if (this.url == 1) {
						str += '<a href="' + linkstring_o + this.formatnum4(this.year) + '-' + this.formatnum2(dispmonth, 'month') + '-' + this.formatnum2(i, 'day') + linkstring_e +'">';
					}
					else {
						str += '<a href="javascript:'+this.name+'.returndate(' + actVal + ');">';
					}
				}
				str += i + "</a></td>"
				dayInWeek++;
			}
			for (i = dayInWeek; i < 7; i++)
				str += '<td class="empty">&nbsp;</td>';
		
			str += "</tr></table>";
		
			this.obj.innerHTML = str;
		};

 	this.returndate =
		// Returns date from calendar.
		// @param   string     date text
		function(y, m, d)
		{
			// with leading zeros
			this.dateField_y.value = y;
			this.dateField_m.value = m;
			this.dateField_d.value = d;
		
			this.hide();
		};

	return true;
}
