/* DHTML_Calendar.js $Revision: 1.1.1.1 $ ($Date: 2005/05/06 07:32:52 $)
* Copyright 2005 Eric Pretorious (eric@pretorious.net)
* Distributed under the terms of the GNU General Public License (http://www.gnu.org/copyleft/gpl.html)
* 
*/


var now;
var currYear,currMonth;
var i,j,currDateObj,firstDay,daysInMonth,sevenDayRows;
var pDHTMLC,pprev,pcurr,pnext;
var DHTMLC,prev,curr,next;
var months	= new Array('Styczeń','Luty','Marzec','Kwiecień','Maj','Czerwiec','Lipiec','Sierpień','Wrzesień','Październik','Listopad','Grudzień');
var days	= new Array('Niedziela','Poniedziałek','Wtorek','Środa','Czwarek','Piątek','Sobota');
var currdatestart = 'mon1';
var currdateend = 'pmon1';
var checkbill = 0;

/*
1. Set currMonth & currYear to now.
2. CBE will automatically call windowOnLoad() which, in turn, will...
 a. Create the CrossBrowserElement objects.
 b. Call Calendar() which will...
  i. Calculate the calendar info: firstDay, daysInMonth, sevenDayRows.
  ii. Determine nextMonth, nextYear, prevMonth, & prevYear.
  iii. Call cbeTraverseTree() with a reference to drawCalendar() which will...
   * Populate the innerHTML for each object in the CBE Object Tree.
*/



// Use the present date to set the global variables (used to generate the calender)
now		= new Date();
currYear	= now.getYear();
currMonth	= now.getMonth();

// Some browsers may not be Y2K compliant...
if (currYear < 1900) currYear += 1900;

function Calendar(Month,Year,pref) {
	currDateObj	= new Date(Year,Month,1);

	// Update the global variables (used to generate the calender)
	currMonth	= Month;
	currYear	= Year;
	
	// Perform the basic calculations necessary to generate a calendar
	firstDay	= currDateObj.getDay();	// The day-of-the-week of the first day of the month;
	if (firstDay != 0) firstDay = firstDay-1;
	else firstDay= 6;
	daysInMonth	= 32 - new Date(Year,Month,32).getDate();
	sevenDayRows	= Math.ceil((firstDay + daysInMonth) / 7);
	
	switch (Month) {
	case 0:
		nextMonth	= Month + 1;
		nextYear	= Year;
		prevMonth	= 11;
		prevYear	= Year - 1;
		break;
	
	case 11:
		nextMonth	= 0;
		nextYear	= Year + 1;
		prevMonth	= Month - 1;
		prevYear	= Year;
		break;
	
	default:
		nextMonth	= Month + 1;
		nextYear	= Year;
		prevMonth	= Month - 1;
		prevYear	= Year;
		break;
	}
	
	i = 0; j = 1;
	cbeTraverseTree('preorder',DHTMLC,drawCalendar);
  
  
	DHTMLC.resizeTo(178, sevenDayRows * 20 + 48);
	prev.innerHtml('<a href="javascript:Calendar(prevMonth,prevYear);">' + months[prevMonth].substr(0,3) + '</a>');
	curr.innerHtml(months[Month] + ', ' + Year);
	next.innerHtml('<a href="javascript:Calendar(nextMonth,nextYear);">' + months[nextMonth].substr(0,3) + '</a>');
	
}

function drawCalendar(node,level,branch) {
	var HREF;
	if (level == 2 && branch > 2) {
		if (i >= firstDay && j <= daysInMonth) {
			cbeGetElementById(node.id).className = 'days';
			if ( (currYear + '-' +(currMonth + 1) + '-' + j) == document.forms[0].elements['date_start'].value) cbeGetElementById(node.id).className = 'daysb';
      HREF = 'javascript:void(cbeGetElementById(currdatestart).className = \'days\');void(cbeGetElementById(\''+node.id+'\').className = \'daysb\');void(currdatestart=\''+node.id+'\');void(document.forms[0].elements[\'date_start\'].value=\'' + currYear + '-' +(currMonth + 1) + '-' + j + '\');';
			node.innerHtml('<a href="' + HREF + '">' + j++ + '</a>');
			node.innerHtml();node.show();
			
		} else {
			node.hide();
		}
		i++;
	}
	return true;
}


function pCalendar(Month,Year) {
	currDateObj	= new Date(Year,Month,1);

	// Update the global variables (used to generate the calender)
	currMonth	= Month;
	currYear	= Year;
	
	// Perform the basic calculations necessary to generate a calendar
	firstDay	= currDateObj.getDay();	// The day-of-the-week of the first day of the month;
	if (firstDay != 0) firstDay = firstDay-1;
	else firstDay= 6;
  daysInMonth	= 32 - new Date(Year,Month,32).getDate();
	sevenDayRows	= Math.ceil((firstDay + daysInMonth) / 7);
	
	switch (Month) {
	case 0:
		nextMonth	= Month + 1;
		nextYear	= Year;
		prevMonth	= 11;
		prevYear	= Year - 1;
		break;
	
	case 11:
		nextMonth	= 0;
		nextYear	= Year + 1;
		prevMonth	= Month - 1;
		prevYear	= Year;
		break;
	
	default:
		nextMonth	= Month + 1;
		nextYear	= Year;
		prevMonth	= Month - 1;
		prevYear	= Year;
		break;
	}
	
	i = 0; j = 1;
	cbeTraverseTree('preorder',pDHTMLC,pdrawCalendar);
    
	pDHTMLC.resizeTo(178, sevenDayRows * 20 + 48);
	pprev.innerHtml('<a href="javascript:pCalendar(prevMonth,prevYear);">' + months[prevMonth].substr(0,3) + '</a>');
	pcurr.innerHtml(months[Month] + ', ' + Year);
	pnext.innerHtml('<a href="javascript:pCalendar(nextMonth,nextYear);">' + months[nextMonth].substr(0,3) + '</a>');

}

function pdrawCalendar(node,level,branch) {
	var HREF;
	if (level == 2 && branch > 2) {
		if (i >= firstDay && j <= daysInMonth) {
			cbeGetElementById(node.id).className = 'days';
			if ( (currYear + '-' +(currMonth + 1) + '-' + j) == document.forms[0].elements['date_end'].value) cbeGetElementById(node.id).className = 'daysb';
      HREF = 'javascript:void(cbeGetElementById(currdateend).className = \'days\');void(cbeGetElementById(\''+node.id+'\').className = \'daysb\');void(currdateend=\''+node.id+'\');void(document.forms[0].elements[\'date_end\'].value=\'' + currYear + '-' +(currMonth + 1) + '-' + (j-1) + '\');';
			node.innerHtml('<a href="' + HREF + '">' + j++ + '</a>');
			node.innerHtml();node.show();
		} else {
			node.hide();
		}
		i++;
	}
	return true;
}


function windowOnload() {
  DHTMLC	= cbeGetElementById('DHTMLC').cbe;
	prev	= cbeGetElementById('prev').cbe;
	curr	= cbeGetElementById('curr').cbe;
	next	= cbeGetElementById('next').cbe;
  Calendar(currMonth,currYear);
	DHTMLC.clip('auto');
	DHTMLC.show();
    
  pDHTMLC	= cbeGetElementById('pDHTMLC').cbe;
	pprev	= cbeGetElementById('pprev').cbe;
	pcurr	= cbeGetElementById('pcurr').cbe;
	pnext	= cbeGetElementById('pnext').cbe;
  pCalendar(currMonth,currYear);
	pDHTMLC.clip('auto');
	pDHTMLC.show();
	
	
}

