/*
   Milonic DHTML Menu - Menu Date Picker Module mm_datapicer.js version 1.0 - August 1 2006
   This module is only compatible with the Milonic DHTML Menu version 5.7 or higher

   Copyright 2004 (c) Milonic Solutions Limited. All Rights Reserved.
   This is a commercial software product, please visit http://www.milonic.com/ for more information.
   
   The object of this module is to enable data entered as a HTML un-ordered list to be used inside menus.
   Data for menu items is taken from HTML Lists and used for menus. Styles are created as normal DHTML Menus and referenced as a CSS classname.
*/

_menuCloseDelay=350           // The time delay for menus to remain visible on mouse out 
_menuOpenDelay=150            // The time delay before menus open on mouse over 
_subOffsetTop=0              // Sub menu top offset 
_subOffsetLeft=0            // Sub menu left offset 

with(calendarStyle=new mm_style()){ 
   headerbgcolor="#ffffff"; 
   pagebgcolor="#f0f0f0"; 
   headercolor="#000000"; 
   onbgcolor="#c0c0c0"; 
   oncolor="#0000FF"; 
   headerbgcolor="#c0c0c0"; 
   headercolor="#000000"; 
   headerborder="1px solid #a0a0a0"; 
   offbgcolor="#f0f0f0"; 
   offcolor="#000000"; 
   bordercolor="#555555"; 
   borderstyle="solid"; 
   borderwidth=1; 
   padding="3px 4px"; 
   margin=3; 
   fontsize="55%"; 
   fontstyle="normal"; 
   fontweight="bold"; 
   fontfamily="Verdana, Tahoma, Arial"; 
   overfilter="Fade(duration=0.2);Alpha(opacity=95);Shadow(color='#777777', Direction=135, Strength=2)"; 
   outfilter="Fade(duration=0.3)"; 
   onbgcolor="#C1D2EE"; 
   oncolor="#000000"; 
   onborder="1px solid #316AC5"; 
   
} 

/*
with(milonic=new menuname("Main Menu")){ 
   style=calendarStyle; 
   top=100
   left=200
   alwaysvisible=1; 
   orientation="horizontal"; 
   aI("text=Calendar;showmenu=calendar"); 
} 

*/

dateFormat="dd/mm/yyyy"; // Use d for day, m for month and y for year, must be lowercase
dayNames=new Array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
monthNames=new Array("January","February","March","April","May","June","July","August","September","October","November","December")
dayNameDisplayChars=1
dayItemWidth=15
dayIteo_=15
startDayNum=1
dayCSS=";color:red;"
todayCSS=";color:blue;background:#c0c0c0"
originalDateCSS=";color:green;"




_dte=new Date()
function convertDate(_date)
{
	_day=_month=_year=""
	for(_a=0;_a<dateFormat.length;_a++)
	{
		_dF=dateFormat.substr(_a,1);_dT=_date.substr(_a,1)
		if(_dF=="d")_day+=_dT
		if(_dF=="m")_month+=_dT
		if(_dF=="y")_year+=_dT
	}	
	if(_year.length==4)
		//alert(monthNames[parseFloat(_month-1)]+" "+parseFloat(_day)+", "+_year+",12:00:00")
		outDate=new Date(monthNames[parseFloat(_month-1)]+" "+parseFloat(_day)+", "+_year+",12:00:00")
		
	else
		outDate=new Date(_year,(_month-1),_day,12)
		//alert(_year+" - "+_month+" - "+_day+" - "+12)
//alert(outDate)		
	return outDate
}



function getUTCDayFromFirstMonthDay(_date)
{
	dy1=new Date(_date.getFullYear(),_date.getUTCMonth(),1,12)
	day1=dy1.getUTCDay();
	return day1
}


function isLeapYear(_date) {
var Year = _date.getUTCFullYear()
if (((Year%4)==0)&&((Year%100)!=0)||((Year%400)==0)){return (true)}else{return (false)}
}


function getDaysInMonth(_date){
	var _dim = new Array(31,28,31,30,31,30,31,31,30,31,30,31)
	if (isLeapYear(_date)==true){_dim[1] = 29}
	return _dim[_date.getUTCMonth()]
}

