var ie = (document.all) ? true:false;
var w3c = document.getElementById ? true:false;
var ver = navigator.appVersion;
var mac = ver.indexOf("Mac") > 0;
var win = ver.indexOf("Win") > 0;
var vnum = ie ? parseFloat(ver.substring(ver.lastIndexOf('MSIE ') + 5)) : parseFloat(ver);
var dynamic = (w3c || (ie && vnum >= 4));
var gCell = 0;

function doBlur()				{if(gCell){mOut(gCell); gCell = 0;}}
function GetObjStyle( objName )	
{	
	var obj = 0;
	if( document.all )
		obj = document.all[objName];
	else if( document.getElementById )
		obj = document.getElementById(objName);
	
	if( obj && obj.style )
		return obj.style
	else
		return 0;
}

function mOvr(bn,theLnk) 
{
	var lnkTitle = '';
	
	if( bn == 1 )		lnkTitle = "Home Page of the NSW Union of Rowers web site.";
	else if( bn == 2 )	lnkTitle = "The goals & objectives of the NSW Union of Rowers.";
	else if( bn == 3 )	lnkTitle = "The history of the NSW Union of Rowers.";
	else if( bn == 4 )	lnkTitle = "The constitution of the NSW Union of Rowers.";
	else if( bn == 5 )	lnkTitle = "Information & application form for joining the NSW Union of Rowers.";
	else if( bn == 6 )	lnkTitle = "Youth development scholarships awarded by the NSW Union of Rowers.";
	else if( bn == 7 )	lnkTitle = "Merchanise for purchase from the NSW Union of Rowers.";
	else if( bn == 8 )	lnkTitle = "'Come Forward' newsletters.";
	else if( bn == 9 )	lnkTitle = "List of Rowing Clubs in NSW.";
	else if( bn == 10 )	lnkTitle = "List of Rowing Schools in NSW.";
	else if( bn == 11 )	lnkTitle = "Rowing Information - description of the various boat classes.";
	else if( bn == 12 )	lnkTitle = "Rowing Information - glossary of rowing terms.";
	else if( bn == 13 )	lnkTitle = "Contact details & the executive of the NSW Union of Rowers.";
	else if( bn == 14 )	lnkTitle = "Links to other relevant rowing web sites.";	
		
	if( dynamic )
		{		
		var cellStyl = GetObjStyle(theLnk.id.substring(0,4) + 'CELL');
		if( cellStyl ) cellStyl.background = '#000000';
		var fontStyl = GetObjStyle(theLnk.id.substring(0,4) + 'FNT');
		if( fontStyl ) fontStyl.color = '#ffff00';		
		var lnkId = theLnk.id.substring(0,4);
		window.status = (w3c ? document.getElementById(lnkId).href : eval(lnkId + ".href"));

		theLnk.title = lnkTitle;
		}
	
	return true;
}

function mOut(theLnk) 
{	
	if( dynamic )
		{
		window.status = "";
		var cellStyl = GetObjStyle(theLnk.id.substring(0,4) + 'CELL');
		if( cellStyl ) cellStyl.background = '#af0000';
		var fontStyl = GetObjStyle(theLnk.id.substring(0,4) + 'FNT');
		if( fontStyl ) fontStyl.color = '#ffffcc';
		}
	
	return true;
}

function mClk(theCell)
{
	gCell = theCell;
	
	if( dynamic )
		{
		var objName = theCell.id.substring(0,4);
		theLnk = (w3c ? document.getElementById(objName) : eval(objName));
		self.location = theLnk.href;
		}
}

function WriteEmail(title)
{
	var addr1 = title;
	addr1 += "\@";
	addr1 += "nswrowers.com";
	
	document.write( '<A HREF="mailto:' + addr1 + '?subject=NSW Union of Rowers">' + addr1 + '\</A>' );
}

function WriteEAddress(addr1,addr2)
{
	if( addr2 == '' )
		addr2 = "nswrowers_com";
	
	addr2 = addr2.replace(/_/g, '.');
	document.write( '<A HREF="mai' );
	document.write( 'lto:' );
	document.write( addr1 );
	document.write( '&#64');
	document.write( addr2 );	
	document.write( '">' );
	document.write( addr1 );
	document.write( "&#64");
	document.write( addr2 );
	document.write( '<\/A>' );
}

//global image variables
var newImg = new Image();
var newImgEvt;
var newImgUrl;
var newImgDiv;

