// JavaScript Document
/**
 * Global Variables
 */
var _url = null;
var screenWidth = 0;
/*	// XHR to request a JS and a CSS
	var xhr = new XMLHttpRequest();
	xhr.open('GET', 'http://mdsscanada.com/Scripts/detect.js');
	xhr.send('');
	//
*/


////<!--
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
// -->

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
////-->

function viewport(){
	w=window,d=document,e=d.documentElement,g=d.getElementsByTagName('body')[0],x=w.innerWidth||e.clientWidth||g.clientWidth,y=w.innerHeight||e.clientHeight||g.clientHeight;
	
	//Set Global Variable
	screenWidth = x;
}

//window.onbeforeonload = ;
//window.onpageshow = swfFitToScreen();
function swfFitToScreen(){
	if ((isIE() == true)&&(getBrowserVersion() >= 8.0)) {
		viewport();
		//x = document width
		var cellWidth = screenWidth - 184; //A1-A3 cell's width = 184 px
		document.getElementById("Flash_Title").width = cellWidth;
	}
}

window.onresize = windowResize();
window.onresize = windowResize();
window.onreset = windowResize();
window.onchange = windowResize();
window.onhaschange = windowResize();
window.onpaint = windowResize();
function windowResize() {
	if (window.screen.width != screenWidth) {
		swfFitToScreen();
		//window.location.refresh;
	}
}
function setActiveProduct(_title, _element) {
	setTitle(_title);
	setCurrent_li(_element);
}
function setTitle(_strTitle) {
	document.title = _strTitle;
	if (document.title != _strTitle) {
		//document.getElementsByTagName('title')[0] = '';
		document.activeElement.setAttribute('title', _strTitle);
	}
}
function setCurrent_li (_elementId) {
	/*
	var attr = document.createAttribute('rel');
	attr.nodeValue = 'me';
	element.setAttributeNode(attr);
	*/
	/*var attrib = document.createAttribute('class');
	attrib.nodeValue = '.current';*/
//	document.getElementById(_elementId).classList.add('current');
/*	var reference = getRefToDiv(_elementId);
	reference.classList.add('current');*/
/*	_url = document.location.href;
	_url= unescape(_url);
	if (_url.*/
}
/***
 * Check if website is accessible or not available.
 */
function checkUrlAccess(_url) {
	var image = new Image();
	
	image.onerror = function() {
		alert('Site not available');
	};
	
	/*image.onload = function() {
		alert('Site available');
	};*/

	// Works
	image.src = 'http://' + _url + '/favicon.ico';
	
	// Shouldn't work
	//image.src = 'http://idefinitelydontexistbutmaybeishould.com/favicon.ico';
}

/*Not Used
function BodyContentFitToScreen(){
	//get values
	viewport(); //x = document width
	//navBarVertical = 154px, padding = 10px, sidebar = 200px
	var navBarVertical = document.getElementByClass("navBarVertical").style.width;
	//var marginLeft = document.getElementById("BodyContent").style.marginLeft;
	var marginLeft = 10; //px
	var marginRight = 10; //px
	var margin = marginLeft + marginRight;
	var sidebar = document.getElementById("Sidebar").style.width;
	
	//calculate
	var bodyContentWidth = x - (navBarVertical + margin + sidebar);
	//output result
	document.getElementByClass("bodyContent").style.width = bodyContentWidth;
}
*/

function ShowHide(element) {
	var myReference = getRefToDiv(element);
	var x = getRefToDiv("x");
	if( !myReference ) {
		window.alert('This feature is not supported by your current Web Browser.\nPlease upgrade to a newer version.');//'Nothing works in this browser'
		return;
	}
	if( myReference.style ) {
		if (myReference.style.visibility == 'hidden') {
			myReference.style.visibility = 'visible';
			x.style.visibility = 'visible';
		} else if (myReference.style.visibility == 'visible') {
			myReference.style.visibility = 'hidden';
			x.style.visibility = 'hidden';
		}
	} else if ( myReference.visibility ) {
		if (myReference.visibility == 'hide') {
			myReference.visibility = 'show';
			x.visibility = 'show';
		} else if (myReference.visibility == 'show') {
			myReference.visibility = 'hide';
			x.visibility = 'hide';
		}
	} else {
		window.alert('This feature is not supported by your current Web Browser.\nPlease upgrade to a newer version.');//'Nothing works in this browser'
		return;
	}
}
function newPage() {
	//All the HTML, XHTML, or whatever, has to be on one line or it will not work!
	document.write('<html><head><title>My Web Page</title></head><body><div id=EchoTopic><p>aWrite 1.0.2 is a small simple JavaScript which writes a whole new web page at the click of a radio button. You can use it for check boxes, select menus, links, and more.</p></div></body></html>');
}

