function wdCalendar(divid,obj,showTag,aktDate){//class
	this.obj = obj;
	this.firstdayofweek = 1;
	this.monthsShort = new Array();
	this.monthsLong = new Array();
	this.daysShort = new Array();
	this.daysMedium = new Array();
	this.daysLong = new Array();
	this.setMonths = setMonths;
	this.setDays = setDays;
	//this.months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	//this.days = new Array("Mo","Tu","We","Th","Fr","Sa","Su");
	this.today = new Date()
	this.today = new Date(this.today.getFullYear(),this.today.getMonth(),this.today.getDate())
	this.aktDate = aktDate.getTime();
	this.divid = divid; //div or span
	this.oldDay;
	this.day = aktDate.getDate();
	this.year = aktDate.getFullYear();
	this.month =aktDate.getMonth();
	this.showTag = showTag; 
	this.hiddenTag = showTag;
	this.path = "";
	//Methods
	this.draw = draw;
	this.getDaysInMonth = getDaysInMonth;
	this.isLeapYear = isLeapYear;
	this.showDate = showDate;
	this.nextMonth = nextMonth;
	this.prevMonth = prevMonth;
	this.gotoToday = gotoToday
	this.dateAsString = dateAsString;
	this.systemDate = systemDate;
	this.setFormat = setFormat;
	this.callBack;
	this.hide = hide;
	this.show = show;
	this.swap = swap;
	this.format = "d.m.Y";
	this.stringClose = "Close";
	this.stringToday = "Today";
}

function isLeapYear(){
	Y=this.year;
	return (!(Y%4)&&(Y%100)) || !(Y%400);
}

function getDaysInMonth(){
	m=this.month+1;
	if (m==1||m==3||m==5||m==7||m==8||m==10||m==12)  return 31;
	else if (m==4 || m==6 || m==9 || m==11) return 30;
	else if (m==2){
		if (this.isLeapYear())return 29;
		else return 28;
	}
}
function nextMonth(){
	this.month = ++this.month%12;
	if(!this.month)
		this.year++;
	this.draw();
}
function prevMonth(){
	if(--this.month < 0){
		this.month = 11;
		this.year--;
	}
	this.draw();
}
function gotoToday(){
	this.day = this.today.getDate()
	this.year = this.today.getFullYear()
	this.month =this.today.getMonth()
	this.aktDate = this.today.getTime()
	this.draw()
//	alert(this.aktDate)
}

function draw(){
	var str_cal = "";
	if (this.path == "") this.path = "../../_img/";
	str_cal = '<table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" class="calendarTable"><tr><td class="calMonth" align="center"><a href="javascript:'+this.obj+'.prevMonth();" title="Previous month"><img src="' + this.path + 'but-arr-cal-left.gif" border="0"></a></td>';
	str_cal += '<td class="calMonth" align="center" colspan="5">'+this.monthsLong[this.month]+' / '+this.year+'</td>';
	str_cal += '<td class="calMonth" align="center"><a href="javascript:'+this.obj+'.nextMonth();" title="Next month"><img src="' + this.path + 'but-arr-cal-right.gif" border="0"></a></td></tr>';
	str_cal += "<tr>";
	for (var i = 0; i < 7; i++) {
		str_cal += "<td class='calendar'>"+ this.daysShort[i] +"</td>"
	}
	str_cal += "</tr>";
	var d2 = new Date(this.year,this.month,1);
	var dow,weeks=0;	
	for(i=1; i<=this.getDaysInMonth(); i++){
		d2.setDate(i);
		dow = d2.getDay();
		if (d2.getTime()== this.aktDate && d2.getTime()== this.today.getTime()) cssStyle = " class='calSelectedToday'";
		else if (d2.getTime()== this.aktDate) cssStyle = " class='calSelectedDay'"
		else if (d2.getTime()== this.today.getTime()) cssStyle = " class='calToday'"
		else cssStyle=" class='calday'"
		
		if (i==1) for(j=1; j<=(dow-this.firstdayofweek+7)%7; j++) str_cal += '<td class="calday">&nbsp;</td>';
		if ((dow == this.firstdayofweek) && (i > 1)){
			str_cal += "</tr><tr>";
			weeks++;
		}
		str_cal += '<td '+cssStyle+'><a href="javascript:'+this.obj+'.showDate('+i+');">'+i+'</a></td>';
	}
	
	for (j=1; j <= 6-((dow-this.firstdayofweek+7)%7); j++)
		str_cal += '<td class="calday">&nbsp;</td>';
	if (weeks <= 4){
		str_cal += "</tr><tr class='calday'>";
		for(j=1; j<=7;j++) str_cal+='<td class="calday">&nbsp;</td>';
	}
	str_cal += '</tr><tr><td class="calMonth" colspan="3"><a href="javascript:'+this.obj+'.gotoToday();">'+this.stringToday+'</a></td><td class="calMonth" colspan="4"><a href="javascript:'+this.obj+'.hide();">'+this.stringClose+'</a></td></tr></table>';
	document.getElementById(this.divid).innerHTML = str_cal;
}

