function auiu_taxonomy_fields_edit()
{
    global $wpdb, $custom_fields;
    $id = intval($_GET['id']);
    ?>
    <div class="wrap">
        <?php 
    //update the fields
    if (isset($_POST['auiu_edit_custom'])) {
        check_admin_referer('auiu_edit', 'auiu_edit');
        $error = false;
        if ($_POST['field'] == '') {
            $error = 'Please enter field name';
        } else {
            if ($_POST['label'] == '') {
                $error = 'Please enter label name';
            }
        }
        if (!$error) {
            //no errors
            //whatever, insert the values
            if (!auiu_starts_with($_POST['field'], 'ct_')) {
                $_POST['field'] = 'ct_' . $_POST['field'];
            }
            $data = array('field' => $_POST['field'], 'label' => $_POST['label'], 'desc' => $_POST['help'], 'required' => $_POST['required'], 'region' => $_POST['region'], 'order' => $_POST['order'], 'type' => $_POST['type'], 'values' => $_POST['field_values']);
            //var_dump($data);
            $result = $wpdb->update($wpdb->prefix . 'auiu_customfields', $data, array('id' => $id), array('%s', '%s', '%s', '%s', '%s', '%d', '%s', '%s'), array('%d'));
            //if row inserted
            if ($result) {
                echo '<div class="updated"><p><strong>Field Updated</strong></p></div>';
            } else {
                echo "<div class='error'><p><strong>Something went wrong or you didn't changed anything</strong></p></div>";
            }
        } else {
            //we got some error
            echo '<div class="error"><p><strong>' . $error . '</strong></p></div>';
        }
    }
    //finished updating
    //now show it
    $row = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}auiu_customfields WHERE `id`={$id}", OBJECT);
    $values = array("field" => "{$row->field}", "label" => "{$row->label}", "help" => "{$row->desc}", "required" => "{$row->required}", "region" => "{$row->region}", "order" => "{$row->order}", "type" => "{$row->type}");
    ?>
        <?php 
    if ($row) {
        ?>
            <form action="" method="post" style="margin-top: 20px;">
                <?php 
        wp_nonce_field('auiu_edit', 'auiu_edit');
        ?>
                <table class="widefat meta" style="width: 850px">
                    <thead>
                        <tr>
                            <th scope="col" colspan="2" style="font-size: 14px;">Edit Custom Field</th>
                        </tr>
                    </thead>
                    <?php 
        auiu_build_form($custom_fields, $values, false);
        ?>
                    <tr valign="top" id="auiu_field_values_row" style="display: none;">
                        <td scope="row" class="label"><label for="auiu_field_values">Values</label></td>
                        <td>
                            <textarea name="field_values" id="auiu_field_values" cols="30"><?php 
        echo $row->values;
        ?>
</textarea>
                            <span class="description"><br>This will be used as option fields. Please separate values with comma</span>
                        </td>
                    </tr>
                </table>
                <input name="auiu_edit_custom" type="submit" class="button-primary" value="<?php 
        _e('Update Field');
        ?>
" style="margin-top: 10px;" />
            </form>
        <?php 
    } else {
        ?>
            <h2>Nothing found</h2>
        <?php 
    }
    ?>
    </div>
    <?php 
}
function auiu_build_custom_field_form($position = 'top', $edit = false, $post_id = 0)
{
    global $wpdb;
    //check, if custom field is enabled
    $enabled = auiu_get_option('enable_custom_field', 'auiu_frontend_posting', 'off');
    //var_dump( $enabled );
    if ($enabled != 'on') {
        return false;
    }
    $table = $wpdb->prefix . 'auiu_customfields';
    $results = $wpdb->get_results("SELECT * FROM {$table} WHERE `region`='{$position}' ORDER BY `order`", OBJECT);
    if (is_array($results)) {
        foreach ($results as $field) {
            if (auiu_starts_with($field->field, 'cf_')) {
                if ($edit && $post_id) {
                    $value = get_post_meta($post_id, $field->field, true);
                } else {
                    $value = '';
                }
                switch ($field->type) {
                    case 'text':
                        ?>
                        <li>
                            <label for="<?php 
                        echo $field->field;
                        ?>
">
                                <?php 
                        echo stripslashes($field->label);
                        ?>
                                <?php 
                        if ($field->required == 'yes') {
                            ?>
                                    <span class="required">*</span>
                                <?php 
                        }
                        ?>
                            </label>
                            <?php 
                        $class = $field->required == 'yes' ? 'requiredField' : '';
                        ?>
                            <input class="<?php 
                        echo $class;
                        ?>
" type="text" name="<?php 
                        echo $field->field;
                        ?>
" id="<?php 
                        echo $field->field;
                        ?>
" minlength="2" value="<?php 
                        echo stripslashes($value);
                        ?>
"
                           <?php 
                        if ($field->desc) {
                            ?>
								 placeholder="<?php 
                            echo stripslashes($field->desc);
                            ?>
"
                            <?php 
                        }
                        ?>
>
                            <div class="clear"></div>
 
                        </li>
                        <?php 
                        break;
                    case 'textarea':
                        ?>
                        <li>
                            <label for="<?php 
                        echo $field->field;
                        ?>
">
                                <?php 
                        echo stripslashes($field->label);
                        ?>
                                <?php 
                        if ($field->required == 'yes') {
                            ?>
                                    <span class="required">*</span>
                                <?php 
                        }
                        ?>
                            </label>
                            <?php 
                        $class = $field->required == 'yes' ? 'requiredField' : '';
                        ?>
                            <textarea class="<?php 
                        echo $class;
                        ?>
" name="<?php 
                        echo $field->field;
                        ?>
" id="<?php 
                        echo $field->field;
                        ?>
"><?php 
                        echo stripslashes($value);
                        ?>
</textarea>
                            <div class="clear"></div>
                            <?php 
                        if ($field->desc) {
                            ?>
                                <p class="description"><?php 
                            echo stripslashes($field->desc);
                            ?>
</p>
                                <div class="clear"></div>
                            <?php 
                        }
                        ?>
                        </li>
                        <?php 
                        break;
                    case 'select':
                        ?>
                        <li>
                            <label for="<?php 
                        echo $field->field;
                        ?>
">
                                <?php 
                        echo stripslashes($field->label);
                        ?>
                                <?php 
                        if ($field->required == 'yes') {
                            ?>
                                    <span class="required">*</span>
                                <?php 
                        }
                        ?>
                            </label>
                            <select name="<?php 
                        echo $field->field;
                        ?>
">
                                <?php 
                        $options = explode(',', $field->values);
                        if (is_array($options)) {
                            foreach ($options as $opt) {
                                $opt = trim(strip_tags($opt));
                                echo "<option value='{$opt}' " . selected($value, $opt, false) . ">{$opt}</option>";
                            }
                        }
                        ?>
                            </select>
                            <div class="clear"></div>
                            <?php 
                        if ($field->desc) {
                            ?>
                                <p class="description"><?php 
                            echo stripslashes($field->desc);
                            ?>
</p>
                                <div class="clear"></div>
                            <?php 
                        }
                        ?>
                        </li>
                        <?php 
                        break;
                    case 'checkbox':
                        ?>
                        <li>
                            <label for="<?php 
                        echo $field->field;
                        ?>
">
                                <?php 
                        echo stripslashes($field->label);
                        ?>
                                <?php 
                        if ($field->required == 'yes') {
                            ?>
                                    <span class="required">*</span>
                                <?php 
                        }
                        ?>
                            </label>
                            <div class="auiu-check-container">
                                <input type="hidden" name="<?php 
                        echo esc_attr($field->field);
                        ?>
" value="" />
                                <?php 
                        $options = explode(',', $field->values);
                        $values = explode(',', $value);
                        if (is_array($options)) {
                            foreach ($options as $opt) {
                                $opt = trim(strip_tags($opt));
                                ?>
										 <?php 
                                $class = $field->required == 'yes' ? 'requiredField' : '';
                                ?>
                                            <input type="checkbox" id="auiu-checkbox" class="<?php 
                                echo $class;
                                ?>
" <?php 
                                echo in_array($opt, $values) ? 'checked="checked"' : '';
                                ?>
 name="<?php 
                                echo esc_attr($field->field);
                                ?>
[]" value="<?php 
                                echo esc_attr($opt);
                                ?>
" /> <span><?php 
                                echo $opt;
                                ?>
</span>
                                        </label>
                                <?php 
                            }
                        }
                        ?>
                            </div>
                            <div class="clear"></div>
                            <?php 
                        if ($field->desc) {
                            ?>
                                <p class="description"><?php 
                            echo stripslashes($field->desc);
                            ?>
</p>
                                <div class="clear"></div>
                            <?php 
                        }
                        ?>
                        </li>
                        <?php 
                        break;
                    default:
                }
                //switch
            } else {
                switch ($field->type) {
                    case 'text':
                        ?>
                        <li>
                            <label for="<?php 
                        echo $field->field;
                        ?>
">
                                <?php 
                        echo stripslashes($field->label);
                        ?>
                                <?php 
                        if ($field->required == 'yes') {
                            ?>
                                    <span class="required">*</span>
                                <?php 
                        }
                        ?>
                            </label>
                            <?php 
                        $class = $field->required == 'yes' ? 'requiredField' : '';
                        ?>
                            <input class="<?php 
                        echo $class;
                        ?>
" type="text" name="<?php 
                        echo $field->field;
                        ?>
" id="<?php 
                        echo $field->field;
                        ?>
" minlength="2" value="<?php 
                        echo stripslashes($value);
                        ?>
">
                            <div class="clear"></div>
                            <?php 
                        if ($field->desc) {
                            ?>
                                <p class="description"><?php 
                            echo stripslashes($field->desc);
                            ?>
</p>
                                <div class="clear"></div>
                            <?php 
                        }
                        ?>
                        </li>
                        <?php 
                        break;
                    case 'select':
                        $fld = substr($field->field, 3);
                        $terms = get_terms($fld);
                        //var_dump( $fld );
                        if ($terms) {
                            foreach ($terms as $t) {
                                $term_option .= '<option  value="' . $t->term_id . '">' . $t->name . '</option>';
                            }
                        }
                        ?>
                        <li>
                            <label for="<?php 
                        echo $field->field;
                        ?>
">
                                <?php 
                        echo stripslashes($field->label);
                        ?>
                                <?php 
                        if ($field->required == 'yes') {
                            ?>
                                    <span class="required">*</span>
                                <?php 
                        }
                        ?>
                            </label>
                            <select name="<?php 
                        echo $field->field;
                        ?>
">
                                <?php 
                        echo $term_option;
                        ?>
                            </select>
                            <div class="clear"></div>
                            <?php 
                        if ($field->desc) {
                            ?>
                                <p class="description"><?php 
                            echo stripslashes($field->desc);
                            ?>
</p>
                                <div class="clear"></div>
                            <?php 
                        }
                        ?>
                        </li>
                    <?php 
                    default:
                }
            }
        }
        //foreach
    }
    // is_array
}
function auiu_custom_fields_edit()
{
    global $wpdb, $custom_fields;
    $id = intval($_GET['id']);
    ?>
    <div class="wrap auiu-admin">
        <?php 
    //update the fields
    if (isset($_POST['auiu_edit_custom'])) {
        check_admin_referer('auiu_edit', 'auiu_edit');
        $error = false;
        if ($_POST['field'] == '') {
            $error = 'Please enter field name';
        } else {
            if ($_POST['label'] == '') {
                $error = 'Please enter label name';
            }
        }
        if (!$error) {
            //no errors
            //whatever, insert the values
            if (!auiu_starts_with($_POST['field'], 'cf_')) {
                $_POST['field'] = 'cf_' . $_POST['field'];
            }
            $data = array('field' => $_POST['field'], 'label' => $_POST['label'], 'desc' => $_POST['help'], 'required' => $_POST['required'], 'region' => $_POST['region'], 'order' => $_POST['order'], 'type' => $_POST['type'], 'values' => $_POST['field_values']);
            //var_dump($data);
            $result = $wpdb->update($wpdb->prefix . 'auiu_customfields', $data, array('id' => $id), array('%s', '%s', '%s', '%s', '%s', '%d', '%s', '%s'), array('%d'));
            //if row inserted
            if ($result) {
                echo '<div class="updated"><p><strong>Field Updated</strong></p></div>';
            } else {
                echo "<div class='error'><p><strong>Something went wrong or you didn't changed anything</strong></p></div>";
            }
        } else {
            //we got some error
            echo '<div class="error"><p><strong>' . $error . '</strong></p></div>';
        }
    }
    //finished updating
    //now show it
    $row = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}auiu_customfields WHERE `id`={$id}");
    //var_dump( $row );
    if ($row) {
        ?>
            <form action="" method="post" style="margin-top: 20px;">
                <?php 
        wp_nonce_field('auiu_edit', 'auiu_edit');
        ?>
                <table class="widefat meta" style="width: 850px">
                    <thead>
                        <tr>
                            <th scope="col" colspan="2" style="font-size: 14px;">Edit Custom Field</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr valign="top">
                            <td scope="row" class="label"><label for="field"><?php 
        _e('Field Name', 'auiu');
        ?>
</label></td>
                            <td>
                                <input type="text" size="25" style="" id="field" value="<?php 
        echo esc_attr($row->field);
        ?>
" name="field" />
                                <span class="description"><?php 
        _e('Name without space. Will be used to store the value in this custom field', 'auiu');
        ?>
</span>
                            </td>
                        </tr>
                        <tr valign="top">
                            <td scope="row" class="label"><label for="label"><?php 
        _e('Label', 'auiu');
        ?>
</label></td>
                            <td>
                                <input type="text" size="25" style="" id="label" value="<?php 
        echo esc_attr($row->label);
        ?>
" name="label" />
                                <span class="description"><?php 
        _e('This will be used as your input fields title', 'auiu');
        ?>
</span>
                            </td>
                        </tr>
                        <tr valign="top">
                            <td scope="row" class="label"><label for="help"><?php 
        _e('Help Text', 'auiu');
        ?>
</label></td>
                            <td>
								<textarea name="help" cols="60" id="help"><?php 
        echo esc_textarea($row->desc);
        ?>
</textarea>
                                <span class="description"><?php 
        _e('Text will be shown to user as help text', 'auiu');
        ?>
</span>
                            </td>
                        </tr>
                        <tr valign="top">
                            <td scope="row" class="label"><label for="required"><?php 
        _e('Required', 'auiu');
        ?>
</label></td>
                            <td>
                                <select id="required" name="required">
                                    <option value="no"<?php 
        selected($row->required, 'no');
        ?>
><?php 
        _e('No', 'auiu');
        ?>
</option>
                                    <option value="yes"<?php 
        selected($row->required, 'yes');
        ?>
><?php 
        _e('Yes', 'auiu');
        ?>
</option>
                                </select>
                                <span class="description"><?php 
        _e('A validation criteria. User must provide input in that field', 'auiu');
        ?>
</span>
                            </td>
                        </tr>
                        <tr valign="top">
                            <td scope="row" class="label"><label for="region"><?php 
        _e('Region', 'auiu');
        ?>
</label></td>
                            <td>
                                <select id="region" name="region">
                                    <option value="top"<?php 
        selected($row->region, 'top');
        ?>
><?php 
        _e('Top', 'auiu');
        ?>
</option>
                                    <option value="description"<?php 
        selected($row->region, 'description');
        ?>
><?php 
        _e('Before Description', 'auiu');
        ?>
</option>
                                    <option value="tag"<?php 
        selected($row->region, 'tag');
        ?>
><?php 
        _e('After Description', 'auiu');
        ?>
</option>
                                    <option value="bottom"<?php 
        selected($row->region, 'bottom');
        ?>
><?php 
        _e('Bottom', 'auiu');
        ?>
</option>
                                </select>
                                <span class="description"><?php 
        _e('Where do you want to show this input field?', 'auiu');
        ?>
</span>
                            </td>
                        </tr>
                        <tr valign="top">
                            <td scope="row" class="label"><label for="order"><?php 
        _e('Order', 'auiu');
        ?>
</label></td>
                            <td>
                                <input name="order" value="<?php 
        echo esc_attr($row->order);
        ?>
" id="order" style="" size="2" type="text">
                                <span class="description"><?php 
        _e('Which order this input field will show in a region', 'auiu');
        ?>
</span>
                            </td>
                        </tr>
                        <tr valign="top">
                            <td scope="row" class="label"><label for="type"><?php 
        _e('Type', 'auiu');
        ?>
</label></td>
                            <td>
                                <select name="type" id="type" onchange="auiu_show(this)">
                                    <option value="text"<?php 
        selected($row->type, 'text');
        ?>
><?php 
        _e('Text Box', 'auiu');
        ?>
</option>
                                    <option value="textarea"<?php 
        selected($row->type, 'textarea');
        ?>
><?php 
        _e('Text Area', 'auiu');
        ?>
</option>
                                    <option value="select"<?php 
        selected($row->type, 'select');
        ?>
><?php 
        _e('Dropdown', 'auiu');
        ?>
</option>
                                    <option value="checkbox"<?php 
        selected($row->type, 'checkbox');
        ?>
><?php 
        _e('Checkbox', 'auiu');
        ?>
</option>
                                </select>
                                <span class="description"></span>
                            </td>
                        </tr>
                        <tr valign="top" id="auiu_field_values_row" style="display: none;">
                            <td scope="row" class="label"><label for="auiu_field_values"><?php 
        _e('Values', 'auiu');
        ?>
</label></td>
                            <td>
                                <textarea name="field_values" id="auiu_field_values" cols="30"><?php 
        echo esc_textarea($row->values);
        ?>
</textarea>
                                <span class="description"><br><?php 
        _e('This will be used as option fields. Please separate values with comma', 'auiu');
        ?>
</span>
                            </td>
                        </tr>
                    </tbody>
                </table>
                <input name="auiu_edit_custom" type="submit" class="button-primary" value="<?php 
        _e('Update Field', 'auiu');
        ?>
" style="margin-top: 10px;" />
            </form>
        <?php 
    } else {
        ?>
            <h2><?php 
        _e('Nothing found', 'auiu');
        ?>
</h2>
		<?php 
    }
    ?>
    </div>
    <?php 
}