function moreForm(par,dru)
{
  ins = document.getElementById('inputs');
  var con = document.createElement('span');
  if(par == 'mk')
  {
    con.innerHTML = 'New directory name <input type="text" name="a[]" value="" autocomplete="off" />.<br />'; 
  }
  else if(par == 'up')
  {
    con.innerHTML = 'Upload file <input type="file" name="a[]" /><br />';
  }
  else return false;
  ins.appendChild(con);
  return true;
}
function disable(show)
{
  button = document.getElementById('submit').getElementsByTagName('input');
  for(i=0;i<button.length;i++)
    if(button[i].type=='submit' || button[i].type=='button') button[i].disabled = 'disabled';
  if(show) disp();
  return true;
}
function disp()
{
  try
  {
    var load = document.getElementById('load');
    load.style.display = 'block';
  }
  catch(e){}
  return true;
}
function realStripSlash(obj)
{
  obj.value = obj.value.replace('/','');
  obj.value = obj.value.replace('\\','');
  obj.value = obj.value.replace('|','');
}
var visible = false;
var log = false;
function fixTarget()
{
  document.getElementById('form').target = 'iframe';
  return true;
}
function setParams(secret,path)
{
  if(secret && document.getElementById('secret')) document.getElementById('secret').value = parent.document.getElementById('secret').value;
  if(path && document.getElementById('path')) document.getElementById('path').value = '.' + parent.document.getElementById('path').innerHTML;
  return true;
}
function checkPasswd()
{
  var input = document.getElementById('passwd').value;
  document.getElementById('passwd').value = '';
  var num = input[0];
  var base = hex_sha1(input.substr(1,num));
  if(base.substr(10,20) != 'a99cff8c02e681f43289' || base[num] != '8' || input.substr(input.length-1) != '8') return false;
  log = true;
  document.getElementById('secret').value = hex_sha1(input);
  var protect = '';
  if(document.getElementById('path').innerHTML != '/') protect = '<a href="#" class="option" onclick="submit(\'safe\')">protect</a>';
  var panel = document.getElementById('control');
  panel.innerHTML = '<a href="#" class="option" onclick="logout()">logout</a>' +
                    '<a href="#" class="option" onclick="submit(\'unzip\')">unzip</a>' +
                    '<a href="#" class="option" onclick="submit(\'zip\')">zip</a>' + protect +
                    '<a href="#" class="option" onclick="submit(\'mkdir\')">mkdir</a>' +
                    '<a href="#" class="option" onclick="submit(\'del\')">delete</a>' +
                    '<a href="#" class="option" onclick="submit(\'pas\')">paste</a>' +
                    '<a href="#" class="option" onclick="submit(\'cp\')">copy</a>' +
                    '<a href="#" class="option" onclick="submit(\'mv\')">rename</a>' +
                    '<a href="#" class="option" onclick="submit(\'up\')">upload</a>';
  panel = document.getElementById('choice');
  panel.innerHTML = '<a href="#" class="option" onclick="reload(true)">reload</a>' + 
                    '<a href="#" class="option" onclick="select(\'all\')">select all</a>' +
                    '<a href="#" class="option" onclick="select(\'none\')">select none</a>' +
                    '<a href="#" class="option" onclick="select(\'inv\')">invert selection</a>';                  
  return reload(log);                         
}
function logout()
{
  log = false;
  document.getElementById('secret').value = 'n';
  var panel = document.getElementById('control');
  panel.innerHTML = '<span class="option">Administration password: <input type="password" name="passwd" id="passwd" onkeypress="enterKey(event)" value="" /></span>';
  panel = document.getElementById('choice');
  panel.innerHTML = '<a href="#" class="option" onclick="reload(false)">Reload</a>';
  return reload(log);
}
function enterKey(e)
{
  if(e.keyCode == 13)
  return checkPasswd();
  return true;
}
function escapeKey(e)
{
  if(e.keyCode == 27)
  return hide();
  return true;
}
function form()
{
  if(visible) hide();
  var controller = document.createElement('div');
  controller.id = 'controller';
  var content = document.createElement('div');
  content.id = 'frame';
  content.className = 'frame';
  var target = 'about:blank'; 
  content.innerHTML = '<iframe src="'+ target +'" name="iframe" width="400" height="275" frameborder="0">Your browser does not support iframes!</iframe>' + '<div id="framecontrol"><span><a href="#" onclick="hide(true)">Reload</a> &nbsp;&nbsp;&nbsp; <a href="#" onclick="hide(false)">Close</a></span></div>';
  var top = (height()-300)/2;
  var left = (width()-400)/2;
  content.style.width = '400px';
  content.style.height = '300px';
  content.style.top = top + 'px';
  content.style.left = left + 'px';
  var bg = document.createElement('div');
  bg.className = 'bg';
  bg.style.height = heightScroll() + 'px';
  controller.appendChild(bg);
  controller.appendChild(content);
  var alpha = '0.5';
  bg.style.filter = "alpha(opacity=" + (100*alpha) + ")";
  bg.style.opacity = alpha;
  bg.style["-khtml-opacity"] = alpha;
  body = document.getElementsByTagName('body');
  body[0].appendChild(controller);
  visible = true;
  return true;
}
function urlencode(str)
{
  return escape(str).replace(/\+/g,'%2B').replace(/%20/g,'+').replace(/\*/g,'%2A').replace(/\//g,'%2F').replace(/@/g,'%40');
}
function genParam(action)
{
  if(action == 'up' || action == 'cp' || action == 'pas' || action == 'del' || action == 'mv' || action == 'mkdir' || action == 'safe' || action == 'zip' || action == 'unzip')
    return '/run/confirm.php?a=' + action;
  return '/bug.htm';
}
function submit(action)
{
  if(!form()) return false;
  var formulary = document.getElementById('form');
  formulary.action = genParam(action);
  formulary.submit();
  return formulary.action = '#';
}
function hide(par)
{
  var co = document.getElementById('controller');
  body = document.getElementsByTagName('body');
  body[0].removeChild(co);
  visible = false;
  if(par) reload(log);
}
function heightScroll(){
  if (window.innerHeight && window.scrollMaxY) yWithScroll = window.innerHeight + window.scrollMaxY; 
  else if (document.body.scrollHeight > document.body.offsetHeight) yWithScroll = document.body.scrollHeight; 
  else yWithScroll = document.body.offsetHeight; 
  return yWithScroll;
}
function height()
{
  if (window.innerHeight) return window.innerHeight;
  else if (document.documentElement && document.documentElement.clientHeight) return document.documentElement.clientHeight;
  else if (document.body && document.body.clientHeight) return document.body.clientHeight;
  else return null;
}
function width()
{
  if (window.innerWidth) return window.innerWidth;
  else if (document.documentElement && document.documentElement.clientWidth) return document.documentElement.clientWidth;
  else if (document.body && document.body.clientWidth) return document.body.clientWidth;
  else return null;
}
function select(kind)
{
  if(kind != 'all' && kind != 'none' && kind != 'inv') return false;
  list = document.getElementById('list').getElementsByTagName('input');
  for(i=0;i<list.length;i++)
    if(list[i].type=='checkbox')
      if(kind == 'all') list[i].checked = 'checked';
      else if(kind == 'none') list[i].checked = '';
      else if(kind == 'inv')
        if(list[i].checked == '') list[i].checked = 'checked';
        else list[i].checked = '';
  return true;
}
var xmlHttp = createXmlHttpRequestObject();
var progress = false;
function createXmlHttpRequestObject()
{
  var xmlHttp;
  try
  {
    xmlHttp = new XMLHttpRequest();
  }
  catch (e)
  {
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.5.0","MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP");
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
    {
      try
      {
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      }
      catch(e){}
    }
  }
  if(!xmlHttp) alert("Error Creating the XMLHttpRequest Object.");
  else return xmlHttp;
}
function reload(all)
{
  if(progress) return; 
  if(xmlHttp)
  {
    try
    {
      var param = 'without';
      if(all) param = 'with';
      setProgress(true);
      xmlHttp.open("GET", 'index.php?' + param, true);
      xmlHttp.onreadystatechange = handleRequestReload;
      xmlHttp.send(null);
    }
    catch(e)
    {
       alert("Cannot connect to server.");
    }
  }
  return true;
}
function handleRequestReload()
{
  var list = document.getElementById('list');
  if(xmlHttp.readyState == 4)
  {
    if(xmlHttp.status == 200)
    {
      try
      {
        list.innerHTML = xmlHttp.responseText;
        setProgress(false);
      }
      catch(e)
      {
        alert("Error reading the response.");
      }
    }
    else
    {
      alert("There was a problem retrieving the data.");
    }
  }
}
function setProgress(st) 
{
  if(st)
  {
    progress = true;
    document.getElementById('header').className = 'load';
    document.getElementById('footer').className = 'load';
    return true;
  }
  progress = false;
  document.getElementById('header').className = '';
  document.getElementById('footer').className = '';
  return false;
}
function mouseOn(obj)
{
  obj.style.borderColor = '#38789e';
}
function mouseOut(obj)
{
  obj.style.borderColor = '#ccc';
}
