function cw_gift_doc_update($doc_id, $current_order)
{
    global $tables, $cart, $config;
    if (!empty($cart['giftcerts']) && empty($current_order['products'])) {
        // Save bought certificates
        foreach ($cart['giftcerts'] as $gk => $giftcert) {
            if (empty($giftcert['gc_id'])) {
                $gcid = cw_gift_get_gcid();
                $insert_data = array('gc_id' => $gcid, 'doc_id' => $doc_id, 'purchaser' => addslashes($giftcert['purchaser']), 'recipient' => addslashes($giftcert['recipient']), 'send_via' => $giftcert['send_via'], 'recipient_email' => @$giftcert['recipient_email'], 'recipient_firstname' => addslashes(@$giftcert['recipient_firstname']), 'recipient_lastname' => addslashes(@$giftcert['recipient_lastname']), 'recipient_address' => addslashes(@$giftcert['recipient_address']), 'recipient_city' => addslashes(@$giftcert['recipient_city']), 'recipient_county' => @$giftcert['recipient_county'], 'recipient_state' => addslashes(@$giftcert['recipient_state']), 'recipient_country' => addslashes(@$giftcert['recipient_country']), 'recipient_zipcode' => addslashes(@$giftcert['recipient_zipcode']), 'recipient_phone' => addslashes(@$giftcert['recipient_phone']), 'message' => addslashes($giftcert['message']), 'amount' => $giftcert['amount'], 'debit' => $giftcert['amount'], 'status' => 'P', 'add_date' => time());
                if ($giftcert['send_via'] == 'P') {
                    $insert_data['tpl_file'] = $giftcert['tpl_file'];
                }
                cw_array2insert('giftcerts', $insert_data);
                unset($insert_data);
                $cart['giftcerts'][$gk]['gc_id'] = $gcid;
            }
        }
    }
}
     $giftcert = array('purchaser' => stripslashes($purchaser), 'recipient' => stripslashes($recipient_to), 'message' => stripslashes($message), 'amount' => $amount, 'debit' => $amount, 'send_via' => $send_via, 'tpl_file' => stripslashes($gc_template), 'recipient_email' => $recipient_email);
 } else {
     // Send via Postal Mail
     $has_states = cw_query_first_cell("SELECT display_states FROM {$tables['map_countries']} WHERE code = '" . $recipient['country'] . "'") == 'Y';
     $fill_error = $fill_error || empty($recipient_firstname) || empty($recipient_lastname) || empty($recipient_address) || empty($recipient_city) || empty($recipient_zipcode) || empty($recipient['state']) && $has_states || empty($recipient['country']);
     $giftcert = array("purchaser" => stripslashes($purchaser), "recipient" => stripslashes($recipient_to), "message" => stripslashes($message), "amount" => $amount, "debit" => $amount, "send_via" => $send_via, "recipient_firstname" => stripslashes($recipient_firstname), "recipient_lastname" => stripslashes($recipient_lastname), "recipient_address" => stripslashes($recipient_address), "recipient_city" => stripslashes($recipient_city), "recipient_zipcode" => $recipient_zipcode, "recipient_state" => $recipient['state'], "recipient_statename" => cw_get_state($recipient['state'], $recipient['country']), "recipient_country" => $recipient['country'], "recipient_countryname" => cw_get_country($recipient['country']), "recipient_phone" => $recipient_phone, "tpl_file" => $gc_template);
 }
 if (!$fill_error || $amount_error) {
     if ($action != 'preview') {
         $db_gc = $giftcert;
         foreach ($db_gc as $k => $v) {
             $db_gc[$k] = addslashes($v);
         }
     }
     if ($action == "add_gc") {
         $db_gc['gc_id'] = $gc_id = cw_gift_get_gcid();
         $db_gc['status'] = 'P';
         $db_gc['add_date'] = time();
         cw_array2insert('giftcerts', $db_gc);
         $top_message['content'] = cw_get_langvar_by_name("msg_adm_gc_add");
     } elseif ($action == "preview") {
         $giftcert['recipient_statename'] = cw_get_state($recipient['state'], $recipient['country']);
         $giftcert['recipient_countryname'] = cw_get_country($recipient['country']);
         $giftcert['gc_id'] = $gc_id;
         $smarty->assign('giftcerts', array($giftcert));
         header("Content-Type: text/html");
         header("Content-Disposition: inline; filename=giftcertificates.html");
         $_tmp_smarty_debug = $smarty->debugging;
         $smarty->debugging = false;
         if (!empty($gc_template)) {
             $css_file = preg_replace('/\\.tpl$/', '.css', $gc_template);