/****************
 * Javascript file to be included to allow use of the iframe calendar
 * 
 * Usage: 'var var_name = new iframeCalendar(id);'
 *				parameter 'id' should be unique among all iframeCalendar objects on the calling page
 * 		  '<input type="text" onfocus="var_name.openCalendar(...)"'
 * 		  '<div id="IFC_div"></div>' <--- Need one per page because IE doesn't let you append to document.body....
 * 		Example:	
 * 					<script src="/shared/iframe_cal/iframe_calendar.js"></script>
 *					<div id="IFC_div"></div>
 *					<script type="text/javascript">
 * 						IFC_ALLOW_MULTIPLE_CALENDARS = true;	//optional
 *						cal1 = new iframeCalendar('1');
 *						cal1.setPosition('150px', '150px')
 *						cal2 = new iframeCalendar('2');
 *						cal2.setPosition('350px', '150px');
 *					</script>
 *					....
 * 					<input ... onfocus="cal1.IFC_openCalendar(this)">
 * ------------------------------------------------------------------------------------------------------
 * Interface:--------------------------------------------------------------------------------------------
 * 		var_name.openCalendar(	element, 	//input element date selection will be returned to
 * 								fdate, 		//date to focus on
 * 								pos_top, 	//position of top of frame, e.g. '150px'
 * 								pos_left);	//position of left of frame, e.g. '150px'
 * 			
 * 		var_name.openCalendar(element);		//similar to above, but uses defaults/previously set values for other variables
 * 
 * 		var_name.setInactive();		//the calendar will no longer open, and date selection will no longer
 * 									//affect the element if the calendar is already open 
 * 
 * 		var_name.setPosition(t, l);	//sets the top and left positions of the calendar
 * -------------------------------------------------------------------------------------------------------
 * Global functions/variables----------------------------------------------------------------------------- 
 * 		(you should never have to directly use or modify these, except for IFC_ALLOW_MULTIPLE_CALENDARS!):
 * 
 * IFC_LIST	:	when a calendar object is created, it adds itself to this array.
 * 
 * IFC_ALLOW_MULTIPLE_CALENDARS	: if false, calling openCalendar(...) closes any other open calendar.  Set to
 * 								  true after including this file to disable this behavior.
 * 
 * IFC_CALENDAR_SRC	:	the location of the iframe code
 * 
 * IFC_selectDateFunction(id, date)	:	called by the iframe when a date is selected.  Searches IFC_LIST for a calendar
 * 										with the specified id, and calls it's selectDateAndClose function
 * 
 * IFC_closeCalendarFunction(id)	:	called by the iframe when the 'close' link is clicked.  Searches IFC_LIST for
 * 										a calendar with the specified id and calls it's closeCalendar function
 * 
 * IFC_closeAllCalendars()			:	calls the closeCalendar function for every calendar is IFC_LIST
 */
var IFC_LIST = new Array();		//list of iframeCalendar objects created
var IFC_ALLOW_MULTIPLE_CALENDARS = false;	//set to true after including this file to allow
											//multiple calendars to be displayed at once
var IFC_CALENDAR_SRC = "/shared/iframe_cal/iframe_calendar.cfm";	//where to find the calendar HTML
											
//called by the iframe after a date is selected.  Finds the calendar with the 
//specified id and passes the date to it
function IFC_selectDateFunction(id, date) {
	//alert(id+" "+date);
	var i;
	for(i = 0; i < IFC_LIST.length; i++) {
		if(IFC_LIST[i].IFC_getID() == id) {
			IFC_LIST[i].IFC_selectDateAndClose(date);
			break;
		}
	}
}

//called from iframe when calendar is moved forward/backward.  Finds the calendar and passes
//date to it
function IFC_setFocusDateFunction(id, date) {
	var i;
	for(i = 0; i < IFC_LIST.length; i++) {
		if(IFC_LIST[i].IFC_getID() == id) {
			IFC_LIST[i].IFC_setFocusDate(date);
			break;
		}
	}
}

//called by the iframe when the close link is clicked.  Finds the calendar
//with the specified id and calls its close function
function IFC_closeCalendarFunction(id) {
	var i;
	for(i = 0; i < IFC_LIST.length; i++) {
		if(IFC_LIST[i].IFC_getID() == id) {
			IFC_LIST[i].IFC_closeCalendar();
			break;
		}
	}
}
//closes all calendars, usually called when IFC_ALLOW_MULTIPLE_CALENDARS is
//false and a calendar is opened
function IFC_closeAllCalendars() {
	var i;
	for(i = 0; i < IFC_LIST.length; i++) {
		IFC_LIST[i].IFC_closeCalendar();
	}
}