function redrawCalendar(_date)
{
	_dIM=getDaysInMonth(_date)
	_dy=getUTCDayFromFirstMonthDay(_date)-1
	if(_dy==-1)_dy=6
	s_=0
	
	for(_cal=0;_cal<_mi.length;_cal++){
		if(_mi[_cal][35]=="mm_calendar"){
			s_=_cal-1
			_cal=_mi.length
		}
	}
	
	for(_cal=1;_cal<43;_cal++)
	{
		_mi[_cal+s_][78]=""
		_cdy=(_cal-_dy)
		if(_cdy>0 && _cdy<_dIM+1)
		{
			_mi[_cal+s_][1]=_cdy
			_mi[_cal+s_][96]=0
			_iRCSS=setCSS(_date,_cdy)
			if(_iRCSS)
			{
				_mi[_cal+s_][78]=_iRCSS
			}
			$Cw(_cal)
		}
		else
		{
			_mi[_cal+s_][1]="&nbsp;"
			_mi[_cal+s_][96]=1
			
			
		}
	}
	_mi[s_-8][1]=drawDateLine()
	g$($h("calendar"))
	//alert(_it)
}

function mm_calChangeMonth(_adj)
{	
	days=_dte.getUTCDate()
	_dIM=getDaysInMonth(new Date(_dte.getFullYear(),_dte.getUTCMonth()+_adj,1,12))
	if(_dIM<_dte.getUTCDate())days=_dIM

	_dte=new Date(_dte.getFullYear(),_dte.getUTCMonth()+_adj,days,12)
	redrawCalendar(_dte)
}

function mm_calChangeYear(_adj)
{
	days=_dte.getUTCDate()
	_dIM=getDaysInMonth(new Date(_dte.getFullYear()+_adj,_dte.getUTCMonth(),1,12))
	if(_dIM<_dte.getUTCDate())days=_dIM


	_dte=new Date(_dte.getFullYear()+_adj,_dte.getUTCMonth(),days,12)
	redrawCalendar(_dte)
}

function cCStatus(t)
{
	_cCS=$c("_calStatus")
	if(t+" "==$u)t="&nbsp;"
	_cCS.innerHTML=t
	//closeCalSubs();
}

function redoYears()
{	
}


function calmover(_th)
{
	closeCalSubs();
	_th.style.cursor="hand"
	_th.style.textDecoration="underline"
}


function calmout(_th)
{
	_th.style.cursor="default"
	_th.style.textDecoration="none"
}


function closeCalendar()
{
	_M=$h('calendar');
	$Y(_M,0);	
	//closeCalSubs()
}

function closeCalSubs()
{
	$Y($h('cal_years'),0);
	$Y($h('cal_months'),0);
	$Y($h('cal_about'),0);
}


function _adjustYears(o)
{
	_cSc=$c("menu"+$h("cal_years"))
	_Mcal=_m[$h("cal_years")][0]
//	alert(_Mcal[0] + " - " + _Mcal[_Mcal.length-1])
	for(_cal=_Mcal[0]+1;_cal<_Mcal[_Mcal.length-1];_cal++)
	{
		_nYr=(parseInt(_mi[_cal][1])+(o))
		_mi[_cal][1]=_nYr
		_mi[_cal][62]="updateByYear("+_nYr+")"
		//alert(_mi[_cal][1] + " - " + (parseInt(_mi[_cal][1])+(o)) + " - " + o)
	}
//	alert(_cSc)
_cSc.innerHTML=o$($h("cal_years"),0)
}

