/**************************************************************************/
/*                                                                        */
/* Copyright (c) 2005,2006 NoMachine, http://www.nomachine.com.           */
/*                                                                        */
/* NXBUILDER, NX protocol compression and NX extensions to this software  */
/* are copyright of NoMachine. Redistribution and use of the present      */
/* software is allowed according to terms specified in the file LICENSE   */
/* which comes in the source distribution.                                */
/*                                                                        */
/* Check http://www.nomachine.com/licensing.html for applicability.       */
/*                                                                        */
/* NX and NoMachine are trademarks of Medialogic S.p.A.                   */
/*                                                                        */
/* All rigths reserved.                                                   */
/*                                                                        */
/**************************************************************************/

//
// Check the site from which to download the NX Client in relation to the O.S.
//
function siteDownload()
{
  if(navigator.userAgent.toLowerCase().indexOf("windows") != -1)
    this.document.getElementById('NoMachineLink').href = 'http://www.nomachine.com/download_client_windows.php';
  else if((navigator.userAgent.toLowerCase().indexOf("linux") != -1) || (navigator.userAgent.indexOf("Unix") != -1))
    this.document.getElementById('NoMachineLink').href = 'http://www.nomachine.com/download_client_linux.php';
  else if(navigator.userAgent.toLowerCase().indexOf("mac") != -1)
    this.document.getElementById('NoMachineLink').href = 'http://www.nomachine.com/download_client_macosx.php';
  else if ((navigator.userAgent.toLowerCase().indexOf("solaris") != -1) || (navigator.userAgent.toLowerCase().indexOf("sunos") != -1))
    this.document.getElementById('NoMachineLink').href = 'http://www.nomachine.com/download_client_solaris.php';
}

function disableLogin()
{
  if(this.document.Login.userName.disabled==false)
  {
    this.document.Login.userName.disabled=true;
    this.document.Login.password.disabled=true; 
    this.document.getElementById('nameText').style.color='#A1A5A9';
    this.document.getElementById('pwdText').style.color='#A1A5A9';     
  }
  else
  {
    this.document.Login.userName.disabled=false;
    this.document.Login.password.disabled=false;      
    this.document.getElementById('nameText').style.color='#514E53';
    this.document.getElementById('pwdText').style.color='#514E53';     
  }
}

//
// These functions realize the passage of options between the two select
// in add and modify user procedures.
//
function moveAllOptions(from,to) 
{
  selectAllOptions(from);
  if (arguments.length==2) 
  {
    moveSelectedOptions(from,to);
  }
  else if (arguments.length==3) 
  {
    moveSelectedOptions(from,to,arguments[2]);
  }
  else if (arguments.length==4) 
  {
    moveSelectedOptions(from,to,arguments[2],arguments[3]);
  }
}

function moveAllOptions1(from,to) 
{
  selectAllOptions(from);
  if (arguments.length==2) 
  {
    moveSelectedOptions1(from,to);
  }
  else if (arguments.length==3) 
  {
    moveSelectedOptions1(from,to,arguments[2]);
  }
  else if (arguments.length==4) 
  {
    moveSelectedOptions1(from,to,arguments[2],arguments[3]);
  }
}


function selectAllOptions(obj) 
{
  if (!hasOptions(obj)) { return; }
  for (var i=0; i<obj.options.length; i++) 
  {
    obj.options[i].selected = true;
  }
}

function sortSelect(obj) 
{
  var o = new Array();
  if (!hasOptions(obj)) { return; }
  for (var i=0; i<obj.options.length; i++) 
  {
    o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;
  }
  if (o.length==0) { return; }
  o = o.sort(
    function(a,b) {
      if ((a.text+"") < (b.text+"")) { return -1; }
      if ((a.text+"") > (b.text+"")) { return 1; }
      return 0;
      }
    );

  for (var i=0; i<o.length; i++) 
  {
    obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
  }
}

