/**** WM_COOKIES.JS BROUGHT IN ****/

/*
Source: Webmonkey Code Library
(http://www.hotwired.com/webmonkey/javascript/code_library/)
Author: Nadav Savio
Author Email: nadav@wired.com
*/

// test whether the user accepts cookies.
var WM_acceptsCookies = false;
if(document.cookie == '') {
	document.cookie = 'WM_acceptsCookies=yes'; // Try to set a cookie.
	if(document.cookie.indexOf('WM_acceptsCookies=yes') != -1) {
		// If it succeeds, set variable
		WM_acceptsCookies = true; 
	}
} else {
	// cookie exists
	WM_acceptsCookies = true;
}

function WM_setCookie (name, value, hours, path, domain, secure) {
	// path is not needed since style should be applied to all pages.
	path = "/";
	if (WM_acceptsCookies) { // Don't waste your time if the browser doesn't accept cookies.
		var not_NN2 = (navigator && navigator.appName 
		       && (navigator.appName == 'Netscape') 
		       && navigator.appVersion 
		       && (parseInt(navigator.appVersion) == 2))?false:true;

		if(hours && not_NN2) {
			// NN2 cannot handle Dates, skip this part
			if ((typeof(hours) == 'string') && Date.parse(hours)) {
				// already a Date string
				var numHours = hours;
			} else if (typeof(hours) == 'number') {
				// calculate Date from number of hours
				var numHours = (new Date((new Date()).getTime() + hours*3600000)).toGMTString();
			}
		}
		document.cookie = name + '=' + escape(value) + ((numHours)?(';expires=' + numHours):'') + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'') + ((secure && (secure == true))?'; secure':''); // Set the cookie, adding any parameters that were specified.
	}
}

function WM_readCookie(name) {
	if(document.cookie == '') {
		// there's no cookie, go no further
		return false; 
	} else {
		// cookie exists
		var firstChar, lastChar;
		var theBigCookie = document.cookie;
		firstChar = theBigCookie.indexOf(name);
		var NN2Hack = firstChar + name.length;
		if((firstChar != -1) && (theBigCookie.charAt(NN2Hack) == '=')) {
			// found the cookie
			firstChar += name.length + 1; // skip 'name' and '='
			lastChar = theBigCookie.indexOf(';', firstChar);
			if(lastChar == -1) {
				lastChar = theBigCookie.length;
			}
			return unescape(theBigCookie.substring(firstChar, lastChar));
		} else {
			// If there was no cookie of that name, return false.
			return false;
		}
	}	
}

function WM_killCookie(name, path, domain) {
	var theValue = WM_readCookie(name); // We need the value to kill the cookie
	if(theValue) {
		document.cookie = name + '=' + theValue + '; expires=Fri, 13-Apr-1970 00:00:00 GMT' + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:''); // set an already-expired cookie
	}
}

/*** END OF WM_COOKIES ***/

//OFT1.1

