
 var g_stealthSocket;
	var g_steathSocketHandlerFunction;

 function getStealthSocket(swfID) 
	{
		 try
			{
     if (navigator.appName.indexOf("Microsoft") > -1) g_stealthSocket = window[swfID];
     else g_stealthSocket = document[swfID];
					
					if ( !g_stealthSocket ) 
					{
						g_stealthSocket='no socket';						
					}
			} catch( e )
			{
						g_stealthSocket='no socket';
			}
 }

 function stealthSocketReply( data )
 {
		 if ( !g_steathSocketHandlerFunction )
			{
				 handlerServerRequest(data); 
			} else
			{
				 var func = g_steathSocketHandlerFunction;
					g_steathSocketHandlerFunction=false;//neccessary to set handler to false before we call handler doing
					//this prevent lost handler if the handler function calls StealthSocket.
					//Basically setting g_steathSocketHandlerFunction to false after call to handler overwipes StealthSocket's setting.
					//thus when the reply comes back from the server.  The g_steathSocketHandlerFunction is false and not the function that StealthSocket set.
					//this bizarre bug came to light whilst writing availability functions for Calendar popup.  See imports/Calendar/calendarAvailability.js.
     func(data);					
			}			
 }

 function StealthSocket( sUrl, handlerFunction, sCaller, xWin, xFormObject, xSubAction, xServlet, xTarget, xAdditionalUrl, xFunctionPointer, xNoShield, xAllowMultiSubmit )
 {
		 if ( !g_stealthSocket || g_stealthSocket == 'no socket' )
			{
				  if ( xAdditionalUrl != undefined )
						{
							 status='attempting backup xAllowMultiSubmit='+xAllowMultiSubmit;
				    xWin.StealthDownload( xFormObject, xSubAction, xServlet, xTarget, xAdditionalUrl, xFunctionPointer, xNoShield, xAllowMultiSubmit );
						}
				  return;
			}
		 g_steathSocketHandlerFunction=handlerFunction;

			if ( top.g_debug >= 2 ) sUrl += "&debuglevel="+top.g_debug+"&scriptHandler="+getFunctionName( handlerFunction.toString() );
			if ( top.g_debug >= 3 ) sUrl += "&caller="+getFunctionName( (sCaller ? sCaller : StealthSocket.caller) );
			try
			{
     g_stealthSocket.sendRequest( sUrl );
			} catch(e)
			{				
						if ( xAdditionalUrl != undefined )
  						xWin.StealthDownload( xFormObject, xSubAction, xServlet, xTarget, xAdditionalUrl, xFunctionPointer, xNoShield, xAllowMultiSubmit );
			}
 }
	
	function getFunctionName( sScriptFunctionText )
	{
    if ( !sScriptFunctionText || sScriptFunctionText.indexOf("function") < 0 ) return "Unknown";
    sScriptFunctionText = sScriptFunctionText.replace(/function /,'');
    sScriptFunctionText = sScriptFunctionText.substring(0, sScriptFunctionText.indexOf("(") );
    return sScriptFunctionText.replace(/\n/,'').replace(/\r/,'');		 
	}
	
	function handlerServerRequest( data )
	{
		  findAndExecuteScripts(data);
	}
	
	
	
		function runStealthProcess( processPk, extraParams )
		{
			alert("recieved run request " + processPk + ",  " + extraParams );
			 try 
				{
						if ( g_mainframe.document.all.Mat_Pain_Div )
						{
								alert("already running a process.");
								failStealthProcess( processPk, extraParams );
								return;
						}
						
						if ( !g_mainframe.document.all.view )
						{
									alert("view doesn't exist.  Create view form.");
									var formObject = g_mainframe.document.createElement("form");
									formObject.method='post';
									formObject.style.margin='0px';
									formObject.style.padding='0px';
									formObject.name='view';
									formObject.id='view';
									g_mainframe.document.body.insertBefore( formObject, g_mainframe.document.body.firstChild );	
						}
									
						if ( !g_mainframe.containsScript(g_root+'scripts/tPane.js') || !g_mainframe.containsScript(g_root+'scripts/processMenu_Control.js') )
						{
								alert("scripts not imported.  Importing scripts.");
								g_mainframe.importScript( g_root+'scripts/tPane.js' );
								g_mainframe.importScript( g_root+'scripts/processMenu_Control.js' );
								setTimeout("attemptStealthProcess( "+processPk+", '"+extraParams+"', 100, 5 )",100);
								return;
						}	
						setTimeout("attemptStealthProcess( "+processPk+", '"+extraParams+"', 100, 2 )",1);
				} catch(e)
				{
					 alert("error " + e.description );
						failStealthProcess( processPk, extraParams );
				}
		}
		
		function attemptStealthProcess( processPk, extraParams, delay, maxAttempts, attemptCount )
		{	
		  try
				{
      g_mainframe.validateProcess( 'view', processPk, '', 'true', '', extraParams);
						return;
				} catch(e)
				{
					  if ( !attemptCount || attempCount <= maxAttempts )
							{
								  if ( !attemptCount ) attempCount = 1;
										else attempCount++;
								  setTimeout("attemptStealthProcess( "+processPk+", '"+extraParams+"', "+delay+", "+maxAttempts+", "+attemptCount+" )",delay);
							} else
							{
								 failStealthProcess( processPk, extraParams );
							}
				}
		}
		
		function noReply(){}
				
		function failStealthProcess( processPk, extraParams )
		{
			 alert("failing " + processPk + ", " + extraParams );
				StealthSocket( "service=StandardSocketServlet&subAction=failStealthProcess&session="+g_session+"&genExecutionPk="+processPk+"&"+extraParams, noReply, ""+failStealthProcess.caller);
		}
	
	
	
	
	
