/*
 * 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 submitTaskFormDyn(addDialog, viewDialog) {
    var prj_id = $("#prj_id");
    var pe_id = $("#pe_id");
    var op_startdate = $("#op_startdate");
    var op_starthours = $("#op_starthours");
    var op_enddate = $("#op_enddate");
    var op_endhours = $("#op_endhours");
    var opcat_id = $("#opcat_id");
    var op_duration = $("#op_duration");
    var op_desc = $("#op_desc");


    var data = 'saction=47&prj_id='+prj_id.val()+'&pe_id='+pe_id.val()
        +'&op_startdate='+op_startdate.val()+'&op_starthours='+op_starthours.val()
        +'&op_enddate='+op_enddate.val()+'&op_endhours='+op_endhours.val()
        +'&opcat_id='+opcat_id.val()+'&op_duration='+op_duration.val()
        +'&op_desc='+op_desc.val();

    if ($("#op_billabletype").attr('checked'))
        data += '&op_billabletype='+$("#op_billabletype").val()

    $.ajax({
        url: 'service.php',
        type: 'POST',
        data: data,
        dataType: 'json',
        cache: false,
        success: function(result) {

            data = result.data;

            if (result.status != 0) {
                hidePageStatus();
                alert(result.message);
            } else {

                $('#dur'+data.prjid).html(data.prjduration);

                var newtr = $('<tr id="oprow'+data.opid+'"></tr>');

                newtr.append($('<td class="opprj" title="opprjid'+prj_id.val()+'">').append($('<a></a>').text(data.prjtitle).attr({ 'href': 'project.php?id='+data.prjid })));
                newtr.append($('<td>').text(data.opdesc));
                newtr.append($('<td>').text(data.opstart));
                newtr.append($('<td>').text(data.opcat));
                newtr.append($('<td>').addClass('time').text(data.opduration));

                actions = $('<td>');

                var onView = $('<a></a>').attr( { 'id': 'tv'+data.opid, 'href': '#' }).addClass('tview')
                    .append('<img src="img/phpaga/view.png" id="vimg'+data.opid+'" height="16" width="16">');

                var onEdit = $('<a></a>').attr({ 'href': 'operation.php?id='+data.opid })
                    .append('<img src="img/phpaga/edit.png" height="16" width="16">');

                var onDelete = $('<a></a>').attr({ 'href': '#', 'id': 'op'+data.opid }).addClass('opdel')
                    .append('<img src="img/phpaga/delete.png" height="16" width="16">');

                onView.appendTo(actions);
                onEdit.appendTo(actions);
                onDelete.appendTo(actions);

                newtr.append(actions);
                $("#lop > tbody:first").prepend(newtr);

                addDialog.dialog('close');

                var year = $('#year').html();
                var month = parseInt($('#month').html());
                var pe_id = parseInt($('#currperson').val());

                $('#mhchartyear').empty();
                $.getJSON("data_service.php?req=mhrs&pe_id="+pe_id+"&type=4&date="+year+"-01-01",  
                        function(data) { mhrsplot = get_mhrsplot_year('mhchartyear', data); });

                $('#mhchartmonth').empty();
                $.getJSON("data_service.php?req=mhrs&pe_id="+pe_id+"&type=2&date="+year+"-"+month+"-01", 
                        function(data) { mhrsplot = get_mhrsplot_month('mhchartmonth', data); });

                $.getJSON("data_service.php?req=mhrsstatpe&pe_id="+pe_id,
                        function(data) { 
                            $('#mh_fy').html(data.fy);
                            $('#mh_curr_fy').html(data.curr_fy);
                            $('#mh_curr_month').html(data.curr_month);
                            $('#mh_curr_week').html(data.curr_week);
                            $('#mh_spark').sparkline(data.spark_month);
                            $('#mh_oprange').html(data.op.begin+' - '+data.op.end);
                        });
            }
        }
    });

    return false;
}


