function hide(id) {
  document.getElementById(id).style.display="none";
  //switchEnable(document.getElementById(id), true, 1);
}
function show(id) {
  document.getElementById(id).style.display="block";
  //switchEnable(document.getElementById(id), false, 1);
}

function showHide(id) {
  var obj=document.getElementById(id);
  if (obj.style.display=="block") obj.style.display="none"; 
  else obj.style.display="block"; 
  return false;
}

function enable(id) {
  document.getElementById(id).disabled=false;
}

function disable(id) {
  document.getElementById(id).disabled=true;
}


////////////////////////////////////////////
// This function traverses the DOM tree of an element and prints the tree.  
// This function called recursively until the DOM tree is fully traversed.
// 
// Parameters:
// - targetDocument is where the tree will be printed into
// - currentElement is the element that we want to print
// - depth is the depth of the current element 
//   (it should be 1 for the initial element)
////////////////////////////////////////////
function switchEnable(currentElement, flag, depth)
{
  if (currentElement)
  {
    var j;
    var tagName=currentElement.tagName;
    // Prints the node tagName, such as <A>, <IMG>, etc
    if (tagName=="INPUT") 
      currentElement.disabled=flag;
      

    // Traverse the tree
    var i=0;
    var currentElementChild=currentElement.childNodes[i];
    while (currentElementChild)
    {
      // Recursively traverse the tree structure of the child node
      switchEnable(currentElementChild, flag, depth+1);
      i++;
      currentElementChild=currentElement.childNodes[i];
    }
  }
}



    function addOption(selectObj, value)
    {
      optionSelected = (value == null);
      if(value == null) value = prompt('', '');
      if(value != null)
      {
        if(value.indexOf(',') != -1)
          alert('Commas are not allowed in size values');
        else
        {
          var i = selectObj.options.length;
          value = value.replace(/1\/2/g, '½');
          selectObj.options.length = i + 1;
          selectObj.options[i].value = (value != '' && value != ' ') ? value : ' ';
          selectObj.options[i].text = (value != '' && value != ' ') ? value : '[empty]';
          selectObj.options[i].selected = optionSelected;
// uncomment the line below if you want the select list to change it's size to match the number of options it contains.
//          selectObj.size = selectObj.options.length;
        }
      }
    }
  
    function editOptions(selectObj)
    {
      for(var i = 0; i < selectObj.options.length; i++)
      {
        if(selectObj.options[i].selected)
        {
          var value = prompt('', selectObj.options[i].value);
          if(value != null)
          {
            if(value.indexOf(',') != -1)
              alert('Commas are not allowed in size values');
            else
            {
              selectObj.options[i].value = value;
              selectObj.options[i].text = (value != '') ? value : '[empty]';
              selectObj.options[i].selected = true;
            }
          }
        }
      }
    }
    
    function deleteOptions(selectObj)
    {
      for(var i = 0; i < selectObj.options.length; i++)
      {
        if(selectObj.options[i].selected)
        {
          for(var j = i; j < selectObj.options.length - 1; j++)
          {
            selectObj.options[j].value = selectObj.options[j + 1].value;
            selectObj.options[j].text = selectObj.options[j + 1].text;
            selectObj.options[j].selected = selectObj.options[j + 1].selected;
          }
          selectObj.options.length = selectObj.options.length - 1;
          i--;
        }
      }
    }
    
    function moveOptions(selectObj, direction)
    {
      if(selectObj.selectedIndex != -1)
      {
        if(direction < 0)
        {
          for(i = 0; i < selectObj.options.length; i++)
          {
            swapValue = (i == 0 || selectObj.options[i + direction].selected) ? null : selectObj.options[i + direction].value;
            swapText = (i == 0 || selectObj.options[i + direction].selected) ? null : selectObj.options[i + direction].text;
            if(selectObj.options[i].selected && swapValue != null && swapText != null)
            {
              thisValue = selectObj.options[i].value;
              thisText = selectObj.options[i].text;
              selectObj.options[i].value = swapValue;
              selectObj.options[i].text = swapText;
              selectObj.options[i + direction].value = thisValue;
              selectObj.options[i + direction].text = thisText;
              selectObj.options[i].selected = false;
              selectObj.options[i + direction].selected = true;
            }
          }
        }
        else
        {
          for(i = selectObj.options.length - 1; i >= 0; i--)
          {
            swapValue = (i == selectObj.options.length - 1 || selectObj.options[i + direction].selected) ? null : selectObj.options[i + direction].value;
            swapText = (i == selectObj.options.length - 1 || selectObj.options[i + direction].selected) ? null : selectObj.options[i + direction].text;
            if(selectObj.options[i].selected && swapValue != null && swapText != null)
            {
              thisValue = selectObj.options[i].value;
              thisText = selectObj.options[i].text;
              selectObj.options[i].value = swapValue;
              selectObj.options[i].text = swapText;
              selectObj.options[i + direction].value = thisValue;
              selectObj.options[i + direction].text = thisText;
              selectObj.options[i].selected = false;
              selectObj.options[i + direction].selected = true;
            }
          }
        }
      }
    }
  
    function selectAll(selectObj)
    {
      if(selectObj.options.length)
        for(i = 0; i < selectObj.options.length; i++)
          selectObj.options[i].selected = true;
      return true;
    }
    
    function providerCheck(txt) {
      var obj=document.getElementById(txt);
      obj.checked=true;
      var objcontainer=document.getElementById(txt+"_container");
      objcontainer.className='block button blockover';
      
      if (txt=="sip") {
        switchEnable(document.getElementById('sip_form'), false, 1);
        switchEnable(document.getElementById('isdn_form'), true, 1);
        document.getElementById('sip_form').style.display="block";
        document.getElementById('isdn_form').style.display="none";
      } else {
        switchEnable(document.getElementById('sip_form'), true, 1);
        switchEnable(document.getElementById('isdn_form'), false, 1);
        document.getElementById('sip_form').style.display="none";
        document.getElementById('isdn_form').style.display="block";
      }
    }
    function providerSwitch(txt) {
      if (txt=="sip") {over="sip"; out="isdn"; }
      else {over="isdn"; out="sip"; }
      
      var objover=document.getElementById(over+"_container");
      var objout =document.getElementById(out+"_container");
      objover.className='block button blockover';
      objout.className='block button';
    }
    function providerRestore() {
      var objsip = document.getElementById('sip');
      var sip = document.getElementById('sip_container');
      var isdn = document.getElementById('isdn_container');
      if (objsip.checked==true) {
        sip.className='block button blockover';
        isdn.className='block button';
      } else {
        isdn.className='block button blockover';
        sip.className='block button';
      } 
      
    }

    function groupCheck(txt) {
      var obj=document.getElementById(txt);
      obj.checked=true;
      var objcontainer=document.getElementById(txt+"_container");
      objcontainer.className='block button blockover';
      
      if (txt=="client") {
        switchEnable(document.getElementById('client_form'), false, 1);
        switchEnable(document.getElementById('dep_form'), true, 1);
        switchEnable(document.getElementById('res_form'), true, 1);
        switchEnable(document.getElementById('num_form'), true, 1);
        document.getElementById('client_form').style.display="block";
        document.getElementById('dep_form').style.display="none";
        document.getElementById('res_form').style.display="none";
        document.getElementById('num_form').style.display="none";
      } else if (txt=="dep") {
        switchEnable(document.getElementById('dep_form'), false, 1);
        switchEnable(document.getElementById('client_form'), true, 1);
        switchEnable(document.getElementById('res_form'), true, 1);
        switchEnable(document.getElementById('num_form'), true, 1);
        document.getElementById('client_form').style.display="none";
        document.getElementById('dep_form').style.display="block";
        document.getElementById('res_form').style.display="none";
        document.getElementById('num_form').style.display="none";
      } else if (txt=="num") {
        switchEnable(document.getElementById('num_form'), false, 1);
        switchEnable(document.getElementById('dep_form'), true, 1);
        switchEnable(document.getElementById('client_form'), true, 1);
        switchEnable(document.getElementById('res_form'), true, 1);
        document.getElementById('client_form').style.display="none";
        document.getElementById('dep_form').style.display="none";
        document.getElementById('res_form').style.display="none";
        document.getElementById('num_form').style.display="block";
      } else {
        switchEnable(document.getElementById('client_form'), true, 1);
        switchEnable(document.getElementById('dep_form'), true, 1);
        switchEnable(document.getElementById('res_form'), false, 1);
        switchEnable(document.getElementById('num_form'), true, 1);
        document.getElementById('client_form').style.display="none";
        document.getElementById('dep_form').style.display="none";
        document.getElementById('res_form').style.display="block";
        document.getElementById('num_form').style.display="none";
      }
    }
    
    function groupSwitch(txt) {

      document.getElementById("client_container").className="block button";
      document.getElementById("dep_container").className="block button";
      document.getElementById("res_container").className="block button";
      document.getElementById("num_container").className="block button";
      document.getElementById(txt+"_container").className='block button blockover';

    }

    function groupRestore() {

      var objclient = document.getElementById('client');
      var objdep = document.getElementById('dep');
      var objres = document.getElementById('res');
      var clientForm = document.getElementById('client_container');
      var depForm = document.getElementById('dep_container');
      var resForm = document.getElementById('res_container');
      var numForm = document.getElementById('num_container');
      if (objclient.checked==true) {
        clientForm.className='block button blockover';
        depForm.className='block button';
        resForm.className='block button';
        numForm.className='block button';
      } else if (objdep.checked==true) {
        clientForm.className='block button ';
        depForm.className='block button blockover';
        resForm.className='block button';
        numForm.className='block button';
      } else if (objres.checked==true) {
        clientForm.className='block button ';
        depForm.className='block button';
        numForm.className='block button';
        resForm.className='block button blockover';
      } else {
        clientForm.className='block button ';
        depForm.className='block button';
        resForm.className='block button';
        numForm.className='block button blockover';
      } 
      

    }
    
    