function ShowImage( e, imgUrl )
{
	newImgEvt = e;
	newImgUrl = imgUrl;
	newImgDiv = document.getElementById("Preview");
	if( newImgDiv )
		{
		//load new image
		newImg.src = imgUrl;
		
		//if its not already loaded then defer display
		if( !newImg.width ) 
			newImg.onload = DisplayImage;
		else
			DisplayImage();
		}
}

function DisplayImage()
{
	if( newImg && newImgDiv )
		{
		var imgTitle = newImgUrl.replace(/.*\//,'');
		
		var imgH = newImg.height;
		var imgW = newImg.width;
		
		var txt = '<TABLE BORDER="1" CELLSPACING="0" BGCOLOR="#ffffff"><COL WIDTH="' + imgW +'"><COL WIDTH="18"><TR><TD ALIGN="center" BGCOLOR="#0060a9" STYLE="color:#ffffff; cursor:default;" ID="titlebar" onmousedown="DragWindowStart(event);">Click photo to close, Right-click to save<\/TD>';
			txt += '<TD WIDTH="18" ALIGN="center" BGCOLOR="#ff0000" STYLE="width:18px;" onClick="HideObject(\'Preview\');"><SPAN style="border: 0px solid grey; padding-left: 2px; padding-right: 2px; color:#ffffff;" onMouseOver="this.style.color=\'#ffff00\'; this.style.cursor=\'pointer\';" onMouseOut="this.style.color=\'#ffffff\';"><B>X<\/B><\/SPAN><\/TD><\/TR>';
			txt += '<TR><TD COLSPAN="2"><IMG SRC="' + newImgUrl + '"border="0" alt="' + imgTitle + '" STYLE="margin:10px;" onClick="HideObject(\'Preview\');"><\/TD><\/TR>';
			txt += '<TR><TD ALIGN="CENTER" COLSPAN="2">' + imgTitle + '<\/TD><\/TR><\/TABLE>';
		
		newImgDiv.innerHTML = txt;
				
		var winPos = GetMouseWindowPos(newImgEvt);
		var left = winPos[0] - GetScrollLeft();
		var top = winPos[1] - GetScrollTop();
	
		//alert(top + ':' + winH + ':' + GetScrollTop());
		
		var winW = GetClientWidth();
		var winH = GetClientHeight();
			
		if( winW && left + imgW > winW )
			left = winW - imgW;
			
		if( winH && top + imgH > winH )
			top = winH - imgH;
					
		newImgDiv.style.left = left + GetScrollLeft() - 40;
		newImgDiv.style.top = top + GetScrollTop() - 80;
			
		ShowObject( "Preview" );
		}
}

//-----------------------------------------------------------------------------
// Window dragging.
//-----------------------------------------------------------------------------

var inDragWindow = false;
var dragTarget = null;
var dragXOffset = 0;
var dragYOffset = 0;

function DragWindowStart(event)
{
	var x, y;

	if( !ie4 && !w3c ) return;
	
	dragTarget = document.getElementById("Preview");

	//Get cursor offset from window frame.
	if( ie4 ) 
		{
		x = window.event.x;
		y = window.event.y;
		}
	if( w3c )
		{
    	x = event.pageX;
    	y = event.pageY;
		}
 	dragXOffset = dragTarget.offsetLeft - x;
 	dragYOffset = dragTarget.offsetTop  - y;

	//Set document to capture mousemove and mouseup events.
	document.onmousemove = DragWindowDrag;
    document.onmouseup   = DragWindowStop;

  	inDragWindow = true;
}

function DragWindowDrag(event)
{
	var x, y;

	if( !inDragWindow ) return;

  	//Get cursor position.	
	if( w3c )
		{
		x = event.pageX;
		y = event.pageY;
    	event.preventDefault();
    	}
	if( ie4 )
		{
		x = window.event.x;
		y = window.event.y;
		window.event.cancelBubble = true;
    	window.event.returnValue = false;
		}

	//Move window frame based on offset from cursor.
	dragTarget.style.left = x + dragXOffset + "px";
	dragTarget.style.top  = y + dragYOffset + "px";
}

function DragWindowStop(event)
{
	inDragWindow = false;

	//Remove mousemove and mouseup event captures on document.
	document.onmousemove = null;
	document.onmouseup   = null;
}

//-----------------------------------------------------------------------------
// http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html
//-----------------------------------------------------------------------------

function GetClientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function GetClientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function GetScrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function GetScrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}


//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------

function MoveObjectTo(objName,x,y)
{
	var obj = document.getElementById(objName);
	if( obj )
		{
		obj.style.left = x;
		obj.style.top = y;
		}
}

function FindObjectPosition(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent) 
		{
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent)
			{
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
			}
		}
	return [curleft,curtop];
}

