function lhg_amazon_update_ajax()
{
    $pid = absint($_REQUEST['post_id']);
    $asin = $_REQUEST['asin'];
    if ($asin == "" or substr($asin, 0, 5) == "00000") {
        # No ASIN of no valid ASIN provided - spare us the hazzle
        print "ASIN not valid";
        print '<a id="more-image-options-link" class="thickbox more-image-options button" href="#TB_inline?width=700&height=550&inlineId=more-image-options">Show more options</a>';
        die;
    }
    $output = lhg_aws_get_price($asin, "com");
    list($image_url_com, $product_url_com, $price_com, $product_title, $label, $brand, $image_url_com2, $image_url_com3, $image_url_com4, $image_url_com5) = split(";;", $output);
    $product_title = str_replace("Title: ", "", $product_title);
    $output = lhg_aws_get_price($asin, "fr");
    list($image_url_fr, $product_url_fr, $price_fr) = split(";;", $output);
    $output = lhg_aws_get_price($asin, "de");
    list($image_url_de, $product_url_de, $price_de) = split(";;", $output);
    $image_url_com = str_replace("Image: ", "", $image_url_com);
    $image_url_com2 = str_replace("Image2: ", "", $image_url_com2);
    $image_url_com3 = str_replace("Image3: ", "", $image_url_com3);
    $product_url_com = str_replace("URL: ", "", $product_url_com);
    $price_com = str_replace("Price: ", "", $price_com);
    $product_url_de = str_replace("URL: ", "", $product_url_de);
    $price_de = str_replace("Price: ", "", $price_de);
    $product_url_fr = str_replace("URL: ", "", $product_url_fr);
    $price_fr = str_replace("Price: ", "", $price_fr);
    $success_image_com = $price_com != "" ? '<span class="amz-ajax-found">found</div>' : '<div class="amz-ajax-not-found">not found</div>';
    $success_image_fr = $price_fr != "" ? '<span class="amz-ajax-found">found</div>' : '<div class="amz-ajax-not-found">not found</div>';
    $success_image_de = $price_de != "" ? '<span class="amz-ajax-found">found</div>' : '<div class="amz-ajax-not-found">not found</div>';
    #
    #### add image to article, if icon empty
    #
    if ($image_url_com == "") {
        $scaled_image_url = "/wp-uploads/2013/03/noimage130.jpg";
    } else {
        $scaled_image_url = lhg_create_article_image($image_url_com, $product_title);
        $si_filename = str_replace("/wp-uploads/", "", $scaled_image_url);
        if (!has_post_thumbnail($pid)) {
            $file = "/var/www/wordpress" . $scaled_image_url;
            #print "PID: $pid";
            #print "<br>Store Thumbnail!";
            #print "<br>SIURL: $scaled_image_url";
            $wp_filetype = wp_check_filetype($file, null);
            $attachment = array('post_mime_type' => $wp_filetype['type'], 'post_title' => sanitize_title($product_title), 'post_content' => '', 'post_status' => 'inherit');
            #  var_dump($attachment);
            $attach_id = wp_insert_attachment($attachment, $si_filename, $pid);
            #print "AID: ".$attach_id;
            require_once ABSPATH . 'wp-admin/includes/image.php';
            $attach_data = wp_generate_attachment_metadata($attach_id, $si_filename);
            wp_update_attachment_metadata($attach_id, $attach_data);
            set_post_thumbnail($pid, $attach_id);
        }
    }
    echo '<div class="amz-ajax-image"><a href="' . $product_url_com . '"><img src="' . $scaled_image_url . '"/></a>';
    print '<a id="more-image-options-link" class="thickbox more-image-options button" href="#TB_inline?width=700&height=550&inlineId=more-image-options">Show more options</a>';
    echo '</div>';
    echo '
                 <div class="ajax-amazon-table">
                 <table ><tr>

                 <td>Region</td> <td>status</td>  <td>Price</td> <td> URL </td>

                 </tr>
                 <td>com</td> <td><span class="amz-ajax-return">' . $success_image_com . '</span></td>
                 <td> ' . $price_com . '</td>
                 <td> ';
    if ($product_url_com != "") {
        print '(<a href="' . $product_url_com . '">visit</a>)';
    }
    print '</td>
                 </tr>

                 </tr>
                 <td>fr</td> <td><span class="amz-ajax-return">' . $success_image_fr . '</span></td>
                 <td> ' . $price_fr . '</td>
                 <td> ';
    if ($product_url_fr != "") {
        print '(<a href="' . $product_url_fr . '">visit</a>)';
    }
    print '</td>
                 </tr>

                 </tr>
                 <td>de</td> <td><span class="amz-ajax-return">' . $success_image_de . '</span></td>
                 <td> ' . $price_de . '</td>
                 <td>';
    if ($product_url_de != "") {
        print '(<a href="' . $product_url_de . '">visit</a>)';
    }
    print '</td>
                 </tr>

                 </table>
                 </div>';
    // exchange images in Amazon image selector popup
    print '

                <script type="text/javascript">
                /* <![CDATA[ */

                jQuery(document).ready( function($) {
                ';
    if ($image_url_com != "") {
        print ' $("#imgsrc-1").attr("src","' . $image_url_com . '");';
    } else {
        print ' $("#imgsel-1").hide();';
    }
    if ($image_url_com2 != "") {
        print ' $("#imgsrc-2").attr("src","' . $image_url_com2 . '");';
    } else {
        print ' $("#imgsel-2").hide();';
    }
    if ($image_url_com3 != "") {
        print ' $("#imgsrc-3").attr("src","' . $image_url_com3 . '");';
    } else {
        print ' $("#imgsel-3").hide();';
    }
    if ($image_url_com4 != "") {
        print ' $("#imgsrc-4").attr("src","' . $image_url_com4 . '");';
    } else {
        print ' $("#imgsel-4").hide();';
    }
    #error_log(' $("#imgsrc-2").attr("src","'.$image_url_com2.'");');
    print '
                });



                /*]]> */

                </script>';
    die;
}
function lhg_update_teaser_image_ajax()
{
    global $lhg_price_db;
    $url = $_REQUEST['url'];
    $id = $_REQUEST['id'];
    $postid = $_REQUEST['postid'];
    $title = get_the_title($postid);
    $product_title = sanitize_title($title);
    #error_log("PID $postid - url: $url - id: $id - title: $product_title");
    if ($url == "") {
        $scaled_image_url = "/wp-uploads/2013/03/noimage130.jpg";
    } else {
        $scaled_image_url = lhg_create_article_image($url, $product_title);
        $si_filename = str_replace("/wp-uploads/", "", $scaled_image_url);
        //if ( !has_post_thumbnail( $postid ) ) {
        // always replace teaser image with selected one
        $file = "/var/www/wordpress" . $scaled_image_url;
        #print "PID: $pid";
        #print "<br>Store Thumbnail!";
        #print "<br>SIURL: $scaled_image_url";
        $wp_filetype = wp_check_filetype($file, null);
        $attachment = array('post_mime_type' => $wp_filetype['type'], 'post_title' => sanitize_title($product_title), 'post_content' => '', 'post_status' => 'inherit');
        #  var_dump($attachment);
        $attach_id = wp_insert_attachment($attachment, $si_filename, $pid);
        #print "AID: ".$attach_id;
        require_once ABSPATH . 'wp-admin/includes/image.php';
        $attach_data = wp_generate_attachment_metadata($attach_id, $si_filename);
        wp_update_attachment_metadata($attach_id, $attach_data);
        set_post_thumbnail($postid, $attach_id);
        //}
    }
    $response = new WP_Ajax_Response();
    $response->add(array('data' => 'success', 'supplemental' => array('file' => $si_filename)));
    $response->send();
    die;
}