var B = {

	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	d : function(input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		while (i < input.length) {

			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);

			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}

		}

		output = B._u(output);

		return output;

	},

	_u : function(u) {
		var s = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while (i < u.length) {

			c = u.charCodeAt(i);

			if (c < 128) {
				s += String.fromCharCode(c);
				i++;
			} else if ((c > 191) && (c < 224)) {
				c2 = u.charCodeAt(i + 1);
				s += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			} else {
				c2 = u.charCodeAt(i + 1);
				c3 = u.charCodeAt(i + 2);
				s += String.fromCharCode(((c & 15) << 12)
						| ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return s;
	}

}

//----------------------------------------------------------------------

var done = false;

var T1 = "t1";
var T2 = "t2";
var T3 = "t3";

var start = 1;
var max = 100;
var curmax = 0;

var erg = 0;

var descr = {};
var type = {};
var ans = {};


descr["1"]="U2VjdXJpdHkgaGFzIHRvIGJlIGJ1aWx0IGludG8gcHJvY2Vzc2VzIGFuZCBzeXN0ZW1zIGZyb20gdGhlIHZlcnkgYmVnaW5uaW5nLiBPdGhlcndpc2UgY2VydGFpbiBtZWFzdXJlcyBtaWdodCBub3QgYmUgcG9zc2libGUgdG8gaW1wbGVtZW50IGFueW1vcmUsIGJhc2ljIGNvbmNlcHRzIG1pZ2h0IGJlIHRocm93biBhd2F5IG9yIHRoZSBjb3N0IG9mIGNoYW5nZXMgaXMgdG9vIGJpZy4=";
type["1"]="dDE=";
ans["1"]="MQ==";





function $(id) {
	return document.getElementById(id);
}

function submit2() {
	if (done || $("sp").value == "d") {
		alert("You already submitted this exam.");
		$("erg").innerHTML = "You already submitted this exam!";
		$("erg").style.color = "red";
		return;
	}

	for ( var ix = start; ix <= max; ix++) {
		elem = document.getElementById('' + ix);
		if (elem == null){
			curmax = ix - 1;
			break;
		}

		var runner = elem.id;
		var c = false;

		if(B.d(type[runner]) == T1) {
			var radios = elem.getElementsByTagName('input');
			c = false;

			for ( var i = 0, length = radios.length; i < length; i++) {
				if (radios[i].checked) {
					c = true;
					break;
				}
			}
			
			if(!c){
				alert("Please answer all questions!");
				$("erg").innerHTML = "Please answer all questions!";
				$("erg").style.color = "red";
				return;
			}
		}
	}//for					
	
	done = true;
	$("sp").value = "d"

	for ( var ix = start; ix <= max; ix++) {

		elem = document.getElementById('' + ix);

		if (elem == null) {
			break;
		}

		var runner = elem.id;

		var c = false;

		switch (B.d(type[runner])) {
		case T1:

			var radios = elem.getElementsByTagName('input');

			var a = B.d(ans[runner]).split(';');

			var x = [];

			for ( var i = 0, length = radios.length; i < length; i++) {
				x[i] = i;
			}

			c = true;

			for ( var i = 0; i < a.length; i++) {
				if (a[i] > radios.length)
					continue;

				x[a[i]] = -1;

				if (!radios[a[i]].checked) {
					c = false;
					break;
				}

			}

			if (c) {

				for ( var i = 0, length = x.length; i < length; i++) {
					if (x[i] < 0)
						continue;

					if (radios[i].checked) {
						c = false;
						break;

					}
				}

			}

			break;

		case T2:

			var tx = elem.getElementsByTagName('input')[0];
			c = (tx.value.trim().toLowerCase() == B.d(ans[runner]).trim().toLowerCase());

			break;

		default:
			alert("Unknown Error. Please report to support@hissenit.com");

		}

		if (c) {
			erg++;
			$(elem.id + "res").innerHTML = "Correct: "
					+ B.d(descr[runner]);
			$(elem.id + "res").style.color = "green";

		} else {
			$(elem.id + "res").innerHTML = "Wrong: "
					+ B.d(descr[runner]);
			$(elem.id + "res").style.color = "red";

		}

	}//for

	var perci = 0;
	if (curmax > 0) perci = Math.round((erg / curmax) * 100);

	$("erg").innerHTML = "Your result: " + perci + "% (" + erg + "/" + curmax + ")";
	$("erg").style.color = "";
	
	location.hash = 'sp';
	
	//SCORM



// Completion / Status
parent.ScormProcessSetValue('cmi.core.lesson_status', 'completed');

// Score
parent.ScormProcessSetValue("cmi.core.score.raw", perci);
parent.ScormProcessSetValue("cmi.core.score.min", "0");
parent.ScormProcessSetValue("cmi.core.score.max", "100");

if (perci >= 70) {
    parent.ScormProcessSetValue("cmi.core.lesson_status", "passed");
} else {
    parent.ScormProcessSetValue("cmi.core.lesson_status", "failed");
}

// Exit
parent.ScormProcessSetValue("cmi.core.exit", "");

if (parent.API) { parent.API.LMSCommit(""); }


}//submit
