// JavaScript Document
function toggleLayer(obj) {
	if (obj.style.display=='none') {
		obj.style.display='';
	} else {
		obj.style.display='none';
	}
}
function scrollMe(obj,bit) {
	if(bit == 1) {
		obj.style.overflow = 'auto';
	} else {
		obj.style.overflow = 'hidden';
	}
}

function confirmNonCorporateEvent() {
	var corp = ge('event_corp_ind_bit');
	if(!corp.checked) {
		corp.checked = confirm('Would you like to make this a corporate event?');
	}
}

function disableCheck(obj,bool) {
	if(bool) {
		obj.disabled = true;
		obj.checked = false;
		try {
			obj.selectedIndex = 0;
		} catch(e) {}
		} else {
		obj.disabled = false;
	}
}

// resizes select box to the number of options it contains
// and back down to the predefined number (4) of options
function resize(obj,objLink) {
	if(obj) {	// expanding list
		if(obj.style.height == 'auto') {
			obj.style.height = '80px'
			objLink.className = 'resize collapse';
			objLink.innerHTML = 'collapse';
			objLink.title = 'Collapse list';
		} else {	// collapsing list
			obj.style.height = 'auto';
			objLink.className = 'resize expand';
			objLink.innerHTML = 'expand';
			objLink.title = 'Expand list';
		}
	}
}

// updates the date range, which in turn updates the begin & end dates
// and finally flashes the begin & end dates
function setDates() {
	var datePart1 = ge('dateRange');
	var datePart2 = ge('year');
	var date1 = ge('begin_date');
	var date2 = ge('end_date');
	
	// get the value from each select box
	var dp1 = datePart1.value;
	var dp2 = datePart2.value;
	
	if (dp1 != '' && dp2 != '') {
		
		// user is looking for a fiscal period 
		if (dp1.indexOf('FP ') >= 0) {
			// trim off 'FP ' 
			dp1 = dp1.replace('FP ','');
			// set begin date
			date1.value = arrPeriod[dp2][dp1][0];
			// set end date
			date2.value = arrPeriod[dp2][dp1][1];
		} 
		
		// user is looking for a fiscal week 
		if (dp1.indexOf('FW ') >= 0) {
			// trim off 'FW ' 
			dp1 = dp1.replace('FW ','');
			// set begin date
			date1.value = arrWeek[dp2][dp1][0];
			// set end date
			date2.value = arrWeek[dp2][dp1][1];
		} 
		
		// user is looking for a specific months range
		if (dp1.indexOf('Month') >= 0) {
			// trim off 'Month ' 
			dp1 = dp1.replace('Month ','');
			// create a date object to hold the date
			var myDate1 = new Date();
			myDate1.setFullYear(dp2,dp1-1,1);
			var myDate2 = new Date();
			myDate2.setFullYear(dp2,dp1-1,1);
			myDate2.setMonth(myDate2.getMonth()+1);
			myDate2.setDate(myDate2.getDate()-1);
			
			// set begin date
			date1.value = padStr(myDate1.getMonth()+1,'0',2) 
					+ '/' + padStr(myDate1.getDate(),'0',2)
					+ '/' + myDate1.getFullYear();
			// set end date
			date2.value = padStr(myDate2.getMonth()+1,'0',2) 
					+ '/' + padStr(myDate2.getDate(),'0',2)
					+ '/' + myDate2.getFullYear();
		}
		
		// user is looking for a specific year range
		if (dp1.indexOf('Year') >= 0) {
			// set begin date
			date1.value = '01/01/' + dp2;
			// set end date
			date2.value = '12/31/' + dp2;
		}
		
		hilightDates();
	}
}	
// revised 10/9/2006 -jp -for faster performance with longer lists
// revised 5/3/2007 -jp - looking for a 3rd argument in the value to make the selection (for sublists)
function checkAll(objCB,arrFlds) {
	// locate all checkboxes except for the indicator
	// example: <input type="checkbox" id="selectAll_1" checked onclick="checkAll(this,this.form.contact);" />
	// the inputs should all have the same name i.e. "contact" in this example	
	var chk = objCB.checked;
	var fld = 0;
	// 10/17/2006 - jp - bug found when only one checkbox is in the array
	if (arrFlds.type == 'checkbox') {
		arrFlds.checked = chk;
		return true;
	}
	// if multiple are found, check them all
	if(arrFlds.length) {
		fld = arrFlds.length;
		for(var j=0;j<fld;j++) {
			if(arguments[2]) {	// if there is a third value, check for the value in the cb
				if(arrFlds[j].value.indexOf(arguments[2]) >= 0) {
					arrFlds[j].checked = chk;
				}
			} else {
				arrFlds[j].checked = chk;
			}
		}
		return true;
	} 
}