//------------------------------IVR -----------------------------------------
function showOption(id) {
  var obj=document.getElementById("ivr_option_"+id);
  if (obj!=null) obj.className="ivr_show";
  return true
}
function hideOption(id) {
  var obj=document.getElementById("ivr_option_"+id);
  if (obj!=null) obj.className="ivr_hide";
  return true
}      
function addItem(id,name) {
  var newId=Math.round(Math.random()*1000);
  var NewNode = document.createElement("LI");
  NewNode.id="item"+newId;   
  NewNode.onmouseover = function() {showOption(newId);}   
  NewNode.onmouseout  = function() {hideOption(newId);}
  var txt;
  txt =  "  <a href=\"#\">"+name+"</a>" ;
  txt += "  <div id=\"ivr_option_"+newId+"\" class=\"ivr_hide\">" ;
  txt += "    <a href=\"#\" class=\"admin\" onclick=\"if (newItemName=prompt('Nuova voce','')) addItem("+newId+",newItemName)\">Aggiungi Voce</a>" ;
  txt += "    <a href=\"#\" class=\"admin\" onclick=\"if (newItemName=prompt('Nuovo menu','')) addMenu("+newId+",newItemName)\">Aggiungi Sottomenu</a>" ;
  txt += "    <a href=\".?cmd=del&id="+newId+"\" class=\"admin\" onclick=\"if (confirm('Sicuro di voler cancellare la voce?')) return true; else return false;\">Rimuovi</a>" ;
  txt += "  </div>" ;
  
  NewNode.innerHTML=txt;
  var obj=document.getElementById("item"+id);
  var listObj = obj.parentNode;
  var nextChild=obj.nextSibling;
  listObj.insertBefore(NewNode,nextChild);
  
  return false;
}

