// JavaScript Document

servicesName = 'ServicesColumnTable';
window.onresize = refresh_all_columns;

function init () {
	if (!document.getElementById) {
		for (i=0;i<document.styleSheets.length;i++) {
			document.styleSheets(i).disabled = true;
		}
	}

	refresh_all_columns();
}



function showTab(show,hide) {
	hideContent = document.getElementById(hide);
	hiddenContentLink = document.getElementById(hide+'-link');
	showContent = document.getElementById(show);
	visibleContentLink = document.getElementById(show+'-link');
	hideContent.style.display = 'none';
	hiddenContentLink.className = '';
	showContent.style.display = 'block';
	visibleContentLink.className = 'active';
	visibleContentLink.blur();	
}
function hem(id,text) {
	if(id!="" || text!="") {
		id_a = id.split(".");
		str = "";
		for(i=0;i<id_a.length;i++) {
			str += String.fromCharCode(id_a[i]/5);
		}
		str2 = text == "" ? str : text;
		return "<a href=\"mailto:" + str + "\">" + str2 + "</a>";
	} else {
		return "";
	}
}
error_msg = " -- error: required field -- ";
function checkform(action,name) {
	var error=false;
	for(i=0;i<document.forms[name].length-2;i++) {
		if(document.forms[name].elements[i].getAttribute('rel')!="optional") {
			if(document.forms[name].elements[i].value=="" || document.forms[name].elements[i].value==error_msg) {
				document.forms[name].elements[i].style.border='solid #FF0000 1px';
				document.forms[name].elements[i].style.borderColor='#FF0000';
				if(document.forms[name].elements[i].type!="file" && document.forms[name].elements[i].type!="password" && action!='rec_form') {
					document.forms[name].elements[i].value=error_msg;
				} else if(document.forms[name].elements[i].type=="file"){
					alert ("The highlighted file field does not containt a path to your file.");
				} else if(document.forms[name].elements[i].type=="password"){
					alert ("The highlighted field is empty.");
				}
				error=true;
			} else if(document.forms[name].elements[i].id=="email") {
				mail_str = document.forms[name].elements[i].value;
				if(mail_str.indexOf(".", mail_str.indexOf("@")) < mail_str.indexOf("@") + 2
					|| mail_str.indexOf(".") < 1
					|| mail_str.indexOf(".") > mail_str.length-2
					|| mail_str.indexOf("@") < 1
					|| mail_str.indexOf("@") > mail_str.length-2)
					{
					error = true;
					document.forms[name].elements[i].style.borderColor='#FF0000';
					document.forms[name].elements[i].value="-- error: invalid email address --";
				}
				
			} else {
				document.forms[name].elements[i].style.borderColor='#C9D8D3';
				if(action=='rec_form') {
					document.forms[name].elements[i].style.border='solid #000000 0px';
				}
			}
		}
	}
	
	if(error) {
		alert("Please ensure that all highlighted fields are completed.");
		return false;
	}

}

function clear_error(field) {
	if(field.value==error_msg || field.value=='-- error: invalid email address --') {
		field.value="";
	}
}
function update_breadcrumbs(xml) {
   document.getElementById('breadcrumbs').innerHTML = xml;

}
function bio_change() {
	if(document.getElementById('bio_fr').style.display=='none' || document.getElementById('bio_fr').style.display=='') {
		document.getElementById('bio_en').style.display='none';
		document.getElementById('bio_fr').style.display='block';
		document.getElementById('bio_fe').innerHTML='<a onclick="bio_change()" href="#">English</a> | French'
	} else {
   		document.getElementById('bio_en').style.display='block';
		document.getElementById('bio_fr').style.display='none';
		document.getElementById('bio_fe').innerHTML='English | <a onclick="bio_change()" href="#">French</a>'
	}
}


function refresh_all_columns() {

	servicesTable = document.getElementById(servicesName);
	if (servicesTable != null) {
		servicesTDs = servicesTable.getElementsByTagName('td');
		for (s in servicesTDs) {
			var servicesTD = servicesTDs[s];
			if ( servicesTD.className == 'services') {

				listObject = servicesTD.firstChild;
				while ( listObject != null ) {
					if ( listObject.nodeType == 1 && listObject.tagName == 'UL' ) {
						refresh_column(servicesTable, listObject);
					}
					listObject = listObject.nextSibling;
				}
			}
		}
		servicesTable.style.display = 'none';
		servicesTable.style.display = '';
	}
}

function refresh_column(servicesTable, servicesUL) {

	columns = parseInt((servicesTable.offsetWidth - 240) / 240);
	columns = Math.max(columns, 1);
	totalHeight = 0;

	itemObject = servicesUL.firstChild;
	while ( itemObject != null ) {
		if (itemObject.nodeType == 1) {
			totalHeight = totalHeight + itemObject.offsetHeight;
		}
		itemObject = itemObject.nextSibling;
	}

	columnHeight = totalHeight / columns;
	currentHeight = 0;
	currentColumn = 0;
	itemObject = servicesUL.firstChild;
	maxHeight = 0;
	count = 0;
	
	while ( itemObject != null ) {

		if (itemObject.nodeType == 1) {
			itemObject.style.marginTop = 0;
			if (count == 0 ) {
				currentHeight = currentHeight + itemObject.offsetHeight;
				itemObject.style.marginLeft = (currentColumn * 240) + 'px';
				count = count + 1;
			} else if ( ((currentHeight + (itemObject.offsetHeight * 2 / 5)) <= columnHeight)) {
				currentHeight = currentHeight + itemObject.offsetHeight;
				itemObject.style.marginLeft = (currentColumn * 240) + 'px';
				count = count + 1;
			} else if ((currentColumn + 1 >= columns)) {
				currentHeight = currentHeight + itemObject.offsetHeight;
				itemObject.style.marginLeft = (currentColumn * 240) + 'px';
				count = count + 1;
			} else { 
				currentColumn = currentColumn + 1;				
				itemObject.style.marginLeft = (currentColumn * 240) + 'px';
				itemObject.style.marginTop = (0 - currentHeight) + 'px';
				maxHeight = Math.max(maxHeight, currentHeight);
				currentHeight = itemObject.offsetHeight;
				count = 1;
			}
		}

		itemObject = itemObject.nextSibling;
	}
	maxHeight = Math.max(maxHeight, currentHeight);
	servicesUL.style.height = maxHeight + 'px';
}
