var cp_timeoutDelay = 100;
var cp_timeoutId = false;
var g_startDay = 1;
var g_Calendar;
function Calendar() {
	g_Calendar = this;
	this.daysOfWeek = jsDani.split(" ");
	this.months = jsMjeseci.split(" ");
	this.daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); 
	this.containerLayer = null;
	this.dayMask = [1,1,1,1,1,1,1];
	//this.Holidays = null;
	//this.showHolidays = true;
}
Calendar.prototype.show = function(e, targetForm, target, dateFormat, dateFrom, dateTo, menuYearFrom, menuYearTo) {
	if(!document.getElementById) {
		alert("This browser is not suppored for a calendar popup.");
		return;
	}
	if (!document.getElementById(target)) {
		alert("Error: input field \"" + target + "\" does not exist.");
		return;
	}
	this.containerLayer = document.getElementById('calcontainer');
	if (!this.containerLayer) {
		this.containerLayer = document.createElement('div');
		this.containerLayer.id = 'calcontainer';
		this.containerLayer.onmouseout=calendarTimeout;
		this.containerLayer.onmouseover=calendarClearTimeout;
		this.containerLayer.onclick=this.handleClick;
		document.body.insertBefore(this.containerLayer,document.body.childNodes[0]);
	}
	if (this.isVisible()) {
		this.hide();
		return;
	}
	calendarClearTimeout();
	var elSrc;
	if (!e){
		e = window.event;
	}
	if (e.target) {
		elSrc = e.target;
	} else if (e.srcElement) {
		elSrc = e.srcElement;
	}
	e.cancelBubble = true;
	if (e.stopPropagation){
		e.stopPropagation();
	}
	var x, y, obj;
	x = y = 0;
	obj = elSrc;
	if(obj.offsetParent) {
		while (obj) {
			x += obj.offsetLeft;
			y += obj.offsetTop;
			obj = obj.offsetParent;
		}
	} else {
		x = obj.x;
		y = obj.y;
	}
	y-=35
	x-=112
	this.containerLayer.style.left = (x+elSrc.offsetWidth+1) + "px";
	this.containerLayer.style.top = y + "px";
	this.dateFrom = dateFrom || new Date(1900,0,1);
	this.dateFromDay = this.padZero(this.dateFrom.getDate());
	this.dateFromMonth = this.padZero(this.dateFrom.getMonth());
	this.dateFromYear = this.dateFrom.getFullYear();
	this.dateTo = dateTo || new Date(3000,0,1);
	this.dateToDay = this.padZero(this.dateTo.getDate());
	this.dateToMonth = this.padZero(this.dateTo.getMonth());
	this.dateToYear = this.dateTo.getFullYear();
	this.dateToday = new Date();
	this.tYear = this.dateToday.getFullYear();
	this.tMonth = this.dateToday.getMonth();
	this.tDay = this.dateToday.getDate();
	if (dateFormat){
		this.dateFormat = dateFormat;
	} else if (dp_dateFormat) {
		this.dateFormat = dp_dateFormat;
	} else if (!this.dateFormat) {
		this.dateFormat = 'm/d/yyyy';
	}
	dp_dateFormat = this.dateFormat;
	if(this.dateFormat.indexOf('-') != -1) {
		this.dateDelim = '-';
	} else if(this.dateFormat.indexOf('/') != -1) {
		this.dateDelim = '/';
	} else if(this.dateFormat.indexOf('.') != -1) {
		this.dateDelim = '.';
	} else if(this.dateFormat.indexOf(' ') != -1) {
		this.dateDelim = ' ';
	} else {
		this.dateDelim = '/';
	}
	form = document.getElementById(targetForm);
	this.target = target;
	this.targetForm = targetForm;
	var tmp = document.getElementById(this.target)
	if (target=="datumOd") var tmp = form.datumOd
	if (target=="datumDo") var tmp = form.datumDo
	tmp.className=tmp.className.replace("error", "").replace("	", " ");
	if (tmp && tmp.value && tmp.value.split(this.dateDelim).length==3){
		var atmp = tmp.value.split(this.dateDelim);
		var delim = eval('/\\' + this.dateDelim + '/g');
		switch (this.dateFormat.replace(delim,"")){
		case 'ddmmmyyyy':
		case 'dmmmyyyy':
			this.day = 1;
			this.year = this.oYear = parseInt(atmp[2],10);
			for (var i=0;i<this.months.length;i++){
				if (atmp[1].toLowerCase()==this.months[i].substr(0,3).toLowerCase()){
					this.month = this.oMonth = i;
					break;
				}
			}
			this.day = parseInt(atmp[0],10);
			break;
		case 'ddmmyyyy':
		case 'dmmyyyy':
		case 'dmyyyy':
			this.day = 1;
			this.year = this.oYear = parseInt(atmp[2],10);
			this.month = this.oMonth = parseInt(atmp[1]-1,10); 
			this.day = parseInt(atmp[0],10);
			break;
		case 'mmddyyyy':
		case 'mddyyyy':
		case 'mdyyyy':
			this.day = 1;
			this.year = this.oYear = parseInt(atmp[2],10);
			this.month = this.oMonth = parseInt(atmp[0]-1,10);
			this.day = parseInt(atmp[1],10);
			break;
		case 'yyyymmdd':
			this.day = 1;
			this.year = this.oYear = parseInt(atmp[0],10);
			this.month = this.oMonth = parseInt(atmp[1]-1,10);
			this.day = parseInt(atmp[2],10);
			break;
		}
	} else {
		var theDate = new Date();
		this.year = this.oYear = theDate.getFullYear();
		this.month = this.oMonth = theDate.getMonth();
		this.day = this.oDay = theDate.getDate();
	}
	this.menuYearFrom = menuYearFrom || this.year - 3;
	this.menuYearTo = menuYearTo || this.year + 3;
	var tmpCurYear = (new Date()).getFullYear();
	if(this.menuYearTo < tmpCurYear)
		this.menuYearTo = tmpCurYear;
	this.menuYearTo++;
	this.writeString(this.buildString());
	this.containerLayer.style.visibility='visible';
};
Calendar.prototype.buildString = function(){
	var tmpStr = '';
	// See if there are holidays for this month. Save the holidays to a temporary array
	// so that you only have to look at holidays for this month when building the calendar.
	// This is to improve performance.
	/*var aHolidays = new Array();
	if(this.Holidays) {
		for(var h = 0; h < this.Holidays.length; h++) {
			if(this.Holidays[h].date.getFullYear() == this.year && this.Holidays[h].date.getMonth() == this.month) {
				aHolidays[aHolidays.length] = this.Holidays[h];
			}
		}
	}*/
	if (document.all&&document.getElementById && !window.Opera){
		if (navigator.appVersion.substr(22,3)!="5.0"){
			tmpStr = '<iframe src="about:blank" scrolling="no" frameborder="0" style="width: ' + this.containerLayer.offsetWidth + 'px; height: ' + this.containerLayer.offsetHeight + 'px; z-index: -1; position: absolute; filter: progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0);"></iframe>';
		}
	}
	tmpStr+='<table cellspacing="0">';
	tmpStr+='<thead><tr><td colspan="7" style="background: #2B97D5;"><ul>\n'
		+'<li id="cp_prevMonth"><a href="javascript:g_Calendar.changeMonth(-1)"><span>&lt;</span></a></li>\n'
		+'<li id="cp_monthMenu"><a href="javascript:g_Calendar.openMenuMonths()">' + this.months[this.month] + '</a>\n'
		+'<ul id="cp_months">'
	for (n1=0;n1<12;n1++) {
		tmpStr+='<li><a href="javascript:g_Calendar.clickMonth('+n1+')">'+this.months[n1]+'</a></li>'
	}
	tmpStr+='</ul>\n'
		+'<li id="cp_nextMonth"><a href="javascript:g_Calendar.changeMonth(1)"><span>&gt;</span></a></li>\n'
		+'<li id="cp_prevYear"><a href="javascript:g_Calendar.changeYear(-1)"><span>&lt;</span></a></li>\n'
		+'<li id="cp_yearMenu"><a href="javascript:g_Calendar.openMenuYears();">' + this.year + '</a>\n';

	if(this.menuYearTo - this.menuYearFrom > 7){
		tmpStr += '<ul id="cp_years" style="overflow: auto; overflow-x: hidden; overflow-y: auto;">';
	}else{
		tmpStr += '<ul id="cp_years">';
	}
	var menuYearTo = this.menuYearTo;
	if(menuYearTo <= this.year)
		menuYearTo = this.year + 1;
	for (var y = this.menuYearFrom; y < menuYearTo; y++) {
		tmpStr += '<li><a href="javascript:g_Calendar.clickYear(' + y + ')">' + y + '</a></li>';
	}
	tmpStr += '</ul></li>\n<li id="cp_nextYear"><a href="javascript:g_Calendar.changeYear(1)"><span>&gt;</span></a></li>\n</ul></td></tr>';
	var iCount = 1;
	var iFirstDOM = (7+this.getFirstDOM()-g_startDay)%7; // to prevent calling it in a loop
	var iDaysInMonth = this.getDaysInMonth(); // to prevent calling it in a loop
	tmpStr += '<tr>';
	for (var i=0;i<7;i++){
		tmpStr += '<th style="font-size:11px;background: #56BAF4;">' + this.daysOfWeek[(g_startDay+i)%7] + '</th>';
	}
	var dtToday = new Date();
	tmpStr += '</tr></thead><tfoot><tr><td colspan="7" style="background: #56BAF4;">'+jsizraz_1111+': <a href="javascript:g_Calendar.clickToday()">' + (/^d/.test(this.dateFormat)?dtToday.getDate() + ' ' + this.months[dtToday.getMonth()] + ' ':this.months[dtToday.getMonth()] + ' ' + dtToday.getDate() + ', ') + dtToday.getFullYear()+'</a></td></tr></tfoot><tbody>';
	var tmpFrom = parseInt('' + this.dateFromYear + this.dateFromMonth + this.dateFromDay,10);
	var tmpTo = parseInt('' + this.dateToYear + this.dateToMonth + this.dateToDay,10);
	var tmpCompare;
	for (var j=1;j<=6;j++){
		tmpStr += '<tr>';
		for (i=1;i<=7;i++){
			var bShowDay = true;
			var sTitle = '';
			/*for(h = 0; h < aHolidays.length; h++) {
				if(aHolidays[h].date.getDate() == iCount) {
					if(!this.showHolidays)
						bShowDay = false;
					sTitle = aHolidays[h].desc;
				}
			}*/
			tmpStr += '<td ';
			if ( (7*(j-1) + i)>=iFirstDOM+1	&& iCount <= iDaysInMonth){
				if (iCount==this.day && this.year==this.oYear && this.month==this.oMonth){
					tmpStr += 'class="calHighlightColor"';
				} else if (i==7-g_startDay || i==((7-g_startDay)%7)+1) {
						tmpStr += 'class="calWeekend"';
				}
				tmpStr += '>';
				if(this.dayMask[i-1] == 0) {
					bShowDay = false;
				} else {
					tmpCompare = parseInt('' + this.year + this.padZero(this.month) + this.padZero(iCount),10);
					if (!(tmpCompare >= tmpFrom && tmpCompare <= tmpTo)) {
						bShowDay = false;
					}
				}
				if(bShowDay) {
					if (iCount==this.tDay && this.year==this.tYear && this.month==this.tMonth) {
						tmpStr += '<a class="today"' + (sTitle == ''?'':' title="' + sTitle + '"') + ' href="javascript: g_Calendar.clickDay(' + iCount + ');"><strong>' + iCount + '</strong></a>';
					} else {
						tmpStr += '<a' + (sTitle == ''?'':' title="' + sTitle + '"') + ' href="javascript: g_Calendar.clickDay(' + iCount + ');">' + iCount + '</a>';
					}
				} else {
					if (iCount==this.tDay && this.year==this.tYear && this.month==this.tMonth) {
						tmpStr += '<span' + (sTitle == ''?'':' title="' + sTitle + '"') + ' class="disabledtoday"><strong>' + iCount + '</strong></span>';
					} else {
						tmpStr += '<span' + (sTitle == ''?'':' title="' + sTitle + '"') + ' class="disabled">' + iCount + '</span>';
					}
				}
				iCount++;
			} else {
				if	(i==7-g_startDay || i==((7-g_startDay)%7)+1) {
					tmpStr += 'class="calWeekend"';
				}
				tmpStr += (sTitle == ''?'':' title="' + sTitle + '"') + 'class="disabled">&nbsp;';
			}
			tmpStr += '</td>';
		}
		tmpStr += '</tr>';
	}
	tmpStr += '</tbody>';
	tmpStr += '</table>';
	return tmpStr;
};
Calendar.prototype.setDateFormat = function(dateFormat){
	this.dateFormat = dateFormat || 'm/d/yyyy';
	dp_dateFormat = this.dateFormat;
};
Calendar.prototype.setDayMask = function(mask){
	var j = this.dayMask.length > mask.length?mask.length:this.dayMask.length;
	for(var i = 0; i < j; i++)
		this.dayMask[i] = mask[i];
	return this.dayMask;
};
Calendar.prototype.getFirstDOM = function() {
	var thedate = new Date();
	thedate.setDate(1);
	thedate.setMonth(this.month);
	thedate.setFullYear(this.year);
	return thedate.getDay();
};
Calendar.prototype.getDaysInMonth = function (){
	if (this.month!=1){
		return this.daysInMonth[this.month];
	}
	if (this.isLeapYear(this.year)){
		return 29;
	}
	return 28;
};
Calendar.prototype.isLeapYear = function(year) {
	if (year%4==0 && ((year%100!=0) || (year%400==0))) {
		return true;
	} else {
		return false;
	}
};
Calendar.prototype.selectChange = function(el) {
	this.month = el.selectedIndex;
	this.writeString(this.buildString());
};
Calendar.prototype.inputChange = function(el) {
	var tmp = el;
	if (tmp.value >=1900 && tmp.value <=2100) {
		this.year = tmp.value;
		this.writeString(this.buildString());
	} else {
		tmp.value = this.year;
	}
};
Calendar.prototype.changeYear = function(incr) {
	if (incr==1){
		this.year++;
	} else {
		this.year--;
	}
	this.writeString(this.buildString());
};
Calendar.prototype.changeMonth = function(incr){ 
	if (this.month==11 && incr==1) {
		this.month = 0;
		this.year++;
	} else if (this.month==0 && incr==-1) {
			this.month = 11;
			this.year--;
	} else if (incr==1) {
		this.month++;
	} else {
		this.month--;
	}
	this.writeString(this.buildString());
};
Calendar.prototype.clickDay = function(day) {
	form = document.getElementById(this.targetForm);
	//var el = document.getElementById(this.target);
	//if(el){
		//el.value = this.formatDateAsString(day,this.month,this.year);
		//el.value = day+'.'+(this.month+1)+'.'+this.year;
		//alert(form)
		if (this.target=="datumOd") {
			form.datumOd.value = day+'.'+(this.month+1)+'.'+this.year;
			form.pocD.selectedIndex=day-1
			form.pocM.selectedIndex=this.month
			form.pocY.selectedIndex=this.year-this.menuYearFrom
		}
		if (this.target=="datumDo") {
			form.datumDo.value = day+'.'+(this.month+1)+'.'+this.year;
			form.krajD.selectedIndex=day-1
			form.krajM.selectedIndex=this.month
			form.krajY.selectedIndex=this.year-this.menuYearFrom
		}
		/*if (this.target=="datumOd") {
			objPocD=document.getElementById("pocD");
			objPocM=document.getElementById("pocM");
			objPocY=document.getElementById("pocY");
			objPocD.selectedIndex=day-1
			objPocM.selectedIndex=this.month
			objPocY.selectedIndex=this.year-this.menuYearFrom
		}
		if (this.target=="datumDo") {
			objKrajD=document.getElementById("krajD");
			objKrajM=document.getElementById("krajM");
			objKrajY=document.getElementById("krajY");
			objKrajD.selectedIndex=day-1
			objKrajM.selectedIndex=this.month
			objKrajY.selectedIndex=this.year-this.menuYearFrom
		}*/
		try {updateDKB()}
		catch(e) {}
	//}
	this.hide();
};
Calendar.prototype.clickMonth = function(month){
	this.month = month;
	this.writeString(this.buildString());
};
Calendar.prototype.clickYear = function(year){
	this.year = year;
	this.writeString(this.buildString());
};

