<?php
	require_once('SOAP/Client.php');
	require_once("ApplicationScope.php");
	require_once('jcode/jcode.phps');
	include_once('jcode/code_table.ucs2jis');

	$app =& new ApplicationScope();
	$soapclient =& $app->get("quiz_client");
	if (!isset($soapclient)) {
		$soapclient = new SOAP_Client('http://jomora.bne.jp:8080/Quiz/QuizService.asmx?wsdl', false, false, array('timeout' => 2)); //for .NET 1.0
		//$soapclient = new SOAP_Client('http://jomora.bne.jp/quiz/services/QuizService?wsdl', false, false, array('timeout' => 2)); //for Apache AXIS 1.1
		$app->set("quiz_client", $soapclient);
	}
	$question = $soapclient->call('getQuestion', $v = array());

	$html = "";
	if ($question->choices == null) {
		$html = "Now loading...";
	} else {
		$html = "<form name=\"QuizForm\" action=\"\">\r\n";
		$html .= "($question->quizTitle : $question->groupName) " . str_replace("\n","<br />", htmlentities($question->sentence, ENT_QUOTES, "UTF-8")) . "<br />\r\n";
		foreach ($question->choices as $choice) {
			$html .= "<input type=\"checkbox\" name=\"cb\" value=\"$choice->correct\" />" . str_replace("\n","<br />\n", htmlentities($choice->text, ENT_QUOTES, "UTF-8")) . "<br />\r\n";
		}
		$html .= "<input type=\"hidden\" name=\"comment\" value=\"" . str_replace("\n","<br />", htmlentities(htmlentities($question->comment, ENT_QUOTES, "UTF-8"), ENT_QUOTES, "UTF-8")) . "\" />\r\n";
		$html .= "<div id=\"qanswer\"><input type=\"button\" name=\"submit\" value=\"判定\" onclick=\"answer();\" style=\"BACKGROUND:#ddffee; BORDER-LEFT:1px; BORDER-TOP:1px;\" /><noscript style=\"color:red;\">　判定にはJavaScriptを使用します。</noscript></div>\r\n";
		$html .= "</form>\n";
		$html .= <<<EOD
<script language="JavaScript" type="text/javascript"><!--
function answer() {
	var correct = true;
	for (i = 0; i < document.QuizForm.cb.length; i++) {
		if ((document.QuizForm.cb[i].checked == true
			&& document.QuizForm.cb[i].value == "")
			|| (document.QuizForm.cb[i].checked == false
			&& document.QuizForm.cb[i].value == "1")) {
			correct = false;break;
		}
	}
	if (correct) {
		document.getElementById('qanswer').innerHTML = "<span style=\"font-size:large;\">正解！(^^)/</span><br />\\r\\n" + document.QuizForm.comment.value;
	} else {
		alert("不正解 (++)");
	}
}
//--></script>
<div align="right" style="font-size:80%;">powered by <a href="http://jomora.bne.jp/wiki/?QuizML">Quiz Web Service</a></div>
EOD;
	}

	// UTF-8 -> Shift_JIS
	print JcodeConvert($html, 4, 2);
?>
