function tdomf_widget_notifyme_post($args)
{
    global $current_user;
    get_currentuserinfo();
    extract($args);
    if (isset($notifyme)) {
        if (!isset($notifyme_email)) {
            if (is_user_logged_in() && tdomf_check_email_address($current_user->user_email)) {
                $notifyme_email = $current_user->user_email;
            } else {
                if (isset($whoami_email)) {
                    $notifyme_email = $whoami_email;
                } else {
                    tdomf_log_message("Could not find a email address to store for notification!", TDOMF_LOG_ERROR);
                }
            }
        }
        setcookie("tdomf_notify_widget_email", $notifyme_email, time() + 30000000, COOKIEPATH, COOKIE_DOMAIN);
        add_post_meta($post_ID, TDOMF_KEY_NOTIFY_EMAIL, $notifyme_email, true);
    }
    return NULL;
}
 function validate($args, $opts, $preview = false, $original_field_name = false)
 {
     $output = "";
     $text = false;
     // grab the input because we're going to test it
     $text = false;
     if (empty($output)) {
         if (isset($args[$this->prefix . 'tf'])) {
             $text = $args[$this->prefix . 'tf'];
         } else {
             if ($original_field_name != false && isset($args[$original_field_name])) {
                 $text = $args[$original_field_name];
             } else {
                 $output .= __("ERROR: Form is invalid. Please check TDO Mini Forms admin.", "tdomf");
             }
         }
     }
     // is it empty?
     if (empty($output) && $opts[$this->prefix . 'required']) {
         if (empty($text) || trim($text) == "" || $text == $opts[$this->prefix . 'default-text']) {
             if ($opts[$this->prefix . 'restrict-type'] == 'url') {
                 if (!empty($opts[$this->prefix . 'title'])) {
                     $output .= sprintf(__("You must specify a vaild URL for %s.", "tdomf"), $opts[$this->prefix . 'title']);
                 } else {
                     $output .= __("You must specify a valid URL.", "tdomf");
                 }
             } else {
                 if ($opts[$this->prefix . 'restrict-type'] == 'email') {
                     if (!empty($opts[$this->prefix . 'title'])) {
                         $output .= sprintf(__("You must specify a vaild email address for %s.", "tdomf"), $opts[$this->prefix . 'title']);
                     } else {
                         $output .= __("You must specify a valid email.", "tdomf");
                     }
                 } else {
                     if ($opts[$this->prefix . 'restrict-type'] == 'number') {
                         if (!empty($opts[$this->prefix . 'title'])) {
                             $output .= sprintf(__("You must specify a number for %s.", "tdomf"), $opts[$this->prefix . 'title']);
                         } else {
                             $output .= __("You must specify a number.", "tdomf");
                         }
                     } else {
                         #$opts[$this->prefix.'restrict-type'] == 'text'
                         if (!empty($opts[$this->prefix . 'title'])) {
                             $output .= sprintf(__("You must specify some %s.", "tdomf"), $opts[$this->prefix . 'title']);
                         } else {
                             $output .= __("You must specify some text.", "tdomf");
                         }
                     }
                 }
             }
         }
     }
     // is it a real email, url or number
     if (empty($output) && $opts[$this->prefix . 'restrict-type'] != 'text') {
         if ($opts[$this->prefix . 'restrict-type'] == 'url') {
             if (!tdomf_check_url($text)) {
                 if (!empty($opts[$this->prefix . 'title'])) {
                     $output .= sprintf(__("The URL \"%s\" for %s does not look correct.", "tdomf"), $text, $opts[$this->prefix . 'title']);
                 } else {
                     $output .= sprintf(__("The URL \"%s\" does not look correct.", "tdomf"), $text);
                 }
             } else {
                 if ($opts[$this->prefix . 'validate-url']) {
                     if (function_exists('wp_get_http')) {
                         $headers = wp_get_http($text, false, 1);
                         if ($headers == false) {
                             $output .= sprintf(__('The URL doesn\'t doesnt seem to exist.', 'tdomf'), $headers["response"]);
                         } else {
                             if ($headers["response"] != '200') {
                                 $output .= sprintf(__('The link doesn\'t doesnt seem to exist. Returned %d error code.', 'tdomf'), $headers["response"]);
                             }
                         }
                     }
                 }
             }
         } else {
             if ($opts[$this->prefix . 'restrict-type'] == 'email') {
                 if (!tdomf_check_email_address($text, $opts[$this->prefix . 'validate-email'])) {
                     if (!empty($opts[$this->prefix . 'title'])) {
                         $output .= sprintf(__("The email address \"%s\" for %s does not seem to be correct.", "tdomf"), $text, $opts[$this->prefix . 'title']);
                     } else {
                         $output .= sprintf(__("The email address \"%s\" does not seem to be correct.", "tdomf"), $text);
                     }
                 }
             } else {
                 if ($opts[$this->prefix . 'restrict-type'] == 'number') {
                     if (is_numeric($text)) {
                         if ($opts[$this->prefix . 'number-decimal']) {
                             $number = floatval($text);
                             if ($opts[$this->prefix . 'number-start'] !== false && $number < $opts[$this->prefix . 'number-start']) {
                                 if (!empty($opts[$this->prefix . 'title'])) {
                                     $output .= sprintf(__("%f for %s is too low. It must be equal to or greater than %f.", "tdomf"), $number, $opts[$this->prefix . 'title'], $opts[$this->prefix . 'number-start']);
                                 } else {
                                     $output .= sprintf(__("%f is too low. It must be equal to or greater than %f.", "tdomf"), $text, $opts[$this->prefix . 'number-start']);
                                 }
                             } else {
                                 if ($opts[$this->prefix . 'number-end'] !== false && $number > $opts[$this->prefix . 'number-end']) {
                                     if (!empty($opts[$this->prefix . 'title'])) {
                                         $output .= sprintf(__("%f for %s is too high. It must be equal to or less than %f.", "tdomf"), $text, $opts[$this->prefix . 'title'], $opts[$this->prefix . 'number-start']);
                                     } else {
                                         $output .= sprintf(__("%f is too high. It must be equal to or less than %f.", "tdomf"), $text, $opts[$this->prefix . 'number-start']);
                                     }
                                 }
                             }
                         } else {
                             $number = intval($text);
                             if ($opts[$this->prefix . 'number-start'] !== false && $number < $opts[$this->prefix . 'number-start']) {
                                 if (!empty($opts[$this->prefix . 'title'])) {
                                     $output .= sprintf(__("%d for %s is too low. It must be equal to or greater than %d.", "tdomf"), $number, $opts[$this->prefix . 'title'], $opts[$this->prefix . 'number-start']);
                                 } else {
                                     $output .= sprintf(__("%d is too low. It must be equal to or greater than %d.", "tdomf"), $text, $opts[$this->prefix . 'number-start']);
                                 }
                             } else {
                                 if ($opts[$this->prefix . 'number-end'] !== false && $number > $opts[$this->prefix . 'number-end']) {
                                     if (!empty($opts[$this->prefix . 'title'])) {
                                         $output .= sprintf(__("%d for %s is too high. It must be equal to or less than %d.", "tdomf"), $text, $opts[$this->prefix . 'title'], $opts[$this->prefix . 'number-start']);
                                     } else {
                                         $output .= sprintf(__("%d is too high. It must be equal to or less than %d.", "tdomf"), $text, $opts[$this->prefix . 'number-start']);
                                     }
                                 }
                             }
                         }
                     } else {
                         if (trim($text) != "") {
                             if (!empty($opts[$this->prefix . 'title'])) {
                                 $output .= sprintf(__("\"%s\" for %s is not a valid number.", "tdomf"), $text, $opts[$this->prefix . 'title']);
                             } else {
                                 $output .= sprintf(__("\"%s\" is not a valid number.", "tdomf"), $text);
                             }
                         }
                     }
                 }
             }
         }
     }
     // does it fit the counts?
     if (empty($output) && $opts[$this->prefix . 'restrict-type'] == 'text' && ($opts[$this->prefix . 'word-limit'] > 0 || $opts[$this->prefix . 'char-limit']) > 0) {
         if ($opts[$this->prefix . 'allowable-tags'] != "" && $opts[$this->prefix . 'restrict-tags']) {
             $text = strip_tags($text, $opts[$this->prefix . 'allowable-tags']);
         }
         $len = strlen($text);
         if ($opts[$this->prefix . 'char-limit'] > 0 && $len > $opts[$this->prefix . 'char-limit']) {
             if (!empty($opts[$this->prefix . 'title'])) {
                 $output .= sprintf(__("You have exceeded the max character length by %d characters for %s.", "tdomf"), $len - $opts[$this->prefix . 'char-limit'], $opts[$this->prefix . 'title']);
             } else {
                 $output .= sprintf(__("You have exceeded the max character length by %d characters.", "tdomf"), $len - $opts[$this->prefix . 'char-limit']);
             }
         } else {
             if ($opts[$this->prefix . 'word-limit'] > 0) {
                 // Remove all HTML tags as they do not count as "words"!
                 $text = trim(strip_tags($text));
                 // Replace newlines with spaces
                 $text = preg_replace("/\r?\n/", " ", $text);
                 // Remove excess whitespace
                 $text = preg_replace('/\\s\\s+/', ' ', $text);
                 // count the words!
                 $word_count = count(explode(" ", $text));
                 if ($word_count > $opts[$this->prefix . 'word-limit']) {
                     if (!empty($opts[$this->prefix . 'title'])) {
                         $output .= sprintf(__("You have exceeded the max word count by %d words for %s.", "tdomf"), $word_count - $opts[$this->prefix . 'word-limit'], $opts[$this->prefix . 'title']);
                     } else {
                         $output .= sprintf(__("You have exceeded the max word count by %d words.", "tdomf"), $word_count - $opts[$this->prefix . 'word-limit']);
                     }
                 }
             }
         }
     }
     return $output;
 }
 /**
  * Validate widget input
  * 
  * @access public
  * @return Mixed
  */
 function validate($args, $options, $preview)
 {
     // only preview - no validation required
     if ($preview) {
         return NULL;
     }
     // if user logged in, no validation required
     if (is_user_logged_in()) {
         return NULL;
     }
     // do validation
     extract($args);
     $output = "";
     if ($options['name-enable'] && $options['name-required'] && (empty($whoami_name) || trim($whoami_name) == "")) {
         $output .= __("You must specify a name.", "tdomf");
     }
     if ($options['email-enable'] && $options['email-required'] && (empty($whoami_email) || trim($whoami_email) == "")) {
         if ($output != "") {
             $output .= "<br/>";
         }
         $output .= __("You must specify a email address.", "tdomf");
     } else {
         if (($options['email-enable'] && $options['email-required'] || $options['email-enable'] && trim($whoami_email) != "") && !tdomf_check_email_address($whoami_email)) {
             if ($output != "") {
                 $output .= "<br/>";
             }
             $output .= __("Your email address does not look correct.", "tdomf");
         }
     }
     if ($options['webpage-enable'] && $options['webpage-required'] && (empty($whoami_webpage) || trim($whoami_webpage) == "")) {
         if ($output != "") {
             $output .= "<br/>";
         }
         $output .= __("You must specify a valid webpage.", "tdomf");
     } else {
         if (($options['webpage-enable'] && $options['webpage-required'] || $options['webpage-enable'] && trim($whoami_webpage) != "http://" && trim($whoami_webpage) != "") && !tdomf_check_url($whoami_webpage)) {
             if ($output != "") {
                 $output .= "<br/>";
             }
             $output .= __("Your webpage URL does not look correct.<br/>", "tdomf");
         }
     }
     // return output if any
     if ($output != "") {
         return $output;
     }
     return NULL;
 }
 /**
  * Determines if we need to show email input for this form or can the 
  * email address be sourced from elsewhere
  * 
  * @access public
  * @return Boolean
  */
 function showEmailInput($form_id)
 {
     global $current_user, $tdomf_widget_whoami;
     get_currentuserinfo();
     $show_email_input = true;
     if (is_user_logged_in() && tdomf_check_email_address($current_user->user_email)) {
         // user has already set a valid email address!
         $show_email_input = false;
     } else {
         $widgets_in_use = tdomf_get_widget_order($form_id);
         if (in_array("who-am-i", $widgets_in_use) && isset($tdomf_widget_whoami)) {
             $whoami_options = $tdomf_widget_whoami->getOptions($form_id);
             if ($whoami_options['email-enable'] && $whoami_options['email-required']) {
                 // great, who-am-i widget will provide a valid email address!
                 $show_email_input = false;
             }
         }
         if ($show_email_input && in_array('notifyme', $widgets_in_use)) {
             // just as good! Notify me will supply an email address
             $show_email_input = false;
         }
     }
     return $show_email_input;
 }
 function tdomf_widget_subscribe_to_comments_post($args)
 {
     global $current_user, $sg_subscribe;
     $options = tdomf_widget_subscribe_to_comments_get_options($args['tdomf_form_id']);
     get_currentuserinfo();
     extract($args);
     if ($options['always_subscribe'] || $subscribe_to_comments) {
         if (!isset($subscribe_to_comments_email)) {
             if (is_user_logged_in() && tdomf_check_email_address($current_user->user_email)) {
                 $subscribe_to_comments_email = $current_user->user_email;
             } else {
                 if (isset($whoami_email)) {
                     $subscribe_to_comments_email = $whoami_email;
                 } else {
                     tdomf_log_message("Could not find a email address to use for comment subscribption!", TDOMF_LOG_ERROR);
                 }
             }
         }
         /* This method doesn't work because the post must be published...
            sg_subscribe_start();
            if(is_user_logged_in()) {
                $sg_subscribe->solo_subscribe("",$post_ID);
            } else {
                $sg_subscribe->solo_subscribe($subscribe_to_comments_email,$post_ID);
            }
            if(isset($sg_subscribe->errors['solo_subscribe'])) {
                if(count($sg_subscribe->errors['solo_subscribe']) > 1) {
                    foreach($sg_subscribe->errors['solo_subscribe'] as $err) {
                        $errors = $err . "<br/>";
                    }
                    return $error;
                } else {
                    return $sg_subscribe->errors['solo_subscribe'][0];
                }
            }*/
         // this is how subscibe to email works
         add_post_meta($post_ID, '_sg_subscribe-to-comments', $subscribe_to_comments_email);
         // set comment email so that you can "unsubscribe"
         setcookie('comment_author_email_' . COOKIEHASH, $subscribe_to_comments_email, time() + 30000000, COOKIEPATH);
         setcookie('tdomf_subscribe_to_comments_widget_email', $subscribe_to_comments_email, time() + 30000000, COOKIEPATH, COOKIE_DOMAIN);
     }
     return NULL;
 }
