String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g, "");
}

function clear_text(field) {
    if ($(field).getValue() == '') {
        $(field).removeClass(field);
    };
}

function show_text(field) {
    if ($(field).getValue() == '') {
        $(field).addClass(field);
    };
}

function change_state() {
    var autologin = $('autologin');
    if (autologin.getValue() == 'on') { // checked
        autologin.removeProperty('checked');
        $('btn_autologin').removeClass('checked');
    } else {
        autologin.setProperty('checked', 'checked');
        $('btn_autologin').addClass('checked');
    }
    return false;
}

function submit_on_enter(e) {
    if (!e) {
        if (window.event) { //Internet Explorer
            e = window.event;
        } else {
            return;
        }
    }
    if (typeof(e.keyCode) == 'number') { //DOM
        e = e.keyCode;
    } else if (typeof(e.which) == 'number') { //NS 4 compatible
        e = e.which;
    } else if (typeof(e.charCode) == 'number') { //also NS 6+, Mozilla 0.9+
        e = e.charCode;
    } else { //total failure, we have no way of obtaining the key code
        return;
    }
    if (e == 13 && document.login_form.username.value != '' && document.login_form.password.value != '') {
        document.login_form.submit();
        return false;
    }
}

function login_submit() {
    if (document.login_form.username.value != '' && document.login_form.password.value != '') {
        document.login_form.submit();
    }
    return false;
}

function mail(who, domain) {
    window.location.href = 'mailto:' + who + '@' + domain;
    return false;
}

$(window).addEvent('domready', function () {
    $('google-search-button').addEvents({
        'mouseover': function () {
            this.addClass('search_hover');
        },
        'mouseout': function () {
            this.removeClass('search_hover');
        }
    });
    $('google-search-q').addEvents({
        'focus': function () {
            this.addClass('text_clear');
        },
        'blur': function () {
            var val = this.getProperty('value').trim();
            this.setProperty('value', val);
            if (val == '') this.removeClass('text_clear');
        }
    });
    $('google-search').addEvent('submit', function (e) {
        var q = $('google-search-q');
        var val = q.getProperty('value').trim();
        if (val == '') {
            new Event(e).stop();
        } else {
            q.setProperty('value', val);
        }
    });
});