function InjectionCalendar(dateField, monthField, yearField, calcBut, table) {
	this.dateF = typeof dateField == 'string' ? document.getElementById(dateField) : dateField;
	this.monthF = typeof monthField == 'string' ? document.getElementById(monthField) : monthField;
	this.yearF = typeof yearField == 'string' ? document.getElementById(yearField) : yearField;
	this.calc = typeof calcBut == 'string' ? document.getElementById(calcBut) : calcBut;
	this.table = typeof table == 'string' ? document.getElementById(table) : table;
	this.months = ['Января', 'Февраля', 'Марта', 'Апреля', 'Мая', 'Июня', 'Июля', 'Августа', 'Сентября', 'Октября', 'Ноября', 'Декабря'];
	this.millisec = 24 * 60 * 60 * 1000;
}

InjectionCalendar.prototype.init = function() {
	this.setupListeners();
}

InjectionCalendar.prototype.setupListeners = function() {
	var prnt = this;
	
	this.monthF.onchange = function() {
		var m = parseInt(this.value);
		var y = parseInt(prnt.yearF.value);
		
		prnt.setDate(m, y);
	}
	
	this.yearF.onchange = function() {
		var m = parseInt(prnt.monthF.value);
		var y = parseInt(this.value);
		
		prnt.setDate(m, y);
	}
	
	this.calc.onclick = function() {
		var d = parseInt(prnt.dateF.value);
		var m = parseInt(prnt.monthF.value);
		var y = parseInt(prnt.yearF.value);
		
		var startDate = prnt.getMillisec(d, m, y);
		var unique = prnt.makeUniqueArray(startDate, d, m, y);
		
		prnt.insertInTable(unique);
	}
}

InjectionCalendar.prototype.insertInTable = function(arr) {
	arr = typeof arr == 'string' ? arr.split(',') : arr;
	
	var rows = this.table.rows;
	
	for (var i = 1; i < rows.length; i++) {
		rows[i].cells[0].getElementsByTagName('b')[0].innerHTML = arr[i - 1];
	}
}

InjectionCalendar.prototype.makeUniqueArray = function(start, d, m, y) {
	var strArr = new Array();
	
	strArr[0] = this.getDate(start).d + ' ' + this.months[this.getDate(start).m] + ' ' + this.getDate(start).y;
	strArr[1] = 'с ' + this.getDate(start + this.getDayShift(2)).d + ' ' + this.months[this.getDate(start + this.getDayShift(2)).m] + ' ' + this.getDate(start + this.getDayShift(2)).y + '<br />по ' +  this.getDate(start + this.getDayShift(6)).d + ' ' + this.months[this.getDate(start + this.getDayShift(6)).m] + ' ' + this.getDate(start + this.getDayShift(6)).y;
	strArr[2] = this.getDate(start + this.getMonthShift(m, y, 1)).d + ' ' + this.months[this.getDate(start + this.getMonthShift(m, y, 1)).m] + ' ' + this.getDate(start + this.getMonthShift(m, y, 1)).y;
	strArr[3] = this.getDate(start + this.getMonthShift(m, y, 2)).d + ' ' + this.months[this.getDate(start + this.getMonthShift(m, y, 2)).m] + ' ' + this.getDate(start + this.getMonthShift(m, y, 2)).y;
	strArr[4] = this.getDate(start + this.getMonthShift(m, y, 3)).d + ' ' + this.months[this.getDate(start + this.getMonthShift(m, y, 3)).m] + ' ' + this.getDate(start + this.getMonthShift(m, y, 3)).y;
	strArr[5] = this.getDate(start + this.getMonthShift(m, y, 4.5)).d + ' ' + this.months[this.getDate(start + this.getMonthShift(m, y, 4.5)).m] + ' ' + this.getDate(start + this.getMonthShift(m, y, 4.5)).y;
	strArr[6] = this.getDate(start + this.getMonthShift(m, y, 6)).d + ' ' + this.months[this.getDate(start + this.getMonthShift(m, y, 6)).m] + ' ' + this.getDate(start + this.getMonthShift(m, y, 6)).y;
	strArr[7] = this.getDate(start + this.getMonthShift(m, y, 12)).d + ' ' + this.months[this.getDate(start+ this.getMonthShift(m, y, 12)).m] + ' ' + this.getDate(start + this.getMonthShift(m, y, 12)).y;
	strArr[8] = this.getDate(start + this.getMonthShift(m, y, 18)).d + ' ' + this.months[this.getDate(start + this.getMonthShift(m, y, 18)).m] + ' ' + this.getDate(start + this.getMonthShift(m, y, 18)).y;
	strArr[9] = this.getDate(start + this.getMonthShift(m, y, 20)).d + ' ' + this.months[this.getDate(start + this.getMonthShift(m, y, 20)).m] + ' ' + this.getDate(start + this.getMonthShift(m, y, 20)).y;
	strArr[10] = this.getDate(start + this.getYearShift(6)).d + ' ' + this.months[this.getDate(start+ this.getYearShift(6)).m] + ' ' + this.getDate(start + this.getYearShift(6)).y;
	strArr[11] = this.getDate(start + this.getYearShift(7)).d + ' ' + this.months[this.getDate(start + this.getYearShift(7)).m] + ' ' + this.getDate(start + this.getYearShift(7)).y;
	strArr[12] = this.getDate(start + this.getYearShift(14)).d + ' ' + this.months[this.getDate(start + this.getYearShift(14)).m] + ' ' + this.getDate(start + this.getYearShift(14)).y;
	strArr[13] = 'Взрослые от 18 лет';
	
	return strArr;
}

InjectionCalendar.prototype.getDate = function(mil) {
	var date = new Date(mil);
	return {d: date.getDate(), m: date.getMonth(), y: date.getFullYear()}
}

InjectionCalendar.prototype.getMillisec = function(d, m, y) {
	var date = new Date(y, m, d);
	return date.getTime();
}

InjectionCalendar.prototype.getDayShift = function(numOfDays) {
	numOfDays = numOfDays * this.millisec;
	return numOfDays;
}

InjectionCalendar.prototype.getMonthShift = function(m, y, numOfMonths) {
	var days = 0;
	var total = 0;
	var left = Math.floor(numOfMonths);
	var right = numOfMonths - Math.floor(numOfMonths);
	
	for (var i = 0; i < left; i++) {
		if (m > 11) {
			m = 0;
			y++
		}
		
		days = new Date(y, m + 1, 0).getDate();
		total += days;
		m++;
	}
	
	if (right > 0) {
		if (m > 11) {
			m = 0;
			y++
		}
		
		days = new Date(y, m + 1, 0).getDate();
		days = Math.round(days * right);
		total += days;
	}
	
	return total * this.millisec;
}

InjectionCalendar.prototype.getYearShift = function(numOfYears) {
	numOfYears = numOfYears * 365 * this.millisec;
	return numOfYears;
}

InjectionCalendar.prototype.setDate = function(m, y) {
	var daysInMonth = new Date(y, m + 1, 0).getDate();
	
	this.dateF.length = 0;
	
	for (var i = 0; i < daysInMonth; i++) {
		this.dateF.options[i] = new Option(i + 1, i + 1);
	}
}
