/********************************************************************************************************************************
* Licensed Materials - Property of IBM                                                                                          *
*                                                                                                                               *
* IBM Cognos Products: AGS                                                                                                      *
*                                                                                                                               *
* (C) Copyright IBM Corp. 2005, 2008                                                                                            *
*                                                                                                                               *
* US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. *
*********************************************************************************************************************************/
/* ****************************************
  This is a couple of js object "classes" that will allow the creation of an action definition
  object in js that can write to and read from hidden form fields

 ****************************************  */
	
	var AGENT_DEFINITION_REPORT = "agentDefinitionReport";
	
	function AgentDefinition(){
		//this is a non duplication items object
		this.summaryEventKey;
		this.m_agent_actions = new Array();
		this.agentDefinitionReport;
		
		this.setSummaryEventKeyValueFromPortalStyleTopics = function(portal_style_topics){
			var translated_item = decodeURIComponent(portal_style_topics);
			//convert it to ags stylee
			this.summaryEventKey.parseFromString(translated_item);
		}
		
		this.getSummaryEventKeyValueWithPortalStyleTopics = function(){
			//convert to portal stylee
			return this.summaryEventKey.toString();
		}
		
		/**
		* get the all items on all agent actions and check that they are not
		* in the event key
		*/
		this.validateOngoingFilterTopics = function(){
			//check that the items in the summary
			var valid = true;
			
			var sek = this.summaryEventKey;
			
			for(var x_index = 0; x_index < this.getActionCount() & valid == true; x_index++){
				var an_action = this.getActionAt(x_index);
				
				if(an_action.nonDuplication && an_action.nonDuplication.size() > 0){
					var items = an_action.nonDuplication.items();
					
					for(j = 0; j < items.length & valid == true; j++){
						var la_item = items[j];
						valid = !sek.containsItem(la_item);
					}	
				}
			}

			return valid;
		}

		
		/**
		* get all on all agent actions and check that it is not
		* in the event key, if it is remove it
		*/
		this.removeInvalidFilterTopics = function(){
			
			for(var x_index= 0; x_index< this.getActionCount(); x_index++){
				var an_action = this.getActionAt(x_index);
				
				var finished = false;
				
				while(an_action.nonDuplication && !finished){
					
					//remove an item	
					finished = !this.removeFirstInvalidTopic(an_action);
				}
			}
		}
		
		/**
		* sweep to the first invalid topic found and remove it
		* return true if a topic was found
		*/
		this.removeFirstInvalidTopic = function(an_action){
			var removed = false;
			var items = an_action.nonDuplication.items();

			for(var x_index = 0; x_index < items.length; x_index++){
				var la_item = items[x_index];

				if(this.summaryEventKey.containsItem(la_item)){
					this.removeActionFilterItem(la_item);
					removed = true;
					break;
				}
			}
			
			return removed;
		}
		
		/**
		* get all items on the summary event key and check that they not
		* in any of the actions ongoing filter, if it returns the first action name its found in
		otherwise return undefined
		*/
		this.validateSummaryEventKey = function (){
			//check that the items in the summary
			var offending_task = undefined;
			
			var items = this.summaryEventKey.items();

			for(var i = 0; i < items.length && offending_task == undefined; i++){
				offending_task = validateSummaryEventKeyTopic(items[i]);
			}	
			
			return offending_task;
		}
		
		/**
		* check a new topic to be added to the sek that they are not already in an actions topic filter
		*/
		this.validateSummaryEventKeyTopic = function (newEventKeyTopic){
			//check that the items in the summary
			var offending_task = undefined;
	
			for(i = 0; i < this.getActionCount(); i++){
				var an_action = this.getActionAt(i);
			
				if(an_action.nonDuplication && an_action.nonDuplication.containsItem(newEventKeyTopic)){
					if(offending_task != undefined){
						offending_task += ", "
						offending_task += an_action.name;
					}else{
						offending_task = an_action.name;
					}
				}	
			}	
			
			return offending_task;
		}
		
		/**
		* passed an item that shouldnt be in the action filters
		* remove it forthwith
		*/
		this.removeActionFilterItem = function(nonDupeItem){
			for(var i = 0; i < this.getActionCount(); i++){
				var an_action = this.getActionAt(i);
				
				if(an_action.nonDuplication.size() > 0){
					an_action.nonDuplication.removeItem(nonDupeItem);
					an_action.setParam("Action_topic_filter", an_action.nonDuplication.toString());
				}
			}
		}
		
		/*
		*just a way of debug showing whats in the object
		*/
		this.toString = function(){
				var description = new AttributeString();
				if(this.summaryEventKey){
					description.setAttribute("summaryEventKey",this.summaryEventKey.toString());
				}
				
				for(var i = 0; i < this.getActionCount(); i++){
					var task = this.getActionAt(i);
					description.setAttribute("action", task.toString());
				}
				
				return description.toString();
		}
		
		/*
		return an object from the string representation
		*/
		this.fromString = function(attributeString){
			var attributes = new AttributeString(attributeString);
			
			if(this.summaryEventKey){
				this.summaryEventKey = this.summaryEventKey.parseFromString(attributes.getAttribute("summaryEventKey"));
			}
			
			var agent_actions_strings = attributes.getAttributes("action");
			
			for(var i = 0; i < agent_actions_strings.length; i++){
								
				var agentAction = new ActionDefinition();
				agentAction.fromString(agent_actions_strings[i]);
				this.addAction(agentAction);
			}
			
			return this;
		}
		
		/*
		array accessor
		*/
		this.getActionCount = function(){
			return this.m_agent_actions.length;
		}
		
		/*
		array accessor
		*/
		this.getActionAt = function(index){
			return this.m_agent_actions[index];
		}
		
		/*
		array accessor
		*/
		this.removeActionAt = function(index){
			
			var new_array = new Array();
			
			for(var i = 0; i < this.m_agent_actions.length; i++){
				if(i != index){
					new_array.push(this.m_agent_actions[i]);
				}
			}
			
			this.m_agent_actions = new_array;
		}
		
		/*
		* add a new action to the collective
		*/
		this.addAction = function(action){
			
			action.name_id = action.name + "_" + this.getActionCount();		
			
			this.m_agent_actions[this.getActionCount()] = action;
			
			return true;
		}
		
		/*
		array accessor
		*/
		this.getActionById = function(id){
			var selected_action;
			
			if(!id){
				window.alert("action with no id!");
				return;
			}
			
			for(var i = 0; i < this.getActionCount(); i++){
				
				var action = this.getActionAt(i);
				
				if(action.name_id == id){
					selected_action = action;
					break;
				}
			}
			
			return selected_action;
		}		
		
	}
	
	function ActionDefinition(name, id, type, img_src,taskLevelFilter,runCondition){
		
		this.type = type ? type : "";
		this.id = id ? id : "";
		this.name_id= "";
		this.name = name ? name : "";
		this.img_src = img_src ? img_src : "";
		this.params = new Params();
		this.nonDuplication;
		this.taskLevelFilter = taskLevelFilter;
        this.runCondition = runCondition;

		this.getParams = function(){
			return this.params;
		}
		
		this.setParams = function(params){
			this.params = params;
		}
		
		this.setParam = function(name, value){
			this.params.setParam(name, value);
		}
		
		this.addParam = function(name, value){
			this.params.addParam(name, value);
		}
		
		this.addPortalStyleParam = function(name, value){
			this.params.addParam(name, decodeURIComponent(value));
		}
		
		this.removeParam = function(name){
			this.params.removeParam(name);
		}
		
		/*
		just a way of debug showing whats in the object
		*/
		this.toString = function(){
				
				var description = new AttributeString();
				description.setAttribute("type",this.type);
				description.setAttribute("id",this.id);
				description.setAttribute("name",this.name);
				description.setAttribute("path" ,this.path) ;
				description.setAttribute("params",this.params.toString()) ;
				description.setAttribute("taskLevelFilter",this.taskLevelFilter) ;
				description.setAttribute("runCondition",this.runCondition) ;
				return description.toString();
		}
		
		/*
		return an object from the string representation
		*/
		this.fromString = function(attributeString){
			var attributes = new AttributeString(attributeString);			
			
			this.type = attributes.getAttribute("type");
			this.id = attributes.getAttribute("id");
			this.name_id= attributes.getAttribute("name_id");
			this.name = attributes.getAttribute("name");
			this.path = attributes.getAttribute("path");
			this.img_src = attributes.getAttribute("img_src");
			this.taskLevelFilter = attributes.getAttribute("taskLevelFilter");
			this.runCondition = attributes.getAttribute("runCondition");
			
			var params_string= attributes.getAttribute("params");
			
			this.params = new Params().fromString(params_string);
			
			return this;
		}
		
	}
	
	
	function Params(vals){
		this.m_params = new Array();
		
		if(isArray(vals)){
			for(var i = 0; i < this.vals.length; ){
					
				var name = vals[i++];
				var value = vals[i++];
				
				this.setParam(name, value);
			}	
		}
		
		this.addParam = function(name, value){
			this.setParam(name, value);
		}
		
		this.setParam = function(p_name, p_value){
			
			
			if(p_value == undefined || p_value == ""){
				p_value = null;
			}
			
			if(p_value != null){
				p_value = decodeURIComponent(p_value);	
			}
			
			var param = this.getParam(p_name);
			
			if(param == undefined && p_value != null){
				
				this.m_params.push(new Param(p_name, p_value));
			}else{
				
				if(p_value == null){
					this.removeParam(p_name);	
				}else{
					param.param_value = p_value;
				}
			}
			
		}
		
		this.getParamCount= function(){
			return this.m_params.length;	
		}
		
		this.getParamValue = function(name){
			for(var i = 0; i < this.m_params.length; i++){
				if(this.m_params[i].param_name == name){
					return this.m_params[i].param_value;
				}
			}		
		}
		
		
		this.getParam = function(name){
			for(var i = 0; i < this.m_params.length; i++){
				if(this.m_params[i].param_name == name){
					return this.m_params[i];
				}
			}		
		}
		
		this.getParamAt = function(index){
			return this.m_params[index];		
		}
		
		this.removeParam = function(name){
						
			var new_array = new Array();
			
			for(var i = 0; i < this.m_params.length; i++){
				if(this.m_params[i].param_name != name){
					new_array.push(this.m_params[i]);
				}
			}
			
			this.m_params = new_array;
		}
		
		/*
		just a way of debug showing whats in the object
		*/
		this.toString = function(){
				var description = new AttributeString(undefined);
				
				for(var i = 0; i < this.m_params.length; i++){
					if(i != 0){description += "&";}
					
					description.setAttribute("param",this.m_params[i].toString());
				}
				
				return description.toString();
		}
		
		/*
		return an object from the string representation
		*/
		this.fromString = function(attributeString){
			var attributes = new AttributeString(attributeString);			
			
			var params = attributes.getAttributes("param");
			for(var i = 0; i < params.length; i++){
				
				var param_attributes = new AttributeString(params[i]);	
				this.setParam(param_attributes.getAttribute("param_name"), param_attributes.getAttribute("param_value")); 
			}
			return this;
		}
		
		function isArray(a) {
			if (a != null && typeof a == "object") return true;
			return false;
		}
	}
	
	function Param(name, value){
		this.param_name = name;
		this.param_value = value;
		
		/**
		check for ![ ]! and use the agent tree to return the portal style
		*/
		this.getPortalStyleValue = function(){
			
			if(this.param_value == undefined || this.param_value == null){
				return this.param_value;
			}
			return this.param_value;
		}
		
		/*
		just a way of debug showing whats in the object
		*/
		this.toString = function(){
			var description = new AttributeString();
			
			description.setAttribute("param_name",this.param_name);
			
			description.setAttribute("param_value",this.param_value);
			
			return description;
		}
		
		
	
	}

	function AttributeString(attribute_String) {
		this.value_array = new Array();		
		
		if(undefined != attribute_String){
			// parse out name/value pairs separated via &	
			var args = attribute_String.split('&');
			
			for (var i=0;i<args.length;i++){
				this.value_array.push(args[i].split('='));
			}
		}

		
		this.setAttribute = function(name, value){
			this.value_array.push(new Array(name, encodeURIComponent(value)));
		}
		
		this.getAttributes = function(name){
			var attributes = new Array();
			// split out each name = value pair
			for (var i=0;i<this.value_array.length;i++){
				if(this.value_array[i][0] == name){
					attributes.push(decodeURIComponent(this.value_array[i][1]));
					break;
				}
			}
			
			return attributes;
		}
		
		this.getAttribute = function(name){
			var attribute;
			// split out each name = value pair
			for (var i=0;i<this.value_array.length;i++){
				if(this.value_array[i][0] == name){
					attribute = decodeURIComponent(this.value_array[i][1]);
					break;
				}
			}
			return attribute;
		}
		
		this.toString = function(){
			var buffer = "";
			
			// split out each name = value pair
			for (var i=0;i<this.value_array.length;i++){
				if(i != 0)buffer += "&";
				buffer += this.value_array[i][0];
				buffer += "=";
				buffer += this.value_array[i][1];	
			}
			
			return buffer;	
		}
	}