function tdomf_handle_form_options_actions()
{
    global $wpdb, $wp_roles;
    $message = "";
    $retValue = false;
    if (!isset($wp_roles)) {
        $wp_roles = new WP_Roles();
    }
    $roles = $wp_roles->role_objects;
    $caps = tdomf_get_all_caps();
    $remove_throttle_rule = false;
    $rule_id = 0;
    if (isset($_REQUEST['tdomf_form_id'])) {
        $form_id = intval($_REQUEST['tdomf_form_id']);
        $rules = tdomf_get_option_form(TDOMF_OPTION_THROTTLE_RULES, $form_id);
        if (is_array($rules)) {
            foreach ($rules as $id => $r) {
                if (isset($_REQUEST["tdomf_remove_throttle_rule_{$id}"])) {
                    $remove_throttle_rule = true;
                    $rule_id = $id;
                    break;
                }
            }
        }
    }
    if ($remove_throttle_rule) {
        check_admin_referer('tdomf-options-save');
        unset($rules[$rule_id]);
        tdomf_set_option_form(TDOMF_OPTION_THROTTLE_RULES, $rules, $form_id);
        $message .= "Throttle rule removed!<br/>";
        tdomf_log_message("Removed throttle rule");
    } else {
        if (isset($_REQUEST['tdomf_add_throttle_rule'])) {
            check_admin_referer('tdomf-options-save');
            $form_id = intval($_REQUEST['tdomf_form_id']);
            $rule = array();
            $rule['sub_type'] = $_REQUEST['tdomf_throttle_rule_sub_type'];
            $rule['count'] = $_REQUEST['tdomf_throttle_rule_count'];
            $rule['type'] = $_REQUEST['tdomf_throttle_rule_user_type'];
            $rule['opt1'] = isset($_REQUEST['tdomf_throttle_rule_opt1']);
            $rule['time'] = intval($_REQUEST['tdomf_throttle_rule_time']);
            $rules = tdomf_get_option_form(TDOMF_OPTION_THROTTLE_RULES, $form_id);
            if (!is_array($rules)) {
                $rules = array();
            }
            $rules[] = $rule;
            tdomf_set_option_form(TDOMF_OPTION_THROTTLE_RULES, $rules, $form_id);
            $message .= "Throttle rule added!<br/>";
            tdomf_log_message("Added a new throttle rule: " . var_export($rule, true));
        } else {
            if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'create_form_page') {
                check_admin_referer('tdomf-create-form-page');
                $form_id = intval($_REQUEST['form']);
                $page_id = tdomf_create_form_page($form_id);
                $message = sprintf(__("A page with the form has been created. <a href='%s'>View page &raquo;</a><br/>", "tdomf"), get_permalink($page_id));
            } else {
                if (isset($_REQUEST['save_settings']) && isset($_REQUEST['tdomf_form_id'])) {
                    check_admin_referer('tdomf-options-save');
                    $form_id = intval($_REQUEST['tdomf_form_id']);
                    // Edit or Submit
                    $edit_form = false;
                    if (isset($_REQUEST['tdomf_mode']) && $_REQUEST['tdomf_mode'] == "edit") {
                        $edit_form = true;
                    }
                    tdomf_set_option_form(TDOMF_OPTION_FORM_EDIT, $edit_form, $form_id);
                    // Allow pages with forms to be editted
                    $edit_page_form = isset($_REQUEST['tdomf_edit_page_form']);
                    tdomf_set_option_form(TDOMF_OPTION_EDIT_PAGE_FORM, $edit_page_form, $form_id);
                    // Allow authors to edit
                    $author_edit = false;
                    if (isset($_REQUEST['tdomf_author_edit'])) {
                        $author_edit = true;
                    }
                    tdomf_set_option_form(TDOMF_OPTION_ALLOW_AUTHOR, $author_edit, $form_id);
                    // Edit post within X seconds of being published
                    $time_edit = false;
                    if (isset($_REQUEST['tdomf_time_edit'])) {
                        $time_edit = intval($_REQUEST['tdomf_time_edit']);
                        if ($time_edit <= 0) {
                            $time_edit = false;
                        }
                    }
                    tdomf_set_option_form(TDOMF_OPTION_ALLOW_TIME, $time_edit, $form_id);
                    // Who can access the form?
                    if (isset($_REQUEST['tdomf_special_access_anyone']) && tdomf_get_option_form(TDOMF_OPTION_ALLOW_EVERYONE, $form_id) == false) {
                        tdomf_set_option_form(TDOMF_OPTION_ALLOW_EVERYONE, true, $form_id);
                        foreach ($roles as $role) {
                            // remove cap as it's not needed
                            if (isset($role->capabilities[TDOMF_CAPABILITY_CAN_SEE_FORM . '_' . $form_id])) {
                                $role->remove_cap(TDOMF_CAPABILITY_CAN_SEE_FORM . '_' . $form_id);
                            }
                        }
                        tdomf_set_option_form(TDOMF_OPTION_ALLOW_CAPS, array(), $form_id);
                    } else {
                        if (!isset($_REQUEST['tdomf_special_access_anyone'])) {
                            tdomf_set_option_form(TDOMF_OPTION_ALLOW_EVERYONE, false, $form_id);
                            // add cap to right roles
                            foreach ($roles as $role) {
                                if (isset($_REQUEST["tdomf_access_" . $role->name])) {
                                    $role->add_cap(TDOMF_CAPABILITY_CAN_SEE_FORM . '_' . $form_id);
                                } else {
                                    if (isset($role->capabilities[TDOMF_CAPABILITY_CAN_SEE_FORM . '_' . $form_id])) {
                                        $role->remove_cap(TDOMF_CAPABILITY_CAN_SEE_FORM . '_' . $form_id);
                                    }
                                }
                            }
                            // list caps that can access form
                            $allow_caps = array();
                            foreach ($caps as $cap) {
                                if (isset($_REQUEST['tdomf_access_caps_' . $cap])) {
                                    $allow_caps[] = $cap;
                                }
                            }
                            tdomf_set_option_form(TDOMF_OPTION_ALLOW_CAPS, $allow_caps, $form_id);
                            // convert user names to ids
                            $allow_users = array();
                            if (isset($_REQUEST['tdomf_access_users_list'])) {
                                $user_names = trim($_REQUEST['tdomf_access_users_list']);
                                if (!empty($user_names)) {
                                    $user_names = explode(' ', $user_names);
                                    foreach ($user_names as $user_name) {
                                        if (!empty($user_name)) {
                                            if (($userdata = get_userdatabylogin($user_name)) != false) {
                                                $allow_users[] = $userdata->ID;
                                            } else {
                                                $message .= "<font color='red'>" . sprintf(__("{$user_name} is not a valid user name. Ignoring.<br/>", "tdomf"), $form_id) . "</font>";
                                                tdomf_log_message("User login {$user_name} is not recognised by wordpress. Ignoring.", TDOMF_LOG_BAD);
                                            }
                                        }
                                    }
                                }
                            }
                            tdomf_set_option_form(TDOMF_OPTION_ALLOW_USERS, $allow_users, $form_id);
                        }
                    }
                    tdomf_set_option_form(TDOMF_OPTION_ALLOW_PUBLISH, isset($_REQUEST['tdomf_user_publish_override']), $form_id);
                    // Who gets notified?
                    $notify_roles = "";
                    foreach ($roles as $role) {
                        if (isset($_REQUEST["tdomf_notify_" . $role->name])) {
                            $notify_roles .= $role->name . ";";
                        }
                    }
                    if (!empty($notify_roles)) {
                        tdomf_set_option_form(TDOMF_NOTIFY_ROLES, $notify_roles, $form_id);
                    } else {
                        tdomf_set_option_form(TDOMF_NOTIFY_ROLES, false, $form_id);
                    }
                    $save = true;
                    $tdomf_admin_emails = $_POST['tdomf_admin_emails'];
                    $emails = explode(',', $tdomf_admin_emails);
                    foreach ($emails as $email) {
                        if (!empty($email)) {
                            if (!tdomf_check_email_address($email)) {
                                $message .= "<font color='red'>" . sprintf(__("The email %s is not valid! Please update 'Who Gets Notified' with valid email addresses.", "tdomf"), $email) . "</font><br/>";
                                $save = false;
                                break;
                            }
                        }
                    }
                    if ($save) {
                        tdomf_set_option_form(TDOMF_OPTION_ADMIN_EMAILS, $tdomf_admin_emails, $form_id);
                    }
                    // Default Category
                    $def_cat = $_POST['tdomf_def_cat'];
                    tdomf_set_option_form(TDOMF_DEFAULT_CATEGORY, $def_cat, $form_id);
                    // Restrict editing to posts submitted by tdomf
                    $edit_restrict_tdomf = isset($_REQUEST['tdomf_edit_tdomf_only']);
                    tdomf_set_option_form(TDOMF_OPTION_EDIT_RESTRICT_TDOMF, $edit_restrict_tdomf, $form_id);
                    $edit_restrict_cats = explode(',', trim($_REQUEST['tdomf_edit_cats']));
                    if (!empty($edit_restrict_cats)) {
                        $cats = array();
                        foreach ($edit_restrict_cats as $cat) {
                            $cat = intval(trim($cat));
                            if ($cat > 0) {
                                $cats[] = $cat;
                            }
                        }
                        $edit_restrict_cats = $cats;
                    } else {
                        $edit_restrict_cats = array();
                    }
                    tdomf_set_option_form(TDOMF_OPTION_EDIT_RESTRICT_CATS, $edit_restrict_cats, $form_id);
                    // add edit link
                    $add_edit_link = $_REQUEST['tdomf_add_edit_link'];
                    if ($add_edit_link == 'custom') {
                        $add_edit_link = $_REQUEST['tdomf_add_edit_link_custom_url'];
                    }
                    tdomf_set_option_form(TDOMF_OPTION_ADD_EDIT_LINK, $add_edit_link, $form_id);
                    $ajax_edit = isset($_REQUEST['tdomf_ajax_edit']);
                    tdomf_set_option_form(TDOMF_OPTION_AJAX_EDIT, $ajax_edit, $form_id);
                    // auto modify edit link
                    $auto_edit_link = $_REQUEST['tdomf_auto_edit_link'];
                    if ($auto_edit_link == 'custom') {
                        $auto_edit_link = $_REQUEST['tdomf_auto_edit_link_custom_url'];
                    }
                    tdomf_set_option_form(TDOMF_OPTION_AUTO_EDIT_LINK, $auto_edit_link, $form_id);
                    //Turn On/Off Moderation
                    $mod = false;
                    if (isset($_POST['tdomf_moderation'])) {
                        $mod = true;
                    }
                    tdomf_set_option_form(TDOMF_OPTION_MODERATION, $mod, $form_id);
                    $tdomf_redirect = isset($_POST['tdomf_redirect']);
                    tdomf_set_option_form(TDOMF_OPTION_REDIRECT, $tdomf_redirect, $form_id);
                    //Preview
                    $preview = false;
                    if (isset($_POST['tdomf_preview'])) {
                        $preview = true;
                    }
                    tdomf_set_option_form(TDOMF_OPTION_PREVIEW, $preview, $form_id);
                    //From email
                    if (trim($_POST['tdomf_from_email']) == "") {
                        tdomf_set_option_form(TDOMF_OPTION_FROM_EMAIL, false, $form_id);
                    } else {
                        tdomf_set_option_form(TDOMF_OPTION_FROM_EMAIL, $_POST['tdomf_from_email'], $form_id);
                    }
                    // Form name
                    if (trim($_POST['tdomf_form_name']) == "") {
                        tdomf_set_option_form(TDOMF_OPTION_NAME, "", $form_id);
                    } else {
                        tdomf_set_option_form(TDOMF_OPTION_NAME, strip_tags($_POST['tdomf_form_name']), $form_id);
                    }
                    // Form description
                    if (trim($_POST['tdomf_form_descp']) == "") {
                        tdomf_set_option_form(TDOMF_OPTION_DESCRIPTION, false, $form_id);
                    } else {
                        tdomf_set_option_form(TDOMF_OPTION_DESCRIPTION, $_POST['tdomf_form_descp'], $form_id);
                    }
                    // Include on "your submissions" page
                    //
                    $include = false;
                    if (isset($_POST['tdomf_include_sub'])) {
                        $include = true;
                    }
                    tdomf_set_option_form(TDOMF_OPTION_INCLUDED_YOUR_SUBMISSIONS, $include, $form_id);
                    if (get_option(TDOMF_OPTION_YOUR_SUBMISSIONS) && $include) {
                        $message .= sprintf(__("Saved Options for Form %d. <a href='%s'>See your form &raquo</a>", "tdomf"), $form_id, "users.php?page=tdomf_your_submissions#tdomf_form%d") . "<br/>";
                    } else {
                        $message .= sprintf(__("Saved Options for Form %d.", "tdomf"), $form_id) . "<br/>";
                    }
                    // widget count
                    //
                    $widget_count = 10;
                    if (isset($_POST['tdomf_widget_count'])) {
                        $widget_count = intval($_POST['tdomf_widget_count']);
                    }
                    if ($widget_count < 1) {
                        $widget_count = 1;
                    }
                    tdomf_set_option_form(TDOMF_OPTION_WIDGET_INSTANCES, $widget_count, $form_id);
                    //Submit page instead of post
                    //
                    $use_page = false;
                    if (isset($_POST['tdomf_use_type']) && $_POST['tdomf_use_type'] == 'page') {
                        $use_page = true;
                    }
                    tdomf_set_option_form(TDOMF_OPTION_SUBMIT_PAGE, $use_page, $form_id);
                    // Queue period
                    //
                    $tdomf_queue_period = intval($_POST['tdomf_queue_period']);
                    tdomf_set_option_form(TDOMF_OPTION_QUEUE_PERIOD, $tdomf_queue_period, $form_id);
                    // Queue on all
                    //
                    $tdomf_queue_on_all = isset($_POST['tdomf_queue_on_all']);
                    tdomf_set_option_form(TDOMF_OPTION_QUEUE_ON_ALL, $tdomf_queue_on_all, $form_id);
                    // ajax
                    //
                    $tdomf_ajax = isset($_POST['tdomf_ajax']);
                    tdomf_set_option_form(TDOMF_OPTION_AJAX, $tdomf_ajax, $form_id);
                    // Send moderation email even for published posts
                    //
                    $tdomf_mod_email_on_pub = isset($_POST['tdomf_mod_email_on_pub']);
                    tdomf_set_option_form(TDOMF_OPTION_MOD_EMAIL_ON_PUB, $tdomf_mod_email_on_pub, $form_id);
                    // Admin users auto-publish?
                    //
                    $tdomf_publish_no_mod = isset($_POST['tdomf_user_publish_auto']);
                    tdomf_set_option_form(TDOMF_OPTION_PUBLISH_NO_MOD, $tdomf_publish_no_mod, $form_id);
                    // Spam
                    //
                    $message .= tdomf_handle_spam_options_actions($form_id);
                    tdomf_log_message("Options Saved for Form ID {$form_id}");
                } else {
                    if (isset($_REQUEST['delete'])) {
                        $form_id = intval($_REQUEST['delete']);
                        check_admin_referer('tdomf-delete-form-' . $form_id);
                        if (tdomf_form_exists($form_id)) {
                            $count_forms = count(tdomf_get_form_ids());
                            if ($count_forms > 1) {
                                if (tdomf_delete_form($form_id)) {
                                    $message .= sprintf(__("Form %d deleted.<br/>", "tdomf"), $form_id);
                                } else {
                                    $message .= sprintf(__("Could not delete Form %d!<br/>", "tdomf"), $form_id);
                                }
                            } else {
                                $message .= sprintf(__("You cannot delete the last form! There must be at least one form in the system.<br/>", "tdomf"), $form_id);
                            }
                        } else {
                            $message .= sprintf(__("Form %d is not valid!<br/>", "tdomf"), $form_id);
                        }
                    } else {
                        if (isset($_REQUEST['copy'])) {
                            $form_id = intval($_REQUEST['copy']);
                            check_admin_referer('tdomf-copy-form-' . $form_id);
                            $copy_form_id = tdomf_copy_form($form_id);
                            if ($copy_form_id != 0) {
                                $message .= sprintf(__("Form %d copied with id %d.<br/>", "tdomf"), $form_id, $copy_form_id);
                                $retValue = $copy_form_id;
                            } else {
                                $message .= sprintf(__("Failed to copy Form %d!<br/>", "tdomf"), $form_id);
                            }
                        } else {
                            if (isset($_REQUEST['new'])) {
                                check_admin_referer('tdomf-new-form');
                                $form_id = tdomf_create_form(__('New Form', 'tdomf'), array());
                                if ($form_id != 0) {
                                    $message .= sprintf(__("New form created with %d.<br/>", "tdomf"), $form_id);
                                    $retValue = $form_id;
                                } else {
                                    $message .= __("Failed to create new Form!<br/>", "tdomf");
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    // Warnings
    $message .= tdomf_get_error_messages(false);
    if (!empty($message)) {
        ?>
   <div id="message" class="updated fade"><p><?php 
        echo $message;
        ?>
</p></div>
   <?php 
    }
    return $retValue;
}