
var curInstance;
var brigade_dates = new Array();


function in_array(arr, p_val) {
	for(var i = 0, l = arr.length; i < l; i++) {
		if(arr[i] == p_val) {
			return true;
		}
	}
	return false;
}

var languageCode = 'sk';	// Possible values: 	en,ge,no,nl,es,pt-br,fr
// en = english, de = german, no = norwegian,nl = dutch, es = spanish, pt-br = portuguese, fr = french, da = danish, hu = hungarian(Use UTF-8 doctype for hungarian)


// Format of current day at the bottom of the calendar
// [todayString] = the value of todayString
// [dayString] = day of week (examle: Mon, Tue, Wed...)
// [day] = Day of month, 1..31
// [monthString] = Name of current month
// [year] = Current year
//var todayStringFormat = '[todayString] [dayString]. [day]. [monthString] [year]';
var pathToImages = '/includes/calendar/images/';	// Relative to your HTML file



var calendar_offsetTop = 0;		// Offset - calendar placement - You probably have to modify this value if you're not using a strict doctype
var calendar_offsetLeft = 0;	// Offset - calendar placement - You probably have to modify this value if you're not using a strict doctype
var calendarDiv = false;
var timePos;
var activeDay;

var MSIE = false;
var Opera = false;
if(navigator.userAgent.indexOf('MSIE')>=0 && navigator.userAgent.indexOf('Opera')<0)MSIE=true;
if(navigator.userAgent.indexOf('Opera')>=0)Opera=true;

switch(languageCode)
{

	default:
	case "en":	/* English */
	var monthArray = ['January','February','March','April','May','June','July','August','September','October','November','December'];
	var monthArrayShort = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
	var dayArray = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun'];
	var weekString = 'Week';
	var todayString = 'Today is';
	var todayStringFormat = '[todayString] [dayString]. [monthString] [day] [year]';
	break;
	case "de":	/* German */
	var monthArray = ['Jänner','Februar','März','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember'];
	var monthArrayShort = ['Jan','Feb','Mar','Apr','Mai','Jun','Jul','Aug','Sep','Okt','Nov','Dez'];
	var dayArray = ['Mon','Die','Mit','Don','Fre','Sam','Son'];
	var weekString = 'Woche';
	var todayString = 'Heute';
	var todayStringFormat = '[todayString] [dayString]. [day]. [monthString] [year]';
	break;

	case "sk":	/* Slovak */
	var monthArray = ['Január','Február','Marec','Apríl','Máj','Jún','Júl','August','September','Október','November','December'];
	var monthArrayShort = ['Jan','Feb','Mar','Apr','Máj','Jún','Júl','Aug','Sep','Okt','Nov','Dec'];
	var dayArray = ['Po','Ut','St','Št','Pia','So','Ne'];
	var weekString = 'týždeň';
	var todayString = 'Dnes je';
	var todayStringFormat = '[todayString] [dayString]. [day]. [monthString] [year]';
	break;

	case "cz":	/* Czech */
	var monthArray = ['Leden','Únor','Březen','Duben','Květen','Červen','Červenec','Srpen','Září','Říjen','Listopad','Prosinec'];
	var monthArrayShort = ['Led','Úno', 'Bře','Dub','Kvě','Čer','Čer','Srp','Zar','Rij','Lis','Pro'];
	var dayArray = ['Po','Ut','St','Čt','Pá','So','Ne'];
	var weekString = 'týden';
	var todayString = 'Dnes je';
	var todayStringFormat = '[todayString] [dayString]. [day]. [monthString] [year]';
	break;


	case "hu":	/* Hungar */
	var monthArray = ['Január', 'Február', 'Március', 'Április', 'Május', 'Június', 'Július', 'Augusztus', 'Szeptember', 'Október', 'November', 'December'];
	var monthArrayShort = ['Jan', 'Feb', 'Már', 'Ápr', 'Máj', 'Jún', 'Júl', 'Aug', 'Szept', 'Okt', 'Nov', 'Dec'];
	var dayArray = ['Hét', 'Kedd', 'Szer', 'Csüt', 'Pé', 'Szo', 'Vas'];
	var weekString = 'hét';
	var todayString = 'Ma';
	var todayStringFormat = '[todayString]: [dayString]. [year] [monthString]. [day].';
	break;
}


var selectBoxHighlightColor = '#FF0000'; // Highlight color of select boxes
var selectBoxRolloverBgColor = 'silver'; // Background color on drop down lists(rollover

C_Mode_DontDisplayBottomBar = 2;
C_Mode_DontDisplayTopBar = 4;
C_Mode_DisplayCustomBottomTitle = 8;
C_Mode_DontClose = 16;
C_Mode_SelectedColor = 32;
C_Mode_CustomDate = 64;
C_Mode_MultipleDaysSelect = 128;
C_Mode_NotSelectAble = 256;
C_Mode_RelativePosition = 512;
C_Mode_JustShowDays = 1024; //show multiple selected days
C_Mode_PickDatesOnSwitchMonth =2048;
C_Mode_CustomRowCount =4096;
C_Mode_MakeInactiveDays = 8192;
C_Mode_CustomAfterOnclickEvent = 16384;
C_Mode_RequiredTime = 32768;

