/*
 * phpaga
 *
 * This file contains some JavaScript functionality for phpaga.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: material.js 753 2006-10-25 21:52:28Z florian $
 *
 * 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() {

    /* 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 ((document.getElementById('mat_prodcode').value.length > 0)
        && (document.getElementById('mat_name').value.length == 0)) {




        showPageStatus();

        AjaxRequest.get(
                        {
                            'url':'service.php?saction=40&prod_code=' + document.getElementById('mat_prodcode').value,
                                'onSuccess':function(req){

                                response = req.responseXML.documentElement;
                                status = response.getElementsByTagName('status')[0].firstChild.data;

                                if (status != 0) {
                                    hidePageStatus();
                                    alert(response.getElementsByTagName('message')[0].firstChild.data);
                                } else if (status == 0) {

                                    price = response.getElementsByTagName('prodprice')[0].firstChild.data;
                                    name = response.getElementsByTagName('prodname')[0].firstChild.data;
                                    id = response.getElementsByTagName('prodid')[0].firstChild.data;

                                    document.getElementById('mat_name').value = name;
                                    document.getElementById('mat_price').value = price;
                                    document.getElementById('prod_id').value = id;

                                    if (document.getElementById('mat_qty').value == '') {
                                        document.getElementById('mat_qty').value = '1';
                                    }

                                    hidePageStatus();
                                }
                            },

                                'onError':function(req){
                                    alert('Error!\nStatusText='+req.statusText+'\nContents='+req.responseText);
                                    hidePageStatus();
                                }
                        }
                        );
    }
}

function viewMaterialDetails(id, elem) {

    var matvid = elem+id;

    showPageStatus();

    AjaxRequest.get(
    {
        'url':'service.php?saction=43&id=' + id,
        'onSuccess':function(req){

            document.getElementById("matdetails").innerHTML = req.responseText;

            vimg = document.getElementById(matvid);
            matdet = document.getElementById("matdetails");

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

            w = document.getElementById("matdetailstab").offsetWidth;

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

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

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

            if (matdet.style.visibility != 'visible') {
                matdet.style.visibility = 'visible';
            }

            hidePageStatus();

        },

        'onError':function(req){
            alert('Error!\nStatusText='+req.statusText+'\nContents='+req.responseText);
            hidePageStatus();
        }
    }
    );

}