function DatePicker(name)
{
     this.name = name;
     this.dt = new Date();
}

DatePicker.prototype.show = function(dt, x, y, callback)
{
     if(dt) this.dt = dt;
     this.callback = callback;
     // if not rendered yet, do so
     if(!this.oSpan)   this.render();
     // set coordinates
     this.oSpan.style.left = x;
     this.oSpan.style.top= y;
     this.fill();
     this.oSpan.style.visibility = "visible";
     this.oMonth.focus();
}

DatePicker.prototype.hide = function()
{
     if ( this.oSpan ) this.oSpan.style.visibility = "hidden";
}

DatePicker.prototype.render = function()
{
     var oT1, oTR1, oTD1, oTH1;
     var oT2, oTR2, oTD2;
     this.oSpan = document.getElementById(this.name);
     this.oSpan.appendChild(oT1 = document.createElement("table"));
     oT1.width = "160";
     oT1.border = 0;
     oTR1 = oT1.insertRow(oT1.rows.length);
     oTD1 = oTR1.insertCell(oTR1.cells.length);
     oTD1.colSpan = 7;
     oTD1.className = 'DatePickerHdr';
     oT2 = document.createElement("table");
     oT2.width = "100%";
     oTD1.appendChild(oT2);
     oT2.border = 0;
     // New row.
     oTR2 = oT2.insertRow(oT2.rows.length);
     // Previous month.
     oTD2 = oTR2.insertCell(oTR2.cells.length);
     oTD2.title = this.texts.prevMonth;
     oTD2.onclick = function() { this.oDatePicker.onPrev(); }
     oTD2.oDatePicker = this;
     oTD2.innerHTML = "<img src=\"sites/hrc/js/datepicker/images/datepicker/prev.gif\">";
     oTD2.className = 'DatePickerHdrBtn';
     // Month combo.
     oTD2 = oTR2.insertCell(oTR2.cells.length);
     oTD2.title = this.texts.monthTitle;
     this.oMonth = document.createElement("select");
     oTD2.appendChild(this.oMonth);
     this.oMonth.oDatePicker = this;
     this.oMonth.onchange = this.oMonth.onkeyup = function() { this.oDatePicker.onMonth(); }
     for(var i = 0; i < 12; i++)
     {
		this.oMonth.add(new Option(this.texts.months[i], i),undefined);
     }
     this.oMonth.className = 'DatePickerHdrBtn';
     // Year combo.
     oTD2 = oTR2.insertCell(oTR2.cells.length);
     oTD2.title = this.texts.yearTitle;
     this.oYear = document.createElement("select");
     oTD2.appendChild(this.oYear);
     this.oYear.oDatePicker = this;
     this.oYear.onchange = this.oYear.onkeyup = function() { this.oDatePicker.onYear(); }
     for(i = this.dt.getFullYear()-70; i < this.dt.getFullYear()+10; i++)
     {
		this.oYear.add(new Option(i, i),undefined);
     }
     this.oYear.className = 'DatePickerHdrBtn';
     // Next month.
     oTD2 = oTR2.insertCell(oTR2.cells.length);
     oTD2.title = this.texts.nextMonth;
     oTD2.onclick = function() { this.oDatePicker.onNext(); }
     oTD2.oDatePicker = this;
     oTD2.innerHTML = "<img src=\"sites/hrc/js/datepicker/images/datepicker/next.gif\">";
     oTD2.className = 'DatePickerHdrBtn';
	 // Close button.
     oTD2 = oTR2.insertCell(oTR2.cells.length);
     oTD2.title = this.texts.close;
     oTD2.onclick = function() { this.oDatePicker.hide(); }
     oTD2.oDatePicker = this;
     oTD2.innerHTML = "<img src=\"sites/hrc/js/datepicker/images/datepicker/close.gif\">";
     oTD2.className = 'DatePickerHdrBtn';
     oTD2.title = this.texts.close;
	 // Next rows (dates).
     oTR1 = oT1.insertRow(oT1.rows.length);
     for ( i = 0; i < 7; i++ )
     {
        oTH1 = document.createElement("th");
        oTR1.appendChild(oTH1);
        oTH1.innerHTML = this.texts.days[i];
        oTH1.className = 'dayheader d' + (i+1);
     }
     this.aCells = new Array;
     for ( var j = 0; j < 6; j++ )
     {
        this.aCells.push(new Array);
        oTR1 = oT1.insertRow(oT1.rows.length);
        oTR1.className="days";
        for ( i = 0; i < 7; i++ )
        {
           this.aCells[j][i] = oTR1.insertCell(oTR1.cells.length);
           this.aCells[j][i].oDatePicker = this;
           this.aCells[j][i].onclick =
              function() { this.oDatePicker.onDay(this); }
        }
     }
}