function drawDateLine()
{
	/*
	dDL=""
	dDL+="<span onmouseup=mm_calChangeMonth(-1)><img src=/images/offstepup></span><span onmouseup=mm_calChangeMonth(1)><img src=/images/offstepdown></span>"
	dDL+="<span id=month_div name=month_div onmouseover=calmover(this) onmouseout=calmout(this) onmouseup=_m[getMenuByname(cal_months)].tooltip=1;popup('cal_months','month_div',-this.offsetHeight,-2)>"+monthNames[_dte.getUTCMonth()]+"</span>&nbsp;"
	dDL+=_dte.getUTCDate()+"&nbsp;"
	dDL+="<span id=year_div name=year_div onmouseover=calmover(this) onmouseout=calmout(this) onmouseup=redoYears();popup('cal_years','year_div',-this.offsetHeight,-2)>"+_dte.getFullYear()+"</span>&nbsp;"
	dDL+="<span onmouseup=mm_calChangeYear(-1)><img src=/images/offstepup></span><span onmouseup=mm_calChangeYear(1)><img src=/images/offstepdown></span>"
*/

	//alert(calendarStyle.fontsize)
	
dDL="\<style>td.cal22{font-weight:bold;font-size:"+calendarStyle.fontsize+";}</style>\
<table border=0 width=100% cellpadding=0 cellspacing=0><tr>\
<td class=cal22 width=1><span id=about_div name=about_div onmouseover=cCStatus('Help');calmover(this) onmouseout=cCStatus();calmout(this) onmouseup=popup('cal_about','about_div',2,-2)>?</span></td>				\
<td class=cal22 width=100% align=center>\
<span id=month_div name=month_div onmouseover=\"cCStatus('Select Months');calmover(this)\" onmouseout=cCStatus();calmout(this) onmouseup=popup('cal_months','month_div',2,-2)>"+monthNames[_dte.getUTCMonth()]+"</span>\
&nbsp"+_dte.getUTCDate()+"&nbsp;\
<span id=year_div name=year_div onmouseover=\"cCStatus('Select Years');calmover(this)\" onmouseout=cCStatus();calmout(this) onmouseup=redoYears();popup('cal_years','year_div',2,-2)>"+_dte.getFullYear()+"</span>&nbsp;\
<td class=cal22 align=right><span id=close_div name=close_div onmouseover=\"cCStatus('Close This Window');calmover(this)\" onmouseout=cCStatus();calmout(this) onmouseup=closeCalendar()>X</span></td>				\
</tr>\
</table>\
<table border=0 width=100% cellpadding=0 cellspacing=0><tr>\
<tr>\
<td class=cal22 width=25%><span onmouseover=\"cCStatus('Previous Year');calmover(this)\" onmouseout=calmout(this) onmouseup=cCStatus();mm_calChangeYear(-1)>&lt;&lt;</span></td>				\
<td class=cal22 width=25%><span onmouseover=\"cCStatus('Previous Month');calmover(this)\" onmouseout=calmout(this) onmouseup=cCStatus();mm_calChangeMonth(-1)>&lt;</span></td>				\
<td class=cal22 width=25% align=right><span onmouseover=\"cCStatus('Next Month');calmover(this)\" onmouseout=calmout(this) onmouseup=cCStatus();mm_calChangeMonth(1)>&gt;</span></td>				\
<td class=cal22 width=25% align=right><span onmouseover=\"cCStatus('Next Year');calmover(this)\" onmouseout=calmout(this) onmouseup=cCStatus();mm_calChangeYear(1)>&gt;&gt;</span></td>				\
</td>\
</tr></table>				\
"
	

	
	return dDL
}


function drawStatusLine()
{
dDL="\<style>td.cal23{font-weight:bold;font-size:"+calendarStyle.fontsize+";}</style>\
<table border=0 width=100% cellpadding=0 cellspacing=0><tr>\
<td id=_calStatus class=cal23 align=center nowrap >&nbsp;</td>				\
</td>\
</tr></table>				\
"
return dDL
}

function isToday(_date)
{
	now=new Date()
	if(_date.getUTCMonth()==now.getUTCMonth()&&_date.getUTCDate()==now.getUTCDate()&&_date.getFullYear()==now.getFullYear())return true;
	return false;
}

