////No need to edit beyond here
var ie4=document.all&&navigator.userAgent.indexOf("Opera")==-1
var ns6=document.getElementById&&!document.all
var ns4=document.layers
var ver=parseInt(navigator.appVersion);
var ns3=(ver==3 && navigator.appName=="Netscape")?1:0;
var returnTime;

var aMonth = new Array ("January","February",  "March","April","May","June","July","August","September","October","November","December");
var aEndDate = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

function FindObject(item) {
	if( window.mmIsOpera ) return(document.getElementById(item));
	if (document.all) return(document.all[item]);
	if (document.getElementById) return(document.getElementById(item));
	return(false);
}

function FindObjPosition( oLink )
{		if( oLink.offsetParent )
		{		for( var posX = 0, posY = 0; oLink.offsetParent; oLink = oLink.offsetParent )
				{		posX += oLink.offsetLeft;
						posY += oLink.offsetTop;
				}
				return [ posX, posY ];
		 }
		 else return [ oLink.x, oLink.y ];
}


function ShowCalendar(name)
{		var control = FindObject('_' + name);
		var control_position = FindObjPosition(control);
		control.thestyle=(ie4||ns6)? control.style : control;
		// หาตำแหน่งรูป
		var control_img_position = FindObjPosition(FindObject('img' + name));

		//เซตตำแหน่งเมนู
		control.thestyle.top=(control_img_position[1]-140) + 'px';
		control.thestyle.left= (control_img_position[0] + 16)+ 'px';

		// เซต การมองเห็น
		if (control.thestyle.visibility=="visible")
		{		control.thestyle.visibility="hidden";

				var control_item = FindObject('_' + name + '_Item');
				control_item.thestyle=(ie4||ns6)? control_item.style : control_item;
				control_item.thestyle.visibility="hidden";
		}
		else control.thestyle.visibility="visible";

		var now = new Date();
		var control_input= FindObject(name);
		if (control_input.value.length>0)
		{		var aValue = control_input.value.split('-');
				now.setYear(parseInt(aValue[0]));
				now.setMonth(parseInt(aValue[1])-1);
				now.setDate(parseInt(aValue[2]));
				/*
				var temp = aValue[2].split(' ');
				//var bValue = temp[1].split(':');
				if (bValue.length==3)
				{
						returnTime = true;
				}*/
		}
		WriteCalendar(name , now);
}