DatePicker.prototype.fill = function()
{
     // first clear all
     this.clear();
     // place the dates in the calendar
     var nRow = 0;
     var d = new Date(this.dt.getTime());
     var m = d.getMonth();
     for ( d.setDate(1); d.getMonth() == m; d.setTime(d.getTime() + 86400000) ) {
        var nCol = d.getDay();
        if(nCol == 0) nCol = 7;
        nCol = nCol-1;
        this.aCells[nRow][nCol].innerHTML = d.getDate();
        if ( d.getDate() == this.dt.getDate() ) {
           this.aCells[nRow][nCol].className = 'DatePickerBtnSelect';
        }
        if ( nCol == 6 ) nRow++;
     }
     // set the month combo
     this.oMonth.value = m;
     // set the year text
     this.oYear.value = this.dt.getFullYear();
}

DatePicker.prototype.clear = function()
{
	for(var j = 0; j < 6; j++)
	{
		for(var i = 0; i < 7; i++)
        {
           this.aCells[j][i].innerHTML = "&nbsp;"
           this.aCells[j][i].className = 'DatePickerBtn';
		}
	}
}

DatePicker.prototype.onPrev = function()
{
     if(this.dt.getMonth() == 0)
     {
        this.dt.setFullYear(this.dt.getFullYear()-1);
        this.dt.setMonth(11);
     }
     else
     {
        this.dt.setMonth(this.dt.getMonth()-1);
     }
     this.fill();
}

DatePicker.prototype.onNext = function()
{
     if(this.dt.getMonth() == 11)
     {
        this.dt.setFullYear(this.dt.getFullYear()+1);
        this.dt.setMonth(0);
     }
     else
     {
        this.dt.setMonth(this.dt.getMonth()+1);
     }
     this.fill();
}

DatePicker.prototype.onMonth = function()
{
     this.dt.setMonth(this.oMonth.value);
     this.fill();
}

DatePicker.prototype.onYear = function()
{
     this.dt.setYear(this.oYear.value);
     this.fill();
}

DatePicker.prototype.onDay = function(oCell)
{
     var d = parseInt(oCell.innerHTML);
     if(d > 0)
     {
        this.dt.setDate(d);
        this.hide();
        
        this.client.value = this.dt.getDate()+"."+(this.dt.getMonth()+1)+"."+this.dt.getFullYear();
        //this.client.value = this.dt;
        //this.callback(this.dt);
     }
}

DatePicker.prototype.onToday = function()
{
	this.dt = new Date();
	this.fill();
}

DatePicker.prototype.onYesterday = function()
{
	today = new Date();
	if(today.getDate() == 1)
	{
		if(today.getMonth() == 1)
		{
			this.dt.setYear(today.getFullYear()-1);
			this.dt.setMonth(11);
			this.dt.setDate(31);
		}
		else
		{
			this.dt.setYear(today.getFullYear());
			this.dt.setMonth(today.getMonth()-1);
			this.dt.setDate(getDaysInMonth(today.getMonth()-1));
		}
	}
	else
	{
		this.dt.setYear(today.getFullYear());
		this.dt.setMonth(today.getMonth());
		this.dt.setDate(today.getDate()-1);
	}
	this.fill();
}

DatePicker.prototype.onTomorrow = function()
{
	today = new Date();
	if(today.getDate() == getDaysInMonth(today))
	{
		if(today.getMonth() == 11)
		{
			this.dt.setYear(today.getFullYear()+1);
			this.dt.setMonth(0);
			this.dt.setDate(1);
		}
		else
		{
			this.dt.setYear(today.getFullYear());
			this.dt.setMonth(today.getMonth()+1);
			this.dt.setDate(1);
		}
	}
	else
	{
		this.dt.setYear(today.getFullYear());
		this.dt.setMonth(today.getMonth());
		this.dt.setDate(today.getDate()+1);
	}
	this.fill();
}

DatePicker.prototype.onWeekAgo = function()
{
	today = new Date();
	if(today.getDate() < 8)
	{
		if(today.getMonth() == 1)
		{
			this.dt.setYear(today.getFullYear()-1);
			this.dt.setMonth(11);
			this.dt.setDate(31+today.getDate()-7);
		}
		else
		{
			this.dt.setYear(today.getFullYear());
			this.dt.setMonth(today.getMonth()-1);
			this.dt.setDate(getDaysInMonth(today)+today.getDate()-7);
		}
	}
	else
	{
		this.dt.setYear(today.getFullYear());
		this.dt.setMonth(today.getMonth());
		this.dt.setDate(today.getDate()-7);
	}
	this.fill();
}

