
function Calendar(oControl, sBase) {

	if (oControl!=null) {
		sBase=oControl.id.split('_')[0];
	}
	
	this.Base=sBase;
	this.MonthYearDropDown=document.getElementById(sBase+'_MonthYear');
	this.DayDropDown=document.getElementById(sBase+'_Day');
	this.Mask=document.getElementById(sBase+'_Mask');
	this.Calendar=document.getElementById(sBase+'_Calendar');
	this.Year=this.MonthYearDropDown.options[this.MonthYearDropDown.selectedIndex].value.split('_')[1];
	this.Month=this.MonthYearDropDown.options[this.MonthYearDropDown.selectedIndex].value.split('_')[0];
	this.Day=this.DayDropDown.options[this.DayDropDown.selectedIndex].value;
	this.CurrentDate=d.New(this.Day, this.Month, this.Year);

	this.Visible=iif(this.Calendar.style.display=='none',false, true);
	
	//set date
	this.SetDate=function(iYear, iMonth, iDay) {
	
		for (var i=0;i<=this.MonthYearDropDown.options.length-1;i++) {
			
			if (this.MonthYearDropDown.options[i].value==iMonth+'_'+iYear) {
				this.MonthYearDropDown.selectedIndex=i;
				this.Year=iYear;
				this.Month=iMonth;
				this.Day=iDay;
				CalendarPopulateDays(this);
				break;
			}
		}
	}
	
	// set position
	this.SetPosition=function() {
		this.Calendar.style.top=parseInt(this.MonthYearDropDown.offsetTop)+20+'px';
		this.Calendar.style.left=this.MonthYearDropDown.offsetLeft+'px';
		this.Mask.style.top=parseInt(this.MonthYearDropDown.offsetTop+20)+'px';
		this.Mask.style.left=this.MonthYearDropDown.offsetLeft+'px';
	}
}



function SetCalendarDate(sBase,iDate) {
	var iIndex=iDate-1;
	
	oDropdown=f.GetObject(sBase+'_Day')
	oDropdown.selectedIndex=iIndex;
	oDropdown.onchange();	
	HideCalendar(sBase);
}


function CalendarDateChange(oDropDown) {

	var oCalendar=new Calendar(oDropDown);
	
	if (oCalendar.Visible==true) {
	
		if (oDropDown.id.indexOf('Day')>0) {
			Cal_ShowCalendar(oDropDown,1);
		} else {
			Cal_ShowCalendar(oDropDown);
		}
	} else {
		
		if (oDropDown.id.indexOf('_Month')>-1) {
			CalendarPopulateDays(oCalendar);
		}
	}
	
}

function CalendarPopulateDays(oCalendar) {

	var aDays=new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
	
	var iNextYear=parseInt(oCalendar.Year)+1;
	var dCurrentDate=new Date(oCalendar.Year,oCalendar.Month-1,1);
	var dEndDate=new Date(iif(oCalendar.Month==12,iNextYear,oCalendar.Year), 
		iif(oCalendar.Month==12,0,oCalendar.Month),1);
	dEndDate.setDate(dEndDate.getDate()-1);
	
	oCalendar.DayDropDown.options.length=0;
	
	while (dCurrentDate<=dEndDate) {
		
		var oOption=document.createElement('option');
		oOption.innerHTML=dCurrentDate.getDate()+' ('+aDays[dCurrentDate.getDay()]+')';
		oOption.value=dCurrentDate.getDate();
	
		if (dCurrentDate.getDate()==oCalendar.Day) {
			oOption.selected=true;
		}	
		oCalendar.DayDropDown.appendChild(oOption);			
					
		dCurrentDate.setDate(dCurrentDate.getDate()+1);
	}
}

