<?php
/**********************************************************************
***********************************************************************/


function can_process_project_site($Mode)
{
    if($Mode=='ADD_ITEM'){
        if(!(get_post('project_id') > 0)){
            display_error("Please select a project.");
            set_focus('project_id');
            return false;
        }
    }
    
    if(!(strlen(get_post('site_name')) > 0)){
        display_error("Job name can not be empty.");
        set_focus('site_name');
        return false;
    }
    
    if(!(check_num('estimated_cost', 0))){
        display_error("Estimated cost can not be negative.");
        set_focus('estimated_cost');
        return false;
    }
    
    if(!(check_num('percent_completed', 0, 100))){
        display_error("Percentage completed should be between 0 and 100.");
        set_focus('percent_completed');
        return false;
    }
    
    if(get_post('cost_1_active') == true){
        if(!(strlen(get_post('cost_1_name')) > 0)){
            $message = "Fixed Cost Before Total component name can not be blank.";
            display_error($message);
            set_focus('cost_1_name');
            return false;
        }
        
        if(!(check_num('cost_1', 0))){
            $message = "Fixed Cost Before Total component value can not be negative.";
            display_error($message);
            set_focus('cost_1');
            return false;
        }
        
    }else{
        $_POST['cost_1_name'] = "";
        $_POST['cost_1'] = 0;
    }
    
    
    
    if(get_post('cost_2_active') == true){
        if(!(strlen(get_post('cost_2_name')) > 0)){
            $message = "Percentage On Total Cost component name can not be blank.";
            display_error($message);
            set_focus('cost_2_name');
            return false;
        }
        
        if(!(check_num('cost_2', 0, 100))){
            $message = "Percentage On Total Cost component value should be between 0 and 100.";
            display_error($message);
            set_focus('cost_2');
            return false;
        }
        
    }else{
        $_POST['cost_2_name'] = "";
        $_POST['cost_2'] = 0;
    }
    
    
    if(get_post('cost_3_active') == true){
        if(!(strlen(get_post('cost_3_name')) > 0)){
            $message = "Fixed Cost After Total component name can not be blank.";
            display_error($message);
            set_focus('cost_3_name');
            return false;
        }
        
        if(!(check_num('cost_3', 0))){
            $message = "Fixed Cost After Total component value can not be negative.";
            display_error($message);
            set_focus('cost_3');
            return false;
        }
        
    }else{
        $_POST['cost_3_name'] = "";
        $_POST['cost_3'] = 0;
    }
    
    return true;
    
}

function display_project_sites()
{
    $site_list = get_project_sites($_POST['project_id']);



    start_table(TABLESTYLE, "width=60%");

    $th = array(_("ID"), "Description", "Contact Nos", "Comments", "Estimated Cost", "Completed %", "Status", " ");

    table_header($th);

    $k = 0; //row colour counter

    $calculated = false;

    while ($myrow = db_fetch($site_list))
    {
        alt_table_row_color($k);

        label_cell($myrow["id"]);
        label_cell($myrow["description"]);        
        label_cell($myrow["contact_nos"]);
        label_cell($myrow["comments"]);
        amount_cell($myrow["estimated_cost"]);
        amount_cell($myrow["percent_completed"]);
        label_cell($myrow["value_name"], "align=center");

    //    label_cell($myrow["inactive"]);
         edit_button_cell("Edit".$myrow['id'], _("Edit"));
       // delete_button_cell("Delete".$myrow['id'], _("Delete"));
        end_row();
    }

    end_table();
}