function setCSS(_date,_day)
{
_iRCSS=""
if(originalDate.getFullYear()==_date.getFullYear()&&originalDate.getUTCMonth()==_date.getUTCMonth()&&originalDate.getUTCDate()==_day)
_iRCSS=originalDateCSS
itD=new Date(_date.getFullYear(),_date.getUTCMonth(),_day)
if(isToday(itD))_iRCSS=todayCSS
if(_date.getUTCDate()==_day)_iRCSS=dayCSS
return _iRCSS;
}

originalDate=_n

function drawCalendar(_date)
{		
	if(_date.length)_dte=convertDate(_date); else _dte=_date
	originalDate=_dte
	_dIM=getDaysInMonth(_dte)
	_dy=getUTCDayFromFirstMonthDay(_dte)-1
	if(_dy==-1)_dy=6
	milonic.aI("text=`"+drawDateLine(_dte)+"`;dividespan=7;type=form")
	for(_cal=0;_cal<7;_cal++){
		milonic.aI("type=header;text="+dayNames[_cal].substr(0,dayNameDisplayChars)); 
	}
	milonic.aI("text= ;fontsize=1;dividespan=7;itemheight=3;disabled=true")
	
	_iBord=""
	if(_W.itemBorder)_iBord="offborder="+itemBorder
	
	isToday(_dte)
	for(_cal=1;_cal<43;_cal++){
		_cdy=(_cal-_dy)
		
		iTxtx=";clickfunction=updateByDay();target=mm_calendar;itemwidth="+dayItemWidth+";itemheight="+dayIteo_+";"+_iBord
		
		if(_cdy>0 && _cdy<_dIM+1){
			_iRCSS="rawcss="+setCSS(_dte,_cdy)
			milonic.aI(iTxtx+";"+_iRCSS+";text="+_cdy+";onfunction=closeCalSubs();cCStatus('"+_cdy+"')"); 
		}
		else{
			milonic.aI(iTxtx+";text=&nbsp;disabled=true"); 
		}
	}
	milonic.aI("text=`"+drawStatusLine()+"`;dividespan=7;type=form")
}

function updateByMonth(_mnth){
	days=_dte.getUTCDate()
	_dIM=getDaysInMonth(new Date(_dte.getFullYear(),_mnth,1,12))
	if(_dIM<_dte.getUTCDate())days=_dIM
	
	
	_dte=new Date(_dte.getFullYear(),_mnth,days,12)
	redrawCalendar(_dte)
	closeCalSubs()
}

function updateByYear(_year){
	days=_dte.getUTCDate()	
	_dIM=getDaysInMonth(new Date(_year,_dte.getMonth(),1,12))
	if(_dIM<_dte.getUTCDate())days=_dIM
	
	_dte=new Date(_year,_dte.getMonth(),days,12)
	redrawCalendar(_dte)
	closeCalSubs()
}

function updateByDay(){
	_dte=new Date(_dte.getFullYear(),_dte.getUTCMonth(),_mi[_itemRef][1],12)
	redrawCalendar(_dte)
}


with(milonic=new menuname('cal_months')){
	style=calendarStyle;
	margin=6
	overflow="scroll"
	zindex=99999
	for(_cal=0;_cal<monthNames.length;_cal++)aI("clickfunction=updateByMonth("+_cal+");text="+monthNames[_cal])
}

with(milonic=new menuname('cal_years')){
	style=calendarStyle;
	margin=6
	overflow="scroll"
	zindex=99999
	_yR=_dte.getFullYear()
	aI("text=<<;clickfunction=_adjustYears(-1)")
	for(_cal=-4;_cal<10;_cal++)aI("clickfunction=updateByYear("+(_yR+_cal)+");text="+(_yR+_cal))
	aI("text=>>;clickfunction=_adjustYears(1)")
}

with(milonic=new menuname('cal_about')){
	style=calendarStyle;
	margin=6
	overflow="scroll"
	zindex=99999
	aI("text=about this Calendar;")
}

with(milonic=new menuname("calendar")){ 
   style=calendarStyle; 
   margin=style.margin; 
   divides=7
   keepalive=1
   align="center"
   //alwaysvisible=1
   drawCalendar("23/11/2005");
   //drawCalendar(new Date());
} 
//inEditMode=1

drawMenus();
