
	function get_browser()
	{
		if(navigator.appName.indexOf( "Netscape" ) != -1) 
		{
			// is netscape
			if(navigator.userAgent.indexOf( "Netscape/7" ) != -1) 
			{
				return "NS7";
			}
			else if (navigator.userAgent.indexOf( "Safari" ) != -1) 
			{
				return "Safari";
			}
			else
			{
				// default to w3c standard if unknown
				return "NS7";
			}
		}
		else
		{
			// is IE
			return "IE";
		}
	}
	
	function show_cal(e, target, x, y)
	{	
		if (typeof target != "undefined" && target.value != '')
		{
			this.form_value = target.value;
			
			re = /([0-9]{2}).([0-9]{2}).([0-9]{4})/;
			matches = target.value.match(re);
			
			if (matches)
			{
				date = new Date(matches[3], matches[2], matches[1]);
	
				if (this.current_time != date)
				{
					refill_cal(matches[3], matches[2]-1, false);
					this.current_time = date;
				}
			}
		}

		if (!this.calendar_on) {
			if(browser=="IE")
			{
	   			obj = document.all['kcal'];

				if (typeof x == 'undefined' || x == -1)
		   			tempX = e.clientX + document.body.scrollLeft;
				else 
					tempX = x;
				if (typeof y == 'undefined' || y == -1)
					tempY = e.clientY + document.body.scrollTop;
				else 
					tempY = y;
					
				tempX+=20;

				/* Determine if we are too close to border X and adjust according */
				re = /[^0-9]/gi;
				offset = obj.style.width.toString().replace(re,'');
				borderDistanceX = document.body.clientWidth-e.clientX-offset;
				if (borderDistanceX < 0) tempX = tempX + borderDistanceX - 10;

				
				/* Only move calendar if it's hidden */
				if (obj.style.visibility == 'hidden') {
					obj.style.top = tempY+this.offsetY;
					obj.style.left = tempX+this.offsetX;
				}
				obj.style.visibility = "visible";
			}
			else if (browser=="NS")
			{
				document.layers['kcal'].visibility = "show";
			}
			else if (browser=="NS7")
			{
				obj = document.getElementById('kcal');
				
				tempX = e.pageX;
				tempY = e.pageY;

				/* Determine if we are too close to border X and adjust according */
				re = /[^0-9]/gi;
				offset = obj.style.width.toString().replace(re,'');
				borderDistanceX = self.innerWidth-tempX-offset;
				if (borderDistanceX < 0) tempX = tempX + borderDistanceX - 10;
				
				/* Only move calendar if it's hidden */
				if (obj.style.visibility == 'hidden') {
					obj.style.top = tempY+this.offsetY;
					obj.style.left = tempX+this.offsetX;
				}
				obj.style.visibility = "visible";
			}
			
			if (target) this.target = target;
		}
		this.calendar_on = 1;
	}
	
	function hide_cal()
	{
		this.calendar_on = 0;
		setTimeout('chk_to_hide();',500);
	}
	
	function chk_to_hide()
	{
		if(!this.calendar_on)
		{
			force_hide();
		}
	}
	
	function force_hide() {
			if(browser=="IE")
			{
				document.all['kcal'].style.visibility = "hidden";
			}
			else if(browser=="NS")
			{
				document.layers['kcal'].visibility = "hide";
			}
			else if(browser=="NS7")
			{
				document.getElementById('kcal').style.visibility = "hidden";
			}
	}	
	function first_day_of_month(this_year, this_month)
	{
		this_date = new Date(this_year, this_month,1,0,0,0);
		return this_date.getDay();
	}
	// returns total days in month
	function days_in_month(this_year, this_month)
	{
		var days = 31;
		if (this_month == 3 || this_month == 5 || this_month == 8 || this_month == 10) days = 30;
		if (this_month == 1 && (this_year/4) != Math.floor(this_year/4)) days = 28;
		if (this_month == 1 && (this_year/4) == Math.floor(this_year/4)) days = 29;
		
		return days;
	}

	// month_as_string requires id of month returned by getMonth() (0 for january, 11 for december)
	function month_as_string(month_id)
	{
		switch(month_id + 1)
		{
			case 1:
				month_name = "January";
				break;
			case 2:
				month_name = "February";
				break;
			case 3:
				month_name = "March";
				break;
			case 4:
				month_name = "April";
				break;
			case 5:
				month_name = "May";
				break;
			case 6:
				month_name = "June";
				break;
			case 7:
				month_name = "July";
				break;
			case 8:
				month_name = "August";
				break;
			case 9:
				month_name = "September";
				break;
			case 10:
				month_name = "October";
				break;
			case 11:
				month_name = "November";
				break;
			case 12:
				month_name = "December";
		}
		return month_name;
	}
	
	// this function builts the innerHtml of the calendar to display
	function build_calendar(int_year, int_month)
	{
		this.calendar_on = 0;
		
		current_col = 1;
		current_date = 1;
		current_cell = 1;
		
		// calculate next and previous months and years
		if(int_month == 0)
		{
			prv_month = 11;
			prv_year = int_year - 1;
			nxt_month = 1;
			nxt_year = int_year;
		}
		else if(int_month == 11)
		{
			prv_month = 10;
			prv_year = int_year;
			nxt_month = 0;
			nxt_year = int_year + 1;
		}
		else
		{
			prv_month = int_month - 1;
			prv_year = int_year;
			nxt_month = int_month + 1;
			nxt_year = int_year;
		}
		
		// set nxt and prv lnks
		nxt_lnk = 'refill_cal(' + nxt_year + ', ' + nxt_month + ');';
		prv_lnk = 'refill_cal(' + prv_year + ', ' + prv_month + ');';
		
		// discover blank_cell count (to appear as padding in tbl before first_day_of_month
		blank_cells = first_day_of_month(int_year, int_month);

		// header with month
		html = '<table width="100%" border="0" cellspacing="0" cellpadding="0" class="calendarborder"><tr><td>';
		html += '<table width="100%" border="0" cellspacing="0" cellpadding="3" class="calendarhead">';
		html += '<tr>';
		html += '<td nowrap class="calheader" width="1%"><a href="#" onclick="' + prv_lnk + '" class="calendar">&lt;&lt;</a></td>';
		html += '<td align="center" class="calheader" nowrap width="98%"><b>';
		html += month_as_string(int_month) + ' ' + int_year;
		html += '</b></td>';
		html += '<td nowrap class="calheader" width="1%"><a href="#" onclick="' + nxt_lnk + '" class="calendar">&gt;&gt;</a></td>';
		html += '</tr></table>';
		
		// day headers (sun - sat)
		// and begin main calendar tbl structure
		html += '<table width="100%" border="0" cellspacing="0" cellpadding="3">'
		html += '<tr><td class="calweeks" align="center">S</td><td class="calweeks" align="center">M</td><td class="calweeks" align="center">T</td><td class="calweeks" align="center">W</td><td class="calweeks" align="center">T</td><td class="calweeks" align="center">F</td><td class="calweeks" align="center">S</td></tr>';
		
		// loop thru building empty cells and inserting dates where applicable
		month_days = days_in_month(int_year, int_month);
		while(current_date <= month_days)
		{
			for(j=1;j<=7;j++)
			{
				if(j==1){html += '<tr>';}
				if(current_cell > blank_cells)
				{
					if(current_date <= month_days)
					{
						// get date in format dd-mm-yyyy
						str_day = current_date.toString();

						// to account of js' month calc + 1
						str_month = (int_month*1) + 1;
						str_month = str_month.toString();
						str_year = int_year.toString();
						
						if(str_month.length == 1)
						{
							str_month = '0' + str_month;
						}
						if(str_day.length == 1)
						{
							str_day = '0' + str_day;
						}
						
						str_date = str_day + '.' + str_month + '.' + str_year;
						date_lnk = '<a href="#" onclick="fill_form(\''+ str_date + '\');force_hide();" class="calday">' + current_date + '</a>';
						
						
						html += '<td width="14%" valign="top" align="center"';
						if (str_date == this.form_value) html += ' class="calday_selected"';
						if(j==1||j==7) {html+=' bgcolor="#EEEEEE" ';}
						else {html += ' bgcolor="#FFFFFF" ';} 
						html += '>' + date_lnk + '</td>';
						current_date++;	
					}
					else
					{
						html += '<td width="14%" valign="top"';
						if(j==1||j==7) {html+=' bgcolor="#EEEEEE" ';}
						else {html += ' bgcolor="#FFFFFF" ';} 
						html += '>&nbsp;</td>';
					}
				}
				else
				{
					html += '<td width="14%" valign="top"';
					if(j==1||j==7) {html+=' bgcolor="#EEEEEE" ';}
					else {html += ' bgcolor="#FFFFFF" ';} 
					html += '>&nbsp;</td>';
				}
				
				current_cell++;
				if(j==7){html += '</tr>';}
			}
		}
		
		html += '</table></td></tr></table>';
		
		return html;
	}
	function refill_cal(int_year, int_month, showcal)
	{
		if (isNaN(int_year) || isNaN(int_month)) return false;
		
		if(browser=="IE")
		{
			document.all['kcal'].innerHTML = build_calendar(int_year, int_month);
		}
		else if(browser=="NS7")
		{
			document.getElementById('kcal').innerHTML = build_calendar(int_year, int_month);
		}
		
		if (typeof showcal == 'undefined') showcal = true;
		
		if (showcal)
			this.calendar_on = 1;
	}
	
	function fill_form(str_val)
	{
		this.target.value = str_val;
	}


	// init 
	offsetX = 5;
	offsetY = 10;
	browser = get_browser();
	current_time = new Date();
	int_year = current_time.getYear().toString();
	int_year = '20' + int_year.substring(int_year.length - 2, int_year.length);
	int_month = current_time.getMonth();
	form_value = '';
		
	if(browser=="NS7" || browser=="IE" || browser=="Safari")
	{
		// div code
		outp = '';
		outp += '<div id="kcal" style="width:150px; position:absolute; visibility:hidden; z-index:99;" onmouseover="show_cal(event);" onmouseout="hide_cal();">';
		outp += build_calendar(Number(int_year), int_month);
		outp += '</div>';
	}
	else
	{
		// layer code
		outp = '';
		outp += '<layer height="25px" width="150px" id="kcal" left="0px" top="0px" visibility="hide">';
		outp += build_calendar(Number(int_year), int_month);
		outp += '</layer>';
	}
	document.write(outp);
	
	this.calendar_init = true;
