scripts/config.js
changeset 50 1b4288399b1f
equal deleted inserted replaced
49:d62212462f9b 50:1b4288399b1f
       
     1 var userlist = [];
       
     2 
       
     3 function register_user(username)
       
     4 {
       
     5   userlist.push(username);
       
     6 }
       
     7 
       
     8 function draw_user(ul, username)
       
     9 {
       
    10   var inp = document.createElement('input');
       
    11   inp.type = 'hidden';
       
    12   inp.name = 'users[]';
       
    13   inp.value = username;
       
    14   ul.parentNode.appendChild(inp);
       
    15   var li = document.createElement('li');
       
    16   li.username = username;
       
    17   li.userinput = inp;
       
    18   var delbtn = document.createElement('a');
       
    19   delbtn.style.color = '#ff0000';
       
    20   delbtn.href = '#';
       
    21   delbtn.onclick = function()
       
    22   {
       
    23     delete_user(this);
       
    24     return false;
       
    25   }
       
    26   delbtn.appendChild(document.createTextNode('[X]'));
       
    27   li.appendChild(delbtn);
       
    28   
       
    29   li.appendChild(document.createTextNode(' ' + username));
       
    30   ul.appendChild(li);
       
    31 }
       
    32 
       
    33 function draw_new_user(username, password)
       
    34 {
       
    35   for ( var i = 0; i < userlist.length; i++ )
       
    36   {
       
    37     if ( userlist[i] == username )
       
    38     {
       
    39       alert('The username you entered is already in the list, please delete the current user and re-add it if you want to change the password.');
       
    40       return false;
       
    41     }
       
    42   }
       
    43   
       
    44   userlist.push(username);
       
    45   
       
    46   var ul = document.getElementById('userlist');
       
    47   
       
    48   var inp = document.createElement('input');
       
    49   inp.type = 'hidden';
       
    50   inp.name = 'users_add[' + username + ']';
       
    51   inp.value = password;
       
    52   ul.parentNode.appendChild(inp);
       
    53   var li = document.createElement('li');
       
    54   li.username = username;
       
    55   li.userinput = inp;
       
    56   var delbtn = document.createElement('a');
       
    57   delbtn.style.color = '#ff0000';
       
    58   delbtn.href = '#';
       
    59   delbtn.onclick = function()
       
    60   {
       
    61     delete_user(this);
       
    62     return false;
       
    63   }
       
    64   delbtn.appendChild(document.createTextNode('[X]'));
       
    65   li.appendChild(delbtn);
       
    66   
       
    67   li.appendChild(document.createTextNode(' ' + username));
       
    68   ul.appendChild(li);
       
    69   
       
    70   return true;
       
    71 }
       
    72 
       
    73 function userlist_init()
       
    74 {
       
    75   var ul = document.getElementById('userlist');
       
    76   for ( var i = 0; i < userlist.length; i++ )
       
    77   {
       
    78     draw_user(ul, userlist[i]);
       
    79   }
       
    80 }
       
    81 
       
    82 function delete_user(a)
       
    83 {
       
    84   var li = a.parentNode;
       
    85   var username = li.username;
       
    86   li.parentNode.parentNode.removeChild(li.userinput);
       
    87   li.parentNode.removeChild(li);
       
    88   
       
    89   for ( var i = 0; i < userlist.length; i++ )
       
    90   {
       
    91     if ( userlist[i] == username )
       
    92     {
       
    93       delete(userlist[i]);
       
    94       break;
       
    95     }
       
    96   }
       
    97 }
       
    98 
       
    99 function add_user_form()
       
   100 {
       
   101   var ul = document.getElementById('userlist');
       
   102   
       
   103   if ( ul.parentNode.getElementsByTagName('form').length > 0 )
       
   104   {
       
   105     return false;
       
   106   }
       
   107   
       
   108   var theform = document.createElement('form');
       
   109   theform.action = 'javascript:void(0);';
       
   110   theform.onsubmit = function()
       
   111   {
       
   112     if ( this.username.value == '' || this.password.value == '' )
       
   113     {
       
   114       alert('Please enter a username and password.');
       
   115       return false;
       
   116     }
       
   117     if ( draw_new_user(this.username.value, this.password.value) )
       
   118     {
       
   119       this.parentNode.removeChild(this);
       
   120     }
       
   121     else
       
   122     {
       
   123       this.username.focus();
       
   124     }
       
   125     
       
   126     return false;
       
   127   }
       
   128   
       
   129   theform.appendChild(document.createTextNode('user: '));
       
   130   
       
   131   var i_user = document.createElement('input');
       
   132   i_user.type = 'text';
       
   133   i_user.name = 'username';
       
   134   i_user.size = '12';
       
   135   theform.appendChild(i_user);
       
   136   
       
   137   theform.appendChild(document.createTextNode(' pass: '));
       
   138   
       
   139   var i_pass = document.createElement('input');
       
   140   i_pass.type = 'password';
       
   141   i_pass.name = 'password';
       
   142   i_pass.size = '12';
       
   143   theform.appendChild(i_pass);
       
   144   
       
   145   theform.appendChild(document.createTextNode(' '));
       
   146   
       
   147   var i_sub = document.createElement('input');
       
   148   i_sub.type = 'submit';
       
   149   i_sub.value = 'Add';
       
   150   theform.appendChild(i_sub);
       
   151   
       
   152   ul.parentNode.appendChild(theform);
       
   153   
       
   154   i_user.focus();
       
   155 }
       
   156 
       
   157 window.onload = userlist_init;