//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// SummaryPopup
//
var summaryPopup;

function SummaryPopup(hidden)
{
  document.write("<div id='sPopup' style='"+(hidden?"display: none; ":"")+"' class='sPopup'>"

  +"<div id='sContentDiv' class='sContent'>"
  +"<div id='sContentArea'>&nbsp;</div>"
  +"</div></div>");

  this.content = document.getElementById("sContentArea");
  this.contentDiv = document.getElementById("sContentDiv");
  this.toggleImg = document.getElementById("sToggleImg");
  this.levelMap = new Object();

  summaryPopup = this;	

  this.setOnRemoved = function(evt)
  {
	  this.onremoved = evt;
  }

  this.addLevelMap = function(level, name)
  {
    this.levelMap[level] = name;
  }

  this.defineGroup = function(groupName, title)
  {
      if (!this.groups)
	      this.groups = new Object();
      this.groups[groupName] = title;
  }

  this.addField = function (groupName, value, id, indent, children)
  {
      if (!this.fields)
	      this.fields = new Object();

      var group = this.fields[groupName];
      if (!group)	
      {
	      group = new Array();
	      this.fields[groupName] = group;
      }

      var o = new Object();
      o.value = value;
      o.id = id;
      o.indent = indent;
      o.children = children;
      
      //group[id] = o;
      group.push(o);

      var idsplit = new String(id).split("=");
      var level = 0;
      if (idsplit.length == 2)
        o.level = idsplit[1];

      var s = "";
      for(var v in this.fields)
      {
	      var a = this.fields[v];
	      var groupTitle = this.groups[v];
	      s += "<h3 id='sbox_" + v + "'>"+groupTitle+"</h3>";

	      for (var idx = 0; idx < a.length; idx++)
	      {
		      var field = a[idx];

		      if (field)
		      {
                          var unremoveable = false;
                          if (field.children)
                          {
                              for (var cidx = 0; cidx < field.children.length; cidx++)
                              {
                                  for (var j in a)
                                  {
                                      if (a[j])
                                      {
                                                var a_j_id = a[j].id;
                                                var a_j_idsplit = new String(a_j_id).split("=");
                                                if (a_j_idsplit.length == 2)
                                                    a_j_id = a_j_idsplit[0];
                                                if (a_j_id == field.children[cidx])
                                                {
                                                    unremoveable = true;
                                                    break;
                                                }
                                      }
                                  }
                                  if (unremoveable)
                                      break;
                              }
                          }
			  var fieldId = v + "_" + field.id;
			  s += "<div id='"+fieldId+"'>" +  "<input id='internal_remove_" + field.id + "'" + (unremoveable ? " style='display: none'" : "") + " type='button' class='delete' onclick='summaryPopup.internalRemove(\""+fieldId+"\")'/>" + "<h2" + (field.indent ? " style='padding-left: 36px'" : "") + " id='"+fieldId+"_value'>" + field.value + (field.level > 0 ? " (" + this.levelMap[field.level] + ")" : "") + "</h2></div>";
		      }
	      }
	      

      }
      this.content.innerHTML = s;
  }

  this.remove = function(groupName, id, noEvent)
  {
      this.internalRemove(groupName+"_"+id, noEvent);
  }

  this.update = function(groupName, id, value)
  {	
      if (!this.fields)
	      this.fields = new Object();

      var group = this.fields[groupName];
      if (!group)	
      {
	      group = new Array();
	      this.fields[groupName] = group;
      }
      if (!group[id])
	      this.addField(groupName, value, id);
      else
      {
	      document.getElementById(groupName+"_"+id+"_value").innerHTML = value;
	      group[id].value = value;
	      group[id].id = id;
      }
  }

  this.internalRemove = function(id, noEvent)
  {
      var li = document.getElementById(id);
      if (!li) return;
      //var container = li.parentNode.parentNode;
      var parent = li.parentNode;
      parent.removeChild(li);
      var uscorePos = id.indexOf('_');
      var groupName = id.substr(0, uscorePos);
      var fieldId = id.substr(uscorePos+1);

      var a = this.fields[groupName];

      for (var idx = 0; idx < a.length; idx++)
      {
          if (a[idx] && a[idx].id == fieldId)
          {
              a[idx] = null;
              break;
          }
      }
      //a[fieldId] = null;



      for (var i in a)
      {
          var field = a[i];
          if (field && field.children)
          {
              var unremoveable = false;
              for (var cidx = 0; cidx < field.children.length; cidx++)
              {
                  for (var j in a)
                  {
                      if (a[j])
                      {
                            var a_j_id = a[j].id;
                            var a_j_idsplit = new String(a_j_id).split("=");
                            if (a_j_idsplit.length == 2)
                                a_j_id = a_j_idsplit[0];
                            if (a_j_id == field.children[cidx])
                            {
                                unremoveable = true;
                                break;
                            }
                      }
                  }
                  if (unremoveable)
                      break;
              }
              if (!unremoveable)
              {
                  document.getElementById("internal_remove_" + field.id).style.display = '';
              }
          }
      }

      if (this.isEmptyGroup(groupName))
          this.removeGroup(groupName);

      if (!noEvent && this.onremoved)
	      this.onremoved(groupName, fieldId);
  }

  this.isEmptyGroup = function(groupName)
  {
      if (!this.fields)
          return true;
      if (!this.fields[groupName])
          return true;

      for (var i in this.fields[groupName])
          if (this.fields[groupName][i])
              return false;
      return true;
  }

  this.removeGroup = function(groupName)
  {
      if (!this.fields)
	      this.fields = new Object();
      delete this.fields[groupName];
      var ul = document.getElementById("sbox_"+groupName);
      if (!ul)
	  return;
      var container = ul;//.parentNode;
      container.parentNode.removeChild(container);

      if (this.isEmpty())
          {
              document.getElementById("sContentArea").innerHTML = "&nbsp;";
          }
  }

  this.toggle = function()
  {
	  if (this.contentDiv.style.display=='none')
	  {
	      this.open();
	  }
	  else
	  {
	      this.close();
	  }
  }

  this.open = function()
  {
	  this.contentDiv.style.display = '';
	  if (this.toggleImg) this.toggleImg.src = 'sbox_close.gif';
  }

  this.close = function()
  {
	  this.contentDiv.style.display = 'none';
	  if (this.toggleImg) this.toggleImg.src = 'sbox_open.gif';
  }

  this.getIds = function (groupName)
  {
      if (!this.fields)
	      this.fields = new Object();
      var a = this.fields[groupName];
      var ret = new Array();
      if (a)
      {
          for(var i in a)
          {
                  var obj = a[i];
                  if (obj)
                  {
                          ret.push(obj.id);
                  }
          }
      }
      return ret;
  }

  this.getValues = function(groupName)
  {
      if (!this.fields)
	      this.fields = new Object();
      var a = this.fields[groupName];
      var ret = new Array();
      for(var i in a)
      {
	      var obj = a[i];
	      if (obj)
	      {
		      ret.push(obj.value);
	      }
      }
      return ret;
  }
  
  this.isEmpty = function()
  {
      if (!this.fields)
          return true;
      for (var g in this.fields)
          for (var i in this.fields[g])
              if (this.fields[g][i])
                  return false;
      return true;
  }
}


