var tm,hml,cr;

function stc(name, value, expires, path, domain, secure) {
  try {
    var today = new Date();
    today.setTime( today.getTime() );
    if ( expires ) expires = expires * 1000;
    var expires_date = new Date( today.getTime() + (expires) );
    document.cookie = name + "=" +escape( value ) + ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + ( ( path ) ? ";path=" + path : "" ) + ( ( domain ) ? ";domain=" + domain : "" ) + ( ( secure ) ? ";secure" : "" );
  } catch (e) { }
}

function snd() {
  try {
    var ob = document.getElementById('f');
    if (ob.value) {
      var tp = ob.value.substring((ob.value.length-3),ob.value.length).toLowerCase();
      if (inr(tp,new Array('jpg','gif','png'))) {
        return true;
      }
    }
    tmo('<div class="e">Click on "Browse" to find your file</div>', true);
  } catch (e) { }
  return false;
}

function inr(needle, haystack, strict) {
  var found = false, key, strict = !!strict;
  for (key in haystack) {
    if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
      return true;
    }
  } 
  return false;
}

function tmo(msg,t) {
  try {
    if (msg) {
      ghi(msg);
      if (t) {
        if (tm)
          clearTimeout(tm);
        tm = setTimeout("ghi(null);",7000);
        cr = true;
      } else {
        cr = false;
      }
    }
  } catch (e) { }
}

function upl() {
  try {
    if (snd()) {
      document.getElementById('r').submit();
    }
  } catch (e) { }
  return false;
}

function opt() {
  try {
    var ee = document.getElementById('i');
    var ob = document.getElementById('f');
    var tp = ob.value.substring((ob.value.length-3),ob.value.length).toLowerCase();
    if (!inr(tp,new Array('jpg','gif','png'))) {
      exc();
      tmo('<div class="e">You can only upload jpg, gif and png-files</div>', true);
    } else {
      ghi(null);
    }
  } catch (e) { }
  return false;
}

function ghi(err) {
  try {
    var gh = document.getElementById('g');
    if (!(!err && !cr))
      gh.innerHTML = (err) ? err : '';
  } catch (e) { }
}

function exc() {
  try {
    document.getElementById('i').innerHTML = '<input class="t" onclick="ghi(null);" onkeypress="return false;" onpaste="return false;" onkeydown="return false;" onchange="opt();" type="file" id="f" name="f" title="Browse for an image to upload.." />';
  } catch (e) { }
}

function redir(st) {
  try {
    rdirectvljs = st;
    var nv = (st) ? '1' : '0';
    document.getElementById('rdirectvl').value = nv;
    stc('rdirectvl', nv, max_expires, '/', 'dingfiles.com', false);
  } catch (e) { }
}

function options() {
  try {
    hml  = '<div class="o">';
    hml += '  <ul>';
    hml += '    <li class="chb"><input onclick="redir(this.checked);" onfocus="this.blur();" type="checkbox" id="redr" /></li>';
    hml += '    <li class="chv"><label for="redr">Redirect me immediately to my file</label></li>';
    hml += '  </ul>';
    hml += '</div>';
    tmo(hml, false);
    document.getElementById('redr').checked = rdirectvljs;
  } catch (e) { }
  return false;
}

function phmml() {
  try {
    hml  = '<form onsubmit="return snd();" method="post" enctype="multipart/form-data" id="r" action="/bin/_upload.php">';
    hml += '  <input type="hidden" name="xc" id="e" />';
    hml += '  <input type="hidden" name="rd" id="rdirectvl" />';
    hml += '  <div class="t">Select an image to upload..</div>';
    hml += '  <div id="i"></div>';
    hml += '  <div class="o">';
    hml += '    <ul>';
    hml += '      <li><a onfocus="this.blur();" onclick="return options();" title="Make your options for your upload" href="/">+ Options</a></li>';
    hml += '      <li class="z">&nbsp;&nbsp;&nbsp;&#149;&nbsp;&nbsp;&nbsp;</li>';
    hml += '      <li class="u"><a onfocus="this.blur();" title="Upload your file to Dingfiles" onclick="return upl();" href="/bin/_upload.php">&#155;&#155; Upload file</a></li>';
    hml += '      <li class="z">&nbsp;&nbsp;&nbsp;&#149;&nbsp;&nbsp;&nbsp;</li>';
    hml += '      <li class="m">(maximum <b>3 MB</b>)</li>';
    hml += '    </ul>';
    hml += '    <ul>';
    hml += '      <li id="g"></li>';
    hml += '    </ul>';
    hml += '  </div>';
    hml += '</form>';
    hml += '<div class="o"></div>';
    document.getElementById('y').innerHTML = hml;
    document.getElementById('e').value = xck;
  } catch (e) { }
}

function pInit() {
  try {
    document.getElementById('y').className = '';
    document.getElementById('u').className = 'd';
    phmml();
    exc();
  } catch (e) { }
}