//DatePrototype::Begin
Date.prototype.isLapYear=function(){if(this.getFullYear() % 4 == 0) return true;return false;}

Date.prototype.getWeekInYear=function(){
    var __JanFirst = new Date(this.getFullYear(),0,1);
    var __Test = 7 + 1 - __JanFirst.getDay();
    if (__Test == 8) __Test = 1;
    var __Day = ((Date.UTC(this.getFullYear(),this.getMonth(),this.getDate(),0,0,0) - Date.UTC((this.getFullYear()),0,1,0,0,0)) /1000/60/60/24) + 1;
    var __Week = Math.floor((__Day-__Test+7)/7);
    if (__Week == 0) {        
        var __Before = new Date(this.getFullYear()-1,0,1);
        var __BTest = 7 + 1 - __Before.getDay();
        if (__BTest == 2 || __BTest == 8) __Week = 53; else __Week = 52;
    }
    return __Week;
}

Date.prototype.getDayName=function(__isShortName){
	if(__isShortName==null)__isShortName=true;
	var __Days = __isShortName ? Date.ShortDayNames() : Date.LongDayNames();
	return __Days[this.getDay()];
}

Date.prototype.MySQLStr=function(){
	var __Year=this.getFullYear();
	var __Month=this.getMonth()+1;
	var __Day=this.getDate();
	if(__Month<10)__Month='0'+__Month;
	if(__Day<10)__Day='0'+__Day;
	return __Year+'-'+__Month+'-'+__Day;
}

Date.prototype.getMonthName=function(){
	var __Months = Date.MonthNames();
	return __Months[this.getMonth()];
}

Date.prototype.getDaysInMonth=function(){
	var __MonthLengths = [31,28,31,30,31,30,31,31,30,31,30,31];
	return __MonthLengths[this.getMonth()] + (this.isLapYear() && this.getMonth()==1 ? 1 : 0);
}

Date.prototype.toDateTimeString=function(){
	var __DateTimeString=this.getDate()+' ';
	var __MonthNames = Date.MonthNames();
	var __DayNames = Date.LongDayNames();
	__DateTimeString += __MonthNames[this.getMonth()]+' ';
	__DateTimeString += this.getFullYear()+' ';
	__DateTimeString += __DayNames[this.getDay()];
	return __DateTimeString;
}

Date.prototype.toShortDateTimeString=function(){
	var __DateTimeString=this.getDate()+' ';
	var __MonthNames = Date.MonthNames();
	var __DayNames = Date.LongDayNames();
	__DateTimeString += __MonthNames[this.getMonth()]+' ';
	__DateTimeString += this.getFullYear();
	return __DateTimeString;
}

Date.MonthNames=function(){
	return ['Ocak','Şubat','Mart','Nisan','Mayıs','Haziran','Temmuz','Ağustos','Eylül','Ekim','Kasım','Aralık'];
}

Date.ShortDayNames=function(){
	return 	['Paz','Pts','Sal','Çar','Per','Cum','Cmt'];
}

Date.LongDayNames=function(){
	return ['Pazar','Pazartesi','Salı','Çarşamba','Perşembe','Cuma','Cumartesi'];
}

Date.prototype.IncreaseDay=function(){
	var __Year	=	this.getFullYear();
	var __Month	=	this.getMonth();
	var __Day	=	this.getDate();
	if(__Day == this.getDaysInMonth() ){
		this.setDate(1);
		if(__Month==11){
			this.setMonth(0);
			this.setYear(__Year+1);
		}
		else{
			this.setMonth(__Month+1);	
		}
	}
	else{
		this.setDate(__Day+1);	
	}
}
Date.prototype.DecreaseDay=function(){
	var __Year	=	this.getFullYear();
	var __Month	=	this.getMonth();
	var __Day	=	this.getDate();
	if(__Day==1){
		__Month--;
		if(__Month<0){
			__Month = 11;			
			this.setYear(__Year-1);
		}
		this.setMonth(__Month);
		__Day=this.getDaysInMonth();
		this.setDate(__Day);
	}
	else{
		this.setDate(__Day-1);	
	}

}
//DatePrototype::End

