function trim(str)
{
    return str.replace(/^(\s+)?(\S*)(\s+)?$/, '$2');
}

function delay(milliseconds)
{
    var then, now;
    then = new Date().getTime();
    now = then;
    while ((now - then) < milliseconds) {
        now=new Date().getTime();
    }
}

function wp(location, width, height, scrolls, center)
{

    day = new Date();
    id  = day.getTime();

    if (center) {
        left = (screen.width - width) / 2;
        thetop  = (screen.height - height) / 2;
        eval("page" + id + " = window.open(location, '" + id + "', 'toolbar=0,scrollbars=" + scrolls + ",location=0,statusbar=0,menubar=0,resizable=0,width=' + width + ',height=' + height + ',left = ' + left + ',top = ' + thetop);");
    }
    else {
        eval("page" + id + " = window.open(location, '" + id + "', 'toolbar=0,scrollbars=" + scrolls + ",location=0,statusbar=0,menubar=0,resizable=0,width=' + width + ',height=' + height);");
    }
    return false;
}

function wpt(location, width, height)
{
    return wp(location, width, height, 1, 1);
}


function installCharacterLimit(id, length, outputId)
{
    elt = $(id);
    if (!elt)
        return;

    output = $(outputId);

    elt.outputElt = output;
    elt.maxLength = length;
    elt.onkeypress = function() {
        var len = trim(this.value).length;
        var remaining = Math.max(0, elt.maxLength - len);

        if (remaining <= 0)
            this.value = trim(this.value).substring(0, elt.maxLength);

        this.outputElt.innerHTML = remaining;
    }

    elt.onkeypress();
    elt.onfocus = elt.onblur = elt.onkeyup = elt.onkeydown = elt.onkeypress;
}

function installLoginCheck(frm)
{
    frm.onsubmit = function() {
        var username = $(frm.username);
        var password = $(frm.password);
        var error = false;

        removeLoginError('username');
        removeLoginError('password');

        if (trim(username.value).length == 0) {
            setLoginError('username', 'Required field must not be blank');
            error = true;
        }
        else
            removeLoginError('username');

        if (trim(password.value).length == 0) {
            setLoginError('password', 'Required field must not be blank');
            error = true;
        }
        else
            removeLoginError('password');


        if (!error) {
            var options = {
                method     : 'post',
                onSuccess  : handleLoginCheckResponse.bindAsEventListener(this),
                onFailure  : handleLoginCheckResponseFailure.bindAsEventListener(this),
                parameters : 'username=' + username.value + '&password=' + password.value
            };

            new Ajax.Request('/account/logincheck', options);
        }

        return false;
    }
}

function handleLoginCheckResponse(transport)
{
    if (transport.responseText == '1') {
        this.submit();
        return;
    }

    setLoginError('username', 'Your login details were invalid');

}

function handleLoginCheckResponseFailure(transport)
{
    this.submit();
}

function setLoginError(field, error)
{
    $(field + '-container').addClassName('error');
    $$('#' + field + '-error td')[0].update(error);
    $(field + '-error').show();
}

function removeLoginError(field)
{
    $(field + '-container').removeClassName('error');
    $(field + '-error').hide();
}