function hilightDates() {
	var date1 = ge('begin_date');
	var date2 = ge('end_date');
	
	date1.style.background = '#CFC';
	date2.style.background = '#CFC';
	
	var timer = setTimeout('revertDateBg()',1000);
}

function revertDateBg() {
	var date1 = ge('begin_date');
	var date2 = ge('end_date');
	date1.style.background = '';
	date2.style.background = '';
}

var childWindow;
function ConfirmDelete(title) {
	return confirm("By deleting the event "+title+", all occurences of the event and all information about the event will be removed.");
}

// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Temporary variables to hold mouse x-y pos.s
var posX = 0;
var posY = 0;

var V_SHIFT = 0;
var H_SHIFT = 0;

var screen_width = 0;

// Main function to retrieve mouse x-y pos.s
function getMouseXY(e) {
	if (IE) { // grab the x-y pos.s if browser is IE
		posX = event.clientX + Math.max(document.documentElement.scrollLeft,document.body.scrollLeft)
		posY = event.clientY + Math.max(document.documentElement.scrollTop,document.body.scrollTop)
		screen_width = document.body.offsetWidth;
		} else {  // grab the x-y pos.s if browser is NS
		posX = e.pageX;
		posY = e.pageY;
		screen_width = window.innerWidth;
	}  
	// catch possible negative values in NS4
	if (posX < 0){posX = 0}
	if (posY < 0){posY = 0}

	return true;
}

function RebuildForm(myField) {
	var myForm = myField.form;
	ajaxPushPull(myForm.action.replace('act_','frm_')+'?'+formVals(myForm),ge('previewPane'))
}

function getBrowserHeight() {
	var intH = 0;
	var intW = 0;
	
	if(typeof window.innerWidth  == 'number') {
		intH = window.innerHeight;
		intW = window.innerWidth;
	} else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		intH = document.documentElement.clientHeight;
		intW = document.documentElement.clientWidth;
	} else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
		intH = document.body.clientHeight;
		intW = document.body.clientWidth;
	}
	
	return {width: parseInt(intW), height: parseInt(intH)};
}

// center layer on middle of screen
function centerLayer(elem) {
	var bws = getBrowserHeight();
	
	// scroll offset
	var scr = document.documentElement.offsetTop ? document.documentElement.offsetTop : document.documentElement.scrollTop;
	// center element based on its width and border width   
	elem.style.left = parseInt((bws.width - elem.offsetWidth)/2 - elem.style.borderWidth) + 'px';
	// how can we wait until the content is loaded before setting the top style?
	elem.style.top = parseInt(scr + (bws.height - elem.offsetHeight)/2 - elem.style.borderWidth) + 'px';
}

function showPreview(url) {
	var p = ge('previewPane');
	var a = ge('previewArrow');
	
	p.style.display = '';
	a.style.display = '';
	
	//changeOpac(0, 'previewPane');	//buggy in IE6
	
	// centers the pane on the screen
	ajaxPushPull(url,p);
	centerLayer(p);

	if(ge('loading')) {
		centerLayer(ge('loading'));
	}
	
	object_to_hide = p;
}