var fac = {};
fac.cCalendar = function() {
	this.currentMonth = false;
	this.currentYear = false;
	this.currentDay = false;
	this.timePos = false;
	this.timeVal = '';
	this.calendarDiv = false;
	this.returnFormat = false;
	this.returnDateTo = false;
	this.iframeObj = false;
	this.exclusiveId = false;
	this.topBar = false;
	this.activeSelectBoxMonth = false;
	this.calendarContentDiv = false;
	this.activeDay = false;
	this.daysInMonthArray = [31,28,31,30,31,30,31,31,30,31,30,31];
	this.inputDay = false;
	this.inputMonth = false;
	this.inputYear = false;
	this.customCurrentDay = false;
	this.customCurrentMonth = false;
	this.customCurrentYear = false;
	this.repository = false;
	this.monthPickDates = new Array();
	this.calTBody = false;
	this.customInactiveDate = false;

	this.buttonObj = false;

	this.startTopPos = 0;
	this.startLeftPos = 0;

	//some dynamic variables
	this.customBottomTitle = '';
	this.customRowCount = 0;
	this.customAfterOnclickEventFunction = false;

	//modes section
	this.mode_DontDisplayBottomBar = false;
	this.mode_DontDisplayTopBar = false;
	this.mode_DisplayCustomBottomTitle = false;
	this.mode_DontClose = false;
	this.mode_SelectedColor = false;
	this.mode_CustomDate = false;
	this.mode_MultipleDaysSelect = false;
	this.mode_NotSelectAble = false;
	this.mode_RelativePosition = false;
	this.mode_JustShowDays = false;
	this.mode_PickDatesOnSwitchMonth = false;
	this.mode_CustomRowCount = false;
	this.mode_MakeInactiveDays = false;
	this.mode_CustomAfterOnclickEvent = false;
	this.mode_RequiredTime = false;

	this.setCustomOnClickEvent = function(f) {
		this.customAfterOnclickEventFunction = f;
	}

	this.setCustomInactiveDate = function(y, m, d) {
		this.customInactiveDate = new Date(y, m, d);
	}

	this.setCustomRowCount = function(rowCount) {
		this.customRowCount = rowCount;
	}

	this.setId = function(id) {
		this.exclusiveId = id;
	};

	this.fetchDatesFromRepository = function(repository) {
		value = repository.value.split('_');
		dates = new Array();
		for (var i in value) {
			if (!in_array(dates, value[i])) {
				dates.push(value[i]);
			}
		}
		return dates;
	}

	this.getTimeSelectbox  = function()
	{
	return '<div class="jscaldiv2"><form name="idxg1idr752314876I214898700" id="idxg1idr752314876I214898700" method="get" onsubmit="return false;"><b>Čas</b>&nbsp;<select name="time" id="timeDiv_'+this.exclusiveId+'" class="selectBox" class="select"><option value="0"></option><option value="6:30">6:30</option><option value="7:00">7:00</option><option value="7:30">7:30</option><option value="8:00">8:00</option><option value="8:30">8:30</option><option value="9:00">9:00</option><option value="9:30">9:30</option><option value="10:00">10:00</option><option value="10:30">10:30</option><option value="11:00">11:00</option><option value="11:30">11:30</option><option value="12:00">12:00</option><option value="12:30">12:30</option><option value="13:00">13:00</option><option value="13:30">13:30</option><option value="14:00">14:00</option><option value="14:30">14:30</option><option value="15:00">15:00</option><option value="15:30">15:30</option><option value="16:00">16:00</option><option value="16:30">16:30</option><option value="17:00">17:00</option><option value="17:30">17:30</option><option value="18:00">18:00</option><option value="18:30">18:30</option><option value="19:00">19:00</option><option value="19:30">19:30</option><option value="20:00">20:00</option></select><input name="idxg2idr66860504I1487253363" type="button" value="OK" onclick="if(curInstance.mode_RequiredTime &amp;&amp; isEmpty(document.getElementsByName(&quot;time&quot;)[0].value)) {alert(&quot;Prosím zvoľte čas&quot;); return false;} pickDate(false,-1,curInstance, true); return true;" /></form></div>';
	}

	this.setPickDatesForMonth = function(days, month_id) {
		this.monthPickDates.push(month_id + '___' + days);
	};

	//return dates for current month
	this.fetchDaysToGivenMonth = function(month) {
		if (this.monthPickDates.length > 0) {
			for (var one_month in this.monthPickDates) {
				if (parseInt(this.monthPickDates[one_month]) == month) {
					s = this.monthPickDates[one_month];
					if (typeof(s) == "string") {
						a = s.split("___");
					}
					return a[1];
				}
			}
		}
		return "";
	};

	this.setCustomDate = function(year, month, day) {
		this.customCurrentYear = year;
		this.customCurrentMonth = month;
		this.customCurrentDay = day;
	};

	//this will contain throwable errors
	this.setOptions = function(options) {
		this.mode_DontDisplayBottomBar = C_Mode_DontDisplayBottomBar & options;
		this.mode_DontDisplayTopBar = C_Mode_DontDisplayTopBar & options;
		this.mode_DisplayCustomBottomTitle = C_Mode_DisplayCustomBottomTitle & options;
		this.mode_DontClose = C_Mode_DontClose & options;
		this.mode_SelectedColor = C_Mode_SelectedColor & options;
		this.mode_CustomDate = C_Mode_CustomDate & options;
		this.mode_MultipleDaysSelect = C_Mode_MultipleDaysSelect & options;
		this.mode_NotSelectAble = C_Mode_NotSelectAble & options;
		this.mode_RelativePosition = C_Mode_RelativePosition & options;
		this.mode_JustShowDays = C_Mode_JustShowDays & options;
		this.mode_PickDatesOnSwitchMonth = C_Mode_PickDatesOnSwitchMonth & options;
		this.mode_CustomRowCount = C_Mode_CustomRowCount & options;
		this.mode_MakeInactiveDays = C_Mode_MakeInactiveDays & options;
		this.mode_CustomAfterOnclickEvent = C_Mode_CustomAfterOnclickEvent & options;
		this.mode_RequiredTime = (C_Mode_RequiredTime & options) != 0;
	};

	this.setHiddenRepository = function(repository) {
		this.repository = repository;
		this.repositoryArray = new Array();
	};

	this.setLeftPos = function(value) {
		this.startLeftPos = value;
	};

	this.setTopPos = function(value) {
		this.startTopPos = value;
	};

	this.showMessage = function() {
	};

	this.displayCalendar = function(inputField,format,buttonObj) {

		var tmpTime;
		format = format.toLowerCase();
		this.buttonObj = buttonObj;
		if(inputField.value.length==format.length) {
			var monthPos = format.indexOf('mm');
			this.currentMonth = inputField.value.substr(monthPos,2)/1 -1;
			var yearPos = format.indexOf('yyyy');
			this.currentYear = inputField.value.substr(yearPos,4);
			var dayPos = format.indexOf('dd');
			this.currentDay = inputField.value.substr(dayPos,2);
		}

		if (this.mode_CustomDate) {
			var d = new Date();
			d.setFullYear(this.customCurrentYear, this.customCurrentMonth, this.customCurrentDay);
			this.currentMonth = d.getMonth();
			this.currentYear = d.getFullYear();
			this.currentDay = d.getDate();
		} else if(!this.currentMonth || !this.currentYear || !this.currentDay) {
			var d = new Date();
			this.currentMonth = d.getMonth();
			this.currentYear = d.getFullYear();
			this.currentDay = d.getDate();
		}

		this.timePos = format.indexOf('hh:ii');
		this.timeVal = inputField.value.substr(this.timePos,5);

		this.inputYear = this.currentYear;
		this.inputMonth = this.currentMonth;
		this.inputDay = this.currentDay/1;

		if(!this.calendarDiv){
			this.initCalendar();
		}else{
			if(this.calendarDiv.style.display=='block'){
				this.closeCalendar(this);
				return false;
			}
			this.writeCalendarContent();
		}
		this.returnFormat = format;
		this.returnDateTo = inputField;
		this.positionCalendar(buttonObj);
		this.calendarDiv.style.visibility = 'visible';
		this.calendarDiv.style.display = 'block';

		if(this.iframeObj) {
			this.iframeObj.style.display = '';
			this.iframeObj.style.height = '140px';
			this.iframeObj.style.width = '195px';
		}

		this.updateYearDiv();
		this.updateMonthDiv();
	};

	this.updateMonthDiv = function() {
		for(no=0;no<12;no++){
			document.getElementById('monthDiv_' + this.exclusiveId + '_' + no).style.color = '';
		}
		document.getElementById('monthDiv_' + this.exclusiveId + '_' + this.currentMonth).style.color = selectBoxHighlightColor;
		this.activeSelectBoxMonth = document.getElementById('monthDiv_' + this.exclusiveId + '_' + this.currentMonth);
	};

	this.positionCalendar = function(inputObj) {
		this.calendarDiv.style.top = this.startTopPos + getTopPos(inputObj) + 'px';
		if (this.mode_RelativePosition) {
			this.calendarDiv.style.top = this.startTopPos + 'px';
		}
		if (screen.availWidth < getleftPos(inputObj)+195) {
			this.calendarDiv.style.right ='0px';
		} else {
			this.calendarDiv.style.left = this.startLeftPos + getleftPos(inputObj) + 'px';
		}

		if (this.mode_RelativePosition) {
			this.calendarDiv.style.left = this.startLeftPos + 'px';
		}

		if(this.iframeObj){
			this.iframeObj.style.left = this.calendarDiv.style.left;
			this.iframeObj.style.top =  this.calendarDiv.style.top;
		}

	};

	this.closeCalendar = function(obj) {
		obj = this.obj ? this.obj : obj;
		document.getElementById('yearDropDown' + obj.exclusiveId).style.display='none';
		document.getElementById('monthDropDown' + obj.exclusiveId).style.display='none';
		obj.calendarDiv.style.display='none';
		if(obj.iframeObj)
			obj.iframeObj.style.display='none';
		if(this.activeSelectBoxMonth)
			obj.activeSelectBoxMonth.className='';
		if(this.activeSelectBoxYear)
			obj.activeSelectBoxYear.className='';
	};

	this.setPlusTop = function(plusTop) {
		this.topPlus = plusTop;
	};

	this.setPlusLeft = function(plusLeft) {
		this.plusLeft = plusLeft;
	};

	this.updateYearDiv = function() {
		var div = document.getElementById('yearDropDown' + this.exclusiveId);
		var yearItems = div.getElementsByTagName('DIV');
		for (var no=1;no<yearItems.length-1;no++){
			yearItems[no].innerHTML = this.currentYear/1 -6 + no;
			if(this.currentYear==(this.currentYear/1 -6 + no)) {
				yearItems[no].style.color = selectBoxHighlightColor;
				this.activeSelectBoxYear = yearItems[no];
			} else {
				yearItems[no].style.color = '';
			}
		}
	};

	this.initCalendar = function() {
		if(MSIE && false){
			this.iframeObj = document.createElement('IFRAME');
			if (this.mode_RelativePosition) {
				//this.iframeObj.style.position = 'relative';
			} else {
				this.iframeObj.style.position = 'absolute';
			}

			this.iframeObj.border='0px';
			this.iframeObj.style.border = '0px';
			this.iframeObj.style.backgroundColor = '#FF0000';
			this.iframeObj.style.display = 'none';
			this.iframeObj.id = "iframe" + this.exclusiveId;
			if (this.mode_RelativePosition) {
				document.body.appendChild(this.iframeObj);
				if (this.buttonObj) {
				}
			} else {
				//document.body.appendChild(this.iframeObj);
			}
		}

		this.calendarDiv = document.createElement('DIV');
		this.calendarDiv.id = 'calendarDiv' + this.exclusiveId;
		this.calendarDiv.className = 'calendarDiv';
		this.calendarDiv.style.zIndex = 1000;

		if (this.mode_RelativePosition) {
			this.calendarDiv.style.position = 'relative';
			//this.calendarDiv.style.float = 'left';
			this.buttonObj.parentNode.insertBefore(this.calendarDiv, this.buttonObj);
		} else {
			document.body.appendChild(this.calendarDiv);
		}

		this.writeBottomBar();
		this.writeTopBar();

		if(!this.currentYear){
			var d = new Date();
			this.currentMonth = d.getMonth();
			this.currentYear = d.getFullYear();
		}
		this.writeCalendarContent();
	};

	this.setCustomBottomTitle = function(title) {
		this.customBottomTitle = title;
	};

	this.writeBottomBar = function() {
		var d = new Date();
		this.bottomBar = document.createElement('DIV');
		this.bottomBar.id = 'bottomBar' + this.exclusiveId;
		this.bottomBar.style.cursor = 'pointer';
		this.bottomBar.className = this.timePos > -1 ? 'todaysDate2' : 'todaysDate';
		if (this.mode_DontDisplayTopBar) {
			if (this.bottomBar.style) {
				this.bottomBar.style.left = '0px';
			}
		}
		if (this.mode_DontDisplayBottomBar) {
			this.bottomBar.style.display = "none";
		}

		var day = d.getDay();
		if (day==0)
			day = 7;
		day--;

		if (this.mode_DisplayCustomBottomTitle) {
			bottomString = this.customBottomTitle;
		} else {
			var bottomString = '&nbsp;' + todayStringFormat;
			bottomString = bottomString.replace('[monthString]',monthArrayShort[d.getMonth()]);
			bottomString = bottomString.replace('[day]',d.getDate());
			bottomString = bottomString.replace('[year]',d.getFullYear());
			bottomString = bottomString.replace('[dayString]',dayArray[day].toLowerCase());
			bottomString = bottomString.replace('[todayString]',todayString);

			if(this.timePos > -1) {
				bottomString += this.getTimeSelectbox();
			}
			this.bottomBar.innerHTML = todayString + ': ' + d.getDate() + '. ' + monthArrayShort[d.getMonth()] + ', ' +  d.getFullYear();
		}
		this.bottomBar.innerHTML = bottomString;
		this.calendarDiv.appendChild(this.bottomBar);

		if(this.timePos > -1) {
			setSelectedValue(document.getElementById('timeDiv_'+this.exclusiveId), this.timeVal.replace(/^0/, ''));
		}

	};

	this.writeTopBar = function() {
		this.topBar = document.createElement('DIV');
		this.topBar.className = 'topBar';
		this.topBar.id = 'topBar' + this.exclusiveId;
		if (this.mode_DontDisplayTopBar) {
			this.topBar.style.display = "none";
		}
		this.calendarDiv.appendChild(this.topBar);


		// Left arrow
		var leftDiv = document.createElement('DIV');
		leftDiv.style.marginRight = '1px';
		var img = document.createElement('IMG');
		img.src = pathToImages + 'left.gif';
		img.onmouseover = highlightArrow;
		img.onclick = switchMonth;
		img.obj = this;
		img.onmouseout = highlightArrow;
		leftDiv.appendChild(img);
		this.topBar.appendChild(leftDiv);
		if(Opera)leftDiv.style.width = '16px';

		// Right arrow
		var rightDiv = document.createElement('DIV');
		rightDiv.style.marginRight = '1px';
		var img = document.createElement('IMG');
		img.src = pathToImages + 'right.gif';
		img.onclick = switchMonth;
		img.obj = this;
		img.onmouseover = highlightArrow;
		img.onmouseout = highlightArrow;
		rightDiv.appendChild(img);
		if(Opera)rightDiv.style.width = '16px';
		this.topBar.appendChild(rightDiv);

		// Month selector
		var monthDiv = document.createElement('DIV');
		monthDiv.id = 'monthSelect' + this.exclusiveId;
		monthDiv.className = 'monthSelect';
		monthDiv.onmouseover = highlightSelect;
		monthDiv.onmouseout = highlightSelect;
		monthDiv.exclusiveId = this.exclusiveId;
		monthDiv.onclick = showMonthDropDown;
		var span = document.createElement('SPAN');
		span.innerHTML = monthArray[this.currentMonth];
		span.id = 'calendar_month_txt' + this.exclusiveId;
		monthDiv.appendChild(span);

		var img = document.createElement('IMG');
		img.src = pathToImages + 'down.gif';
		img.style.position = 'absolute';
		img.style.right = '0px';
		monthDiv.appendChild(img);
		monthDiv.className = 'selectBox';
		if(Opera){
			img.style.cssText = 'float:right;position:relative';
			img.style.position = 'relative';
			img.style.styleFloat = 'right';
		}
		this.topBar.appendChild(monthDiv);

		var monthPicker = this.createMonthDiv();
		monthPicker.style.left = '37px';
		monthPicker.style.top = monthDiv.offsetTop + monthDiv.offsetHeight + 1 + 'px';
		monthPicker.style.width ='70px';
		monthPicker.id = 'monthDropDown' + this.exclusiveId;

		this.calendarDiv.appendChild(monthPicker);

		// Year selector
		var yearDiv = document.createElement('DIV');
		yearDiv.className = 'monthSelect';
		yearDiv.onmouseover = highlightSelect;
		yearDiv.onmouseout = highlightSelect;
		yearDiv.id = 'yearDropSelect' + this.exclusiveId;
		yearDiv.exclusiveId = this.exclusiveId;
		yearDiv.onclick = showYearDropDown;
		var span = document.createElement('SPAN');
		span.innerHTML = this.currentYear;
		span.id = 'calendar_year_txt' + this.exclusiveId;
		yearDiv.appendChild(span);
		this.topBar.appendChild(yearDiv);

		var img = document.createElement('IMG');
		img.src = pathToImages + 'down.gif';
		yearDiv.appendChild(img);
		yearDiv.className = 'selectBox';
		if(Opera){
			yearDiv.style.width = '50px';
			img.style.cssText = 'float:right';
			img.style.position = 'relative';
			img.style.styleFloat = 'right';
		}

		var yearPicker = this.createYearDiv();
		yearPicker.style.left = '123px';
		yearPicker.style.top = monthDiv.offsetTop + monthDiv.offsetHeight + 1 + 'px';
		yearPicker.style.width = '35px';
		yearPicker.id = 'yearDropDown' + this.exclusiveId;
		this.calendarDiv.appendChild(yearPicker);

		var img = document.createElement('IMG');
		img.src = pathToImages + 'close.gif';
		img.style.styleFloat = 'right';
		img.onmouseover = highlightClose;
		img.onmouseout = highlightClose;
		img.exclusiveId = this.exclusiveId;
		img.obj = this;
		img.onclick = this.closeCalendar;
		this.topBar.appendChild(img);
		if(!document.all){
			img.style.position = 'absolute';
			img.style.right = '2px';
		}
	};

	this.createMonthDiv = function() {
		var div = document.createElement('DIV');
		div.className = 'monthYearPicker';
		div.id = 'monthPicker' + this.exclusiveId;

		for(var no=0;no<monthArray.length;no++){
			var subDiv = document.createElement('DIV');
			subDiv.innerHTML = monthArray[no];
			subDiv.obj = this;
			subDiv.exclusiveId = this.exclusiveId;
			subDiv.onmouseover = highlightMonthYear;
			subDiv.onmouseout = highlightMonthYear;
			subDiv.onclick = selectMonth;
			subDiv.id = 'monthDiv_' + this.exclusiveId + '_' + no;
			subDiv.style.width = '56px';
			subDiv.onselectstart = cancelCalendarEvent;
			div.appendChild(subDiv);
			if(this.currentMonth && this.currentMonth==no){
				subDiv.style.color = selectBoxHighlightColor;
				this.activeSelectBoxMonth = subDiv;
			}
		}
		return div;
	};

	this.createYearDiv = function() {
		if (!document.getElementById('yearDropDown' + this.exclusiveId)) {
			var div = document.createElement('DIV');
			div.className='monthYearPicker';
		} else {
			var div = document.getElementById('yearDropDown' + this.exclusiveId);
			var subDivs = div.getElementsByTagName('DIV');
			for (var no=0;no<subDivs.length;no++) {
				subDivs[no].parentNode.removeChild(subDivs[no]);
			}
		}

		var d = new Date();
		if(this.currentYear){
			d.setFullYear(this.currentYear);
		}

		var startYear = d.getFullYear()/1 - 5;

		var subDiv = document.createElement('DIV');
		subDiv.innerHTML = '&nbsp;&nbsp;- ';
		subDiv.onclick = changeSelectBoxYear;
		subDiv.exclusiveId = this.exclusiveId;
		subDiv.obj = this;
		subDiv.onmouseover = highlightMonthYear;
		subDiv.onmouseout = highlightMonthYear;
		subDiv.onselectstart = cancelCalendarEvent;
		div.appendChild(subDiv);

		for(var no=startYear;no<(startYear+10);no++){
			var subDiv = document.createElement('DIV');
			subDiv.innerHTML = no;
			subDiv.onmouseover = highlightMonthYear;
			subDiv.onmouseout = highlightMonthYear;
			subDiv.onclick = selectYear;
			subDiv.obj = this;
			subDiv.exclusiveId = this.exclusiveId;
			subDiv.id = 'yearDiv' + this.exclusiveId + no;
			subDiv.onselectstart = cancelCalendarEvent;
			div.appendChild(subDiv);
			if(this.currentYear && this.currentYear==no){
				subDiv.style.color = selectBoxHighlightColor;
				this.activeSelectBoxYear = subDiv;
			}
		}
		var subDiv = document.createElement('DIV');
		subDiv.innerHTML = '&nbsp;&nbsp;+ ';
		subDiv.exclusiveId = this.exclusiveId;
		subDiv.obj = this;
		subDiv.onclick = changeSelectBoxYear;
		subDiv.onmouseover = highlightMonthYear;
		subDiv.onmouseout = highlightMonthYear;
		subDiv.onselectstart = cancelCalendarEvent;
		div.appendChild(subDiv);
		return div;
	};

	this.writeCalendarContent = function() {
		var calendarContentDivExists = true;
		if(!this.calendarContentDiv){
			this.calendarContentDiv = document.createElement('DIV');
			this.calendarDiv.appendChild(this.calendarContentDiv);
			this.calendarContentDiv.style.textAlign = 'left';
			calendarContentDivExists = false;
		}

		this.currentMonth = this.currentMonth/1;
		var d = new Date();

		d.setFullYear(this.currentYear);
		d.setDate(1);
		d.setMonth(this.currentMonth);

		var dayStartOfMonth = d.getDay();
		if(dayStartOfMonth==0)
			dayStartOfMonth = 7;
		dayStartOfMonth--;

		document.getElementById('calendar_year_txt' + this.exclusiveId).innerHTML = this.currentYear;
		document.getElementById('calendar_month_txt' + this.exclusiveId).innerHTML = monthArray[this.currentMonth]+'&nbsp;&nbsp;&nbsp;&nbsp;';

		var existingTable = this.calendarContentDiv.getElementsByTagName('TABLE');
		if (existingTable.length > 0) {
			this.calendarContentDiv.removeChild(existingTable[0]);
		}

		var calTable = document.createElement('TABLE');
		calTable.cellSpacing = '0';

		if(this.timePos > -1) {
			calTable.style.marginBottom = '22px';
		}

		this.calendarContentDiv.appendChild(calTable);
		var calTBody = document.createElement('TBODY');
		this.calTBody = calTBody;
		calTable.appendChild(calTBody);
		var row = calTBody.insertRow(-1);
		var cell = row.insertCell(-1);
		cell.innerHTML = weekString;
		cell.style.backgroundColor = selectBoxRolloverBgColor;

		for(var no=0;no<dayArray.length;no++){
			var cell = row.insertCell(-1);
			cell.style.width = '34px';
			cell.innerHTML = dayArray[no];
		}

		var row = calTBody.insertRow(-1);
		var cell = row.insertCell(-1);
		cell.style.backgroundColor = selectBoxRolloverBgColor;
		var week = getWeek(this.currentYear,this.currentMonth,1);
		cell.innerHTML = week;		// Week

		for(var no=0;no<dayStartOfMonth;no++){
			var cell = row.insertCell(-1);
			cell.innerHTML = '&nbsp;';
		}

		var colCounter = dayStartOfMonth;
		var daysInMonth = this.daysInMonthArray[this.currentMonth];
		if(daysInMonth==28){
			if(isLeapYear(this.currentYear))
				daysInMonth=29;
		}

		for(var no=1;no<=daysInMonth;no++){
			d.setDate(no-1);
			if(colCounter>0 && colCounter%7==0){
				var row = calTBody.insertRow(-1);
				var cell = row.insertCell(-1);
				var week = getWeek(this.currentYear,this.currentMonth,no);
				cell.innerHTML = week;
				cell.style.backgroundColor = selectBoxRolloverBgColor;
			}
			var cell = row.insertCell(-1);
			//novy mod na vyrovnanie dvoch kalendarov
			todayDate = this.currentYear + "-" + this.currentMonth + "-" + no;
			if((this.currentYear==this.inputYear && this.currentMonth == this.inputMonth && no==this.inputDay) || (in_array(brigade_dates, todayDate))) {
				cell.className='activeDay';
				this.activeDay = cell;
			}
			cell.innerHTML = no;

			//
			if (this.mode_MakeInactiveDays) {
				var date = new Date(this.currentYear, this.currentMonth, no);
				if (date < this.customInactiveDate) {
					cell.style.backgroundColor = '#eee';
					cell.style.color = '#ccc';
				} else {
					cell.obj = this;
					cell.onclick = pickDate;
					cell.id = this.currentYear + "-" + PretendZero(this.currentMonth + 1) + "-" + PretendZero(no) + this.exclusiveId;
				}
			} else {
				cell.obj = this;
				cell.onclick = pickDate;
				cell.id = this.currentYear + "-" + PretendZero(this.currentMonth + 1) + "-" + PretendZero(no) + this.exclusiveId;
			}
			colCounter++;
		}

		if (this.mode_CustomRowCount) {
			for (var i = calTBody.rows.length; i < this.customRowCount; i++) {
				var row = calTBody.insertRow(-1);
				var cell = row.insertCell(-1);
				cell.innerHTML = '&nbsp;';
				cell.style.backgroundColor = "#c0c0c0";
			}
		}

		if(!document.all){
			if(this.calendarContentDiv.offsetHeight)
				document.getElementById('topBar' + this.exclusiveId).style.top = this.calendarContentDiv.offsetHeight + document.getElementById('topBar' + this.exclusiveId).offsetHeight -1 + 'px';
			else {
				document.getElementById('topBar' + this.exclusiveId).style.top = '';
				document.getElementById('topBar' + this.exclusiveId).style.bottom = '0px';
			}
		}

		if(this.iframeObj){
			if(!calendarContentDivExists)
				this.resizeIframe();
			else
				this.resizeIframe();
		}

		if (this.mode_PickDatesOnSwitchMonth) {
			days = this.fetchDaysToGivenMonth(this.currentMonth);
			if (days != "") {
				this.pickDates(days.split("_"));
			}
		}
	};

	this.callCustomAfterOnClickEvent = function(obj) {
		eval(this.customAfterOnclickEventFunction);
	}

	this.resizeIframe = function() {
		this.iframeObj.style.width = this.calendarDiv.offsetWidth + 'px';
		this.iframeObj.style.height = this.calendarDiv.offsetHeight + 'px' ;
	};

	this.pickDates = function(days) {
		//days = new Array("2009-01-08", "2009-02-08", "2009-03-09");
		var dates;
		for (i = 0; i < days.length; i++) {
			if (days[i].length == 0) {
				continue;
			}
			day = days[i].split("-");
			var element = day[0] + '-' + day[1] + '-' + day[2];
			if ((document.getElementById(element + this.exclusiveId) && this.mode_SelectedColor)) {
				var el;
				el = document.getElementById(element + this.exclusiveId);
				el.style.backgroundColor = 'orange';

				if (!this.mode_JustShowDays && !in_array(this.repositoryArray, "_"+element)) {
					this.repositoryArray.push('_' + element);
					dates = new Array();
					dates = this.fetchDatesFromRepository(this.repository);
					if (!in_array(dates, element)) {
						this.repository.value += '_' + element;
						//alert(this.repository.value + dates.length);
					}
				}
			}
		}
	};

	this.resetCalendar = function(clearRepository) {
		days = this.repository.value.split('_');
		for (var d in days) {
			if (days[d].length == 0) {
				continue;
			}
			if (document.getElementById(days[d] + this.exclusiveId)) {
				document.getElementById(days[d] + this.exclusiveId).style.backgroundColor = 'white';
			}
		}
		if (clearRepository) {
			this.repository.value = '';
			this.repositoryArray = new Array();
		}
	}
}