function hasOptions(obj) 
{
  if (obj!=null && obj.options!=null) { return true; }
  return false;
}

function moveSelectedOptions(from,to) 
{
  if (!hasOptions(from)) {return;}
  for (var i=0; i<from.options.length; i++) 
  {
    var o = from.options[i];
    if (o.selected) 
    {
      if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
      to.options[index] = new Option( o.text, o.value,true);
    }
  }
  // Delete them from original
  for (var i=(from.options.length-1); i>=0; i--) 
  {
    var o = from.options[i];
    if (o.selected) 
    {
      from.options[i] = null;
    }
  }
  if((from.options.length)==0)
  {
    this.document.create.add.disabled=true;
    this.document.create.allAdd.disabled=true;
    this.document.create.allAdd.style.backgroundImage='url(images/buttons/b_addAllDsb.gif)';
    this.document.create.add.style.backgroundImage='url(images/buttons/b_addPartialDsb.gif)';
  }
  if((from.options.length)>=0)
  {
    this.document.create.allRemove.style.backgroundImage='url(images/buttons/b_removeAll.gif)';
    this.document.create.remove.style.backgroundImage='url(images/buttons/b_removePartial.gif)';
    this.document.create.remove.disabled=false;
    this.document.create.allRemove.disabled=false;
  }

  sortSelect(from);
  sortSelect(to);
    
  from.selectedIndex = -1;
  to.selectedIndex = -1;
}

function moveSelectedOptions1(from,to) 
{
  // Move them over
  if (!hasOptions(from)) {return;}
  for (var i=0; i<from.options.length; i++) 
  {
    var o = from.options[i];
    if (o.selected) 
    {
      if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
      to.options[index] = new Option( o.text, o.value,true);
    }
  }
  // Delete them from original
  for (var i=(from.options.length-1); i>=0; i--) 
  {
    var o = from.options[i];
    if (o.selected) 
    {
      from.options[i] = null;
    }
  }
  if((from.options.length)==0)
  {
    this.document.create.remove.disabled=true;
    this.document.create.allRemove.disabled=true;
    this.document.create.allRemove.style.backgroundImage='url(images/buttons/b_removeAllDsb.gif)';
    this.document.create.remove.style.backgroundImage='url(images/buttons/b_removePartialDsb.gif)';
  }
  if((from.options.length)>=0)
  {
    this.document.create.allAdd.style.backgroundImage='url(images/buttons/b_addAll.gif)';
    this.document.create.add.style.backgroundImage='url(images/buttons/b_addPartial.gif)';
    this.document.create.add.disabled=false;
    this.document.create.allAdd.disabled=false;
  }
  sortSelect(from);
  sortSelect(to);
  from.selectedIndex = -1;
  to.selectedIndex = -1;
}

//
// These functions visualizes or hides some fields in AddServer and
// ModifyServer. 
//
  
function enableRDP() 
{
  if(this.document.addServer.serverRDP.disabled == true)
  {
    this.document.getElementById('rdpText').style.color='#514E53';
    this.document.addServer.serverRDP.disabled=false; 
  }
  else
  {
    this.document.getElementById('rdpText').style.color='#A1A5A9';
    this.document.addServer.serverRDP.disabled=true; 
  }
}
  
function enableVNC() 
{
  if(this.document.addServer.serverVNC.disabled == true)
  {
    this.document.getElementById('vncText').style.color='#514E53';
    this.document.getElementById('vncText1').style.color='#514E53';
    this.document.getElementById('vncText2').style.color='#514E53';
    this.document.addServer.serverVNC.disabled=false;
    this.document.addServer.displayVNC.disabled=false;
    this.document.addServer.passwordVNC.disabled=false;
  } 
  else
  {
    this.document.getElementById('vncText').style.color='#A1A5A9';
    this.document.getElementById('vncText1').style.color='#A1A5A9';
    this.document.getElementById('vncText2').style.color='#A1A5A9';
    this.document.addServer.serverVNC.disabled=true;
    this.document.addServer.displayVNC.disabled=true;
    this.document.addServer.passwordVNC.disabled=true;
  }
}

