// public
function MVNetGetEntriesAsXml(list)
{
   var xml = "<attribute" + _getMoo(list.name) + ">";
	return xml + _toXml(list) + "</attribute>";
}

// public
function NetGetEntryAsXml(textfield)
{
   var xml = "<attribute" + _getMoo(textfield.name) + ">";
   xml += "<value>"+textfield.value+"</value>";
   xml += "</attribute>";
	return xml;  
}

function MVNetRemove(list)
{
   list = eval(list);
	if (list.selectedIndex > -1)
	{
		list.options[list.selectedIndex] = null;
		list.selectedIndex = 0;
	}
}

function MVNetLoadList(l,x)
{
   if (!x) return;
   l = eval(l);
	if (x)
	{
		var xml = parseXml(x);
		var children = xml.getChildren();

      // set mode
      var modeControl = eval("document.forms[0]." + l.name + "_mode");
      var s = x.indexOf("mode='");
      if(modeControl && s!=-1)
      {
         s+=6;
         var e = x.indexOf("'", s);
         var mode = x.substring(s,e);

         var i=0;
         if(mode=="set") i=1;
         else if(mode=="add") i=2;
         else if(mode=="remove") i=3;
         modeControl.selectedIndex = i;
      }


		if (children == null || children.length == 0)
		{
         l.childName = "value";
		   return;
		}

		l.rootName = xml.getName();
		l.childName = children[0].getName();

		for (var i=0; i<children.length; i++)
		{
			var addrStr = children[i].getChildTextTrim("address-string");
         // if we have just a text field for single valued
         if (l.type == "text")
         {
            l.value = addrStr;
            return;
         }
         else
         {
            l[l.length] = new Option(addrStr,addrStr);
         }
		}
		l.selectedIndex = 0;
	}
}

function MVNetAdd(list,n,p)
{
	if(p)
	{
		n = prompt("new item","");
	}

	if (n!="" && n!=null)
	{
		list = eval(list);
		if(list.type == "text")
		{
		   // if text field just change the value
         list.value = n;
		}
		else
		{
		   // if select box then add a new option
		   list[list.length] = new Option(n,n);
		   list.selectedIndex = list.length-1;
		}
	}
}

function AddAddress(userContext, control)
{
	var url = 'webacc?taskId=dev.Empty&merge=dev.Netaddr&User.context='+userContext+'&control='+control;
	window.open(url, 'Add', "toolbar=no,location=no,directories=no,menubar=no,scrollbars=yes,resizable=yes,width=540,height=500");
}

function _getMoo(name)
{
   var mode = eval("document.forms[0]." + name + "_mode");
   if(mode)
      return " mode='" + mode.value + "'";
   else
      return "";
}

function _toXml(list)
{
   var xml = "";

	for (var i=0; i<list.length; i++)
   {
		var str = list[i].value;
		var type = str.substring(0, str.indexOf(":") );
		var val = str.substring(str.indexOf(":")+2, str.length );
		xml += "<value>"+str+"</value>";
    }

   return xml;
}

function uih_netAddress(name, uiControl, xmlControl)
{
   xmlControl.value = NetGetEntryAsXml(uiControl);
   return true;
}

function uih_netAddressMV(name, uiControl, xmlControl)
{
   xmlControl.value = MVNetGetEntriesAsXml(uiControl);
   return true;
}
