// 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 == false) {
		enteredDate = '';
	    if (document.getElementById(this.id).value != '' || this.altField == '' || this.altField == null) 
            enteredDate = document.getElementById(this.id).value;
        else if (document.getElementById(this.altField).value != '') 
            enteredDate = document.getElementById(this.altField).value;
	    var dateRegExp = /^(\d{4})(\-)(\d{1,2})\2(\d{1,2})$/;
	    if (enteredDate.match(dateRegExp)) {
			enteredDate.replace('/', '-');
			dateArray = enteredDate.split('-');
		    d.setDate(dateArray[2]);
		    d.setMonth(dateArray[1]-1);
		    d.setFullYear(dateArray[0]);
		    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);
	this.firstDay = d.getDay();
	// 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.nextDay = 1;
	this.nextMonth = this.dateObject.getMonth();
	this.nextYear = this.dateObject.getFullYear();
	this.dateObject.setMonth(this.nextMonth-1, 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 += '<table id="cal' + this.id + '" cellspacing=0 bgcolor="#000066"><tr><td>';
	    calString += '<table cellspacing=1 cellpadding=2 bgcolor="#FFFFFF" border=0>';
	    calString += '<tr class="innertableheadline">';
	    calString += '<td><a href="Javascript:changeMonth(-1,\'' + this.divid + '\')"><img src="' + imgPath + 'arrow-left.gif" alt="previous month" border=0></a></td>';
	    calString += '<td colspan=5 align=center class="innertableheadline">' + months[this.month] + ' ' + this.year + '</td>';
	    calString += '<td align=right><a href="Javascript:changeMonth(1,\'' + this.divid + '\')"><img src="' + imgPath + 'arrow-right.gif" alt="next month" border=0></a></td></tr>';
	    calString += '<tr>';
	
	    for (i=0; i<days.length; i++) {
		    calString += '<td class="innertableheadline">' + days[i] + '</td>';
	    }
	
	    calString += '</tr>';
	
	    // main calendar
	    var weekStartDay = 0;
	    calString += '<tr>';
	    for (j=0; j<42; j++) {
		    var displayNum = (j-this.firstDay+1);
			if (displayNum == this.date && currentDayDifferent == true) {
				tdclass = 'innertableheadline';
		    } else if (j%7==6 || j%7==0) {
			    tdclass = 'weekend';
		    } else {
			    tdclass = 'month';
		    }
		    if (j<this.firstDay) {
			    calString += '<td class="nomonth" align=center><a href="Javascript:changeDate(\'' + this.previousYear + '-' + (this.previousMonth+1) + '-' + this.daysPreviousMonth + '\',\'' + this.id +'\',\'' + this.divid +'\')" class="nomonth">' + this.daysPreviousMonth + '</a></td>';
			    this.daysPreviousMonth++;
		    } else if(displayNum==this.date) {
			    calString += '<td class="' + tdclass + '" align=center><a href="Javascript:changeDate(\'' + this.year + '-' + (this.month+1) + '-' + displayNum + '\',\'' + this.id +'\',\'' + this.divid +'\')" class="' + tdclass + '">' + displayNum + '</a></td>';
		    } else if(displayNum>this.length) {
			    calString += '<td class="nomonth" align=center><a href="Javascript:changeDate(\'' + this.nextYear + '-' + (this.nextMonth+1) + '-' + this.nextDay + '\',\'' + this.id +'\',\'' + this.divid +'\')" class="nomonth">' + this.nextDay + '</a></td>';
				this.nextDay++;
		    } else {
			    calString += '<td class="' + tdclass + '" align=center><a href="Javascript:changeDate(\'' + this.year + '-' + (this.month+1) + '-' + displayNum + '\',\'' + this.id +'\',\'' + this.divid +'\')" class="' + tdclass + '">' + displayNum + '</a></td>';
		    }
		    if (j%7==6) {
			    if (j < 41 && displayNum < this.length) {
			        calString += '</tr><tr>';
			    } else {
				    j = 42;
			    }
		    }
	    }
	    calString += '</tr>';
	
	    calString += '<tr><td colspan=7><a href="Javascript:hideCalendar(\'' + this.divid + '\');">' + calendarClose + '</a></td></tr>';
	
	    calString += '</table></td></tr></table>';

        return calString;
    }

}

// function for change date
function changeDate(date, calid, divid) {
	document.getElementById(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();
}

var currentDayDifferent = false;

