function TOPASS_refreshImage( id )
{
	if ( id && document[id] ) // ie bug: refresh image
	{
		var image = document[id].src;
		document[id].src = image;
	}
}

var TOPASS_popUp;

function TOPASS_open( url, pars, name, toCenter, button )
{
	if ( typeof( button ) != "undefined" ) TOPASS_refreshImage( button );
	
	function getCenterPos( type, winSize, scrDefault )
	{
		if ( !winSize ) return "";
		
		var scrSize = (screen[type]) ? screen[type] : scrDefault;
		var winPos  = (scrSize - winSize)/2;
		
		return ( type == "width" ) ? ",screenX="+winPos+",left="+winPos : ",screenY="+winPos+",top="+winPos;
	}
	
	var width  = 200; // default values
	var height = 200;
	// get width/height of window
	if ( toCenter )
	{
		// get width/height values
		var pairs = pars.split(",");
		for ( var i in pairs )
		{
			var p = pairs[i].split("=");
			if ( p[0] == "width" ) width = p[1];
			if ( p[0] == "height" ) height = p[1];
		}
	}
	
	if ( toCenter == "right" ) // right aligned
	{
		var scrSize = ( screen.width ) ? screen.width : 800;
		var winPos = scrSize - width;
		pars += ",screenX="+winPos+",left="+winPos;
		pars += ",screenY=0,top=0";
	}
	else if ( toCenter )
	{
		pars += getCenterPos("width",width,800);
		pars += getCenterPos("height",height,600);
	}
	
	TOPASS_popUp = window.open( url, name, pars );
	// check window due to pop-up blocking
	if ( TOPASS_popUp )
	{
		// use little delay to put window in foreground
		setTimeout("TOPASS_popUp.focus()",500);
	}
}

function TOPASS_openWindow( url, name, width, height, toCenter, button )
{
	var pars = "dependent=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes";
	
	if ( width )
	{
		// increase width to avoid scrollbars
		var innerWidth = parseInt( width )+10;
		width = innerWidth + 10;

		pars += ",width="+width+",innerWidth="+innerWidth;
	}
	if ( height )
	{
		pars += ",height="+height+",innerHeight="+height;
	}

	TOPASS_open( url, pars, name, toCenter, button );
}

function TOPASS_close()
{
	if ( opener ) opener.focus();
	window.close();
}

function TOPASS_linkToSelected( formname, fieldname )
{
	var form = document.forms[formname];

	if ( form )
	{
		var obj = form.elements[fieldname];
		var url = obj.options[obj.selectedIndex].value;

		if ( url != "" )
		{
			var urlObj = url.split(",");
			if ( urlObj[1] )
			{
				window.open( urlObj[0], urlObj[1] );
			}
			else
			{
				document.location.href = url;
			}
		}
	}
}

function TOPASS_trim( string ) // remove leading/trailing spaces
{
	var invalid = " \n"; // characters to remove

	// check from start
	for ( var i = 0; i < string.length; i++ )
	{
		var ch = string.charAt( i );
		if ( invalid.indexOf( ch ) < 0 ) // valid char
		{
			string = string.sTOPASString( i );
			break; // stop loop
		}

		if ( i == string.length-1 ) string = ""; // no valid char found
	}

	// check from end
	for ( var i = string.length; i >= 0; i-- )
	{
		var ch = string.charAt( i );
		if ( invalid.indexOf( ch ) < 0 ) // valid char
		{
			string = string.sTOPASString( 0, i+1 );
			break; // stop loop
		}
	}

	return string;
}

function TOPASS_requestStat( link )
{
	if ( !link ) return true;

	var w = window.open( link, "TOPASS_Stats", "width=1,height=1,alwaysLowered=1" );
	w.blur();
	w.close();

	return true;
}

function TOPASS_search( formname, fieldname, isSubmit, button )
{
	TOPASS_refreshImage( button );
	
	var form = document.forms[formname];
	var query = form[fieldname].value;

	if ( isSubmit )
	{
		if ( query != "" || button )
		{
			if ( form["restrictArea"] )
			{
				query = form["restrictArea"].value+":"+form["restrictContent"].value+" || "+query;
			}

			form["qt"].value = query;
			form.submit();
		}
	}

	// no return value if it's a button action
	if ( button ) return;

	return query != "";
}

var TOPASS_oldImage = "";

function TOPASS_imageOn( imageId, newImage )
{
	if ( !document.images ) return;
	TOPASS_oldImage = document.images[ imageId ].src;
	document.images[ imageId ].src = newImage;
}

function TOPASS_imageOff( imageId )
{
	if ( document.images && TOPASS_oldImage )
	{
		document.images[ imageId ].src = TOPASS_oldImage;
		TOPASS_oldImage = "";
	}
}

