// js scripts utils

function add_to_cart(id) { modify_cart('add', id); }

function remove_from_cart(id) { modify_cart('remove', id); }

function modify_cart(action,id)
{
    new Ajax.Updater(   'tool_cart',
                        '/cart/modify',
                        {   method: 'get',
                            parameters: 'action=' + action + '&id=' + id }
                    );
    return false;
}

function update_quantity ( element_id, quantity )
{
    if ( parseInt($(element_id).value) + quantity > 0 )
        $(element_id).value = parseInt($(element_id).value) + quantity;
    return false;
}

function validate_terms_and_conditions (message)
{
    if ($('terms_and_conditions').checked)
        return true;

    alert(message);
    return false;
}

function validate_order_message (alert_message)
{
    if ( $('order_message').value.length > 5 )
        return true;

    alert(alert_message);
    return false;
}

function observer_for_cart_editor(id)
{
    new Form.Element.Observer(
        'cart_element_' + id,
        2,
        function(element, value) {
            if ( value > 0 )
            {
                new Ajax.Updater(
                    'cart_editor',
                    '/cart/update_partial',
                    {
                        asynchronous:true,
                        parameters:'id=' + id + '&quantity=' + value,
                        evalScripts: true})
            }
        });
}