function display_project_site_edit_controls(&$Mode, &$selected_id)
{

    if ($Mode == 'Edit')
    {
            $myrow = get_project_site_details($selected_id);

            $_POST['site_name'] = $myrow["description"];
            $_POST['project_site_status_code'] = $myrow["status_code"];
            $_POST['contact_no'] = $myrow["contact_nos"];
            $_POST['comment'] = $myrow["comments"];
            $_POST['inactive'] = $myrow["inactive"];
            $_POST['estimated_cost'] = $myrow["estimated_cost"];
            $_POST['percent_completed'] = $myrow["percent_completed"];
            
            $_POST['cost_1_active'] = $myrow["cost_1_active"];
            $_POST['cost_1_name'] = $myrow["cost_1_name"];
            $_POST['cost_1'] = $myrow["cost_1"];
            
            $_POST['cost_2_active'] = $myrow["cost_2_active"];
            $_POST['cost_2_name'] = $myrow["cost_2_name"];
            $_POST['cost_2'] = $myrow["cost_2"];
            
            $_POST['cost_3_active'] = $myrow["cost_3_active"];
            $_POST['cost_3_name'] = $myrow["cost_3_name"];
            $_POST['cost_3'] = $myrow["cost_3"];
    }

    hidden('selected_id', $selected_id);
    if (@$_GET['popup'])
    {
            hidden('_tabs_sel', get_post('_tabs_sel'));
            hidden('popup', @$_GET['popup']);
    }


    start_table(TABLESTYLE2);

    text_row(_("Job Name:"), 'site_name', null, 50, 50);
    text_row(_("Contact Nos:"), 'contact_no', null, 50, 50);
    textarea_row(_("Comment:"), 'comment', null, 31, 3);
    amount_row("Estimated Cost", 'estimated_cost');
    amount_row("Completed %", 'percent_completed');
    value_list_code_row(_("Status:"), 'project_site_status_code', 'PROJ_SITE_STATUS');
    
    record_status_list_row(_("Inactive:"),'inactive');

    end_table(1);
    
    start_table(TABLESTYLE2,"width=60%");
    $th = array("Description","Active", "Display Name","Value Type","Value");
    table_header($th);
    start_row();
        label_cell("Fixed Cost Before Total");
        yesno_list_cells("", 'cost_1_active');
        text_cells("", 'cost_1_name', null, 50, 50);
        amount_cells("Amount", 'cost_1');
    end_row();
    
    start_row();
        label_cell("Percentage On Total Cost");
        yesno_list_cells("", 'cost_2_active');
        text_cells("", 'cost_2_name', null, 50, 50);
        amount_cells("Percentage", 'cost_2');
    end_row();
    
    start_row();
        label_cell("Fixed Cost After Total");
        yesno_list_cells("", 'cost_3_active');
        text_cells("", 'cost_3_name', null, 50, 50);
        amount_cells("Amount ", 'cost_3');
    end_row();

    end_table(1);

    submit_add_or_update_center($selected_id == -1, '', 'both');
}


function handle_update_project_site(&$Mode, &$selected_id)
{
    if(can_process_project_site($Mode)){
        update_site($selected_id, $_POST['site_name'],
                $_POST['project_site_status_code'],$_POST['contact_no'],
                $_POST['comment'], input_num('estimated_cost'), 
                input_num('percent_completed'), $_POST['inactive'],
                $_POST['cost_1_active'], $_POST['cost_1_name'], input_num('cost_1'),
                $_POST['cost_2_active'], $_POST['cost_2_name'], input_num('cost_2'),
                $_POST['cost_3_active'], $_POST['cost_3_name'], input_num('cost_3'));
        
        clear_site_data();
        $msg = _("Job details has been updated.");
        
        display_notification($msg);
        $Mode = 'RESET';
    }
}

function handle_add_project_site(&$Mode, &$selected_id)
{
    if(can_process_project_site($Mode)){
        add_site(get_post('project_id'), $_POST['site_name'],
                $_POST['project_site_status_code'],$_POST['contact_no'],
                $_POST['comment'], input_num('estimated_cost'),
                input_num('percent_completed'), $_POST['inactive'],
                $_POST['cost_1_active'], $_POST['cost_1_name'], input_num('cost_1'),
                $_POST['cost_2_active'], $_POST['cost_2_name'], input_num('cost_2'),
                $_POST['cost_3_active'], $_POST['cost_3_name'], input_num('cost_3'));            
        $msg = _("The new site has been added.");
             
        clear_site_data();
        $msg = _("New job has been added.");

        display_notification($msg);
        $Mode = 'RESET';  
    }
}