Calendar.prototype.clickToday = function(){
	var theDate = new Date();
	this.year = theDate.getFullYear();
	this.month = theDate.getMonth();
	this.writeString(this.buildString());
};
Calendar.prototype.writeString = function(str){
	this.containerLayer.innerHTML = str;
};
Calendar.prototype.hide = function(){
	calendarClearTimeout();
	this.containerLayer.style.visibility='hidden';
};
Calendar.prototype.isVisible = function() {
	return this.containerLayer.style.visibility=='visible' ? true : false;
};
Calendar.prototype.isHidden = function() {
	return this.containerLayer.style.visibility=='hidden' ? true : false;
};
Calendar.prototype.padZero = function(num) {
	return ((num <= 9) ? ("0" + num) : num);
};
Calendar.prototype.getWindowHeight = function() {
	var windowHeight=0;
	if (typeof(window.innerHeight)=='number') {
		windowHeight=window.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowHeight=document.documentElement.clientHeight;
	} else if (document.body && document.body.clientHeight) {
		windowHeight=document.body.clientHeight;
	}

	return windowHeight;
};
Calendar.prototype.openMenu = function(e) {
	var el = document.getElementById(e);
	if(!el) {
		return;
	}
	if(el.className == "open") {
		el.className = "";
	} else {
		el.className = "open";
	}
};
Calendar.prototype.closeMenu = function(e) {
	var el = document.getElementById(e);
	if(!el) {
		return;
	}
	el.className = "";
};
Calendar.prototype.openMenuMonths = function() {
	this.closeMenu('cp_years');
	this.openMenu('cp_months');
};
Calendar.prototype.openMenuYears = function() {
	this.closeMenu('cp_months');
	this.openMenu('cp_years');
};
Calendar.prototype.closeMenus = function() {
	this.closeMenu('cp_years');
	this.closeMenu('cp_months');
};
Calendar.prototype.handleClick = function(e) {
	if (!e){
		e = window.event;
	}
	if (e.target){
		elSrc = e.target;
	}else if (e.srcElement){
		elSrc = e.srcElement;
	}
	if (e.pageX || e.pageY) {
		x = e.pageX;
		y = e.pageY;
	} else if (e.clientX || e.clientY) {
		x = e.clientX + document.body.scrollLeft;
		y = e.clientY + document.body.scrollTop;
	}
	if(y > parseInt(g_Calendar.containerLayer.style.top,10) + 20) {
		g_Calendar.closeMenus();
	}
	e.cancelBubble = true;
	if (e.stopPropagation){
		e.stopPropagation();
	}
};
function calendarTimeout() {
	if(cp_timeoutDelay) {
		cp_timeoutId = setTimeout('g_Calendar.hide();',cp_timeoutDelay);
	}
}
function calendarClearTimeout() {
	if (cp_timeoutId) {
		clearTimeout(cp_timeoutId);
	}
}
function cp_handleDocumentClick(e){
	var elSrc, x, y;
	if(!g_Calendar.containerLayer){
		return;
	}
	if(g_Calendar.isHidden()) {
		return;
	}
	g_Calendar.hide();
}
Date.prototype.addDays = function(num){
	return new Date((num*(1000*60*60*24))+this.valueOf());
};
new Calendar(new Date());