
// Determine client capabilities

document.global = new Object();
document.global.browserIsWC3 = document.getElementById ? 1 : 0;
document.global.browserIsIE = document.all ? 1 : 0;
document.global.browserIsNS4 = document.layers ? 1 : 0;
document.global.osIsWindows = ( navigator.appVersion.indexOf( "Windows" ) != -1 ) ? true : false;
document.global.osIsMac = ( navigator.appVersion.indexOf( "Mac" ) != -1 ) ? true : false;

// Common functions

function findElement( inObjectName )
{
	if( document.global.browserIsWC3 )
		return window.document.getElementById( inObjectName );
	
	if( document.global.browserIsIE )
		return window.document.all[ inObjectName ];
	
	if( document.global.browserIsNS4 )
		return window.document.layers[ inObjectName ];
	
	return null;
}

function loadGraphics()
{
	var argumentCount = loadGraphics.arguments.length;
	if( ( argumentCount > 0 ) && document.images )
	{
		document.global.preloadedImages = new Array( argumentCount );
		for( argumentIndex = 0; argumentIndex < argumentCount; argumentIndex++ )
		{
			document.global.preloadedImages[ argumentIndex ] = new Image();
			document.global.preloadedImages[ argumentIndex ].src = loadGraphics.arguments[ argumentIndex ];
		}
	}
}

function rolloverGraphic( inImage, inRolled )
{
	var objectName = inImage.name;
	if( document.images )
	{
		var image = document.images[ objectName ];
		if( image )
		{
			var imageSource = image.src;
			var imageName = imageSource.substr( 0, imageSource.length - 4 );
			var imageExtension = imageSource.substr( imageSource.length - 3, 3 );
			var imageIsOn = ( imageName.substr( imageName.length - 3, 3 ) == "_on" );
			
			if( ( imageIsOn && inRolled ) || ( !imageIsOn && !inRolled ) )
				return;
			
			if( imageIsOn )
				imageName = imageName.substr( 0, imageName.length - 3 );
			
			document.images[ objectName ].src = imageName + ( inRolled ? "_on" : "" ) + "." + imageExtension;
		}
	}
}

function changeGraphic( inName, inNewSource )
{
	document.images[ inName ].src = inNewSource;
}

function changeVisibility( inObject, inVisible )
{
	if( document.global.browserIsNS4 )
		inObject.visibility = ( inVisible == 'visible' ) ? 'show' : 'hide';
	else
		inObject.visibility = inVisible;
}

function fieldPlaceholder( inField, inFocused, inPlaceholder )
{
	if( inFocused )
	{
		if( inField.value == inPlaceholder )
			inField.value = "";
	}
	else
	{
		if( inField.value == "" )
			inField.value = inPlaceholder;
	}
}

function openWindow( inURL, inWidth, inHeight, inResizable, inScrollbars, inToolbar )
{
	var width = Math.ceil( screen.availWidth * ( 60.0 / 100.0 ) );
	var height = Math.ceil( screen.availHeight * ( 80.0 / 100.0 ) );
	var pos_x = 0;
	var pos_y = 0;
	var scrollbars = 1;
	var resizable = 1;
	var toolbar = 0;
	
	if( inWidth != null )
	{
		var widthString = inWidth + "";
		if( widthString.substr( widthString.length - 1, widthString.length ) == "%" )
		{
			width = widthString.substr( 0, widthString.length - 1 );
			width = Math.ceil( screen.availWidth * ( width / 100.0 ) );
		}
		else
		{
			width = inWidth;
		}
	}
	
	if( inHeight != null )
	{
		var heightString = inHeight + "";
		if( heightString.substr( heightString.length - 1, heightString.length ) == "%" )
		{
			height = heightString.substr( 0, heightString.length - 1 );
			height = Math.ceil( screen.availHeight * ( height / 100.0 ) );
		}
		else
		{
			height = inHeight;
		}
	}
	
	pos_x = Math.ceil( ( screen.availWidth - width ) / 2.0 );
	pos_y = Math.ceil( ( screen.availHeight - height ) / 2.0 );
	
	if( inResizable != null )
		resizable = ( inResizable ) ? 1 : 0;
	
	if( inScrollbars != null )
		scrollbars = ( inScrollbars ) ? 1 : 0;
	
	if( inToolbar != null )
		toolbar = ( inToolbar ) ? 1 : 0;
	
	if( document.global.browserIsNS4 )
		window.open( inURL, 'subwin', 'toolbar='+toolbar+',location=0,directories=0,status=0,menubar=0,scrollbars='+scrollbars+',resizable='+resizable+',copyhistory=0,width='+width+',height='+height+',screenX='+pos_x+',screenY='+pos_y+'' );
	else
		newWindow = window.open( inURL, 'subwin', 'toolbar='+toolbar+',location=0,directories=0,status=0,menubar=0,scrollbars='+scrollbars+ ',resizable='+resizable+',copyhistory=0,width='+width+',height='+height+',left='+pos_x+',top='+pos_y+'' );
}

/* Validation functions */

function validateEmailAddress( inEmail )
{
	return ( inEmail != "" && inEmail.indexOf( '@', 1 ) != -1 && inEmail.indexOf( '.', 3 ) != -1 );
}

/* String functions */

function stripInvertedCharacterSetFromString( inCharacterSet, inString )
{
	var result = "";
	
	for( var i = 0; i < inString.length; i++ )
	{
		var c = inString.charAt( i );
		if( inCharacterSet.indexOf( c ) != -1 )
			result += c;
	}
	
	return result;
}

function stripCharacterSetFromString( inCharacterSet, inString )
{
	var result = "";
	
	for( var i = 0; i < inString.length; i++ )
	{
		var c = inString.charAt( i );
		if( inCharacterSet.indexOf( c ) == -1 )
			result += c;
	}
	
	return result;
}

function stripAlphabetFromString( inString )
{
	return stripInvertedCharacterSetFromString( "1234567890", inString );
}

function stripNumbersFromString( inString )
{
	return stripCharacterSetFromString( "1234567890", inString );
}

function stripWhitespaceFromString( inString )
{
	return stripCharacterSetFromString( " \t\r\n", inString );
}