function enableCustom() 
{
  if (this.document.addServer.command.disabled == true)
  {
    this.document.addServer.delCommand.style.backgroundImage='url(images/buttons/b_delCommand.gif)';
    this.document.addServer.delCommand.disabled=false;  
    this.document.addServer.addCommand.style.backgroundImage='url(images/buttons/b_addCommand.gif)';
    this.document.addServer.addCommand.disabled=false;  
    this.document.getElementById('selectCommand').disabled=false;  
    this.document.addServer.command.disabled=false;
    this.document.getElementById('commandText').style.color='#514E53';
  }
  else
  {
    this.document.addServer.delCommand.style.backgroundImage='url(images/buttons/b_delCommandDsb.gif)';
    this.document.addServer.delCommand.disabled=true;  
    this.document.addServer.addCommand.style.backgroundImage='url(images/buttons/b_addCommandDsb.gif)';
    this.document.addServer.addCommand.disabled=true;  
    this.document.getElementById('selectCommand').disabled=true;  
    this.document.addServer.command.disabled=true;
    this.document.getElementById('commandText').style.color='#A1A5A9';
  }
}
  
function initiateVNC()
{
this.document.getElementById('vncText').style.color='#514E53';
    this.document.getElementById('vncText1').style.color='#514E53';
    this.document.getElementById('vncText2').style.color='#514E53';
    this.document.addServer.serverVNC.disabled=false;
    this.document.addServer.displayVNC.disabled=false;
    this.document.addServer.passwordVNC.disabled=false;
}

function initiateRDP()
{
  this.document.getElementById('rdpText').style.color='#514E53';
    this.document.addServer.serverRDP.disabled=false; 
}
function initiateCustom()
{
  this.document.addServer.delCommand.style.backgroundImage='url(images/buttons/b_delCommand.gif)';
  this.document.addServer.delCommand.disabled=false;  
  this.document.addServer.addCommand.style.backgroundImage='url(images/buttons/b_addCommand.gif)';
  this.document.addServer.addCommand.disabled=false;  
  this.document.getElementById('selectCommand').disabled=false;  
  this.document.addServer.command.disabled=false;
  this.document.getElementById('commandText').style.color='#514E53';
} 
function initiate()
{
  // disable CUSTOM
  if (this.document.addServer.command.disabled==false)
  {
  this.document.addServer.delCommand.style.backgroundImage='url(images/buttons/b_delCommandDsb.gif)';
  this.document.addServer.delCommand.disabled=true;  
  this.document.addServer.addCommand.style.backgroundImage='url(images/buttons/b_addCommandDsb.gif)';
  this.document.addServer.addCommand.disabled=true;  
  this.document.getElementById('selectCommand').disabled=true;  
  this.document.addServer.command.disabled=true;
  this.document.getElementById('commandText').style.color='#A1A5A9';
  }
  // disable RDP
  if (this.document.addServer.serverRDP.disabled==false)
  {
  this.document.getElementById('rdpText').style.color='#A1A5A9';
  this.document.addServer.serverRDP.disabled=true; 
  }
  // disable VNC
  if (this.document.addServer.serverVNC.disabled==false)
  {
  this.document.getElementById('vncText').style.color='#A1A5A9';
  this.document.getElementById('vncText1').style.color='#A1A5A9';
  this.document.getElementById('vncText2').style.color='#A1A5A9';
  this.document.addServer.serverVNC.disabled=true;
  this.document.addServer.displayVNC.disabled=true;
  this.document.addServer.passwordVNC.disabled=true; 
  }
}
function initiate_start()
{
  this.document.getElementById('sessionRDP').style.display = 'none';
  this.document.getElementById('sessionVNC').style.display = 'none';
  this.document.getElementById('sessionCustom').style.display = 'none';
} 

