var months = ["Januari", "Februari", "Maart", "April", "Mei", "Juni", "Juli", "Augustus", "September", "Oktober", "November", "December"];
var days = ["Zo", "Ma", "Di", "Wo", "Do", "Vr", "Za"];
var year;
var month;
var day;
var inited;
var birthday;
var name;
var monthstoplan = 0;

function Datepicker(birthday, name){
	this.birthday = birthday;
	this.name = name;
	
	this.calcMonthYear = function(p_Month, p_Year, incr){
		var ret_arr = new Array();

		if (incr == -1) {
			if (p_Month == 0) {
				ret_arr[0] = 11;
				ret_arr[1] = p_Year - 1;
			}
			else {
				ret_arr[0] = p_Month - 1;
				ret_arr[1] = p_Year;
			}
		} 
		else if (incr == 1) {
			if (p_Month == 11) {
				ret_arr[0] = 0;
				ret_arr[1] = p_Year + 1;
			}
			else {
				ret_arr[0] = p_Month + 1;
				ret_arr[1] = p_Year;
			}
		}
		return ret_arr;
	};
	
	this.setLanguage = function(language){
		switch(language){
			case 2:
				months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
				break;
			case 1:
				months = ["Januari", "Februari", "Maart", "April", "Mei", "Juni", "Juli", "Augustus", "September", "Oktober", "November", "December"];
				break;
			default:
				months = ["Januari", "Februari", "Maart", "April", "Mei", "Juni", "Juli", "Augustus", "September", "Oktober", "November", "December"];
		}
	};
	
	this.selectDate = function(element){
		if(element.className == "dayothermonth"){
			return;
		}
		this.day = element.innerHTML;
		this.build(true);
		hidestuff(this.name);
		hidestuff('overhead_' + this.name);
	};
	
	this.build = function(selectDate){	
		if(this.day > this.getDaysOfMonth(this.month, this.year)){
			this.day = 1;
		}
		this.buildCalendarPart(selectDate);
		// set month name
		document.getElementById("month_" + this.name).selected = months[this.month - 1];
		// set year
		document.getElementById("year_" + this.name).selected = this.year;
		
		//this.buildMonths();
	};
	
	this.buildMonths = function(){
		if(birthday){
			var select = document.getElementById("month_" + this.name);
			var date = new Date();
			for(var i = 0; i <= date.getMonth() - 1; i++){
				select.options[select.options.length] = new Option(months[i], i);
			}
			this.selectOptionByValue(select, this.month - 1);
		}
		else{
			var select = document.getElementById("month_" + this.name);
			var date = new Date();
			for(var i = date.getMonth() - 1; i < months.length - date.getMonth(); i++){
				select.options[select.options.length] = new Option(months[i], i);
			}
			this.selectOptionByValue(select, this.month - 1);
		}
	};

	this.initDatePicker = function(){
		this.setToday();
		this.build(true);
	};
	
	this.setMonthstoplan = function(monthstoplan){
		this.monthstoplan = monthstoplan;
	};

	this.initTry = function(){
		var inputValue = document.getElementById("DATE_INPUT_" + this.name).value;
		if(inputValue.length > 0){
			if(inputValue == '0-0-0000'){
				this.setToday();
			}
			else{
				var values = inputValue.split('-');
				this.day = parseInt(values[0]);
				this.month = parseInt(values[1]) - 1;
				this.year = parseInt(values[2]);
			}
		}
		else{
			this.setToday();
		}
		this.build(true);
		this.selectOptionByValue(document.getElementById("month_" + this.name), this.month);
		this.selectOptionByValue(document.getElementById("year_" + this.name), this.year);
		if(!this.inited){
			// Months
			var select = document.getElementById("month_" + this.name);
			for(var i = 0; i < months.length; i++){
				select.options[select.options.length] = new Option(months[i], i);
			}
			this.selectOptionByValue(select, this.month);
			
			// Years
			var select = document.getElementById("year_" + this.name);
			if(this.birthday){
				var date = new Date();
				
				for(var i = 1900; i <= date.getFullYear(); i++){
					select.options[select.options.length] = new Option(i, i);
				}
			}
			else{
				var date = new Date();
				for(var i = date.getFullYear(); i < 2100; i++){
					select.options[select.options.length] = new Option(i, i);
				}
			}
			
			this.selectOptionByValue(select, this.year);
			
			this.inited = true;
		}
	};

	this.yearChanged = function(){
		var select = document.getElementById("year_" + this.name);
		this.year = parseInt(select.options[select.selectedIndex].value);
		this.build(false);
	};

	this.selectOptionByValue = function(select, val){
		var A= select.options, L= A.length;
		while(L){
			if (A[--L].value== val){
				select.selectedIndex= L;
				L= 0;
			}
		}
	};

	this.monthChanged = function(){
		var select = document.getElementById("month_" + this.name);
		this.month = parseInt(select.options[select.selectedIndex].value);
		this.build(false);
	};

	this.rebuildCalendarPart = function(numberRows){
		var calendar = document.getElementById("calendar_" + this.name);
		
		while(calendar.rows.length > 1) {
			calendar.deleteRow(calendar.rows.length - 1);
		}
		
		var myTable = document.getElementById("calendar_" + this.name);
		var tBody = myTable.getElementsByTagName('tbody')[0];
		var dayButtonNumber = 1;
		var numberOfDays = numberRows * 7;
		
		for(var x = 0; x < numberRows; x++){
			var newTR = document.createElement('tr');
			
			for(var i = 0; i < 7; i++){
				var newTD = document.createElement('td');
				newTD.innerHTML = 'i';
				newTD.setAttribute('id', "DAY_BUTTON_" + this.name + dayButtonNumber);
				newTD.setAttribute('onclick', this.name + ".selectDate(this);");
				if(this.name == 'datePickerAfter'){
					newTD.onclick = function(){datePickerAfter.selectDate(this);};
				}
				else{
					newTD.onclick = function(){datePicker.selectDate(this);};
				}				
				newTR.appendChild (newTD);
				dayButtonNumber++;
			}
			
			tBody.appendChild(newTR);
		}		
	};

	this.buildCalendarPart = function(selectDate){
		var date = new Date();
		date.setDate(1);
		date.setMonth(this.month);
		date.setFullYear(this.year); 
		dayNo = date.getDay();
		
		var daysCurrentMonth = this.getDaysOfMonth(this.month, this.year);
		
		var prevMonth = this.calcMonthYear(this.month, this.year, -1);
		var prevMonthDays = this.getDaysOfMonth(prevMonth[0], prevMonth[1]);
		var prevMonthDaysStart = prevMonthDays - dayNo + 1;
		var dayButtonNumber = 1;
		
		if(dayNo + daysCurrentMonth < 29){
			this.rebuildCalendarPart(4);
			max = 0;
		}
		else if(dayNo + daysCurrentMonth < 36){
			this.rebuildCalendarPart(5);
			max = 35 - (dayNo + daysCurrentMonth);
		}
		else{
			this.rebuildCalendarPart(6);
			max = 42 - (dayNo + daysCurrentMonth);
		}
		
		// Maand voor huidige maand
		for(var i = 0; i < dayNo; i++){
			var element = document.getElementById("DAY_BUTTON_" + this.name + dayButtonNumber);
			element.innerHTML = prevMonthDaysStart + i;
			element.className='dayothermonth';
			dayButtonNumber++;
		}
		
		// Huidige maand
		for(var i = 1; i <= daysCurrentMonth; i++){
			
			var element = document.getElementById("DAY_BUTTON_" + this.name + dayButtonNumber);
			element.innerHTML = i;
			date = new Date();
			var inputValue = document.getElementById("DATE_INPUT_" + this.name).value;
			var values = inputValue.split('-');
			if(this.day == i && (selectDate || (this.month == parseInt(values[1]) - 1 && this.year == parseInt(values[2])))){
				element.className='current';
			}
			else{
				if(date.getDate() == i && this.month == date.getMonth() && this.year == date.getFullYear()){
					element.className='today';
				}
				else{
					if(this.birthday){
						if((i > date.getDate() && this.month == date.getMonth() && this.year == date.getFullYear()) || (this.month > date.getMonth() && this.year == date.getFullYear()) || this.year > date.getFullYear()){
							element.className='dayothermonth';
						}
						else{
							element.className='day';
						}
					}
					else{
						if(this.monthstoplan > 0){
							var date2 = new Date();
							var date3 = new Date();
							date3.setDate(i);
							date3.setMonth(this.month);
							date3.setFullYear(this.year); 
							
							date2.setMonth(date2.getMonth() + this.monthstoplan);
							
							if(date2 < date3){
								element.className='dayothermonth';
							}
							else if((i < date.getDate() && this.month == date.getMonth() && this.year == date.getFullYear()) || (this.month < date.getMonth() && this.year == date.getFullYear()) || this.year < date.getFullYear()){
								element.className='dayothermonth';
							}
							else{
								element.className='day';
							}
						}
						else if((i < date.getDate() && this.month == date.getMonth() && this.year == date.getFullYear()) || (this.month < date.getMonth() && this.year == date.getFullYear()) || this.year < date.getFullYear()){
							element.className='dayothermonth';
						}
						else{
							element.className='day';
						}
					}
				}
			}
			dayButtonNumber++;
		}
		
		// Maand na de huidige maand
		for(var i = 1; i <= max; i++){
			var element = document.getElementById("DAY_BUTTON_" + this.name + dayButtonNumber);
			element.innerHTML = i;
			element.className='dayothermonth';
			dayButtonNumber++;
		}
		if(selectDate){
			document.getElementById("DATE_INPUT_" + this.name).value = this.day + "-" + (this.month + 1) + "-" + this.year;
		}
	};

	this.buildNextMonth = function(){
		var monthYear = this.calcMonthYear(this.month, this.year, 1);
		if(birthday){
			var date = new Date();
			if(monthYear[1] > date.getFullYear()){
				return;
			}
		}
		this.month = monthYear[0];
		this.year = monthYear[1];
		this.build(false);
		this.selectOptionByValue(document.getElementById("month_" + this.name), this.month);
		this.selectOptionByValue(document.getElementById("year_" + this.name), this.year);
	};

	this.buildPreviousMonth = function(){
		var monthYear = this.calcMonthYear(this.month, this.year, -1);
		if(!birthday){
			var date = new Date();
			if(monthYear[1] < date.getFullYear()){
				return;
			}
		}
		this.month = monthYear[0];
		this.year = monthYear[1];
		this.build(false);
		this.selectOptionByValue(document.getElementById("month_" + this.name), this.month);
		this.selectOptionByValue(document.getElementById("year_" + this.name), this.year);
	};

	this.buildNextYear = function(){
		if(birthday){
			var date = new Date();
			if(this.year + 1 > date.getFullYear()){
				return;
			}
		}
		this.year++;
		this.build(false);
		this.selectOptionByValue(document.getElementById("year_" + this.name), this.year);
	};

	this.buildPreviousYear = function(){
		if(!birthday){
			var date = new Date();
			if(this.year - 1 < date.getFullYear()){
				return;
			}
		}
		this.year--;
		this.build(false);
		this.selectOptionByValue(document.getElementById("year_" + this.name), this.year);
	};

	this.getDaysOfMonth = function(month, year){
		if(month == 1){
			if ((year % 4) == 0){
				return 29;
			}
			return 28;
		}
		switch(month){
			case 0: case 2: case 4: case 6: case 7: case 9: case 11:
				return 31;
			case 3: case 5: case 8: case 10:
				return 30;
		}
	};

	this.setToday = function(){
		var now = new Date();
		this.day = now.getDate();
		this.month = now.getMonth();
		this.year = now.getFullYear();
	};
}
