<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_ASSET_LOCATIONS';
$path_to_root = "../..";
include($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/assets/includes/db/asset_locations_db.inc");

page(_($help_context = "Asset Locations"));

simple_page_mode(true);

function can_commit_asset_location(){

    $_POST['loc_code'] = strtoupper($_POST['loc_code']);

    if((strlen(db_escape($_POST['loc_code'])) > 20) || empty($_POST['loc_code'])){ //check length after conversion
        display_error( _("The location code can not be empty."));
        set_focus('loc_code');
        return false;
    } 
	
    if(strlen($_POST['location_name']) == 0){
        display_error( _("The location name must be entered."));		
        set_focus('location_name');
        return false;
    }
    
    return true;
}

if($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM'){

    if(!can_commit_asset_location()){
        return;
    }
    
    if($selected_id != -1){
        update_asset_location($selected_id, $_POST['location_name'], $_POST['delivery_address'], $_POST['phone'], $_POST['phone2'], 
                              $_POST['fax'], $_POST['email'], $_POST['contact'], $_POST['inactive']);	
        display_notification(_('Selected asset location has been updated'));
    }else{
        add_asset_location($_POST['loc_code'], $_POST['location_name'], $_POST['delivery_address'], $_POST['phone'], $_POST['phone2'], 
                           $_POST['fax'], $_POST['email'], $_POST['contact'], $_POST['inactive']);
        display_notification(_('New asset location has been added'));
    }

    $Mode = 'RESET';
} 

//----------------------------------------------------------------------------------

if($Mode == 'RESET'){
    $selected_id = -1;
    $sav = get_post('show_inactive');
    unset($_POST);
    $_POST['show_inactive'] = $sav;
}

$result = get_asset_locations(check_value('show_inactive'));

start_form();
    start_table(TABLESTYLE, "width=85%");
        $th = array(_("Location Code"), _("Location Name"), _("Address"), _("Phone"), _("Secondary Phone"), _("Inactive"), "");
            table_header($th);
            $k = 0; //row colour counter

            while($myrow = db_fetch($result)){

                alt_table_row_color($k);
                    label_cell($myrow["loc_code"]);
                    label_cell($myrow["location_name"]);
                    label_cell($myrow["delivery_address"]);
                    label_cell($myrow["phone"]);
                    label_cell($myrow["phone2"]);
                    if($myrow["inactive"] == 1){
                        $inactive = "Yes";
                    }else{
                        $inactive = "No";
                    }
                    label_cell($inactive, "align=center");
                    edit_button_cell("Edit".$myrow["loc_code"], _("Edit"));
                end_row();
            }
    end_table();

br();

    start_table(TABLESTYLE2);

    $_POST['email'] = "";
    if($selected_id != -1){
            //editing an existing Location

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

            $_POST['loc_code'] = $myrow["loc_code"];
            $_POST['location_name']  = $myrow["location_name"];
            $_POST['delivery_address'] = $myrow["delivery_address"];
            $_POST['contact'] = $myrow["contact"];
            $_POST['phone'] = $myrow["phone"];
            $_POST['phone2'] = $myrow["phone2"];
            $_POST['fax'] = $myrow["fax"];
            $_POST['email'] = $myrow["email"];
            $_POST['inactive'] = $myrow["inactive"];
        }

        hidden("selected_id", $selected_id);
        hidden("loc_code");
        label_row(_("Location Code:"), $_POST['loc_code']);
    }else{ //end of if $selected_id only do the else when a new record is being entered
        text_row(_("Location Code:"), 'loc_code', null, 19, 20);
    }

    text_row_ex(_("Location Name:"), 'location_name', 50, 100);
    text_row_ex(_("Contact for deliveries:"), 'contact', 30, 30);

    textarea_row(_("Address:"), 'delivery_address', null, 35, 5);	

    text_row_ex(_("Telephone No:"), 'phone', 32, 30);
    text_row_ex(_("Secondary Phone Number:"), 'phone2', 32, 30);
    text_row_ex(_("Facsimile No:"), 'fax', 32, 30);
    email_row_ex(_("E-mail:"), 'email', 32);
    yesno_list_row("Inactive", "inactive");

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

end_form();

end_page();

?>