function hideChildren() {
	if(ge('calList')) {
		if(ge('calList').style.display == 'none') {
			ge('calListShield').style.display = 'none';
			ge('calSelect').disabled = false;
			ge('calSelect').style.visibility = 'visible';
		}
	}
	if(ge('previewPane')) {
		if(ge('previewPane').style.display == 'none') {
			ge('previewArrow').style.display = 'none';
			ge('previewPane').innerHTML = '';
		}
	}
}
function hidePreview() {
	if(ge('previewPane').style.display == 'none') {
		document.location = '../dsp_month_calendar.cfm';
	} else {
		ge('previewPane').style.display = 'none';
		hideChildren();
	}
}

function closeDetails() {
	hidePreview();
}

function CloseEventDetail() {
	if (childWindow != null) {
		try {
			childWindow.close();
		} catch(e) {}
	}
}

if (document.all) {
	window.onfocus=CloseEventDetail;
	window.onunload=CloseEventDetail;
	} else {
	window.captureEvents(Event.MOUSEDOWN | Event.ONUNLOAD);
	window.onmousedown=CloseEventDetail;
	window.onunload=CloseEventDetail;
}

function getNextMonth(intMonth) {
	var d1 = ge('begin_date'); // start date
	var d2 = ge('end_date'); // end date
	var date1 = new Date(d1.value);
	var date2 = new Date(d2.value);
	
	date1.setMonth(parseInt(date1.getMonth()) + parseInt(intMonth));
	date2.setMonth(parseInt(date2.getMonth()) + parseInt(intMonth));
	
	// months are 0 based (0-11)
	d1.value = date1.getMonth()+1 + "/" + date1.getDate() + "/" + date1.getFullYear();		
	d2.value = date2.getMonth()+1 + "/" + date2.getDate() + "/" + date2.getFullYear();
	
	ge('frmSearch').submit();
}

function selectDate(d) {
	var d1 = ge('begin_date'); // start date
	var d2 = ge('end_date'); // end date
	
	var date1 = new Date(d);
	
	// months are 0 based (0-11)
	d1.value = date1.getMonth()+1 + "/" + date1.getDate() + "/" + date1.getFullYear();		
	d2.value = d1.value;
	
	ge('dateRange').options[0].selected = true;
	ge('year').options[0].selected = true;
	
	hilightDates();
	ge('frmSearch').submit();
}

function clearAndSubmit() {
	ge('q').value = '';
	ge('frmSearch').submit();
}

function calList() {
	var divSel = ge('calList');
	var ifr = ge('calListShield');
	var c = ge('calSelect');
	toggleLayer(divSel);
	toggleLayer(ifr);
	//toggleLayer(c);
	/*
		need to set the visibility of the calendar drop down instead of 
		display in order to maintain width
	*/
	if(c.style.visibility == 'hidden') {
		c.disabled = false;
		c.style.visibility = 'visible';
	} else {
		c.disabled = true;
		c.style.visibility = 'hidden';
	}
	c.blur();
}

function selectWeek(w,y) {
	ge('dateRange').value = 'FW ' + w;
	ge('year').value = y;
	setDates();
	ge('frmSearch').submit();
}

function selectMonth(m,y) {
	ge('dateRange').value = 'Month ' + m;
	ge('year').value = y;
	setDates();
	ge('frmSearch').submit();
}

function selectFiscalPeriod(p,y) {
	ge('dateRange').value = 'FP ' + p;
	ge('year').value = y;
	setDates();
	ge('frmSearch').submit();
}

function initDateRange() {
	var dr1 = ge('dateRange');	// date range select box
	var dr2 = ge('year'); // date range year

	dr1.options[0].selected = true;
	dr2.options[0].selected = true;
}

