
var actualExercise = null;
var asyncCanLoad = true;
var clockStartTime = null;
var cdTries = 10;
var jumpToNextID = null;
var loadSolution = true;

function clearCompositionMsg () {
	HTML_AJAX_Util.setInnerHTML ('btnSave', '');
}

var exercise = {
	delSubscription: function (id_subscription) {
		asyncExercise.ajaxDelSubscription (id_subscription);
	},
	getHtml: function (p_sType, p_aArgs, p_oValu){
		asyncExercise.getHtml(p_oValu);
	},
	checkAnswer: function (ex_id, ex_line_id, solution) {
		tar = solution;
		while(!HTML_AJAX_Util.hasClass (tar, 'form')) {
			tar = tar.parentNode;
		}
		if (HTML_AJAX_Util.hasClass (tar, 'allready_checked')) {
			var first_check = false;
		} else {
			HTML_AJAX_Util.addClass (tar, 'allready_checked');
			var first_check = true;
		}
		asyncExercise.checkAnswer (ex_id, ex_line_id, HTML_AJAX.formEncode(tar, 'PHP'), first_check);
		return false;
	},
	getExercise: function (p_sAction, p_aArgs, p_oValue) {
		if (inExamMode) {
			// save actual exercise values
			var frms = document.getElementsByTagName ('form');
			asyncExercise = new Exercise ();
			for (var i = 1; i < frms.length; i++) { /* > */
				var dv = document.getElementById ('exl_div_' + i);
				if (dv.onclick) dv.onclick ();
			}
			asyncExercise = new Exercise (new ecallback ());
		}
		asyncExercise.ajaxGetExercise (p_oValue);
	},
	permitLeave: function () {
		return ('You shouldn\'t leave this page until finished with exam!');
	},
	startExamMode: function (id_exercises) {
		inExamMode = true;
		examID = 4101;
//		exercise.hideMenu ();
		var menulist = syncExercise.ajaxSetExamMode ('on', id_exercises);
		while (oMenuBar.removeItem(oMenuBar.getItem(0)));
   		oMenuBar.addItem ('Exam');
		oMenuBar.subscribe ("beforeRender", function () {
			if (this.getRoot() == this) {
//				oMenuBar.getItem(0).cfg.setProperty("submenu", eval('examMenus' + examID + '[0]'));
				oMenuBar.getItem(0).cfg.setProperty("submenu", eval('examMenus' + menulist.menuID + '[0]'));
			}
		});
		oMenuBar.render();
		help.disableHelp ();
		helpEnabled = false;
		if (oldOnbeforeunloadFunction == null)
				oldOnbeforeunloadFunction = window.onbeforeunload;
		window.onbeforeunload = exercise.permitLeave;
		/* set timeout for 1.5 hours = 5400000 msec */
		examModeTimeout = setTimeout ('exercise.endExamMode();', 4500000);
		var cd = new Date ();
		clockStartTime = cd.getTime();
		clockTimeout = setTimeout ('exercise.updateClock();', 1000);
	},
	updateClock: function () {
		if (inExamMode) {
			var cd = new Date ();
			var delay = Math.round ((cd.getTime () - clockStartTime) / 1000);
			delay = 4500 - delay;
			var sec = delay % 60;
			if (sec < 10) sec = "0" + sec; /* > */
			var min = Math.floor (delay / 60);
			HTML_AJAX_Util.setInnerHTML ('js_clock', min + ":" + sec);
			clockTimeout = setTimeout ('exercise.updateClock();', 1000);
		} else {
			HTML_AJAX_Util.setInnerHTML ('js_clock', '&nbsp;');
		}
	},
	endExamMode: function () {
		inExamMode = false;
		/* clear timeout */
		clearTimeout (examModeTimeout);
//		exercise.showMenu ();
		while (oMenuBar.removeItem(oMenuBar.getItem(0)));
		oMenuBar.addItems (defaultMenus);
		YAHOO.util.Event.removeListener("productsandservices", "beforeRender");
		oMenuBar.subscribe ("beforeRender", function () {
            if (this.getRoot() == this) {
            	if (! inExamMode) {
                    this.getItem(0).cfg.setProperty("submenu", aSubmenuData[0]);
                    this.getItem(1).cfg.setProperty("submenu", aSubmenuData[1]);
                    this.getItem(2).cfg.setProperty("submenu", aSubmenuData[2]);
                    this.getItem(3).cfg.setProperty("submenu", aSubmenuData[3]);
                    this.getItem(4).cfg.setProperty("submenu", aSubmenuData[4]);
                    this.getItem(5).cfg.setProperty("submenu", aSubmenuData[5]);
                    this.getItem(6).cfg.setProperty("submenu", aSubmenuData[6]);
                }
            }
		});
		oMenuBar.render();
		helpEnabled = true;
		help.enableHelp ();
		window.onbeforeunload = oldOnbeforeunloadFunction;
		syncExercise.ajaxSetExamMode ('off', 0);
		asyncCanLoad = false;
		asyncExercise.ajaxGetExamStats ();
	},
	getExam: function (p_sAction, p_aArgs, p_oValue) {
		asyncExercise.ajaxSetExamMode ('on', p_oValue);
		exercise.getExercise (null, null, p_oValue);
		if (! inExamMode) exercise.startExamMode (p_oValue);
//		asyncExercise.ajaxGetExercise (p_oValue);
	},
	hideMenu: function () {
		oMenuBar.hide ();
	},
	showMenu: function () {
		oMenuBar.show ();
	},
	getHelp: function (p_sAction, p_aArgs, p_oValue) {
		if (p_oValue.length > 0) asyncExercise.ajaxGetHelp (p_oValue);
	},
	getSubscriptions: function (p_sAction, p_aArgs, p_oValue) {
		asyncExercise.ajaxGetSubscriptions ();
	},
	getHelpFromFile: function (id) {
		asyncExercise.ajaxGetHelp (id, true);
	},
	chkAnswerBody: function (ex_id, ex_line_id, solution) {
		help.setHelpId(ex_line_id);
		tar = solution;
		while(!HTML_AJAX_Util.hasClass (tar, 'form')) {
			tar = tar.parentNode;
		}
		if (HTML_AJAX_Util.hasClass (tar, 'allready_checked')) {
			var first_check = false;
		} else {
			HTML_AJAX_Util.addClass (tar, 'allready_checked');
			var first_check = true;
		}
		el = tar.getElementsByTagName('input');
		for (var i=0; i< el.length; i++) { /*>*/
			if (el[i].name == 'solution') {
				id = el[i].id;
			}
		}
		var encodedString = HTML_AJAX.formEncode(tar, 'PHP');
		if (encodedString.solution) {
			if (encodedString.solution.length > 0) asyncExercise.ajaxChkAnswer (id, ex_id, ex_line_id, encodedString, first_check);
			if (encodedString.solution.length == 0) if (loadSolution == true) asyncExercise.ajaxGetSolution (id, ex_line_id, encodedString, first_check);
		} else {
			if (loadSolution == true) asyncExercise.ajaxGetSolution (id, ex_line_id, encodedString, first_check);
		}
	},
	chkAnswerMC: function(ex_id, ex_line_id, solution) {
		exercise.chkAnswerBody(ex_id, ex_line_id, solution);
		tar = solution;
		while(!HTML_AJAX_Util.hasClass (tar, 'form')) {
			tar = tar.parentNode;
		}
		var elements = tar.getElementsByClassName('solution');
		var elemCount = elements.length;
		for (var i = 0; i < elemCount; i += 1) {
			elements[i].onclick = null;
			elements[i].disabled = 'disabled';
		}
	},
	chkAnswerSSSS: function (ex_id, ex_line_id, solution, number) {
		help.setHelpId(ex_line_id);
		tar = solution;
		while(!HTML_AJAX_Util.hasClass (tar, 'form')) {
			tar = tar.parentNode;
		}
		if (HTML_AJAX_Util.hasClass (tar, 'allready_checked')) {
			var first_check = false;
		} else {
			HTML_AJAX_Util.addClass (tar, 'allready_checked');
			var first_check = true;
		}
		el = tar.getElementsByTagName('input');
		for (var i=0; i< el.length; i++) { /*>*/
			if (el[i].name == 'field_2') {
				id2 = el[i].id;
			}
			if (el[i].name == 'field_3') {
				id3 = el[i].id;
			}
		}
		var id = id2 + "@@@@" + id3;
		var encodedString = HTML_AJAX.formEncode(tar, 'PHP');
		if ((encodedString.question && encodedString.field_2)
			|| (encodedString.field_2 && encodedString.field_3)
			|| (encodedString.question && encodedString.field_3)) {
				if ((encodedString.question.length && encodedString.field_2.length)
					|| (encodedString.field_2.length && encodedString.field_3.length)
					|| (encodedString.question.length && encodedString.field_3.length)) {
						encodedString.solution = encodedString.field_2 + "@@@@" + encodedString.field_3;
						asyncExercise.ajaxChkAnswer (id, ex_id, ex_line_id, encodedString, first_check);
				} else
					if (loadSolution == true) asyncExercise.ajaxGetSolution (id, ex_line_id, encodedString, first_check);
		} else {
			if (loadSolution == true) asyncExercise.ajaxGetSolution (id, ex_line_id, encodedString, first_check);
		}
	},
	chkAnswerSSSD: function (ex_id, ex_line_id, solution, number) {
		help.setHelpId(ex_line_id);
		tar = solution;
		while(!HTML_AJAX_Util.hasClass (tar, 'form')) {
			tar = tar.parentNode;
		}
		if (HTML_AJAX_Util.hasClass (tar, 'allready_checked')) {
			var first_check = false;
		} else {
			HTML_AJAX_Util.addClass (tar, 'allready_checked');
			var first_check = true;
		}
		el = tar.getElementsByTagName('input');
		for (var i=0; i< el.length; i++) { /*>*/
			if (el[i].name == 'question') {
				id1 = el[i].id;
			}
			if (el[i].name == 'field_2') {
				id2 = el[i].id;
			}
			if (el[i].name == 'field_3') {
				id3 = el[i].id;
			}
		}
		var id = id1 + ";;;;" + id2 + ";;;;" + id3;
		var encodedString = HTML_AJAX.formEncode(tar, 'PHP');
		if ((encodedString.question && encodedString.field_2)
			|| (encodedString.field_2 && encodedString.field_3)
			|| (encodedString.question && encodedString.field_3)) {
				if ((encodedString.question.length && encodedString.field_2.length)
					|| (encodedString.field_2.length && encodedString.field_3.length)
					|| (encodedString.question.length && encodedString.field_3.length)) {
						encodedString.solution = encodedString.question + ";;;;" + encodedString.field_2 + ";;;;" + encodedString.field_3;
						asyncExercise.ajaxChkAnswer (id, ex_id, ex_line_id, encodedString, first_check);
				} else
					if (loadSolution == true) asyncExercise.ajaxGetSolution (id, ex_line_id, encodedString, first_check);
		} else {
			if (loadSolution == true) asyncExercise.ajaxGetSolution (id, ex_line_id, encodedString, first_check);
		}
	},
	chkAnswerLBF: function (ex_id, ex_line_id, solution, number) {
		help.setHelpId(ex_line_id);
		tar = solution;
		while(!HTML_AJAX_Util.hasClass (tar, 'form')) {
			tar = tar.parentNode;
		}
		if (HTML_AJAX_Util.hasClass (tar, 'allready_checked')) {
			var first_check = false;
		} else {
			HTML_AJAX_Util.addClass (tar, 'allready_checked');
			var first_check = true;
		}
		el = tar.getElementsByTagName('input');
		id1 = id2 = id3 = '';
		for (var i=0; i< el.length; i++) { /*>*/
			if (el[i].name == 'solution') {
				id1 = el[i].id;
			}
			if (el[i].name == 'field_2') {
				id2 = el[i].id;
			}
			if (el[i].name == 'field_3') {
				id3 = el[i].id;
			}
		}
		var id = id1 + "____" + id2 + "____" + id3;
		var encodedString = HTML_AJAX.formEncode(tar, 'PHP');
		if (encodedString.solution == undefined) encodedString.solution = '';
		if (encodedString.field_2 == undefined) encodedString.field_2 = '';
		if (encodedString.field_3 == undefined) encodedString.field_3 = '';
		if ((encodedString.solution)
			|| (encodedString.field_2)
			|| (encodedString.field_3)) {
				if ((encodedString.solution.length)
					|| (encodedString.field_2.length)
					|| (encodedString.field_3.length)) {
//						encodedString.solution = encodedString.solution + "____" + encodedString.field_2 + "____" + encodedString.field_3;
						asyncExercise.ajaxChkAnswer (id, ex_id, ex_line_id, encodedString, first_check);
				} else
					if (loadSolution == true) asyncExercise.ajaxGetSolution (id, ex_line_id, encodedString, first_check);
		} else {
			if (loadSolution == true) asyncExercise.ajaxGetSolution (id, ex_line_id, encodedString, first_check);
		}
	},
	chkAnswerWNext: function (ex_id, ex_line_id, solution, number) {
        jumpToNextID = number;
        loadSolution = true;
		exercise.chkAnswerBody (ex_id, ex_line_id, solution);
//		exercise.jumpToNext (++number);
		return false;
	},
	chkOneGap: function (e, ex_id, ex_line_id, solution) {
	    if (null == e)
				e = window.event;
	    if (e.keyCode == 13)  {
	    		loadSolution = false;
				exercise.chkAnswerBody (ex_id, ex_line_id, solution);
				return false;
	    } else loadSolution = true;
	},
	chkOneGapWNext: function (e, ex_id, ex_line_id, solution, number) {
        jumpToNextID = number;
	    if (null == e)
				e = window.event;
	    if (e.keyCode == 13)  {
	    	loadSolution = false;
            exercise.chkAnswerBody (ex_id, ex_line_id, solution);
//          exercise.jumpToNext (++number);
            return false;
	    } else loadSolution = true;
	},
	jumpToNext: function (number) {
		if (number >= 0) number;
            else number = jumpToNextID;
		nxtFrm = document.getElementById ('frm' + number);
		if (nxtFrm == null) nxtFrm = document.getElementById ('frm1');
		if (nxtFrm != null) {
			var els = nxtFrm.elements;
			for (var i = 0, len = els.length; i < len; i++) { /*>*/
				if (els[i].focus) {
					els[i].select ();
					els[i].focus ();
				}
			}
		}
	},
    disableFields: function (number) {
		nxtFrm = document.getElementById ('frm' + number);
		if (nxtFrm == null) nxtFrm = document.getElementById ('frm1');
		if (nxtFrm != null) {
			var els = nxtFrm.elements;
			for (var i = 0, len = els.length; i < len; i++) { /*>*/
				if (els[i].focus) {
					els[i].disabled = true;
				}
			}
		}
    },
	getAnswer: function (base_path, table_name, imagewidth, exlineid, number) {
		HTML_AJAX_Util.setInnerHTML('sol' + number,'<img src="' + base_path + 'image.php?t=' + table_name + '&amp;w=' + imagewidth + '&amp;c=solution&amp;i=' + exlineid + '">');
	},
	saveComposition: function (id, textarea) {
		if (id != '') {
			var txt = document.getElementById (textarea);
			txt = txt.value;
			if (txt.length)
					asyncExercise.ajaxSaveComposition (id, txt);
				else
					HTML_AJAX_Util.setInnerHTML ('btnSave', '<span>Cannot save empty exercise</span>');
		} else {
			HTML_AJAX_Util.setInnerHTML ('btnSave', '<span>Cannot save exercise, notice admin</span>');
		}
		setTimeout ('clearCompositionMsg()', 5000);
	},
	cdCheckLetter: function (id_exercise_line, letter) {
		if (cdTries > 0) asyncExercise.ajaxCDCheckLetter (id_exercise_line, letter);
	}
}

