
/***************************************************************************
 [ActiveAgent.js]
 
 Copyright (C) 2004 Next IT Corporation, Inc. Spokane, WA. All Rights Reserved. 
 This document is confidential work and intellectual property of Next IT 
 Corporation. Permission to copy, distribute or use any portion of this file 
 is prohibited without the express written consent of Next IT Corporation.

*****************************************************************************/

// ActiveAgent BotWrapper
// This file is what passes data to the webService through our WebServiceWrapper, into
// the brain.  It also is used to get the respnse from the brain.  We use AJAX.
// Note that this can be done asyncronusly, which stops the UI from freezing
// when a AJAX post/get is sent up.

var _aaWebService;

function ActiveAgentService( url, cb )
{
	_aaWebService = new WebServiceManager(url);
	
	this.prefix = "OC";
	
	// If you want a asyncronus call back from the brain, you NEED to put this function
	// imto your method call as the final parameter, and have  _aaWebService.Async set to true.
	this.handleAsync_MyEvent = function( obj )
	{
		try
		{
			var o = new Object();
			o = obj.value;
			//alert( 'ASYNC \r\n\r\n'+ o.Response );
			if( obj != null && obj.errorDetail != null )
			{
				//alert( "ERROR - " + obj.errorDetail.code + "\n" + obj.errorDetail.string );
				throw true;
			}
			aaUI.ProcessResponse( o );		// Send the response to our responseObject.
		}
		catch( e )
		{
			//alert('FAIL ->'+e.message); 
			//gBotResponseObj.ToggleInterface( false );
			return null;
		}
	};	
	
	this.GetBrainResponse = function( userInput, isAppEvent )
	{
		if( userInput.Input.length > 200 )
		{
			//alert('Please limit your question to 200 characters or less');
			//gBotResponseObj.ToggleInterface( false );
			return null;
		}
		
		userInput.Input = userInput.Input.replace(/\</g,"less than");
		userInput.Input = userInput.Input.replace(/\>/g,"greater than");
	
		if( userInput.Input.replace(' ','').length == 0 )
		{
			//gBotResponseObj.ToggleInterface( false );
			return null;
		}
		
		var r = _aaWebService.InvokeMethod( "GetBrainResponse", this.handleAsync_MyEvent, userInput );
		
		if( r.error )
		{
			//alert( r.errorDetail.code + '\n' + r.errorDetail.string );
			return null;
		}
		else
		{
			return this.GetObject( r );
		}
	};
	
	this.AgentCleanup = function( id )
	{
		var r = _aaWebService.InvokeMethod( "AgentCleanup", null, id );
		if( r.error )
		{
			this.AlertError( "AgentCleanup", r );
		}
		else
			return this.GetObject( r );
	};	
	
	this.CreateBrainID = function()
	{
		var r = _aaWebService.InvokeMethod( "CreateBrainID", this.handleAsync_MyEvent, this.prefix );
		return this.GetObject( r );
	};
	
	this.SetSoundVariable = function( val )
	{
		var r = _aaWebService.InvokeMethod( "SetSoundVariable", this.SetSoundVariable_Async, val );
		return this.GetObject( r );
	};
	
	this.SetSoundVariable_Async = function( obj )
	{
	};	
	
	this.GenerateTTS = function( rsp )
	{
		var r = _aaWebService.InvokeMethod( "GenerateTTS", this.handleAsync_MyEvent, rsp );
		return this.GetObject( r );
	};
	
	this.DoImpasse = function( brainID, currentUrl )
	{
		var r = _aaWebService.InvokeMethod( "DoImpasse", null, brainID, currentUrl );
		return this.GetObject( r );
	};
	
	this.GetImpasseResponse = function( brainID )
	{
		var r = _aaWebService.InvokeMethod( "GetImpasseResponse", null, brainID );
		return this.GetObject( r );
	};
	
	this.SendImpasseResponse = function( userInput )
	{
		var r = _aaWebService.InvokeMethod( "SendImpasseResponse", null, userInput );
		return this.GetObject( r );
	};
	
	this.EndImpasse = function( brainID )
	{
		var r = _aaWebService.InvokeMethod( "EndImpasse", null, brainID );
		return this.GetObject( r );
	};
	
	this.DoLogin = function( fname, lname, email, gradyr, zip, alumni )
	{
		var r = _aaWebService.InvokeMethod( "DoLogin", this.DoLogin_Async, fname, lname, email, gradyr, zip, alumni );
		
		if( r.error )
		{
			return null;
		}
		else
		{
			return this.GetObject( r );
		}		
	};
	
	this.DoLogin_Async = function( obj )
	{
		try
		{
			var o = new Object();
			o = obj.value;
			if( obj != null && obj.errorDetail != null )
			{
				throw true;
			}
			aaUI.ProcessLogin( o );
		}
		catch( e )
		{
			return null;
		}				
	};
		
	this.GetObject = function( r )
	{
		var o = new Object();
		
		if( r.value != null )
		{
			//alert(r.value.Response);
			o = r.value;
			return o;
		}
		else
		{
			//alert(typeof(r));
			return null;
		}
	};	
};

function UserInput( input, isAppEvent, user )
{
	this.Input = input;
	this.IsAppEvent = isAppEvent;
	this.User = user;
	this.CurrentPage = "";
};

function User()
{
	this.ID = this.GetUserID();
	this.UserName = "User";
	this.BrainID = null;
};

User.prototype.GetUserID = function()
{
	if( _agentUserID != null )
		this.ID = _agentUserID;
	else
		this.ID = -1;
};