var __NextImageSrc= '__Templates/__Default/__Imgx/__gif/Calendar_next.gif';
var __BackImageSrc= '__Templates/__Default/__Imgx/__gif/Calendar_back.gif';
var __CalendarImageSrc = '__Imgx/__png/date.png';

function Test(){
	new Calendar();
}

Calendar = function(__Date,__Properties){
	var __Table,__Tr,__Td;
	
	if(__Date==null)__Date=new Date();
	
	if(__Properties)
		this.Properties=__Properties;
	else
		this.Properties=Calendar.DefaultProperties();
		
	if(!this.Properties.ShowDateName)this.Properties.ShowDateName=false;
	this.ShowDateName=this.Properties.ShowDateName;
		
	if(!this.Properties.Target)return;
	
	this.Owner=this.Properties.Owner;		
	
	if(this.Properties.Target){
		if(this.Properties.Target.tagName=='INPUT'){
			this.Properties.Target.setAttribute('readonly',true);
			this.Properties.Target.setAttribute('readOnly',true);
			if(this.Properties.Target.value.length==10){
				var __Year	= parseInt(this.Properties.Target.value.substring(0,4));
				var __Month = parseInt(this.Properties.Target.value.substring(5,7));
				__Month--;
				var __Day = parseInt(this.Properties.Target.value.substring(8,10));
				__Date = new Date(__Year,__Month,__Day);
			}
		}		
		var __Hidden = _CreateElement({'TagName':'DIV','StyleName':{'display':'none'}});
		var __Parent = this.Properties.Target.parentNode;
		__Parent.appendChild(__Hidden);

		var __CalendarImage = new Image();
		__CalendarImage.id='CalendarImage';
		__CalendarImage.src=this.Properties.CalendarImage;
		__CalendarImage.style.cursor='pointer';
		__CalendarImage.setAttribute('align','absmiddle');
		__CalendarImage.Calendar=this;
		__CalendarImage.onclick=function(e){			
			if(!document.openedCalendars){
				document.openedCalendars=new Array();
				eval("document.body." + (UsedBrowser.Type == 0 ? "attachEvent" : "addEventListener") + "('" + (UsedBrowser.Type == 0 ? "on" : "") + "click',Calendar.Close" + (UsedBrowser.Type == 0 ? "" : ",false") + ")");				
			}				
			this.Calendar.Container.style.display='block';
			this.Calendar.Container.style.zIndex=1000;
			var Pos = ObjectPosition(this);
			var __DiffTop=0;
			var __DiffLeft=0;
			
			if(this.Calendar.Properties.ScrollCatch){
				__DiffTop=this.Calendar.Properties.ScrollCatch.scrollTop;
				__DiffLeft=this.Calendar.Properties.ScrollCatch.scrollLeft;	
			}
			
			this.Calendar.Container.style.left=Pos.x-__DiffLeft+'px';
			this.Calendar.Container.style.top=Pos.y+3-__DiffTop+this.height+'px';	
			if(document.openedCalendars.MySearch(this.Calendar.Container)<0)
				document.openedCalendars[document.openedCalendars.length]=this.Calendar.Container;			

			
			
		}
		var __LabelContainer = _CreateElement({'TagName':'DIV'});
		__LabelContainer.id='CurrentDateContainer';
		var __Label = _CreateElement({'TagName':'DIV'});
		__Label.id='CurrentDateLabel';
		__Label.Calendar=this;		
		__Label.setAttribute('class','CalendarLabel');
		__Label.setAttribute('className','CalendarLabel');
		__Label.onclick=function(e){__CalendarImage.onclick();}
		if(UsedBrowser.Type!=0)__Label.style.height='14px';
		this.Label=__Label;
		__LabelContainer.appendChild(__Label);
		__LabelContainer.appendChild(__CalendarImage);
		__Parent.appendChild(__LabelContainer);
		__Hidden.appendChild(this.Properties.Target);
		
	}
	
	this.BuildEmptyContainer=function(){
		var __Container=_CreateElement(
									{
										'TagName':'DIV',
										'StyleName':{'display':'none'},
										'Attributes':
										{
											'class':'Calendar',
											'className':'Calendar'
											}
											
									}
									);	
		__Container.id='Calendar';
		this.Container=__Container;		
		this.Container.Calendar=this;
		
		if(this.Properties.ChangeableMonth || this.Properties.ChangeableYear){			
			
			var __ControlArea=_CreateElement({'TagName':'TABLE','StyleName':{'width':'100%'}});
			__ControlArea.insertRow(0);
			
			this.ControlArea = __ControlArea;
			
			//Month::Begin
			if(this.Properties.ChangeableMonth){

				var __MonthArea = _CreateElement({'TagName':'TABLE'});
				__Tr=__MonthArea.insertRow(0);
				__Td=__Tr.insertCell(0);	
				var __MBI = new Image();
				__MBI.setAttribute('align','absmiddle');
				__MBI.setAttribute('vspace',2);
				__MBI.setAttribute('hspace',2);
				__MBI.Calendar = this;
				__MBI.src=this.Properties.BackImage;
				__MBI.style.cursor='pointer';
				__MBI.onclick = function(e){
					this.Calendar.DecreaseMonth();
				}
				__Td.appendChild(__MBI);	
				__Td=__Tr.insertCell(1);
				__Td.align='center';
				__Td.valign='middle';
				__Td.style.width='70px';
				__Td=__Tr.insertCell(2);
				var __MNI = new Image();
				__MNI.setAttribute('align','absmiddle');
				__MNI.setAttribute('vspace',2);
				__MNI.setAttribute('hspace',2);			
				__MNI.Calendar = this;
				__MNI.src=this.Properties.NextImage;
				__MNI.style.cursor='pointer';
				__MNI.onclick = function(){
					this.Calendar.IncreaseMonth();
				}
				__Td.appendChild(__MNI);
				
				this.MonthArea = __MonthArea;
				this.MonthArea.Label = __MonthArea.rows[0].cells[1];
				__Td = this.ControlArea.rows[0].insertCell(this.ControlArea.rows[0].cells.length-1);
				__Td.setAttribute('width','50%');
				__Td.setAttribute('align','center');
				__Td.setAttribute('valign','middle');
				__Td.appendChild(this.MonthArea);
			}
			//Month::End
			//Year::Begin
			if(this.Properties.ChangeableYear){

				var __YearArea = _CreateElement({'TagName':'TABLE'});
				__Tr=__YearArea.insertRow(0);
				
				__Td=__Tr.insertCell(0);
				var __YBI = new Image();
				this.YearDownButton=__YBI;
				__YBI.id=_CreateElementID('IMG');
				__YBI.setAttribute('align','absmiddle');
				__YBI.setAttribute('vspace',2);
				__YBI.setAttribute('hspace',2);			
				__YBI.Calendar = this;
				__YBI.src=this.Properties.BackImage;
				__YBI.style.cursor='pointer';
				__YBI.onmousedown=function(e){if(this.TimeOutID)clearInterval(this.TimeOutID);this.TimeOutID = setInterval("Calendar.YearDown('"+this.id+"')",100);}
				__YBI.onmouseup=function(e){if(this.TimeOutID)clearInterval(this.TimeOutID);}
				__Td.appendChild(__YBI);
				__Td=__Tr.insertCell(1);
				__Td=__Tr.insertCell(2);
				var __YNI = new Image();
				this.YearUpButton=__YNI;
				__YNI.id=_CreateElementID('IMG');
				__YNI.setAttribute('align','absmiddle');
				__YNI.setAttribute('vspace',2);
				__YNI.setAttribute('hspace',2);			
				__YNI.Calendar = this;
				__YNI.src=this.Properties.NextImage;
				__YNI.style.cursor='pointer';
				__YNI.onmousedown=function(e){if(this.TimeOutID)clearInterval(this.TimeOutID);this.TimeOutID = setInterval("Calendar.YearUp('"+this.id+"')",100);}														
				__YNI.onmouseup=function(e){if(this.TimeOutID)clearInterval(this.TimeOutID);}																									
				__Td.appendChild(__YNI);
				this.YearArea=__YearArea;
				this.YearArea.Label = __YearArea.rows[0].cells[1];
				__Td = this.ControlArea.rows[0].insertCell(this.ControlArea.rows[0].cells.length-1);
				__Td.setAttribute('width','50%');
				__Td.setAttribute('align','center');
				__Td.setAttribute('valign','middle');				
				__Td.appendChild(this.YearArea);

			}
			//Year::End			
			
			this.Container.appendChild(this.ControlArea);
		}
		

		
		
		//Headers::Begin 		
		var __Headers = Date.ShortDayNames();
		__Table = _CreateElement({'TagName':'TABLE','Name':'CalendarTable'});
		__Table.id='CalendarTable';
		this.DataGrid=__Table;
		
		__Tr=__Table.insertRow(__Table.rows.length);
		__Td=__Tr.insertCell(0);
		__Td.setAttribute('class', 'CalendarBox');
		__Td.setAttribute('className', 'CalendarBox');
		
		for(var __i=0;__i<__Headers.length;__i++){
			__Td=__Tr.insertCell(__Tr.cells.length);
			__Td.align='center';
			__Td.valign='middle';
			__Td.setAttribute('class', 'CalendarBox DayHeader');
			__Td.setAttribute('className', 'CalendarBox DayHeader');					
			__Td.innerHTML= __Headers[__i];
		}
		
		for(var __m=0;__m<6;__m++){
			__Tr=__Table.insertRow(__Table.rows.length);
			__Td=__Tr.insertCell(0);
			__Td.setAttribute('class', 'CalendarBox');
			__Td.setAttribute('className', 'CalendarBox');			
			for(var __n=0;__n<7;__n++){
				__Td=__Tr.insertCell(__Tr.cells.length);
				__Td.align='center';
				__Td.valign='middle';
				__Td.setAttribute('class', 'CalendarBox');
				__Td.setAttribute('className', 'CalendarBox');	
				__Td.setAttribute('align','center');
				__Td.setAttribute('valign','middle');
				__Td.Calendar=this;
				__Td.onclick=function(e){					
					var __Current=this.CurrentDate;
					if(__Current){
						this.Calendar.setDateTime(__Current,true);
						if(this.Calendar.Container){
							this.Calendar.Container.style.display='none';
							if(this.Calendar.YearUpButton.TimeOutID)
								clearInterval(this.Calendar.YearUpButton.TimeOutID);
							if(this.Calendar.YearDownButton.TimeOutID)
								clearInterval(this.Calendar.YearDownButton.TimeOutID);								
						}						
					}	
				}
				__Td.onmouseover=function(e){
					this.setAttribute('class', 'CalendarBox OnMouseOver');
					this.setAttribute('className', 'CalendarBox OnMouseOver');					
				}
				__Td.onmouseout=function(e){
					this.setAttribute('class', this.getAttribute('MouseOutclassName'));
					this.setAttribute('className', this.getAttribute('MouseOutclassName'));					
				}
			}	
		}
		
		this.Container.appendChild(__Table);
		//Headers::End		
		this.Owner.Calendar=this;
		this.Owner.appendChild(this.Container);
	}
	
	this.Build=function(){
		if(!this.Container)
			this.BuildEmptyContainer();
		
		if(this.MonthArea){
			this.MonthArea.Label.innerHTML=this.Now.getMonthName();	
		}
		
		if(this.YearArea){
			this.YearArea.Label.innerHTML=this.Now.getFullYear();	
		}		
		
		var __Year	=	this.Now.getFullYear();
		var __Month	=	this.Now.getMonth();
		var __Day	=	this.Now.getDate();
		
		var __FDate	=	new Date(__Year,__Month,1);
		var __Test	=	__FDate.getDay();
		var __SDate	=	__FDate;

		for(var __t=__FDate.getDay();__t>=1;__t--)
			__SDate.DecreaseDay();
			for(__m=0;__m<6;__m++){
				__Td = this.DataGrid.rows[__m+1].cells[0];
				__Td.innerHTML = __SDate.getWeekInYear();
				__Td.setAttribute('class','CalendarBox WeekNumber');
				__Td.setAttribute('className','CalendarBox WeekNumber');
				for(__n=0;__n<7;__n++){
					var __Current = new Date(__SDate.getFullYear(),__SDate.getMonth(),__SDate.getDate());
					__Td = this.DataGrid.rows[__m+1].cells[__n+1];
					__Td.CurrentDate=__Current;
					__Td.innerHTML = __SDate.getDate();

					if(__Current.getMonth()<this.Now.getMonth()){
						__Td.setAttribute('class','CalendarBox BeforeMonth');
						__Td.setAttribute('className','CalendarBox BeforeMonth');
						__Td.setAttribute('MouseOutclassName','CalendarBox BeforeMonth');
					}
					else if(__Current.getMonth()==this.Now.getMonth()){
						__Td.setAttribute('class','CalendarBox CurrentMonth');
						__Td.setAttribute('className','CalendarBox CurrentMonth');	
						__Td.setAttribute('MouseOutclassName','CalendarBox CurrentMonth');
						if(__Current.getDate()==this.Now.getDate()){
							__Td.setAttribute('class','CalendarBox SelectedDay');
							__Td.setAttribute('className','CalendarBox SelectedDay');
							__Td.setAttribute('MouseOutclassName','CalendarBox SelectedDay');
							__Td.Selected=true;
						}
					}
					else{
						__Td.setAttribute('class','CalendarBox AfterMonth');
						__Td.setAttribute('className','CalendarBox AfterMonth');
						__Td.setAttribute('MouseOutclassName','CalendarBox AfterMonth');
					}
					
					__SDate.IncreaseDay();	
				}	
			}
		
	}
	
	this.setDateTime=function(__Date,__WriteValue){		
		if(__WriteValue==null)__WriteValue=false;
		if(this.Properties.Target && this.Properties.Target.tagName=='INPUT'){
			if(__WriteValue)
				this.Properties.Target.value=__Date.MySQLStr();
		}
		this.Now=__Date;
		this.Label.innerHTML=this.ShowDateName?this.Now.toDateTimeString():this.Now.toShortDateTimeString();
		this.Build();
	}
	
	this.setDateTime(__Date);
	
		
	this.IncreaseYear=function(){
		var __Date = new Date(this.Now.getFullYear()+1,this.Now.getMonth(),this.Now.getDate());
		this.setDateTime(__Date);
	}
	this.DecreaseYear=function(){
		var __Date = new Date(this.Now.getFullYear()-1,this.Now.getMonth(),this.Now.getDate());
		this.setDateTime(__Date);
	}	
	
	this.IncreaseMonth=function(){
		var __Year	=	this.Now.getFullYear();
		var __Month	=	this.Now.getMonth();
		__Month++;
		if(__Month>11){
			__Month	=	0;
			__Year++;
		}
		var __Date	=	new Date(__Year,__Month,this.Now.getDate());
		this.setDateTime(__Date);
	}
	this.DecreaseMonth=function(){
		var __Year	=	this.Now.getFullYear();
		var __Month	=	this.Now.getMonth();
		__Month--;
		if(__Month<0){
			__Month	=	11;
			__Year--;
		}
		var __Date	=	new Date(__Year,__Month,this.Now.getDate());
		this.setDateTime(__Date);
	}	
	
}

Calendar.YearDown=function(ButtonID){
	var __Element = $(ButtonID);
	__Element.Calendar.DecreaseYear();
}

Calendar.YearUp=function(ButtonID){
	var __Element = $(ButtonID);
	__Element.Calendar.IncreaseYear();
}

Calendar.Close=function(e){
	var __SrcElement = null;
	if(window.event)
		__SrcElement=window.event.srcElement;
	else if (e)
		__SrcElement=e.target;
	if(document.openedCalendars && (__SrcElement && !__SrcElement.Calendar)){
		var __Container;
		for(var __i=0;__i<document.openedCalendars.length;__i++){
			__Container=document.openedCalendars[__i];
			__Container.style.display='none';
		}
	}	
}

Calendar.DefaultProperties=function(){
	var __Properties = {
			'Owner':document.body,
			'Target':$('Test'),
			'backgroundColor':'#FFFFFF',
			'NextImage':__NextImageSrc,
			'BackImage':__BackImageSrc,
			'CalendarImage':__CalendarImageSrc,
			'ChangeableMonth':true,		
			'ChangeableYear':true
		};
	return __Properties;
}