function setMonths(months_) {
	for (var i = 0; i < 12; i++) {
		this.monthsLong[i] = months_[i];
		this.monthsShort[i] = months_[i].substring(0,3);
	}
}

function setDays(days_) {
	for (var i = 0; i < 7; i++) {
		this.daysLong[i] =days_[i];
		this.daysMedium[i] = days_[i].substring(0,3);
		this.daysShort[i] = days_[i].substring(0,2);
	}
}

function dateAsString() {
	var result = this.format;
	result = result.replace(new RegExp("F", "gi"), "FFFFF");
	result = result.replace(new RegExp("l", "gi"), "lllll");
	if (result.indexOf("Y") >= 0) {
		result = result.replace(new RegExp("Y", "gi"), this.year);
	} else if (result.indexOf("y") >= 0) {
		var year = ""+this.year;
		result = result.replace(new RegExp("y", "gi"), year.substring(2,4));
	}
	if (result.indexOf("m") >= 0) {
		if ((this.month + 1) < 10) result = result.replace(new RegExp("m", "gi"), "0" + (this.month + 1));
		else result = result.replace(new RegExp("m", "gi"), this.month + 1);
	}
	result = result.replace(new RegExp("n", "gi"), this.month + 1);
	if (result.indexOf("d") >= 0) {
		if ((this.day) < 10) result = result.replace(new RegExp("d", "gi"), "0" + this.day);
		else result = result.replace(new RegExp("d", "gi"), this.day);
	}
	result = result.replace(new RegExp("j", "gi"), this.day);
	if (result.indexOf("S")) {
		var suffix;
			if (this.day == 1) suffix = "st";
			else if (this.day == 2) suffix = "nd";
			else if (this.day == 3) suffix = "rd";
			else suffix = "th";
		result = result.replace(new RegExp("S", "gi"), suffix);
	}
	result = result.replace(new RegExp("FFFFF", "gi"), this.monthsLong[this.month]);
	if (result.indexOf("lllll")) {
		var date = new Date(this.year, this.month, this.day);
		result = result.replace(new RegExp("lllll", "gi"), this.daysLong[date.getUTCDay()]);
	}
	return result;
}

function systemDate() {
	return this.year+"-"+((this.month+1 < 10) ? "0"+(this.month+1) : this.month+1)+"-"+((this.day < 10) ? "0"+this.day : this.day);
}

function setFormat(format_) {
	this.format = format_;
}

function showDate(d_){
	this.oldDay = this.day;
	this.day = d_;
//		alert(this.showTag.value);
	this.showTag.value = this.dateAsString();
	this.hiddenTag.value = this.systemDate();
	this.aktDate = new Date(this.year,this.month,this.day).getTime()
	this.draw()
	//this.hide();
//	document.getElementById("hid_"+this.showTag.id).value = this.dateAsString();
	if (this.callBack) this.callBack();
//	alert(document.getElementById("dateDisplayed").value);
//	alert(document.getElementById("date").value);
}

function show(){
	document.getElementById(this.divid).style.display = "block";
	document.getElementById(this.divid).style.position = "absolute";
	document.getElementById(this.divid).style.top = tempY + "px";
	document.getElementById(this.divid).style.left = (tempX + 10) + "px";
	//alert("x="+tempX+" y="+tempY+" scrollTop="+document.body.scrollTop);
}
function hide(){
	document.getElementById(this.divid).style.display = "none";	
}

function swap(){
	if (document.getElementById(this.divid).style.display == "none") this.show();
	else this.hide();
}


var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;

// Temporary variables to hold mouse x-y pos.s
var tempX = 0;
var tempY = 0;

// Main function to retrieve mouse x-y pos.s

function getMouseXY(e) {
  if (IE) {
    tempX = event.clientX + document.documentElement.scrollLeft;
    tempY = event.clientY + document.documentElement.scrollTop
  }
	else {
    tempX = e.pageX
    tempY = e.pageY
  }  
  if (tempX < 0) {
		tempX = 0
	}
  if (tempY < 0) {
		tempY = 0
	}
  return true
}

function calendar(iid) {
	var dat = document.getElementById(iid).value.split("-");
	if (dat[0] != "0000") date = new Date(dat[0], dat[1] - 1, dat[2]);
	else date = new Date();
	cal.aktDate = date.getTime();
	cal.day = date.getDate();
	cal.year = date.getFullYear();
	cal.month = date.getMonth();
	if (cal.showTag == document.getElementById(iid)) {
		cal.swap();
	} else {
		cal.hide();
		cal.hiddenTag = document.getElementById(iid);
		cal.showTag = document.getElementById(iid+"Displayed");
		cal.show();
		cal.draw();
	}
}

