////////////////////////////////////////////////////////////////////////////////
//DatePicker_Script . Copyright(c) 2005 . Programmed by Pham Duy
//Contact me : duy120779@yahoo.com
//For My Loved Wife DTHH
//You use this code free. But please do not remove this note.
///////////////////////////////////////////////////////////////////////////////
var g_curDatePicker = null;

function DatePicker(id , divid , valueid)
{
	this.ID = id;
	this.divID = divid;
	this.valueID = valueid;
	this.monthID = id + '_ddlMonth';
	this.yearID = id + '_txtYear';
	this.containerID = id + '_InnerConatiner';
	///////////////////////////////////
	this.PlusIcon = '';
	this.MinusIcon = '';
	this.NextIcon = '';
	this.PrevIcon = '';
	this.ButtonCss = '';
	this.HeaderCss = '';
	this.DayCss = '';
	this.SelectedDayCss = '';
	this.FromYear = 1970;
	this.ToYear = 9999;
	this.Format = 'mm/dd/yyyy';
	///////////////////////////////////
	this.m_iCurrDay = 0;
	this.m_iCurrMonth = 0;
	this.m_iCurrYear = 0;	
	this.Init();	
}

DatePicker.prototype.GetPosition = function(node)
{
	var pos = new Array(0, 0);
	if (node.offsetParent)
	{
		while (node.offsetParent)
		{
			pos[0] += node.offsetLeft;
			pos[1] += node.offsetTop;
			node = node.offsetParent;
			if (node == document.body)
			{
				pos[0] -= node.offsetLeft;
				pos[1] -= node.offsetTop;
			}
		}
	}
	return pos;
}

DatePicker.prototype.Popup = function()
{
	this.Init();
	var div = document.getElementById( this.divID );
	if(div.style.display != 'none') return;
	
	var txtValue = document.getElementById(this.valueID);
	var pos = this.GetPosition(txtValue);
	var x = pos[0];
	var y = pos[1] + txtValue.offsetHeight + 1;	
	
	div.style.left = x + 'px';
	div.style.top = y + 'px';
	div.style.display = 'block';
	
	var container = document.getElementById(this.ID + '_ifrContainer');
	container.style.top = div.style.top;
	container.style.left = div.style.left;
	container.style.width = div.offsetWidth;
	container.style.height = div.offsetHeight;
	container.style.display = 'block';
	
	//Update
	var ddlMonth = document.getElementById(this.monthID);
	var txtYear = document.getElementById(this.yearID);
	ddlMonth.value = this.m_iCurrMonth+1;
	txtYear.value = this.m_iCurrYear;
	this.Update();
	
	if(g_curDatePicker != null)
		g_curDatePicker.KillPopup();
		
	g_curDatePicker = this;	
}

DatePicker.prototype.KillPopup = function()
{	
	var div = document.getElementById( this.divID );
	if(div.style.display != 'block') return;
	div.style.display = 'none';
	g_curDatePicker = null;
	
	var container = document.getElementById(this.ID + '_ifrContainer');
	container.style.display = 'none';
}

