var stXmlHttp = false;
var szCgiPath = "";

function XmlHttpUse()
{
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		stXmlHttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE
		try {
			stXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				stXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
}

function ParseResult()
{
	var szResult;
	var szQuizCode;

	if (stXmlHttp.readyState == 4) {
		if (stXmlHttp.status == 200) {
			szResult = stXmlHttp.responseText;

			/* get the question code */
			szResult.match(/QuizCode=([^;]*);/);
			document.all.quizcode.value = RegExp.$1;

			/* get the image source */
			szResult.match(/QuizSrc=([^;]*);/);
			document.all.quizsrc.src = RegExp.$1;
			if (!document.all.quizsrc.src) {
				ErrorMsg(document.all.quizsrc);
			}
		} else {
			alert("無法正確取得資訊, 請按F5重新載入頁面");
		}
	}
}

function XmlHttpGet(szUrl)
{
	XmlHttpUse();
	if (!stXmlHttp) {
		return false;
	}

	stXmlHttp.open("GET", szUrl, true);
	stXmlHttp.onreadystatechange = ParseResult;
	stXmlHttp.send(null);
	return true;
}

function SetCgiPath(szPath)
{
	szCgiPath = szPath;
}

function Get()
{
	var nRandNo = Math.random();
	var szUrl = szCgiPath + "?cm=get&rn=" + nRandNo;
	
	XmlHttpGet(szUrl);
}

function Refresh()
{
	var nRandNo = Math.random();
	var szUrl = szCgiPath + "?cm=refresh&quizcode=" + document.all.quizcode.value + "&rn=" + nRandNo;
	
	XmlHttpGet(szUrl);
}

function ErrorMsg(stImg)
{
	stImg.src = "/images/error.jpg";
}


