/*
	Description:
		Generic js functions, mostly providing cross browser compatibility

	Public Functions:
		array parseGetVars()
		object fixDiv( object )
		writeHtml( object, string )
		deleteElement( array, string || integer )

	Written by:
		Tim Booker
		tim@saltstonemedia.co.uk

	Version:
		2.0.0
		7th March 2002
*/


if( !document.getElementById ) {
	
	// if getElementById doesn't exist, point to the custom function	
	
	if( typeof( Document ) == 'function' ) {
		
		// add the custom method to every instance of Document
		Document.prototype.getElementById = window.getElById;	
	} else {
		document.getElementById = getElById;	
	}
}

function getElById( n, d ) {

// provides the standard getElementById method for Netscape 4 and IE4
// for Netscape, recurse down the nested layer structure until we find one with the required id
// for IE, simply get the reference using the proprietary document.all

	if( !d ) var d = document;
	if( d.layers ) {
		var x = d[ n ];
		for( var i = 0; !x && i < d.layers.length; i++ ) {
			x = getElById( n, d.layers[ i ].document );
		}
	} else if( d.all ) {
		x = d.all[ n ];
	}
	if( x ) x = fixDiv( x );
	return x;
}


function fixDiv( div ) {
	
// fix non standard property names in a layer

	if( !div.style ) div.style = div;
	if( typeof( div.clip ) == 'object' ) {
		div.offsetWidth  = div.clip.width;
		div.offsetHeight = div.clip.height;
	}

	return div;	
}

if( typeof( loadQueue ) == 'object' ) {
    loadQueue[ loadQueue.length ] = 'fixImagesArray()';
}

function fixImagesArray( d ) {

// solves cross layer image referencing problems in Netscape 4
// loops through the layers in the page
// adds all images to the window.document.images array

	if( !d ) { var d = document; }
	if( d.layers ) {
		for( var i = 0; i < d.images.length; i++ ) {
			if( typeof( d.images[ i ].src == 'string' ) ) {
				window.document.images[ d.images[ i ].name ] = d.images[ i ];
				window.document.images[ i ] = d.images[ i ];
			}
		}
		if( d.layers.length > 0 ) {
			for( var i = 0; i < d.layers.length; i++ ) {
				fixImagesArray( d.layers[ i ].document );
			}
		}
	}
}

function parseGetVars() {

// splits the query string from the url
// returns associative array of HTTP get vars

	var args = new Array();
	var query = window.location.search.substring( 1 )
	if( query ) {
		var strList = query.split( '&' );
		for( str in strList ) {
			var parts = strList[ str ].split( '=' );
			args[ unescape( parts[ 0 ] ) ] = unescape( parts[ 1 ] );
		}
	}
	return args;
}

function writeHtml( div, htm ) {

// cross browser alternative to innerHTML

	if( document.layers ) {
		with( div.document ) {
			write( htm );
			close();
		}
	} else {
	 	div.innerHTML = htm;
	}
}

function deleteElement( array, idx ) {

// deletes an element from an array
// if it's a numberical index, we reduce the following indexes, and trim the array length
// otherwise use the normal delete statement

	if( typeof( idx == 'number' ) ) {
		var length = array.length;
		if( idx >= length || idx < 0 ) { return; }
		for( var i = idx; i < length - 1; i++ ) {
			array[ i ] = array[ i + 1 ];
		}
	  	array.length--;
  	} else {
  		delete array[ idx ];
  	}
}

function getAbsOffset( div, val ) {

	if( document.layers ) {
		pos = eval( 'div.page' + ( val == 'Left' ? 'X' : 'Y' ) );
	} else {
		pos = eval( 'div.offset' + val );
		while( ( div.tagName != 'BODY' ) && ( div.offsetParent.style.position != 'absolute' ) ) {
			div = div.offsetParent;
			pos += eval( 'div.offset' + val );
		}
	}
	return pos;
}
