﻿var IE = (navigator.userAgent.indexOf("Win") != -1) && (navigator.appVersion.indexOf("MSIE")!=-1);
var IEVersion = IE ? parseFloat((new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})")).exec(navigator.userAgent)[1]):-1;
var IE6 = IEVersion == 6 ? true : false;

if (((navigator.appName == 'Netscape')&&(parseInt(navigator.appVersion.charAt(0)) <= 4))||(navigator.userAgent.indexOf('Netscape6')!=-1)){
			//document.location.replace('/ca/browser.html');
}

function setHighContrast( IContrast ) {
	try {	// Must fail silently if the <link/> elements aren't present
		if( IContrast == 0 ) {
			document.getElementById( "highContrastLink" ).href = "../StyleSheet/styles.css";

			document.getElementById( "normalContrast" ).className = "contrastOff";
			document.getElementById( "normalContrast" ).removeAttribute( "href" );

			document.getElementById( "highContrast" ).className = "";
			document.getElementById( "highContrast" ).setAttribute( "href", "javascript:setHighContrast(1)" );
		} else {
			document.getElementById( "highContrastLink" ).href = "../StyleSheet/custom.css";

			document.getElementById( "normalContrast" ).className = "";
			document.getElementById( "normalContrast" ).setAttribute( "href", "javascript:setHighContrast(0)" );

			document.getElementById( "highContrast" ).className = "contrastOff";
			document.getElementById( "highContrast" ).removeAttribute( "href" );
		}

		setCookie2( "Contrast", IContrast );
	} catch( err ) {}
}
// 	getCookieValue
function getCookieValue(cookieName){
	var cookieValue = document.cookie;
	var cookieStartsAt = cookieValue.indexOf(" " + cookieName + "=");
	
	// check if cookie exists.  If cookie does not exist, do a second check at the beginning of the string
	if (cookieStartsAt == -1){
		cookieStartsAt = cookieValue.indexOf(cookieName + "=");
	}
	
	if (cookieStartsAt == -1){
		// coookie really does not exist
		cookieValue = null;
	}else{
		cookieStartsAt = cookieValue.indexOf("=", cookieStartsAt) + 1;
		var cookieEndsAt = cookieValue.indexOf(";", cookieStartsAt);
		if (cookieEndsAt == -1){
			cookieEndsAt = cookieValue.length;
		}
		cookieValue = unescape(cookieValue.substring(cookieStartsAt, cookieEndsAt));
	}
	return cookieValue;
}

// setSessionCookie
function setSessionCookie(cookieName, cookieValue, cookiePath, cookieDomain){
	cookieValue = escape(cookieValue);
	
	if (cookiePath == "" || cookiePath == null){
		cookiePath = "; path=/";
	}else{
		cookiePath = "; path=" + cookiePath;
	}
	
	if (cookieDomain == "" || cookieDomain == null){
		cookieDomain = "";
	}else{
		cookieDomain = "; domain=" + cookieDomain;
	}
	document.cookie = cookieName + "=" + cookieValue + ";" + cookiePath + cookieDomain;
}

// setCookie
function setCookie2(cookieName, cookieValue, cookieExpires, cookiePath, cookieDomain){
	cookieValue = escape(cookieValue);
	
	if (cookieExpires == "" || cookieExpires == null){
		var nowDate = new Date();
		nowDate.setMonth(nowDate.getMonth() + 1);
		cookieExpires = nowDate.toGMTString();
	}
	
	if (cookiePath == "" || cookiePath == null){
		cookiePath = "; path=/";
	}else{
		cookiePath = "; path=" + cookiePath;
	}

	if (cookieDomain == "" || cookieDomain == null || cookieDomain == undefined){
		cookieDomain = "; domain=" + document.domain.replace(/www\./,"");
	}else{
		cookieDomain = "; domain=" + cookieDomain;
	}
	//alert(cookieName + "=" + cookieValue + "; expires=" + cookieExpires + cookiePath + cookieDomain);
	document.cookie = cookieName + "=" + cookieValue + "; expires=" + cookieExpires + cookiePath + cookieDomain;
}



var trackTest = getCookieValue("trackCookie");
var locTest = getCookieValue("locCookie");
var domain = ".bruyere.org";
var trackingVal = "";
var theDate = new Date();
theDate.setYear(theDate.getFullYear() + 1);
dateExpires = theDate.toGMTString();
if(!trackTest){
	trackingVal = Math.random() * 99999999999999999;
	setCookie2('trackCookie', trackingVal, dateExpires, '/', domain);
}
if(!locTest){
	trackingVal = "m";
	setCookie2('locCookie', trackingVal, dateExpires, '/', domain);
}




function textSize( ISize ) {
	try {	// Must fail silently if the <link/> elements aren't present
		if( ISize === null ) {
			ISize = 0;
		}
		
		/* Some browser detection until the website is redesigned  */
		var browser=navigator.appName;
		
		if (navigator.appName=='Netscape') {
			document.getElementById( "textSizeLink" ).href = "../StyleSheet/textsizeNets-" + ISize + ".css";
		}
		else {
			document.getElementById( "textSizeLink" ).href = "../StyleSheet/textsize-" + ISize + ".css";
		}
		

		AButtons = getElementsByClassName( "letterButton", null, "img" );
		if( AButtons.length > 0 ) {
			IButtons = AButtons.length;
			for( i = 0; i != IButtons; i++ ) {
				if( i == ISize ) {
					SSelected = "sel";
				} else {
					SSelected = "des";
				}

				AButtons[i].src = "../imgs/" + i + "-" + SSelected + ".gif";
			}
		}

		setCookie2( "FontSize", ISize );
	} catch( err ) {}
}


	function getElementsByClassName(searchClass, node, tag) {
		var classElements = new Array();
		if (node == null) { node = document; }
		if (tag == null) { tag = '*'; }

		// safari mac 2.0 doesn't support this
		var els = node.getElementsByTagName(tag);

		var elsLen = els.length;
		var pattern = new RegExp("(^|\\s)" + searchClass + "(\\s|$)");
		for (var i = 0, j = 0; i < elsLen; i++) {
			if (pattern.test(els[i].className)) {
				classElements[j] = els[i];
				j++;
			}
		}
		return classElements;
	}



function setInitialAccessibility() {
	var IContrast = getCookieValue( "Contrast" );
	if( IContrast === null ) {
		IContrast = 0;
	}

	setHighContrast( IContrast );

	var ISize = getCookieValue( "FontSize" );
	textSize( ISize );
}

// add setInitialAccessibility to the window's onLoad chain.
//window.addEventListener( "load", setInitialAccessibility, false );
window.setTimeout( setInitialAccessibility, 100 );