fac.calendars = {
	getCalendar: function(id, options) {

		if (!this[id]) {
			this[id] = new fac.cCalendar(id);
			this[id].setId(id);
			this[id].setOptions(options);
		}
		curInstance = this[id];
		return this[id];
	}
}


function PretendZero(num) {
	if (num < 10) {
		return ('0' + num);
	}
	return num;
}

function cancelCalendarEvent() {
	return false;
}

function isLeapYear(inputYear) {
	if(inputYear%400==0||(inputYear%4==0&&inputYear%100!=0)) return true;
	return false;
}

function highlightMonthYear() {
	if (this.obj.activeSelectBoxMonth)
		this.obj.activeSelectBoxMonth.className='';
	if (this.className=='monthYearActive') {
		this.className='';
	} else {
		this.className = 'monthYearActive';
		this.obj.activeSelectBoxMonth = this;
	}
}

function showMonthDropDown() {
	if(document.getElementById('monthDropDown' + this.exclusiveId).style.display=='block'){
		document.getElementById('monthDropDown' + this.exclusiveId).style.display='none';
	} else {
		document.getElementById('monthDropDown' + this.exclusiveId).style.display='block';
		document.getElementById('yearDropDown' + this.exclusiveId).style.display='none';
	}
}

function showYearDropDown() {
	if (document.getElementById('yearDropDown' + this.exclusiveId).style.display=='block'){
		document.getElementById('yearDropDown' + this.exclusiveId).style.display='none';
	} else {
		document.getElementById('yearDropDown' + this.exclusiveId).style.display='block';
		document.getElementById('monthDropDown' + this.exclusiveId).style.display='none';
	}
}

