var weburl = "http://fieo.org/";
function DatePicker(name)
{
     this.name = name;
     this.dt = new Date();
     document.write('<span id="'+name+'" name="'+name+'" style="z-index:1; position:absolute; visibility:hidden" class="DatePicker"></span>');
}

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;//x-179;
     this.oSpan.style.top= (y+15);
     this.fill();
	// this.oSpan.style.position = "relative";
     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 = "200";
     oT1.border = 1;
     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=\""+weburl+"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 = 'DatePickerHdrSelect';
     // 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()-100; i < this.dt.getFullYear()+25; i++)
     {
		this.oYear.add(new Option(i, i),undefined);
     }
     this.oYear.className = 'DatePickerHdrSelect';
     // 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=\""+weburl+"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=\""+weburl+"datepicker/images/datepicker/close.gif\">";
     oTD2.className = 'DatePickerHdrBtn';
     oTD2.title = "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 = 'DatePicker';
     }
     this.aCells = new Array;
     for ( var j = 0; j < 6; j++ )
     {
        this.aCells.push(new Array);
        oTR1 = oT1.insertRow(oT1.rows.length);
        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); }
        }
     }
     // New buttons.
     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);
     // Month ago button.
     oTD2 = oTR2.insertCell(oTR2.cells.length);
     oTD2.title = this.texts.monthAgo;
     oTD2.align = "center";
     oTD2.onclick = function() { this.oDatePicker.onMonthAgo(); }
     oTD2.oDatePicker = this;
     oTD2.innerHTML = "<img src=\"datepicker/images/datepicker/monthago.gif\">";
     oTD2.className = 'DatePickerHdrBtn';
     // Week ago button.
     oTD2 = oTR2.insertCell(oTR2.cells.length);
     oTD2.title = this.texts.weekAgo;
     oTD2.align = "center";
     oTD2.onclick = function() { this.oDatePicker.onWeekAgo(); }
     oTD2.oDatePicker = this;
     oTD2.innerHTML = "<img src=\"datepicker/images/datepicker/weekago.gif\">";
     oTD2.className = 'DatePickerHdrBtn';
     // Yesterday button.
     oTD2 = oTR2.insertCell(oTR2.cells.length);
     oTD2.title = this.texts.yesterday;
     oTD2.align = "center";
     oTD2.onclick = function() { this.oDatePicker.onYesterday(); }
     oTD2.oDatePicker = this;
     oTD2.innerHTML = "<img src=\"datepicker/images/datepicker/yesterday.gif\">";
     oTD2.className = 'DatePickerHdrBtn';
     // Today button.
     oTD2 = oTR2.insertCell(oTR2.cells.length);
     oTD2.title = this.texts.today;
     oTD2.align = "center";
     oTD2.onclick = function() { this.oDatePicker.onToday(); }
     oTD2.oDatePicker = this;
     oTD2.innerHTML = "<img src=\"datepicker/images/datepicker/today.gif\">";
     oTD2.className = 'DatePickerHdrBtn';
     // Tomorrow button.
     oTD2 = oTR2.insertCell(oTR2.cells.length);
     oTD2.title = this.texts.tomorrow;
     oTD2.align = "center";
     oTD2.onclick = function() { this.oDatePicker.onTomorrow(); }
     oTD2.oDatePicker = this;
     oTD2.innerHTML = "<img src=\"datepicker/images/datepicker/tomorrow.gif\">";
     oTD2.className = 'DatePickerHdrBtn';
     // Week after button.
     oTD2 = oTR2.insertCell(oTR2.cells.length);
     oTD2.title = this.texts.weekAfter;
     oTD2.align = "center";
     oTD2.onclick = function() { this.oDatePicker.onWeekAfter(); }
     oTD2.oDatePicker = this;
     oTD2.innerHTML = "<img src=\"datepicker/images/datepicker/weekafter.gif\">";
     oTD2.className = 'DatePickerHdrBtn';
     // Month after button.
     oTD2 = oTR2.insertCell(oTR2.cells.length);
     oTD2.title = this.texts.monthAfter;
     oTD2.align = "center";
     oTD2.onclick = function() { this.oDatePicker.onMonthAfter(); }
     oTD2.oDatePicker = this;
     oTD2.innerHTML = "<img src=\"datepicker/images/datepicker/monthafter.gif\">";
     oTD2.className = 'DatePickerHdrBtn';*/
}

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.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(this.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() == this.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(this.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 > this.getDaysInMonth(today))
	{
		if(today.getMonth() == 11)
		{
			this.dt.setYear(today.getFullYear()+1);
			this.dt.setMonth(0);
			this.dt.setDate(today.getDate()+7-this.getDaysInMonth(today));
		}
		else
		{
			this.dt.setYear(today.getFullYear());
			this.dt.setMonth(today.getMonth()+1);
			this.dt.setDate(today.getDate()+7-this.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() > this.getDaysInMonth(new Date(today.getFullYear(), today.getMonth()-1, 1)))
		{
			this.dt.setMonth(today.getMonth()-1);
			this.dt.setDate(this.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: [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ],
     close: "Close",
     days: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
     monthTitle: "Month",
     prevMonth: "Last month",
     nextMonth: "Next month",
     yearTitle: "Year",
     today: "Today",
     yesterday: "Yesterday",
     tomorrow: "Tomorrow",
     weekAgo: "One week ago",
     weekAfter: "In a week",
     monthAgo: "One month ago",
     monthAfter: "In a month"
}
function callback(dt)
{     
	 	var dtdatevalue =dt.getDate()<10 ? '0'+dt.getDate():dt.getDate() ;
		var dtMonthvalue =(dt.getMonth()+1)<10 ? '0'+(dt.getMonth()+1):(dt.getMonth()+1);
		var strFulldate  = dtdatevalue+"/"+dtMonthvalue+"/"+dt.getFullYear();		
		oDatePicker.client.value = strFulldate;
  
}

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, oBtn)
{
     if(!document.getElementById) return;
     // since we control the text format in callback(), getting the date is easy
     var aDt = oTxt.value.split("/");
     var dt = null;
     if(aDt && (aDt.length == 3))
     {
        dt = new Date();
		var strTemp = '';
		dt.setFullYear(parseInt(aDt[2]));
		strTemp = aDt[1];
		strTemp = strTemp.charAt(0)=='0'?  strTemp.charAt(1):strTemp;		
		dt.setMonth(parseInt(strTemp)-1);
		strTemp = aDt[0];
		strTemp = strTemp.charAt(0)=='0'?  strTemp.charAt(1):strTemp;
		dt.setDate(parseInt(strTemp));
     }
     oDatePicker.client = oTxt;
     pos = getElementPosition(oBtn);	 
     oDatePicker.show(dt, pos.left, pos.top, callback);
}
DatePicker.prototype.getDaysInMonth = function (calDate) {
	var days;
	var month = calDate.getMonth () + 1;
	var year = calDate.getFullYear ();
	// RETURN 31 DAYS 
	if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
		days = 31;
	} else if (month == 4 || month == 6 || month == 9 || month == 11) { 
		// RETURN 30 DAYS 
		days = 30;
	} else if (month == 2) {
		if (isLeapYear (year)) { 
			// RETURN 28 DAYS 
			days = 29;
		} else { 
			// RETURN 28 DAYS 
			days = 28;
		}
	}
	return (days);
}
var oDatePicker = new DatePicker('theDatePicker');