DatePicker.prototype.GetDaysPerMonth = function()
{
	var m = this.m_iCurrMonth + 1;
	var  y = 	this.m_iCurrYear;
	if(((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))
	{
		if(m == 2) return 29;
		else if(m == 1 || m == 3 || m== 5 || m== 7 || m==8 || m==10 || m== 12) return 31;
		else return 30;
	}
	else
	{
		if(m == 2) return 28;
		else if(m == 1 || m == 3 || m== 5 || m== 7 || m==8 || m==10 || m== 12) return 31;
		else return 30;
	}
}

DatePicker.prototype.Init = function()
{
	var obj = document.getElementById( this.divID );
	obj.insertAdjacentHTML('beforeBegin' , '<IFRAME id="' + this.ID + '_ifrContainer" style="DISPLAY: none; LEFT: 0px; POSITION: absolute; TOP: 0px" src="javascript:false;" frameBorder="0" scrolling="no"></IFRAME>');
	var container = document.getElementById(this.ID + '_ifrContainer');
	container.style.zIndex = 100;
	container.style.top = obj.style.top;
	container.style.left = obj.style.left;
	container.style.width = obj.offsetWidth;
	container.style.height = obj.offsetHeight;
	container.style.filter='progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
	/////////////////////////////////////////////////////
	var txtValue = document.getElementById(this.valueID);
	if(txtValue.value != '')
	{
		var ms = Date.parse(txtValue.value);		
		if(isNaN(ms)) txtValue.value = '';
		else
		{
			var dt = new Date(ms);		
			
			this.m_iCurrMonth = dt.getMonth();
			this.m_iCurrYear = dt.getYear();	
			this.m_iCurrDay = dt.getDate();			
		}
	}
	else
	{
		var ddlMonth = document.getElementById(this.monthID);	
		var txtYear = document.getElementById(this.yearID);
		if(ddlMonth && txtYear)
		{
			this.m_iCurrMonth = parseInt(ddlMonth.value) - 1;
			this.m_iCurrYear = parseInt(txtYear.value);	
		}
		else
		{
			var d = new Date();
			
			this.m_iCurrMonth = d.getMonth();
			this.m_iCurrYear = d.getYear();	
			this.m_iCurrDay = d.getDate();
		}
	}	
	
	if (document.all)
	{
		document.attachEvent("onclick", BodyClickHandler);
	}
	else
	{
		document.addEventListener('click', BodyClickHandler, true);
	}
}

DatePicker.prototype.NextMonth = function()
{
	var ddlMonth = document.getElementById(this.monthID);
	var iMonth = parseInt(ddlMonth.value);
	if(iMonth < 12) iMonth ++;
	else
	{		
		var txtYear = document.getElementById(this.yearID);
		var iYear = parseInt(txtYear.value);
		if(iYear < this.ToYear ) { iMonth = 1; iYear ++; }
		txtYear.value = iYear;
	}
	
	ddlMonth.value = iMonth;
	
	this.Select();
}

DatePicker.prototype.PrevMonth = function()
{
	var ddlMonth = document.getElementById(this.monthID);
	var iMonth = parseInt(ddlMonth.value);
	if(iMonth > 1) iMonth --;
	else
	{		
		var txtYear = document.getElementById(this.yearID);
		var iYear = parseInt(txtYear.value);
		if(iYear > this.FromYear ) { iMonth = 12; iYear --; }
		txtYear.value = iYear;
	}
	ddlMonth.value = iMonth;
	
	this.Select();
}

DatePicker.prototype.PrevYear = function()
{
	var txtYear = document.getElementById(this.yearID);
	var iYear = parseInt(txtYear.value);
	if(iYear < this.FromYear) return;
	else
	{		
		iYear --;
	}
	
	txtYear.value = iYear;
	this.Select();
}

DatePicker.prototype.NextYear = function()
{
	var txtYear = document.getElementById(this.yearID);
	var iYear = parseInt(txtYear.value);
	if(iYear > this.ToYear) return;
	else
	{		
		iYear ++;
	}
	
	txtYear.value = iYear;
	this.Select();
}

DatePicker.prototype.SelectDay = function(val)
{
	var ddlMonth = document.getElementById(this.monthID);
	var txtYear = document.getElementById(this.yearID);
	var txtValue = document.getElementById(this.valueID);
		
	txtValue.value = ddlMonth.value + "/" + val + "/" + txtYear.value;
	
	this.KillPopup();
}

DatePicker.prototype.SetMonthYear = function(m,y)
{
	if(isNaN(m) || isNaN(y)) return;
	if(y >= 1900 && y <= 9999)  this.m_iCurrYear = y;
	if(m >= 1 && m <= 12) 	this.m_iCurrMonth = m - 1;
	this.Update();
}

DatePicker.prototype.Select = function()
{
	var ddlMonth = document.getElementById(this.monthID);
	var txtYear = document.getElementById(this.yearID);
	
	var iYear = parseInt(txtYear.value);
	if(iYear > this.ToYear || iYear < this.FromYear) 
	{
		txtYear.value = this.m_iCurrYear; return;
	}
	
	this.SetMonthYear(parseInt(ddlMonth.value) , parseInt(txtYear.value));
}

DatePicker.prototype.Display = function()
{	
	var div = document.getElementById( this.divID );

	var sHTML = '<TABLE cellpadding="0" cellspacing="1" border="0" bgcolor=silver>';
	sHTML += '<TR>';
	sHTML += '<TD>';
	sHTML += '<TABLE width="100%" cellpadding="0" cellspacing="1">';
	sHTML += '<TR>';
	sHTML += '<TD>';
	sHTML += '<SELECT id="' + this.monthID + '">';
	for(var i = 1 ; i <= 12 ; i++)
	{
		if(i == this.m_iCurrMonth + 1)
			sHTML += '<OPTION selected value="' + i + '">' + i + '</OPTION>';
		else
			sHTML += '<OPTION value="' + i + '">' + i + '</OPTION>';
	}
	sHTML += '</SELECT>';
	sHTML += '<TD width="3"></TD>';
	sHTML += '<TD>';
	sHTML += '<INPUT size="5" id="' + this.yearID + '" value="' + this.m_iCurrYear + '" onchange="' + this.ID + '.Select()">';
	sHTML += '</TD>';
	sHTML += '<TD width="100%">';
	sHTML += '<TABLE cellpadding="0" cellspacing="0">';
	sHTML += '<TR><TD class="' + this.ButtonCss + '"><IMG src="' + this.PlusIcon + '" onclick="' + this.ID + '.NextYear()"></TD></TR>';
	sHTML += '<TR><TD class="' + this.ButtonCss + '"><IMG src="' + this.MinusIcon + '" onclick="' + this.ID + '.PrevYear()"></TD></TR>';
	sHTML += '</TABLE>'
	sHTML += '</TD>';
	sHTML += '<TD width="1" style="padding-left:2px;padding-right:2px">';
	sHTML += '<INPUT type="button" id="btnGo" value="Go" onclick="' + this.ID + '.Select()' + '" class="' + this.ButtonCss + '">';
	sHTML += '</TD>';
	sHTML += '<TD width="1" style="padding-left:2px;padding-right:2px">';
	sHTML += '<IMG src="' + this.PrevIcon + '" align="absMiddle" style="CURSOR:HAND" onclick="' + this.ID + '.PrevMonth()' + '">';
	sHTML += '</TD>';
	sHTML += '<TD width="1" style="padding-left:2px;padding-right:2px">';
	sHTML += '<IMG src="' + this.NextIcon + '" align="absMiddle" style="CURSOR:HAND" onclick="' + this.ID + '.NextMonth()' + '">';
	sHTML += '</TD>';
	sHTML += '</TR>';
	sHTML += '</TABLE>';
	//////////////////////////////////////////////////////////////////////////////
	sHTML += '</TD>';
	sHTML += '</TR>';
	sHTML += '<TR>';
	sHTML += '<TD>';
	sHTML += '<DIV id="' + this.containerID + '">';
	sHTML += '</DIV>';
	sHTML += '</TD>';
	sHTML += '</TR>';
	sHTML += '</TABLE>';
	div.innerHTML = sHTML;
}

DatePicker.prototype.Update = function()
{
	var div = document.getElementById( this.containerID );
	var sHTML = '<TABLE cellpadding="0" cellspacing="1" border="0">';	
	var aDays = new Array('Sun' , 'Mon' , 'Tue' , 'Wed' , 'Thu' , 'Fri' , 'Sat');
	sHTML += '<TR>';
	for(var i = 0 ; i < aDays.length ; i++ )
	{
		sHTML += '<TD class="' + this.HeaderCss + '" width="14%" align="center">' + aDays[i] + '</TD>';
	}
	sHTML += '</TR>';
	
	sHTML += '<TR>';	
	var diw = 0;
	
	var dtest = new Date(this.m_iCurrYear , this.m_iCurrMonth , 1);
	diw = dtest.getDay();

	var day = 0;
	var maxday = this.GetDaysPerMonth() + diw;
	var adj = maxday;
	while(adj %7 > 0) adj ++;	
	
	for(var i = 1; i <= adj; i++ )
	{		
		sHTML += '<TD align="center"';
		if(i > diw && i <= maxday) 
		{
			day ++ ;
			if(day == this.m_iCurrDay)
				sHTML += ' class="' + this.SelectedDayCss + '"';
			else
				sHTML += ' class="' + this.DayCss + '"';
			
			sHTML += ' style="CURSOR:HAND" onclick="' + this.ID + '.SelectDay(' + day.toString() + ')' + '">';
			sHTML += day.toString();
		}
		else 
		{
			sHTML += '>&nbsp;';
		}
		sHTML += '</TD>';
		if( i > 0 && i % 7 == 0) 	sHTML += '<TR>';
	}
	sHTML += '</TR>';
	sHTML += '</TABLE>';
	div.innerHTML = sHTML;
}

function BodyClickHandler(e)
{	
	if (!e) 
	{
		var e = window.event;
	}	
	
	if(g_curDatePicker != null)
	{		
		g_curDatePicker.KillPopup();		
	}	
}
