function updateNewValue()
{
  var ni = document.getElementById('_DynamicTable'); 
  newvalue = document.getElementById("newvalue");
  newvalue.value = '';
  for (i=1;i<ni.rows.length;i++)
  {
    aname = ni.rows[i].cells[0].innerHTML;
    avalue = ni.rows[i].cells[1].innerHTML;
	var row = ni.rows[i];
	var cells = row.cells;
	cellcount=cells.length;


	if (document.getElementById('Password_'+i))
      newvalue.value = newvalue.value + aname+"="+avalue+","+document.getElementById('Password_'+i).innerHTML+";";
    else
	{
	  j=2;
      while (j<cellcount-2)
      {
        var el = cells[j];
		var elvalue = null;
		if (el)
		{
  		  elvalue = el.innerHTML;
		}

		avalue = avalue+','+elvalue;
		j=j+1;
	  }
      newvalue.value = newvalue.value + aname+"="+avalue+";";
	}
  }
  newvalue.value = newvalue.value.substring(0, newvalue.value.length-1);
}

function alltrim(str) {
  return str.replace(/^\s+|\s+$/g, '');
}

function addAlias()
{
//	if (document.getElementById('theIdentifierValue').value.charAt(document.getElementById('theIdentifierValue').value.length-1)!='\\')
//	  document.getElementById('theIdentifierValue').value = document.getElementById('theIdentifierValue').value + '\\';
    document.getElementById('theIdentifier').value = alltrim(document.getElementById('theIdentifier').value);
	addElement(document.getElementById('theRule').value, 'Alias Path', document.getElementById('theIdentifierRule').value, 'Alias Name');
}

function addElement(rule, rulename, identifiernamerule, identifiernamerulename) {
  var ni = document.getElementById('_DynamicTable');
  var numi = document.getElementById('theValue');
  var num = (document.getElementById('theValue').value -1)+ 2;
  numi.value = num;

  if ((rule == null) && (document.getElementById('theRule')!=null))
	  rule = document.getElementById('theRule').value;

  if (identifiernamerule!=null)
  {
    var re = new RegExp(identifiernamerule, 'i');
    if (!document.getElementById('theIdentifier').value.match(re))
	{
      alert('Invalid value "'+ document.getElementById('theIdentifier').value +'" for "'+((identifiernamerulename!=null)?identifiernamerulename:rule+'"')+'".');
      return;
	}
  }


  if (rule!=null)
  {
    var re = new RegExp(rule, 'i');
    if (!document.getElementById('theIdentifierValue').value.match(re))
	{
      alert('Invalid value "'+ document.getElementById('theIdentifierValue').value +'" for "'+((rulename!=null)?rulename:rule+'"')+'".');
      return;
	}
  }
 
  for (i=1;i<ni.rows.length;i++)
  {
    aname = ni.rows[i].cells[0].innerHTML;
    if (aname.toUpperCase() ==  document.getElementById('theIdentifier').value.toUpperCase())
    {
      if (!confirm('Item exists. Do you want to update it?'))
	    return;
	  else
		ni.deleteRow(i);	
    }
  }
  
if ((document.getElementById('theIdentifier').value+' ')==' ') 
{
    alert('Can not insert an empty item.');      
	return;
}

if ((document.getElementById('theIdentifierValue').value+' ')==' ') 
{
    alert('Can not insert an empty item.');      
	return;
}

  var newrow = ni.insertRow(-1);
  var divIdNameID = 'DynamicRow_'+num;
  newrow.setAttribute('id',divIdNameID);
  aCell = newrow.insertCell(-1);
  aCell.innerHTML = document.getElementById('theIdentifier').value;

  var divIdName = 'DynamicRowIdentifier_'+num;
  aCell.setAttribute('id',divIdName);

  aCell = newrow.insertCell(-1);
  aCell.innerHTML = document.getElementById('theIdentifierValue').value;
  var divIdName = 'DynamicRowValue_'+num;
  aCell.setAttribute('id',divIdName);

  i=1;
  while (document.getElementById('AddRow'+i.toString()))
  {
    var el = document.getElementById('AddRow'+i.toString());
    aCell = newrow.insertCell(-1);
    var atype = el.getAttribute('type');
	if (atype=='checkbox')
	{
		atype = el.Value;
		if (el.checked)
			aCell.innerHTML = 'True';
		else
			aCell.innerHTML = 'False';
	}
	else
    {
      aCell.innerHTML = el.value;
	}
    var divIdName = 'AddRow'+i.toString()+'_'+num;
    aCell.setAttribute('id',divIdName);
    i=i+1;
  }
  
  aCell = newrow.insertCell(-1);
  aCell.innerHTML = "<input type='button' value='Edit' onclick='editRow(\""+divIdNameID+"\");'/>";
  aCell = newrow.insertCell(-1);
  aCell.innerHTML = "<input type='button' value='Delete' onclick='removeRow(\""+divIdNameID+"\");'/>";

  if (document.getElementById('theIdentifierAdditionalValue'))
  {
    ap = document.createElement("span");
    ap.innerHTML = document.getElementById('theIdentifierAdditionalValue').value;
    ap.setAttribute('id','Password_'+newrow.rowIndex);
    ap.style.visibility='hidden';
    ap.style.height = 0;
    ni.parentNode.appendChild(ap);
  }

  document.getElementById('theIdentifier').value = "";
  document.getElementById('theIdentifierValue').value = "";
  if (document.getElementById('theIdentifierAdditionalValue'))
    document.getElementById('theIdentifierAdditionalValue').value = "";
  updateNewValue();
}		 

function removeRow(src)
{
  var oRow = document.getElementById(src); 
  var ni = document.getElementById('_DynamicTable');

  ni.deleteRow(oRow.rowIndex);
  updateNewValue();
}

function updateMulti(acount, anID)
{
  ni = document.getElementById('NewValue_'+anID); 

  var val = 0;
  for (i=0; i<acount; i++)
  {
	  el = document.getElementById('MultiValue_'+anID+'_'+i);
	  if (el.checked)
		val = val | el.value;
  }
  ni.value = val;
}

function editRow(src) {
  var oRow = document.getElementById(src); 
  var ni = document.getElementById('_DynamicTable');

  document.getElementById('theIdentifier').value = oRow.cells[0].innerHTML;
  document.getElementById('theIdentifierValue').value = oRow.cells[1].innerHTML;
  for (i=2; i<oRow.cells.length-1; i++)
  {
	  el = document.getElementById('AddRow'+(i-1).toString());
	  if (el)
	  {
         var atype = el.getAttribute('type');
      	 if (atype=='checkbox')
	     {
		    atype = el.Value;
			if (oRow.cells[i].innerHTML=='True')
		      el.checked = true
			else
		      el.checked = false;
	     }
	    else
		  document.getElementById('AddRow'+(i-1).toString()).value = oRow.cells[i].innerHTML;
	  }
  }
//  removeRow(src);
}