function ecallback() {}
ecallback.prototype = {
	ajaxDelSubscription: function (result) {
		asyncExercise.ajaxGetSubscriptions ();
	},
	getHtml: function(result) {
		HTML_AJAX_Util.addClass ('exercise_status_inner', 'hidden');
		HTML_AJAX_Util.setInnerHTML('exercise_title',result.exercise_title);
		if (result.container_info_short_help.length > 0)
			HTML_AJAX_Util.setInnerHTML('container_info_short_help',result.container_info_short_help);
		else
			HTML_AJAX_Util.setInnerHTML('container_info_short_help','&nbsp;');
		HTML_AJAX_Util.setInnerHTML('container_text',result.html);
	},
	checkAnswer: function(result) {
		var parentElement = document.getElementById('exercise_element_'+result.exercise_line);
		var helpImg = HTML_AJAX_Util.getElementsByClassName('exercise_help_img',parentElement);
		HTML_AJAX_Util.addClass(helpImg[0].parentNode,result.exercise_help_extra_class);
		helpImg[0].src = result.exercise_face;
		if(result.setRadio !=undefined) {
			var radio = HTML_AJAX_Util.getElementsByClassName('solution_'+result.setRadio,parentElement);
			radio[0].checked = true;
		}
		if (result.first_check == true) {
			if (result.correct == true) {
				var value = document.getElementById('correct_answers').innerHTML;
				document.getElementById('correct_answers').innerHTML =  value-0+1;
			}
			if (result.correct == false) {
				var value = document.getElementById('bad_answers').innerHTML;
				document.getElementById('bad_answers').innerHTML =  value-0+1;
			}
		}
	},
	ajaxGetExercise: function(result) {
		cdTries = 10;
		loadSolution = true;
		HTML_AJAX_Util.removeClass ('exercise_status_inner', 'hidden');
		if (result.error) {
			alert (result.error);
		} else if (asyncCanLoad) {
			HTML_AJAX_Util.setInnerHTML ('exercise_help_text', (result.exercise_help) ? result.exercise_help : '');
			var hsdiv = document.getElementById ('help_text_select');
			HTML_AJAX_Util.setInnerHTML ('help_text_select', '');
			HTML_AJAX_Util.setInnerHTML ('help_text', '');
			var sel = document.createElement ('select');
			sel.onchange = function () {
				HTML_AJAX_Util.setInnerHTML ('help_text', this.options[this.selectedIndex].value);
				help.setHeight ('container_help_text');
			}
			sel[sel.length] = new Option ('select help', '');
			if (result.lineHelps != undefined)
					for (x = 1; x <= result.lineHelps.length; x++) { /* > */
						if (result.lineHelps[x - 1] != null && result.lineHelps[x - 1].length > 0) {
							sel[sel.length] = new Option ('Question ' + x, result.lineHelps[x - 1]);
						}
					}
			if (sel.length > 1) {
//				hsdiv.appendChild (document.createElement ('br'));
				hsdiv.appendChild (sel);
				hsdiv.appendChild (document.createElement ('br'));
				hsdiv.appendChild (document.createElement ('br'));
			}
			if (result.hasLineHelp == 1
					|| (result.exercise_help != undefined
						&& result.exercise_help.length > 0
						&& result.exercise_help != 'DISABLED')
				) {
				help.enableHelp ();
			} else {
				help.disableHelp ();
			}
			actualExercise = result.exercise_id;
			HTML_AJAX_Util.setInnerHTML ('exercise_title', (result.exercise_title) ? result.exercise_title : '&nbsp;');
			HTML_AJAX_Util.setInnerHTML ('container_info_short_help', (result.container_info_short_help) ? result.container_info_short_help : '&nbsp;');
			if (! inExamMode) HTML_AJAX_Util.setInnerHTML ('navigation_line', (result.navigation_line) ? result.navigation_line : '&nbsp;');
				else HTML_AJAX_Util.setInnerHTML ('navigation_line', '&nbsp;');
			HTML_AJAX_Util.setInnerHTML ('container_text', (result.html) ? result.html : '&nbsp;');
			HTML_AJAX_Util.setInnerHTML ('correct_answers', 0);
			HTML_AJAX_Util.setInnerHTML ('bad_answers', 0);
		}
	},
	ajaxGetSubscriptions: function (result) {
		HTML_AJAX_Util.addClass ('exercise_status_inner', 'hidden');
		help.disableHelp ();
		if (result.error) {
			alert (result.error);
		} else if (asyncCanLoad) {
			HTML_AJAX_Util.setInnerHTML ('exercise_help_text', result.exercise_help);
			HTML_AJAX_Util.setInnerHTML ('exercise_title', result.exercise_title);
			if (result.container_info_short_help.length > 0)
					HTML_AJAX_Util.setInnerHTML('container_info_short_help',result.container_info_short_help);
				else
					HTML_AJAX_Util.setInnerHTML('container_info_short_help','&nbsp;');
			HTML_AJAX_Util.setInnerHTML ('container_text', '<div id="dHelpDiv">' + result.html + '</div>');
			HTML_AJAX_Util.setInnerHTML ('correct_answers', 0);
			HTML_AJAX_Util.setInnerHTML ('bad_answers', 0);

			help.setHeight ('dHelpDiv');
		}
	},
	ajaxGetHelp: function (result) {
		HTML_AJAX_Util.addClass ('exercise_status_inner', 'hidden');
		help.disableHelp ();
		if (result.error) {
			alert (result.error);
		} else if (asyncCanLoad) {
			HTML_AJAX_Util.setInnerHTML ('exercise_help_text', result.exercise_help);
			HTML_AJAX_Util.setInnerHTML ('exercise_title', result.exercise_title);
			if (result.container_info_short_help.length > 0)
					HTML_AJAX_Util.setInnerHTML('container_info_short_help',result.container_info_short_help);
				else
					HTML_AJAX_Util.setInnerHTML('container_info_short_help','&nbsp;');
			HTML_AJAX_Util.setInnerHTML ('container_text', '<div id="dHelpDiv">' + result.html + '</div>');
			HTML_AJAX_Util.setInnerHTML ('correct_answers', 0);
			HTML_AJAX_Util.setInnerHTML ('bad_answers', 0);

			help.setHeight ('dHelpDiv');
		}
	},
	ajaxGetSolution: function (result) {
		if (result.error) {
			alert (result.error);
		} else if (asyncCanLoad) {
			if (result.dragdrop == true) {
				var dom = YAHOO.util.Dom;
				// get source object
				var oSS = YAHOO.util.DDM.getDDById ("id_sol_" + result.md5);
				if (oSS && oSS.slot) {
                    var narray = oSS.slot.id.split('_');
                    var hdn2 = dom.get ("id_hdn_" + narray[2]);
                    hdn2.value = '';
					oSS.slot.player = null;
				}
				// get destination
				var dst = dom.get ("id_que_" + result.exercise_line);
				var endPos = dom.getXY (dst);
				// move to destination
				new YAHOO.util.Motion (
						'id_sol_' + result.md5,
						{
							points: {
								to: endPos
							}
						},
						0.3,
						YAHOO.util.Easing.easeOut
					).animate ();
				// get destination object
				var oDD = YAHOO.util.DDM.getDDById ("id_que_" + result.exercise_line);
				// if it had a previous object, move it away
                if (oDD.player) {
                    YAHOO.util.Dom.setXY(oDD.player.getEl(), oDD.player.startPos);
                }
                // store solution in hidden field
				var hdn = dom.get ("id_hdn_" + result.exercise_line);
                hdn.value = result.md5;
                // lock solution object
                var sol = YAHOO.util.DDM.getDDById ("id_sol_" + result.md5);
                if (sol != null) sol.lock ();
			} else {
				if (result.id.match ('@@@@')) {
					var ids = result.id.split ('@@@@');
					var id2 = document.getElementById(ids[0]);
					var id3 = document.getElementById(ids[1]);
					id2.value = result.field_2;
					id3.value = result.field_3;
				} else if (result.id.match (';;;;')) {
					var ids = result.id.split (';;;;');
					var id1 = document.getElementById(ids[0]);
					var id2 = document.getElementById(ids[1]);
					var id3 = document.getElementById(ids[2]);
					id1.value = result.question;
					id2.value = result.field_2;
					id3.value = result.field_3;
				} else if (result.id.match ('____')) {
					var ids = result.id.split ('____');
					var id1 = document.getElementById(ids[0]);
					var id2 = document.getElementById(ids[1]);
					var id3 = document.getElementById(ids[2]);
					if (id1 != null) id1.value = result.solution;
					if (id2 != null) id2.value = result.field_2;
					if (id3 != null) id3.value = result.field_3;
				} else {
					var inp = document.getElementById(result.id);
					inp.checked = true;
					inp.value = result.solution;
				}
			}
			if (result.number != undefined && result.number > 0) {
				var tar = document.getElementById (result.id);
				while(!HTML_AJAX_Util.hasClass (tar, 'form')) {
					tar = tar.parentNode;
				}
				var radio = HTML_AJAX_Util.getElementsByClassName('solution_'+result.number, tar);
				if (radio[0] != undefined) radio[0].checked = true;
			}
			var helpImg = document.getElementById ('help_img_' + result.exercise_line);
			HTML_AJAX_Util.addClass (helpImg.parentNode, result.exercise_help_extra_class);
			HTML_AJAX_Util.addClass (helpImg.parentNode.parentNode, result.exercise_help_extra_class);
			helpImg.src = result.exercise_face;
            // disable and go to next
                exercise.disableFields (jumpToNextID);
                exercise.jumpToNext (++jumpToNextID);
                helpImg.parentNode.onclick = function () {}; /* */
		}
	},
	ajaxChkAnswer: function (result) {
		if (result.getSolution) {
			if (loadSolution == true) asyncExercise.ajaxGetSolution (result.id, result.ex_line_id, result.encodedString, result.first_check);
		} else if (asyncCanLoad) {
			loadSolution = true;
			if (result.error) {
				alert (result.error);
			}
			var helpImg = document.getElementById ('help_img_' + result.exercise_line);
			HTML_AJAX_Util.addClass (helpImg.parentNode, result.exercise_help_extra_class);
			HTML_AJAX_Util.addClass (helpImg.parentNode.parentNode, result.exercise_help_extra_class);
			helpImg.src = result.exercise_face;
			/*
			if(result.setRadio != undefined) {
				var radio = document.getElementById ('solution_' + result.setRadio + result.exercise_line);
				radio.checked = true;
			}
			//*/
			if (result.first_check == true) {
				if (result.correct == 1) {
					var value = document.getElementById ('correct_answers').innerHTML;
					document.getElementById ('correct_answers').innerHTML =  value - 0 + 1;
				}
				if (result.correct != 1) {
					var value = document.getElementById ('bad_answers').innerHTML;
					document.getElementById ('bad_answers').innerHTML =  value - 0 + 1;
				}
			}
            if (result.correct == 1) {
                exercise.disableFields (jumpToNextID);
                exercise.jumpToNext (++jumpToNextID);
                helpImg.parentNode.onclick = function () {}; /* */
                var sol = YAHOO.util.DDM.getDDById ("id_sol_" + result.md5);
                if (sol != null) sol.lock ();
            } else {
                exercise.jumpToNext (-1);
            }
		}
	},
	ajaxSetExamMode: function (result) {
	},
	ajaxGetExamStats: function (result) {
		HTML_AJAX_Util.addClass ('exercise_status_inner', 'hidden');
		HTML_AJAX_Util.setInnerHTML ('container_info_short_help', '&nbsp;');
		HTML_AJAX_Util.setInnerHTML ('exam_text', result.html);
		HTML_AJAX_Util.setInnerHTML ('container_text', '&nbsp;');
		HTML_AJAX_Util.setInnerHTML ('correct_answers', 0);
		HTML_AJAX_Util.setInnerHTML ('bad_answers', 0);
		showHelp ('container_exam');
	},
	ajaxSaveComposition: function (result) {
		if (result.error) {
			alert (result.error);
		} else {
			HTML_AJAX_Util.setInnerHTML ('btnSave', result.msg);
			setTimeout ('clearCompositionMsg()', 5000);
			var ifr = document.getElementById ('btnIframe');
			ifr.src = '/downloadComposition.php?idel=' + result.id_exercise_line + '&idus=' + result.id_users;
		}
	},
	ajaxCDCheckLetter: function (result) {
		if (result.error) {
			alert (result.error);
		}
		var number = 0;
		for (var i = 0; i < result.data.length; i++) { /* > */
			var element = 'sl' + result.id_exercise_line + result.data[i];
			HTML_AJAX_Util.setInnerHTML (element, result.letter);
			number++;
		}
		var element = 'l' + result.id_exercise_line + result.letter;
		HTML_AJAX_Util.removeClass (element, 'active');
		HTML_AJAX_Util.addClass (element, 'inactive');
		var element = document.getElementById ('cdCount');
		if (result.data.length == 0) cdTries--;
		element.innerHTML = cdTries;
		if (cdTries == 0) {
			var value = document.getElementById ('bad_answers').innerHTML;
			document.getElementById ('bad_answers').innerHTML = value - 0 + 1;
			var cdRes = syncExercise.ajaxGetCDSolution (result.id_exercise_line);
			var letters = 'a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z';
			letters = letters.split('.');
			for (var k = 0; k < letters.length; k++) { /* > */
				var element = 'l' + result.id_exercise_line + letters[k];
				HTML_AJAX_Util.removeClass (element, 'active');
				HTML_AJAX_Util.addClass (element, 'inactive');
			}
			document.getElementById('dCDSorry').style.display='block';
			HTML_AJAX_Util.setInnerHTML ('sCDSorry', cdRes);
			if (false) {
			    alert ('Sorry! The word was ' + cdRes);
			}
		}
		if (result.finished == true) {
			var value = document.getElementById ('correct_answers').innerHTML;
			document.getElementById ('correct_answers').innerHTML = value - 0 + 1;
			var letters = 'a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z';
			letters = letters.split('.');
			for (var k = 0; k < letters.length; k++) { /* > */
				var element = 'l' + result.id_exercise_line + letters[k];
				HTML_AJAX_Util.removeClass (element, 'active');
				HTML_AJAX_Util.addClass (element, 'inactive');
			}
			document.getElementById('dCDNext').style.display='block';
			if (false) {
			    var answer = confirm ('Correct! Would you like to play again?');
			    if (answer) {
				    asyncExercise.ajaxGetExercise (result.id_exercises);
			    }
			}
		}
//*/
	}
}