function selectMonth() {
	document.getElementById('calendar_month_txt' + this.exclusiveId).innerHTML = this.innerHTML
	this.obj.currentMonth = this.id.replace(/[^\d]/g,'');

	document.getElementById('monthDropDown' + this.exclusiveId).style.display='none';
	for(var no=0;no<monthArray.length;no++){
		document.getElementById('monthDiv_' + this.exclusiveId + '_' + no).style.color='';
	}
	this.style.color = selectBoxHighlightColor;
	this.obj.activeSelectBoxMonth = this;
	this.obj.writeCalendarContent();
}

function selectYear() {
	document.getElementById('calendar_year_txt' + this.exclusiveId).innerHTML = this.innerHTML
	this.obj.currentYear = this.innerHTML.replace(/[^\d]/g,'');
	document.getElementById('yearDropDown' + this.exclusiveId).style.display='none';
	if (this.obj.activeSelectBoxYear) {
		this.obj.activeSelectBoxYear.style.color='';
	}
	this.obj.activeSelectBoxYear=this;
	this.style.color = selectBoxHighlightColor;
	this.obj.writeCalendarContent();
}

function switchMonth() {
	if(this.src.indexOf('left')>=0){
		this.obj.currentMonth=this.obj.currentMonth-1;;
		if(this.obj.currentMonth<0){
			this.obj.currentMonth=11;
			this.obj.currentYear=this.obj.currentYear-1;
		}
	} else {
		this.obj.currentMonth=this.obj.currentMonth+1;;
		if(this.obj.currentMonth>11){
			this.obj.currentMonth=0;
			this.obj.currentYear=this.obj.currentYear/1+1;
		}
	}

	this.obj.writeCalendarContent();
}