/*
	calculates the duration of time between two times
	assumes the times are in a pair of 3 select boxes hh : mm tt
	Automatically puts the duration in the duration object as inner HTML
*/
function calcDuration() {
	var h1 = ge('START_HOUR');
	var m1 = ge('START_MINUTE');
	var t1 = ge('START_AMPM');
	var h2 = ge('END_HOUR');
	var m2 = ge('END_MINUTE');
	var t2 = ge('END_AMPM');3
	var d = ge('duration');
	
	// create the date/time objects to hold the times
	var time1 = new Date();
	var time2 = new Date();
	
	// convert hours to 24 hr format
	/*
		Since the values in the hour drop down are 01 - 12, parseInt needs to have a radix of 10
		# If the string begins with "0", the radix is 8 (octal). This feature is deprecated
		# If the string begins with any other value, the radix is 10 (decimal)
	*/
	var tempH1 = parseInt(h1.value,10);
	if(tempH1 < 12 && t1.value == 'PM') {
		tempH1 = tempH1 + 12;
	}
	if(tempH1 == 12 && t1.value == 'AM') {
		tempH1 = 0;
	}
	var tempH2 = parseInt(h2.value,10);
	if(tempH2 < 12 && t2.value == 'PM') {
		tempH2 = tempH2 + 12;
	}
	if(tempH2 == 12 && t2.value == 'AM') {
		tempH2 = 0;
	}
	
	// set the times on the dates objects
	time1.setHours(tempH1,parseInt(m1.value));
	time2.setHours(tempH2,parseInt(m2.value));
	
	// calculate time difference in milliseconds
	var timeDiff = time2.getTime() - time1.getTime();
	timeDiff = timeDiff / 1000 / 60; // returns total minutes
	
	// if the value is negative, auto update the end time to equal the start time and recall this function
	if(timeDiff < 0) {
		if(d) {
			d.innerHTML = '<br/>WARNING: End time occurs before start time.';
		}
		return;
	}
	
	var timeDiffH = Math.floor(timeDiff / 60);	// return hours
	var timeDiffM = timeDiff - timeDiffH * 60;	// returns minutes
	
	if(d) {
		d.innerHTML 
			= 'Duration: ' 
				+ String(timeDiffH) + 'h ' 
				+ String(timeDiffM) + 'm';
	}
}

function getNextRange(int) {	// accepts an int, usu. 1 to increment or -1 to decrement
	var dr1 = ge('dateRange');	// date range select box
	var dr2 = ge('year'); // date range year
	
	// if the current date range is at some value other than Calendar Year or -Optional-, 
	// select the next (or previous) one
	if(dr1.selectedIndex == 1
		&& dr2.options.length != dr2.selectedIndex+1) {
		dr2.options[dr2.selectedIndex+int].selected = true;
		setDates();
		ge('frmSearch').submit();
		return true;
	}
	if(dr1.selectedIndex > 1
		&& dr1.options.length != dr1.selectedIndex+1) {
		dr1.options[dr1.selectedIndex+int].selected = true;
		setDates();
		ge('frmSearch').submit();
		return true;
	}
	
	var d1 = ge('begin_date'); // start date
	var d2 = ge('end_date'); // end date
	var date1 = new Date(d1.value);
	var date2 = new Date(d2.value);
	var dateDiff = 0;
	var mth1 = '';
	var mth2 = '';
	var day1 = '';
	var day2 = '';
	
	// add a day to make calculations correct
	var ONE_DAY = 24*60*60*1000;
	
	// find the difference in days between the dates
	dateDiff = int*(date2.getTime() - date1.getTime() + ONE_DAY);
	
	//adds the amount of milliseconds between the date and the date diff
	date1.setTime(date1.getTime()+dateDiff);	
	date2.setTime(date2.getTime()+dateDiff);
	
	// months are 0 based (0-11)
	d1.value = padStr(date1.getMonth()+1,'0',2) + "/"
			 + padStr(date1.getDate(),'0',2) + "/"
			 + date1.getFullYear();	
	d2.value = padStr(date2.getMonth()+1,'0',2) + "/"
			 + padStr(date2.getDate(),'0',2) + "/"
			 + date2.getFullYear();	
	hilightDates();
	
	// set the date range off
	ge('dateRange').value = '';
	ge('year').value = '';
	
	ge('frmSearch').submit();
	
}
function padStr(strToPad,padWith,untilLength) {
	var retStr = '';
	
	if(String(strToPad).length == parseInt(untilLength)) {
		return strToPad;
	}
	while(String(strToPad).length < parseInt(untilLength)) {
		strToPad = String(padWith).concat(strToPad);
	}
	return strToPad;
}

