// Copyright (c)2004,2005 All rights reserved.

HCAL_LAYER='HCAL';HCAL_OPEN=null;HCAL_FIELD=null;HCAL_DATE=new Date();HCAL_STARTDATE=new Date();HCAL_DIS=false;

//function my_h_open(a,d){h_open_pos(a,getW(a)-getW(a),-190,d);}
//function my_h_open(a,d){h_open_pos(a,getW(a)-getW(a),getH(a),d);} was in All-Caribbean

// function h_open(a,d){h_open_pos(a,getW(a)-getW(a)-160,0,d);} // from All-Caribbean
function h_open(a,d){h_open_pos(a,getW(a)+10,0,d);}
function h_open_pos(a,xpos,ypos,d){HCAL_DIS=d;var o=getObject(HCAL_LAYER);var n=(HCAL_OPEN==null);if(HCAL_FIELD&&(a==HCAL_FIELD))return false;if(!n)hideObject(HCAL_OPEN);HCAL_OPEN=o;HCAL_FIELD=a;var d;if(a.name=='FromDate'){d=new Date();d.setDate(d.getDate()+3);HCAL_STARTDATE=new Date();}else{d=new Date(HCAL_DATE.getFullYear(),HCAL_DATE.getMonth(),HCAL_DATE.getDate());d.setDate(d.getDate());HCAL_STARTDATE=d;}h_renderMonth(d.getMonth()+1,d.getFullYear());moveObject(o,getX(a)+xpos,getY(a)+ypos);showObject(o);if(n)trackMouseClick(h_checkClose);}
function h_checkClose(e){if(HCAL_OPEN==null)return;var x=getEventX(e);var y=getEventY(e);if(!(isInside(HCAL_FIELD,x,y,0,0,4,0)||isInside(HCAL_OPEN,x,y)))h_close();}
function h_setDate(y,m,d){if(HCAL_OPEN==null)return;var f=HCAL_FIELD;f.value=(m<10?'0':'')+m+'/'+(d<10?'0':'')+d+'/'+y;if(f.name=='FromDate')HCAL_DATE=new Date(y,m-1,d);h_close();if(HCAL_DIS)distributeFormValue(f);}
function h_close(){hideObject(HCAL_OPEN);if(HCAL_OPEN!=null)untrackMouseClick();HCAL_OPEN=null;HCAL_FIELD=null;}

function h_renderMonth(month,year)
{
	if(HCAL_OPEN==null)
	{
		return;
	}
	var t_y=HCAL_STARTDATE.getFullYear();
	var t_m=HCAL_STARTDATE.getMonth()+1;
	var t_d=HCAL_STARTDATE.getDate();
	var d=new Date(year,month-1,1);
	var max=(month==2?28+((year%4==0)&&((year%100!=0)||(year%400==0))?1:0):30+(month<8?1-((month-1)%2):(month-1)%2));
	var html='<table>' + 
	         '  <tr><td class="calWrapper"><table class="cal" cellspacing="1">' +
	         '    <tr><td class="month"><a href="javascript:void(0)" onclick="h_renderMonth('+(month>1?month-1:12)+',' +(month>1?year:year-1)+');">&laquo;</a></td>' +
	         '      <td colspan=5 class="month">'+MONTHS[month-1].toUpperCase()+' '+year+'</td>' +
	         '      <td class="month"><a href="javascript:void(0)" onclick="h_renderMonth('+(month<12?month+1:1)+','+(month<12?year:year+1)+');">&raquo;</a></td></tr>' +
	         '    <tr><td class="week">S</td>' +
	         '      <td class="week">M</td>' +
	         '      <td class="week">T</td>' +
	         '      <td class="week">W</td>' +
	         '      <td class="week">T</td>' +
	         '      <td class="week">F</td>' +
	         '      <td class="week">S</td></tr>';
	         
	var offset=d.getDay()+1;
	var count=1, i, a;
	for (i=1;i<=max;i++)
	{
		if(count==1)
		{
			html+='<tr>';
		}
		if((i==1)&&(offset>1))
		{
			html+='      <td colspan='+(offset-1)+' class="emptyDay">&nbsp;</td>';
			count=offset;
		}
		a=((year>t_y)||((year==t_y)&&((month>t_m)||((month==t_m)&&(i>=t_d))))); // If I want +1 day i>=t_d+1
		if (a)
		{
			html+='      <td class="activeDay"><a href="javascript:void(0)" onclick="h_setDate('+year+','+month+','+i+')">'+i+'</a></td>';
		}
		else
		{
			html+='      <td class="inactiveDay">'+i+'</td>';
		}
				
		if((i==max)&&(count<7))
		{
			html+='      <td colspan='+(7-count)+' class="emptyDay">&nbsp;</td>';
			count=7;
		}
		count++;
		
		if(count>7)
		{
			count=1;
			html+='</tr>';
		}
	}
	html+='</table></td></tr></table>';
	writeObject(HCAL_OPEN,html);
}

