//	---------------------------------------------------------------------------
//	FUNCTION:	buildToolTip(), attachToolTip(), showToolTip(mouseX,mouseY,source)
//	AUTHOR:		Ryan J. Salva, http://www.capitolmedia.com
//	REVISED:	July 24, 2004
//
//	Displays tooltip over <DFN tooltip="text goes here">. Used primarily for forms.
//
//	TESTED ON:
//	Windows Firefox 1, IE 6, Netscape 6.2
//
//	REQUIRED ASSETS: 
//	stylesheet with DFN.ToolTip and DIV.ToolTip defined
//
//	IMPLEMENTATION:
//	Simply place both the js and css files in the header.
//	No other action required.

function buildToolTip() {
	info('buildToolTip()');

	// Start by creating the container <div class="ToolTip"></div>	
	var myToolTip = document.createElement('DIV');
	myToolTip.className = 'ToolTip';

	// previously, myToolTip existed in a kind of limbo within the DOM.
	// Now we add myToolTip as a child of the body element and assign
	document.body.appendChild(myToolTip);
	this.myToolTip = myToolTip;	
	myToolTip.id='ToolTip';
	info('buildToolTip(): created <DIV class=ToolTip id=ToolTip></DIV>');
	
	attachToolTip()
}

function attachToolTip() {
	info('attachToolTip()');

	dfn = document.getElementsByTagName("DFN");
	info('attachToolTip(): found '+dfn.length+' <DFN> tag(s)');

	for (d=0;d<dfn.length;d++) {
		myDFN = dfn[d];
		if (myDFN.getAttribute("tooltip") != null && myDFN.getAttribute("tooltip") != "") {
			myDFN.className = "ToolTip";
			if (document.all) { 
				myDFN.onmousemove = function ieToolTip() {
					showToolTip(event.clientX,event.clientY,event.srcElement);
					info('attachToolTip(): attached showToolTip(x,y,source) onmousemove to <DFN id='+event.srcElement.id+'>'+event.srcElement.innerHTML+'</DFN>');
					return false;
				}
			}
			else {
				myDFN.onmousemove = function ieToolTip(event) {
					showToolTip(event.clientX,event.clientY,event.target);
					info('attachToolTip(): attached showToolTip(x,y,source) onmousemove to <DFN id='+event.target.id+'>'+event.target.innerHTML+'</DFN>');
					return false;
				}
			}
			myDFN.onmouseout = function hideToolTip()
			{
				document.getElementById('ToolTip').style.display='none';
				document.getElementById('ToolTip').style.visibility='hidden';
				info('attachToolTip() > hideToolTip(): set display=none for tooltip');
			}
		}
	}
}


function showToolTip(mouseX,mouseY,source) {
	info('showToolTip('+mouseX+','+mouseY+','+source+')');

	var myToolTip = document.getElementById('ToolTip');
	myToolTip.innerHTML = '<b>' + source.innerHTML + '</b> ' + source.getAttribute("tooltip");

	if (document.all) { // IE scroll offset
		XOffset = document.documentElement.scrollLeft;
		YOffset = document.documentElement.scrollTop;
	}
	else { // Mozilla scroll offset
		XOffset = window.pageXOffset;
		YOffset = window.pageYOffset;
	}

	myToolTip.style.position='absolute';
	myToolTip.style.top=mouseY + YOffset +  'px';
	myToolTip.style.left=mouseX + XOffset + 10 + 'px';
	
	// reveal the myToolTip
	myToolTip.style.display="block";
	myToolTip.style.visibility="visible";
}

// initialize the tooltip by calling buildToolTip() on page load
// the addLoadEvent() function can be found it utility.js
addLoadEvent(function(){buildToolTip();});


//	---------------------------------------------------------------------------
//	FUNCTION:	passwordQuality()
//	AUTHOR:		Ryan J. Salva, http://www.capitolmedia.com
//	REVISED:	September 20045
//
//	Performs a quality test on new password creation and displays
//	bar showing 
//
//	TESTED ON:
//	Windows Firefox 1, IE 6, Netscape 6.2
//
//	REQUIRED ASSETS: 
//	stylesheet with DIV.Quality and DIV.Quality DIV defined
//
//	IMPLEMENTATION:
//	Simply place both the js and css files in the header.
//	No other action required.



