function noenter() 
{
  return !(window.event && window.event.keyCode == 13); 
}

function send_xmlhttprequest(state_change, method, url, content, headers) {
    var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest() : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));
    if (!xmlhttp) {
        return false;
    }
    xmlhttp.open(method, url);
    xmlhttp.onreadystatechange = function () {
        state_change(xmlhttp);
    };
    headers = headers || {};
    headers['X-Requested-With'] = headers['X-Requested-With'] || 'XMLHttpRequest';
    for (var key in headers) {
        xmlhttp.setRequestHeader(key, headers[key]);
    }
    xmlhttp.send(content);
    return true;
}

function progress_update(xmlhttp) {
    if (xmlhttp.readyState == 4) {
        alert(xmlhttp.responseText);
        //$('#progress').text(xmlhttp.responseText);
        //document.getElementById('progress').innerHTML = xmlhttp.responseText;
        var a = xmlhttp.responseText;
        var begin = a.indexOf("Progress: ");
        var end = a.indexOf("%", begin);
        var percent = a.substring(begin, end);
        document.title = percent + "% - uploading... | Quickbox";
        //window.setTimeout(progress_send, 1000);
    }
}

function progress_send(id) {
//id = {/literal}'{$id}';{literal}
//document.getElementById('progress').innerHTML = id;
    $.ajax({
      type: "GET",
      url: '?',
      data: ({action:"info", id:id }),
      dataType: "html",
      cache: false,
      async: false,
      success: function(html){
       // alert(html);
        $("#progress").html(html);
        
        var a = html;
        var begin = a.indexOf("Progress: ");
        var end = a.indexOf("%", begin);
        var percent = a.substring(begin, end);
        document.title = percent + "% - uploading... | Quickbox";
      }
    });
    //send_xmlhttprequest(progress_update, 'GET', '?action=info&id='+id);
}
/*
function submit(form)
{
    $(form).closest('form').submit();
}

function send_form(id, form)
{
   // alert('progress_send(\''+id+'\')');    
    //progress_send(id);
    var inter = window.setInterval('progress_send(\''+id+'\')',1000);
    window.setTimeout(function(){submit(form);},1300);
}
*/
function addinput(form)
{
    var parent=document.getElementById(form);
    var p=document.createElement('p');
    var input=document.createElement('input');
    var a=document.createElement('a');
    input.type='file';
    input.name='file[]';
    a.innerHTML='aaa';
    a.href='#';
    //a.onclick='popup(\'del\');';
    //p.appendChild(input);
    //p.appendChild(a);
    p.innerHTML = '<a href="#" onclick="removeinput(this);return false;"><img class="dropfile" src="/image/dropfile.png" /></a> <input class="uploadfile" type="file" name="file[]" size="60" />';
    parent.appendChild(p);
}

$(function()
{
    $("#notitle").bind('change',function(){
        var a = 0;
        $("#realtitle").val($("#notitle").val());
    });
});

function checkall(formid, bCheck)
{
    var form=document.getElementById(formid);
    for (var i = 0; i < form.elements.length; i++)
    {
        var el = form.elements[i];
        if(el.type=="checkbox" && (el.name=="dir[]" || el.name=="file[]"))
            el.checked=bCheck;
    }
}

function dirclick(link,def)
{
    var l = $(link);
    if (l.data('opened') == undefined)
    {
        l.data('opened',def);
    }
    var tr = l.closest('tr');
    var val = tr.find('input:checkox').val();
    var childid = 'child' + val;
    var childs = $('input[name=' + childid + ']');
    if (l.data('opened'))
    {
        childs.closest('tr').hide();
    }
    else
    {
        childs.closest('tr').show();
    }
    l.data('opened', !l.data('opened'));
    var url = link.href;
    if (l.data('opened'))
    {
        url = url.replace('closedir', 'opendir');
    }
    else
    {
        url = url.replace('opendir', 'closedir');
    }
    $.get(url);
    return false;
}

function content_select(box)
{
    var id = box.id;
    var value = box.value;
    var form = document.forms[0];
    var childid = 'child' + value;
    var parentid = 'parent' + value;
    var childs = $('input[name=' + childid + ']');
    var parent = $('input[name=' + parentid + ']');
    if (box.checked == true)
    {
        for (var i = 0; i < childs.length; i++)
        {
            var val = $(childs[i]).val();
            $('input[value=' + val + ']').attr('checked', 'checked');
            content_select(childs[i]);
        }
    }
    else
    {
        for (var i = 0; i < childs.length; i++)
        {
            var val = $(childs[i]).val();
            $('input[value=' + val + ']').removeAttr('checked');
            content_select(childs[i]);
        }
        if (parent.length > 0)
        {
            var val = $(parent).val();
            $('input[value=' + val + ']').removeAttr('checked');
            content_select(parent);
        }
    }
}

function check(box, bCheck)
{
    if (bCheck)
    {
        //box.addAttribute('checked');
        box.setAttribute('checked', 'checked');
    }
    else
    {
        box.removeAttribute('checked');
    }
}

function removeinput(obj)
{
    var parent = obj.parentNode;
    parent.parentNode.removeChild(parent);
}

function hide(id,bshow)
{
    var el = document.getElementById(id);
    if(bshow)
        el.style.display = "block";
    else
        el.style.display = "none";
}