///////////////////////////////////////////////////////////////////////////////////////////////////
//     Code Name:-      Javascript PopUp Calendar                                                //
//     Last Updated:-   12th January 2009                                                        //
//                                                                                               //
//     Author:-         Lee J. Bradley                                                           //
//     WebSite:-        http://www.new-it.co.uk                                                  //
//     License:-        GPL (General Public License)                                             //
//                                                                                               //
//     NOTES:-          I give authority for anyone to use this script however they see fit      //
//                      I request that these comments are left in place and request that any     //
//                      alterations made to this script a commented                              //
//     Usage:-         	1. Attach JS file                                                        //
//                      2. Any section of the document you wish to have a calendar PopUp         //
//                         from attach an 'onclick=""' or 'href="javascript:..."                 //
//                         with the following call                                               //
//                         "JSPopCal_CalBox(                                                     //
//                                          'id of input field',                                 //
//                                          'id of target location for the PopUp',               //
//                                          'DateObj Ref',                                       //
//                                          'X Offset from Left of target location (px)',        //
//                                          'Y Offset from Top of target location (px)'          //
//                                         )'                                                    //
//     Still to Fix:-   adding events in IE for dropdown & input boxes                           //
///////////////////////////////////////////////////////////////////////////////////////////////////

var JSPopCal_DateFormat		=	'DD+"/"+MM+"/"YYYY';	//set using D, DD, M, MM, YY, YYYY
var JSPopCal_DaysFull		=	new Array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
var JSPopCal_Days1		=	new Array("S","M","T","W","T","F","S");
var JSPopCal_MonthsLong		=	new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var JSPopCal_MonthsShort	=	new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var JSPopCal_MonthLength	=	new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var JSPopCal_Today		=	Date().split(" ");
var JSPopCal_CalDateObj		=	new Date();
var JSPopCal_CalDateObjs	=	new Array();
var JSPopCal_TODAY		=	new Array(JSPopCal_CalDateObj.getDate(),JSPopCal_CalDateObj.getMonth(),JSPopCal_CalDateObj.getFullYear());
var JSPopCal_IE			=	document.all;


