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

/*
* Adds given variable 'name' into text of 'field'.
*/
function addReplacement( field, name )
{
	if ( ! IS_MOZ && ! IS_IE ) return;

	var f = document.form[field];
	if ( f ) {
		if ( IS_IE ) {
//			var sel = document.selection.createRange();
//			sel.text = name;
			f.value += name;
		} else {
			if ( f.selectionStart || f.selectionStart == "0" ) {
				var start = f.selectionStart;
				var end = f.selectionEnd;
				var value = f.value;
				f.value = value.substring( 0, start ) + name + value.substring( end, value.length );
			} else {
				f.value += name;
			}
		}
		f.focus();
	}
}

/*
* Append given className at the end of TR.className,
* where at least one TD contains <SPAN class="className">..</SPAN>.
*/
function appendRowClass( className )
{
	if ( ! IS_MOZ && ! IS_IE ) return;

	var obj = document.getElementsByTagName( 'SPAN' );
	if ( obj ) {
		var i = obj.length-1;
		do {
			if ( obj[i] && obj[i].className == className ) {
				var tr = obj[i].parentNode.parentNode;
				if ( tr ) {
					//tr.className = className;
					tr.className = ( tr.className ? ( tr.className + ' highlight' ) : className );
				}
			}
		} while ( --i > -1 );
	}
}

/*
* Adds external script into document HEAD.
*/
function addScript( src )
{
	var script = document.createElement( 'script' );
	script.setAttribute( 'src', src );
	script.setAttribute( 'type', 'text/javascript' );

	document.getElementsByTagName( 'head' ).item( 0 ).appendChild( script );
}

/*
* 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 ));
}

/*
* Reloads window.
*/
function doReloadPage()
{
	window.location = window.location;
}

/*
* 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;
}

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,dependent=yes'

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

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

		if ( win ) win.focus();

		return win;
	}
}

/*
* 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.className.indexOf( 'selected' ) == -1 ) {
		var className = row.className;
		row.className = (
			state == true ?
			( className.indexOf( 'highlight' ) == -1 ? ( className + ' highlight' ) : className ) :
			className.replace( /highlight/, "" )
		);
	}
}

/*
* 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;
}

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

	return true;
}

var shortcut_go;
var sc=false;
var sc_container=false;

function shortcut(url,ur2)
{
	if(!sc){
		width=300;
		height=100;
		left=(screen.width-width)/2;

		sc=Builder.node('div',{'id':'shortcut2'});

		sc_container=Builder.node('div',{
			'id': 'shortcut',
			'style': 'position:absolute; top:150px; width:'+width+'px; left:'+left+
				'px; background:#CCC; border:2px outset #999; display:none; padding:0px 0px 5px 0px;'
		},[sc]);

		document.body.appendChild(sc_container);
	}

	shortcut_close();

	new Ajax.Updater(sc,url,{
		evalScripts:true,onComplete:function(a){
			Element.show(sc_container);
			shortcut_go=ur2;
			$("sc_search_str_str").focus();
		}
	});

}

function sc_open_page(id)
{
	window.location=shortcut_go+id;
}

function shortcut_close()
{
	Element.hide(sc_container);
}