function passwordQuality() {
	// we know that password is the first input element in <fieldset id="Password">
	if (document.getElementById("Password")) {
		var IA = document.getElementById("Password").getElementsByTagName("INPUT");
		if(IA.length>1) { // only display security bar if we're creating a new password

			var D = document.createElement('DIV');
			D.className = "Info";
			D.id = "Quality";
			var H = document.createElement('H2');
			H.innerHTML = "Password Security:";
			D.appendChild(H);
			var P = document.createElement('P');
			P.innerHTML = "Your personal information is only as safe as your password. We recommend using lowercase (a), uppercase (A), numbers (123) and special characters (%$@). The graph below estimates your password quality.";
			D.appendChild(P);
			var Q = document.createElement('DIV');
			D.appendChild(Q);
			var V = document.createElement('DIV');
			V.id = "V";
			Q.appendChild(V);
			document.getElementById('Password').appendChild(D);
			this.D = D;

			IA[0].onkeyup = function (){
				var p = this.value;
				var q = 0; // quality, 1-100
				var a = new RegExp('[a-z]'); // lowercase
				var A = new RegExp('[A-Z]'); // uppercase 
				var d = new RegExp('[0-9]'); // digit
				var s = new RegExp('[^a-zA-Z0-9_]'); // special character 
				
				if (p.match(a))q+=9;
				if (p.match(A))q+=9;
				if (p.match(d))q+=9;
				if (p.match(s))q+=9;
				q+=(p.length*4);
				if (q>100) q=100;
				document.getElementById("V").style.width = q + "%";
			}
		}
	}
}

// initialize the password checker by calling passwordQuality() on page load
// the addLoadEvent() function can be found it utility.js
addLoadEvent(function(){passwordQuality();});



// Title:  Cross-Browser JavaScript Calendar Control
// Description:  Defines a screen control that displays a calendar control; the
//					calendar defaults to the month of the current date; the
//					control allows a visitor to navigate to the next and
//					previous months; the control also allows a programmer to 
//					retrieve the selected day, so that the control can be used
//					as a day picker	for a larger application
//					This program requires a browser that supports the
//					innerHTML property

// Array that contains the names of the days of the week
var days = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");

// Array that contains the names of the months of the year
var months = new Array(	"January","February","March","April","May","June","July","August","September","October","November","December");

// Holds a date representing the first day of the currently displayed month;
//	this value is used in the previous and next month calculations
firstDayCurrentMonth = new Date();

var calendarTarget = null;

// Title:  Cross-Browser JavaScript Calendar Control
// Description:  Defines a screen control that displays a calendar control; the
//					calendar defaults to the month of the current date; the
//					control allows a visitor to navigate to the next and
//					previous months; the control also allows a programmer to 
//					retrieve the selected day, so that the control can be used
//					as a day picker	for a larger application
//					This program requires a browser that supports the
//					innerHTML property

// Array that contains the names of the days of the week
var days = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");

// Array that contains the names of the months of the year
var months = new Array(	"January","February","March","April","May","June","July","August","September","October","November","December");

// Holds a date representing the first day of the currently displayed month;
//	this value is used in the previous and next month calculations
firstDayCurrentMonth = new Date();

var calendarTarget = null;