var JSPopCal_target = "Date";
function JSPopCal_CalBox(ref,target,dateObj,XOffSet,YOffSet)
{
	// ref		-	This is the target Object for the Result
	// target	-	This is the target for the placement of the calendar
	// dateObj	-	To use the same Calendar over multiple fields use the same ref. in here,
	// XOffSet	-	Defines the offset (px) from the far LEFT of the element, NOTE	-	Must be numeric, NOT in ''
	// YOffSet	-	Defines the offset (px) from the very TOP of the element, NOTE	-	Must be numeric, NOT in ''

	if(!JSPopCal_CalDateObjs[dateObj])
	{
		JSPopCal_CalDateObjs[dateObj]	=	new Date(JSPopCal_TODAY[2],JSPopCal_TODAY[1],01);
	}
	if(XOffSet==null) {XOffSet=0};
	if(YOffSet==null) {YOffSet=0};

	JSPopCal_removeElementByID('JSPopCal_Box')
	monthlength			=	JSPopCal_MonthLength[JSPopCal_CalDateObjs[dateObj].getMonth()];
	Location			=	document.getElementById(ref);
	CalendarBox			=	document.createElement('div');
	CalendarBox.id			=	"JSPopCal_Box";
	CalendarBox.className		=	"JSPopCal_Box";	
	//	Navigation & Info Top Bar
	CalendarTopBar			=	document.createElement('div');
	CalendarTopBar.className	=	"JSPopCal_TopBar";
	CalendarPrevMonth		=	document.createElement('div');
	CalendarPrevMonth.className	=	"JSPopCal_PrevMonth";
	ChgMonth			=	document.createElement('a');
	ChgMonth.href			=	"javascript:JSPopCal_changeCalMonth('-1','"+ref+"','"+target+"','"+dateObj+"','"+XOffSet+"','"+YOffSet+"')";
	ChgMonth.appendChild(document.createTextNode("<<"));
	CalendarPrevMonth.appendChild(ChgMonth);
	CalendarYear			=	document.createElement('div');
	CalendarYear.className		=	"JSPopCal_MonthYear";
//////////////////////////////////////////////////////////////////////////////////
//     This is the old code for displaying the Calendar Month & Year.           //
//     NOW attempting to make 'month' drop down box, & 'year' as text input     //
//////////////////////////////////////////////////////////////////////////////////
/*
	CalendarYear.appendChild(document.createTextNode(JSPopCal_MonthsLong[JSPopCal_CalDateObjs[dateObj].getMonth()]+" - "+JSPopCal_CalDateObjs[dateObj].getFullYear()));
*/
	CalendarNextMonth		=	document.createElement('div');
	CalendarNextMonth.className	=	"JSPopCal_NextMonth";
	CalendarMonthSel		=	document.createElement('select');
	CalendarMonthSel.setAttribute('tabindex','100');
	CalendarMonthSel.setAttribute('id','JSPopCal_SelectMonth');
	CalendarMonthSel.setAttribute('name','JSPopCal_SelectMonth');
	if(JSPopCal_IE)
	{
		CalendarMonthSel.attachEvent('onchange',function(){JSPopCal_MonthSel(document.getElementById("JSPopCal_SelectMonth").value,ref,target,dateObj,XOffSet,YOffSet)});
	}
	else
	{
		CalendarMonthSel.setAttribute('onchange','JSPopCal_MonthSel(this.value,"'+ref+'","'+target+'","'+dateObj+'","'+XOffSet+'","'+YOffSet+'")');
	}
	var i = 0;
	while(JSPopCal_MonthsLong[i]!=null)
	{
		CalMonth		=	document.createElement('option');
		if(JSPopCal_IE)
		{
			CalMonth.setAttribute('value',i);
			//CalMonth.value	=	i;
		}
		else
		{
			CalMonth.setAttribute('value',i);
		}
		CalMonth.appendChild(document.createTextNode(JSPopCal_MonthsLong[i]));
		if(i == JSPopCal_CalDateObjs[dateObj].getMonth())
		{
			CalMonth.setAttribute('selected','selected');
		}
		CalendarMonthSel.appendChild(CalMonth);
		i	=	i+1;
	}
	CalendarYearInput		=	document.createElement('input');
	CalendarYearInput.setAttribute('tabindex','101');
	CalendarYearInput.id	=	'JSPopCal_YearInput';
	CalendarYearInput.setAttribute('value',JSPopCal_CalDateObjs[dateObj].getFullYear());
	if(JSPopCal_IE)
	{
		CalendarYearInput.attachEvent('onblur',function(){JSPopCal_YearSel(document.getElementById('JSPopCal_YearInput').value,ref,target,dateObj,XOffSet,YOffSet)});
		CalendarYearInput.attachEvent('onkeypress',function(){if (event.keyCode==13){return false}});
		//CalendarYearInput.attachEvent('onblur',function(){alert(document.getElementById('JSPopCal_YearInput').value+" - "+ref+" - "+target+" - "+dateObj+" - "+XOffSet+" - "+YOffSet)});
	}
	else
	{
		CalendarYearInput.setAttribute('onblur','JSPopCal_YearSel(this.value,"'+ref+'","'+target+'","'+dateObj+'","'+XOffSet+'","'+YOffSet+'")');
		CalendarYearInput.setAttribute('onKeyPress','if (event.keyCode==13){return false}');
	}
	CalendarYearInput.setAttribute('class','JSPopCal_Input');
	if(JSPopCal_IE)
	{
		CalendarYearInput.width	=	"28";
	}
	DateForm		=	document.createElement("form");
	DateForm.setAttribute('name','JSPopCal_DateForm');
	DateForm.setAttribute('id','JSPopCal_DateForm');
	DateForm.appendChild(CalendarMonthSel);
	DateForm.appendChild(CalendarYearInput);
	CalendarYear.appendChild(DateForm);
//	End of testing for adding drop down
	ChgMonth			=	document.createElement('a');
	ChgMonth.href			=	"javascript:JSPopCal_changeCalMonth('1','"+ref+"','"+target+"','"+dateObj+"','"+XOffSet+"','"+YOffSet+"')";
	ChgMonth.appendChild(document.createTextNode(">>"));
	CalendarNextMonth.appendChild(ChgMonth);
	CalendarTopBar.appendChild(CalendarPrevMonth);
	CalendarTopBar.appendChild(CalendarYear);
	CalendarTopBar.appendChild(CalendarNextMonth);
	CalendarBox.appendChild(CalendarTopBar);
	CalendarTab			=	document.createElement('table');
	CalendarTab.style.width		=	'100%';
	// Bar with Days of week
	Row				=	CalendarTab.insertRow(-1);
	Row.className	=	"JSPopCal_DaysOfWeek";
	for(i=0; i<JSPopCal_Days1.length; i++)
	{
		Cell			=	Row.insertCell(-1);
		Cell.appendChild(document.createTextNode(JSPopCal_Days1[i]));
	}
	// Start creating calendar dates
	Row				=	CalendarTab.insertRow(-1);
	Row.className			=	"JSPopCal_Dates";
	FirstDay			=	JSPopCal_getFirstDay(dateObj);
	for(i=0; i<FirstDay; i++)
	{
		Cell			=	Row.insertCell(-1);
		Cell.className		=	"JSPopCal_EmptyDate";
		Cell.appendChild(document.createTextNode(''));
	}
	Day	=	FirstDay;
	MonthL	=	JSPopCal_MonthLength[JSPopCal_CalDateObjs[dateObj].getMonth()];
	if(JSPopCal_CalDateObjs[dateObj].getMonth() == 1)
	{
		if((JSPopCal_CalDateObjs[dateObj].getYear()%4 == 0 && JSPopCal_CalDateObjs[dateObj].getYear()%100 != 0) || JSPopCal_CalDateObjs[dateObj].getYear()%400 == 0) //
		{
			MonthL = 29;
		}
	}
	var DayCount	=	FirstDay;
	for(i=0; i<MonthL; i++)
	{
		DayCount++;
		if(DayCount==8) {DayCount=1}
		Cell			=	Row.insertCell(-1);
		if((i == JSPopCal_TODAY[0]-1) && (JSPopCal_CalDateObjs[dateObj].getMonth() == JSPopCal_TODAY[1]) && (JSPopCal_CalDateObjs[dateObj].getFullYear() == JSPopCal_TODAY[2]))// 
		{
			Cell.className	=	"JSPopCal_TODAY JSPopCal_Date";
		}
		else
		{
			Cell.className	=	"JSPopCal_Date JSPopCal_DayCol"+DayCount;
		}
		Link			=	document.createElement('a');
		Link.href		=	"javascript:JSPopCal_updateFromCal('"+target+"','"+(i+1)+"/"+(JSPopCal_CalDateObjs[dateObj].getMonth()+1)+"/"+JSPopCal_CalDateObjs[dateObj].getFullYear()+"');";
		Link.appendChild(document.createTextNode(i+1));
		Cell.appendChild(Link);
		Day++;
		if(Day>6)
		{
			Day		=	0;
			Row		=	CalendarTab.insertRow(-1);
			Row.className	=	"JSPopCal_Dates";
		}
	}
	LocPosX		=	JSPopCal_findPosX(target,'');
	LocPosY		=	JSPopCal_findPosY(target,'');
	CalendarBox.style.left		=	parseFloat(LocPosX+parseFloat(XOffSet))+"px";
	CalendarBox.style.top		=	parseFloat(LocPosY+parseFloat(YOffSet))+"px";
	CalendarBox.appendChild(CalendarTab);
	CalendarToday			=	document.createElement('div');
	CalendarToday.className		=	"JSPopCal_TODAYLink";
	CalendarTodayLink		=	document.createElement('a');
	CalendarTodayLink.href		=	"javascript:JSPopCal_SetCalDate(JSPopCal_TODAY[0],JSPopCal_TODAY[1],JSPopCal_TODAY[2],'"+ref+"','"+target+"','"+dateObj+"','"+XOffSet+"','"+YOffSet+"')";
	CalendarTodayLink.appendChild(document.createTextNode('TODAY'));
	CalendarToday.appendChild(CalendarTodayLink);
	CalendarBox.appendChild(CalendarToday);
	if(JSPopCal_IE)	//	IE method for attaching listeners
	{
		document.attachEvent ("onmousedown",function(){JSPopCal_OffClickClose(event,'JSPopCal_Box')});
	}
	else		//	DOM method for attaching listeners
	{
		document.body.setAttribute('onmousedown',"JSPopCal_OffClickClose(event,'JSPopCal_Box')");
	}
	document.body.appendChild(CalendarBox);
	IFrameBack	=	document.createElement('iframe');
	IFrameBack.id	=	'JSPopCal_IFrameBack';
	IFrameBack.style.height	=	document.getElementById('JSPopCal_Box').offsetHeight+'px';
	IFrameBack.style.width	=	document.getElementById('JSPopCal_Box').offsetWidth+'px';
	IFrameBack.style.top	=	document.getElementById('JSPopCal_Box').style.top;
	IFrameBack.style.left	=	document.getElementById('JSPopCal_Box').style.left;
	document.body.appendChild(IFrameBack);
}
function JSPopCal_$(Obj)
{
	if(document.getElementById(Obj)) return document.getElementById(Obj);
}
function JSPopCal_OffClickClose(event, ID)
{
	if(document.getElementById(ID))
	{
		ev	=	event || window.event;
		boxX	=	parseFloat(document.getElementById(ID).offsetLeft);
		boxY	=	parseFloat(document.getElementById(ID).offsetTop);
		boxW	=	parseFloat(document.getElementById(ID).clientWidth);
		boxH	=	parseFloat(document.getElementById(ID).clientHeight);
		
		mouseX	=	ev.clientX;
		mouseY	=	ev.clientY;
		if(JSPopCal_IE)
		{
			mouseX	=	mouseX+document.documentElement.scrollLeft;
			mouseY	=	mouseY+document.documentElement.scrollTop;
		}
		else
		{
			mouseX	=	mouseX+window.pageXOffset;
			mouseY	=	mouseY+window.pageYOffset;
		}
		if((mouseX > boxX && mouseX < boxX+boxW) && (mouseY > boxY && mouseY < boxY+boxH))
		{
		}
		else
		{
			JSPopCal_removeElementByID(ID);
			JSPopCal_removeElementByID('JSPopCal_IFrameBack');
		}
	}
}

