
function executeAction( name, args )
{
	_ajxExecuteAction( name, args );
};

function executeActionSendAll( name )
{
	flushEditors();
	_ajxExecuteAction( name, xajax.getFormValues( { tagName : 'FORM', elements : jQuery( 'body :input' ).get() } ) ); // need this to trick xajax into thinking that it is form node
};

function executeGearSendAll( name, args )
{
	flushEditors();
	_ajxExecuteGear( name, xajax.getFormValues( { tagName : 'FORM', elements : jQuery( 'body :input' ).get() } ), args ); // need this to trick xajax into thinking that it is form node
};

function refreshGear( name, filter )
{
	_ajxRefreshGear( name, filter );
};

function onRefreshGear( name, data )
{
	var ctl = jQuery( '#' + name );
	ctl.html( data.body );
	var headcnt = jQuery( data.head );
	var styles = data.style.split( ';' );
	for( var i = 0; i < styles.length; i++ )
	{
		var parts = styles[ i ].split( ':' );
		ctl.css( parts[ 0 ], parts[ 1 ] );
	}
	var head = jQuery( 'head' );
	for( var i = 0; i < headcnt.length; i++ )
	{
		var headel = headcnt.eq( i );
		switch( headel[ 0 ].nodeName.toLowerCase() )
		{
			case 'link':
				if( jQuery( 'link[href=' + headel.attr( 'href' ) + ']', head ).length == 0 )
				{
					head.append( headel );
				}
			break;

			case 'script':
				if( jQuery( 'script[src=' + headel.attr( 'src' ) + ']', head ).length == 0 )
				{
					head.append( headel );
				}
			break;

			case 'style':
				if( jQuery( 'style#' + headel.attr( 'id' ), head ).length == 0 )
				{
					head.append( headel );
				}
			break;
		}
	}
};

function refreshChart( name )
{
	var now = new Date().getTime() / 1000;
	var auxAnchor = document.createElement('a');
	auxAnchor.href = _(name).src;
	var new_chart_source = auxAnchor.protocol + '//' + auxAnchor.hostname + auxAnchor.pathname + '?' + now;
	_(name).src = new_chart_source;
};

function refreshCaptcha( name )
{
	var dt = new Date();
	jQuery('#kcptch_'+name).attr('src', '/kcptch/register?'+dt.getTime());
};

function flushEditors()
{
	for ( var i in CKEDITOR.instances )
	{
		jQuery('#'+CKEDITOR.instances[i].name).val( CKEDITOR.instances[i].getData() );
	}
};

var CYCImage = function( id )
{

	// constructor {
		this._elem = el( id );
		if( this._elem == null )
		{
			this._elem = new Image();
		}
	// }

	this.loadImage = function( name )
	{
//		_ajxLoadImage( name, this._elem.id );
	};

	this.reload = function()
	{
		var orgSrc = this.src().replace( /([?&])rnd=[0-9.]*/g, '' ); // remove old rnd=
		if( orgSrc.indexOf( '?' ) == -1 )
		{
			orgSrc += '?';
		}
		else
		{
			orgSrc += '&';
		}
		orgSrc += 'rnd=' + Math.random();
		this.setSrc( orgSrc );
	}

};

CYCImage.prototype = new CImage();

function _handleEvent( obj, evtname, evt )
{
	if( window.pageLoaded === false )
	{
		return false;
	}
	var mectl, meevt, objname;
	if( obj == document )
	{
		mectl = obj;
		objname = 'application';
		meevt = null;
	}
	else
	{
		mectl = ctl( obj );
		objname = mectl.id();
		if( mectl == null )
		{
			throw new Error( 'No constructor defined for control type: ' + _getCtlType( obj ) );
		}
		meevt = new CEvent( evt );
	}
	var args = [ mectl ];
	switch( evtname )
	{
		case 'onmousemove':
			args.push( meevt.clientX(), meevt.clientY() );
		break;

		case 'onmousedown':
		case 'onmouseup':
			args.push( meevt.clientX(), meevt.clientY(), meevt.mouseButtons() );
		break;

		case 'onmouseover':
			args.push( meevt.fromElement() );
		break;

		case 'onmouseout':
			args.push( meevt.toElement() );
		break;
	}
	args.push( meevt );
	try
	{
		var func = window[ objname + '_' + evtname ];
		if( func )
		{
			return func.apply( window, args );
		}
	}
	catch( e )
	{
		alert(
			'Your script contains an error!\n'
			+ '\n'
			+ '    Please review \'' + evtname + '\' event handler for object \'' + objname + '\'\n'
			+ '\n'
			+ 'Information about this error:\n'
			+ '\n'
			+ '    ' + e.message + '\n'
		);
		return false;
	}
};

/*
 * Try to handle some javascript errors. Unfortunately this doesn't handle syntax errors.
 */
//window.onerror = function( msg, file, line )
//{
//	if( msg == 'uncaught exception: Permission denied to get property HTMLDivElement.nodeName' ) // weird firefox ?bug?
//	{
//		return;
//	}
//	return true; // do not use internal browser error handling
//};

function jooxbrUnsupportedHandler( msg )
{
	alert(
		'Unfortunately you browser doesn\'t support some features used by GYRE.\n'
		+ 'Also there is a possibility that you disabled some of them for security sake.\n'
		+ 'If it is the case then please enable them for application to work correctly.\n'
		+ '\n'
		+ 'The missing features are:\n'
		+ msg
	);
};

function today()
{
	var date = new Date();
	return date.getMonth() + '/' + date.getDay() + '/' + date.getFullYear();
};

function genPDF(name)
{
	_ajxgenPDF(name);
};
function openWindow(d_message,d_title,d_width,d_height,d_options){
	  var d = new Date();
	  var dialogID = 'AppDialog'+d.getTime();
	  var container = '<div id="'+dialogID+'" style="display: none; background-color: #FFF;padding:0px;overflow:hidden;"></div>';
	  var htmlText = '<p>'+d_message+'</p>';
	  jQuery('#Body').append(container);
	  jQuery('#'+dialogID).html(htmlText);
	  if(d_options==undefined){
		  d_options = { buttons:{Close:function(){jQuery(this).dialog('close');}}};
	  }
	  var setup_options = {
				bgiframe: true,
				autoOpen: true,
				width:d_width,
				height:d_height,
				modal:true,
				close: function(event, ui){
					  jQuery(this).dialog('destroy').remove();
				},
				title: d_title};
	  jQuery.extend(setup_options,d_options);
	  jQuery('#'+dialogID).dialog(setup_options);
}
function openURLWindow(d_url,d_title,d_width,d_height,d_options){
	  var d = new Date();
	  var dialogID = 'AppDialog'+d.getTime();
	  var container = '<div id="'+dialogID+'" style="display: none; background-color: #FFF;padding:0px;overflow:hidden;"></div>';
	  var htmlText = "<iframe name=\"pdfframe\" src=\""+d_url+"\" width=\"100%\" height=\"100%\" frameborder=\"0\" hspace=\"0\" vspace=\"0\"></iframe>";
	  jQuery('#Body').append(container);
	  jQuery('#'+dialogID).html(htmlText);
	  if(d_options==undefined){
		  d_options = { buttons:{Close:function(){jQuery(this).dialog('close');}}};
	  }
	  var setup_options = {
				bgiframe: true,
				autoOpen: true,
				width:d_width,
				height:d_height,
				modal:true,
				close: function(event, ui){
					  jQuery(this).dialog('destroy').remove();
				},
				title: d_title};
	  jQuery.extend(setup_options,d_options);
	  jQuery('#'+dialogID).dialog(setup_options);
}