/***
*  Onfocus tooltips by Brothercake
*  http://www.brothercake.com/
/***

//global object and initial properties
var tip = {
	'tooltip' : null,
	'parent' : null,
	'timer' : null
};

//initialisation function
tip.init = function() {
	//if the necessary collection is supported
	if(typeof document.getElementsByTagName != 'undefined') {
		//get all tags 
		tip.tags = document.getElementsByTagName('*');
		tip.tagsLen = tip.tags.length;
		for (var i=0; i < tip.tagsLen; i++) {
			//if tag has a title attribute
			if(tip.tags[i].title)
			{
				//attach handlers
				tip.tags[i].onfocus = tip.focusTimer;
				tip.tags[i].onblur = tip.blurTip;
				tip.tags[i].onmouseover = tip.blurTip;

			}
		}
	}
}

//setup initialization function
if(typeof window.addEventListener != 'undefined') {
	//.. gecko, safari, konqueror and standard
	window.addEventListener('load', tip.init, false);
	window.addEventListener('load', fillYear, false);
} else if(typeof document.addEventListener != 'undefined') {
	//.. opera 7
	document.addEventListener('load', tip.init, false);
	document.addEventListener('load', fillYear, false);
} else if(typeof window.attachEvent != 'undefined') {
	//.. win/ie
	window.attachEvent('onload', tip.init);
	window.attachEvent('onload', fillYear);
}

//find object position
tip.getRealPosition = function(ele, dir) {
	tip.pos = (dir=='x') ? ele.offsetRight : ele.offsetTop;
	tip.tmp = ele.offsetParent;
	while(tip.tmp != null) {
		tip.pos += (dir=='x') ? tip.tmp.offsetRight : tip.tmp.offsetTop;
		tip.tmp = tip.tmp.offsetParent;
	}
	return tip.pos;
}

//delay timer
tip.focusTimer = function(e) {
	//second loop	
	if(tip.timer != null) {
		//clear timer
		clearInterval(tip.timer);
		tip.timer = null;
	
		//pass object to create tooltip
		tip.focusTip(e);
	} else {
		//first loop
		//get focussed object to pass back through timer
		tip.tmp = (e) ? e.target : event.srcElement;
		//set interval
		tip.timer = setInterval('tip.focusTimer(tip.tmp)',400);
	}
}

//create tooltip
tip.focusTip = function(obj) {
	//remove any existing tooltip
	tip.blurTip();
	//if tooltip is null
	if(tip.tooltip == null) {
		//get window dimensions
		if(typeof window.innerWidth!="undefined") {
			tip.window = {
				x : window.innerWidth,
				y : window.innerHeight
			};
		} else if(typeof document.documentElement.offsetWidth!="undefined") {
			tip.window = {
				x : document.documentElement.offsetWidth,
				y : document.documentElement.offsetHeight
			};
		} else {
			tip.window = {
				x : document.body.offsetWidth,
				y : document.body.offsetHeight
			};
		}
		//create toolTip, detecting support for namespaced element creation, in case we're in the XML DOM
		tip.tooltip = (typeof document.createElementNS != 'undefined') ? document.createElementNS('http://www.w3.org/1999/xhtml', 'div') : document.createElement('div');

		//add classname
		tip.tooltip.setAttribute('class','');
		tip.tooltip.className = 'tooltip';

		//get focussed object co-ordinates
		if(tip.parent == null) {
			tip.parent = {
				x : tip.getRealPosition(obj,'x') - 3,
				y : tip.getRealPosition(obj,'y') + 2
			};
		}

		// offset tooltip from object
		tip.parent.y += obj.offsetHeight;

		//apply tooltip position
		tip.tooltip.style.right = tip.parent.x + 'px';
		tip.tooltip.style.top = tip.parent.y + 'px';

		//write in title attribute 
		tip.tooltip.appendChild(document.createTextNode(obj.title));

		//add to document
		document.getElementsByTagName('body')[0].appendChild(tip.tooltip);

		//restrict width
		if(tip.tooltip.offsetWidth > 300) {
			tip.tooltip.style.width = '300px';
		}

		//get tooltip tip.extent
		tip.extent = {
			x : tip.tooltip.offsetWidth,
			y : tip.tooltip.offsetHeight
		};

		//if tooltip exceeds window width
		if((tip.parent.x + tip.extent.x) >= tip.window.x) {
			//shift tooltip right
			tip.parent.x -= tip.extent.x;
			tip.tooltip.style.right = tip.parent.x + 'px';
		}
		
		//get scroll height
		if(typeof window.pageYOffset!="undefined") {
			tip.scroll = window.pageYOffset;
		} else if(typeof document.documentElement.scrollTop!="undefined") {
			tip.scroll = document.documentElement.scrollTop;
		} else {
			tip.scroll = document.body.scrollTop;
		}

		//if tooltip exceeds window height
		if((tip.parent.y + tip.extent.y) >= (tip.window.y + tip.scroll)) {
			//shift tooltip up
			tip.parent.y -= (tip.extent.y + obj.offsetHeight + 4);
			tip.tooltip.style.top = tip.parent.y + 'px';
		}
	}
}

//remove tooltip
tip.blurTip = function() {
	try {	
		//if tooltip exists
		if(tip.tooltip != null) {
			//remove and nullify tooltip
			document.getElementsByTagName('body')[0].removeChild(tip.tooltip);
			tip.tooltip = null;
			tip.parent = null;
		}
	
		//cancel timer
		clearInterval(tip.timer);
		tip.timer = null;
	} catch(e){}
}

/**********/

function P7_JumpMenu(selObj,restore){ //v1.3 by Project Seven
	var theFullString = selObj.options[selObj.selectedIndex].value;
	if (restore) selObj.selectedIndex=0;
	var theLength = theFullString.length;
	var endPos = theFullString.lastIndexOf("~");
	var theUrl, theTarget, theParent;
	if (endPos > 0) {theUrl = theFullString.substring(0,endPos);}
	else {theUrl = theFullString;}
	endPos++
	if (endPos < theLength) {theTarget = theFullString.substring(endPos,theLength)}
	else {theTarget = "window:Main";}
	if (theTarget == "window:New") {window.open(theUrl);}
	else if (theTarget == "window:Main") {eval("parent.location='"+theUrl+"'");}
	else {eval("parent.frames[\'"+theTarget+"\'].location='"+theUrl+"'");}
}