function JSPopCal_YearSel(Year,ref,target,dateObj,XOffSet,YOffSet)
{
	JSPopCal_CalDateObjs[dateObj].setYear(Year);
	JSPopCal_removeElementByID('JSPopCal_Box');
	JSPopCal_removeElementByID('JSPopCal_IFrameBack');
	JSPopCal_CalBox(ref,target,dateObj,XOffSet,YOffSet);
}
function JSPopCal_MonthSel(Month,ref,target,dateObj,XOffSet,YOffSet)
{
	JSPopCal_CalDateObjs[dateObj].setMonth(Month);
	JSPopCal_removeElementByID('JSPopCal_Box');
	JSPopCal_removeElementByID('JSPopCal_IFrameBack');
	JSPopCal_CalBox(ref,target,dateObj,XOffSet,YOffSet);
}
function JSPopCal_changeCalMonth(Amt,ref,target,dateObj,XOffSet,YOffSet)
{
	JSPopCal_CalDateObjs[dateObj].setMonth(parseFloat(JSPopCal_CalDateObjs[dateObj].getMonth())+parseFloat(Amt));
	JSPopCal_removeElementByID('JSPopCal_Box');
	JSPopCal_removeElementByID('JSPopCal_IFrameBack');
	JSPopCal_CalBox(ref,target,dateObj,XOffSet,YOffSet);
}
function JSPopCal_getFirstDay(dateObj)
{
	var d	=	new Date(JSPopCal_CalDateObjs[dateObj]);
	d.setDate(1);
	Day	=	d.getDay();
	return Day;
}
function JSPopCal_updateFromCal(target, date)
{
	JSPopCal_removeElementByID('JSPopCal_Box');
	JSPopCal_removeElementByID('JSPopCal_IFrameBack');
	document.getElementById(target).value	=	date;
}
function JSPopCal_removeElementByID(ID)
{
//	document.body.setAttribute('onmousedown','');
	if(typeof ID== 'string') ID=document.getElementById(ID);
	if(ID && ID.parentNode)ID.parentNode.removeChild(ID);
	return
}
function JSPopCal_findPosX(targetStr,targetObj)
{
	var X	=	document.body.offsetLeft;
	if(targetStr!="")
	{
		targetObj	=	document.getElementById(targetStr);
	}
	while((targetObj != document.body)&&(targetObj != null))
	{
//		alert(targetObj.id+" - "+targetObj.className);
		X	=	X+targetObj.offsetLeft;
		targetObj	=	targetObj.offsetParent;
	}
	return X
}

