var closeMenuTimeout = null;
var activeMenu = null;
var menuList = Array();
window.addEvent('domready', function() {
	var topMenuItems = $('pageNavigation').getElement('ul').getChildren();
	topMenuItems.each(function(elem) {
		elem.setStyles({'z-index':'1000','white-space':'nowrap'});
		if (elem.getElement('ul')) {
			var subMenu = elem.getElement('ul');
			subMenu.setStyles({'display':'none'});
			menuList[menuList.length] = subMenu;
			elem.getElement('a').addEvent('mouseenter',function(el){
				for (var i=0; menuList[i]; i++) {
					if (menuList[i] != el) {
						menuList[i].getParent().getElement('a').setStyles({'background-color':'transparent'});
						menuList[i].setStyles({'display':'none'});
					}
				}
				this.setStyles({'background-color':'#665','opacity':'.7'});					
				if (activeMenu && activeMenu.closeMenuTimeout)
					clearTimeout(activeMenu.closeMenuTimeout);
				subMenu.setStyles({'opacity':'0','display':'block','top':(elem.getSize().y+elem.getPosition().y)+'px','left':elem.getPosition().x+'px'});
				subMenu.tween('opacity',0,.7);
				//subMenu.closeMenuTimeout = setTimeout(closeMenu,2000);
				activeMenu = subMenu;
			});
			elem.getElement('a').addEvent('mouseleave',function(el){
				if (activeMenu && activeMenu.closeMenuTimeout)
					clearTimeout(activeMenu.closeMenuTimeout);
				if (activeMenu)
					activeMenu.closeMenuTimeout = setTimeout(closeMenu,1500);
			});
			subMenu.addEvent('mouseenter',function(){
				if (activeMenu && activeMenu.closeMenuTimeout)
					clearTimeout(activeMenu.closeMenuTimeout);
			});
			subMenu.addEvent('mouseleave',function(){
				activeMenu.closeMenuTimeout = setTimeout(closeMenu,250);
			});
		} else {
			elem.getElement('a').addEvent('mouseenter',function(){
				closeMenu();
			});
		}
	});

});
function closeMenu() {
	if (activeMenu) {
		activeMenu.setStyles({'display':'none'});
		activeMenu.getParent().getElement('a').setStyles({'background-color':'transparent','opacity':'1'});					
		//activeMenu.fade('out');
	}
	activeMenu = null;
}