function changeSelectBoxYear() {
	var yearItems = this.parentNode.getElementsByTagName('DIV');
	if (this.innerHTML.indexOf('-')>=0) {
		var startYear = yearItems[1].innerHTML/1 -1;
		if(this.obj.activeSelectBoxYear){
			this.obj.activeSelectBoxYear.style.color='';
		}
	} else {
		var startYear = yearItems[1].innerHTML/1 +1;
		if (this.obj.activeSelectBoxYear) {
			this.obj.activeSelectBoxYear.style.color='';
		}
	}

	for(var no=1;no<yearItems.length-1;no++){
		yearItems[no].innerHTML = startYear+no-1;
		yearItems[no].id = 'yearDiv' + '_' + this.exclusiveId + '_' + (startYear/1+no/1-1);

	}
	if(this.obj.activeSelectBoxYear){
		this.obj.activeSelectBoxYear.style.color='';
		if(document.getElementById('yearDiv'+'_'+this.exclusiveId+'_'+this.obj.currentYear)){
			this.obj.activeSelectBoxYear = document.getElementById('yearDiv'+'_'+this.exclusiveId+'_'+currentYear);
			this.obj.activeSelectBoxYear.style.color=selectBoxHighlightColor;
		}
	}
}

function highlightSelect() {
	if(this.className=='selectBox'){
		this.className = 'selectBoxOver';
		this.getElementsByTagName('IMG')[0].src = pathToImages + 'down_over.gif';
	} else {
		this.className = 'selectBox';
		this.getElementsByTagName('IMG')[0].src = pathToImages + 'down.gif';
	}
}

