if ($_getgoadd == 'true') {
    $by = $_SESSION['user_id'];
    //	4/14/11
    $frm_lat = empty($_POST['frm_lat']) ? 'NULL' : quote_smart(trim($_POST['frm_lat']));
    // 7/22/10
    $frm_lng = empty($_POST['frm_lng']) ? 'NULL' : quote_smart(trim($_POST['frm_lng']));
    // 7/15/10
    $now = mysql_format_date(time() - get_variable('delta_mins') * 60);
    $query = "INSERT INTO `{$GLOBALS['mysql_prefix']}facilities` (\n\t\t\t`name`, `street`, `city`, `state`, `handle`, `icon_str`, `boundary`, `description`, `capab`, `status_id`, `contact_name`, `contact_email`, `contact_phone`, `security_contact`, `security_email`, `security_phone`, `opening_hours`, `access_rules`, `security_reqs`, `pager_p`, `pager_s`, `lat`, `lng`, `type`, `user_id`, `updated` )\n\t\t\tVALUES (" . quote_smart(trim($_POST['frm_name'])) . "," . quote_smart(trim($_POST['frm_street'])) . "," . quote_smart(trim($_POST['frm_city'])) . "," . quote_smart(trim($_POST['frm_state'])) . "," . quote_smart(trim($_POST['frm_handle'])) . "," . quote_smart(trim($_POST['frm_icon_str'])) . "," . quote_smart(trim($_POST['frm_boundary'])) . "," . quote_smart(trim($_POST['frm_descr'])) . "," . quote_smart(trim($_POST['frm_capab'])) . "," . quote_smart(trim($_POST['frm_status_id'])) . "," . quote_smart(trim($_POST['frm_contact_name'])) . "," . quote_smart(trim($_POST['frm_contact_email'])) . "," . quote_smart(trim($_POST['frm_contact_phone'])) . "," . quote_smart(trim($_POST['frm_security_contact'])) . "," . quote_smart(trim($_POST['frm_security_email'])) . "," . quote_smart(trim($_POST['frm_security_phone'])) . "," . quote_smart(trim($_POST['frm_opening_hours'])) . "," . quote_smart(trim($_POST['frm_access_rules'])) . "," . quote_smart(trim($_POST['frm_security_reqs'])) . "," . quote_smart(trim($_POST['frm_pager_p'])) . "," . quote_smart(trim($_POST['frm_pager_s'])) . "," . $frm_lat . "," . $frm_lng . "," . quote_smart(trim($_POST['frm_type'])) . "," . quote_smart(trim($_SESSION['user_id'])) . "," . quote_smart(trim($now)) . ");";
    $result = mysql_query($query) or do_error($query, 'mysql_query() failed', mysql_error(), __FILE__, __LINE__);
    $new_id = mysql_insert_id();
    $status_id = $_POST['frm_status_id'];
    //4/14/11
    foreach ($_POST['frm_group'] as $grp_val) {
        // 6/10/11
        if (test_allocates($new_id, $grp_val, 3)) {
            $query_a = "INSERT INTO `{$GLOBALS['mysql_prefix']}allocates` (`group` , `type`, `al_as_of` , `al_status` , `resource_id` , `sys_comments` , `user_id`) VALUES \n\t\t\t\t\t({$grp_val}, 3, '{$now}', {$status_id}, {$new_id}, 'Allocated to Group' , {$by})";
            $result_a = mysql_query($query_a) or do_error($query_a, 'mysql query failed', mysql_error(), basename(__FILE__), __LINE__);
        }
    }
    do_log($GLOBALS['LOG_FACILITY_ADD'], 0, mysql_insert_id(), $_POST['frm_status_id']);
    //	2/17/11
    $caption = "<B>Facility  <i>" . stripslashes_deep($_POST['frm_name']) . "</i> data has been updated.</B><BR /><BR />";
    finished($caption);
    // wrap it up
}
// end if ($_getgoadd == 'true')
// add ===========================================================================================================================
// add ===========================================================================================================================
// add ===========================================================================================================================
if ($_getadd == 'true') {
 function updt_ticket($id)
 {
     /* 1/25/09 */
     global $addrs, $NOTIFY_TICKET;
     $post_frm_meridiem_problemstart = empty($_POST) || !empty($_POST) && empty($_POST['frm_meridiem_problemstart']) ? "" : $_POST['frm_meridiem_problemstart'];
     $post_frm_meridiem_booked_date = empty($_POST) || !empty($_POST) && empty($_POST['frm_meridiem_booked_date']) ? "" : $_POST['frm_meridiem_booked_date'];
     //10/1/09
     $post_frm_affected = empty($_POST) || !empty($_POST) && empty($_POST['frm_affected']) ? "" : $_POST['frm_affected'];
     $_POST['frm_description'] = strip_html($_POST['frm_description']);
     //clean up HTML tags
     $post_frm_affected = strip_html($post_frm_affected);
     $_POST['frm_scope'] = strip_html($_POST['frm_scope']);
     if (!get_variable('military_time')) {
         //put together date from the dropdown box and textbox values
         if ($post_frm_meridiem_problemstart == 'pm') {
             $post_frm_meridiem_problemstart = ($post_frm_meridiem_problemstart + 12) % 24;
         }
     }
     if (!get_variable('military_time')) {
         //put together date from the dropdown box and textbox values
         if ($post_frm_meridiem_booked_date == 'pm') {
             $post_frm_meridiem_booked_date = ($post_frm_meridiem_booked_date + 12) % 24;
         }
     }
     if (empty($post_frm_owner)) {
         $post_frm_owner = 0;
     }
     $frm_problemstart = "{$_POST['frm_year_problemstart']}-{$_POST['frm_month_problemstart']}-{$_POST['frm_day_problemstart']} {$_POST['frm_hour_problemstart']}:{$_POST['frm_minute_problemstart']}:00{$post_frm_meridiem_problemstart}";
     if (intval($_POST['frm_status']) == 3) {
         // 1/21/11
         $frm_booked_date = "{$_POST['frm_year_booked_date']}-{$_POST['frm_month_booked_date']}-{$_POST['frm_day_booked_date']} {$_POST['frm_hour_booked_date']}:{$_POST['frm_minute_booked_date']}:00{$post_frm_meridiem_booked_date}";
     } else {
         //				$frm_booked_date = "NULL";
         $frm_booked_date = "";
         // 6/20/10
     }
     if (!get_variable('military_time')) {
         //put together date from the dropdown box and textbox values
         if ($post_frm_meridiem_problemstart == 'pm') {
             $_POST['frm_hour_problemstart'] = ($_POST['frm_hour_problemstart'] + 12) % 24;
         }
         if (isset($_POST['frm_meridiem_problemend'])) {
             if ($_POST['frm_meridiem_problemend'] == 'pm') {
                 $_POST['frm_hour_problemend'] = ($_POST['frm_hour_problemend'] + 12) % 24;
             }
         }
         if (isset($_POST['frm_meridiem_booked_date'])) {
             //10/1/09
             if ($_POST['frm_meridiem_booked_date'] == 'pm') {
                 $_POST['frm_hour_booked_date'] = ($_POST['frm_hour_booked_date'] + 12) % 24;
             }
         }
     }
     $frm_problemend = isset($_POST['frm_year_problemend']) ? quote_smart("{$_POST['frm_year_problemend']}-{$_POST['frm_month_problemend']}-{$_POST['frm_day_problemend']} {$_POST['frm_hour_problemend']}:{$_POST['frm_minute_problemend']}:00") : "NULL";
     $now = mysql_format_date(time() - intval(get_variable('delta_mins') * 60));
     // 6/20/10
     if (empty($post_frm_owner)) {
         $post_frm_owner = 0;
     }
     //			$inc_num_ary = unserialize (get_variable('_inc_num'));					// 11/13/10
     $temp = get_variable('_inc_num');
     // 3/2/11
     $inc_num_ary = strpos($temp, "{") > 0 ? unserialize($temp) : unserialize(base64_decode($temp));
     $name_rev = $_POST['frm_scope'];
     if ($inc_num_ary[0] == 0) {
         // no auto numbering scheme
         switch (get_variable('serial_no_ap')) {
             // incident name revise -1/22/09
             case 0:
                 /*  no serial no. */
                 $name_rev = $_POST['frm_scope'];
                 break;
             case 1:
                 /*  prepend  */
                 $name_rev = $id . "/" . $_POST['frm_scope'];
                 break;
             case 2:
                 /*  append  */
                 $name_rev = $_POST['frm_scope'] . "/" . $id;
                 break;
             default:
                 /* error????  */
                 $name_rev = " error  error  error ";
         }
         // end switch
         // 8/23/08, 9/20/08, 8/13/09
     }
     // end if()
     $facility_id = empty($_POST['frm_facility_id']) ? 0 : trim($_POST['frm_facility_id']);
     // 9/28/09
     $rec_facility_id = empty($_POST['frm_rec_facility_id']) ? 0 : trim($_POST['frm_rec_facility_id']);
     // 9/28/09
     $groups = "," . implode(',', $_POST['frm_group']) . ",";
     //	6/10/11
     if ($facility_id > 0) {
         // 9/22/09
         $query_g = "SELECT * FROM {$GLOBALS['mysql_prefix']}facilities WHERE `id`= {$facility_id} LIMIT 1";
         $result_g = mysql_query($query_g) or do_error($query_g, 'mysql query failed', mysql_error(), basename(__FILE__), __LINE__);
         $row_g = stripslashes_deep(mysql_fetch_array($result_g));
         $the_lat = $row_g['lat'];
         // use facility location
         $the_lng = $row_g['lng'];
     } else {
         $the_lat = quote_smart(trim($_POST['frm_lat']));
         // use incident location
         $the_lng = quote_smart(trim($_POST['frm_lng']));
     }
     if (strlen($the_lat) < 3 && strlen($the_lng) < 3) {
         // 1/29/11
         $the_lat = $the_lng = 0.999999;
     }
     // perform db update	//9/22/09 added facility capability, 10/1/09 added receiving facility
     @session_start();
     $by = $_SESSION['user_id'];
     //			$booked_date = empty($frm_booked_date)? "NULL" : quote_smart(trim($frm_booked_date)) ;	// 6/20/10
     $booked_date = intval(trim($_POST['frm_do_scheduled']) == 1) ? quote_smart($frm_booked_date) : "NULL";
     // 1/2/11, 1/19/10
     //			die;
     // 6/26/10
     $query = "UPDATE `{$GLOBALS['mysql_prefix']}ticket` SET \n\t\t\t\t`contact`= " . quote_smart(trim($_POST['frm_contact'])) . ",\n\t\t\t\t`street`= " . quote_smart(trim($_POST['frm_street'])) . ",\n\t\t\t\t`city`= " . quote_smart(trim($_POST['frm_city'])) . ",\n\t\t\t\t`state`= " . quote_smart(trim($_POST['frm_state'])) . ",\n\t\t\t\t`phone`= " . quote_smart(trim($_POST['frm_phone'])) . ",\n\t\t\t\t`facility`= " . quote_smart($facility_id) . ",\n\t\t\t\t`rec_facility`= " . quote_smart($rec_facility_id) . ",\n\t\t\t\t`lat`= " . $the_lat . ",\n\t\t\t\t`lng`= " . $the_lng . ",\n\t\t\t\t`scope`= " . quote_smart(trim($name_rev)) . ",\n\t\t\t\t`owner`= " . quote_smart(trim($post_frm_owner)) . ",\n\t\t\t\t`severity`= " . quote_smart(trim($_POST['frm_severity'])) . ",\n\t\t\t\t`in_types_id`= " . quote_smart(trim($_POST['frm_in_types_id'])) . ",\n\t\t\t\t`status`=" . quote_smart(trim($_POST['frm_status'])) . ",\n\t\t\t\t`problemstart`=" . quote_smart(trim($frm_problemstart)) . ",\n\t\t\t\t`problemend`=" . $frm_problemend . ",\n\t\t\t\t`description`= " . quote_smart(trim($_POST['frm_description'])) . ",\n\t\t\t\t`comments`= " . quote_smart(trim($_POST['frm_comments'])) . ",\n\t\t\t\t`nine_one_one`= " . quote_smart(trim($_POST['frm_nine_one_one'])) . ",\n\t\t\t\t`booked_date`= " . $booked_date . ",\n\t\t\t\t`date`='{$now}',\n\t\t\t\t`updated`='{$now}',\n\t\t\t\t`_by` = {$by}\n\t\t\t\tWHERE ID={$id}";
     $result = mysql_query($query) or do_error($query, 'mysql query failed', mysql_error(), basename(__FILE__), __LINE__);
     $tick_stat = $_POST['frm_status'];
     // 6/10/11
     $prob_start = quote_smart(trim($frm_problemstart));
     // 6/10/11
     foreach ($_POST['frm_group'] as $grp_val) {
         // 6/10/11
         if (test_allocates($id, $grp_val, 1)) {
             $query_a = "INSERT INTO `{$GLOBALS['mysql_prefix']}allocates` (`group` , `type`, `al_as_of` , `al_status` , `resource_id` , `sys_comments` , `user_id`) VALUES \n\t\t\t\t\t\t({$grp_val}, 1, '{$now}', {$tick_stat}, {$id}, 'Allocated to Group' , {$by})";
             $result_a = mysql_query($query_a) or do_error($query_a, 'mysql query failed', mysql_error(), basename(__FILE__), __LINE__);
         }
     }
     do_log($GLOBALS['LOG_INCIDENT_OPEN'], $id);
     if (intval($facility_id) > 0) {
         //9/22/09, 10/1/09, 3/24/10
         do_log($GLOBALS['LOG_FACILITY_INCIDENT_OPEN'], $id, '', 0, $facility_id);
         // - 7/11/10
     }
     if (intval($rec_facility_id) > 0) {
         do_log($GLOBALS['LOG_CALL_REC_FAC_SET'], $id, 0, 0, 0, $rec_facility_id);
         // 6/20/10 - 7/11/10
     }
     $the_year = date("y");
     if ((int) $inc_num_ary[0] == 3 && !($inc_num_ary[5] == $the_year)) {
         // year style and change?
         $inc_num_ary[3] = 1;
         // roll over and start at 1
         $inc_num_ary[5] = $the_year;
     } else {
         if ((int) $inc_num_ary[0] > 0) {
             // step to next no. if scheme in use
             $inc_num_ary[3]++;
             // do the deed for next use
         }
         $out_str = base64_encode(serialize($inc_num_ary));
         // 3/2/11
         $query = "UPDATE`{$GLOBALS['mysql_prefix']}settings` SET `value` = '{$out_str}' WHERE `name` = '_inc_num'";
         $result = mysql_query($query) or do_error($query, 'mysql query failed', mysql_error(), basename(__FILE__), __LINE__);
     }
     return $name_rev;
 }