/**********/

// This code taken from 
//  http://www.brucelawson.co.uk/2005/opening-links-in-new-windows-in-xhtml-strict-2/
// to make sure that links that used to use rel="external"(which is not Strict XHTML)
// now comply and also are accessible.
// NOTE - you must use the rel="external" attribute of the anchor tag for this to work.


function externalLinks() {
	var objCurrent, objReplacement;
	var opentype="";
	
	if (isFrenchSite()) {
		opentype=" (ouvrir une nouvelle fenêtre)";
	} else {
		opentype=" (open new window)";
	}
	
	if (document.getElementsByTagName) {
		var objAnchors = document.getElementsByTagName('a');
		for (var iCounter=0; iCounter<objAnchors.length; iCounter++) {
			if (objAnchors[iCounter].getAttribute('href') &&objAnchors[iCounter].getAttribute('rel') == 'external') {
				objAnchors[iCounter].onclick = function(event){return launchWindow(this, event);}
				objAnchors[iCounter].onkeypress = function(event){return launchWindow(this, event);}
				if (isFrenchSite()) {
					objAnchors[iCounter].title = (objAnchors[iCounter].title != "") ? objAnchors[iCounter].title+' (ouvrir une nouvelle fenêtre)' : "open new window";
				} else {
					objAnchors[iCounter].title = (objAnchors[iCounter].title != "") ? objAnchors[iCounter].title+" (open new window)" : "open new window";
				}

				if (document.replaceChild) {
					objCurrent = objAnchors[iCounter].firstChild;
					var replaced = false;
					if (objCurrent.nodeType == 3) {
						// Text node
     						if (isFrenchSite()) {
     							if (objCurrent.data.indexOf("(ouvrir une nouvelle")==-1) {
     								replaced=true;
     								objReplacement = document.createTextNode(objCurrent.data + ' (ouvrir une nouvelle fenêtre)');
     							}
     						} else {
     							if (objCurrent.data.indexOf("(open new window)")==-1) {
     								replaced=true;
								objReplacement = document.createTextNode(objCurrent.data + ' (open new window)');
							}
						}
						if(replaced) {
							objAnchors[iCounter].replaceChild(objReplacement, objCurrent);
						}
					} else if (objCurrent.alt) {
						// Current element is an image
						objReplacement = objCurrent;
						if (isFrenchSite()) {
							if (objReplacement.alt.indexOf("(ouvrir une nouvelle")==-1) {
								replaced=true;
								objReplacement.alt = objCurrent.alt + ' (ouvrir une nouvelle fenêtre)';
							}
						} else {
							if (objReplacement.alt.indexOf("(open new window)")==-1) {
								replaced=true;
								objReplacement.alt = objCurrent.alt + ' (open new window)';
							}
						}
						if (replaced) {
							try {
								objAnchors[iCounter].replaceChild(objReplacement, objCurrent);
							} catch(e){}
						}
					}
				}
			}
		}
	}
}

function launchWindow(objAnchor, objEvent) {
	var iKeyCode;

	if (objEvent && objEvent.type == 'keypress') {
		if (objEvent.keyCode) {
			iKeyCode = objEvent.keyCode;
		} else if (objEvent.which) {
			iKeyCode = objEvent.which;
		}
		if (iKeyCode != 13 && iKeyCode != 32) {
			return true;
		}
	}
	return !window.open(objAnchor);
}

/*Use for Newsletters */
function gotoLink(f)     //checks all the address and name info, returns in the validate function
{
	var x="";
	if (isFrenchSite()) {
		if (f.month.value=="99") {
			alert ("Vous devez choisir un mois valide afin de continuer.");
		} else {
			x = "/francais/news/energy_matters/em_";
		}
	} else {
		if (f.month.value=="99") {
			alert ("You must select a valid month in order to proceed.");
		} else {
			if (f.name=="em") {
				x = "/news/energy_matters/em_";
			} else if (f.name=="ins") {
				x = "/news/insights/insights_";
			}
		}
	}
	if (x!="") {
		x=x + f.year.value.substr(2) + "_" + f.month.value + ".shtml"
		window.location.href=x;
	}
	
}

/*this hides all the form select boxes when you use the zoom picture
put this code on the link
onmouseover="hideSelect()" onmouseout="unhideSelect()"
*/

