function redirect_alpha(obj1, obj2) {
    if (isNaN(obj1.value.substr(0,1))) {
        obj1.value = "";
        obj2.focus();
    }
}

function toggleVisibility(obj) {
    if (visible(obj))
        hide(obj);
    else show(obj);
}

function visible(obj) {
    if (typeof(obj) == 'string')
        obj = document.getElementById(obj);
    if (obj) {
        if (!obj.style ||
            (typeof(obj.style.visible) != 'string' || obj.style.visible == 'visible') &&
            (typeof(obj.style.display) != 'string' || obj.style.display != 'none'))
            return true;
    }
    return false;
}

function focus(obj) {
    if (typeof(obj) == "string")
        obj = document.getElementById(obj);
    if (obj && visible(obj))
        obj.focus();
}

// Puts the focus on the first 'input' element of obj, if there is one
function focusToInput(obj) {
    var elm;
    if (typeof(obj) == 'string')
        elm = document.getElementById(obj);
    else if (typeof(obj) == 'undefined')
        elm = document.body;
    else elm = obj;
    
    if (elm && visible(elm) && !elm.disabled) {
        if (elm.nodeName == 'INPUT' && (elm.type == 'text' || elm.type == 'password')) {
            focus(elm);
            return true;
        }
        else for (var i = 0; i < elm.childNodes.length; i++) {
            if (focusToInput(elm.childNodes[i]))
                return true;
        }
    }
}

/**
 * Selects on option from a <select> box
 */
function selectOption(select, option) {
    if (typeof(select) == 'string')
        select = document.getElementById(select);
    select.selectedIndex = 0;
    for (var i = 0; i < select.options.length; i++) {
        if ((select.options[i].value == option) ||
            (select.options[i].innerHTML == option))
            select.selectedIndex = i;
    }
}

/**
 * Clears all form elements where 'text' is contained in the
 * element's name or id, or all form elements if 'text' is null
 */
function form_clear(elm, text) {
    if (typeof(elm) == 'string')
        elm = document.getElementById(elm);

    var ok = true;
    if (typeof(text) == 'string') {
        ok = false;
        if (elm) {
            if (elm.id && elm.id.indexOf(text) != -1) ok = true;
            else if (typeof(elm.name) == 'string' && elm.name.indexOf(text) != -1) ok = true;
        }
    }

    if (ok) {
        if (elm.nodeName == 'INPUT') {
            if (elm.type == 'text' || elm.type == 'password') elm.value = "";
            else if (elm.type == 'radio' || elm.type == 'checkbox') elm.checked = false;
        }
        else if (elm.nodeName == 'SELECT')
            elm.selectedIndex = 0;
        else if (elm.nodeName == 'TEXTAREA')
            elm.value = "";
            
        if (elm.old_class)
            elm.className = elm.old_class;
    }

    for (var i = 0; i < elm.childNodes.length; i++)
        form_clear(elm.childNodes[i], text);
}
