function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	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 closeFrame()
{
    if ( document.body.myIframe && ( document.body.myIframe != null ) )
	{
		document.body.removeChild( document.body.myIframe );
		document.body.onclick = null;
		document.body.myIframe = null;
	}
}

// make a trim function
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };


function makeFrame( width,  height ) { 
	closeFrame();
	ifrm = document.createElement("IFRAME"); 
	ifrm.setAttribute("src", "movie/howto.swf"); 
	document.body.appendChild(ifrm);    
	ifrm.style.width = width+"px"; 
	ifrm.style.height = height+"px";
	ifrm.style.position = "absolute";
	var leftVal = ( f_clientWidth() / 2 ) - ( width / 2 );
	if ( leftVal < 0 ) { leftVal = 0; }
	ifrm.style.left = leftVal;
	var topVal = ( f_clientHeight() / 2 ) - ( height / 2 );
	if ( topVal < 0 ) { topVal = 0; }
	ifrm.style.top = topVal;
	ifrm.style.zIndex = "1";
	ifrm.style.border = "none";
   
	document.body.myIframe = ifrm;
	setTimeout("document.body.onclick = closeFrame;", 1);


	return true;

}


function closeImage()
{
	if ( document.body.myImage && ( document.body.myImage != null ) )
	{
		document.body.removeChild( document.body.myImage );
		document.body.onclick = null;
		document.body.myImage = null;
	}
}

function makeImage( width,  height, src ) { 
	closeImage();
	width += 16;
	height += 16;
    scaleW = 1.0;
    scaleH = 1.0;

    // make sure the image doesn't take up too much of the user's browser
    if ( width > f_clientWidth() * 0.8 )
    {
        scaleW = ( f_clientWidth() * 0.8 ) / width;
    }

    if ( height > f_clientHeight() * 0.8 )
    {
        scaleH = ( f_clientHeight() * 0.8 ) / height;
    }
    
    scale = scaleH < scaleW ? scaleH : scaleW;
    width *= scale;
    height *= scale;


	img = document.createElement("img"); 
	img.setAttribute("src", src); 
	document.body.appendChild(img);    
	img.style.width = width+"px"; 
	img.style.height = height+"px";
	img.style.position = "absolute";
	img.style.left = ( f_clientWidth() / 2 ) - ( width / 2 );
	img.style.top = ( f_clientHeight() / 2 ) - ( height / 2 );
	img.style.zIndex = "1";
	img.style.border = "none";
   
	document.body.myImage = img;
	setTimeout("document.body.onclick = closeImage;", 1);
    if ( document.body.imageGoodFunc ) // used in IE 5.5 and 6 so that the PNGs work nicely
    {
        img.id = "makeImageId";
        document.body.imageGoodFunc( img );
        document.body.myImage = document.getElementById( "makeImageId" );
    }


	return true;

}

function delta( itemPtr, stop, current, direction )
{

	if ( current != stop )
    {
        current += direction;
        itemPtr.style.width = current + 'px';

        itemPtr.currentTimeout = window.setTimeout( function()
                                                    {
                                                        delta( itemPtr, stop, current, direction );
                                                    }, 20 );
    }
    
}
function expand( item )
{

    itemPtr = document.getElementById( item );    
	
	if ( itemPtr.currentTimeout)
    {
        // cancel any pending movings
        clearTimeout( itemPtr.currentTimeout);
    }

    // get the current width
    var current = parseInt( itemPtr.style.width );
    itemPtr.currentVal = current;
    delta( itemPtr, 10, current, 2 );

}


function contract( item )
{
    itemPtr = document.getElementById( item );
    

	if ( itemPtr.currentTimeout)
    {
        clearTimeout( itemPtr.currentTimeout);
    }

    var current = parseInt( itemPtr.style.width );
    itemPtr.currentVal = current;
    delta( itemPtr, 0, current,  -2 );
}

defaultBoingVel = 5.0;
defaultBoingGrav = -1.0;

function updateBoing( thing )
{

    thing.boingPos += thing.boingVel;
    thing.boingVel += defaultBoingGrav;

    
    if ( thing.boingPos <= 0 )
    {
        if ( thing.boinging == false )
        {
            // we don't want to boing anymore
            window.clearInterval( thing.interval );
            thing.interval = null;
        }
        thing.style.top = "0px";
        thing.boingPos = 0;
        thing.boingVel = defaultBoingVel;
    }
    else
    {
        thing.style.top = -thing.boingPos + "px";        
    }

    
}


function boing( thingName )
{
    var thing = document.getElementById( thingName );
    thing.boinging = true; // mark it as boinging

    if ( thing.interval ) return; // if we're already actually bouncing, we're good


    thing.boingPos = 0.0;
    thing.boingVel = defaultBoingVel;

    thing.style.top = "0px";
    thing.interval = window.setInterval( function() 
                                       {
                                           updateBoing( thing );
                                       }, 25 );

    
}


function stopBoing( thingName )
{
    var thing = document.getElementById( thingName );
    thing.boinging = false;
    
}


function checkField( name, focusObj )
{
    var field = document.getElementById( name );
    var errorMsg = document.getElementById( name + "Error" );
    if  ( field.value.trim() == "" )
    {

        if ( errorMsg ) errorMsg.style.display = "block";
        if ( focusObj == null )
        {
            focusObj = field;
        }
    } 
    else
    {
        if ( errorMsg ) errorMsg.style.display = "none";
    }


    return focusObj;
}