function buildCalendar() {
// Draws a blank calendar onto the document; the contents of the calendar are
//	populated later using the innerHTML attribute of a div; be sure to 
//	initialize the divs with a readable, non-space character; some browsers 
//	have a bug that prevents JavaScript from accessing empty divs using the 
//	getElementById() function

	info('buildCalendar()');

	var nameCounter;
	var weeksLoop;
	var daysLoop;

	// create our basic table elements
	myTable = document.createElement("TABLE");
	myTable.id = "PopCalendar";
	myHead = document.createElement("THEAD");
	myBody = document.createElement("TBODY");
	myFoot = document.createElement("TFOOT");

	// draw a row that will contain the title (month and year) of the calendar
	monthTR = document.createElement("TR");
	monthTH = document.createElement("TH");
	monthTH.setAttribute("colspan","7");
	monthTH.id = "MonthYear";
	monthText = document.createTextNode("m yyyy");

	monthTH.appendChild(monthText);
	monthTR.appendChild(monthTH);
	myHead.appendChild(monthTR);

	// draw a row that will contain the weekday names
	weekTR = document.createElement("TR")
	nameCounter = 0;
	for (daysLoop = 0; daysLoop <= 6; daysLoop++) {

		dayTH = document.createElement("TH");

		// The weekend labels are colored differently from weekday labels
		if ((daysLoop == 0) || (daysLoop == 6)) {
			dayTH.className = "Weekend"; }
		else {
			dayTH.className = "Weekday"; }

		textTH = document.createTextNode(days[daysLoop]);
		dayTH.appendChild(textTH);
		weekTR.appendChild(dayTH);

		nameCounter++;
	}
	myHead.appendChild(weekTR);


	// Loop to draw a row for each week in a month
	nameCounter = 0;
	for (weeksLoop = 0; weeksLoop <= 5; weeksLoop++) { 
		dayTR = document.createElement("TR"); // current row
		
		// Draw a cell for each day in a week
		for (daysLoop = 0; daysLoop <= 6; daysLoop++) {

			dayTD = document.createElement("TD");
			dayTD.id = "d" + nameCounter.toString();

			// The weekend days are colored differently from weekdays
			if ((daysLoop == 0) || (daysLoop == 6)) {
				dayTD.className = "Weekend"; }
			else {
				dayTD.className = "Weekday"; }
				
			textTD = document.createTextNode("xx");
			dayTD.appendChild(textTD);
			dayTR.appendChild(dayTD);
			
			nameCounter++;
		}
		myBody.appendChild(dayTR);
	}

	// draw the footer with next and previous buttons
	footTR = document.createElement("TR");

	prevTD = document.createElement("TD");
	prevTD.setAttribute("colspan","4");	
	prevTD.className = "Previous";
	prevA = document.createElement("A");
	prevA.setAttribute("href","javascript:goPrevious()");
	prevText = document.createTextNode("previous");
	prevA.appendChild(prevText);
	prevTD.appendChild(prevA);

	nextTD = document.createElement("TD");
	nextTD.setAttribute("colspan","3");
	nextTD.className = "Next";
	nextA = document.createElement("A");
	nextA.setAttribute("href","javascript:goNext()");
	nextText = document.createTextNode("next");
	nextA.appendChild(nextText);
	nextTD.appendChild(nextA);

	footTR.appendChild(prevTD);
	footTR.appendChild(nextTD);
	myFoot.appendChild(footTR);


	// add our table parts to the body
	myTable.appendChild(myHead);
	myTable.appendChild(myBody);
	myTable.appendChild(myFoot);
	document.body.appendChild(myTable);
}

function goNext() {
// Goes to the next month

	info('goNext()');

	var selectedDate = new Date;
	var selectedMonth;
	var selectedDay;
	var selectedYear;

	// Get the current month information from the saved date
	selectedDay = firstDayCurrentMonth.getDate();
	selectedYear = firstDayCurrentMonth.getFullYear();
	selectedMonth = firstDayCurrentMonth.getMonth();

	// Add one to the current month, and be sure to check to be sure 
	//	that the function does not overflow the array boundaries
	selectedMonth = selectedMonth + 1;
	if (selectedMonth > 11) {
		selectedMonth = 0;
		selectedYear = selectedYear + 1;
	}

	// Construct a new date object, and update the calendar for the new month
	selectedDate.setTime(Date.parse(
			months[selectedMonth] + " " + 
			selectedDay.toString() + ", " + 
			selectedYear.toString()
			));
	updateCalendar(selectedDate);

}

function goPrevious() {
// Goes to the previous month

	info('goPrevious()');

	var selectedDate = new Date;
	var selectedMonth;
	var selectedDay;
	var selectedYear;

	// Get the current month information from the saved date
	selectedDay = firstDayCurrentMonth.getDate();
	selectedYear = firstDayCurrentMonth.getFullYear();
	selectedMonth = firstDayCurrentMonth.getMonth();

	// Subtract one from the current month, and be sure to check to be sure 
	//	that the function does not overflow the array boundaries
	selectedMonth = selectedMonth - 1;
	if (selectedMonth < 0) {
		selectedMonth = 11;
		selectedYear = selectedYear - 1;
	}

	// Construct a new date object, and update the calendar for the new month
	selectedDate.setTime(Date.parse(
			months[selectedMonth] + " " + 
			selectedDay.toString() + ", " + 
			selectedYear.toString()
			));
	updateCalendar(selectedDate);

}

