// General Purpose JS library

function preloadImages()
{ //v3.0
	var d = document;
	
	if( d.images )
	{
		if( !d.preloadedImages )
		{
			d.preloadedImages = new Array();
		}
		
		var i,j = d.preloadedImages.length,a = arguments;
		
		for( i=0; i < a.length; i++ )
		{
			if( a[i].indexOf("#") != 0 )
			{
				d.preloadedImages[j]=new Image;
				d.preloadedImages[j++].src=a[i];
			}
		}
	}
}

function externalWrite( str )
{
	document.write( str );
}

function getElement( id, d )
{
	var x = null;
	d = d ? d : document;
	
	if( d.getElementById )
	{
		x = d.getElementById( id );
	}
	else if( !( x = d[id] ) && d.all )
	{
		x = d.all[id];
	}
	
	return x;
}

function delegate( obj, func )
{
	return function()
	{
		func.apply( obj );
	}
}

//used to set display of an element
//t = display type, all other arguments are what to change - either ref to element, or string of element id
function display( t )
{
	t = t ? t : '' ;
	var c;
	
	for( var i = 1; i < arguments.length; ++i )
	{
		c = arguments[i];
		
		if( typeof( c ) == 'string' )
		{
			c = getElement( c );
		}
		
		if( c )
		{
			if( c.style )
			{
				try
				{
					c.style.display = 'none';
				}
				
				catch( e )
				{
					logError( 3, 'unable to set style' );
				}
			}
		}
	}
}

//1 = info, 2 = notification, 3 = error, 4 = fatal error
function logError( errorLvl, errorMsg )
{
	if( errorLvl > 3 )
	{
		alert( errorMsg );
	}
}

function convertCharacter( str )
{
	if( str )
	{
		var myRegEx = new RegExp( String.fromCharCode( 8216 ) + "|" + String.fromCharCode( 8217 ), "g" );
		str = str.replace( myRegEx, "'" );
		
		var myRegEx = new RegExp( String.fromCharCode( 8220 ) + "|" + String.fromCharCode( 8221 ), "g" );
		str = str.replace( myRegEx, '"' );
	
		var myRegEx = new RegExp( String.fromCharCode( 61558 ) + "|" + String.fromCharCode( 8226 ) + "|" + String.fromCharCode( 61607 ), "g" );
		//str = str.replace( myRegEx, String.fromCharCode( 111 ) );
	
		var myRegEx = new RegExp( String.fromCharCode( 8211 ), "g" );
		str = str.replace( myRegEx, "-" );
	
		var myRegEx = new RegExp( String.fromCharCode( 8230 ), "g" );
		str = str.replace( myRegEx, "..." );
	
		var myRegEx = new RegExp( String.fromCharCode( 8482 ), "g" );
		str = str.replace( myRegEx, "TM" );
	
		var myRegEx = new RegExp( String.fromCharCode( 169 ), "g" );
		str = str.replace( myRegEx, "(c)" );
	
		var myRegEx = new RegExp( String.fromCharCode( 174 ), "g" );
		str = str.replace( myRegEx, "(r)" );
	
		var myRegEx = new RegExp( String.fromCharCode( 187 ), "g" );
		str = str.replace( myRegEx, ">>" );
		
		var myRegEx = new RegExp( String.fromCharCode( 171 ), "g" );
		str = str.replace( myRegEx, "<<" );
		
		/*try
		{
			console.log( 'replacing string: '+str.length );
		}
		
		catch( e )
		{
			
		}*/
		
		return str;
	}
	else
	{
		try
		{
			console.log( 'no string supplied' );
		}
		
		catch( e )
		{
			
		}
	}
}