function GetScreenSize()
{
  var myWidth = 0, myHeight = 0;
  if( screen.availWidth ) 
  	{
    myWidth = screen.availWidth;
    myHeight = screen.availHeight;
  	} 
  else if( screen.width ) 
  	{
    myWidth = screen.width;
    myHeight = screen.height;
  	} 
  return [myWidth,myHeight];
}

function GetWindowSize()
{
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) 
  	{
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  	} 
  else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
  	{
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  	} 
  else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
  	{
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  	}
  return [myWidth,myHeight];
}

function GetMouseScreenPos(e) 
{
	var posx = 0;
	var posy = 0;
	if(!e) var e = window.event;
	if(e.screenX || e.screenY) 	
		{
		posx = e.screenX;
		posy = e.screenY;
		}

	return [posx,posy];
}

function GetMouseWindowPos(e) 
{
	var posx = 0;
	var posy = 0;
	if(!e) var e = window.event;
	if(e.pageX || e.pageY) 	
		{
		posx = e.pageX;
		posy = e.pageY;
		}
	else if(e.clientX || e.clientY)
		{
		posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
	// posx and posy contain the mouse position relative to the document
	return [posx,posy];
}

//-----------------------------------------------------------------------------
// 
//-----------------------------------------------------------------------------

function XML_Request( requestUrl,formContents ) 
{
	var xmlRequest = null;
	var xmlResponse = 'ERROR: xml request not supported';

	if( window.XMLHttpRequest ) 
		{
		try		{xmlRequest = new XMLHttpRequest();}
		catch(e){xmlRequest = false;}
		} 
	else if( window.ActiveXObject ) 
		{
		try		 {xmlRequest = new ActiveXObject("Msxml2.XMLHTTP");}
		catch(e) {xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");}
		}
	if( xmlRequest ) 
		{
		if( formContents && formContents.length )
			{
			xmlRequest.open("POST", requestUrl, false);
			xmlRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    		xmlRequest.setRequestHeader("Content-length", formContents.length);
    		xmlRequest.send(formContents);
			}
		else
			{
			xmlRequest.open("GET", requestUrl, false);	//false -> synchronous (wait for completion)
			xmlRequest.send(null);
			}
			
		if( xmlRequest.status == 200 ) 
			xmlResponse = xmlRequest.responseText;
		else
			xmlResponse = (xmlRequest.statusText ? "ERROR: " + xmlRequest.statusText : 'Unknown Error');
		}		
		
	return xmlResponse;
}

//-----------------------------------------------------------------------------
// Detect whether PDF plugin is available
//-----------------------------------------------------------------------------

// initialize global variables
var detectableWithVB = false;
var pluginFound = false;

//Browser detection (using IE conditional compilation)
var UNDEF = "undefined";
var ua = new Array();
ua.w3cdom = typeof document.getElementById != UNDEF && typeof document.getElementsByTagName != UNDEF && typeof document.createElement != UNDEF && typeof document.appendChild != UNDEF && typeof document.replaceChild != UNDEF && typeof document.removeChild != UNDEF && typeof document.cloneNode != UNDEF;
ua.u = navigator.userAgent.toLowerCase();
ua.p = navigator.platform.toLowerCase();
ua.webkit = /webkit/.test(ua.u) ? parseFloat(ua.u.replace(/^.*webkit\/(\d+(\.\d+)?).*$/, "$1")) : false; // returns either the webkit version or false if not webkit
ua.ie = false;
ua.windows = ua.p ? /win/.test(ua.p) : /win/.test(ua.u);
ua.mac = ua.p ? /mac/.test(ua.p) : /mac/.test(ua.u);
/*@cc_on
	ua.ie = true;
	@if (@_win32)
		ua.windows = true;
	@elif (@_mac)
		ua.mac = true;
	@end
@*/

function DetectPDF(redirectURL) 
{
	//redirect if we can't detect
	if( !canDetectPlugins() )
		{
		pluginNotAvailable(redirectURL);
		return false;
		}

	pluginFound = detectPlugin('Adobe','Acrobat');
	if( !pluginFound )
		pluginFound = detectPlugin('PDF');
	// if not found, try to detect with VisualBasic
	if( !pluginFound && detectableWithVB )
		{
		var vers = 1;
		while( !pluginFound && vers <= 6 )
			{
			if(!pluginFound )	pluginFound = detectActiveXControl('PDF.PdfCtrl.' + vers);
			//v7.0 && 8.0
			if(!pluginFound )	pluginFound = detectActiveXControl('AcroPDF.PDF.' + vers);
			vers ++;
			}
		}
	
	if( !pluginFound )
		pluginNotAvailable( redirectURL );

	return pluginFound;
}

function ResizePDF()
{
	//var embPDF = document.getElementById( ua.ie ? 'pdfobject' : 'pdfembed');
	var embPDF = document.getElementById('pdfembed');
	if( embPDF )
		{
		embPDF.style.width = GetClientWidth() - 230;
		embPDF.style.height = GetClientHeight() - 170;
		embPDF.style.zIndex = 0;
		}
}

function pluginNotAvailable( redirectURL )
{
	document.write('<small class="noprint">This page is best viewed with the Adobe Reader PDF plugin installed.');
	document.write('<br><a href="http://www.adobe.com/products/acrobat/readstep2.html">Adobe Reader</a> may be downloaded from the Adobe website.');
	document.write('<br>If the PDF is not visible below, use the link above to open or download it.<p></small>');
	//if( ua.webkit )
	//	document.write('<small><b>Safari deos not support viewing PDFs within the page.</b> Please use the link above.<p></small>');
	
	if( redirectURL )
		{
		var fileName = redirectURL;
		if( redirectURL.search( /\/([^\/]*)\.pdf$/i ) != -1 )
			fileName = RegExp.$1;
		
		fileName = fileName.replace( /%20/g, ' ' );
		
		document.write('<BR>Download File: <A HREF="' + redirectURL + '"><IMG SRC="/images/icon_pdf.gif" BORDER=0>' + fileName + '</A><P>' );
		}
}

function canDetectPlugins() {
	if( detectableWithVB || (navigator.plugins && navigator.plugins.length > 0) ) {
		return true;
	} else {
		return false;
	}
}

function detectPlugin() {
	// allow for multiple checks in a single pass
	var daPlugins = detectPlugin.arguments;
	// consider pluginFound to be false until proven true
	var pluginFound = false;
	// if plugins array is there and not fake
	if (navigator.plugins && navigator.plugins.length > 0) {
		var pluginsArrayLength = navigator.plugins.length;
		// for each plugin...
		for (pluginsArrayCounter=0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++ ) {
			// loop through all desired names and check each against the current plugin name
			var numFound = 0;
			for(namesCounter=0; namesCounter < daPlugins.length; namesCounter++) {
				// if desired plugin name is found in either plugin name or description
				if( (navigator.plugins[pluginsArrayCounter].name.indexOf(daPlugins[namesCounter]) >= 0) || 
					(navigator.plugins[pluginsArrayCounter].description.indexOf(daPlugins[namesCounter]) >= 0) ) {
					// this name was found
					numFound++;
				}   
			}
			// now that we have checked all the required names against this one plugin,
			// if the number we found matches the total number provided then we were successful
			if(numFound == daPlugins.length) {
				pluginFound = true;
				// if we've found the plugin, we can stop looking through at the rest of the plugins
				break;
			}
		}
	}
	return pluginFound;
} // detectPlugin


// Here we write out the VBScript block for MSIE Windows
if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1)) {
	document.writeln('<script language="VBscript">');

	document.writeln('\'do a one-time test for a version of VBScript that can handle this code');
	document.writeln('detectableWithVB = False');
	document.writeln('If ScriptEngineMajorVersion >= 2 then');
	document.writeln('  detectableWithVB = True');
	document.writeln('End If');

	document.writeln('\'this next function will detect most plugins');
	document.writeln('Function detectActiveXControl(activeXControlName)');
	document.writeln('  on error resume next');
	document.writeln('  detectActiveXControl = False');
	document.writeln('  If detectableWithVB Then');
	document.writeln('     detectActiveXControl = IsObject(CreateObject(activeXControlName))');
	document.writeln('  End If');
	document.writeln('End Function');

	document.writeln('\'and the following function handles QuickTime');
	document.writeln('Function detectQuickTimeActiveXControl()');
	document.writeln('  on error resume next');
	document.writeln('  detectQuickTimeActiveXControl = False');
	document.writeln('  If detectableWithVB Then');
	document.writeln('    detectQuickTimeActiveXControl = False');
	document.writeln('    hasQuickTimeChecker = false');
	document.writeln('    Set hasQuickTimeChecker = CreateObject("QuickTimeCheckObject.QuickTimeCheck.1")');
	document.writeln('    If IsObject(hasQuickTimeChecker) Then');
	document.writeln('      If hasQuickTimeChecker.IsQuickTimeAvailable(0) Then ');
	document.writeln('        detectQuickTimeActiveXControl = True');
	document.writeln('      End If');
	document.writeln('    End If');
	document.writeln('  End If');
	document.writeln('End Function');

	document.writeln('</scr' + 'ipt>');
}
