
//-----------------------------------------------------------
//
//		Toolbar For FrameWork
//
//		Renders a XAPDatePicker
//
//		Requires : CoreLib.js, Base.js
//
//-----------------------------------------------------------

function XAPDatePicker(){}; _this = XAPDatePicker.prototype = new Base();
_this				= XAPDatePicker.prototype;
_this.constructor	= XAPDatePicker();
_this.ClassName		= "XAPDatePicker";
_this.ePane			= null;
_this.eTitleBar		= null;
_this.eContentArea	= null;
_this.eStatusBar	= null;
_this.cTitle		= "";
_this.ShowTitleBar	= true;
_this.ShowStatusBar	= true;
_this.Resizable		= false;
_this.DragingWindow	= false;
_this.MousePosition	= [0,0];
_this.XAP			= new XAP();
_this.ChildWindows	= [];
_this.State			= "Windowed";
_this.MinSize		= [0,0];
_this.Modal			= false;
_this.Width			= 200;
_this.Height		= 147;
_this.dDate			= new Date();
_this.StartYear		= 1995;
_this.EndYear		= 2015;
_this.Target		= null;
//_this.Left				= 100;
//_this.Top				= 100;


	

_this.Events		= [];

_this.Init			= function ()
{	
	
	if(this.Modal){this.eModal  = this.NewObj( "div", {className:"XAPDialogModal"}, document.body, "" );}
	var aSel = document.getElementsByTagName("select");
	for( var i=0;i<aSel.length; i++ ){ aSel[i].style.visibility = "hidden"; }
	this.Target = this.Event.src;
	this.cId					= "cclDialog" +  this.Target.name; 
	this.ePane					= this.NewObj( "div", {className:"XAPDialog", id:this.cId}, document.body, "" );
	
	if ( this.Width  != null ) { this.ePane.style.width  = this.Width;}
	if ( this.Height != null ) { this.ePane.style.height = this.Height;}
	this.ePane.style.top    = FindXY(this.Target).y;
	this.ePane.style.left   = FindXY(this.Target).x;
	this.dDate = new Date( this.Event.src.value.split("/")[2], (+this.Event.src.value.split("/")[1]-1), this.Event.src.value.split("/")[0] );
	if ( this.cTitle == null ) { this.cTitle			 = "Select Date";}
	var self = this;

	//this.eTitleBar		= this.NewObj( "div", {className:"XAPTitleBar", CCLIdToDrag:this.cId}, this.ePane, "" );
	//this.eTitle = this.NewObj( "span", {}, this.eTitleBar, this.cTitle );
	
	
	/* Content Area */
	this.eContentHolder	= this.NewObj( "div", {className:"XAPContentHolder"}, this.ePane, "" );
	this.eContentArea	= this.NewObj( "div", {className:"XAPContentArea"}, this.eContentHolder, "" );
	
	var self = this;
				
	this.lnkClose				 = this.NewObj("a", {className:'DialogButton', href:'#'}, this.eContentArea, "X");
	this.lnkClose.onclick        = function (){ self.Dispose(); return false; }
	this.lnkClose.style.position = 'absolute';
	this.lnkClose.style.top		 = '0px';
	this.lnkClose.style.right    = '0px';
	this.lnkClose.style.width	 = '20px';
	this.lnkClose.style.height	 = '20px';
	this.Target.blur();
	
	this.RenderCalender();
}	

_this.RenderCalender = function ()
{		
	var self = this;
	
	this.eSelects = this.NewObj("div", {}, this.eContentHolder, "");
	/*Months*/
	this.eMonth = this.NewObj("select", {}, this.eSelects, "");
	this.NewObj("option", {value:"1"}, this.eMonth, "January");
	this.NewObj("option", {value:"2"}, this.eMonth, "February");
	this.NewObj("option", {value:"3"}, this.eMonth, "March");
	this.NewObj("option", {value:"4"}, this.eMonth, "April");
	this.NewObj("option", {value:"5"}, this.eMonth, "May");
	this.NewObj("option", {value:"6"}, this.eMonth, "June");
	this.NewObj("option", {value:"7"}, this.eMonth, "July");
	this.NewObj("option", {value:"8"}, this.eMonth, "August");
	this.NewObj("option", {value:"9"}, this.eMonth, "September");
	this.NewObj("option", {value:"10"}, this.eMonth, "October");
	this.NewObj("option", {value:"11"}, this.eMonth, "November");
	this.NewObj("option", {value:"12"}, this.eMonth, "December");
	
	/*Years*/
	this.eYear = this.NewObj("select", {}, this.eSelects, "");
	for( var i = this.StartYear; i <= this.EndYear; i++ )
	{
		this.NewObj("option", {value:i}, this.eYear, i);
	}
	
	
	this.eMonth.value = this.dDate.getMonth() + 1;
	this.eMonth.onchange = function()
	{
		self.dDate = new Date( self.dDate.getFullYear() + "/" + this.value + "/" + self.dDate.getDate() );
		self.RenderDays();
	}

	this.eYear.value = this.dDate.getFullYear();
	this.eYear.onchange = function()
	{
		self.dDate = new Date( this.value + "/" + self.dDate.getMonth() + "/" + self.dDate.getDate() );
		self.RenderDays();
	}
	this.eSelects.style.textAlign = "left";
	this.eMonth.style.margin = "5px";
	this.eYear.style.margin = "5px";
	
	this.RenderDays();
}

