// general.js: useful javascript functions

var ie4=document.all&&navigator.userAgent.indexOf("Opera")==-1;
var ns4=document.layers;
var ns6=document.getElementById&&navigator.userAgent.indexOf("Opera")==-1;

function $(id) { return document.getElementById(id); }

// set_content: <div name="foo"></div>  set_content('foo', '...')
// replaces a tag within a document with other content
function set_content(id, content) {
  if(ie4){showContentObj=document.all[id];}
  if(ns6){showContentObj=document.getElementById(id);}
  if(ie4||ns6){
    showContentObj.innerHTML=content;
  }
  if(ns4){document.ns_div.document.write(content); document.ns_div.document.close();}
}

// content_of: get content of a named element
function get_content(id) {
    if (ie4) {
        return document.all[id].innerHTML;
    }
    if (ns6) {
        return document.getElementById(id).innerHTML;
    }
}

function checkAll(field) {
    for (i = 0; i < field.length; i++) {
        field[i].checked = true;
    }
}

function uncheckAll(field) {
    for (i = 0; i < field.length; i++) {
        field[i].checked = false;
    }
}

var isMSIE/*@cc_on=1@*/; //http://dean.edwards.name/weblog/2007/03/sniff/

if (isMSIE) {
    // IE's getElementsByName is broken, this fixes it at the cost of
    // some efficiency
    document.getElementsByName = function(name, tag) {
        if (!tag) tag = '*';
        var elems = document.getElementsByTagName(tag);
        var res = [];
        for (var i = 0; i < elems.length; i++) {
            att = elems[i].getAttribute('name');
            if(att == name) res.push(elems[i]);
        }
        return res;
    }
}

function window_location(url) {
    // IE doesn't pass referer for window.location, all other browsers do
    if ( ! isMSIE ) {
        window.location = url;
        return;
    }
    var a = document.createElement("a");
    a.setAttribute("href", url);
    document.getElementsByTagName("body")[0].appendChild(a);
    a.click();
}

function addNotice(type,text) {
    var span = document.createElement('span');
    span.className = 'head';
    span.innerHTML = type + ': ';
    var new_notice = document.createElement('li');
    new_notice.className = type;
    new_notice.appendChild( span );
    new_notice.appendChild( document.createTextNode( text ) );
    var ul = $('notices');
    ul.appendChild( new_notice );
}

function display_bytes(size) {
    if ( ! size ) return;
    if ( size < 1024 ) return size + ' bytes';
    var units = ['bytes', 'kB', 'MB', 'GB'];
    var unit = 0;
    while ( size >= 1024 ) {
        unit++;
        size /= 1024;
    }
    var rounded = parseInt(size);
    rounded += parseInt((size-rounded+.05)*10)/10;
    return rounded + ' ' + units[unit];
}

function commify(num) {
    num = num.toString();
    var n = num.length;
    if ( n <= 3 ) return num;
    return commify( num.substr(0,n-3) ) + ',' + num.substr(n-3);
}

function inline_icon(img) { return '<img src="/static/icons/' + img + '.png">'; }

function newIcon(name) {
    var img = document.createElement('img');
    img.src = '/static/icons/' + name + '.png';
    return img;
}

function clone(obj){
    if(obj == null || typeof(obj) != 'object')
        return obj;

    var temp = new obj.constructor(); // changed (twice)
    for(var key in obj)
        temp[key] = clone(obj[key]);

    return temp;
}

function defined(x) {
    if (typeof(x) != 'undefined' && x != null)
        return true;
    return false;
}

function createTable() {
    var table = document.createElement('table');
    var thead = document.createElement('thead');
    thead.style.height = '0px';
    var tfoot = document.createElement('tfoot');
    tfoot.style.height = '0px';
    var tbody = document.createElement('tbody');
    var row = document.createElement('tr');
    var cell = document.createElement('td');
    table.appendChild(thead);
    table.appendChild(tfoot);
    table.appendChild(tbody);
    tbody.appendChild(row);
    row.appendChild(cell);
    return table;
}

function html(string) {
    return string.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
}

// UTF8 functions borrowed from
// http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html
function encode_utf8(string) {
    return unescape( encodeURIComponent( string ) );
}

function decode_utf8(string) {
    return decodeURIComponent( escape( string ) );
}