var syncExercise = new Exercise();
var asyncExercise = new Exercise(new ecallback());

var help = {
	sH: function() {
		if (helpEnabled) showHelp('container_help');
	},
	disableHelp: function () {
		helpEnabled = false;
		help.setHelpStyle (false);
	},
	enableHelp: function () {
		if (! inExamMode) {
			helpEnabled = true;
			help.setHelpStyle (true);
		}
			var cht = document.getElementById('container_help_text');
			var ht = document.getElementById('help_text');
			cht.style.height = "auto";
			ht.style.height = "auto";
		showHelp ('container_help');
			var cht_height = cht.offsetHeight;
			var ht_height = ht.offsetHeight;
		hideHelp ('container_help');
			if (cht_height > 300) {
				cht.style.height = "300px";
			}
			if (ht_height > 300) {
				ht.style.height = "300px";
			}
	},
	setHelpStyle: function (enabled) {
		if (enabled) {
			HTML_AJAX_Util.replaceClass ('info_help_anchor', 'inactive', ' active');
		} else {
			HTML_AJAX_Util.replaceClass ('info_help_anchor', 'active', ' inactive');
		}
	},
	setHelpId: function (exercise_line_id) {
		if (exercise_line_id != '0') {
			this.setEvent ('info_help_anchor', 'click', this.sH);
			if (helpEnabled) HTML_AJAX_Util.replaceClass ('info_help_anchor', 'inactive', ' active');
			var ch = document.getElementById('container_help');
			var ht = document.getElementById('help_text');
			ht.style.height = "auto";
			ch.style.display = "block";
		//	HTML_AJAX_Util.setInnerHTML ('help_text', document.getElementById('hd_'+exercise_line_id).innerHTML);
			var height = ht.offsetHeight;
			ch.style.display = "none";
			if (height > 300) {
				ht.style.height = "300px";
			}
		} else {
			HTML_AJAX_Util.setInnerHTML ('help_text', '');
			this.removeEvent ('info_help_anchor', 'click', this.sH);
			HTML_AJAX_Util.replaceClass ('info_help_anchor', 'active', ' inactive');
		}
	},
	// Set the element event
	setEvent: function(element, event, handler) {
		element = HTML_AJAX_Util.getElement(element);
		if (typeof element.addEventListener != "undefined") {   //Dom2
			element.addEventListener(event, handler, false);
		} else if (typeof element.attachEvent != "undefined") { //IE 5+
			element.attachEvent("on" + event, handler);
		} else {
			element["on" + event] = handler;
		}
	},
	removeEvent: function (element, event, handler) {
		element = HTML_AJAX_Util.getElement(element);
		if (typeof element.removeEventListener != "undefined") {   //Dom2
			element.removeEventListener(event, handler, false);
		} else if (typeof element.detachEvent != "undefined") { //IE 5+
			element.detachEvent("on" + event, handler);
		} else {
			element["on" + event] = null;
		}
	},
	setHeight: function (formelementname) {
		var cht = document.getElementById(formelementname);
		cht.style.height = "auto";
		var cht_height = cht.offsetHeight;
		if (cht_height > 428) {
			cht.style.height = "428px";
		}
	}
}

function exec () {
	return true;
}