function show_hideSelect(show_hide) {
	if (document.all) {// Only do this for IE
		for (i=0; i<document.forms.length; i++) {
			var f = document.forms[i];
			for(x=0; x<f.elements.length; x++) {
				window.status += f[x].type;
				if(f[x].type == "select-one") {
					f[x].style.visibility = show_hide;
				}
			}
		}
	}
}


// Hide all select boxes
function hideSelect() {
	show_hideSelect("hidden");
}

// Unhide all select boxes
function unhideSelect() {
 	show_hideSelect("visible");
}

/*****/
function dpSmartLink(u,n,w,h,p) { // v1.4 by David Powers
  var a,j,k,x,y,f='';if(!n){n='';}if(w){f+='width='+w+',';}if(h){f+='height='+h+',';}
  if(p){p=p.split(':');if(p[0]!='z'){p[0]=='c'?(x=(screen.width-w)/2):x=p[0];f+='left='+x+',';}
  if(p[1]!='z'){if(p[0]=='c'){y=(screen.height-h-p[1])/2;if(navigator.appName.indexOf('Op')!=-1){
  y-=96;y=y<0?0:y;}}else{y=p[1];}f+='top='+y+',';}}a=arguments.length;if(a>5){for (k=5;k<a;k++){
  switch(arguments[k]){case 'all':f+='toolbar,menubar,location,scrollbars,status,resizable,';break;
  case 't':f+='toolbar,';break; case 'm':f+='menubar,';break;case 'l':f+='location,';break;
  case 'sc':f+='scrollbars,';break;case 's':f+='status,';break;case 'r':f+='resizable,';}}}
  if(f.charAt(f.length-1)==','){f=f.slice(0,-1);}j=window.open(u,n,f);j.focus();
  document.MM_returnValue=false;
}

/*****/
/*put the year in for year options */
function fillYear() {
	var yearsToDisplay=3;
	var formIDs = document.getElementsByTagName("form");
	var time=new Date();
	var year=time.getFullYear()-yearsToDisplay;

	for(var i=0; i<formIDs.length; i++) {
		if (document.forms[i].elements["year"] !=null) {
			for (y=(yearsToDisplay); y>0; y--) {
				var option = document.createElement("OPTION");
				option.text = option.value = year+y;
				try {
					document.forms[i].elements["year"].add(option, null);
				} catch(ex) {
					// For IE.
					document.forms[i].elements["year"].add(option);
				} 
			}
		}
	}
}

/*for news release application*/
function Home() {
	var f = document.forms[0];
	f.action = "MainLaunch";
	f.submit();
}

/*hide and unhide layers */
function unhide(divID) {
  var item = document.getElementById(divID);
  if (item) {
    item.className=(item.className=='hideBlock')?'unhidden':'hideBlock';
  }
}

document.createElement('abbr');

function setName() {
	window.name="MyMain"
}

/*** BOOKMARKLET.JS BROUGHT IN ***/

u=""; 

isGecko = isOpera = isIE = false;

if (window.opera) {
	// first, because opera likes to spoof
	isOpera = true;

	if (!document.createComment) {
		alert("You're using Opera 6 or older.  Please upgrade to Opera 7. (This tool works in Opera 7, Mozilla, and IE.)");
	}
} else if (navigator.userAgent.indexOf("Gecko") != -1) {
	isGecko = true;

	if (navigator.userAgent.indexOf("rv:0.") != -1) {
		alert("You are using an old version of Mozilla.  Please upgrade.");
	}
} else if (navigator.userAgent.indexOf("MSIE") != -1) {
	isIE = true;
} else {
	alert("I'm not familiar with the browser you're using.  I'll pretend you're using Mozilla, but this tool probably won't work for you.");
	isGecko = true;
}

