/*
 * phpaga
 *
 * This file contains some JavaScript functionality for phpaga.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: tasks.js 837 2007-08-26 21:35:19Z 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 viewTaskDetails(id, elem) {

    var taskvid = elem+id;

    showPageStatus();

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

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

                            vimg = document.getElementById(taskvid);
                            opdet = document.getElementById("opdetails");

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

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

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

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

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

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

                            hidePageStatus();

                        },

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

}

function addTask(id, elem, current_pe_id) {

    prjvid = elem+id;

    showPageStatus();

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

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

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

                                prjtitle = response.getElementsByTagName('prjtitle')[0].firstChild.data;
                                opcats = response.getElementsByTagName('opcat');
                                users = response.getElementsByTagName('user');

                                document.getElementById('prj_id').value = response.getElementsByTagName('prjid')[0].firstChild.data;

                                document.getElementById('frmprjtitle').innerHTML = prjtitle;

                                /* Task categories */

                                for (i = 0; i < document.getElementById("sel_opcat").options.length; i++) {
                                    document.getElementById("sel_opcat").options[i] = null;
                                }

                                document.getElementById("sel_opcat").options.length = 0;

                                document.getElementById("sel_opcat").options.length = opcats.length;

                                for (i=0;i<opcats.length;i++){
                                    if (opcats[i].firstChild.data.length > 0) {
                                        document.getElementById("sel_opcat").options[i] = new Option(opcats[i].firstChild.data, opcats[i].getAttribute('id'));
                                    }
                                }

                                /* Project members */

                                for (i = 0; i < document.getElementById("sel_prjmembers").options.length; i++) {
                                    document.getElementById("sel_prjmembers").options[i] = null;
                                }

                                document.getElementById("sel_prjmembers").options.length = 0;

                                document.getElementById("sel_prjmembers").options.length = users.length;

                                for (i=0;i<users.length;i++){
                                    if (users[i].firstChild.data.length > 0) {
                                        document.getElementById("sel_prjmembers").options[i] = new Option(users[i].firstChild.data, users[i].getAttribute('id'));
                                        if (current_pe_id == users[i].getAttribute('id'))
                                            document.getElementById("sel_prjmembers").options[i].selected = true;
                                    }
                                }

                                document.getElementById('op_startdate').value = getCurrentDateString();
                                document.getElementById('op_starthours').value = '';
                                document.getElementById('op_enddate').value = '';
                                document.getElementById('op_endhours').value = '';
                                document.getElementById('op_duration').value = '';
                                document.getElementById('op_billabletype').checked = true;
                                document.getElementById('op_desc').value = '';

                                vimg = document.getElementById(prjvid);
                                opdet = document.getElementById("editopdetails");

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

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

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

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

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

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

                                document.getElementById('op_startdate').focus();

                            }

                            hidePageStatus();
                        },

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


}

function submitTaskForm(theform) {

    var status = AjaxRequest.submit(
                                    theform
                                    ,{
                                        'onSuccess':function(req){

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

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

                                                prjid = response.getElementsByTagName('prjid')[0].firstChild.data;
                                                prjduration = response.getElementsByTagName('prjduration')[0].firstChild.data;

                                                prjtitle = response.getElementsByTagName('prjtitle')[0].firstChild.data;
                                                opid = response.getElementsByTagName('opid')[0].firstChild.data;
                                                opdesc = response.getElementsByTagName('opdesc')[0].firstChild.data;
                                                taskdate = response.getElementsByTagName('opstart')[0].firstChild.data;

                                                tasktype = response.getElementsByTagName('opcat')[0].firstChild.data;
                                                opduration = response.getElementsByTagName('opduration')[0].firstChild.data;

                                                deltxt = response.getElementsByTagName('deltxt')[0].firstChild.data;

                                                hideElement('editopdetails');

                                                document.getElementById('dur'+prjid).innerHTML = prjduration;

                                                //document.getElementById('imgmhy').src=document.getElementById('imgmhy').src + '&t1';
                                                //document.getElementById('imgmhw').src=document.getElementById('imgmhw').src + '&t1';

                                                document.getElementById('objmh1').data += '&t1';
                                                document.getElementById('objmh2').data += '&t1';

                                                addTaskToTable(prjid, prjtitle, opid, opdesc, taskdate, tasktype, opduration, deltxt);
                                            }

                                            hidePageStatus();

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

    return status;

}


function addTaskToTable(prjid, prjtitle, opid, opdesc, taskdate, tasktype, duration, deltxt)
{
    var tbl = document.getElementById('lop');

    var row = tbl.insertRow(1);

    if (tbl.rows[2].getAttribute('class') == 'odd') {
        row.className = 'even';
    } else {
        row.className = 'odd';
    }

    var cell0 = row.insertCell(0);

    prjlink = document.createElement("a");
    prjlink.href = 'projects.php?id=' + prjid;

    prjtitle = document.createTextNode(prjtitle);

    prjlink.appendChild(prjtitle);
    cell0.appendChild(prjlink);

    var cell1 = row.insertCell(1);
    tasktext = document.createTextNode(opdesc);
    cell1.appendChild(tasktext);

    var cell2 = row.insertCell(2);
    cell2.className = 'time';
    taskdate = document.createTextNode(taskdate);
    cell2.appendChild(taskdate);

    var cell3 = row.insertCell(3);
    taskcat = document.createTextNode(tasktype);
    cell3.appendChild(taskcat);

    var cell4 = row.insertCell(4);
    cell4.className = 'time';
    taskhrs = document.createTextNode(duration);
    cell4.appendChild(taskhrs);

    var cell5 = row.insertCell(5);

    var elview = document.createElement('img');
    elview.setAttribute('src', 'img/phpaga/view.png');
    elview.setAttribute('height', '16');
    elview.setAttribute('width', '16');

    viewlink = document.createElement("a");
    viewlink.className = 'ptr';
    viewlink.setAttribute('onclick', "viewTaskDetails("+opid+", 'vimg');");
    viewlink.setAttribute('id', 'vimg'+opid);
    viewlink.appendChild(elview);

    var eledit = document.createElement('img');
    eledit.setAttribute('src', 'img/phpaga/edit.png');
    eledit.setAttribute('height', '16');
    eledit.setAttribute('width', '16');

    editlink = document.createElement("a");
    editlink.href = 'operation.php?id='+opid;
    editlink.appendChild(eledit);

    var eldel = document.createElement('img');
    eldel.setAttribute('src', 'img/phpaga/delete.png');
    eldel.setAttribute('height', '16');
    eldel.setAttribute('width', '16');

    dellink = document.createElement("a");
    dellink.className = 'ptr';
    //    dellink.setAttribute('onclick', "confirm_delete_op("+opid+", '"+deltxt+"');");
    dellink.onclick = function() { confirm_delete_op(opid, deltxt) };
    dellink.appendChild(eldel);

    cell5.appendChild(viewlink);
    cell5.appendChild(editlink);
    cell5.appendChild(dellink);

}
