
var f_emb_audio;

var mDop;

function StandaloneDop(oElementArg)
{
	this.oElement = oElementArg;
	
	this.get_Id = function()	// this notation binds the function to the object better
	{
		var e = this.oElement.selectSingleNode("//Id");
		if (e==null)
			return "[no Id]";
		var txt_node = e.firstChild;
		return txt_node.nodeValue;
	}

	this.get_Thai = function()
	{
		var e = this.oElement.selectSingleNode("//Thai");
		if (e==null)
			return "[no Thai word]";
		var txt_node = e.firstChild;
		return txt_node.nodeValue;
	}
	
	this.get_ThaiLink = function(cls)
	{
		var id = this.get_Id();
		var s = "<a";
		if (cls != "")
			s += " class=" + cls;
		s += " href='/id/" + id + "'>" + this.get_Thai() + "</a>";
		return s;
	}
	
	this.get_Xlit = function()
	{
		var e = this.oElement.selectSingleNode("//Xlit");
		if (e==null)
			return "[no Xlit]";
		var txt_node = e.firstChild;
		return txt_node.nodeValue;
	}
	
	this.get_English = function()
	{
		var e = this.oElement.selectSingleNode("//English");
		if (e==null)
			return "[no English]";
		var txt_node = e.firstChild;
		return txt_node.nodeValue;
	}
	
	this.get_ToneArray = function()
	{
		var e = this.oElement.selectSingleNode("//ToneArray");
		if (e==null)
			return "[no ToneArray]";
		var txt_node = e.firstChild;
		return txt_node.nodeValue;
	}
	
	this.get_AudioLink = function(bigicon)
	{
		var e = this.oElement.selectSingleNode("//AudioFile");
		if (e==null)
			return "";
		var txt_node = e.firstChild;
			
		var s = "&nbsp;";
		if (f_emb_audio)
			s += "<a onClick=javascript:PlayAudioFile('" + txt_node.nodeValue + "') style=cursor:pointer>";
		else
			s += "<a href='" + txt_node.nodeValue + "'>";
		s += "<img src='/img/speaker";
		if (bigicon != true)
			s += '_sm';
		s += ".gif'></a>";
		return s;
	}
	
	this.get_ImageFile = function()
	{
		var e = this.oElement.selectSingleNode("//ImageFile");
		if (e==null)
			return "[no Image]";
		var txt_node = e.firstChild;
		return txt_node.nodeValue;
	}

	this.get_PosFlags = function()
	{
		var e = this.oElement.selectSingleNode("//PosFlags");
		if (e==null)
			return "[no PosFlags]";
		var txt_node = e.firstChild;
		return txt_node.nodeValue;
	}
}


///////////////////////////////////////////////////////////////

var xmlhttp;

function next()
{
	xmlhttp = null;
	if (window.XMLHttpRequest)
		xmlhttp = new XMLHttpRequest();
	else if (window.ActiveXObject)
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	if (xmlhttp==null)
		return;

// NOTE TO 3rd PARTIES: THIS AJAX INTERFACE IS SUBJECT TO FREQUENT EVOLVING CHANGES AND THUS IS NOT INTENDED FOR 3rd PARTY USE
	var s = "<?xml version=\"1.0\" encoding=\"TIS-620\" ?>";
	s += "<tl-xml-request>";
	s +=	"<type>GetRandomThaiWord</type>";
	s +=	"<parameters>";
	s +=		"<SyllableCount>1</SyllableCount>";
	s +=		"<HasAudioFile />";
//	s +=		"<HasImageFile />";
//	s +=		"<IsStarred />";
	s +=		"<NoTransliterally />";
	s +=		"<LoanLangFlags>3</LoanLangFlags>";
	s +=	"</parameters>";
	s += "</tl-xml-request>";
	
	xmlhttp.open("POST","/xml");
	xmlhttp.onreadystatechange = completion_callback;
	xmlhttp.send(s);
}

function completion_callback()
{
	if (xmlhttp.readyState!=4)
		return;
		
	var domDoc = xmlhttp.responseXML;
	var doc = domDoc.documentElement;
	var eResult = doc.firstChild;
	var eDop = eResult.firstChild;
	
	mDop = new StandaloneDop(eDop);
	
	$$("qv").innerHTML = mDop.get_ThaiLink("hy") + mDop.get_AudioLink(true);
	xmlhttp = null;
}


var n_correct = 0;
var n_wrong = 0;

function ans(a)
{
	if (a == mDop.get_ToneArray())
	{
		n_correct++;
		$$("lastresult").innerHTML = "<img src=/img/green_check.gif /> &nbsp; Correct. &nbsp; " + mDop.get_ThaiLink("th") + "&nbsp;" + mDop.get_AudioLink(false) + "&nbsp; /" + mDop.get_Xlit() + "/ &nbsp;" + mDop.get_English();
		next();
	}
	else
	{
		n_wrong++;
		var s = "<img src=/img/red_x.gif /> &nbsp; Incorrect.";
		$$("lastresult").innerHTML = s;
	}
	$$("scoring").innerHTML = "Score: " + n_correct + " / " + (n_correct + n_wrong) + " = " + Math.round((n_correct/(n_correct+n_wrong))*100) + "%";
}


function start_quiz(f_emb_audio_arg) {
	$('#disclaimer').hide();
	f_emb_audio = f_emb_audio_arg;
	next();
}