DatePicker.prototype.onWeekAfter = function()
{
	today = new Date();
	if(today.getDate()+7 > getDaysInMonth(today))
	{
		if(today.getMonth() == 11)
		{
			this.dt.setYear(today.getFullYear()+1);
			this.dt.setMonth(0);
			this.dt.setDate(today.getDate()+7-getDaysInMonth(today));
		}
		else
		{
			this.dt.setYear(today.getFullYear());
			this.dt.setMonth(today.getMonth()+1);
			this.dt.setDate(today.getDate()+7-getDaysInMonth(today));
		}
	}
	else
	{
		this.dt.setYear(today.getFullYear());
		this.dt.setMonth(today.getMonth());
		this.dt.setDate(today.getDate()+7);
	}
	this.fill();
}

DatePicker.prototype.onMonthAgo = function()
{
	today = new Date();
	if(today.getMonth() == 1)
	{
		this.dt.setYear(today.getFullYear()-1);
		this.dt.setMonth(11);
		this.dt.setDate(today.getDate());
	}
	else
	{
		this.dt.setYear(today.getFullYear());
		if(today.getDate() > getDaysInMonth(new Date(today.getFullYear(), today.getMonth()-1, 1)))
		{
			this.dt.setMonth(today.getMonth()-1);
			this.dt.setDate(getDaysInMonth(new Date(today.getFullYear(), today.getMonth()-1, 1)));
		}
		else
		{
			this.dt.setMonth(today.getMonth()-1);
			this.dt.setDate(today.getDate());
		}
	}
	this.fill();
}

DatePicker.prototype.onMonthAfter = function()
{
	today = new Date();
	if(today.getMonth() == 11)
	{
		this.dt.setYear(today.getYear()+1);
		this.dt.setMonth(0);
		this.dt.setDate(today.getDate());
	}
	else
	{
		this.dt.setYear(today.getFullYear());
		this.dt.setMonth(today.getMonth()+1);
		this.dt.setDate(today.getDate());
	}
	this.fill();
}

DatePicker.prototype.texts = {
months: [ "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" ],
close: "close",
days: ["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
monthTitle: "Month",
prevMonth: "prev month",
nextMonth: "next month",
yearTitle: "year",
today: "today",
yesterday: "yesterday",
tomorrow: "tomorrow",
weekAgo: "last week",
weekAfter: "next week",
monthAgo: "nextmonth",
monthAfter: "prev month"
}
function callback(dt)
{
     oDatePicker.client.value = dt.getDate()+"."+(dt.getMonth()+1)+"."+dt.getFullYear();
}

function getElementPosition(elem)
{
    var offsetTrail = elem;
    var offsetLeft = 0;
    var offsetTop = 0;
    while(offsetTrail)
    {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if ((navigator.userAgent.indexOf("Mac") != -1)&&(typeof document.body.leftMargin != "undefined"))
    {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return {left:offsetLeft, top:offsetTop};
}

function DatePickerShow(oTxt)
{
     if(!document.getElementById) return;
     // since we control the text format in callback(), getting the date is easy
	 var aDt = oTxt.value;
     if(aDt.indexOf(".")>0)
     {
      aDt = oTxt.value.split(".");
     }else{
      aDt = oTxt.value.split("/");
     }
     var dt = null;
     if(aDt && (aDt.length == 3))
     {
        dt = new Date();
		dt.setFullYear(parseInt(aDt[2]));
		dt.setMonth(parseInt(aDt[1])-1);
		dt.setDate(parseInt(aDt[0]));
     }
	if(document.getElementById('theDatePicker'))
	{
	    var span = document.getElementById('theDatePicker');
		span.innerHTML = '';
	}
	else
	{
	    var span = document.createElement("span");
		span.id = "theDatePicker";
		span.className = "DatePickerJS";
		document.body.appendChild(span);
	}
	 var oDatePicker = new DatePicker('theDatePicker');
     oDatePicker.client = oTxt;
     pos = getElementPosition(oTxt);
	 if(pos.left > 1000-180) pos.left = 1000-170;
     oDatePicker.show(dt, pos.left, pos.top + oTxt.offsetHeight , callback);
}