window.addEvent('domready', function() {
	$$('.tabGroupContainer').each(function(tg) {
		tg.getElement('div.tabContentGroup').setStyles({'clear':'both'});
		var tabs = tg.getElement('ul').getChildren();
		tg.getElement('ul').getElement('a').setStyles({'background-color':'#333','border-bottom': '1px solid #333'});	
		var tabContentSections = tg.getElement('div.tabContentGroup').getChildren('div');
		tabs.each(function(t) {
			t.getElement('a').addEvent('click',function(e) {
		   		var id = e.target.href.replace(/^.*#/,'');
				tabContentSections.each(function(tc) {
					if ($('tabContent_'+id) != tc)
						tc.setStyles({'display':'none'});
					else
						tc.setStyles({'display':'block'});	
				});
		   		e.target.getParent('ul').getElements('li').each(function(el) {
		   			if (el.getElement('a') == e.target)
						el.getElement('a').setStyles({'background-color':'#333','border-bottom': '1px solid #333'});	
		   			else
						el.getElement('a').setStyles({'background-color':'transparent','border-bottom': '1px solid #aa9'});	
		   		});
				return false;
			});	
		});
		tabContentSections.each(function(tc) {
			if (tabContentSections[0] != tc)
				tc.setStyles({'display':'none'});	
		});
	});
});



var DateFormatString = "yyyy-mm-dd";

if (location.href.indexOf('lang=fr') >-1) { currentLang = 'fra' }
else { currentLang = 'eng' }

var dateField = null;
var CalFrameObj = null;
var CalFrameInit = false;
function ShowCalendar(thisObj) {
	dateField = null;
	dateField = $(thisObj).getPrevious();
	if (!dateField || dateField.tagName != "INPUT") {
		var parentElem = $(thisObj).getParent();
		dateField=parentElem.getElementsByTagName('INPUT')[0];
	}
	if (!CalFrameObj) {
		CalFrameObj = new Element('iframe', {
			'styles': {'position': 'absolute', 'display':'none',
				'z-index': '100', 'margin':'0px',
				'bottom':'0px', 'border': 'solid 1px black', 'background': 'white',
				'width':'175px', 'height': '205px'
				},
			'id': 'CalFrame', 'name': 'CalFrame', 'src': '/php_core/calendar.php?'+Math.random(), 
			'frameBorder': '0',	'scrolling': 'no', 'marginHeight': '0',
			'allowTransparency': false, 'marginWidth': '0'
			}).injectInside($(document.body));
	}
	var CalFrameIndex = null;
	if (document.frames) {
		for (i=0;i<document.frames.length;i++) {
			if (document.frames[i].location.href.toLowerCase().indexOf("calendar.php") > -1)
				CalFrameIndex = i;
		}
	}
	CalFrameObj.setStyles({display: "", left: $(thisObj).getPosition().x+"px",top: $(thisObj).getPosition().y+"px"});
	CalFrameObj.src = "/php_core/calendar.php?"+Math.random();

	document.addEvent("mouseup", hideCalendar);
	return false;
}

function hideCalendar() {
	CalFrameObj.style.display = "none";
	document.removeEvent("mouseup", hideCalendar);
	dateField = null;
}

function setLinkRedirects() {
	if (document.getElementsByTagName) {
		var allAnchors = document.getElementsByTagName("A");
		for (var i=0; i<allAnchors.length; i++) {
			if (allAnchors[i].href.toLowerCase().indexOf("http://") == 0 && allAnchors[i].href.toLowerCase().indexOf(location.hostname.toLowerCase()) == -1) {
				allAnchors[i].target = "_blank";
			}
		}
	}
}

/* Return an object by name, checking to see if the value passed is already an object */
function getThisObj (objName) {
	if (typeof(objName) == 'object') {
		return objName;
	} else {
		if (document.getElementById && document.getElementById(objName)) {
			return document.getElementById(objName);
		} else if (document.layers && document.layers[objName]) {
			return document.layers[objName];
		} else if (document.all && document.all[objName]) {
			return document.all[objName];
		} else if (document[objName]) {
			return document[objName];
		} else {
			alert(objName + " not found!");
			return false;
		}
	}
}

/* get the left pixel position for a given element */
function getXPos(thisObj) {
	var x=0;
	do {
		x += thisObj.offsetLeft;
		thisObj = thisObj.offsetParent;
	} while (thisObj != null)
	return x;
}

/* get the top pixel position for a given element */
function getYPos(thisObj) {
	var y=0;
	do {
		y += thisObj.offsetTop;
		thisObj = thisObj.offsetParent;
	} while (thisObj != null)
	return y;
}

function telephoneFrmt(formElement) {
	formElement.value = formElement.value.replace(/\D+/g,"");
	if (formElement.value.replace(/^1(\d{3})(\d{3})(\d{4})(\d+)/g,"true") == "true") {
		formElement.value = formElement.value.replace(/^1(\d{3})(\d{3})(\d{4})(\d+)/g,"1 ($1) $2-$3 x$4");
	} else if (formElement.value.replace(/^1(\d{3})(\d{3})(\d{4})/g,"true") == "true") {
		formElement.value = formElement.value.replace(/^1(\d{3})(\d{3})(\d{4})/g,"1 ($1) $2-$3");
	} else if (formElement.value.replace(/^(\d{3})(\d{3})(\d{4})(\d+)/g,"true") == "true") {
		formElement.value = formElement.value.replace(/^(\d{3})(\d{3})(\d{4})(\d+)/g,"($1) $2-$3 x$4");
	} else {
		formElement.value = formElement.value.replace(/^(\d{3})(\d{3})(\d{4})/g,"($1) $2-$3");
	}
}

function PSCodeFrmt(formElement) {
	formElement.value = formElement.value.toUpperCase().replace(/[^A-Z0-9]+/g,"");
	if (formElement.value.replace(/^([A-Z]\d[A-Z])(\d[A-Z]\d)$/g,"true") == "true") {
		formElement.value = formElement.value.replace(/^([A-Z]\d[A-Z])(\d[A-Z]\d)$/g,"$1 $2");
	} else {
		formElement.value = "";
	}	
}


function togglePrintView() {
	if (document.styleSheets[0].href == "null") {
		document.styleSheets[0].href = mainStyleSheetUrl;
		document.getElementById("topBanner").style.display="block";
		document.getElementById("clfCommonMenu").style.display="block";
		document.getElementById("sideMenu").style.display="block";
	} else {
		mainStyleSheetUrl = document.styleSheets[0].href;
		document.styleSheets[0].href="null";
		document.getElementById("topBanner").style.display="none";
		document.getElementById("clfCommonMenu").style.display="none";
		document.getElementById("sideMenu").style.display="none";
	}
}


// always returns year, month, day
function getDateArray(dtString,dtFrmt) {
	var separator = "-";
	if (dtFrmt.indexOf('/') != -1) {
		separator = "/";
	} else if (dtFrmt.indexOf('.') != -1) {
		separator = ".";
	}
	var dtFrmtArray = dtFrmt.toLowerCase().split(separator);
	var dtStringArray = dtString.split(separator);
	var ymd = new Array;
	var parseError = false;
	if (dtFrmtArray.length == 3 && dtStringArray.length == 3) {
		for (i=0;i<3;i++) {
			if (dtFrmtArray[i].charAt(0) == "m") {
				ymd[1] = parseInt(dtStringArray[i],10);
			} else if (dtFrmtArray[i].charAt(0) == "d") {
				ymd[2] = parseInt(dtStringArray[i],10);
			} else if (dtFrmtArray[i].charAt(0) == "y") {
				ymd[0] = parseInt(dtStringArray[i],10);
			} else {
				parseError = true;
			}
		}
	} else {
		parseError = true;
	}
	if (parseError) {
		return null;
	} else {
		return ymd;
	}
}
function getDateString(year,month,day,dtFrmt) {
	var separator = "-";
	if (dtFrmt.indexOf('/') != -1) {
		separator = "/";
	} else if (dtFrmt.indexOf('.') != -1) {
		separator = ".";
	}
	var dtFrmtArray = dtFrmt.toLowerCase().split(separator);
	var DateOutput = "";
	var parseError = false;
	if (dtFrmtArray.length == 3) {
		for (var i=0;i<3;i++) {
			if (DateOutput != "") {
				DateOutput = DateOutput + separator;
			}
			if (dtFrmtArray[i] == "mm") {
				DateOutput = DateOutput + padNumber(2,month+"");
			} else if (dtFrmtArray[i] == "m") {
				DateOutput = DateOutput + (parseInt(month,10)+0);
			} else if (dtFrmtArray[i] == "dd") {
				DateOutput = DateOutput + padNumber(2,day+"");
			} else if (dtFrmtArray[i] == "d") {
				DateOutput = DateOutput + (parseInt(day,10)+0);
			} else if (dtFrmtArray[i] == "yy") {
				DateOutput = DateOutput + year.substring(2,3);
			} else if (dtFrmtArray[i] == "yyyy") {
				DateOutput = DateOutput + ((parseInt(year,10) < 100) ? (parseInt(year,10) + 2000) : year);
			} else {
				parseError = true;
			}
		}
	}
	if (parseError) {
		return "";
	} else {
		return DateOutput;
	}
}

function padNumber(padLength,StringNumber) {
	StringNumber = StringNumber.replace(/[^0-9]/g,"");
	var padElem = padLength - StringNumber.length;
	for (var i=0;i<padElem;i++) {
		StringNumber = "0"+StringNumber;
	}
	return StringNumber;
}

var currentFontScaling = null;
function textSize(upDown) {
	if (!currentFontScaling) currentFontScaling = parseInt(Get_Cookie("_fs"),10);
	var currSize = currentFontScaling;
	if (!currSize) currSize = 100;
	if (upDown == 1)
		currSize = currSize+20;
	else if (upDown == -1)
		currSize = currSize-20;
	if (currSize < 80) currSize = 80;
	if (currSize > 200) currSize = 200;
	currentFontScaling = currSize;
	document.getElementsByTagName("BODY")[0].style.fontSize=currSize+"%";
	Set_Cookie("_fs", currSize);
	return false;
}


/******************************************************/
/* Functions for getting and setting cookie values    */
/******************************************************/
function Set_Cookie( _name, _value, _expires, _path, _domain, _secure ) {
	var setCookie = Cookie.set(_name, _value, {duration: (_expires?_expires:0), path: (_path?_path:"/"),  domain: (_domain?_domain:""),  secure: (_secure?_secure:"")});
	if (!_value || _value == "")
		Cookie.remove(setCookie);
}
// this function gets the cookie, if it exists
function Get_Cookie(_name) {
	return Cookie.get(_name);
}



