function isdefined( variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}


function formatFloat(value, decimals, addThousandSpacings) {
    if (value == null) {
        return null;
    }

    var part1;
    var part2;

    var s = value+'';

    var m;
    if (m = s.match(/^([0-9]*)(\.([0-9]*))?$/)) {
        part1 = m[1];
        part2 = m[3];
    }

    if(decimals) {
        var r = Math.pow(10, decimals);
        value*=r;
        if (value-Math.floor(value) >= 0.5) {
            value = Math.ceil(value);

        } else {
            value = Math.floor(value);
        }

        part1 = Math.floor(value/r)+'';
        part2 = '';

        if (decimals > 0) {
            // (value+r) ensures enough 0 on the left, e.g. 0.08 -> 8 -> substr(8, -2) -> false
            value=(value+r)+'';
            part2 = value.substring(value.length-decimals);

            for (var i=part2.length; i<decimals; i++) {
                part2 += '0';
            }
        }

    } 

    if (addThousandSpacings) {
        var s = '';
        while (part1.length > 3) {
            s = float_thousand_spacing + part1.substring(part1.length-3) + s;
            part1=part1.substring(0,part1.length-3);
        }
        part1 += s;
    }

    if (part2 && part2.length > 0) {
        value = part1 + float_decimal_character + part2;

    } else {
        value = part1;
    }

    return value;
}

String.implement({
    replace_callback: function(search, callback){
        var isFunction = ($type(callback) == 'function');

        var r = '';
        var s = ''+this;

        if ($type(search) != 'regexp') {
            if (!isFunction) {
                return this.replace(search, callback);
            }

            var i = 0;
            var l = (''+search).length;
            while ((i = s.indexOf(search)) != -1) {
                r += s.substring(0, i);

                r += callback(s.substr(i, l));

                s = s.substring(i+l);
            }
            r += s;

            return r;
        }


        var m;
        while (m = search.exec(s)) {
            r += s.substring(0, m.index);

            if (isFunction) {
                r += callback(m);
            } else {
                r += callback;
            }

            s = s.substring(m.index + m[0].length);
        }
        r += s;

        return r;
    }
});

var nextInputId = 1;
function createSelectInput(title, items, required) {
    if (required == null) {
        required = true;
    }
    var id = 'ajaxinput'+(nextInputId++);

    var select = new Element('select', {
        'id': id
    });

    items.each((function(i){
        this.select.adopt(
            new Element('option', {
                'value': i[0],
                'text': i[1]
            })
        );
    }).bind({
        select: select
    }));

    var element = new Element('div', {
        'class': 'input'+(required ? ' required' : '')
    }).adopt(
        new Element('label', {
            'for': id
        }).adopt(
            new Element('span', {
                'html': title
            }),
            (required ?
                new Element('span', {
                    'class': 'requiredLabel',
                    'html': '*'
                })
                : null
            )
        ),
        select
    );


    return element;
}

function createTextInput(title, value, required) {
    if (required == null) {
        required = true;
    }
    var id = 'ajaxinput'+(nextInputId++);

    var input = new Element('input', {
        'id': id,
        'type': 'text',
        'value': value
    });

    var element = new Element('div', {
        'class': 'input'+(required ? ' required' : '')
    }).adopt(
        new Element('label', {
            'for': id
        }).adopt(
            new Element('span', {
                'html': title
            }),
            (required ?
                new Element('span', {
                    'class': 'requiredLabel',
                    'html': '*'
                })
                : null
            )
        ),
        input
    );


    return element;
}

function createSubmitInput(title, clickEvent) {
    var b;

    var button = new Element('div', {
        'class': 'button'
    }).adopt(
        b = new Element('input', {
            'type': 'submit',
            'value': title
        })
    );

    if (clickEvent) {
        b.addEvent('click', clickEvent);
    }

    return button;
}