// JavaScript Document

var DEFAULT_YEAR = '2008';

function thisYear() {
	return new Date().getYear() + 1900;
}

function printYear(element) {
	
	if (element.innerHTML) {
		element.innerHTML = thisYear();
	} else if (document.getElementById(element)) {
		document.getElementById(element).innerHTML = thisYear();
	} else {
		document.write(thisYear());
	}
}


function checkDay(checkbox) {
	if (!checkbox || typeof(checkbox.id)=='undefined') return;
	var vis = checkbox.checked?'visible':'hidden';
	document.getElementById(checkbox.id + 'From').style.visibility = vis;
	document.getElementById(checkbox.id + 'To').style.visibility = vis;
}


function getParentRecur(currentElement, targetTag) {
	var p = currentElement.parentNode;
	if (p==null) return;
	if (p.nodeName == targetTag) {
		return p;
	} else {
		return getParentRecur(p, targetTag);
	}
}

function toggleDay(checkBox, dayRow) {
	if (checkBox.checked) {
		checkBox.onclick = function(dayRow) { dayRow.className = 'checkedDay'; }
	} else {
		checkBox.onclick = function(dayRow) { dayRow.className = 'day'; }
	}
}


function initDayRows() {
	var dayRows = document.getElementById('dayTable').getElementsByTagName('tr');
	var rowCount = 0;
	for (var r=0; r<dayRows.length; r++) {
		var dayRow = dayRows[r];
		var check = document.getElementById('pref'+dayRow.id);
		if (check && check.id) {
			check.dayRow = dayRow;
			check.onclick = function() {
				var clName = (this.checked ? 'checkedDay' : 'day');
				this.dayRow.className = clName;
			}
			rowCount++;
		} 
	}
	if (rowCount < dayRows.length) {
		alert('Only initialized '+rowCount+' dayRows');
	}
}


function addBookmark() {
	// only works in IE
	if (window.external) {
		window.external.AddFavorite("http://www.pathstoachievement.com/", "Paths To Achievement");
	}
}


function setLinkDescription(currentLink) {
	var ld = document.getElementById('linkDescription');
	if (document.body.innerHTML) {
		ld.innerHTML = currentLink.getElementsByTagName('span')[2].innerHTML;
		ld.style.visibility = 'visible';
	}
}
function clearLinkDescription() {
	var ld = document.getElementById('linkDescription');
	if (document.body.innerHTML)
		ld.innerHTML = '';
	ld.style.visibility = 'hidden';
}

function initFavLinks() {
	var items = document.getElementById('favoriteLinks').getElementsByTagName('ul')[0].getElementsByTagName('li');
	if (!items) alert("No Links!");
	for (var j=0; j<items.length; j++) {
		var lnk = items[j].getElementsByTagName('a')[0];
		lnk.onmouseover = function() { setLinkDescription(this); }
		lnk.onmouseout = clearLinkDescription;
	}
	if (!document.body.innerHTML)
		alert("innerHTML property unavailable!");
}


var DATA_READY_MAX_ITERATIONS = 100;

function spryDataReady(testId, finishFunc, wait, iters) {
	if (!iters)
		var iters = 0;
	else if (iters > DATA_READY_MAX_ITERATIONS) { //safety valve
		//alert("DataReady Alert: Reached preset iteration ceiling of "+DATA_READY_MAX_ITERATIONS+" without finding TestID element '"+testId+"' in the current document.");
		return; 
	}
	
	if (!wait) 
		var wait = 1000;
	
	if (document.getElementById(testId)) {
		finishFunc();
	} else {
		var recheck = function() { pseudoObserver(testFunc, finishFunc, wait, iters+1); } 
		var timer = setTimeout(recheck, wait);
	}
}

function pseudoObserver(testFunc, finishFunc, wait, iters) {
	if (!iters)
		iters = 0;
	else if (iters > DATA_READY_MAX_ITERATIONS) { //safety valve
		//alert("PseudoObserver Alert: Reached preset iteration ceiling of "+DATA_READY_MAX_ITERATIONS+" without getting affirmative result for test function: '"+testFunc+"'.");
		return; 
	}
	
	if (!wait) 
		wait = 1000;
	
	if (testFunc()) {
		finishFunc();
	} else {
		var recheck = function() { pseudoObserver(testFunc, finishFunc, wait, iters+1); } 
		var timer = setTimeout(recheck, wait);
	}
	
}