function TRCalendar(id,months,weekdays,href,year_start,year_end,selectDate) 
{ this._construct(id,months,weekdays,href,year_start,year_end,selectDate) }
(function() {
    TRCalendar.prototype = {

        // Internals.
        _id:			null,
        _months:		null,
		_weekdays:		null,
        _date:			new Date,
        _selectDate:	null,
        _year_start:	null,
        _year_end:		null,
		_href:			null,
        _err_msgs:		null,
        _errors:		null,
        _internal_errs:	null,
        _hidden_field:  null,
        //_highLightDate: null,    
        

        // Constructor.
        _construct: function(id,months,weekdays,href,year_start,year_end,selectDate) {
			this._id = id;
			this._months = months;
			this._weekdays = weekdays;
			this._href = href;
    	    this._year_start = year_start;
	        this._year_end = year_end;
	    	this.setStartDate(selectDate);
	    	//test
	    	this.cur = null;
	    	//this._highLightDate = highLightDate;   		    	
        },

        _isLeapYear: function(year) {
			return ((year % 4) == 0) && (((year % 100) != 0) || ((year % 400) == 0));
        },

        _optionAdd: function(_objSelect,_text,_value, _selected) {
            var _option = document.createElement("OPTION");
    	    _option.value = _value;
            _option.innerHTML = _text;
			_option.selected = (_value == _selected)?true:false;
	    	_objSelect.appendChild(_option);
        },

        _daysPerMonth: function(year, month) {
		   days_in_month = new Array (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
		   if (month == -1) month = 11;
		   days = days_in_month[month];
		   if ((month == 1) && this._isLeapYear(year)) {days++}
		   return days;
        },

        setStartDate: function(date) {
			testdate = date;
			//alert("date = "+date);
			var d = Date.parse(date);
			//alert("d = "+d);
			if(!isNaN(d)) {
				this._selectDate = new Date(d);
				//alert("this._selectDate = ",this._selectDate);
				
				this._date = this._selectDate;
				//alert("this._date = "+this._date);
			}
        },

		print: function() {
			//alert("print "+selectDate);
		    document.write("<div id=\""+this._id+"_calendar\" class=\"trcalendar\">");
			var clr = document.getElementById(this._id+"_calendar");
			var new_el = document.createElement("DIV");
			new_el.id = this._id+"_control";
			new_el.className = "control";
			clr.appendChild(new_el);
			var control = document.getElementById(this._id+"_control");

			new_el = document.createElement("SELECT");
			new_el.id = this._id+"_months";
			new_el.name = this._id+"_months";
			new_el.className = "clrsel";
			new_el.style.marginRight = "5";
			var _this = this;
			
			new_el.onchange = function(){return _this.changeMonth()};

		    for (i=0;i<this._months.length;i++) {
				this._optionAdd(new_el,this._months[i],i,this._date.getMonth());
			}

			control.appendChild(new_el);

			new_el = document.createElement("SELECT");
			new_el.id = this._id+"_years";
			new_el.name = this._id+"_years";
			new_el.className = "clrsel_invisible";
			new_el.onchange = function(){return _this.changeYear()};

		    for (i=this._year_start;i<=this._year_end;i++)
				this._optionAdd(new_el,i,i,this._date.getFullYear());

			control.appendChild(new_el);

			new_el = document.createElement("DIV");
			new_el.id = this._id+"_weekdays";
			new_el.className = "weekdays";

	    	var alink;
		    for (i=0;i<this._weekdays.length;i++) {
		    	alink = document.createElement("A");
				alink.innerHTML = this._weekdays[i]['short'];
				alink.alt = this._weekdays[i]['day'];
				new_el.appendChild(alink);
			}
			clr.appendChild(new_el);

			this._printDays(clr);

		    document.write("</div>");
		},

        _printDays: function(clr) {
			//var hidden_date = this._hidden_date;
			
			var _this = this;
        	var new_el = document.createElement("DIV");
			new_el.id = this._id+"_monthdays";
			new_el.className = "monthdays";

			var _monthDay = this._daysPerMonth(this._date.getFullYear(),this._date.getMonth());
			var _d = new Date(this._date.getFullYear(),this._date.getMonth(),1);
			var _dayWeek = _d.getDay();
			if(_dayWeek == 0) {
				_dayWeek = 7;
			}

			var _days = _monthDay + (_dayWeek - 1);

			var _day = 1;
			for(i=0;i<_days;i++) {
				var a = document.createElement("A");
				if(_dayWeek <= (i+1) && _monthDay >= _day) {
					var _years = document.getElementById(this._id+"_years");
					var _months = document.getElementById(this._id+"_months");
					var _today = new Date;
					if(this._selectDate && (this._selectDate.getFullYear() == _years[_years.selectedIndex].value && this._selectDate.getMonth() == _months[_months.selectedIndex].value && this._selectDate.getDate() == _day)) {
						a.className="sel";
						//a.className="today";
					} else if (_years[_years.selectedIndex].value == _today.getFullYear() && _months[_months.selectedIndex].value == _today.getMonth() && _day == _today.getDate()) {
						a.className="today";
						//a.className="sel";
					}
					
					
//					var hidden_date = this._date.getFullYear()+"-"+(this._date.getMonth()+1)+"-"+_day;
					var hidden_date = (this._date.getMonth()+1);
					a.href=this._href+hidden_date;
					// TEST
		
		//  реалізує підсвітку дати , яка передана через GET  			
					
				if (hidden_date == testdate ) {
					//alert("good hidden_date = "+hidden_date+" testdate = "+testdate)
					a.className="sel";
					if (this.cur) this.cur.className = '';	
					this.cur = a;
					
					
					} 
						else {
						//alert("bad hidden_date = "+hidden_date+" testdate = "+testdate)
						}
					
					
					if ( this._hidden_field != null) {
						a.hidden_date = hidden_date;
						//alert(this._hidden_field);
						var hidden_field = this._hidden_field;
						//var hidden_date = this._href+this._date.getFullYear()+"-"+(this._date.getMonth()+1)+"-"+_day;
						
						//if (this._id == 'chose_calend_1') { alert(hidden_date)}
						a.onclick = function() {return _this.setHiddenDate(this,hidden_field);
						//alert(hidden_date);
						}	
					}
					//a.href = "javascript:void(0)";
					//a.onclick = function() {return setHiddenDate(hidden_date);}

					a.innerHTML=_day;
					_day++;
				}
				new_el.appendChild(a);
			}
			_dot = document.createElement("img");
			_dot.src="imgs/dot.gif";
			new_el.appendChild(_dot);
			clr.appendChild(new_el);
        },

        changeYear: function(id) {
        	var _years = document.getElementById(this._id+"_years");
			var _monthdays = document.getElementById(this._id+"_monthdays");
			var _d = new Date(_years[_years.selectedIndex].value,this._date.getMonth(),this._date.getDate());
			this._date = _d;
			_parentObj = _monthdays.parentNode;
			_parentObj.removeChild(_monthdays);
			this._printDays(_parentObj);
        },

        changeMonth: function() {
			var _months = document.getElementById(this._id+"_months");
			var _monthdays = document.getElementById(this._id+"_monthdays");
			var _d = new Date(this._date.getFullYear(),_months[_months.selectedIndex].value,this._date.getDate());
			this._date = _d;
			_parentObj = _monthdays.parentNode;
			_parentObj.removeChild(_monthdays);
			this._printDays(_parentObj);
        },
        
 // встановлюються приховані поля
        setHiddenField: function(field) {
        	
        	this._hidden_field = field;
        	//alert('function: setHiddenField = '+this._hidden_field);	
        	       	
        },
        
		setHiddenDate: function (a,field)
			//function setHiddenDate (a) 
		{
			
				var hidden_date = a.hidden_date;
				a.className = 'sel';
				if (this.cur) this.cur.className = '';
				//alert('function: setHiddenDate = '+hidden_date);
				document.getElementById(field).value = hidden_date;
				//alert(document.getElementById(field).value);
				this.cur = a;
				
				return false;
				
			/* 
			на початку встановлюю: this.cur = null;  - це конкретний нажатий в даний момент елемент (цифра в календарі), початково - він null
			a.className = 'sel';  стиль об"єкту а встановлюю 'sel', а потім каренту присвоюю об"єкт а
			
			*/	
			},
			
		showHiddenDate: function	(field)
		{
			dt = document.getElementById(field);
//			alert(dt);
		}
			        
			        
}
				
	
})();

// при клікані на дату в календарі - встановлюється додаткове приховане поле
/*
function setHiddenDate (a) {

	var hidden_date = a.hidden_date;
	alert('function: setHiddenDate = '+hidden_date);
	
	
	return false;
}
*/