function addMenu(id,name) {
  var newId=Math.round(Math.random()*1000);
  var NewNode = document.createElement("LI");

/*
  var NewList = document.createElement("UL");
  var NewListNode = document.createElement("LI");
  NewListNode.onmouseover = function() {showOption(newId);}   
  NewListNode.onmouseout  = function() {hideOption(newId);}
  NewListNode.id="item"+newId;   
  NewList.insertBefore(NewListNode,null);
  NewNode.insertBefore(NewList,null);
*/

  var txt ="";
  txt =  "<ul class=\"LinkList\">";
  txt += " <li onmouseover=\"showOption("+newId+")\" onmouseout=\"hideOption("+newId+")\" id=\"item"+newId+"\">";
  txt += "  <a href=\"#\">"+name+"</a>" ;
  txt += "  <div id=\"ivr_option_"+newId+"\" class=\"ivr_hide\">" ;
  txt += "    <a href=\"#\" class=\"admin\" onclick=\"if (newItemName=prompt('Nuova voce','')) addItem("+newId+",newItemName)\">Aggiungi Voce</a>" ;
  txt += "    <a href=\"#\" class=\"admin\" onclick=\"if (newMenuName=prompt('Nuovo menu','')) addMenu("+newId+",newMenuName)\">Aggiungi Sottomenu</a>" ;
  txt += "    <a href=\".?cmd=del&id="+newId+"\" class=\"admin\" onclick=\"if (confirm('Sicuro di voler cancellare la voce?')) return true; else return false;\">Rimuovi</a>" ;
  txt += "  </div>" ;
  txt += "</li></ul>";
  NewNode.innerHTML=txt;

  var obj=document.getElementById("item"+id);
  var listObj = obj.parentNode;
  var nextChild=obj.nextSibling;
  listObj.insertBefore(NewNode,nextChild);
  
  return false;
}

  function concatSelect(selectObj,textObj) {
    selectArray = new Array();
    selectIndex = 0;
    selectString = '';
    selectLength = selectObj.options.length;
    for(i = 0; i < selectLength; i++)
    {
      if(selectObj.options[i].selected)
      {
        selectArray[selectIndex] = selectObj.options[i].value;
        selectIndex++;
      }
    }
    stringLength = selectArray.length;
    if(stringLength > 0)
    {
      for(j = 0; j < stringLength; j++)
      {
        selectString += (j > 0) ? ';' + selectArray[j] : selectArray[j];
      }
    }
    textObj.value += (textObj.value != '') ? ';' + selectString : selectString;
  }
    
  function help(obj,txt) {
    if (document.getElementById("help")) {
      var help=document.getElementById("help");
      help.innerHTML=txt;
      help.style.display="block";
      obj.onmouseout=function() {
        help.innerHTML="";
        help.style.display="none";
      }
    }
  }
  
  
  /* DUAL LIST */