function selectDay(divNameNumber, timeValue) {
	info('selectDay('+divNameNumber+','+timeValue+')');

// Highlights the appearance of the currently selected day, and provides a
//	hook by which a programmer can retrieve the selected day value

	var nameCounter;
	var divPointer;
	var selectedDate = new Date();

	// Clear the previous selected cells (loop for 6 rows of 7 days ... 
	//	or 42 cells)
	for (nameCounter = 0; nameCounter < 42; nameCounter++) {
		divPointer = document.getElementById("d" + nameCounter.toString());
		divPointer.className = "";
	}

	// Set the color of the div for the selected day
	divPointer = document.getElementById("d" + divNameNumber.toString());
	divPointer.className = "SelectedDay";

	// Load the selected date value into a real date time object
	selectedDate.setTime(timeValue);
//	targetCalendar.value = selectedDate.getMonth + "/" + selectedDate.getDate + "/" + selectedDate.getYear;
	
	document.getElementById('PopCalendar').style.left='-1000px';

}

function updateCalendar(selectedDate) {
	info('updateCalendar('+selectedDate+')');

// Receives a date and updates the calendar information to display the 
//	corresponding month that includes the specified date

	var currentDay = new Date();
	var nameCounter;
	var divPointer;
	var dayCounter;
	var now = new Date();

	// Set the title of the calendar to the month and year
	document.getElementById("MonthYear").innerHTML = 
		months[selectedDate.getMonth()] +
		" " + selectedDate.getFullYear().toString();

	// Clear the old day values; clear any selected days; clear any bolded
	//	days (loop for 6 rows of 7 days ... or 42 cells)
	for (nameCounter = 0; nameCounter < 42; nameCounter++) {
		divPointer = document.getElementById("d" + nameCounter.toString());
		divPointer.className = "";
		divPointer.innerHTML = "&nbsp;";
	}

	// Find the first day of the month, and then the day of the week for
	//	that day
	currentDay.setTime(Date.parse(
		months[selectedDate.getMonth()] + 
		" 1, " + 
		selectedDate.getFullYear().toString()
		));
	nameCounter = currentDay.getDay();
	dayCounter = 1;

	// Save the current date as a reference that can be used by the previous
	//	and next month links
	firstDayCurrentMonth.setTime(currentDay.getTime());

	// Keep incrementing the day value until the month changes; for each day,
	//	write a link that will execute a function to handle the day selection
	//	event; when the month value changes, the calendar is complete
	while (selectedDate.getMonth() == currentDay.getMonth()) {
		divPointer = document.getElementById("d" + nameCounter.toString());
		divPointer.innerHTML = "<a href=\"javascript:selectDay(" + nameCounter.toString() + 
			", " + currentDay.getTime().toString() + 
			");\">" + dayCounter.toString() + "</a>";

		// Check for today's date, and make it bold
		if ((now.getDate() == currentDay.getDate()) &&
			 (now.getMonth() == currentDay.getMonth()) && 
			 (now.getFullYear() == currentDay.getFullYear())) {
				divPointer.className = "Today";
		}

		// Increment the day and div name counters; build a new date object
		nameCounter++;
		dayCounter++;
		currentDay.setTime(Date.parse(
			months[currentDay.getMonth()] + " " + 
			dayCounter.toString() + ", " + 
			currentDay.getFullYear().toString()
			));
	}

}

function showCalendar(source,target,format)
{
	info('showCalendar('+source+','+target+','+format+')');
	var PopCalendar = document.getElementById('PopCalendar')
	var myDate = new Date(document.getElementById(target).value);
	
	if (myDate == 'Invalid Date') myDate = Date(); // suspect code

	var YOffset = null;
	var XOffset = null;
	
	if (document.all) { // IE scroll offset
		XOffset = document.documentElement.scrollLeft;
		YOffset = document.documentElement.scrollTop;
	}
	else { // Mozilla scroll offset
		XOffset = window.pageXOffset;
		YOffset = window.pageYOffset;
	}
	
	// set the target, used by selectDay()
	targetCalendar = document.getElementById(target);

	// Update the calendar, seeding with the current date
	updateCalendar(myDate);

	PopCalendar.style.left=source.clientX + XOffset + 'px';
	PopCalendar.style.top=source.clientY + YOffset + 'px';
}


// initialize the calendar by calling buildCalendar() on page load
// the addLoadEvent() function can be found it utility.js
addLoadEvent(function(){buildCalendar();});

