function UBS_refreshImage( id )
{
	if ( id && document[id] ) // ie bug: refresh image
	{
		var image = document[id].src;
		document[id].src = image;
	}
}

function UBS_open( url, pars, name, toCenter, button )
{
	if ( typeof( button ) != "undefined" ) UBS_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;
	}
	
	if ( toCenter )
	{
		var width = 0;
		var height = 0;
		
		// 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];
		}
		
		pars += getCenterPos("width",width,800);
		pars += getCenterPos("height",height,600);
	}
	
	var w = window.open( url, name, pars );
	w.focus();
}

function UBS_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 ) { width += 6; pars += ",width="+width+",innerWidth="+width; }
	if ( height ) { pars += ",height="+height+",innerHeight="+height; }

	UBS_open( url, pars, name, toCenter, button );
}

function UBS_close()
{
	if ( opener ) opener.focus();
	window.close();
}

function UBS_linkToSelected( formname, fieldname )
{
	var form = document.forms[formname];

	if ( form )
	{
		var obj = form[fieldname];
		var url = obj.options[obj.selectedIndex].value;

		if ( url != "" )
		{
			if ( url.substring(0,11) == "javascript:" )
			{
				var jscall = url.substring(11);
				eval( jscall ); // execute javascript function
			}
			else
			{
				var urlObj = url.split(",");
				if ( urlObj[1] )
				{
					window.open( urlObj[0], urlObj[1] );
				}
				else
				{
					document.location.href = url;
				}
			}
		}
	}
}

function UBS_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.substring( 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.substring( 0, i+1 );
			break; // stop loop
		}
	}

	return string;
}

function UBS_search( formname, fieldname, isSubmit, button )
{
	UBS_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 UBS_oldImage = "";

function UBS_imageOn( imageId, newImage )
{
	if ( !document.images ) return;
	UBS_oldImage = document.images[ imageId ].src;
	document.images[ imageId ].src = newImage;
}

function UBS_imageOff( imageId )
{
	if ( document.images && UBS_oldImage )
	{
		document.images[ imageId ].src = UBS_oldImage;
		UBS_oldImage = "";
	}
}
