// Javascript calendar class

// Constructor
function calendar(id, d, divid, altField, noCheck) {
	this.id = id;
	this.dateObject = d;
	this.divid = divid;
	this.write = writeCalendar;
	if (altField != null) {
	    this.altField = altField;
	}
	// check if there is already a date entered and start there
	if (!noCheck == true) {
		enteredDate = '';
	    if (eval(this.id+'.value') != '' || this.altField == '' || this.altField == null) 
            enteredDate = eval(this.id+'.value');
        else if (eval(this.altField+'.value') != '') 
            enteredDate = eval(this.altField+'.value');
	    var dateRegExp = /^(\d{1,2})(\-|\/|\.)(\d{1,2})\2(\d{4})$/;
	    if (enteredDate.match(dateRegExp)) {
			enteredDate.replace('-', '.');
			enteredDate.replace('/', '.');
			dateArray = enteredDate.split('.');
		    d.setDate(dateArray[0]);
		    d.setMonth(dateArray[1]-1);
		    d.setFullYear(dateArray[2]);
		    this.dateObject = d;
	    }
	}
	// settings for current month
	this.dateObject.setMonth(d.getMonth());
	this.month = d.getMonth();
	this.length = getLength(this.month, this.dateObject);
	this.date = d.getDate();
	this.day = d.getDay();
	this.year = d.getFullYear();
	d.setDate(1);
	// adjust firstday of the week to Monday instead of Sunday
	this.firstDay = d.getDay()-1;
	if (this.firstDay < 0) {
		this.firstDay += 7;
	}
	// settings for previous month
	this.dateObject.setMonth(this.month-1,1);
	this.daysPreviousMonth = getLength(this.dateObject.getMonth(), this.dateObject);
	this.previousMonth = this.dateObject.getMonth();
	this.previousYear = this.dateObject.getFullYear();
	this.dateObject.setMonth(this.previousMonth+1, this.date);
    this.daysPreviousMonth = this.daysPreviousMonth - this.firstDay + 1;
	// settings for next month
	this.dateObject.setMonth(this.month+1,1);
this.daysNextMonth = getLength(this.dateObject.getMonth(), this.dateObject);
	this.nextDay = 1;
	this.nextMonth = this.dateObject.getMonth();
	this.nextYear = this.dateObject.getFullYear();
	this.dateObject.setMonth(this.nextMonth-1, this.date);
// then reset the date object to the correct date d.setDate(this.date); 	
	
	d.setDate(this.date);



    function getLength(month, dateObject) {
        switch(month) {
	        case 1:
		        if ((dateObject.getFullYear()%4==0 &&
			         dateObject.getFullYear()%100!=0) ||
				     dateObject.getFullYear()%400==0)
				    return 29;
			    else
			        return 28;
		    case 3:
		        return 30;
		    case 5:
		        return 30;
		    case 8:
		        return 30;
		    case 10:
		        return 30;
		    default:
		        return 31;
	    }
    }

    function writeCalendar() {
	    var calString = '';
		calString = '<div id="cal' + this.id + '" style="clear:right;position:absolute;left:5px">';
	    
calString += '<div class="calendarCon1cal">';
calString += '<table class="calZagBack" border="0" cellspacing="0" cellpadding="0" width="100%">';
  calString += '<tr>';
   calString += '<td width="15"><a href="Javascript:changeMonth(-1,\'' + this.divid + '\')"><img border="0" src="/img/banners/nav/arrow_left.gif"></a></td>';
    calString += '<td width="15"><a href="Javascript:changeMonth(1,\'' + this.divid + '\')"><img border="0" src="/img/banners/nav/arrow_right.gif"></a></td>';
calString += '<td width="15"><a href="Javascript:hideCalendar(\'' + this.divid + '\')"><img border="0" src="/img/banners/nav/button_close.gif"></a></td>';
    calString += '<td class="calZaglavlje" nowrap>'+months[this.month] + ' ' + this.year+'</td>';

  calString += '</tr>'
calString += '</table>'


	 calString += '<div class="datumiRow1">';
	    for (i=0; i<days.length; i++) {
		    calString += '<span class="imeDana">' + days[i] + '</span>';
	    }
	    calString += '</div>';

	
	    // main calendar
	    var weekStartDay = 0;
	    calString += '<div class="datumiRow1">';
	    for (j=0; j<42; j++) {
		    var displayNum = (j-this.firstDay+1);
		    if (j%7>=5) {
			    tdclass = 'calWeek';
		    } else {
			    tdclass = 'calActiv';
		    }
calString +='<span class="danCont">';
		    if (j<this.firstDay) {
			    calString += '<span class="datSivo"><a href="Javascript:changeDate(\'' + this.daysPreviousMonth + '.' + (this.previousMonth+1) + '.' + this.previousYear + '\',\'' + this.id +'\',\'' + this.divid +'\')" class="datSivo">' + this.daysPreviousMonth + '</a></span>';
			    this.daysPreviousMonth++;
		    } else if(displayNum>this.length) {
			    calString += '<span class="datSivo" align=right><a href="Javascript:changeDate(\'' + this.nextDay + '.' + (this.nextMonth+1) + '.' + this.nextYear + '\',\'' + this.id +'\',\'' + this.divid +'\')" class="datSivo">' + this.nextDay + '</a></span>';
				this.nextDay++;
		    } else {
			    calString += '<span class="' + tdclass + '"><a href="Javascript:changeDate(\'' + displayNum + '.' + (this.month+1) + '.' + this.year + '\',\'' + this.id +'\',\'' + this.divid +'\')" class="' + tdclass + '">' + displayNum + '</a></span>';
		    }
calString += '</span>';

		    if (j%7==6) {
			    if (j < 41 && displayNum < this.length) {
			        calString += '</div><div class="datumiRow1">';
			    } else {
				    j = 42;
			    }
		    }
	    }
	    calString += '</div>';
	
	
	    calString += '</div></div>';

        return calString;
    }

}

// function for change date
function changeDate(date, calid, divid) {
	calid = eval(calid);
	calid.value = date;
	hideCalendar(divid);
}

// function for change month
function changeMonth(mo, divid) {
	cal = eval(divid);
	cal.dateObject.setMonth(cal.dateObject.getMonth() + mo);
	cal = new calendar(cal.id, cal.dateObject, divid, null, true);
	document.getElementById(divid).innerHTML = cal.write();
}

// function for calendar refresh
function refreshMonth(divid) {
	cal = eval(divid);
	cal = new calendar(cal.id, cal.dateObject, divid, cal.altField, false);
	document.getElementById(divid).innerHTML = cal.write();
}



