/*
#VERSION_HISTORY_START

V1.01 - 05/11/10 AC
- updated showPopup function z-index to above all other divs

V1,0 - 07/07/10 TS
- calendar function library

#VERSION_HISTORY_END
*/

// FUNCTION:	cal_iframe_show ()
// PURPOSE:		re-positions and shows the calendar iframe so that the popup calendar shows over select elements
// PARAMETERS:	none
// RETURNS:		nothing
function cal_iframe_show () {
	// if IE
	if (window.showModalDialog) {
		// show it
		document.getElementById("cal_iframe").style.visibility = 'visible';
		// match the div's position and dimensions
		document.getElementById("cal_iframe").style.top = document.getElementById("cal_div").style.top;
		document.getElementById("cal_iframe").style.left = document.getElementById("cal_div").style.left;
		document.getElementById("cal_iframe").style.width = document.getElementById("cal_div").offsetWidth;
		document.getElementById("cal_iframe").style.height = document.getElementById("cal_div").offsetHeight;
	}
}


// FUNCTION:	cal_iframe_hide ()
// PURPOSE:		hides the calendar iframe
// PARAMETERS:	none
// RETURNS:		nothing
function cal_iframe_hide () {
	// if IE
	if (window.showModalDialog) {
		// hide it
		document.getElementById("cal_iframe").style.visibility = 'hidden';
	}
}


// FUNCTION:	cal_define (string input name)
// PURPOSE:		defines the calendar if not already defined
// PARAMETERS:	[1] input_name (string) -> the name of the input
// RETURNS:		nothing
function cal_define (input_name) {
	// check if the global calendar array is not defined
	if(!window.cals) {
		// if not create it
		window.cals = new Array();
	}
	
	// check if the calendar for the input name is not defined
	if (!cals[input_name]) {
		// if not, define it
		cals[input_name] = new CalendarPopup("cal_div");
		cals[input_name].showNavigationDropdowns();
		cals[input_name].setYearSelectStartOffset(5);
		cals[input_name].offsetX = 24;
		cals[input_name].offsetY = 0;
	}
}


// FUNCTION:	showPopup (object btn_object, string date_input)
// PURPOSE:		wrapper function to allow the new calendar function to be called without code
//				modifcation on all scripts
// PARAMETERS:	[1] btn_object (object) -> the button object reference
//				[2] input_name (string) -> the name of the input
// RETURNS:		nothing
function showPopup (btn_object, date_input) {
	if (!document.getElementById('cal_div')) {
		var calDiv = document.createElement('div');
		calDiv.id = 'cal_div';
		calDiv.style.zIndex = 99999;
		calDiv.style.position = 'absolute';
		calDiv.style.visibility = 'hidden';
		calDiv.style.backgroundColor = 'white';
		calDiv.style.layerBackgroundColor = 'white';
		var calIframe = document.createElement('iframe');
		calIframe.id = 'cal_iframe';
		calIframe.style.zIndex = 998;
		calIframe.style.position = 'absolute';
		calIframe.style.visibility = 'hidden';
		calIframe.style.display = 'block';
		document.body.appendChild(calDiv);
		document.body.appendChild(calIframe);
	}
	// define the calendar
	cal_define (date_input.name);
	
	// check if the id of the object has not set by this function
	if (btn_object.id.indexOf('calbtn_') == -1) {
		// check if the global counter for calendar buttons has not been set
		if (!window.cal_count) {
			window.cal_count = 0;	
		}
		// increment the calender counter
		window.cal_count++;
		// set the id and name of the button object
		btn_object.id = 'calbtn_' + window.cal_count;
		btn_object.name = 'calbtn_' + window.cal_count;
	}
	// call the function to create the calendar
	cals[date_input.name].select(date_input,btn_object.id,'dd-MM-yyyy');
	
	if (window.showModalDialog) {
		win_width = document.body.clientWidth;	
		win_height = document.body.clientHeight;
		div_width = document.getElementById("cal_div").offsetWidth;
		div_height = document.getElementById("cal_div").offsetHeight;
		div_left = document.getElementById("cal_div").offsetLeft;
		div_top = document.getElementById("cal_div").offsetTop;
	} else {
		win_width = window.innerWidth;
		win_height = window.innerHeight;
		div_width = document.getElementById("cal_div").innerWidth;
		div_height = document.getElementById("cal_div").innerHeight;
		div_left = document.getElementById("cal_div").innerLeft;
		div_top = document.getElementById("cal_div").innerTop;
	}
	
	var scrOfX = 0;
	var scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	
	if ((div_left + div_width - scrOfX) > win_width) {
		document.getElementById("cal_div").style.left = win_width - div_width + scrOfX;
		document.getElementById("cal_iframe").style.left = win_width - div_width + scrOfX;
	}
	
	if ((div_top + div_height - scrOfY) > win_height) {
		document.getElementById("cal_div").style.top = win_height - div_height + scrOfY;
		document.getElementById("cal_iframe").style.top = win_height - div_height + scrOfY;
	}

}

document.write(getCalendarStyles());