//function doNuhn() {}

function getRefToDiv(divID,oDoc) {
	if( document.getElementById ) { return document.getElementById(divID); }
	if( document.all ) { return document.all[divID]; }
	if( !oDoc ) { oDoc = document; }
	if( document.layers ) {
		if( oDoc.layers[divID] ) {
			return oDoc.layers[divID];
		} else {
			for( var x = 0, y; !y && x < oDoc.layers.length; x++ ) {
				y = getRefToDiv(divID,oDoc.layers[x].document);
			}
			return y;
		}
	}
	return false;
}
/*============================================================================================
function showDiv(divID_as_a_string) {
	var myReference = getRefToDiv(divID_as_a_string);
	if( !myReference ) { window.alert('Nothing works in this browser'); return; }
	if( myReference.style ) { myReference.style.visibility = 'visible'; } else {
		if( myReference.visibility ) { myReference.visibility = 'show'; } else {
			window.alert('Nothing works in this browser'); return; } }
}

function hideDiv(divID_as_a_string) {
	var myReference = getRefToDiv(divID_as_a_string);
	if( !myReference ) { window.alert('Nothing works in this browser'); return; }
	if( myReference.style ) { myReference.style.visibility = 'hidden'; } else {
		if( myReference.visibility ) { myReference.visibility = 'hide'; } else {
			window.alert('Nothing works in this browser'); return; } }
}

function mvDiv(divID_as_a_string) {
	var myReference = getRefToDiv(divID_as_a_string), noPx = document.childNodes ? 'px' : 0;
	if( !myReference ) { window.alert('Nothing works in this browser'); return; }
	if( myReference.style ) { myReference = myReference.style; }
	if( parseInt(myReference.left) ) {
		window.alert('Sorry, I will not allow you to move this element too far,\ninstead I will reset it.');
		myReference.left = 0 + noPx; myReference.top = 0 + noPx; } else {
		myReference.left = 20 + noPx; myReference.top = 10 + noPx; }
}

var clodiv = true;

function clDiv(divID_as_a_string) {
	var dC = clodiv ? '#00ff00' : '#dddddd'; clodiv = clodiv ? false : true;
	var myReference = getRefToDiv(divID_as_a_string);
	if( !myReference ) { window.alert('Nothing works in this browser'); return; }
	if( myReference.style ) { myReference = myReference.style; }
	myReference.background = dC;
	myReference.backgroundColor = dC;
	myReference.bgColor = dC;
}

var rewr = 0;

function rwDiv(divID_as_a_string) {
	var myReference = getRefToDiv(divID_as_a_string);
	if( !myReference ) { window.alert('Nothing works in this browser'); return; }
	if( myReference.innerHTML ) { myReference.innerHTML = '<p>The contents of this element have been rewritten ' + ( ++rewr ) + ' time' + ( ( rewr - 1 ) ? 's' : '' ) + '.<\/p>'; } else {
		if( myReference.document && myReference.document != window.document ) {
			myReference.document.open();
			myReference.document.write( '<p>The contents of this element have been rewritten ' + ( ++rewr ) + ' time' + ( ( rewr - 1 ) ? 's' : '' ) + '.<\/p>' );
			myReference.document.close();
		} else {
			window.alert('Your browser does not allow the contents to be re-written.\nI have not implemented an iframe here.\nSee my generic re-writable elements example for how this should be done.'); return; }
    }
}

function clpDiv(divID_as_a_string,ot,ob) {
	var myReference = getRefToDiv(divID_as_a_string);
	if( !myReference ) { window.alert('Nothing works in this browser'); return; }
	if( myReference.clip ) { myReference.clip.top = ot; myReference.clip.bottom = ob; } else {
		if( myReference.style ) { myReference.style.clip = 'rect(' + ot + 'px,250px,' + ob + 'px,0px)'; } else {
			window.alert('Nothing works in this browser'); return; } }
}

var zInd = 300;

function upEl(theDiv) {
	if( document.layers ) {
		theDiv = document.layers['myDiv6C'].document.layers[theDiv];
	} else {
		theDiv = getRefToDiv(theDiv);
	}
	if( !theDiv ) { window.alert( 'Nothing works in this browser.' ); return; }
	if( theDiv.style ) { theDiv = theDiv.style; }
	theDiv.zIndex = zInd; zInd++;
}

function changeDisplay( elementId, setTo ) {
	if( document.getElementById ) { var theElement = document.getElementById( elementId ); } else {
		if( document.all ) { var theElement = document.all[ elementId ]; } else { var theElement = new Object(); } }
	if( !theElement ) { return; }
	if( theElement.style ) { theElement = theElement.style; }
	if( typeof( theElement.display ) == 'undefined' && !( window.ScriptEngine && ScriptEngine().indexOf( 'InScript' ) + 1 ) ) { window.alert( 'Nothing works in this browser.' ); return; }
	theElement.display = setTo;
}

function chBG( oColour ) {
	if( document.documentElement && document.documentElement.style ) {
		document.documentElement.style.backgroundColor = oColour; }
	if( document.body && document.body.style ) { document.body.style.backgroundColor = oColour; }
	document.bgColor = oColour;
}

function rSDiv(oName,newWidth,newHeight) {
	var myReference = getRefToDiv(oName), noPx = document.childNodes ? 'px' : 0;
	if( myReference.style ) { myReference = myReference.style; }
	if( myReference.resizeTo ) { myReference.resizeTo( newWidth, newHeight ); }
	myReference.width = newWidth + noPx; myReference.pixelWidth = newWidth;
	myReference.height = newHeight + noPx; myReference.pixelHeight = newHeight;
}

function createPosEl() {
	window.oPsnx = window.oPsnx ? ( window.oPsnx + 1 ) : 1;
	if( document.layers && window.Layer ) {
		var a = new Layer( 350, document.layers['elNew'] );
		a.document.open();
		a.document.write('This positioned element (number ' + window.oPsnx + ') has been created inside the existing positioned element');
		a.document.close();
		a.bgColor = ( window.oPsnx % 2 ) ? '#ff6666' : '#66ff66'; a.left = window.oPsnx * 2; a.top = window.oPsnx * 2; a.visibility = 'show';
	} else {
		var a = getRefToDiv('elNew');
		var theString = '<div style="position:absolute;left:'+(window.oPsnx*2)+'px;top:'+(window.oPsnx*2)+'px;width:350px;background-color:#'+((window.oPsnx%2)?'ff6666':'66ff66')+';">This positioned element (number ' + window.oPsnx + ') has been created inside the existing positioned element<\/div>';
		if( a.iinsertAdjacentHTML ) { a.insertAdjacentHTML('beforeEnd',theString);
		} else if( typeof( a.innerHTML ) != 'undefined' ) { a.innerHTML += theString;
		} else { window.alert('Nothing works in this browser'); }
	}
}
=====================================================================================================*/
/********************
 * Browser Detection
 ********************/
 function isIE() {
	 //***
	 //* Detect Client Browser type
	 //* Copyright 2005-2007 Adobe Systems Incorporated.  All rights reserved.
	var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
	 //**
	return isIE;
}
function getBrowserVersion() {
	var browserVersion = null;
	if (isIE() == true) {
		var IEVersion = readIEVersion();
		//window.alert('IE Version: ' + IEVersion);
		browserVersion = IEVersion;
	}
	return browserVersion;
}

//
function readIEVersion( ) {
    var ua = navigator.userAgent;
    var IEOffset = ua.indexOf("MSIE ");
    //return parseFloat(ua.substring(IEOffset + 5, ua.indexOf(";", IEOffset)));
	var value = (ua.substring(IEOffset + 5, ua.indexOf(";", IEOffset)));
	return value;
}