//
// This function create an input dinamic.
//

function createOption()
{
  if (document.getElementById('valueCustom').value != '')
  {
    var sel=document.getElementById('selectCommand');    
    var val=this.document.getElementById('valueCustom').value;
    this.document.getElementById('valueCustom').value='';
    sel.options[sel.options.length]= new Option (val, val, false);
    this.document.getElementById('valueCustom').focus()
  }
  if (sel.options.length != 0)
  {
    this.document.addServer.delCommand.style.backgroundImage='url(images/buttons/b_delCommand.gif)';
    this.document.addServer.delCommand.disabled=false;   
  }
}

function deleteOption()
{
  var sel=document.getElementById('selectCommand');
  for (var i=sel.options.length-1; i>=0; i--) 
  {
    var o = sel.options[i];
    if (o.selected) 
      sel.remove(i);
  }
  if (sel.options.length == 0)
  {
    this.document.addServer.delCommand.style.backgroundImage='url(images/buttons/b_delCommandDsb.gif)';
    this.document.addServer.delCommand.disabled=true;  
  } 
}

function showInfo(divId)
{
  if (divId.style.display=='block') 
    divId.style.display='none';
  else
    divId.style.display='block';
}

function displayAdministrator()
{
  if(this.document.getElementById('admin').style.display == 'block'){ 
    this.document.getElementById('admin').style.display = 'none'; 
    this.document.getElementById('serverAvailable').style.display = 'none'; 
  }
  else
  { 
    this.document.getElementById('serverAvailable').style.display = 'block';
    this.document.getElementById('admin').style.display = 'block';
  }
}

function initiateAddUser()
{
  this.document.getElementById('serverAvailable').style.display = 'block';
  this.document.getElementById('admin').style.display = 'block';
}

function initiateModifyAdministrator()
{
  this.document.getElementById('serverAvailable').style.display = 'none'; 
  this.document.getElementById('admin').style.display = 'none';
}

function displayServerLabel()
{
  this.document.getElementById('serverLabel').style.display = 'none';
  this.document.getElementById('availableServerLabel').style.display = 'none';
}
//
// These functions realize the passage of options between the three
// select in add and modify guest procedures.
//
function showSessions(server)
{
  val=server.split("-");
  server=val[0];
  if (val[1]!='')
    icon=val[1];
  else 
    icon='imgEmpty.gif';  
  this.document.getElementById('serverIcon').src="images/shared/builder/imgEmpty.gif";
  l=this.document.guest.length;
  for(i=0;i<l;i++)
  {
    if(this.document.guest.elements[i].name.indexOf("sessionServer-") != -1)
  
        if (this.document.getElementById(this.document.guest.elements[i].name).style.display == 'block')
           this.document.getElementById(this.document.guest.elements[i].name).style.display='none';
  }
  this.document.getElementById('sessionServer-'+server).style.display='block';
  this.document.getElementById('serverIcon').src="images/shared/servers/"+icon;
  
}

function moveSelectedSessions(from,to) 
{
  val=from.name.split("-");
    
  if (!hasOptions(from)) {return;}
  for (var i=0; i<from.options.length; i++) 
  {
    var o = from.options[i];
    if (o.selected) 
    {
      if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
      server=o.value.split("-");
      text=server[0]+"-"+o.text;
      val2=o.value+"-"+val[1];   
      to.options[index] = new Option( text, val2,true);
    }
  }
  for (var i=(from.options.length-1); i>=0; i--) 
  {
    var o = from.options[i];
    if (o.selected) 
    {
      from.options[i] = null;
    }
  }
  sortSelect(to);
  
  from.selectedIndex = -1;
  to.selectedIndex = -1;
}

