function BirthDate(dateField, monthField, yearField, calcBut, table, txtField) {
	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.txtField = typeof txtField == 'string' ? document.getElementById(txtField) : txtField;
	this.dayWords = ['день', 'дня', 'дней'];
	this.weekWords = ['неделю', 'недели', 'недель'];
	
	this.injection = new InjectionCalendar(this.dateF, this.monthF, this.yearF, this.calc, this.table);
}

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

BirthDate.prototype.setupListeners = function() {
	var prnt = this;
	
	this.monthF.onchange = function() {
		var m = parseInt(this.value);
		var y = parseInt(prnt.yearF.value);
		
		prnt.injection.setDate(m, y);
	}
	
	this.yearF.onchange = function() {
		var m = parseInt(prnt.monthF.value);
		var y = parseInt(this.value);
		
		prnt.injection.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.injection.getMillisec(d, m, y);
		var pregnantDate = prnt.injection.getMillisec(prnt.injection.getDate(startDate + prnt.injection.getDayShift(14)).d, prnt.injection.getDate(startDate + prnt.injection.getDayShift(14)).m, prnt.injection.getDate(startDate + prnt.injection.getDayShift(14)).y);
		var today = prnt.injection.getMillisec(prnt.getToday().d, prnt.getToday().m, prnt.getToday().y);
		var unique = prnt.makeUniqueArray(startDate, d, m, y);
		
		var numW = prnt.getWeek(prnt.deltaInDays(today, pregnantDate)).w;
		var numD = prnt.getWeek(prnt.deltaInDays(today, pregnantDate)).d;
		
		var wText = '';
		
		if (numW < 42) {
			wText = 'Вы беременны ' + numW + ' ' + prnt.weekWords[prnt.getWord(numW)] + ' и ' + numD + ' ' + prnt.dayWords[prnt.getWord(numD)];
		} else {
			wText = 'Скорее всего, вы уже родили';
		}
		
		prnt.txtField.innerHTML = wText;
		prnt.insertInTable(unique);
	}
}

BirthDate.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[1].getElementsByTagName('b')[0].innerHTML = arr[i - 1];
	}
}