function singleQuoteEscape(s) { return s.replace(/\\/g,"\\\\").replace(/\'/g,"\\\'"); }
function doubleQuoteEscape(s) { return s.replace(/\\/g,'\\\\').replace(/\"/g,'\\\"'); }
function percentEscape(s)     { return s.replace(/\%/g,'%25'); }

function makeBookmarklet (styles) {
	// Convert all whitespace, including newlines, to single spaces.
	styles = styles.replace(/\s+/g, " ");

	// Remove whitespace from the beginning or end of the styles.
	styles = styles.replace(/^\s/, "");
	styles = styles.replace(/\s$/, "");

	if (isIE) {
		// Two percentEscapes for being in two javascript URLs.
		// Two *quoteEscapes for being in two javascript strings in the generated bookmarklet.
		return "javascript:document.createStyleSheet(\"javascript:'" + percentEscape(doubleQuoteEscape(percentEscape(singleQuoteEscape(styles)))) + "'\").v"
	} else if (isGecko) {
		/* Escape styles twice: once for javascript: urls (51355?), 
		* and once for data: urls (136538, fixed for 1.0 and for 1.1alpha).  
		* Call percentEscape as we generate the bookmarklet and then call
		* the more conservative escape() in the generated bookmarklet itself.
		*/
		return "javascript:styles='" + percentEscape(singleQuoteEscape(styles)) + "'; newSS = document.createElement('link'); newSS.rel = 'stylesheet'; newSS.href = 'data:text/css,' + escape(styles); document.documentElement.childNodes[0].appendChild(newSS); void 0"
	} else if (isOpera) {
		/* Opera 7: 
		* Create stylesheet like Mozilla
		* But don't append to document (heirarchy_request_error; unnecessary!)
		* URL of stylesheet is javascript:, like in IE.
		* javascript: URLs aren't %-escaped like they are in IE and Mozilla.
		*/
		return "javascript:styles='" + singleQuoteEscape(styles) + "'; newSS = document.createElement('link'); newSS.rel = 'stylesheet'; newSS.href = (\"javascript:'" + doubleQuoteEscape(singleQuoteEscape(styles)) + "'\"); void 0";
	}
}

function updateLink (styles,idlink) {
	//  styles = document.getElementById("idstyles").value
	//  name = document.getElementById(idname).value;
	CL = document.getElementById(idlink);
	CL.href = makeBookmarklet(styles);
}

/*** END OF BOOKMARKLET.JS CODE ***/

/*** LANGUAGE.JS BROUGHT IN ***/

function switchLanguage() {
	var currLoc, newLoc, newDefault;
	currLoc = new String(document.location);
	if(currLoc.search(/\/francais\//i) >= 0) {
		// This is the french version, going to english
		newLoc = currLoc.replace(/hydro.mb.ca\/francais\//, "hydro.mb.ca/");
		newDefault = "http://www.hydro.mb.ca/index.shtml";
	} else {
		// This is the english version, going to french
		newLoc = currLoc.replace(/hydro.mb.ca\//, "hydro.mb.ca/francais/");
		newDefault = "http://www.hydro.mb.ca/francais/index.shtml";
	}
	gotoIfExists(newLoc, newDefault);
}

// Reference: http://jibbering.com/2002/4/httprequest.html
// Original code by Jim Ley

function createXMLHTTP() {
	var xmlhttp = false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	 try {
	  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
	  try {
	   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (E) {
	   xmlhttp = false;
	  }
	 }
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	  xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

var pendingLocation, pendingRedirect;
var getter;

function gotoIfExists(page, alternate) {
	getter = createXMLHTTP();
	pendingLocation = page;
	pendingRedirect = alternate;
	getter.onreadystatechange = function() {
		if(getter.readyState == 4) {
			if(getter.status == 200) {
				document.location = pendingLocation;
			} else {
				document.location = pendingRedirect;
			}
		}
	}
	getter.open("HEAD", page, true);
	// .send needs null parameter to work in FireFox.
	getter.send(null);
}

/*** END OF LANGUAGE.JS ***/

function getTables() {
	if (!document.getElementsByTagName) {
		return false;
	}

	var tables = document.getElementsByTagName("table");
	for (var z=0; z<tables.length; z++)
	for (var zz=0; zz<tables.length; zz++){
		if ((tables[z].className=="mh stripe") || (tables[zz].className=="mh numbersRight stripe")) {
		stripeTables(tables[z]);
		stripeTables(tables[zz]);		}
	}
}

function stripeTables(table) {
	var tr = table.getElementsByTagName("tr");
	for (var i=1; i<tr.length; i++) {
		if (tr[i].parentNode.tagName.toUpperCase()!="THEAD") {
			if (i%2==0) {
				tr[i].className = "oddrow";
			} else {
				tr[i].className = "evenrow";
			}
		}
	}
}

function isFrenchSite() {
	var server = location.toString();
	if (server.indexOf("francais")>-1) {
		return true;
	} else {
		return false;
	}
}

//setup initialisation function
if(typeof window.addEventListener != 'undefined') {
	//.. gecko, safari, konqueror and standard
	window.addEventListener('load', externalLinks, false);
} else if(typeof document.addEventListener != 'undefined') {
	//.. opera 7
	document.addEventListener('load', externalLinks, false);
} else if(typeof window.attachEvent != 'undefined') {
	//.. win/ie
	window.attachEvent('onload', externalLinks);
}
/*****/