function moveAllSessions(from,to) 
{
  selectAllOptions(from);
  moveSelectedSessions(from,to);
}
function removeSelectedSessions(from)
{
  if (!hasOptions(from)) {return;}
  for (var i=0; i<from.options.length; i++) 
  {
    var o = from.options[i];
    if (o.selected) 
    {
      val=o.value.split("-");
      to=this.document.getElementById('sessionServerid-'+val[2]);
      if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
      text=o.text.split("-");
      val2=val[0]+"-"+val[1];   
      to.options[index] = new Option( text[1], val2,true);
    }
  }
  for (var i=(from.options.length-1); i>=0; i--) 
  {
    var o = from.options[i];
    if (o.selected) 
    {
      from.options[i] = null;
    }
  }
}

function removeAllSessions(from) 
{
  selectAllOptions(from);
  removeSelectedSessions(from);
}

function showSessionIcon(icon)
{
  var id=this.document.guest.idUpload.value;
  this.document.getElementById('icon'+id).src='images/shared/sessions/'+icon;
}

function showSessionDetails(session)
{
  this.document.guest.icon.disabled=false;
  this.document.getElementById('uploadFileIcon').disabled=false;
  this.document.guest.file1.disabled=false;
  l=this.document.guest.length;
  details=session.split('-');
  for(i=0;i<l;i++)
  {
    if(this.document.guest.elements[i].name.indexOf("iconSession") != -1)
    {
      divName=this.document.guest.elements[i].name;
      if (this.document.getElementById(divName).style.display == 'block')
        this.document.getElementById(this.document.guest.elements[i].name).style.display='none';
    }
  }
  this.document.getElementById('iconSessionEmpty').style.display='none';
  icon=this.document.guest.icon;
  image=this.document.getElementById('hiddenIcon'+details[1]).value;
  if(image == '')
    image='empty.gif';
  l=icon.length;
  for(i=0;i<l;i++)
  {
    if (icon.options[i].value==image)
    {
      icon.options[i].selected=true;
    } 
  }
  
  sessionName=this.document.getElementById('nameSession'+details[1]).value.split('-');
  app=sessionName[1].split('/'); 
  this.document.getElementById('sessionTitle').innerHTML="Session: "+sessionName[0]+"-"+app[(app.length)-1];
  this.document.getElementById('iconSession'+details[1]).style.display='block';
  this.document.guest.idUpload.value=details[1];
  this.document.getElementById('b_browse').src="images/buttons/b_browse.gif";
}

function showIcon(idSession)
{
  this.document.guest.icon.disabled=false;
  this.document.getElementById('uploadFileIcon').disabled=false;
  this.document.guest.file1.disabled=false;
  
  sessionName=this.document.getElementById('nameSession'+idSession).value.split('-');
  app=sessionName[1].split('/'); 
  this.document.getElementById('sessionTitle').innerHTML="Session: "+sessionName[0]+"-"+app[(app.length)-1];
  
  icon=this.document.guest.icon;
  image=this.document.getElementById('hiddenIcon'+idSession).value;
  if(image == '')
    image='empty.gif';
  l=icon.length;
  for(i=0;i<l;i++)
  {
    if (icon.options[i].value==image)
    {
      icon.options[i].selected=true;
    } 
  }
  this.document.getElementById('iconSessionEmpty').style.display='none';
  this.document.getElementById('iconSession'+idSession).style.display='block';
  this.document.getElementById('b_browse').src="images/buttons/b_browse.gif";
}

function disableResolution(radioRes)
{
  l=radioRes.length;
  for(i=0;i<l;i++)
  {
    radioRes[i].disabled=true;  
  }
}

function enableResolution(radioRes)
{
  l=radioRes.length;
  for(i=0;i<l;i++)
  {
    radioRes[i].disabled=false;  
  }
}

function showServerIcon(icon)
{
  this.document.getElementById('serverIcon').src='images/shared/servers/'+icon;
  this.document.getElementById('image').value='images/shared/servers/'+icon;
}