function WriteCalendar(name , now)
{
				var a = now.getYear();
				if (a<100)a += 1900;
				var nowdate = a + '-' + (1+now.getMonth()) + '-' + now.getDate() ;

				var prev_year =  (a-1) + '-' + (1+now.getMonth()) + '-' + now.getDate();
				var prev_month =  (a) + '-' + (now.getMonth()) + '-' + now.getDate();
				var next_month =  (a) + '-' + (2+now.getMonth()) + '-' + now.getDate();
				var next_year =  (a+1) + '-' + (1+now.getMonth()) + '-' + now.getDate();

				var control = FindObject('_' + name);
				var result = '<table width="100%" border="0" cellpadding="2" cellspacing="0" class="Note" bgColor="#FFFFFF">';
				result += '<tr>';
				result += '<td width="27%" align=\"center\"><span onClick=ShowSubCalendar(0,"'+ name + '","' + nowdate + '") style="cursor:hand">' + now.getDate() + '</span></td>';
				result += '<td width="27%" align=\"center\"><span onClick=ShowSubCalendar(1,"'+ name + '","' + nowdate + '") style="cursor:hand" >' + aMonth[now.getMonth()] + '</span> ' +'</td>';
				result += '<td width="27%" align=\"center\"><span onClick=ShowSubCalendar(2,"'+ name + '","' + nowdate + '") style="cursor:hand" >' + a + '</span> ' +'</td>';
				result += '<td width="10%" align="center" style="border:1px solid;"><a title="OK" href="JavaScript:void(0);" onClick=SelectDate(3,"' + name + '","' + nowdate + '","' + nowdate + '",1);>Y</a></td>';
				result += '<td width="10%" align="center" style="border:1px solid;"><a title="Cancel" href="JavaScript:void(0);" onClick=CloseCalendar("' + name + '");>X</a></td>';
				result += '</tr>';
				result += '</table>';

				result += '<table width="100%" border="1" BorderColor="#000000" BorderColorDark="#FFFFFF" bgColor="#F0F0F0" cellpadding="2" cellspacing="0" class="Note">';
				result += '<tr align="center"><td><a href="JavaScript:void(0);" onClick=SelectDate(3,"' + name + '","' + nowdate + '","' + prev_year + '",0);>&lt;&lt;</a></td>';
				result += '<td align="center"><a href="JavaScript:void(0);" onClick=SelectDate(3,"' + name + '","' + nowdate + '","' + prev_month + '",0);>&lt;</a></td>';
				//result += '<td align="center"><a href="JavaScript:void(0);" onClick=SelectDate(4,"' + name + '","' + nowdate + '","' + nowdate + '",0);>Today</a></td>';
				result += '<td align="center"><a href="JavaScript:void(0);" onClick=SelectDate(3,"' + name + '","' + nowdate + '","' + next_month + '",0);>&gt;</a></td>';
				result += '<td align="center"><a href="JavaScript:void(0);" onClick=SelectDate(3,"' + name + '","' + nowdate + '","' + next_year + '",0);>&gt;&gt;</a></td></tr>';
				result += '</table>';


				result += '<table width="100%" border="1" BorderColor="#000000" BorderColorDark="#FFFFFF" cellpadding="1" cellspacing="0" class="Note" bgColor="#FFFFFF">';
				result += '<tr><td align="center" bgColor="#FFFFCC"><b>S</b></td><td align="center"><b>M</b></td><td align="center"><b>T</b></td><td align="center"><b>W</b></td><td align="center"><b>T</b></td><td align="center"><b>F</b></td><td align="center" bgColor="#FFFFCC"><b>S</b></td></tr> <tr>';
				Value1 = new Date(now.getYear() , now.getMonth(), 1);
				for (k=0; k< Value1.getDay(); k++)
				{	result += '<td>&nbsp;</td>';
				}
				var temp_date = a + '-' + (1+now.getMonth()) + '-' ;
				var j = Value1.getDay()+1;
				for (i=1;i<=aEndDate[now.getMonth()] ;i++ , j++)
				{		if (j>7 )
						{		j = 1;
								result += '</tr><tr>';
								result += '<td bgColor="#FFFFCC" align="center" onMouseOver="this.bgColor=\'#F0F0FF\'" onMouseOut="this.bgColor=\'#FFFFCC\'" style="cursor:hand" onClick=SelectDate(3,"' + name + '","' + nowdate + '","' + temp_date + i + '",1);>' + i +  '</td>';
						}
						else if (j==7)
						{
							result += '<td bgColor="#FFFFCC" align="center" onMouseOver="this.bgColor=\'#F0F0FF\'" onMouseOut="this.bgColor=\'#FFFFCC\'" style="cursor:hand" onClick=SelectDate(3,"' + name + '","' + nowdate + '","' + temp_date + i + '",1);>' + i +  '</td>';
						}
						else result += '<td align="center" onMouseOver="this.bgColor=\'#F0F0FF\'" onMouseOut="this.bgColor=\'\'" style="cursor:hand" onClick=SelectDate(3,"' + name + '","' + nowdate + '","' + temp_date + i + '",1);>' + i +  '</td>';
				}
				result += '</tr>';
				var today = new Date();
				result += '<tr><td style=\"cursor:hand\" colspan=\"7\" align=\"center\" bgColor=\"#F0F0F0\" onClick=SelectDate(4,"' + name + '","' + nowdate + '","' + nowdate + '",1);>today < ' +  (today.getDate()) + ' ' + aMonth[today.getMonth()]  + ' ' + today.getYear() + ' > </td></tr>';
				result += '</table>';
				control.innerHTML = result;
}