function iframeCalendar(id) {
	///////////////Constructor
	var active = false;	//if not active, calendar will not open and dates will not be saved into the input element
	var calendar_id = id;	//should be unique among all other iframeCalendar objects on the page
	var input_element;	//element to return date selection to
	var top_pos = "150px";		//top position of the frame
	var left_pos = "150px";		//left position of the frame
	var today = new Date();
	var focus_date = (today.getMonth()+1)+"/"+today.getDate()+"/"+today.getFullYear();
	
	//create the frame and append it to the body of the page
	var frame = document.createElement('iframe');
	frame.style.display = "none";
	frame.style.position = "absolute";
	frame.setAttribute('scrolling', 'no');
	frame.setAttribute('frameBorder', 'no');
	frame.setAttribute('marginWidth', '0px');
	frame.setAttribute('marginHeight', '0px');
	frame.style.width = "157px";
	frame.style.height = "247px";
	frame.style.margin = "0 0 0 0";
	frame.style.padding = "0 0 0 0";
	frame.style.spacing = "0 0 0 0";
	frame.setAttribute('name', id);
	//frame.setAttribute('src', IFC_CALENDAR_SRC+"?focus_date="+focus_date+"&cal_id="+calendar_id);
	frame.setAttribute('src', IFC_CALENDAR_SRC+"?focus_date=1/1/2001&cal_id="+calendar_id);

	//document.getElementById('IFC_div').appendChild(frame);
	
	//add this calendar to the IFC_LIST
	IFC_LIST[IFC_LIST.length] = this;
	//////////////////End Constructor
	
	this.IFC_activateCalendar = function() {
		document.getElementById('IFC_div').appendChild(frame);
		active = true;
	}
	//opens the calendar.  sets the element that date selection will be sent to,
	//the focus date of the calendar, the left positions, and the top position
	this.IFC_openCalendar = function(element, fdate, pos_top, pos_left) {
		if(!active) return;
		
		if(!IFC_ALLOW_MULTIPLE_CALENDARS)
			IFC_closeAllCalendars();
			
		input_element = element;
		top_pos = pos_top;
		left_pos = pos_left;

		frame.contentWindow.changeFocus(focus_date);
		frame.style.top = top_pos;
		frame.style.left = left_pos;
		frame.style.display = "";
	}
	
	//assumes fdate, pos_left, and pos_top were all set, or that default values are okay
	this.IFC_openCalendar = function(element) {
		if(!active) return;
		
		if(!IFC_ALLOW_MULTIPLE_CALENDARS)
			IFC_closeAllCalendars();
			
		input_element = element;
		input_element.select();
		frame.contentWindow.changeFocus(focus_date);
		frame.style.top = top_pos;
		frame.style.left = left_pos;
		frame.style.display = "";
		//alert(element.name);
	}
	
	this.IFC_setPosition = function(t, l) {
		top_pos = t;
		left_pos = l;
		//alert(t+" "+l);
	}
	
	this.IFC_setPositionByElement = function(element) {
		var x = IFC_findPosX(element) + 75;
		var y = IFC_findPosY(element) - 20;
		this.IFC_setPosition(y+'px', x+'px');
		//this.IFC_setPosition('261px', '402px');
		//alert(element+" "+x+" "+y);
	}
		
	this.IFC_closeCalendar = function() {
		if(!active) return;
		
		frame.style.display = "none";
		//frame.src = "";
	}
	
	this.IFC_closeIfLeaving = function() {
		
	}
	
	this.IFC_selectDateAndClose = function(date) {
		if(!active) return;
		focus_date = date;
		input_element.value = date;
		if(input_element.onchange) {
			input_element.onchange();		
		}
		this.IFC_closeCalendar();
	}
	
	this.IFC_setFocusDate = function(date) {
		focus_date = date;		
	}

	this.IFC_setInactive = function() {
		active = false;	
	}
	
	this.IFC_getID = function() {
		return calendar_id;	
	}
	
	var IFC_findPosX = function(obj)
	{
		var curleft = 0;
		if (obj.offsetParent)
		{
			while (obj.offsetParent)
			{
				curleft += obj.offsetLeft
				obj = obj.offsetParent;
			}
		}
		else if (obj.x)
			curleft += obj.x;
		return curleft;
	}

	var IFC_findPosY = function (obj)
	{
		var curtop = 0;
		if (obj.offsetParent)
		{
			while (obj.offsetParent)
			{
				curtop += obj.offsetTop
				obj = obj.offsetParent;
			}
		}
		else if (obj.y)
			curtop += obj.y;
		return curtop;
	}
}