function smartSeo_upload($option)
{
    global $post;
    $upload = $option['std'];
    $id = $option['id'];
    $type = $option['type'];
    if (basename($_SERVER['PHP_SELF']) == "page.php" || basename($_SERVER['PHP_SELF']) == "page-new.php" || basename($_SERVER['PHP_SELF']) == "post-new.php" || basename($_SERVER['PHP_SELF']) == "post.php" || basename($_SERVER['PHP_SELF']) == "media-upload.php") {
        if ($type == 'slider') {
            $uploader .= '<input type="hidden" id="' . $id . '_type" name="' . $id . '_type" value="upload" /> ';
        } else {
            $upload = get_post_meta($post->ID, $id, true);
        }
    } elseif (smartSeo_option_exist($id)) {
        $upload = get_option($id);
    }
    //$uploader = '<br />';
    if (!empty($upload) and $type == 'upload') {
        $val = $upload;
    }
    $uploader .= '<input class="upload-input-text" name="' . $id . '" id="' . $id . '_upload" type="text" value="' . $val . '" />';
    $uploader .= '<div class="upload_button_div"><a href="#" class="button upload_button" id="' . $id . '">Upload Image</a> ';
    if (!empty($upload) and $type == 'upload') {
        $hide = '';
    } else {
        $hide = 'hide';
    }
    $uploader .= '<a href="#" class="button reset_button ' . $hide . '" id="reset_' . $id . '" title="' . $id . '">Remove</a> ';
    $uploader .= '<a href="#" class="uploadtext" id="uploadtext_' . $id . '" ></a>';
    $uploader .= '</div>' . "\n";
    $uploader .= '<div class="clear"></div>' . "\n";
    $uploader .= '<a id="uploaded_image_' . $id . '" href="' . $upload . '" target="_blank">';
    if (!empty($upload) and $type == 'upload') {
        $uploader .= '<img id="image_' . $id . '" src="' . home_url() . '/wp-content/plugins/' . PLUGIN_NAME . '/' . '/thumb.php?src=' . $upload . '&w=120" alt="" target="_blank" />';
    }
    $uploader .= '</a>';
    $uploader .= '<div class="clear"></div>' . "\n";
    return $uploader;
}
function smartSeo_save_slider($option)
{
    $id = $option['id'];
    $slider_type = $_REQUEST[$id . '_type'];
    //if slider type is post, save tags
    if ($slider_type == 'posts') {
        $nr_of_tags = $_REQUEST[$id . '_posts_tags_hidden'];
        $tagsArray = array();
        for ($k = 0; $k <= $nr_of_tags; $k++) {
            $newTAG = $_REQUEST[$id . "_posts_tags_" . $k];
            if (!in_array($newTAG, $tagsArray) && $newTAG != '' && $newTAG != '0') {
                $tagsArray[] = $newTAG;
            }
        }
        update_option($id . '_type', $slider_type);
        update_option($id . '_posts_tags', implode(',', $tagsArray));
    }
    //if slider type is categories, save categories
    if ($slider_type == 'categories') {
        $nr_of_categories = $_REQUEST[$id . '_cat_hidden'];
        $categoriesArray = array();
        for ($k = 0; $k <= $nr_of_categories; $k++) {
            $newCAT = $_REQUEST[$id . "_cat_" . $k];
            if (!in_array($newCAT, $categoriesArray) && $newCAT != '' && $newCAT != '0') {
                $categoriesArray[] = $newCAT;
            }
        }
        update_option($id . '_type', $slider_type);
        update_option($id . '_categories', implode(',', $categoriesArray));
    }
    //UPLOAD
    if ($slider_type == 'upload') {
        $uploaded_imgs = $_REQUEST[$id . '_sliderdata'];
        $k = 0;
        if ($uploaded_imgs) {
            foreach ($uploaded_imgs as $image) {
                $k++;
                $image_data_name = $id . '_sliderdata_' . $k;
                foreach ($option['fields'] as $field) {
                    $field_id = $field['id'];
                    $field_desc = $field['desc'];
                    $image[$field_id] = stripslashes(htmlentities($image[$field_id], ENT_QUOTES));
                    $field_val = $image[$field_id];
                    if ($field_val == $field_desc) {
                        $image[$field_id] = '';
                    }
                }
                //save images to db
                if (basename($_SERVER['PHP_SELF']) == 'post.php' || basename($_SERVER['PHP_SELF']) == 'page.php') {
                    update_post_meta($_POST['post_ID'], $image_data_name, $image);
                } elseif (basename($_SERVER['PHP_SELF']) == 'admin.php') {
                    update_option($image_data_name, $image);
                }
            }
        } else {
            //delete old data that not ben send this time
            $i = 1;
            //$_POST['post_ID'] = stripslashes(htmlentities($_POST['post_ID'], ENT_QUOTES));
            if (basename($_SERVER['PHP_SELF']) == 'post.php' || basename($_SERVER['PHP_SELF']) == 'page.php') {
                while (smartSeo_meta_exist($id . '_sliderdata_' . $i, $_POST['post_ID'])) {
                    if (!isset($_REQUEST[$id . '_sliderdata_' . $i])) {
                        delete_post_meta($_POST['post_ID'], $id . '_sliderdata_' . $i, get_post_meta($_POST['post_ID'], $id . '_sliderdata_' . $i, true));
                    }
                    $i++;
                }
            } elseif (basename($_SERVER['PHP_SELF']) == 'admin.php') {
                while (smartSeo_option_exist($id . '_sliderdata_' . $i)) {
                    if (!isset($_REQUEST[$id . '_sliderdata_' . $i])) {
                        delete_option($id . '_sliderdata_' . $i);
                    }
                    $i++;
                }
            }
        }
        if (basename($_SERVER['PHP_SELF']) == 'admin.php') {
            update_option($id . '_img_count', $k);
            update_option($id . '_type', $slider_type);
        }
    }
}
function smartSeo_multi($value)
{
    global $post;
    $output .= '<div class="multiple_box">';
    $hidden_name = $value['id'] . '_hidden';
    //get nr of entries
    if ($_REQUEST['page'] == 'smartSeo' || basename($_SERVER['PHP_SELF']) == "categories.php") {
        if (smartSeo_option_exist($hidden_name)) {
            $settings_hidden_name = get_option($hidden_name);
        }
    } else {
        if (smartSeo_meta_exist($hidden_name)) {
            $settings_hidden_name = get_post_meta($post->ID, $hidden_name, true);
        }
    }
    if ($settings_hidden_name == "" || $settings_hidden_name === false) {
        $settings_hidden_name = 1;
    }
    for ($i = 0; $i < $settings_hidden_name; $i++) {
        if ($value['subtype'] == 'page') {
            $select = 'Select additional page?';
            $entries = get_pages('title_li=&orderby=name');
        } elseif ($value['subtype'] == 'category') {
            $select = 'Select additional category?';
            $entries = get_categories('title_li=&orderby=name&hide_empty=0');
        } else {
            $select = 'Select additional category or page?';
            $entries_cat = get_categories('title_li=&orderby=name&hide_empty=0');
            $entries_page = get_pages('title_li=&orderby=name');
            $entries = array_merge($entries_page, $entries_cat);
        }
        $output .= '<select class="postform multiply_me disable_me" id="' . $value['id'] . '_' . $i . '" name="' . $value['id'] . '_' . $i . '"> ';
        $output .= '<option value="0">' . $select . '</option>  ';
        if ($_REQUEST['page'] == 'smartSeo' || basename($_SERVER['PHP_SELF']) == "categories.php") {
            if (smartSeo_option_exist($value['id'] . '_' . $i)) {
                $home_val = trim(get_option($value['id'] . '_' . $i));
            }
        } else {
            if (smartSeo_meta_exist($value['id'] . '_' . $i)) {
                $home_val = trim(get_post_meta($post->ID, $value['id'] . '_' . $i, true));
            }
        }
        if ($home_val == "home") {
            $selected = "selected='selected'";
        } else {
            $selected = "";
        }
        if ($value['subtype'] != 'category' && $value['subtype'] != 'page') {
            $output .= '<option ' . $selected . ' value="home">Home</option>  ';
        }
        foreach ($entries as $entry) {
            $prefixt = '';
            if ($value['subtype'] == 'page' || $entry->post_title != '') {
                if ($value['subtype'] != 'category' && $value['subtype'] != 'page') {
                    $prefixt = "Page - ";
                }
                $id = "pag_" . $entry->ID;
                $title = $prefixt . $entry->post_title;
            } else {
                if ($value['subtype'] != 'category' && $value['subtype'] != 'page') {
                    $prefixt = "Category - ";
                }
                $id = "cat_" . $entry->term_id;
                $title = $prefixt . $entry->name;
            }
            if ($_REQUEST['page'] == 'smartSeo' || basename($_SERVER['PHP_SELF']) == "categories.php") {
                if (smartSeo_option_exist($value['id'] . '_' . $i)) {
                    $val = get_option($value['id'] . '_' . $i);
                }
            } else {
                if (smartSeo_meta_exist($value['id'] . '_' . $i)) {
                    $val = get_post_meta($post->ID, $value['id'] . '_' . $i, true);
                }
            }
            if ($val == $id) {
                $selected = "selected='selected'";
            } else {
                $selected = "";
            }
            $output .= "<option {$selected} value='" . $id . "'>" . $title . "</option>";
        }
        $output .= '</select>';
    }
    if (isset($settings_hidden_name)) {
        $value['std'] = $settings_hidden_name;
    }
    $output .= '<input name="' . $hidden_name . '" class="' . $hidden_name . '" type="hidden" value="' . $settings_hidden_name . '" />';
    $output .= '</div>';
    return $output;
}