function ShowSubCalendar(type , name , nowdate)
{			var control_item = FindObject('_' + name + '_Item');
			var control_img_position = FindObjPosition(FindObject('img' + name));
			control_item.thestyle=(ie4||ns6)? control_item.style : control_item;

			var now = new Date();
			if (nowdate.length>0)
			{		var aValue = nowdate.split('-');
					now.setYear(parseInt(aValue[0]));
					now.setMonth(parseInt(aValue[1])-1);
					now.setDate(parseInt(aValue[2]));
			}
			var a = now.getYear();
			if (a<100)	a += 1900;

			// เซต การมองเห็น
			if (control_item.thestyle.visibility=="visible")
			{		control_item.thestyle.visibility="hidden";
			}
			else control_item.thestyle.visibility="visible";

			var start = stop = 0;
			var result = '<table width="100%" border="0" cellpadding="1" cellspacing="0" class="Note">';
			if (type==0)
			{		start = now.getDate()-7;
					stop = now.getDate()+7;
					if (start<1) start = 1;
					if (stop>aEndDate[now.getMonth()])	stop = aEndDate[now.getMonth()];

					control_item.thestyle.top=(control_img_position[1]-140 - (Math.abs((start - now.getDate())+1)*15)) + 'px';
					control_item.thestyle.left= (control_img_position[0] + 41)+ 'px';

					for (i=start; i<= stop; i++ )
					{		result += '<tr><td align="center"><a href="JavaScript:void(0);" onClick=SelectDate('+ type + ',"' + name + '","' + nowdate + '","' + i + '")>' + i + '</a></td></tr>';
					}
			}
			else if (type==1) // Month
			{		start = 0;
					stop = 11;

					control_item.thestyle.top=(control_img_position[1]-124- (Math.abs(start - now.getMonth()-1)*15)) + 'px';
					control_item.thestyle.left= (control_img_position[0] + 90)+ 'px';

					for (i=start; i<= stop; i++ )
					{		result += '<tr><td align="center"><a href="JavaScript:void(0);" onClick=SelectDate('+ type + ',"' + name + '","' + nowdate + '","' + i + '")>' + aMonth[i] + '</a></td></tr>';
					}
			}
			else
			{		start = a-5;
					stop = a+5;

					control_item.thestyle.top=(control_img_position[1]-124- (Math.abs(start - a-1)*15)) + 'px';
					control_item.thestyle.left= (control_img_position[0] + 168)+ 'px';
					for (i=start; i<= stop; i++ )
					{		result += '<tr><td align="center"><a href="JavaScript:void(0);" onClick=SelectDate('+ type + ',"' + name + '","' + nowdate + '","' + i + '")>' + i + '</a></td></tr>';
					}

			}
			result += '</table>';

			control_item.innerHTML = result;
}

function SelectDate(type , name, nowdate , value , action)
{			var control_item = FindObject('_' + name + '_Item');
			control_item.thestyle=(ie4||ns6)? control_item.style : control_item;
			control_item.thestyle.visibility = 'hidden';

			var now = new Date();
			if (nowdate.length>0)
			{		var aValue = nowdate.split('-');
					now.setYear(parseInt(aValue[0]));
					now.setMonth(parseInt(aValue[1])-1);
					now.setDate(parseInt(aValue[2]));
			}

			if (type==0)
			{		now.setDate(parseInt(value));
			}
			else if (type==1)
			{		var mon = parseInt(value);
					//alert(mon + ' , ' + now.getMonth());
					if (now.getMonth() != mon) now.setMonth(mon);
			}
			else if(type==2)
			{		now.setYear(parseInt(value));
			}
			else if (type==3)
			{		var aValue = value.split('-');
					now.setYear(parseInt(aValue[0]));
					now.setMonth(parseInt(aValue[1])-1);
					now.setDate(parseInt(aValue[2]));
			}
			else
			{		var now = new Date();
			}
			if (action ==1)
			{		ReturnDate(name , now);
					CloseCalendar(name);
			}
			else WriteCalendar(name , now);
}

function ReturnDate(name , value)
{		var control_input = FindObject(name);
		var a = value.getYear();
		if (a<100)a += 1900;
		var nowdate = a + '-' + (1+value.getMonth()) + '-' + value.getDate() ;
		if (returnTime)
		{
			var temp = control_input.value.split(' ');
			control_input.value = nowdate + ' ' + temp[1];
		}
		else control_input.value = nowdate;
}

function CloseCalendar(name)
{		var control = FindObject('_' + name);

		control.thestyle=(ie4||ns6)? control.style : control;
		control.thestyle.visibility = 'hidden';

		// Sub เมนู
		var control_item = FindObject('_' + name + '_Item');
		control_item.thestyle=(ie4||ns6)? control_item.style : control_item;
		control_item.thestyle.visibility = 'hidden';
}

function getColor(name , color)
{
	var control = FindObject('_' + name);

}