function qs_service()
{
    global $q_config, $qs_public_key, $qs_error_messages;
    if (!isset($_REQUEST['post'])) {
        echo '<script type="text/javascript">document.location="edit.php";</script>';
        printf(__('To translate a post, please go to the <a href="%s">edit posts overview</a>.', 'qtranslate'), 'edit.php');
        exit;
    }
    $post_id = intval($_REQUEST['post']);
    $confirm = isset($_GET['confirm']) ? true : false;
    $translate_from = '';
    $translate_to = '';
    $translate_from_name = '';
    $translate_to_name = '';
    if (isset($_REQUEST['source_language']) && ppqtrans_isEnabled($_REQUEST['source_language'])) {
        $translate_from = $_REQUEST['source_language'];
    }
    if (isset($_REQUEST['target_language']) && ppqtrans_isEnabled($_REQUEST['target_language'])) {
        $translate_to = $_REQUEST['target_language'];
    }
    if ($translate_to == $translate_from) {
        $translate_to = '';
    }
    $post =& get_post($post_id);
    if (!$post) {
        printf(__('Post with id "%s" not found!', 'qtranslate'), $post_id);
        return;
    }
    $default_service = intval(get_option('qs_default_service'));
    $service_settings = get_option('qs_service_settings');
    // Detect available Languages and possible target languages
    $available_languages = ppqtrans_getAvailableLanguages($post->post_content);
    if (sizeof($available_languages) == 0) {
        $error = __('The requested Post has no content, no Translation possible.', 'qtranslate');
    }
    // try to guess source and target language
    if (!in_array($translate_from, $available_languages)) {
        $translate_from = '';
    }
    $missing_languages = array_diff($q_config['enabled_languages'], $available_languages);
    if (empty($translate_from) && in_array($q_config['default_language'], $available_languages) && $translate_to != $q_config['default_language']) {
        $translate_from = $q_config['default_language'];
    }
    if (empty($translate_to) && sizeof($missing_languages) == 1) {
        $translate_to = $missing_languages[0];
    }
    if (in_array($translate_to, $available_languages)) {
        $message = __('The Post already has content for the selected target language. If a translation request is send, the current text for the target language will be overwritten.', 'qtranslate');
    }
    if (sizeof($available_languages) == 1) {
        if ($available_languages[0] == $translate_to) {
            unset($translate_to);
        }
        $translate_from = $available_languages[0];
    } elseif ($translate_from == '' && sizeof($available_languages) > 1) {
        $languages = ppqtrans_getSortedLanguages();
        foreach ($languages as $language) {
            if ($language != $translate_to && in_array($language, $available_languages)) {
                $translate_from = $language;
                break;
            }
        }
    }
    // link to current page with get variables
    $url_link = add_query_arg('post', $post_id);
    if (!empty($translate_to)) {
        $url_link = add_query_arg('target_language', $translate_to, $url_link);
    }
    if (!empty($translate_from)) {
        $url_link = add_query_arg('source_language', $translate_from, $url_link);
    }
    // get correct title and content
    $post_title = ppqtrans_use($translate_from, $post->post_title);
    $post_content = ppqtrans_use($translate_from, $post->post_content);
    $post_excerpt = ppqtrans_use($translate_from, $post->post_excerpt);
    if (!empty($translate_from)) {
        $translate_from_name = $q_config['language_name'][$translate_from];
    }
    if (!empty($translate_to)) {
        $translate_to_name = $q_config['language_name'][$translate_to];
    }
    if (!empty($translate_from) && !empty($translate_to)) {
        $title = sprintf('Translate &quot;%1$s&quot; from %2$s to %3$s', htmlspecialchars($post_title), $translate_from_name, $translate_to_name);
    } elseif (!empty($translate_from)) {
        $title = sprintf('Translate &quot;%1$s&quot; from %2$s', htmlspecialchars($post_title), $translate_from_name);
    } else {
        $title = sprintf('Translate &quot;%1$s&quot;', htmlspecialchars($post_title));
    }
    // Check data
    if (isset($_POST['service_id'])) {
        $service_id = intval($_POST['service_id']);
        $default_service = $service_id;
        update_option('qs_default_service', $service_id);
        $order_key = substr(md5(time() . AUTH_KEY), 0, 20);
        $request = array('order_service_id' => $service_id, 'order_url' => get_option('home'), 'order_key' => $order_key, 'order_title' => $post_title, 'order_text' => $post_content, 'order_excerpt' => $post_excerpt, 'order_source_language' => $translate_from, 'order_source_locale' => $q_config['locale'][$translate_from], 'order_target_language' => $translate_to, 'order_target_locale' => $q_config['locale'][$translate_to]);
        // check for additional fields
        if (isset($service_settings[$service_id]) && is_array($service_settings[$service_id])) {
            $request['order_required_field'] = array();
            foreach ($service_settings[$service_id] as $setting => $value) {
                $request['order_required_field'][$setting] = $value;
            }
        }
        if (isset($_POST['token'])) {
            $request['order_token'] = $_POST['token'];
        }
        $answer = qs_queryQS(QS_INIT_TRANSLATION, $request);
        if (isset($answer['error'])) {
            $error = sprintf(__('An error occured: %s', 'qtranslate'), $qs_error_messages[$answer['error']]);
            if ($answer['message'] != '') {
                $error .= '<br />' . sprintf(__('Additional information: %s', 'qtranslate'), ppqtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($answer['message']));
            }
        }
        if (isset($answer['order_id'])) {
            $orders = get_option('qs_orders');
            if (!is_array($orders)) {
                $orders = array();
            }
            $orders[] = array('post_id' => $post_id, 'service_id' => $service_id, 'source_language' => $translate_from, 'target_language' => $translate_to, 'order' => array('order_key' => $order_key, 'order_id' => $answer['order_id']));
            update_option('qs_orders', $orders);
            if (empty($answer['message'])) {
                $order_completed_message = '';
            } else {
                $order_completed_message = htmlspecialchars($answer['message']);
            }
            qs_UpdateOrder($answer['order_id']);
        }
    }
    if (isset($error)) {
        ?>
<div class="wrap">
<h2><?php 
        _e('qTranslate Services', 'qtranslate');
        ?>
</h2>
<div id="message" class="error fade"><p><?php 
        echo $error;
        ?>
</p></div>
<p><?php 
        printf(__('An serious error occured and qTranslate Services cannot proceed. For help, please visit the <a href="%s">Support Forum</a>', 'qtranslate'), 'http://www.qianqin.de/qtranslate/forum/');
        ?>
</p>
</div>
<?php 
        return;
    }
    if (isset($order_completed_message)) {
        ?>
<div class="wrap">
<h2><?php 
        _e('qTranslate Services', 'qtranslate');
        ?>
</h2>
<div id="message" class="updated fade"><p><?php 
        _e('Order successfully sent.', 'qtranslate');
        ?>
</p></div>
<p><?php 
        _e('Your translation order has been successfully transfered to the selected service.', 'qtranslate');
        ?>
</p>
<?php 
        if (!empty($order_completed_message)) {
            ?>
<p><?php 
            printf(__('The service returned this message: %s', 'qtranslate'), $order_completed_message);
            ?>
</p>
<?php 
        }
        ?>
<p><?php 
        _e('Feel free to choose an action:', 'qtranslate');
        ?>
</p>
<ul>
	<li><a href="<?php 
        echo add_query_arg('target_language', null, $url_link);
        ?>
"><?php 
        _e('Translate this post to another language.', 'qtranslate');
        ?>
</a></li>
	<li><a href="edit.php"><?php 
        _e('Translate a different post.', 'qtranslate');
        ?>
</a></li>
	<li><a href="options-general.php?page=qtranslate#ppqtranslate_service_settings"><?php 
        _e('View all open orders.', 'qtranslate');
        ?>
</a></li>
	<li><a href="options-general.php?page=qtranslate&qs_cron=true#ppqtranslate_service_settings"><?php 
        _e('Let qTranslate Services check if any open orders are finished.', 'qtranslate');
        ?>
</a></li>
	<li><a href="<?php 
        echo get_permalink($post_id);
        ?>
 "><?php 
        _e('View this post.', 'qtranslate');
        ?>
</a></li>
</ul>
</div>
<?php 
        return;
    }
    ?>
<div class="wrap">
<h2><?php 
    _e('qTranslate Services', 'qtranslate');
    ?>
</h2>
<?php 
    if (!empty($message)) {
        ?>
<div id="message" class="updated fade"><p><?php 
        echo $message;
        ?>
</p></div>
<?php 
    }
    ?>
<h3><?php 
    echo $title;
    ?>
</h3>
<form action="edit.php?page=ppqtranslate_services" method="post" id="qtranslate-services-translate">
<p><?php 
    if (sizeof($available_languages) > 1) {
        $available_languages_name = array();
        foreach (array_diff($available_languages, array($translate_from)) as $language) {
            $available_languages_name[] = '<a href="' . add_query_arg('source_language', $language, $url_link) . '">' . $q_config['language_name'][$language] . '</a>';
        }
        $available_languages_names = join(", ", $available_languages_name);
        printf(__('Your article is available in multiple languages. If you do not want to translate from %1$s, you can switch to one of the following languages: %2$s', 'qtranslate'), $q_config['language_name'][$translate_from], $available_languages_names);
    }
    ?>
</p>
<input type="hidden" name="post" value="<?php 
    echo $post_id;
    ?>
"/>
<input type="hidden" name="source_language" value="<?php 
    echo $translate_from;
    ?>
"/>
<?php 
    if (empty($translate_to)) {
        ?>
<p><?php 
        _e('Please choose the language you want to translate to:', 'qtranslate');
        ?>
</p>
<ul>
<?php 
        foreach ($q_config['enabled_languages'] as $language) {
            if ($translate_from == $language) {
                continue;
            }
            ?>
	<li><label><input type="radio" name="target_language" value="<?php 
            echo $language;
            ?>
" /> <?php 
            echo $q_config['language_name'][$language];
            ?>
</li>
<?php 
        }
        ?>
</ul>
	<p class="submit">
		<input type="submit" name="submit" class="button-primary" value="<?php 
        _e('Continue', 'qtranslate');
        ?>
" />
	</p>
<?php 
    } else {
        ?>
<input type="hidden" name="target_language" value="<?php 
        echo $translate_to;
        ?>
"/>
<?php 
        if ($confirm) {
            ?>
	<input type="hidden" name="service_id" value="<?php 
            echo $_REQUEST['service_id'];
            ?>
"/>
	<input type="hidden" name="token" value="<?php 
            echo $_REQUEST['token'];
            ?>
"/>
	<div id="submitboxcontainer" class="metabox-holder">
		<div id="submitdiv" class="postbox">
			<h3 class="hndle"><?php 
            _e('Confirm Order', 'qtranslate');
            ?>
</h3>
			<div class="inside">
				<p><?php 
            _e('Please confirm your order.', 'qtranslate');
            ?>
</p>
				<div class="qs_submit"><a class="button-primary" onclick="sendorder();"><?php 
            _e('Confirm Order', 'qtranslate');
            ?>
</a></div>
			</div>
		</div>
	</div>
<?php 
        } else {
            ?>
	<div id="submitboxcontainer" class="metabox-holder">
		<div id="submitdiv" class="postbox">
			<h3 class="hndle"><?php 
            _e('Request Translation', 'qtranslate');
            ?>
</h3>
			<div class="inside request">
				<noscript><?php 
            _e('Javascript is required for qTranslate Services', 'qtranslate');
            ?>
</noscript>
				<p><?php 
            _e('Please choose a service first', 'qtranslate');
            ?>
</p>
			</div>
		</div>
	</div>
<?php 
        }
        $timestamp = time();
        if ($timestamp != qs_queryQS(QS_VERIFY, $timestamp)) {
            ?>
<p class="error"><?php 
            _e('ERROR: Could not connect to qTranslate Services. Please try again later.', 'qtranslate');
            ?>
</p>
<?php 
            return;
        }
        ?>
<div id="qs_boxes" class="metabox-holder">
	<div class="postbox">
		<h3 class="hndle"><?php 
        _e('Translation Service', 'qtranslate');
        ?>
</h3>
		<div class="inside">

<ul>
<?php 
        if ($services = qs_queryQS(QS_GET_SERVICES)) {
            foreach ($services as $service_id => $service) {
                // check if we have data for all required fields
                $requirements_matched = true;
                foreach ($service['service_required_fields'] as $field) {
                    if (!isset($service_settings[$service_id][$field['name']]) || $service_settings[$service_id][$field['name']] == '') {
                        $requirements_matched = false;
                    }
                }
                if (!$requirements_matched) {
                    ?>
<li>
	<label><input type="radio" name="service_id" disabled="disabled" /> <b><?php 
                    echo ppqtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($service['service_name']);
                    ?>
</b> ( <a href="<?php 
                    echo $service['service_url'];
                    ?>
" target="_blank"><?php 
                    _e('Website', 'qtranslate');
                    ?>
</a> )</label>
	<p class="error"><?php 
                    printf(__('Cannot use this service, not all <a href="%s">required fields</a> filled in for this service.', 'qtranslate'), 'options-general.php?page=qtranslate#qs_service_' . $service_id);
                    ?>
</p>
	<p class="service_description"><?php 
                    echo ppqtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($service['service_description']);
                    ?>
</p>
</li>
<?php 
                } else {
                    ?>
<li><label><input type="radio" id="qs_service_<?php 
                    echo $service['service_id'];
                    ?>
" onclick="chooseservice(this.value)" value="<?php 
                    echo $service['service_id'];
                    ?>
" <?php 
                    echo $confirm ? 'disabled="disabled"' : 'name="service_id"';
                    ?>
 /> <b><?php 
                    echo ppqtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($service['service_name']);
                    ?>
</b> ( <a href="<?php 
                    echo $service['service_url'];
                    ?>
" target="_blank"><?php 
                    _e('Website', 'qtranslate');
                    ?>
</a> )</label><p class="service_description"><?php 
                    echo ppqtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($service['service_description']);
                    ?>
</p></li>
<?php 
                }
            }
            ?>
</ul>
<script type="text/javascript">
	function chooseservice(id) {
		jQuery('#qs_service_'+id).attr('checked','checked');
		jQuery('#submitdiv .request').html('<?php 
            _e('<p><img src="images/wpspin_light.gif"> Getting Quote...</p>', 'qtranslate');
            ?>
');
		jQuery.post(ajaxurl, {
			action: 'qs_quote',
			translate_from: '<?php 
            echo $translate_from;
            ?>
',
			translate_to: '<?php 
            echo $translate_to;
            ?>
',
			service_id: id, 
			post_id: '<?php 
            echo $post_id;
            ?>
'}, 
			function(response) {
				eval(response);
		});
	}
	
	function sendorder() {
		jQuery("#qtranslate-services-translate").submit();
	}
	
	chooseservice('<?php 
            echo isset($_REQUEST['service_id']) ? $_REQUEST['service_id'] : $default_service;
            ?>
');
</script>
		</div>
	</div>
	<div class="postbox closed">
		<div class="handlediv" title="<?php 
            _e('Click to toggle');
            ?>
" onclick="jQuery(this).parent().removeClass('closed');jQuery(this).hide();"><br></div>
		<h3 class="hndle"><?php 
            _e('Review Article', 'qtranslate');
            ?>
</h3>
		<div class="inside">
			<textarea name="qs_content_preview" id="qs_content_preview" readonly="readonly"><?php 
            echo $post_content;
            ?>
</textarea>
		</div>
	</div>

<p><?php 
            _e('Your article will be SSL encrypted and securly sent to qTranslate Services, which will forward your text to the chosen Translation Service. Once qTranslate Services receives the translated text, it will automatically appear on your blog.', 'qtranslate');
            ?>
</p>
<?php 
        }
        ?>
	</div>
<?php 
    }
    ?>
</div>
</form>
<?php 
}
function ppqtrans_languageColumn($column)
{
    global $q_config, $post;
    if ($column == 'language') {
        $available_languages = ppqtrans_getAvailableLanguages($post->post_content);
        $missing_languages = array_diff($q_config['enabled_languages'], $available_languages);
        $available_languages_name = array();
        $missing_languages_name = array();
        foreach ($available_languages as $language) {
            $available_languages_name[] = $q_config['language_name'][$language];
        }
        $available_languages_names = join(", ", $available_languages_name);
        echo apply_filters('ppqtranslate_available_languages_names', $available_languages_names);
        do_action('ppqtranslate_languageColumn', $available_languages, $missing_languages);
    }
    return $column;
}
function ppqtrans_isAvailableIn($post_id, $language = '')
{
    global $q_config;
    if ($language == '') {
        $language = $q_config['default_language'];
    }
    $post =& get_post($post_id);
    $languages = ppqtrans_getAvailableLanguages($post->post_content);
    return in_array($language, $languages);
}