/**
 * =========================
 *  DHTML UTILITY FUNCTIONS
 *    (c) Zero One - 2003
 *   Author: Gary Jacobson
 * http://www.zero-one.co.za
 * =========================
 */

/**
 * Set a property of a div (layer). This can also be used to set properties of any HTML
 * element with an id, but this will break in Netscape 4.
 */
function setDivProperty( divId, property, value )
{
	if (document.getElementById)
		eval( "document.getElementById( divId ).style." + property + "='" + value + "';" );
	else if (document.all)
		eval( "document.all." + divId + ".style." + property + "='" + value + "';" );
	else if (document.layers)
		eval( "getLayer( divId )." + property + "='" + value + "';" );
}

/**
 * Set the src of a specified image.
 * E.g. setImageSrc( 'myImg', '/images/x.gif' )
 */
function setImageSrc( imgName, src )
{
	getImage( imgName ).src = src;
}

/**
 * Get the x coordinate of a specified Image.
 * E.g. getImageX( 'myImg' )
 */
function getImageX( imgName )
{
	return getImage( imgName, 'x' );
}

/**
 * Get the y coordinate of a specified Image.
 * E.g. getImageY( 'myImg' )
 */
function getImageY( imgName )
{
	return getImage( imgName, 'y' );
}

/**
 * Utility function to get an Image object, used by setImageSrc etc.
 * E.g. getImage( 'myImg' )
 */
function getImage( imgName, getPart, doc )
{
	if (doc == null)
		doc = document;
	var img = doc.images[ imgName ];
	if (img != null)
	{
		if (getPart == null)
			return img;
		if (getPart == 'x')
			return getPageX( img );
		if (getPart == 'y')
			return getPageY( img );
	}
	if (doc.layers)
		for (var i = 0; i < doc.layers.length; i++)
		{
			img = getImage( imgName, getPart, doc.layers[i].document );
			if (img != null)
			{
				if (getPart == null)
					return img;
				if (getPart == 'x')
					return img + doc.layers[i].x;
				if (getPart == 'y')
					return img + doc.layers[i].y;
			}
		}
	if (doc == document)
		alert( 'Error: Could not find image ' + imgName );
	return null;
}

/**
 * Utility function to get a Layer object, used by setDivProperty.
 * E.g. getLayer( 'myDiv' )
 */
function getLayer( layerName, doc )
{
	// check for non-NS browsers
	if (document.getElementById)
		return document.getElementById( layerName );
	if (document.all)
		return document.all[ layerName ];

	// continue with NS stuff
	if (doc == null)
		doc = document;
	var layer = doc.layers[ layerName ];
	if (layer != null)
		return layer;
	for (var i = 0; i < doc.layers.length; i++)
	{
		layer = getLayer( layerName, doc.layers[i].document );
		if (layer != null)
			return layer;
	}
	if (doc == document)
		alert( 'Error: Could not find layer ' + layerName );
	return null;
}

/**
 * Utility function to get the x coordinate of an object, used by getImageX.
 * E.g. getPageX( getImage( 'myImg' ) )
 */
function getPageX( obj )
{
	if (document.layers)
	{
		var x = obj.x;
		if (obj.parentLayer)
			while ((obj = obj.parentLayer) != window)
				x += obj.x;
		return x;
	}
	var x = obj.offsetLeft;
	while ((obj = obj.offsetParent) != null)
		x += obj.offsetLeft;
	return x;
}

/**
 * Utility function to get the y coordinate of an object, used by getImageY.
 * E.g. getPageY( getImage( 'myImg' ) )
 */
function getPageY( obj )
{
	if (document.layers)
	{
		var y = obj.y;
		if (obj.parentLayer)
			while ((obj = obj.parentLayer) != window)
				y += obj.y;
		return y;
	}
	var y = obj.offsetTop;
	while((obj = obj.offsetParent) != null)
		y += obj.offsetTop;
	return y;
}

/**
 * Utility function to get the width of an object.
 * E.g. getWidth( getLayer( 'myDiv' ) )
 */
function getWidth( obj )
{
	if (document.layers)
	{
		if (obj.clip)
			return obj.clip.width;
		return obj.width;
	}
	return obj.offsetWidth;
}

/**
 * Utility function to get the height of an object.
 * E.g. getHeight( getLayer( 'myDiv' ) )
 */
function getHeight( obj )
{
	if (document.layers)
	{
		if (obj.clip)
			return obj.clip.height;
		return obj.height;
	}
	return obj.offsetHeight;
}

/**
 * docWrite and docFlush are used for more efficient document writing.
 * E.g. docWrite( '<br>' ); docWrite( '<br>' ); docFlush();
 */
var docBuffer = '';

function docWrite( text )
{
	docBuffer += text;
}

function docFlush()
{
	if (false)	// debug
	{
		docBuffer = docBuffer.replace( /&/g, '&amp;' );
		docBuffer = docBuffer.replace( /</g, '&lt;' );
		docBuffer = docBuffer.replace( />/g, '&gt;' ) + '<br>';
	}
	document.write( docBuffer );
	docBuffer = '';
}

/**
 * Escapes all single quotes in the string
 */
function formatString( text )
{
	return text.replace( /'/g, '\\\'' );
}