function compareOptionValues(a, b) 
{ 
  // Radix 10: for numeric values
  // Radix 36: for alphanumeric values
  var sA = parseInt( a.value, 36 );  
  var sB = parseInt( b.value, 36 );  
  return sA - sB;
}

// Compare two options within a list by TEXT
function compareOptionText(a, b) 
{ 
  // Radix 10: for numeric values
  // Radix 36: for alphanumeric values
  var sA = parseInt( a.text, 36 );  
  var sB = parseInt( b.text, 36 );  
  return sA - sB;
}

// Dual list move function
function moveDualList( srcList, destList, moveAll ) 
{
  // Do nothing if nothing is selected
  if (  ( srcList.selectedIndex == -1 ) && ( moveAll == false )   )
  {
    return;
  }

  newDestList = new Array( destList.options.length );

  var len = 0;

  for( len = 0; len < destList.options.length; len++ ) 
  {
    if ( destList.options[ len ] != null )
    {
      newDestList[ len ] = new Option( destList.options[ len ].text, destList.options[ len ].value, destList.options[ len ].defaultSelected, destList.options[ len ].selected );
    }
  }

  for( var i = 0; i < srcList.options.length; i++ ) 
  { 
    if ( srcList.options[i] != null && ( srcList.options[i].selected == true || moveAll ) )
    {
       // Statements to perform if option is selected

       // Incorporate into new list
       newDestList[ len ] = new Option( srcList.options[i].text, srcList.options[i].value, srcList.options[i].defaultSelected, srcList.options[i].selected );
       len++;
    }
  }

  // Sort out the new destination list
  newDestList.sort( compareOptionValues );   // BY VALUES
  //newDestList.sort( compareOptionText );   // BY TEXT

  // Populate the destination with the items from the new array
  for ( var j = 0; j < newDestList.length; j++ ) 
  {
    if ( newDestList[ j ] != null )
    {
      destList.options[ j ] = newDestList[ j ];
    }
  }

  // Erase source list selected elements
  for( var i = srcList.options.length - 1; i >= 0; i-- ) 
  { 
    if ( srcList.options[i] != null && ( srcList.options[i].selected == true || moveAll ) )
    {
       // Erase Source
       //srcList.options[i].value = "";
       //srcList.options[i].text  = "";
       srcList.options[i]       = null;
    }
  }

} // End of moveDualList()
  
  
function switchOnOff(obj,element,id,path) {

  if (obj.className=='ON') {
    obj.innerHTML='<img src=\"'+path+'public/img/checkbox_ok.gif\" alt=\"\" />';
    document.getElementById("on_"+element).className="show";
    document.getElementById("off_"+element).className="hide";
    document.getElementById("help_"+element).className="show";
    document.getElementById("label_"+element).className="required";
    document.getElementById("status_"+id).value="1"; 
    obj.className='OFF';
  } else {
    obj.innerHTML='<img src=\"'+path+'public/img/checkbox.gif\" alt=\"\" />';
    document.getElementById("on_"+element).className="hide";
    document.getElementById("off_"+element).className="show";
    document.getElementById("help_"+element).className="hide";
    document.getElementById("label_"+element).className="commented";
    document.getElementById("status_"+id).value="0";
    obj.className='ON';
  }
  
}  




