<!--

/**
* Common functions.
*
* 'browser.js' should be included already.
*
* @package Api
* @subpackage JavaScript
*/

/*
* Converts given input to default number format by replacing
* given decimal separator to '.' and thousands separator to ''.
*/
function defaultNumberFormat( input, dec_sep, tho_sep )
{
	var dec_def = '.';
	var tho_def = '';
	var out = '';

	out = input.replace( eval( '/' + tho_sep + '/g' ), tho_def );
	out = out.replace( eval( '/' + dec_sep + '/g' ), dec_def );

	return ( isNaN( out ) ? '' : parseFloat( out ));
}

/*
* Formats given input according to given decimal places,
* decimal separator and thousands separator.
* It assumes, that input is in default number format.
*/
function numberFormat( input, dec_places, dec_sep, tho_sep )
{
	var dec_def = '.';
	var dec_def_re = '\\.';
	var dec_append = '0';
	var tho_def = '';
	var out = '';

	// round to decimal places
	var p = Math.pow( 10, dec_places );
	var tmp1 = input * p;
	tmp1 = ( tmp1 >= 0 ? Math.floor( tmp1 + 0.5 ) : Math.ceil( tmp1 - 0.5 ));
	tmp1 /= p;

	if ( ! isNaN( tmp1 )) {

		// append zeros
		var tmp1 = tmp1 + '';
		var dec_point = tmp1.indexOf( dec_def );
		var append = 0;
		if ( dec_point == -1 ) {
			append = dec_places;
			tmp1 += dec_def;
		} else {
			append = dec_places - tmp1.substr( dec_point+1 ).length;
		}
		if ( append ) {
			do {
				tmp1 += dec_append;
			} while ( --append );
		}

		// apply thousands separator
		if ( tho_sep.length > 0 && parseFloat( tmp1 ) >= 1000.0 ) {
			var tmp2 = tmp1.slice( 0, -( dec_places + 1 ));
			var l = tmp2.length-1;
			var i = l;
			do {
				out = tmp2.substr( i, 1 ) + out;
				if (( l-i ) < l && (( l-i+1 ) % 3 ) == 0 ) {
					out = tho_sep + out;
				}
			} while ( --i > -1 );
			out += tmp1.slice( -( dec_places + 1 ));
		} else {
			out = tmp1;
		}

		// apply decimal separator
		out = out.replace( eval( '/' + dec_def_re + '/' ), dec_sep );
	}

	return out;
}

/*
* Shows text in statusbar of window.
*/
function statusText( txt )
{
	window.status = txt;

	return true;
}

/*
* Sets source of image.
*/
function setImg( img, newsrc )
{
	document.images[img].src = newsrc;
}

/*
* Sets HTML of some object.
*/
function setHTML( targetframe, obj_id, html )
{
	if ( ! IS_MOZ && ! IS_IE ) return;
	if ( ! targetframe ) return;

	var obj = targetframe.document.getElementById( obj_id );
	if ( ! obj ) return;

	obj.innerHTML = html;
}

function addScript( src )
{
	var script = document.createElement('script');
	script.setAttribute('src', src );
	script.setAttribute('language','Javascript');
	script.setAttribute('type','text/javascript');
	document.getElementsByTagName('head').item(0).appendChild(script);
}

function doReloadPage()
{
	window.location = window.location;
}

function execRPC( url )
{
	addScript( url );
}

function toggle_div ( id )
{
	var obj = document.getElementById ( id );
	if ( !obj ) return;

	obj.style.visibility = ( obj.style.visibility == 'visible' ) ? 'hidden' : 'visible';
	obj.style.display = ( obj.style.display == 'block' ) ? 'none' : 'block';
}

/*
* Highlights/unhighlights cells of given table row.
*/
function setHighlight( row, state )
{
	if (( ! IS_MOZ && ! IS_IE ) || ! row ) return;

	// don't do anything if given row is selected already
	if ( row.cells[0].className.indexOf( 'selected' ) == -1 ) {

		// go through cells of row
		for ( var i = 0; i < row.cells.length; i++ ) {
			var className = row.cells[i].className;

			// highlight/unhighlight cell
			row.cells[i].className = (
				state == true ?
				( className.indexOf( 'highlight' ) == -1 ? ( className + ' highlight' ) : className ) :
				className.replace( / highlight/, "" )
			);
		}
	}
}

if ( typeof openPopup == "undefined") {
	/*
	* Opens popup window in center of the screen.
	*/
	function openPopup( url, name, width, height, prop )
	{
		var left = Math.floor( screen.availWidth / 2 ) - Math.floor( width / 2 );
		var top = Math.floor( screen.availHeight / 2 ) - Math.floor( height / 2 );
		if ( ! prop ) prop = 'location=no,resizable=yes,menubar=no,status=yes,scrollbars=yes'

		if ( typeof SID != "undefined" ) {
			url = url + (( url.indexOf ('?') > -1 ) ? '&' : '?') + SID;
		}

		var win = window.open( url, name, 'width=' + width + ',height=' + height + ',left=' + left + ',top=' + top + ',' + prop );

		if ( win ) win.focus();

		return win;
	}
}

//-->

