AutomatedTesting = new function(){

  var _commands = new Array();
	
	var _currentCommand = 0;
	
	var _delay = 4000;
	
	var _div = null;  

  var _table = null; 
	
	var _processedRequests = 0; 
	
	var _start = null;
	
	var _stop = null;
	
	var _loops = 0;
	
	var _processingCommands = false;
	
	var _separator = "<|>";
	
	
	/*  - - - - - - - - -  public functions  - - - - - - - - -  */
	return{

		/* Adds a command to be executed on a timer */
		AddCommand: function(jsonCommand){
		  try{
		  
  		  if(!jsonCommand.addToHistory || _processingCommands)
  			  return;
  
  			_start.removeAttr("disabled");
  				
  			_commands.push(jsonCommand);
  			
  			_div.find("textarea").val(AutomatedTesting.GetCommands());
  			
  			_div.find("#queue").text(_commands.length);
  			
  			var cmds = $("#CommandURL");
  			
  			var tr = $("<tr id='" + _commands.length + "'><td>" + _commands.length + "</td><td>" + jsonCommand.url + "</td>");
  			
  			cmds.append(tr);
				
			}catch(err){Log.Add("AutomatedTesting.AddCommand", err, LogType.Error);}
		},
		
		ClearCommands: function(){

  		var cmds = $("#CommandURL");
	
		  for(var i = 1; i<_commands.length + 1; i++)
			  cmds.find("#" + i).remove();
			  
		  _commands = new Array();			
			
		},
		
		/* Gets the json commands to be sent to server */
		GetCommands: function(){
		  try{
  		  var str = "";
  			
  			for(var i = 0; i<_commands.length; i++)
  			{
  			  var s = Utilities.ToString(_commands[i]);
  
  				if(i < _commands.length)
  				   s += _separator;
  					 
  				str += s;
			}

			return str;
			
      }catch(err){ Log.Add("AutomatedTesting.GetCommands", err, LogType.Error); }			
		},

		/* Prepares the html for testing */		
		Initialize: function(div){
		  try{
  		  _div = $(div);
  			
  			_start = _div.find("#start");
  			_start.click(function(){
  			  $(this).attr("disabled", "true");
  				$("#Automation #stop").removeAttr("disabled");
  				_processingCommands = true;
  				AutomatedTesting.RunCommands();
  			});
  			
  			_stop = _div.find("#stop");
  			_stop.click(function(){
  			  $(this).attr("disabled", "true");
  				$("#Automation #start").removeAttr("disabled");
  				_processingCommands = false;
  			});
  			
  			_div.find("#SaveButton").click(function(){
				  AutomatedTesting.Save();
  			});
				
				Communication.CustomRequest("Automation.max?action=getoptions", function(options){
				  if(options.length == 0)
					  return;
						
					var html = $(options);
					$("#Automation #OpenName").html(html);
				});
				
				_div.find("#OpenButton").click(function(){
				  var name = $("#Automation #OpenName").val();
					Communication.CustomRequest("Automation.max?action=getjson&name=" + name, function(json){
						AutomatedTesting.ClearCommands();
						var rs = json.split(_separator);
						for(var i = 0; i<rs.length; i++)
						{
						  if(rs[i]){
							  var req = eval('(' + rs[i] + ')');
							  AutomatedTesting.AddCommand(req);
							}
						}
						$("#Automation #start").removeAttr("disabled");
					});
				});
				
				
  			
		  }catch(err){Log.Add("AutomatedTesting.Initialize", err, LogType.Error);}				
		},		

		/* Executes commands on a timer */
		RunCommands: function(delay){
		  try{
  		  if(delay && (typeof delay == "number"))
  			  _delay = delay;
  				
  			if(!_processingCommands)
  			  return;
  			
  			ExecuteCommand();
  			
  			_div.find("#processedRequests").text(_processedRequests);
  			
  			_div.find("loops").text(_loops);
  
  	  	setTimeout("AutomatedTesting.RunCommands()", _delay);
		  }catch(err){Log.Add("AutomatedTesting.RunCommands", err, LogType.Error);}				
		},
		
		/* Saves the test to the server */
		Save: function(){
		  try{
			
  			//var json = AutomatedTesting.GetCommands();
				
				var json = _div.find("textarea").val();
				
				var name = $("#SaveName").val();
				
				Communication.CustomRequest("Automation.max?action=save&name=" + name, saveResponse, "&json=" + encodeURIComponent(json)); 
				
		  }catch(err){Log.Add("AutomatedTesting.RunCommands", err, LogType.Error);}
		}

		
  }

	
	/*  - - - - - - - - -  private functions  - - - - - - - - -  */	
  /* gets a command and executes it */
  function ExecuteCommand(){
	  try{
  	
    	if(_currentCommand == _commands.length)
  		{
    	  _currentCommand = 0;
  			_processedRequests = 0;			
  		  _loops++;
  			_div.find("#loops").text(_loops);
  			_div.find("#processedRequests").text(_processedRequests);			 
  		}
  		
  		var cmds = $("#CommandURL");
  		cmds.find(".current").removeClass("current");
  		cmds.find("#" + (_currentCommand + 1).toString()).addClass("current");
    	
    	var command = _commands[_currentCommand];
    		
    	var type = command.requestType.replace(" ", "");
  		
  		SubstituteValues(command);
  
    	Communication[type](command);
  		
  		_processedRequests++;
  		
  		_currentCommand++;
			
	  }catch(err){Log.Add("AutomatedTesting.ExecuteCommand", err, LogType.Error);}  				
  }
	
	/*  */
	function saveResponse(response){
	  alert("saveResponse " + response);
	}

	
	/* updates values in the command that need to be changed */
	function SubstituteValues(command){
	  try{
		
  	  command.id = ReqList.GetValue("id");
			
	  }catch(err){Log.Add("AutomatedTesting.SubstituteValues", err, LogType.Error);}			
	}
	
}