function DrawCalendar(oCalendar, bSuppressDayRedraw) {

	var aDays=new Array('Mo','Tu','We','Th','Fr','Sa','Su');
	
	//make table
	var oTable=document.createElement('table');
	var oBody=document.createElement('tbody');
	oTable.appendChild(oBody);
	
	//control row
	var oTR=document.createElement('tr');
	oBody.appendChild(oTR);
	oTR.className='calendarcontrol';
	
	var oTD=document.createElement('td');
	oTD.colSpan=7;
	oTR.appendChild(oTD)
	
	var aLink=document.createElement('a');
	aLink.href='javascript:CalendarPreviousMonth(\''+oCalendar.Base+'\')';
	aLink.className='calendarpreviousmonth'
	oTD.appendChild(aLink);
	
	aLink=document.createElement('a');
	aLink.href='javascript:CalendarNextMonth(\''+oCalendar.Base+'\')';
	aLink.className='calendarnextmonth'
	oTD.appendChild(aLink);
	
	aLink=document.createElement('a');
	aLink.href='javascript:HideCalendar(\''+oCalendar.Base+'\')';
	aLink.className='calendarhide'
	oTD.appendChild(aLink);
	
	//day rows
	oTR=document.createElement('tr');
	var oTH;
	for (var i=0;i<aDays.length;i++) {
		oTH=document.createElement('th');
		oTH.innerHTML=aDays[i];
		oTR.appendChild(oTH);
	}
	oBody.appendChild(oTR);
	
	//date work
	var iNextYear=parseInt(oCalendar.Year)+1;
	var dStartDate=new Date(oCalendar.Year,oCalendar.Month-1,1);
	var dEndDate=new Date(iif(oCalendar.Month==12,iNextYear,oCalendar.Year), 
		iif(oCalendar.Month==12,0,oCalendar.Month),1);
	dEndDate.setDate(dEndDate.getDate()-1);
	
	var dCurrentDate=new Date(oCalendar.Year,oCalendar.Month-1,1);
	if (dCurrentDate.getDay()==0) {
		dCurrentDate.setDate(dCurrentDate.getDate()-6);
	} else {
		dCurrentDate.setDate(dCurrentDate.getDate()-dCurrentDate.getDay()+1);
	}

	var dToday=new Date();
	dToday=new Date(dToday.getFullYear(), dToday.getMonth(), dToday.getDate());

	//work out which date to display
	var iDayOfMonth=iif(oCalendar.Day<dEndDate.getDate(),oCalendar.Day,dEndDate.getDate());
	if (new Date(oCalendar.Year,oCalendar.Month-1,iDayOfMonth)<=dToday) {
		iDayOfMonth=dToday.getDate();
		oCalendar.Day=iDayOfMonth;
	}
	
	
	//each row
	// count rows to determine height
	var iRowHeight=35;
	for (var i=0;i<6;i++) {
		oTR=document.createElement('tr');
		oBody.appendChild(oTR);
		
		iRowHeight+=21;
		for (var j=0;j<7;j++) {
			var oTD=document.createElement('td');
			
		
			if (dCurrentDate>=dStartDate && dCurrentDate<=dEndDate) {
			
				if (dCurrentDate>=dToday) {
					var aLink=document.createElement('a');
					aLink.href='javascript:SetCalendarDate(\''+oCalendar.Base+'\','+dCurrentDate.getDate()+')';
					aLink.innerHTML=dCurrentDate.getDate();
					aLink.id=oCalendar.Base+'_'+dCurrentDate.getDate();
											
					if (dCurrentDate.getDate()==iDayOfMonth) {
						aLink.className='selected';
					}
					
					oTD.appendChild(aLink);
				} else {
					oTD.innerHTML=dCurrentDate.getDate();
					oTD.className='old';
				}						
			} else {
				oTD.className='dead';
			}
			
			dCurrentDate.setDate(dCurrentDate.getDate()+1);
			oTR.appendChild(oTD);
		}
		
		if (dCurrentDate>dEndDate) {
			break;
		}

	}
	
	if (bSuppressDayRedraw==undefined) {
		CalendarPopulateDays(oCalendar);
	}
	
	
	oCalendar.Calendar.style.height=iRowHeight+2+'px';
	oCalendar.Mask.style.height=iRowHeight+4+'px';
	oCalendar.Calendar.innerHTML='';
	oCalendar.Calendar.appendChild(oTable);

}

function CalendarPreviousMonth(sBase) {
	
	var oYearMonth=document.getElementById(sBase+'_MonthYear');
	if (oYearMonth.selectedIndex>0) {
		oYearMonth.selectedIndex--;
		var oCalendar=new Calendar(null,sBase);
		DrawCalendar(oCalendar);
	}
	
}

function CalendarNextMonth(sBase) {
	
	var oYearMonth=document.getElementById(sBase+'_MonthYear');
	if (oYearMonth.selectedIndex<oYearMonth.length-1) {
		oYearMonth.selectedIndex++;
		var oCalendar=new Calendar(null,sBase);
		DrawCalendar(oCalendar);
	}
}

function Cal_ShowCalendar(oDropDown, bSuppressDayRedraw) {

	var oCalendar=new Calendar(oDropDown);
	oCalendar.SetPosition();
	
	DrawCalendar(oCalendar, bSuppressDayRedraw);
	
	oCalendar.Mask.style.display='block';
	oCalendar.Calendar.style.display='block';
}


function HideCalendar(sBase) {
	document.getElementById(sBase+'_Mask').style.display='none';
	document.getElementById(sBase+'_Calendar').style.display='none';
	var oDropDown=document.getElementById(sBase+'_MonthYear')
	oDropDown.onchange();
}