//functions created for the ZAPATA cards
//author: Martin Kessler

function zapata(id,number,port) {
  name=document.getElementById("\'more"+port+"\'");
  numbernew=number;
  numbernew++;
  //document.write(document.getElementById('ibo').value);
  document.getElementById('more'+id).innerHTML='<table><tr><td>#<input type=text name=channel1['+port+']['+number+'] size=5> - <input type=text name=channel2['+port+']['+number+'] size=5> <select name=type['+port+']['+number+'] onclick=\"Buildkey(this.options[this.options.selectedIndex].value,\''+port+'\',\''+number+'\');\"><option><option value=FXO>FXO<option value=FXS>FXS</select>  <select id=signalling['+port+']['+number+'] name=signalling['+port+']['+number+']><option></select> <select name=xlaw['+port+']['+number+']><option><option>alaw<option>mulaw</select></tr></table><div id=more'+id+1+' ><a href="javascript:" onclick=\"return zapata(\''+id+1+'\',\''+numbernew+'\',\''+port+'\');\">more</a></div>';
  //document.write('<table><tr><td>hi,tesinghere</tr></table><div id=more'+port+' ><a href=# onclick=\"return zapata(\''+number+'\',\''+port+'\');\">more</a></div>');
}

//functions created for the ZAPATA cards
//author: Martin Kessler

function Buildkey(num,id,countr) {
  var ctr=1;
  //document.write(num+id+countr);
  if(num==""){document.getElementById('signalling['+id+']['+countr+']').options[0]=new Option("","");}
  if(num=="FXO"){document.getElementById('signalling['+id+']['+countr+']').options[ctr]=new Option('fxsks','fxsks');ctr=ctr+1;} 
  if(num=="FXO"){document.getElementById('signalling['+id+']['+countr+']').options[ctr]=new Option('fxsgs','fxsgs');ctr=ctr+1;} 
  if(num=="FXO"){document.getElementById('signalling['+id+']['+countr+']').options[ctr]=new Option('fxsls','fxsls');ctr=ctr+1;} 
  if(num=="FXS"){document.getElementById('signalling['+id+']['+countr+']').options[ctr]=new Option('fxoks','fxoks');ctr=ctr+1;} 
  if(num=="FXS"){document.getElementById('signalling['+id+']['+countr+']').options[ctr]=new Option('fxogs','fxogs');ctr=ctr+1;} 
  if(num=="FXS"){document.getElementById('signalling['+id+']['+countr+']').options[ctr]=new Option('fxols','fxols');ctr=ctr+1;} 
  document.getElementById('signalling['+id+']['+countr+']').length=ctr;
  document.getElementById('signalling['+id+']['+countr+']').options[0].selected=true;
}