function check_selection_main()
{
    //global $b_row;
    # check the status of the block.
    ###################################################
    if (USE_LOCK_TABLES == 'Y') {
        $sql = "LOCK TABLES blocks WRITE, temp_orders WRITE, currencies READ, prices READ, banners READ";
        $result = mysql_query($sql) or die(" <b>Dear Webmaster: The current MySQL user does not have permission to lock tables. Please give this user permission to lock tables, or turn off locking in the Admin. To turn off locking in the Admin, please go to Main Config and look under the MySQL Settings.<b>");
    } else {
        // poor man's lock
        $sql = "UPDATE `config` SET `val`='YES' WHERE `key`='SELECT_RUNNING' AND `val`='NO' ";
        $result = mysql_query($sql) or die(mysql_error());
        if (mysql_affected_rows() == 0) {
            // make sure it cannot be locked for more than 30 secs
            // This is in case the proccess fails inside the lock
            // and does not release it.
            $unix_time = time();
            // get the time of last run
            $sql = "SELECT * FROM `config` where `key` = 'LAST_SELECT_RUN' ";
            $result = @mysql_query($sql);
            $t_row = @mysql_fetch_array($result);
            if ($unix_time > $t_row['val'] + 30) {
                // release the lock
                $sql = "UPDATE `config` SET `val`='NO' WHERE `key`='SELECT_RUNNING' ";
                $result = @mysql_query($sql) or die(mysql_error());
                // update timestamp
                $sql = "REPLACE INTO config (`key`, `val`) VALUES ('LAST_SELECT_RUN', '{$unix_time}')  ";
                $result = @mysql_query($sql) or die(mysql_error());
            }
            usleep(5000000);
            // this function is executing in another process. sleep for half a second
            check_selection_main();
            return;
        }
    }
    ####################################################
    $upload_image_file = get_tmp_img_name();
    $size = getimagesize($upload_image_file);
    $new_size = get_required_size($size[0], $size[1]);
    $block_id = $_REQUEST['block_id'];
    //print_r($_REQUEST);
    // get width and height of uploaded image
    //echo "[".$size[0]." ".$size[1]."] ";
    //echo $block_id;
    if (function_exists("imagecreatetruecolor")) {
        $dest = imagecreatetruecolor(BLK_WIDTH, BLK_HEIGHT);
        $whole_image = imagecreatetruecolor($new_size[0], $new_size[1]);
    } else {
        $dest = imagecreate(BLK_WIDTH, BLK_HEIGHT);
        $whole_image = imagecreate($new_size[0], $new_size[1]);
    }
    $parts = split('\\.', $upload_image_file);
    $ext = strtolower(array_pop($parts));
    //echo $ext."($upload_image_file)\n";
    switch ($ext) {
        case 'jpeg':
        case 'jpg':
            $upload_image = imagecreatefromjpeg($upload_image_file);
            break;
        case 'gif':
            $upload_image = imagecreatefromgif($upload_image_file);
            break;
        case 'png':
            $upload_image = imagecreatefrompng($upload_image_file);
            break;
    }
    // create the requ
    //$imagebg = imageCreateFromPNG (SERVER_PATH_TO_ADMIN.'temp/block.png'); // transparent PNG
    //echo GRID_BLOCK;
    $imagebg = imageCreateFromstring(GRID_BLOCK);
    imageSetTile($whole_image, $imagebg);
    imageFilledRectangle($whole_image, 0, 0, $new_size[0], $new_size[1], IMG_COLOR_TILED);
    imagecopy($whole_image, $upload_image, 0, 0, 0, 0, $size[0], $size[1]);
    //imagepng($whole_image);
    for ($i = 0; $i < $size[1]; $i += BLK_HEIGHT) {
        for ($j = 0; $j < $size[0]; $j += BLK_WIDTH) {
            $map_x = $j + $_REQUEST['map_x'];
            $map_y = $i + $_REQUEST['map_y'];
            $r_x = $map_x;
            $r_y = $map_y;
            //echo "map_x: $map_x map_y: $map_y \n";
            $GRD_WIDTH = BLK_WIDTH * G_WIDTH;
            $cb = $map_x / BLK_WIDTH + $map_y / BLK_HEIGHT * ($GRD_WIDTH / BLK_WIDTH);
            $in_str = $in_str . "{$comma}{$cb}";
            $comma = ',';
            $block_info[$cb]['map_x'] = $map_x;
            $block_info[$cb]['map_y'] = $map_y;
            // bool imagecopy ( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h )
            imagecopy($dest, $whole_image, 0, 0, $j, $i, BLK_WIDTH, BLK_HEIGHT);
            //echo "imagecopy ( $dest, $whole_image, 0, 0, $j, $i, ".BLK_HEIGHT.", '".BLK_WIDTH."' );";
            ob_start();
            imagepng($dest);
            $data = ob_get_contents();
            ob_end_clean();
            $data = base64_encode($data);
            $block_info[$cb]['image_data'] = $data;
        }
    }
    //
    imagedestroy($dest);
    imagedestroy($upload_image);
    //print_r ($block_info);
    // create a temporary order and place the blocks on a temp order
    place_temp_order($in_str, $price);
    //echo "in_str is:".$in_str;
    reserve_temp_order_pixels($block_info, $in_str);
    ###################################################
    if (USE_LOCK_TABLES == 'Y') {
        $sql = "UNLOCK TABLES";
        $result = mysql_query($sql) or die(mysql_error() . " <b>Dear Webmaster: The current MySQL user set in config.php does not have permission to lock tables. Please give this user permission to lock tables, or set USE_LOCK_TABLES to N in the config.php file that comes with this script.<b>");
    } else {
        // release the poor man's lock
        $sql = "UPDATE `config` SET `val`='NO' WHERE `key`='SELECT_RUNNING' ";
        mysql_query($sql);
        $unix_time = time();
        // update timestamp
        $sql = "REPLACE INTO config (`key`, `val`) VALUES ('LAST_SELECT_RUN', '{$unix_time}')  ";
        $result = @mysql_query($sql) or die(mysql_error());
    }
    ####################################################
}
    $BID = $_REQUEST['BID'];
}
load_banner_constants($BID);
$user_id = $_SESSION['MDS_ID'];
$filename = get_tmp_img_name();
if (file_exists($filename)) {
    $handle = fopen($filename, 'r');
    $contents = fread($handle, filesize($filename));
    $size = getimagesize($filename);
    fclose($handle);
    $image = imagecreatefromstring($contents);
} else {
    $image = imagecreatefrompng('pointer.png');
    $size = getimagesize('pointer.png');
}
$new_size = get_required_size($size[0], $size[1]);
$out = imagecreatetruecolor($new_size[0], $new_size[1]);
$imagebg = imageCreateFromPNG('block.png');
// tile filler
imageSetTile($out, $imagebg);
imageFilledRectangle($out, 0, 0, $new_size[0], $new_size[1], IMG_COLOR_TILED);
imagecopy($out, $image, 0, 0, 0, 0, $size[0], $size[1]);
if (MDS_RESIZE == 'YES') {
    // make it smaller
    $newsize_img = imagecreate($new_size[0], $new_size[1]);
    imagecopyresampled($newsize_img, $image, 0, 0, 0, 0, $new_size[0], $new_size[1], $size[0], $size[1]);
    imagecopy($out, $newsize_img, 0, 0, 0, 0, $new_size[0], $new_size[1]);
}
//);
//imagestring ($out, 2, 10, 10, 'hello'.$new_size[0]." - ".$new_size[1], 0 );
//imagedestroy($image);