// JavaScript Document

// ================= INITIALIZATION FUNCTION ================== // 
function init() {
	addEvent(window, 'load', getAnchors, false);	
}

// add event function
function addEvent(elm, evType, fn, useCapture) {
	if(elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	} else {
		elm['on' + evType] = fn;
	}
}







// =========== DYNAMIC POPUP SCRIPT ================ //
function getAnchors() {
	var anchors = document.getElementsByTagName("a");
	
	for(i=0; i<anchors.length; i++) {
		// set anchor vars
		var a = anchors[i];
		var aHref = a.getAttribute('href');
		// create an array of the options
		if(a.getAttribute('rel')) {
			var aRel = a.getAttribute('rel');
			// split the rel into attributes
			var aRelArr = aRel.split('|');
			// make sure the link is a popup
			switch(aRelArr[0]) {
				case 'popup':
					// if the noicon flag is not set, style the link
					if(aRelArr[1] != 'noicon') {
						a.style.backgroundImage = "url(images/icons/popup.png)";
						a.style.backgroundPosition = "top right";
						a.style.backgroundRepeat = "no-repeat";
						a.style.paddingRight = "15px";
						a.style.marginRight = "3px";
					}
					// append to the title attribute
					a.title += ' [Opens in new window]';
					
					// add an onclick handler to trigger the popup
					a.setAttribute('href',"javascript:popUp('" + aHref + "','newWin','')");
				break;			
				
				// if the link needs to be made into a mailto link
				case 'emailLink':
					// get the munged email address from the link's child text node
					mungStr = a.firstChild.nodeValue;
					mungStr = mungStr.replace(' [at] ','@');
					mungStr = mungStr.replace(' [dot] ','.');
					// set the mailto href
					a.setAttribute('href',"mailto:" + mungStr);
					a.firstChild.nodeValue = mungStr;
				break;
			}

		}	
	}
}

function popUp(url) {
	window.open(url,'newWin','');
	
	if(window.event) {
		window.event.returnValue = false;
		window.event.cancelBubble = true;
	}
}







// ================= SWITCH TABS ON THE CALENDAR ====================== //
function tabSwitch(hideStr,showStr,tabOff,tabOn) {
	// hide all calendars and turn all tabs off
	if(hideStr != '') {
		var hideIDs = hideStr.split(',');	
		for(i=0; i<hideIDs.length; i++) {
			var hideObj = hideIDs[i];
			document.getElementById(hideObj).style.display = 'none';
		}
	}
	if(tabOff != '') {
		var tabIDs = tabOff.split(',');		
		for(i=0; i<tabIDs.length; i++) {
			var tabObj = tabIDs[i];
			document.getElementById(tabObj).className = 'tabOff';
		}
	}
	
	// then show the selected calendar and turn its tab on
	document.getElementById(showStr).style.display = 'block';
	document.getElementById(tabOn).className = 'tabOn';
}







// ================= ENABLE / DISABLE FORM FIELDS =================== //
function toggleDisable(sourceID,targetIDs,targetIDs2,toggleVisibleStr1,toggleVisibleStr2) {
	// source id is a checkbox
	// targetIDs is an array of items to be enabled when box is checked
	// targetIDs2 is an array of items to be disabled when box is checked
	sourceObj = document.getElementById(sourceID);
	targetArr = targetIDs.split(',');
	targetArr2 = targetIDs2.split(',');


	// disable when source is not checked
	for(i=0; i<targetArr.length; i++) {
		targetObj = document.getElementById(targetArr[i]);
		if(sourceObj.checked == false) {
			targetObj.disabled = true;
			targetObj.className = 'disabled';
			hideStr = toggleVisibleStr1;
			showStr = toggleVisibleStr2;
		} else {
			targetObj.disabled = false;
			targetObj.className = '';
			showStr = toggleVisibleStr1;
			hideStr = toggleVisibleStr2;
		}
	}
	
	// disable when source is checked
	for(i=0; i<targetArr2.length; i++) {
		targetObj = document.getElementById(targetArr2[i]);
		if(sourceObj.checked == false) {
			targetObj.disabled = false;
			targetObj.className = '';
		} else {
			targetObj.disabled = true;
			targetObj.className = 'disabled';
		}
	}
	
	// finally show / hide the appropriate elements
	showHide(showStr,hideStr);
	
}







// =================== HIGHLIGHT FUNCTION ======================= //
function highlight(sourceID,targetID) {
	var sourceObj = document.getElementById(sourceID);
	var targetObj = document.getElementById(targetID);	
	
	// if the source is selected .. add the class
	if(sourceObj.checked == true) {
		targetObj.style.backgroundColor = '#f6f6e2';
	} else {
		targetObj.style.backgroundColor = '';	
	}
}







// =================== SHOW / HIDE FUNCTION ======================= //
function showHide(showStr,hideStr) {
	// hide first
	hideArr = hideStr.split(',');
	for(i=0; i<hideArr.length; i++) {
		if(hideArr[i] != '') {
			hideObj = document.getElementById(hideArr[i]);
			hideObj.style.display = 'none';
		}
	}
	
	
	// then show 
	showArr = showStr.split(',');
	for(i=0; i<showArr.length; i++) {	
		if(showArr[i] != '') {
			showObj = document.getElementById(showArr[i]);	
			if(showObj.style.display == 'block') {
				showObj.style.display = 'none';
			} else {
				showObj.style.display = 'block';
			}
		}
	}
	
}







// =================== DYNAMIC DAYS IN MONTH FUNCTION ======================= //
// called onchange() of the static <select> box
function autoDays(y,m,d) {
	mIndex = document.getElementById(m).selectedIndex;
	yIndex = document.getElementById(y).selectedIndex;
	dIndex = document.getElementById(d).selectedIndex;
	month = document.getElementsByTagName("option")[mIndex].value;
	year = document.getElementsByTagName("option")[yIndex].value;
		
	daysInMonth = 32 - new Date(year, month-1, 32).getDate();
	dBox = document.getElementById(d);
	
	while(dBox.length > 0) {
		dBox.remove(0);
	}
	
	for(i = 1; i<=daysInMonth; i++) {
		newOpt = document.createElement("option");
		newOpt.setAttribute('value',i);
		newOptText = document.createTextNode(i);
		newOpt.appendChild(newOptText);
		dBox.appendChild(newOpt);
	}
	
	// reset the selected index
	dBox.getElementsByTagName("option")[dIndex].selected = true;
	
}







window.onload = init();
