/*
 * phpaga
 *
 * This file contains some JavaScript functionality for phpaga.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id$
 *
 * Copyright (c) 2006, Florian Lanthaler <florian@phpaga.net>
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *    * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *    * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in
 *      the documentation and/or other materials provided with the
 *      distribution.
 *
 *    * Neither the name of Florian Lanthaler nor the names of his
 *      contributors may be used to endorse or promote products derived
 *      from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

function getMatProdInfo(overwrite) {

    if (typeof overwrite == 'undefined')
        overwrite = false;

    /* If mat_prodcode is set and mat_name is not set then fetch product information.
       Do not fetch data for rows that have already been filled out. */

    if (overwrite || (($('#mat_prodcode').val().length > 0) && ($('#mat_name').val().length == 0))) {

        showPageStatus();

        $.ajax({
            url: 'service.php?saction=40&prod_code='+$('#mat_prodcode').val(),
            type: 'GET',
            dataType: 'json',
            cache: false,
            success: function(result) {

                hidePageStatus();

                if (result == null) {
                    alert('An unexpected error has occurred.');
                } else if (result.status != 0 && result.status != 30) {
                    alert(result.message);
                } else {

                    if (result.status == 0) {
                        $('#mat_name').val(result.data.prod_name);
                        $('#mat_price').val(result.data.prod_price);
                        $('#prod_id').val(result.data.prod_id);

                        if ($('#mat_qty').val().length == 0)
                            $('#mat_qty').val(1);
                    }
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {

                msg = textStatus;
                if (textStatus == 'parsererror')
                    msg = 'Invalid response from the server.';

                hidePageStatus();
                alert(msg);
            }
        });
    }
}

function materialGetProductCatalog() {

    var pbox = document.getElementById('prodselectbox');
    var prcode = document.getElementById('mat_prodcode');

    x = findPosX(prcode);
    y = findPosY(prcode);

    w = pbox.offsetWidth;

    if (x > w) {
        x = x - w;
    }

    if (y > pbox.offsetHeight) {
        y = y - pbox.offsetHeight + 20;
    }

    pbox.style.left = x+"px";
    pbox.style.top = y+"px";

    $('#prodselectbox').css('visibility', 'visible');
}

function materialSelectProduct(prod_code) {
    $('#mat_prodcode').val(prod_code);
    $('#prodselectbox').css('visibility', 'hidden');
    getMatProdInfo(true);
}