function highlightArrow() {
	if(this.src.indexOf('over')>=0){
		if(this.src.indexOf('left')>=0)this.src = pathToImages + 'left.gif';
		if(this.src.indexOf('right')>=0)this.src = pathToImages + 'right.gif';
	}else{
		if(this.src.indexOf('left')>=0)this.src = pathToImages + 'left_over.gif';
		if(this.src.indexOf('right')>=0)this.src = pathToImages + 'right_over.gif';
	}
}

function highlightClose() {
	if(this.src.indexOf('over')>=0){
		this.src = pathToImages + 'close.gif';
	}else{
		this.src = pathToImages + 'close_over.gif';
	}
}


function pickTodaysDate() {
	var d = new Date();
	currentMonth = d.getMonth();
	currentYear = d.getFullYear();
	pickDate(false,d.getDate(), null, false);
}


function pickDate(e, inputDay, obj, doclose) {
	obj = this.obj ? this.obj : obj;

	var day;
	var tmp;
	var month = obj.currentMonth/1 + 1;

	if(month<10) {
	  month = '0' + month;
	}

	if(inputDay == -1) {
		day = obj.currentDay;
	} else {
		if (!inputDay) {
			day = this.innerHTML;
		} else {
			day = inputDay;
		}
		obj.currentDay = day;
	}

	if (day/1<10)
		day = '0' + day;

	if(this) {
		obj.activeDay.className='';
		this.className='activeDay';
		if (obj.mode_SelectedColor && !obj.mode_NotSelectAble) {
			if (this.style) {
				if (this.style.backgroundColor == 'orange' || this.style.backgroundColor == '#ffa500') {
					this.style.backgroundColor = 'white';
				} else {
					this.style.backgroundColor = 'orange';
				}
			}
		}
		obj.activeDay = this;
	}

	if(obj.timePos > -1 && e) {
		return;
	}

	if(obj.returnFormat) {
		tmp = obj.returnFormat.replace('dd',day);
		tmp = tmp.replace('mm',month);
		tmp = tmp.replace('yyyy',obj.currentYear);
		current_date_selected = tmp;
		if(obj.timePos > -1) {
			tmp = tmp.replace('hh:ii', document.getElementById('timeDiv_'+obj.exclusiveId).value);
			tmp = tmp.replace(/^\s*/, "").replace(/\s*$/, "");
		}
		obj.returnDateTo.value = tmp;
	}

	if (obj.mode_MultipleDaysSelect) {
		el = obj.repository;
		found = false;
		dates = el.value.split("_");
		c = 0;
		if (dates.length > 0) {
			for (one_day in dates) {
				if (dates[one_day] == current_date_selected) {
					found = true;
					c = one_day;
				}
			}
		}

		if (!found) {
			el.value += "_" + current_date_selected;
			obj.repositoryArray.push(current_date_selected);
		} else {
			dates.splice(c, 1);
			obj.repositoryArray = dates;
			el.value = dates.join("_");
		}

	}
	else if (obj.mode_DontClose)
	{
	}
	else if(doclose || !obj.mode_RequiredTime)
	{
		obj.closeCalendar(obj);
	}

	if (obj.mode_CustomAfterOnclickEvent) {
		obj.callCustomAfterOnClickEvent();
	}
}

