/*
SWITCH language
*/
function switchLang() {
	var URL = document.location.href;
	var newURL = URL;
	var fIndex = URL.lastIndexOf( '_f.asp' );
	if( fIndex > 0 ) {
		//Switch to english page
		newURL = URL.substr( 0, fIndex ) + URL.substr( fIndex + 2 );
		alert( newURL );
	}
	else {
		//Switch to french page
		var offSet = 6
		fIndex = URL.lastIndexOf( '_e.asp' );
		if( fIndex == -1 ) { fIndex = URL.lastIndexOf( '.asp' ); offSet = 4; }
		if( fIndex > 0 ) {
			newURL = URL.substr( 0, fIndex ) + '_f.asp' + URL.substr( fIndex + offSet );
		}
	}
	document.location.href = newURL;
}

/*
SPAWN Window
*/
var objWin = new Object;
function openWin( url, width, height, allowScroll, dontClose, allowResize ) {
	if( allowScroll != "" ) { allowScroll = String( allowScroll ).toLowerCase(); }
	if( allowScroll != 'yes' && allowScroll != 'no' ) { allowScroll = 'yes'; }
	if( allowResize != "" ) { allowResize = String( allowResize ).toLowerCase(); }
	if( allowResize != 'yes' && allowResize != 'no' ) { allowResize = 'no'; }
	
	//allowScroll = 'yes'; // Disable allowScroll option, but keep code in place for future use
	var x = (640 - width)/2; var y = (480 - height)/2;
	if( screen ) { y = ( screen.availHeight - height ) / 2; x = ( screen.availWidth - width ) / 2; }
	if( screen.availWidth > 1800 ) { x = ( ( screen.availWidth / 2 ) - width ) / 2; }
	if( objWin.closed == false && dontClose != true ) { objWin.close(); }

	//if( objWin != null ) { objWin.close(); }
	objWin = window.open( url, "Win" + dontClose, 'width=' + width + ',height=' + height + ',status=no,scrollbars=' + allowScroll + ',resizable=' + allowResize + ',screenX=' + x + ',screenY=' + y + ',top=' + y + ',left=' + x );
	objWin.focus();
}

function showWord( thisID, descLength ) {
	var winHeight = parseInt( ( parseInt( descLength / 77 ) + 1.5 ) * 26 );
	winHeight = ( winHeight < 100 ) ? 145 : winHeight + 35;
	openWin( "glossary_showword.asp?id=" + thisID + "&h=" + winHeight, 500, winHeight, 'no', '', 'no' );
}

/*
FORM Validation
*/
function validateEmail( emailStr ) {

	var emailPat = /^(.+)@(.+)$/;
	var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	var validChars = "\[^\\s" + specialChars + "\]";
	var quotedUser = "(\"[^\"]*\")";
	var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom = validChars + '+';
	var word = "(" + atom + "|" + quotedUser + ")";
	var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$");

	var matchArray = emailStr.match( emailPat );
	if( matchArray == null ) { return false; }

	var user = matchArray[1];
	var domain = matchArray[2];

	if( user.match(userPat) == null )
    	return false;

	var IPArray = domain.match( ipDomainPat );
	if( IPArray != null ) {
		for( var i = 1; i <= 4; i++ ) {
		    if( IPArray[i] > 255 ) { return false; }
		}
    	return true;
	}

	// Domain is symbolic name
	var domainArray = domain.match( domainPat );
	if( domainArray == null ) { return false; }

	var atomPat = new RegExp( atom,"g" );
	var domArr = domain.match( atomPat );
	var len = domArr.length;
	if( domArr[ domArr.length-1 ].length < 2 || domArr[ domArr.length-1 ].length > 4 ) { return false; }
	if( len < 2 ) { return false; }

	// Email address checks out!
	return true;
}


/*
IMAGE Rollovers
*/
var buttons = new Array();

function buttonPrototype( imgHover, imgNormal ) {
	this.Hover = new Image;
	this.Hover.src = imgHover;
	this.Normal = new Image;
	this.Normal.src = imgNormal;
}

function rollImg( thisImg, thisState ) {
	document.images[ thisImg ].src = eval( 'buttons[ \'' + thisImg + '\' ].' + thisState + '.src' );
}


/*
FLASH Detection
*/
function FlashInstalled() {
	var result = false;
	if( navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"] ) {
		result = navigator.mimeTypes[ "application/x-shockwave-flash" ].enabledPlugin;
	}
	else if( document.all && ( navigator.appVersion.indexOf( "Mac" ) == -1 ) )	{
		// IE Windows only -- check for ActiveX control, have to hide code in eval from Netscape (doesn't like try)
		eval( 'try {var xObj = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");if (xObj)	result = true; xObj = null;	} catch (e)	{}' );
	}
	return result;
}


/*
MEDIA VIEWER spawner
*/
function openMediaClip( thisClipID, MediaType, Language ) {
	switch( MediaType.toLowerCase() ) {
		case 'sound':
		case 'audio':
			openWin( 'http://hww.ca/media_sound_viewer.asp?id=' + thisClipID + '&Language=' + Language, 536, 90, 'no', 'yes', 'no' );
			break;			
		
		case 'pdf':
		case 'p':
			openWin( 'http://hww.ca/media_pdf_viewer.asp?id=' + thisClipID + '&Language=' + Language, 800, 600, 'yes', 'yes', 'yes' );
			break;

		case 'video':
			openWin( 'http://hww.ca/media_viewer2.asp?id=' + thisClipID + '&Language=' + Language, 550, 363, 'no', 'yes', 'no' );
			break;
	}
}
