/*
 * phpaga
 *
 * This file contains some JavaScript functionality for phpaga.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: material.js 848 2007-08-31 15:41:51Z 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(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 || ((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;
                                xstatus = response.getElementsByTagName('status')[0].firstChild.data;

                                if ((xstatus != 0) && (xstatus != 30)) {
                                    alert(response.getElementsByTagName('message')[0].firstChild.data);
                                } else if ((xstatus == 0) || (xstatus == 30)) {


                                    if (status == 0) {
                                        price = response.getElementsByTagName('prodprice')[0].firstChild.data;
                                        name = response.getElementsByTagName('prodname')[0].firstChild.data;
                                        id = response.getElementsByTagName('prodid')[0].firstChild.data;
                                    } else {
                                        price = '';
                                        name = '';
                                        id = null;
                                    }

                                    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();
        }
    }
    );
}

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";

    pbox.style.visibility = "visible";
}


function materialSelectProduct(prod_code) {

    document.getElementById('mat_prodcode').value = prod_code;
    //    document.getElementById('prodname'+elem_num).value = '';

    document.getElementById('prodselectbox').style.visibility = "hidden";
    getMatProdInfo(true);
}