// This function is from http://www.codeproject.com/csharp/gregorianwknum.asp
// Only changed the month add
function getWeek(year,month,day) {
	day = day/1;
	year = year /1;
	month = month/1 + 1; //use 1-12
	var a = Math.floor((14-(month))/12);
	var y = year+4800-a;
	var m = (month)+(12*a)-3;
	var jd = day + Math.floor(((153*m)+2)/5) +
	(365*y) + Math.floor(y/4) - Math.floor(y/100) +
	Math.floor(y/400) - 32045;      // (gregorian calendar)
	var d4 = (jd+31741-(jd%7))%146097%36524%1461;
	var L = Math.floor(d4/1460);
	var d1 = ((d4-L)%365)+L;
	NumberOfWeek = Math.floor(d1/7) + 1;
	return NumberOfWeek;
}

function getTopPos(inputObj) {
	var returnValue = inputObj.offsetTop + inputObj.offsetHeight;
	while((inputObj = inputObj.offsetParent) != null)returnValue += inputObj.offsetTop;
	return returnValue + calendar_offsetTop;
}

function getleftPos(inputObj) {
	var returnValue = inputObj.offsetLeft;
	while((inputObj = inputObj.offsetParent) != null)returnValue += inputObj.offsetLeft;
	return returnValue + calendar_offsetLeft;
}