function JSPopCal_findPosY(targetStr,targetObj)
{
	var Y	=	document.body.offsetTop;
	if(targetStr!="")
	{
		targetObj	=	document.getElementById(targetStr);
	}
	while((targetObj != document.body)&&(targetObj != null))
	{
		Y	=	Y+targetObj.offsetTop;
		targetObj	=	targetObj.offsetParent;
	}
	return Y
}
function JSPopCal_ChgDate()
{
	DD = document.getElementById('Date').value;
	MM = document.getElementById('Month').value;
	YY = document.getElementById('Year').value;
	JSPopCal_SetCalDate(DD,MM,YY);
}
function JSPopCal_SetCalDate(DD,MM,YYYY,ref,target,dateObj, XOffSet, YOffSet)
{
	if(YYYY.length == 2)
	{
		thisYear	=	CalDateObjs[dateObj].getFullYear()
		YYYY = String(thisYear).substring(0,2)+String(YYYY);
		alert(YYYY);
	}
	JSPopCal_CalDateObjs[dateObj].setDate(DD);
	JSPopCal_CalDateObjs[dateObj].setMonth(MM);
	JSPopCal_CalDateObjs[dateObj].setFullYear(YYYY);
	JSPopCal_removeElementByID('JSPopCal_Box');
	JSPopCal_removeElementByID('JSPopCal_IFrameBack');
	JSPopCal_CalBox(ref,target,dateObj, XOffSet, YOffSet)
}
/* NO LONGER USED
function JSPopCal_ChkExist(Obj)
{
	if(document.getElementById(Obj))
	{
		alert('true');
		return true
	}
	else
	{
		alert('false');
		return false
	}
}
function JSPopCal_KillCalendars()
{
	if(JSPopCal_ChkExist('JSPopCal_Box'));
	{
		document.getElementById('JSPopCal_Box').parentNode.removeChild('JSPopCal_Box');
		JSPopCal_KillCalendars();
	}
	return
}*/