String.prototype.endswith = function(test)
{
    return this.substring(this.length-test.length, this.length) == test;
}
String.prototype.startswith = function(test)
{
    return this.substring(0, test.length) == test;
}

function show(elid)
{
    var el = document.getElementById(elid);
    if (el) {
        el.style.display = 'block';
        }
}

function hide(elid)
{
    var el = document.getElementById(elid);
    if (el) {
        el.style.display = 'none';
        }
}

function highlight(id)
{
    var el = document.getElementById(id);
    if (el) {
        el.className += ' highlight';
    }
}

function revert(id)
{
    var el = document.getElementById(id);
    if (el) {
        el.className = el.className.replace(' highlight', '');
    }
}

function set_combobox_options(el, option_array)
{
    var newel, container;

    var parent = getBody();
    // var parent = el.parentNode;
    for(var i=parent.childNodes.length-1; i>=0; i--) {
        if (parent.childNodes[i].tagName=='DIV' && parent.childNodes[i].className=='combobox_options') {
            parent.removeChild(parent.childNodes[i]);
        }
    }

    container = document.createElement('div');
    container.className = 'combobox_options';

    var offset;
    offset = MochiKit.Position.cumulativeOffset(el);
    container.style.top = offset.y + el.offsetHeight + 'px';
    container.style.left = offset.x + 'px';

    for(var j=0; j<option_array.length; j++) {
        newel = document.createElement('div');
        newel.className = 'combobox_option';
        newel.onclick = function () {
            pick_option(el, this);
        }
        newel.setAttribute('name', option_array[j]);
        newel.innerHTML = option_array[j];

        container.appendChild(newel);
    }

    parent.appendChild(container);
}

function pick_option(el, option)
{
    el.value = option.innerHTML;
    option.parentNode.parentNode.removeChild(option.parentNode);
    el.focus();
}

function getBody()
{
    var nodes = document.childNodes;
    var found = null;
    var i=0;
    while(found === null || i<nodes.length) {
        if (nodes[i].tagName=='HTML'){
            nodes = nodes[i].childNodes;
            i=0;
        }
        if (nodes[i].tagName=='BODY'){
            found = nodes[i];
        }
        i++;
    }

    return found;
}

/*
* Strips the submits form the form, except when the clicked
* attribute is set. This is done automaticly with the onclick.
* The actual downside of all this is the MUST HAVE of JS...
*/
function strip_submits(form)
{
    var element;
    for(var i=0; i<form.elements.length; i++) {
        element = form.elements[i];
        if (element.type=='submit' || element.tagName=='BUTTON') {
            if (!element.clicked) {
                // Lets create a weird bogus name. This does not change any layout
                element.name = element.name + '_dummied';
                // We disable the element BEFORE posting. Changes the layout slightly
                // element.disabled = true; 
                // Removing can screw up the layout badly
                // element.parentElement.removeChild(element);
            }
        }
    }
}

function set_check(el)
{
    var checked = false;
    var temp = el.className.split(' ');
    for(var i=0; i<temp.length; i++) {
        if (temp[i]=='checked') {
            checked = true;
        }
    }

    if (checked) {
        uncheck(el);
    }
    else {
        check(el);
    }
}

function check(el)
{
    if (el) {
        el.className += ' checked';
    }
}

function uncheck(el)
{
    if (el) {
        el.className = el.className.replace(' checked', '');
    }
}

function check_box(box)
{
    if (box.value=='yes') {
        box.value='no';
    } else {
        box.value='yes';
    }
}

function submit_form(el)
{
    var parentel = el;
    while(parentel.tagName != 'FORM') {
    // while(parentel != null) {
        parentel = parentel.parentNode;
    }
    parentel.submit();
}

function select_all(el, searchname)
{
    var parentel = el;
    while(parentel.tagName != 'FORM') {
    // while(parentel != null) {
        parentel = parentel.parentNode;
    }

    for(var i=0; i<parentel.elements.length; i++) {
        var name = parentel.elements[i].name.split('[');
        if (name[0]==searchname) {
            parentel.elements[i].checked = true;
        }
    }
}