function GetDatesInterval(date_from, date_to, justHoliday) {
  var exp;

  if ((date_from.length == 0) || (date_to.length == 0)) {
  	return false;
  }

  var d= new Date();
  //check right format elst return false
  exp = date_from.split("-");
  day = exp[2];
  month = parseInt(exp[1]);
  year = exp[0];

  d.setDate(day);
  d.setMonth(month - 1);
  d.setYear(year);

  exp = date_to.split("-");
  dt = new Date();
  dt.setDate(exp[2]);
  dt.setMonth(parseInt(exp[1]) - 1);
  dt.setYear(exp[0]);

  if (d > dt) {
  	alert('Pozor, dátum začatia brigády je väčší ako dátum ukončenia brigády.');
  	return false;
  }

  max_value = 30000;
  c = 0;
  dates = new Array();
  while ((c < max_value) && (d.getTime() <= dt.getTime())) {
  	c++;
    if (justHoliday) {
		if (d.getDay() == 0 || d.getDay() == 6) {
			dates.push(d.getFullYear() + "-" + PretendZero(d.getMonth() + 1) + "-" + PretendZero(d.getDate()));
		}
		d.setDate(d.getDate() + 1);
		continue;
    } else {
		dates.push(d.getFullYear() + "-" + PretendZero(d.getMonth() + 1) + "-" + PretendZero(d.getDate()));
		d.setDate(d.getDate() + 1);
    }
  }
	return dates;
}