BirthDate.prototype.makeUniqueArray = function(start, d, m, y) {
	var strArr = new Array();
	
	strArr[0] = this.injection.getDate(start + this.injection.getDayShift(14)).d + ' ' + this.injection.months[this.injection.getDate(start + this.injection.getDayShift(14)).m] + ' ' + this.injection.getDate(start + this.injection.getDayShift(14)).y;
	strArr[1] = this.injection.getDate(start + this.injection.getDayShift(40 * 7)).d + ' ' + this.injection.months[this.injection.getDate(start + this.injection.getDayShift(40 * 7)).m] + ' ' + this.injection.getDate(start + this.injection.getDayShift(40 * 7)).y;
	strArr[2] = this.injection.getDate(start + this.injection.getDayShift(5 * 7)).d + ' ' + this.injection.months[this.injection.getDate(start + this.injection.getDayShift(5 * 7)).m] + ' ' + this.injection.getDate(start + this.injection.getDayShift(5 * 7)).y;
	strArr[3] = this.injection.getDate(start + this.injection.getDayShift(5 * 7)).d + ' ' + this.injection.months[this.injection.getDate(start + this.injection.getDayShift(5 * 7)).m] + ' ' + this.injection.getDate(start + this.injection.getDayShift(5 * 7)).y;
	strArr[4] = this.injection.getDate(start + this.injection.getDayShift(10 * 7)).d + ' ' + this.injection.months[this.injection.getDate(start + this.injection.getDayShift(10 * 7)).m] + ' ' + this.injection.getDate(start + this.injection.getDayShift(10 * 7)).y;
	strArr[5] = this.injection.getDate(start + this.injection.getDayShift(12 * 7)).d + ' ' + this.injection.months[this.injection.getDate(start + this.injection.getDayShift(12 * 7)).m] + ' ' + this.injection.getDate(start + this.injection.getDayShift(12 * 7)).y;
	strArr[6] = 'с ' + this.injection.getDate(start + this.injection.getDayShift(10 * 7)).d + ' ' + this.injection.months[this.injection.getDate(start + this.injection.getDayShift(10 * 7)).m] + ' ' + this.injection.getDate(start + this.injection.getDayShift(10 * 7)).y + '<br />по ' + this.injection.getDate(start + this.injection.getDayShift(13 * 7)).d + ' ' + this.injection.months[this.injection.getDate(start + this.injection.getDayShift(13 * 7)).m] + ' ' + this.injection.getDate(start + this.injection.getDayShift(13 * 7)).y;
	strArr[7] = 'с ' + this.injection.getDate(start + this.injection.getDayShift(16 * 7)).d + ' ' + this.injection.months[this.injection.getDate(start + this.injection.getDayShift(16 * 7)).m] + ' ' + this.injection.getDate(start + this.injection.getDayShift(16 * 7)).y + '<br />по ' + this.injection.getDate(start + this.injection.getDayShift(18 * 7)).d + ' ' + this.injection.months[this.injection.getDate(start + this.injection.getDayShift(18 * 7)).m] + ' ' + this.injection.getDate(start + this.injection.getDayShift(18 * 7)).y;
	strArr[8] = 'с ' + this.injection.getDate(start + this.injection.getDayShift(14 * 7)).d + ' ' + this.injection.months[this.injection.getDate(start + this.injection.getDayShift(14 * 7)).m] + ' ' + this.injection.getDate(start + this.injection.getDayShift(14 * 7)).y + '<br />по ' + this.injection.getDate(start + this.injection.getDayShift(20 * 7)).d + ' ' + this.injection.months[this.injection.getDate(start + this.injection.getDayShift(20 * 7)).m] + ' ' + this.injection.getDate(start + this.injection.getDayShift(20 * 7)).y;
	strArr[9] = this.injection.getDate(start + this.injection.getDayShift(18 * 7)).d + ' ' + this.injection.months[this.injection.getDate(start + this.injection.getDayShift(18 * 7)).m] + ' ' + this.injection.getDate(start + this.injection.getDayShift(18 * 7)).y;
	strArr[10] = this.injection.getDate(start + this.injection.getDayShift(20 * 7)).d + ' ' + this.injection.months[this.injection.getDate(start + this.injection.getDayShift(20 * 7)).m] + ' ' + this.injection.getDate(start + this.injection.getDayShift(20 * 7)).y;
	strArr[11] = this.injection.getDate(start + this.injection.getDayShift(22 * 7)).d + ' ' + this.injection.months[this.injection.getDate(start + this.injection.getDayShift(22 * 7)).m] + ' ' + this.injection.getDate(start + this.injection.getDayShift(22 * 7)).y;
	strArr[12] = this.injection.getDate(start + this.injection.getDayShift(24 * 7)).d + ' ' + this.injection.months[this.injection.getDate(start + this.injection.getDayShift(24 * 7)).m] + ' ' + this.injection.getDate(start + this.injection.getDayShift(24 * 7)).y;
	strArr[13] = this.injection.getDate(start + this.injection.getDayShift(26 * 7)).d + ' ' + this.injection.months[this.injection.getDate(start + this.injection.getDayShift(26 * 7)).m] + ' ' + this.injection.getDate(start + this.injection.getDayShift(26 * 7)).y;
	strArr[14] = this.injection.getDate(start + this.injection.getDayShift(27 * 7)).d + ' ' + this.injection.months[this.injection.getDate(start + this.injection.getDayShift(27 * 7)).m] + ' ' + this.injection.getDate(start + this.injection.getDayShift(27 * 7)).y;
	strArr[15] = this.injection.getDate(start + this.injection.getDayShift(28 * 7)).d + ' ' + this.injection.months[this.injection.getDate(start + this.injection.getDayShift(28 * 7)).m] + ' ' + this.injection.getDate(start + this.injection.getDayShift(28 * 7)).y;
	strArr[16] = this.injection.getDate(start + this.injection.getDayShift(30 * 7)).d + ' ' + this.injection.months[this.injection.getDate(start + this.injection.getDayShift(30 * 7)).m] + ' ' + this.injection.getDate(start + this.injection.getDayShift(30 * 7)).y;
	strArr[17] = this.injection.getDate(start + this.injection.getDayShift(32 * 7)).d + ' ' + this.injection.months[this.injection.getDate(start + this.injection.getDayShift(32 * 7)).m] + ' ' + this.injection.getDate(start + this.injection.getDayShift(32 * 7)).y;
	strArr[18] = this.injection.getDate(start + this.injection.getDayShift(38 * 7)).d + ' ' + this.injection.months[this.injection.getDate(start + this.injection.getDayShift(38 * 7)).m] + ' ' + this.injection.getDate(start + this.injection.getDayShift(38 * 7)).y;
	
	return strArr;
}

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

BirthDate.prototype.deltaInDays = function(m1, m2) {
	var delta = m1 - m2;
	var days = Math.round(delta / this.injection.millisec);
	return days;
}

BirthDate.prototype.getWeek = function(days) {
	var week = 0;
	var delta = 0;
	
	if (days > 0) {
		week = Math.floor(days / 7);
		var weekDays = week * 7;
		delta = days > weekDays ? days - weekDays : 0;
	}
	
	return {w: week, d: delta}
}

BirthDate.prototype.getWord = function(num) {
	var word = 0;
	
	if (num == 1 || (num % 10 == 1)) {
		word = 0;
	} else if ((num >= 2 && num <= 4) || (num % 10 >= 2 && num % 10 <= 4)) {
		word = 1;
	} else if (num == 0 || num > 4 || (num % 10 > 4)) {
		word = 2;
	}
	
	return word;
}
