/******
 * Author: Ivo Kotev
 * Date: 2006
 * Calendar Class
 */

var month_names = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
var day_names = new Array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");


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="images/spacer.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';

			clearTimeout(this.timeout);

			return false;
		};

	this.swap =
		// Opens, closes calendar window.
		// @param   string      object
		// @param   string      event
		// @param   string      field name
		// @param   string      edit type - date/timestamp
		function(obj, e, dateSet, datetype, mindate, maxdate){
			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, dateSet, datetype, mindate, maxdate);
			else if (e.type=="click")
				this.hide();
		
			return false;
		};

	this.show =
		// Opens calendar window.
		// @param   string      object
		// @param   string      field name
		// @param   string      edit type - date/timestamp
		function(obj, dateSet,  mindate, maxdate){
			this.obj = document.getElementById(this.objname);
			this.mindate = mindate;
			this.maxdate = maxdate;
			if (this.ie)
				this.iframeobj = document.getElementById(this.iframeobjname);
		
			this.dayfield = document.getElementById("Srch"+dateSet+"Day");
			this.monthfield = document.getElementById("Srch"+dateSet+"Month");
			this.yearfield = document.getElementById("Srch"+dateSet+"Year");
			this.dateSet = dateSet;
			this.iyear = NaN;
			this.imonth = NaN;
			this.year = NaN;
			this.month = NaN;
			this.day = NaN;
			this.hour = NaN;
			this.minute = NaN;
			this.second = NaN;
	
			this.obj.style.left = this.obj.style.top = -500;
			this.obj.style.left = this.getoffset(obj, 'x') + "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';
			}
					
			clearTimeout(this.timeout);

			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;
		
					case 'day':
						f = (f > 31 ? 31 : f);
						break;
		
					case 'hour':
						f = (f > 23 ? 23 : f);
						break;
		
					default:
					case 'second':
					case 'minute':
						f = (f > 59 ? 59 : 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.year && !this.month && !this.day)
			{
				this.iyear = this.yearfield.value
				this.imonth = this.monthfield.value -1 
				this.day = this.dayfield.value
				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--;
				}
			}
		
			str = "";
			//heading table
			today = new Date()
		//	str += '<form method="NONE" onsubmit="return 0">';
			str +='<table width=100% border="0" cellpadding="0" cellspacing="0" class="calendar">'
            str +='  <tr>'
            str +='    <td align=left width="40">'
            str +='			<a href="javascript:'+this.name+'.year--; '+this.name+'.initcalendar();"><img src="' + g_httpURL + 'tosc/images/Availability_left.gif" border=0><img src="' + g_httpURL + 'tosc/images/Availability_left.gif" border=0></a>&nbsp;&nbsp;'
            str +='			<a href="javascript:'+this.name+'.month--; '+this.name+'.initcalendar();"><img src="' + g_httpURL + 'tosc/images/Availability_left.gif" border=0></a>'
            str +='	   </td>'
            str +='    <td align=middle width="50"><span id="month">'+month_names[this.month] +'&nbsp;'+this.year+'</span></td>'
            str +='    <td align=right width="40">'
			str +='			<a href="javascript:'+this.name+'.month++; '+this.name+'.initcalendar();"><img src="' + g_httpURL + 'tosc/images/Availability_right.gif" border=0></a>&nbsp;&nbsp;'
			str +='			<a href="javascript:'+this.name+'.year++; '+this.name+'.initcalendar();"><img src="' + g_httpURL + 'tosc/images/Availability_right.gif" border=0><img src="' + g_httpURL + 'tosc/images/Availability_right.gif" border=0></a>';
			str +='		</td>'
            str +='  </tr>'
            str +='</table>'
			str +='<table width=100% border=0 cellpadding=0 cellspacing=0>'
           	str +='   <tr align=middle>'	
            str +='    <td><table width="100%" border="0" cellpadding="1" cellspacing="0" class="calendar">'
            str +='        <tr>'
			for (i = 0; i < 7; i++)
				str +='          <td width="14%" id="days_of_week" align="center" valign="bottom">' + day_names[i] +'</td>'
            str +='        </tr>'
            str +='      </table></td>'
            str +='  </tr>'
            str +='</table>'

			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-1) % 7;
			if(firstDay<0) firstDay= 6;
			var lastDay = new Date(this.year, this.month + 1, 0).getDate();
		
			str += '<table id="calendar_day_table" border="0" cellpadding="1" cellspacing="1" width="100%">'
    	    str += '      <tr>'
		
			dayInWeek = 0;
			for (i = 0; i < firstDay; i++)
			{
				str += '<td>&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="day_selected"';
				else
					style = ' class="day"';

				
				if( this.mindate > new Date(this.year, this.month, i+1) || (this.maxdate <= new Date(this.year, this.month, i)) ){
					str += '<td' + style + ' align="center" >'
					str += '' + i;
				}else{
					str += '<td' + style + ' align="center" >'				
					str += '<a href="javascript:'+this.name+'.returndate(' + actVal + ');">' + i + '</a>';
				}
				str += '</td>';				
				dayInWeek++;
				
			}
			for (i = dayInWeek; i < 7; i++)
				str += '<td>&nbsp;</td>';
		
			str += '</tr></table>';
		

			this.obj.innerHTML = str;
		};

 	this.returndate =
		// Returns date from calendar.
		// @param   string     date text
		function(y,m,d){
			this.yearfield.value = y
			this.monthfield.value = m
			this.dayfield.value = d
			SetDayOfWeekGroupLocal(this.dateSet)
			this.hide();
		};

	return true;
}