_this.RenderDays = function ()
{
	var self = this;
	
	if( this.eDays ){ this.eDays.outerHTML = ""; this.eDays = null; }
	this.eDays = this.NewObj("div", {}, this.eContentHolder, "");
	this.eDays.style.width			= "100%";	 
	this.eDays.style.height			= "";	 	 
	
	var table = this.NewObj( "table", {className:"Cal", border:0, cellPadding:0, cellSpacing:0}, this.eDays, "" )
	table.style.width = "100%"
	table.style.border = "solid 0px";
	table.style.borderTop = "solid 1px #d0d0d0";
	var thead = this.NewObj( "thead", {}, table, "" )


	var tr = this.NewObj( "tr", {}, thead, "" );

	tr.style.backgroundColor = "#f0f0f0";
	tr.style.color = "black";
	td = this.NewObj( "td", {}, tr, "M" );
	td.style.borderBottom = "solid 1px #d0d0d0";
	td = this.NewObj( "td", {}, tr, "T" );
	td.style.borderBottom = "solid 1px #d0d0d0";
	td = this.NewObj( "td", {}, tr, "W" );
	td.style.borderBottom = "solid 1px #d0d0d0";
	td = this.NewObj( "td", {}, tr, "T" );
	td.style.borderBottom = "solid 1px #d0d0d0";
	td = this.NewObj( "td", {}, tr, "F" );
	td.style.borderBottom = "solid 1px #d0d0d0";
	td = this.NewObj( "td", {}, tr, "S" );
	td.style.borderBottom = "solid 1px #d0d0d0";
	td = this.NewObj( "td", {}, tr, "S" );
	td.style.borderBottom = "solid 1px #d0d0d0";
	
	var nDaysInMonth  = new Date(this.dDate.getFullYear(), this.dDate.getMonth() + 1, 0).getDate();
	this.dTemp = new Date(this.dDate.getFullYear(), this.dDate.getMonth(), 0);

	var tbody = this.NewObj( "tbody", {}, table, "" )
	tr = this.NewObj( "tr", {}, tbody, "" );
	
	var lFirstWeek = true;
	var nDay = 0;
	var lEOW = false;
	for( var i = 1; i <=42 ; i++  )
	{
		if( lFirstWeek )
		{
			if( i == this.dTemp.getDay() + 1 || i == 7  ){	lFirstWeek = false;	nDay++;	}
		}
		else if( nDay < nDaysInMonth  && !lEOW ){	nDay++;	}
		else	{	nDay = 0;	lEOW = true	}
		
		var cDay = "";
		if( nDay != 0 ){ cDay = nDay; }
		var td = this.NewObj( "td", {}, tr, cDay );
		td.style.backgroundColor="white";
		if( cDay != "" )
		{
			td.day = cDay;
			td.onclick = function (){ self.updateDate(this.day); }
			td.style.cursor = "pointer";
			td.style.color = "green";
			td.style.fontWeight = "bold";
			td.onmouseover = function(){this.style.backgroundColor="green";this.style.color="white";}
			td.onmouseout  = function(){this.style.backgroundColor="white";this.style.color="green";}
		
			if( Math.floor(i/7) == i/7 || Math.floor((i+1)/7) == (i+1)/7 )	
			{	
				// SAT / SUN
				td.style.backgroundColor="#f0f0f0";
				td.onmouseout  = function(){this.style.backgroundColor="#f0f0f0";this.style.color="green";}
			}
			
			if( cDay == this.dDate.getDate() )
			{
				td.style.backgroundColor="#d0d0d0";
				td.onmouseout  = function(){this.style.backgroundColor="#d0d0d0";this.style.color="green";}
			}
			
			
		}
		
		if( Math.floor(i/7) == i/7  )	{	tr = this.NewObj( "tr", {}, tbody, "" );	}						
	}

}

_this.updateDate = function(nDay)
{
	this.Target.value = nDay + "/" + (+this.dDate.getMonth()+1) + "/" + this.dDate.getFullYear();
	this.Dispose();
	
}

_this.Blur			= function (cText)
{
	AddClass(this.ePane, "Blured");
	this.ePane.style.zIndex = 900;
}

_this.Dispose = function ()
{
	if(this.Model){this.eModal.outerHTML = "";}
	//this.ePane.outerHTML = "";
	this.ePane.parentNode.removeChild( this.ePane );
	var aSel = document.getElementsByTagName("select"); for( var i=0;i<aSel.length; i++ ){ aSel[i].style.visibility = ""; }
}

_this.ReSize = function()
{
	if ( this.Width  != null ) { this.ePane.style.width  = this.Width;}
	if ( this.Height != null ) { this.ePane.style.height = this.Height;}
	if ( this.Top    != null ) { this.ePane.style.top    = this.Top;}
	if ( this.Left   != null ) { this.ePane.style.left   = this.Left;}	
}

_this.SetTitle = function(cTitle)
{
	if( cTitle ){ this.cTitle = cTitle }
	this.eTitle.innerHTML = cTitle;
}