function testURL() {
	var strURL = ge('URL').value;
	if(strURL != '') {
		window.open(strURL,'_blank','');
	}
}

function dispTestURLlink() {
	var u = ge('testURLlink');
	if(ge('URL').value != '') {
		u.style.display = 'inline';
	} else {
		u.style.display = 'none';
	}
}

function clearAllCheck(obj) {
	if(!obj.checked) {
		ge('checkall').checked = false;
	}
}

function showHideTimes(obj) {
	var d = '';
	if(obj.checked) {
		d = 'none';
	}
	ge('start_end_times').style.display = d;
}

function isDateBefore(dateString1, dateString2) {
	return (Date.parse(dateString1) <= Date.parse(dateString2));
}
function stripNewLineCharacters(source) {
	source = source.replace(/\n+/g, " ");
	source = source.replace(/\r+/g, " ");
	source = source.replace(/\s+/g, " ");
	return source;
}
function escapeQuotes(source) {
	source = source.replace(/"/g, "&quot;");
	source = source.replace(/\<XMP\>/ig, "");
	source = source.replace(/\<\/XMP\>/ig, "");
	return source;
}
function lowercaseSpecialCharacters(formElement) {
	formElement.value = formElement.value.replace(/\&QUOT\;/g, "&quot;");
	formElement.value = formElement.value.replace(/\&NBSP\;/g, "&nbsp;");
	formElement.value = formElement.value.replace(/\&LT\;/g, "&lt;");
	formElement.value = formElement.value.replace(/\&GT\;/g, "&gt;");
}
function copyEditorValueToHiddenField(editorElement, formElement) {
	formElement.value = escapeQuotes(stripNewLineCharacters(editorElement.document.body.innerHTML));
}

function changecss(theClass,element,value) {
//documentation for this script at http://www.shawnolson.net/a/503/
	var cssRules;
	//var tBox = ge('allStyles');
	//tBox.value = '';
	if (document.all) {
		cssRules = 'rules';
	}
	else if (ge) {
		cssRules = 'cssRules';
	}
	for (var S = 0; S < document.styleSheets.length; S++){
		for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
			//tBox.value = tBox.value + document.styleSheets[S][cssRules][R].selectorText + '\n';
			if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
				document.styleSheets[S][cssRules][R].style[element] = value;
			}
		}
	}	
}

var object_to_hide;

// hides an object
function checkClick(e) {
	e ? evt = e : evt = event;
	CSE = evt.target ? evt.target : evt.srcElement;
	
	if (object_to_hide) {
		if (!isChild(CSE, object_to_hide)) {
			object_to_hide.style.display = "none";
			// hides other elements associated to floating items
			hideChildren();
		}
	}
}
function isChild(s, d) {
	while (s) {
		if (s == d) {
			return true;
		}
		s = s.parentNode;
	}
	return false;
}
document.all ? document.attachEvent("onclick", checkClick) : document.addEventListener("click", checkClick, false);


var inited = false;
function init() {
	if(!inited) {
		try {
			initFCK();
		} catch(e) {}
		// Set-up to use getMouseXY function onMouseMove
		document.onmousemove = getMouseXY;
		inited = true;
	}
}

//window.onload = init;