// cPaint object to handle ajax call 
var cpQuiz = new cpaint();
cpQuiz.set_transfer_mode('post');
cpQuiz.set_response_type('XML');
cpQuiz.set_persistent_connection(false);
//cpQuiz.set_debug(1);

var cpSetUser = new cpaint();
cpSetUser.set_transfer_mode('post');
cpSetUser.set_response_type('XML');
cpSetUser.set_persistent_connection(false);
//cpSetUser.set_debug(1);

var cpPreTestSubmit = new cpaint();
cpPreTestSubmit.set_transfer_mode('post');
cpPreTestSubmit.set_response_type('XML');
cpPreTestSubmit.set_persistent_connection(false);
//cpPreTestSubmit.set_debug(1);

var cpGetDBContent = new cpaint();
cpGetDBContent.set_transfer_mode('post');
cpGetDBContent.set_response_type('TEXT');
cpGetDBContent.set_persistent_connection(false);
//cpGetDBContent.set_debug(1);

var cpLogin = new cpaint();
cpLogin.set_transfer_mode('post');
cpLogin.set_response_type('XML');
cpLogin.set_persistent_connection(false);
//cpLogin.set_debug(1);

var cpEvalForm = new cpaint();
cpEvalForm.set_transfer_mode('post');
cpEvalForm.set_response_type('XML');
cpEvalForm.set_persistent_connection(false);
//cpEvalForm.set_debug(1);

var cpSurvey = new cpaint();
cpSurvey.set_transfer_mode('post');
cpSurvey.set_response_type('XML');
cpSurvey.set_persistent_connection(false);
//cpSurvey.set_debug(1);

var cpResults = new cpaint();
cpResults.set_transfer_mode('post');
cpResults.set_response_type('XML');
cpResults.set_persistent_connection(false);
//cpResults.set_debug(1);

var cpCertificate = new cpaint();
cpCertificate.set_transfer_mode('post');
cpCertificate.set_response_type('XML');
cpCertificate.set_persistent_connection(false);
//cpCertificate.set_debug(1);

var cpEmail = new cpaint();
cpEmail.set_transfer_mode('post');
cpEmail.set_response_type('XML');
cpEmail.set_persistent_connection(false);
//cpEmail.set_debug(1);

function getQuiz(userID, type) {
			
			// Send all specific variables to the function which gets relayed to a CPaint call and retrives 
			// all result based on the passed variables.  The results are then passed the a handle functions
			// wich in this case generates HTML content to populate a div.
			//alert(userID);
			var lang = getCurrentLanguage();
			document.getElementById('userContainer_div').style.display = 'none';
			document.getElementById('loading').style.display = 'block';
			document.body.style.cursor = 'wait';
			cpQuiz.call('../../../wp-includes/php/lib_getQuiz.php', 'Quiz', handleQuiz, userID, type, lang);
			//cpQuiz.call('wp-includes/php/lib_getQuiz.php', 'Quiz', handleQuiz, userID, type, lang);
		
}

function handleQuiz(result) {
	
			document.body.style.cursor = 'default';
			document.getElementById('loading').style.display = 'none';
			
			var Err = result.getElementsByTagName('err').item(0).firstChild.nodeValue;
			var ErrorType = result.getElementsByTagName('errType').item(0).firstChild.nodeValue;
			
			if(Err == 0) {
			
				var QType = parseInt(result.getElementsByTagName('qType').item(0).firstChild.nodeValue);
				var QuestionID = parseInt(result.getElementsByTagName('questionID').item(0).firstChild.nodeValue);
				var QuestionIDText = parseInt(result.getElementsByTagName('questionIDText').item(0).firstChild.nodeValue);
				var QuestionText = result.getElementsByTagName('question').item(0).firstChild.nodeValue;
				var MediaDestenation = result.getElementsByTagName('mediaDestenation').item(0).firstChild.nodeValue;
				var MediaType = result.getElementsByTagName('mediaType').item(0).firstChild.nodeValue;
				var QDetails = result.getElementsByTagName('qDetails').item(0).childNodes;
				document.getElementById('blnQType').value = QType;
			
				var htmlContent = '<form name="questionForm" action="">';
				htmlContent += '<ul id="quizChoices_ul">';
				
				var qNum = new Array("A","B","C","D","E","F","G");
				
				for (var i = 0; i < QDetails.length; i++) {
					
					var QDetailsID = QDetails[i].getElementsByTagName('questionDetailsID').item(0).firstChild.nodeValue
					var QChoice = QDetails[i].getElementsByTagName('choice').item(0).firstChild.nodeValue.replace(/\\U0026/gi, '&').replace(/\U0026/gi, '&').replace(/\\u0026/gi, '&').replace(/\\u000a/gi, '').replace(/\\u000d/gi, '');
					htmlContent += '<li><input type="radio" name="qSelection" id="qSelection' +  i + '" value="' + QDetailsID + '" />' + qNum[i] + '.  ' + QChoice + '</li>';
				}
				
				htmlContent += '</ul>';
				htmlContent += '<ul id="quizBtn">';
				htmlContent += '<li><input type="button" id="btnQuizQuestionSubmit" value="Next" onclick="validateQuestion(questionForm, '+QuestionID+');" class="btn" /></li>';
				htmlContent += '</ul>';
				htmlContent += '</form>';
				
				document.getElementById('intQuestionID').value = QuestionID;
				
				buildQuestion(QuestionID, QuestionIDText, QuestionText, MediaDestenation, htmlContent, MediaType);
			
			} else if(Err == 2) {
			
				var QType = parseInt(result.getElementsByTagName('qType').item(0).firstChild.nodeValue);
				var lang = getCurrentLanguage();
				cpQuiz.call('../../../wp-includes/php/lib_getScore.php', 'QuizScore', handleScore, document.getElementById('intUserID').value, QType, lang);
				//cpQuiz.call('wp-includes/php/lib_getScore.php', 'QuizScore', handleScore, document.getElementById('intUserID').value, QType, lang);
					
			} else {
				
				alert(ErrorType);
				
			}
}

function getQuestion(questionID, questionDeatilsID, userID, qType) {
			
			// Send all specific variables to the function which gets relayed to a CPaint call and retrives 
			// all result based on the passed variables.  The results are then passed the a handle functions
			// wich in this case generates HTML content to populate a div.
			var lang = getCurrentLanguage();
			document.getElementById('userContainer_div').style.display = 'none';
			document.getElementById('quizContainer_div').style.display = 'none';
			document.getElementById('loading').style.display = 'block';
			document.body.style.cursor = 'wait';
			cpQuiz.call('../../../wp-includes/php/lib_getQuestion.php', 'Question', handleQuiz, questionID, questionDeatilsID, userID, qType, lang);
			//cpQuiz.call('wp-includes/php/lib_getQuestion.php', 'Question', handleQuiz, questionID, questionDeatilsID, userID, qType, lang);
		
}


function buildQuestion(QuestionID, QuestionIDText, QuestionText, MediaDestenation, htmlContent, MediaType) {

	var html = "";
	
	html += "<h3>" + QuestionIDText + ".  " + QuestionText + "</h3>";
	if(MediaType == 'image') {
						
		html += "<div id='quizImage'><img src='" + MediaDestenation + "' alt='Question " + QuestionIDText + " Image' class='qImage' /></div>";
						
	} else if (MediaType == 'flash') {
						
		html += "<div clas='quizImage'><object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0' id='lftad_one' align='' width='300' height='270'> <param name='movie' value='" + MediaDestenation + "'> <param name='quality' value='high'> <param name='bgcolor' value='#FFFFFF'>  <param name='wmode' value='opaque'>  <embed src='" + MediaDestenation + "' quality='high' wmode='opaque' bgcolor='#FFFFFF' swliveconnect='FALSE' name='lftad_one' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' align='' width='300' height='270'></object></div>";
						
	} else {
						
		html += "<div clas='quizImage'><img src='" + MediaDestenation + "' alt='Question " + QuestionID + " Image' class='qImage' /></div>";
	
	}
	
	html += "<div class='clear'></div>";
	if (MediaType != 'flash') {
		html += "<div id='quizChoices_div'>";
	}	else {
		html += "<div id='quizChoices_div' style='float:right;margin:-290px 0 0 0;width:300px;'>";
	}
	
	html += htmlContent
	html += "</div><div class='clear'></div>";

	document.getElementById('quizContainer_div').innerHTML = html;
	document.getElementById('quizContainer_div').style.display = 'block';
	document.getElementById('quizContainer_div').style.height = '360px';

}

function validateLoginData(e) {// Validation Login

	var Valid = true;
	var rEmail = document.getElementById('txtRegisteredEmail');
	var rPassword = document.getElementById('txtRegisteredPassword');
	
	if(Valid) { // Email
		if(rEmail.value == '') {
			
			alert ( "Please fill in your email address." );
			Valid = false;
			rEmail.select();
			rEmail.focus();
		}
	}
	if(Valid) { // Email regular expression
		if(!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(rEmail.value)) {
			
			alert ( "Invalid e-mail address! Please re-enter." );
			Valid = false;
			rEmail.select();
			rEmail.focus();
		}
	}
	if(Valid) { // Password 
		if(rPassword.value == '') {
			
			alert ( "Please fill in your password." );
			Valid = false;
			rPassword.select();
			rPassword.focus();
		}
	}
	var lang = getCurrentLanguage();
	if(Valid) { checkLogin(rEmail.value, rPassword.value, e, lang) }; // Calls the cPaint Quiz function if Valid to check the email and password
}


function validateRegistrationData(e) { // Basic validation check
	
	//getPreQuiz(8);
	//getQuiz(2, 1);
	//cpQuiz.call('wp-includes/php/lib_getScore.php', 'QuizScore', handleScore, '6797', 1);
	//getStats();
	
	var Valid = true;
	var email = document.getElementById('txtEmail');
	var password = document.getElementById('txtPassword');
	var password2 = document.getElementById('txtConfirmPassword');
	var fname = document.getElementById('txtFirstName');
	var lname = document.getElementById('txtLastName');
	var city = document.getElementById('txtCity');
	var provinceState = document.getElementById('txtProvinceState');
	var country = document.getElementById('txtCountry');
	var consent = document.getElementById('chkConsent');
	var sessionID = document.getElementById('userSessionID').value;
	
	if(Valid) { // Email
		if(email.value == '') {
			
			alert ( "Please fill in your email address." );
			Valid = false;
			email.select();
			email.focus();
		}
	}
	var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	if(Valid) { // Email regular expression
		if(!re.test(email.value)) {
			
			alert ( "Invalid e-mail address! Please re-enter." );
			Valid = false;
			email.select();
			email.focus();
		}
	}
	if(Valid) { // Password 
		if(password.value == '') {
			
			alert ( "Please set a password." );
			Valid = false;
			password.select();
			password.focus();
		}
	}
	if(Valid) { // Password 
		if(password2.value == '') {
			
			alert ( "Please confirm your password." );
			Valid = false;
			password2.select();
			password2.focus();
		}
	}
	if(Valid) { // Password 
		if(password2.value != password.value) {
			
			alert ( "Password dones not match! Please re-enter." );
			Valid = false;
			password2.select();
			password2.focus();
		}
	}
	if(Valid) { // First Name 
		if(fname.value == '') {
			
			alert ( "Please fill in your first name." );
			Valid = false;
			fname.select();
			fname.focus();
		}
	}
	if(Valid) { // Last Name 
		if(lname.value == '') {
			
			alert ( "Please fill in your last name." );
			Valid = false;
			lname.select();
			lname.focus();
		}
	}
	if(Valid) { // Last Name 
		if(lname.value == '') {
			
			alert ( "Please fill in your last name." );
			Valid = false;
			lname.select();
			lname.focus();
		}
	}
	if(Valid) { // Last Name 
		if(consent.checked != true) {
			
			alert ( "Please read the consent and conditions of use.\nYou must check 'I agree to the terms, conditions and consent to participate in this study' in order to proceed." );
			Valid = false;
			consent.focus();
		}
	}
	
	if(Valid) { setUser(email.value, password.value, fname.value, lname.value, city.value, provinceState.value, country.value, sessionID, e) }; // Calls the cPaint Quiz function if Valid
}

function validateQuestion(e, questionID) {

	document.getElementById('btnQuizQuestionSubmit').disabled = 'disabled';
	document.getElementById('btnQuizQuestionSubmit').setAttribute('onclick','NULL');
	
	var Valid = true;
	var cnt = -1;
	var radio = e.qSelection;
	
	for (var i=radio.length-1; i > -1; i--) {
		if (radio[i].checked) {
		   cnt = i; i = -1;
		}
	}
	if (cnt < 0 ) { 
		alert ( "Please make a selection!." );
		document.getElementById('btnQuizQuestionSubmit').removeAttribute('disabled');
		document.getElementById('btnQuizQuestionSubmit').onclick = function() { validateQuestion(document.questionForm, questionID) };
		Valid = false;
	} else {
		var questionDeatilsID = radio[cnt].value;
	}
	
	
	if(Valid) { getQuestion(questionID, questionDeatilsID, document.getElementById('intUserID').value, document.getElementById('blnQType').value); }
}

function validatePreTestData(e) {
	
	document.getElementById('btnPreTestSubmit').disabled = 'disabled';
	document.getElementById('btnPreTestSubmit').setAttribute('onclick','NULL');
	
	var Valid = true;
	var cnt = -1;
	var radio1 = e.rdQ1;
	for (var i=radio1.length-1; i > -1; i--) {
		if (radio1[i].checked) {
		   cnt = i; i = -1;
		}
		if(radio1[6].checked ) {
			if(document.getElementById('txtOther').value == '') {
				alert ( "Please specity your level of training." );
				document.getElementById('txtOther').select();
				document.getElementById('txtOther').focus();
				document.getElementById('btnPreTestSubmit').removeAttribute('disabled');
				document.getElementById('btnPreTestSubmit').onclick = function() { validatePreTestData(e) };
				Valid = false;
			} else {
				var q1txt = document.getElementById('txtOther').value;
			}
		} else {
				var q1txt = '';
		}
	}
	if(Valid) {
		if (cnt < 0 ) { 
			alert ( "Please answer question 1." );
			document.getElementById('btnPreTestSubmit').removeAttribute('disabled');
			document.getElementById('btnPreTestSubmit').onclick = function() { validatePreTestData(e) };
			Valid = false;
		} else {
			var q1 = radio1[cnt].value;
		}
	}
	var radio2 = e.rdQ2;
	var cnt = -1;
	for (var i=radio2.length-1; i > -1; i--) {
		if (radio2[i].checked) {
		   cnt = i; i = -1;
		}
	}
	if(Valid) {
		if (cnt < 0 ) { 
			alert ( "Please answer question 2." );
			document.getElementById('btnPreTestSubmit').removeAttribute('disabled');
			document.getElementById('btnPreTestSubmit').onclick = function() { validatePreTestData(e) };
			Valid = false;
		} else {
			var q2 = radio2[cnt].value;
		}
	}
	var radio3 = e.rdQ3;
	var cnt = -1;
	for (var i=radio3.length-1; i > -1; i--) {
		if (radio3[i].checked) {
		   cnt = i; i = -1;
		}
	}
	if(Valid) {
		if (cnt < 0 ) { 
			alert ( "Please answer question 3." );
			document.getElementById('btnPreTestSubmit').removeAttribute('disabled');
			document.getElementById('btnPreTestSubmit').onclick = function() { validatePreTestData(e) };
			Valid = false;
		} else {
			var q3 = radio3[cnt].value;
		}
	}
	if(Valid) {
		if(document.getElementById('txtO4').value == '') {
			alert ( "Please specity the number of bronchoscopic examinations performed." );
			document.getElementById('txtO4').select();
			document.getElementById('txtO4').focus();
			document.getElementById('btnPreTestSubmit').removeAttribute('disabled');
			document.getElementById('btnPreTestSubmit').onclick = function() { validatePreTestData(e) };
			Valid = false;
		} else {
			if(isNaN(document.getElementById('txtO4').value)) {
				alert ( "Please enter a numeric value." );
				document.getElementById('txtO4').select();
				document.getElementById('txtO4').focus();
				document.getElementById('btnPreTestSubmit').removeAttribute('disabled');
				document.getElementById('btnPreTestSubmit').onclick = function() { validatePreTestData(e) };
				Valid = false;
			} else {
				var q4 = document.getElementById('txtO4').value;
			}
		}
	}
	
	if(Valid) { submitPreTest(q1, q2, q3, q4, q1txt); }
}

function setUser(email, password, fname, lname, title, organization, address, address2, city, postalZip, provinceState, country, sessionID, type) {
	
		// Send all specific variables to the function which gets relayed to a CPaint call and retrives 
		// all result based on the passed variables.  The results are then passed the a handle functions
		// wich in this case generates HTML content to populate a div.
			
		document.getElementById('userContainer_div').style.display = 'none';
		document.getElementById('loading').style.display = 'block';
		document.body.style.cursor = 'wait';
		cpSetUser.call('../../../wp-includes/php/lib_setUser.php', 'User', handleSetUser, email, password, fname, lname, title, organization, address, address2, city, postalZip, provinceState, country, sessionID, type);
		//cpSetUser.call('wp-includes/php/lib_setUser.php', 'User', handleSetUser, email, password, fname, lname, title, organization, address, address2, city, postalZip, provinceState, country, sessionID, type);
		
}

function handleSetUser(result) {
	
		var Err = result.getElementsByTagName('err').item(0).firstChild.nodeValue;
		var ErrorType = result.getElementsByTagName('errType').item(0).firstChild.nodeValue;
		
		if(Err == 1) {
				
			alert(ErrorType);
		
		} else if (Err == 2) {
			
			alert(ErrorType);
			document.location.href='?page_id=3';
			
		} else {
			
			var typeID = result.getElementsByTagName('type').item(0).firstChild.nodeValue;
			var userID = result.getElementsByTagName('userID').item(0).firstChild.nodeValue;
			document.getElementById('intUserID').value = userID;
			
			alert('You have been successfully registered!');
			
			if(typeID == 1) {
				
				getPreQuiz(8);
				
			} else if (typeID == 3) {
				
				window.location.href = "http://www.thoracic-anesthesia.com/?page_id=2";
				//window.location.href = "http://www.thoracic-anesthesia.com/dev/?page_id=2";
				
			} else if (typeID == 4) {
				
				window.location.href = "http://www.thoracic-anesthesia.com/?page_id=15";	
				//window.location.href = "http://www.thoracic-anesthesia.com/dev/?page_id=15";	
					
			}
		
		}
			
}

function getPreQuiz(wpPostID) {

	var lang = getCurrentLanguage();
	if(lang == 'es') {
		cpGetDBContent.call('../../../wp-includes/php/lib_getDBContent.php', 'Content', handleContent, 73);
		//cpGetDBContent.call('wp-includes/php/lib_getDBContent.php', 'Content', handleContent, 73);
	} else if(lang == 'cn') {
		cpGetDBContent.call('wp-includes/php/lib_getDBContent.php', 'Content', handleContent, 72);
		//cpGetDBContent.call('../../../wp-includes/php/lib_getDBContent.php', 'Content', handleContent, 72);
	} else {
		cpGetDBContent.call('../../../wp-includes/php/lib_getDBContent.php', 'Content', handleContent, wpPostID);
		//cpGetDBContent.call('wp-includes/php/lib_getDBContent.php', 'Content', handleContent, wpPostID);
	}
	
}

function handleContent(result) {

	var html = result;
	document.getElementById('loading').style.display = 'none';
	document.body.style.cursor = 'default';
	document.getElementById('userContainer_div').innerHTML = html.replace(/\\u003c/gi, '<').replace(/\\u003e/gi, '>').replace(/\\u000d\\u000a\\/gi, '\n').replace(/\\u0009/gi, '\t').replace(/u0009/gi, '\t').replace(/\\u000d\\u000a/gi, '\n');
	document.getElementById('userContainer_div').style.display = 'block';
	
}

function submitPreTest(q1, q2, q3, q4, q1txt) {
	
	cpPreTestSubmit.call('../../../wp-includes/php/lib_setPreTestAnswer.php', 'PreTestAnswers', handlePreTestAnswers, q1, q2, q3, q4, q1txt, document.getElementById('intUserID').value);	
	//cpPreTestSubmit.call('wp-includes/php/lib_setPreTestAnswer.php', 'PreTestAnswers', handlePreTestAnswers, q1, q2, q3, q4, q1txt, document.getElementById('intUserID').value);	
	
}

function handlePreTestAnswers(result) {
	
	var Err = result.getElementsByTagName('err').item(0).firstChild.nodeValue;
	var ErrorType = result.getElementsByTagName('errType').item(0).firstChild.nodeValue;
		
		if(Err != 0) {
			
			alert(ErrorType);
			
		} else {
			
			var userID = result.getElementsByTagName('userID').item(0).firstChild.nodeValue;
			document.getElementById('intUserID').value = userID;
			checkFlash(userID, 1);
		
		}
			
}

function checkFlash(userID, qID, qdID, qType) {

	document.getElementById('userContainer_div').innerHTML = '';
	document.getElementById('quizContainer_div').innerHTML = '';
	var html = "Verify Your Browser Settings ...";
	document.getElementById('quizContainer_div').innerHTML = html;
	document.getElementById('quizContainer_div').style.display = 'block';
	<!--
	// Version check based upon the values entered above in "Globals"
	var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
	// Check to see if the version meets the requirements for playback
	if (hasReqestedVersion) {
		// if we've detected an acceptable version
		// embed the Flash Content SWF when all tests are passed
		if(qID != '') {
			setTimeout("getQuiz(" + userID + ", " + qID + ");", 2000);
		} else {
			setTimeout("getQuestion(" + qdID + ",  '', " + userID + ", " + qType + ");", 2000);
		}
		
	} else {  // flash is too old or we can't detect the plugin
		var alternateContent = 'This site requires the Adobe Flash Player 8 or later. <br /><br />'
		+ '<a href=http://www.adobe.com/go/getflash/>Get Flash</a><br /><br />'
		+ 'Refresh the page once you complete installing Adobe Flash Player.';
		document.getElementById('quizContainer_div').innerHTML = alternateContent;
	}
	// -->
}

function checkLogin(Email, Password, e, lang) {
	
	cpLogin.call('../../../wp-includes/php/lib_checkLogin.php', 'Login', handleLogin, Email, Password, e, lang);	
	//cpLogin.call('wp-includes/php/lib_checkLogin.php', 'Login', handleLogin, Email, Password, e, lang);	
	
}

function handleLogin(result) {
	
	var Lang = result.getElementsByTagName('language').item(0).firstChild.nodeValue;
	var Err = result.getElementsByTagName('err').item(0).firstChild.nodeValue;
	var ErrorType = result.getElementsByTagName('errType').item(0).firstChild.nodeValue;
		
		if(Err == 1) {
				
			alert(ErrorType);
			
		} else {
			
			var userID = result.getElementsByTagName('userID').item(0).firstChild.nodeValue;
			var loginType = result.getElementsByTagName('loginType').item(0).firstChild.nodeValue;
			document.getElementById('intUserID').value = userID;
			 
			var menuHTML = document.getElementById('menu').innerHTML; 
			menuHTML += '<li><a href="#" onclick="logout();">Logout</a></li>';
			document.getElementById('menu').innerHTML = menuHTML;
			
			if(loginType == '1') {
				
				getLoginScreen();
			
			} else if(loginType == '3') { 
				
				window.location.href = "http://www.thoracic-anesthesia.com/?page_id=2&langswitch_lang="+Lang;
				//window.location.href = "http://www.thoracic-anesthesia.com/dev/?page_id=2&langswitch_lang="+Lang;
			
			} else if(loginType == '4') { 
				
				window.location.href = "http://www.thoracic-anesthesia.com/?page_id=15&langswitch_lang="+Lang;
				//window.location.href = "http://www.thoracic-anesthesia.com/dev/?page_id=15&langswitch_lang="+Lang;
			
			} else {
			
				window.location.href = "http://www.thoracic-anesthesia.com/";
				//window.location.href = "http://www.thoracic-anesthesia.com/dev/";
			
			}
			
		}
			

}

function handleScore(result) {
	
	var Err = result.getElementsByTagName('err').item(0).firstChild.nodeValue;
	var ErrorType = result.getElementsByTagName('errType').item(0).firstChild.nodeValue;
	var lang = getCurrentLanguage();	
		if(Err == 1) {
				
			alert(ErrorType);
			
		} else {
			
			var Score = result.getElementsByTagName('score').item(0).firstChild.nodeValue;
			var QNum = result.getElementsByTagName('qnum').item(0).firstChild.nodeValue;
			var Sim = result.getElementsByTagName('sim').item(0).firstChild.nodeValue;
			var QType = result.getElementsByTagName('qtype').item(0).firstChild.nodeValue; //Survey
			
			var htmlContent = "<h2>You're Done</h2>";
				htmlContent += "<h4>You scored " + Score + " out of " + QNum + " on the quiz!</h4>";
				
				if(Sim == 0) {
					htmlContent += "<p>Please experiment with the bronchoscopy simulator and then try the quiz again.</p>";
					htmlContent += "<p>Click <a href=\"#\" onclick=\"window.location.href='http://www.thoracic-anesthesia.com/?page_id=2&langswitch_lang="+lang+"'\" />here</a> for the simulator.</p>";
					//htmlContent += "<p>Click <a href=\"#\" onclick=\"window.location.href='http://www.thoracic-anesthesia.com/dev/?page_id=2&langswitch_lang="+lang+"'\" />here</a> for the simulator.</p>";
					
					//Survey Stuff
					
				} else {
					htmlContent += "<p>Please continue and complete the evaluation form.  Once you have submitted the form you'll be able to compare how you scored with other participants.</p>";
					if(QType == 2) {
						//htmlContent += "<p><input type='button' value='Continue' onclick='getSurvey();' class='btn' /></p>";
						htmlContent += "<p><input type='button' value='Continue' onclick='getEvaluationForm();' class='btn' /></p>";
					} else {
						htmlContent += "<p><input type='button' value='Continue' onclick='getEvaluationForm();' class='btn' /></p>";
					}
				var Q = result.getElementsByTagName('questions').item(0).childNodes;
				var QNum = 1;
				for (var i = 0; i < Q.length; i++) {
						
					var Question = Q[i].getElementsByTagName('question').item(0).firstChild.nodeValue.replace(/\\U0026/gi, '&').replace(/\U0026/gi, '&').replace(/\\u0026/gi, '&').replace(/\\u000a/gi, '').replace(/\\u000d/gi, '');
					var MediaDestenation = Q[i].getElementsByTagName('media').item(0).firstChild.nodeValue;
					var MediaType = Q[i].getElementsByTagName('mediaType').item(0).firstChild.nodeValue;
					var Answer = Q[i].getElementsByTagName('answer').item(0).firstChild.nodeValue.replace(/\\U0026/gi, '&').replace(/\U0026/gi, '&').replace(/\\u0026/gi, '&').replace(/\\u000a/gi, '').replace(/\\u000d/gi, '');
					var AnswerID = Q[i].getElementsByTagName('answerID').item(0).firstChild.nodeValue;
					var QuestionDetialsID = Q[i].getElementsByTagName('questionDetialsID').item(0).firstChild.nodeValue;
						
						htmlContent += "<h3>" + QNum + ".  " + Question + "</h3>";
						if(MediaType == 'image') {
							
							htmlContent += "<div clas='answerImage'><img src='" + MediaDestenation + "' alt='Question " + QNum + " Image' class='qImage' /></div>";
							
						} else if (MediaType == 'flash') {
							
							htmlContent += "<div clas='answerImage' style='width:300px;height:295px;'><object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0' id='lftad_one' align='' width='300' height='270'> <param name='movie' value='" + MediaDestenation + "'> <param name='quality' value='high'> <param name='bgcolor' value='#FFFFFF'>  <param name='wmode' value='opaque'>  <embed src='" + MediaDestenation + "' quality='high' wmode='opaque' bgcolor='#FFFFFF' swliveconnect='FALSE' name='lftad_one' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' align='' width='300' height='270'></object></div>";
							
						} else {
							
							htmlContent += "<div clas='answerImage'><img src='" + MediaDestenation + "' alt='Question " + QNum + " Image' class='qImage' /></div>";
						}
						htmlContent += "<h3>Correct Answer: " + Answer + "</h3>";
						htmlContent += "<div class='clear'></div>";
						htmlContent += '<ul id="quizChoices_ul" style="float:right;margin:-360px 0 0 0;width:300px;">';
						htmlContent += '<li>Your Answer</li>';
				
					var Q2 = result.getElementsByTagName('questions2').item(i).childNodes;
					var QNum2 = new Array("A","B","C","D","E","F","G");
					for (var j = 0; j < Q2.length; j++) {
					
						var QChoice = Q2[j].getElementsByTagName('choice').item(0).firstChild.nodeValue.replace(/\\U0026/gi, '&').replace(/\U0026/gi, '&').replace(/\\u0026/gi, '&').replace(/\\u000a/gi, '').replace(/\\u000d/gi, '');
						var QuestionDetialsID2 =  Q2[j].getElementsByTagName('choiceQuestionDetialsID').item(0).firstChild.nodeValue;
						if(QuestionDetialsID == QuestionDetialsID2) {
							
							if(AnswerID == QuestionDetialsID2) {
								
								htmlContent += '<li style="color:#00D300;">' + QNum2[j] + '.  ' + QChoice + ' <img src="http://www.thoracic-anesthesia.com/wp-content/themes/integral-10/images/button_ok.png" alt="Correct Answer" class="qImage" /></li>';
								
							} else {
								
								htmlContent += '<li style="color:#FF2A00;">' + QNum2[j] + '.  ' + QChoice + ' <img src="http://www.thoracic-anesthesia.com/wp-content/themes/integral-10/images/button_cancel.png" alt="Wrong Answer" class="qImage" /></li>';
								
							}
							
						} else {
						
							htmlContent += '<li>' + QNum2[j] + '.  ' + QChoice + '</li>';
						
						}
						
					}
					
					htmlContent += '</ul>';
					htmlContent += "<hr noshade='noshade' size='1' />";
					QNum++;
					
				}
				htmlContent += "<div class='clear'></div>";
				
				if(QType == 2) {
					htmlContent += "<p><input type='button' value='Continue' onclick='getSurvey();' class='btn' /></p>";
				} else {
					htmlContent += "<p><input type='button' value='Continue' onclick='getEvaluationForm();' class='btn' /></p>";
				}
			}
			document.getElementById('userContainer_div').innerHTML = htmlContent;
			document.getElementById('userContainer_div').style.display = 'block';
		
		}
				
}

function getLoginScreen() {

	cpLogin.call('../../../wp-includes/php/lib_checkLoginStatus.php111', 'LoginStatus', handleLoginStatus, document.getElementById('intUserID').value);	
	//cpLogin.call('wp-includes/php/lib_checkLoginStatus.php', 'LoginStatus', handleLoginStatus, document.getElementById('intUserID').value);	

}

function handleLoginStatus(result) {

	var Err = result.getElementsByTagName('err').item(0).firstChild.nodeValue;
	var ErrorType = result.getElementsByTagName('errType').item(0).firstChild.nodeValue;
	var lang = getCurrentLanguage();
	if(Err == 1) {
			
		alert(ErrorType);
		
	} else if(Err == 2) {
		
		var userID = result.getElementsByTagName('userID').item(0).firstChild.nodeValue;
		var QuestionID = result.getElementsByTagName('QuestionID').item(0).firstChild.nodeValue;
		var QType = result.getElementsByTagName('QType').item(0).firstChild.nodeValue;
		
		var htmlContent = "<h2>You have a quiz that has not been completed.</h2>";
			htmlContent += "<p>Click <a href=\"#\" onclick=\"checkFlash("+userID+", '', "+QuestionID+", "+QType+");\" />here</a> to continue where you left off.</p>";
			
		document.getElementById('userContainer_div').innerHTML = htmlContent;
		document.getElementById('userContainer_div').style.display = 'block';
		
	} else if(Err == 3) {
		
		var htmlContent = "<h2>You have not completed the pre-test quiz.</h2>";
			htmlContent += "<p>Click <a href=\"#\" onclick=\"getPreQuiz(8);\" />here</a> to continue where you left off.</p>";
			
		document.getElementById('userContainer_div').innerHTML = htmlContent;
		document.getElementById('userContainer_div').style.display = 'block';
		
	} else if(Err == 4) {
		
		var htmlContent = "<h2>You have not completed the evaluation form.</h2>";
			htmlContent += "<p>Click <a href=\"#\" onclick=\"getEvaluationForm();\" />here</a> to continue where you left off.</p>";
			
		document.getElementById('userContainer_div').innerHTML = htmlContent;
		document.getElementById('userContainer_div').style.display = 'block';
			
	} else if(Err == 5) {
		
		var htmlContent = "<h2>You have not used the Bronchoscopy Simulator.</h2>";
			htmlContent += "<p>Click <a href=\"#\" onclick=\"window.location.href='http://www.thoracic-anesthesia.com/?page_id=2&langswitch_lang="+lang+"'\" />here</a> for the simulator. Once you are finished with the simulator please click on the Bronchial Anatomy Quiz link to retake the quiz.</p>";
			//htmlContent += "<p>Click <a href=\"#\" onclick=\"window.location.href='http://www.thoracic-anesthesia.com/dev/?page_id=2&langswitch_lang="+lang+"'\" />here</a> for the simulator. Once you are finished with the simulator please click on the Bronchial Anatomy Quiz link to retake the quiz.</p>";
			
		document.getElementById('userContainer_div').innerHTML = htmlContent;
		document.getElementById('userContainer_div').style.display = 'block';
	
	} else if(Err == 6) {
			
		var userID = result.getElementsByTagName('userID').item(0).firstChild.nodeValue;
		
		
		var htmlContent = "<h2>You have completed the Bronchoscopy Simulator.</h2>";
			htmlContent += "<p>Click <a href=\"#\" onclick=\"checkFlash("+userID+", 3)\" />here</a> to retake the Bronchial Anatomy Quiz.</p>";
		
		
		document.getElementById('userContainer_div').innerHTML = htmlContent;
		document.getElementById('userContainer_div').style.display = 'block';
		
	} else if(Err == 7) {
			
		var QuestionID = result.getElementsByTagName('QuestionID').item(0).firstChild.nodeValue;
		var QType = result.getElementsByTagName('QType').item(0).firstChild.nodeValue;
		
		var htmlContent = "<h2>You have a quiz that has not been completed.</h2>";
			htmlContent += "<p>Click <a href=\"#\" onclick=\"getQuestion(" + QuestionID + ",  '', document.getElementById('intUserID').value, " + QType + ");\" />here</a> to continue where you left off.</p>";
			
		document.getElementById('userContainer_div').innerHTML = htmlContent;
		document.getElementById('userContainer_div').style.display = 'block';
		
	} else {
;
		var Score = result.getElementsByTagName('score').item(0).firstChild.nodeValue;
		var QNum = result.getElementsByTagName('qnum').item(0).firstChild.nodeValue;
		var Retake = result.getElementsByTagName('retake').item(0).firstChild.nodeValue;
		var Attempt = result.getElementsByTagName('attempt').item(0).firstChild.nodeValue;
		var userID = result.getElementsByTagName('userID').item(0).firstChild.nodeValue;
		var Survey = result.getElementsByTagName('survey').item(0).firstChild.nodeValue; // Add for the survey
		var Eval = result.getElementsByTagName('eval').item(0).firstChild.nodeValue; 


		var htmlContent = "<h2>You've completed the quiz.</h2>";
			htmlContent += "<h4>You scored " + Score + " out of " + QNum + " on the " + Attempt + " attempt!</h4>";
		
		// Add for the survey
		if(Survey == 0 && Retake == 'complete') {	
			htmlContent += "<p>Click <a href=\"#\" onclick=\"getSurvey();\">here</a> to complete a quick survey and compare how you scored with other participants. </p>";
		} else {
			htmlContent += "<p>Click <a href=\"#\" onclick=\"getStats();\">here</a> to compare how you scored with other participants. </p>";
		}
		if(Eval == 1 ) {	
			htmlContent += "<p>Click <a href=\"http://www.uhn.ca/BSCME09.asp\"  target=\"_blank\">here</a> to purchase your CME credits and certificate.</p>";
		} 
		/*if(Retake == 'true') {
		
			htmlContent += "<p>It's been more then 3 months since you took the quiz.  At this time we would like to ask you to retake the quiz.  Upon completion you'll be able to compare your second score with othre participants and receive a certificate.</p>";
			htmlContent += "<p><a href=\"#\" onclick=\"checkFlash("+userID+", 2);\">Retake Quiz</a></p>";
			
		}*/
		
		document.getElementById('userContainer_div').innerHTML = htmlContent;
		document.getElementById('userContainer_div').style.display = 'block';
		
	}
	
}

//Survey Stuff
function getSurvey() {
	
	var lang = getCurrentLanguage();
	if(lang == 'es') {
		cpGetDBContent.call('../../../wp-includes/php/lib_getDBContent.php', 'Content', handleContent, 116);	
		//cpGetDBContent.call('wp-includes/php/lib_getDBContent.php', 'Content', handleContent, 116);	
	} else if(lang == 'cn') {
		cpGetDBContent.call('../../../wp-includes/php/lib_getDBContent.php', 'Content', handleContent, 116);	
		//cpGetDBContent.call('wp-includes/php/lib_getDBContent.php', 'Content', handleContent, 116);	
	} else {
		cpGetDBContent.call('../../../wp-includes/php/lib_getDBContent.php', 'Content', handleContent, 116);	
		//cpGetDBContent.call('wp-includes/php/lib_getDBContent.php', 'Content', handleContent, 116);	
	}
	
	
}
//Survey Stuff
function validateSurveyData(e) {
	
	
	document.getElementById('btnEvaluationSubmit').disabled = 'disabled';
	document.getElementById('btnEvaluationSubmit').setAttribute('onclick','NULL');
	
	var Valid = true;
	var cnt = -1;
	var radio1 = e.rdQ1;
	for (var i=radio1.length-1; i > -1; i--) {
		if (radio1[i].checked) {
		   cnt = i; i = -1;
		}
	}
	if(Valid) {
		if (cnt < 0 ) { 
			alert ( "Please answer question 1." );
			document.getElementById('btnEvaluationSubmit').removeAttribute('disabled');
			document.getElementById('btnEvaluationSubmit').onclick = function() { validateEvaluationData(document.myform) };
			Valid = false;
		} else {
			var q1 = radio1[cnt].value;
		}
	}
	var radio2 = e.rdQ2;
	var cnt = -1;
	for (var i=radio2.length-1; i > -1; i--) {
		if (radio2[i].checked) {
		   cnt = i; i = -1;
		}
	}
	if(Valid) {
		if (cnt < 0 ) { 
			alert ( "Please answer question 2." );
			document.getElementById('btnEvaluationSubmit').removeAttribute('disabled');
			document.getElementById('btnEvaluationSubmit').onclick = function() { validateEvaluationData(document.myform) };
			Valid = false;
		} else {
			var q2 = radio2[cnt].value;
		}
	}
	var radio3 = e.rdQ3;
	var cnt = -1;
	for (var i=radio3.length-1; i > -1; i--) {
		if (radio3[i].checked) {
		   cnt = i; i = -1;
		}
	}
	if(Valid) {
		if (cnt < 0 ) { 
			alert ( "Please answer question 3." );
			document.getElementById('btnEvaluationSubmit').removeAttribute('disabled');
			document.getElementById('btnEvaluationSubmit').onclick = function() { validateEvaluationData(document.myform) };
			Valid = false;
		} else {
			var q3 = radio3[cnt].value;
		}
	}
	var radio4 = e.rdQ4;
	var cnt = -1;
	for (var i=radio4.length-1; i > -1; i--) {
		if (radio4[i].checked) {
		   cnt = i; i = -1;
		}
	}
	if(Valid) {
		if (cnt < 0 ) { 
			alert ( "Please answer question 4." );
			document.getElementById('btnEvaluationSubmit').removeAttribute('disabled');
			document.getElementById('btnEvaluationSubmit').onclick = function() { validateEvaluationData(document.myform) };
			Valid = false;
		} else {
			var q4 = radio4[cnt].value;
		}
	}
	var q5 = document.getElementById('txtQ5').value;
	
	if(Valid) { submitSurvey(q1, q2, q3, q4, q5); }
}

function submitSurvey(q1, q2, q3, q4, q5) {
	
	cpEvalForm.call('../../../wp-includes/php/lib_setEvalFormAnswer.php', 'EvalAnswers', handleEvalAnswers, q1, q2, q3, q4, q5, document.getElementById('intUserID').value);	
	//cpSurvey.call('wp-includes/php/lib_setSurveyAnswer.php', 'SurveyAnswers', handleSurveyAnswers, q1, q2, q3, q4, q5, document.getElementById('intUserID').value);	
}

function handleSurveyAnswers(result) {
	
	var Err = result.getElementsByTagName('err').item(0).firstChild.nodeValue;
	var ErrorType = result.getElementsByTagName('errType').item(0).firstChild.nodeValue;
	
	
		if(Err != 0) {
			
			alert(ErrorType);
			
		} else {
			
			getStats();
		
		}
			
}
//End Survey Stuff


function getEvaluationForm() {
	
	var lang = getCurrentLanguage();
	if(lang == 'es') {
		cpGetDBContent.call('../../../wp-includes/php/lib_getDBContent.php', 'Content', handleContent, 12);	
		//cpGetDBContent.call('wp-includes/php/lib_getDBContent.php', 'Content', handleContent, 12);	
	} else if(lang == 'cn') {
		cpGetDBContent.call('../../../wp-includes/php/lib_getDBContent.php', 'Content', handleContent, 74);	
		//cpGetDBContent.call('wp-includes/php/lib_getDBContent.php', 'Content', handleContent, 74);	
	} else {
		cpGetDBContent.call('../../../wp-includes/php/lib_getDBContent.php', 'Content', handleContent, 12);	
		//cpGetDBContent.call('wp-includes/php/lib_getDBContent.php', 'Content', handleContent, 12);	
	}
	
}

function validateEvaluationData(e) {
	
	
	document.getElementById('btnEvaluationSubmit').disabled = 'disabled';
	document.getElementById('btnEvaluationSubmit').setAttribute('onclick','NULL');
	
	var Valid = true;
	var cnt = -1;
	var radio1 = e.rdQ1;
	for (var i=radio1.length-1; i > -1; i--) {
		if (radio1[i].checked) {
		   cnt = i; i = -1;
		}
	}
	if(Valid) {
		if (cnt < 0 ) { 
			alert ( "Please answer question 1." );
			document.getElementById('btnEvaluationSubmit').removeAttribute('disabled');
			document.getElementById('btnEvaluationSubmit').onclick = function() { validateEvaluationData(document.myform) };
			Valid = false;
		} else {
			var q1 = radio1[cnt].value;
		}
	}
	var radio2 = e.rdQ2;
	var cnt = -1;
	for (var i=radio2.length-1; i > -1; i--) {
		if (radio2[i].checked) {
		   cnt = i; i = -1;
		}
	}
	if(Valid) {
		if (cnt < 0 ) { 
			alert ( "Please answer question 2." );
			document.getElementById('btnEvaluationSubmit').removeAttribute('disabled');
			document.getElementById('btnEvaluationSubmit').onclick = function() { validateEvaluationData(document.myform) };
			Valid = false;
		} else {
			var q2 = radio2[cnt].value;
		}
	}
	var radio3 = e.rdQ3;
	var cnt = -1;
	for (var i=radio3.length-1; i > -1; i--) {
		if (radio3[i].checked) {
		   cnt = i; i = -1;
		}
	}
	if(Valid) {
		if (cnt < 0 ) { 
			alert ( "Please answer question 3." );
			document.getElementById('btnEvaluationSubmit').removeAttribute('disabled');
			document.getElementById('btnEvaluationSubmit').onclick = function() { validateEvaluationData(document.myform) };
			Valid = false;
		} else {
			var q3 = radio3[cnt].value;
		}
	}
	var radio4 = e.rdQ4;
	var cnt = -1;
	for (var i=radio4.length-1; i > -1; i--) {
		if (radio4[i].checked) {
		   cnt = i; i = -1;
		}
	}
	if(Valid) {
		if (cnt < 0 ) { 
			alert ( "Please answer question 4." );
			document.getElementById('btnEvaluationSubmit').removeAttribute('disabled');
			document.getElementById('btnEvaluationSubmit').onclick = function() { validateEvaluationData(document.myform) };
			Valid = false;
		} else {
			var q4 = radio4[cnt].value;
		}
	}
	var radio5 = e.rdQ5;
	var cnt = -1;
	for (var i=radio5.length-1; i > -1; i--) {
		if (radio5[i].checked) {
		   cnt = i; i = -1;
		}
	}
	if(Valid) {
		if (cnt < 0 ) { 
			alert ( "Please answer question 5." );
			document.getElementById('btnEvaluationSubmit').removeAttribute('disabled');
			document.getElementById('btnEvaluationSubmit').onclick = function() { validateEvaluationData(document.myform) };
			Valid = false;
		} else {
			var q5 = radio5[cnt].value;
		}
	}
	var radio6 = e.rdQ6;
	var cnt = -1;
	for (var i=radio6.length-1; i > -1; i--) {
		if (radio6[i].checked) {
		   cnt = i; i = -1;
		}
	}
	if(Valid) {
		if (cnt < 0 ) { 
			alert ( "Please answer question 6." );
			document.getElementById('btnEvaluationSubmit').removeAttribute('disabled');
			document.getElementById('btnEvaluationSubmit').onclick = function() { validateEvaluationData(document.myform) };
			Valid = false;
		} else {
			var q6 = radio6[cnt].value;
		}
	}
	var radio7 = e.rdQ7;
	var cnt = -1;
	for (var i=radio7.length-1; i > -1; i--) {
		if (radio7[i].checked) {
		   cnt = i; i = -1;
		}
	}
	if(Valid) {
		if (cnt < 0 ) { 
			alert ( "Please answer question 7." );
			document.getElementById('btnEvaluationSubmit').removeAttribute('disabled');
			document.getElementById('btnEvaluationSubmit').onclick = function() { validateEvaluationData(document.myform) };
			Valid = false;
		} else {
			var q7 = radio7[cnt].value;
		}
	}
	var q8 = document.getElementById('txtQ8').value;
	
	if(Valid) { submitEvaluationForm(q1, q2, q3, q4, q5, q6, q7, q8); }
}

function submitEvaluationForm(q1, q2, q3, q4, q5, q6, q7, q8) {
	
	cpEvalForm.call('../../../wp-includes/php/lib_setEvalFormAnswer.php', 'EvalAnswers', handleEvalAnswers, q1, q2, q3, q4, q5, q6, q7, q8, document.getElementById('intUserID').value);	
	//cpEvalForm.call('wp-includes/php/lib_setEvalFormAnswer.php', 'EvalAnswers', handleEvalAnswers, q1, q2, q3, q4, q5, q6, q7, q8, document.getElementById('intUserID').value);	
}

function handleEvalAnswers(result) {
	
	var Err = result.getElementsByTagName('err').item(0).firstChild.nodeValue;
	var ErrorType = result.getElementsByTagName('errType').item(0).firstChild.nodeValue;
	
	
		if(Err != 0) {
			
			alert(ErrorType);
			
		} else {
			
			getStats();
		
		}
			
}

function getStats() {
	
	document.getElementById('userContainer_div').innerHTML = 'Calculating statistics please be patient this may take several minutes.';
	document.getElementById('loading').style.display = 'block';
	document.body.style.cursor = 'wait';
	
	cpResults.call('../../../wp-includes/php/lib_getResults.php', 'Results', handleResults, document.getElementById('intUserID').value);
	//cpResults.call('wp-includes/php/lib_getResults.php', 'Results', handleResults, document.getElementById('intUserID').value);

}

function handleResults(result) {
	
	document.body.style.cursor = 'default';
	document.getElementById('loading').style.display = 'none';
	var Err = result.getElementsByTagName('err').item(0).firstChild.nodeValue;
	var ErrorType = result.getElementsByTagName('errType').item(0).firstChild.nodeValue;
		
		if(Err != 0) {
			
			alert(ErrorType);
			
		} else {
			
			var firstTimeQuiz = result.getElementsByTagName('firstTimeQuiz').item(0).firstChild.nodeValue;
			//var secondTimeQuiz = result.getElementsByTagName('secondTimeQuiz').item(0).firstChild.nodeValue;
			var thirdTimeQuiz = result.getElementsByTagName('thirdTimeQuiz').item(0).firstChild.nodeValue;
			var averageScoreFirstTimeQuiz = result.getElementsByTagName('averageScoreFirstTimeQuiz').item(0).firstChild.nodeValue;
			//var averageScoreSecondTimeQuiz = result.getElementsByTagName('averageScoreSecondTimeQuiz').item(0).firstChild.nodeValue;
			var averageScoreThridTimeQuiz = result.getElementsByTagName('averageScoreThirdTimeQuiz').item(0).firstChild.nodeValue;
			var averageScoreTrainingLevel = result.getElementsByTagName('averageScoreTrainingLevel').item(0).firstChild.nodeValue;
			//var averageScoreTrainingLevel2 = result.getElementsByTagName('averageScoreTrainingLevel2').item(0).firstChild.nodeValue;
			var averageScoreTrainingLevel3 = result.getElementsByTagName('averageScoreTrainingLevel3').item(0).firstChild.nodeValue;
			//var averageScoreNumOfExamination = result.getElementsByTagName('averageScoreNumOfExamination').item(0).firstChild.nodeValue;
			//var averageScoreNumOfExamination2 = result.getElementsByTagName('averageScoreNumOfExamination2').item(0).firstChild.nodeValue;
			//var averageScoreNumOfExamination3 = result.getElementsByTagName('averageScoreNumOfExamination3').item(0).firstChild.nodeValue;
			var qnum = result.getElementsByTagName('qnum').item(0).firstChild.nodeValue;
			var score = result.getElementsByTagName('score1').item(0).firstChild.nodeValue;
			//var score2 = result.getElementsByTagName('score2').item(0).firstChild.nodeValue;
			var score3 = result.getElementsByTagName('score3').item(0).firstChild.nodeValue;
			var level = result.getElementsByTagName('training').item(0).firstChild.nodeValue;
			var training = result.getElementsByTagName('TrainingLevel').item(0).childNodes;
			var userNumOfExamination = result.getElementsByTagName('userNumOfExamination').item(0).firstChild.nodeValue;
			var examination = result.getElementsByTagName('ExaminationResult').item(0).childNodes;
				
			var Levels = new Array("Anesthesia resident (PGY1 to PGY3)","Anesthesia resident (PGY 4 or 5)","Anesthesia fellow","Thoracic surgical fellow","Staff anesthesiologist","Staff thoracic surgeon","Other");
			var userLevels = new Array("a","b","c","d","e","f","g");
			var htmlTrainingLevelContent = "<ul>";
			//var htmlTrainingLevel2Content = "<ul>";
			var htmlTrainingLevel3Content = "<ul>";
			
			for (var l = 0; l < training.length; l++) {
			
				var TrainingLevelArray = training[l].getElementsByTagName('averageScoreTrainingLevelArray').item(0).firstChild.nodeValue;
				//var TrainingLevel2Array = training[l].getElementsByTagName('averageScoreTrainingLevel2Array').item(0).firstChild.nodeValue;
				var TrainingLevel3Array = training[l].getElementsByTagName('averageScoreTrainingLevel3Array').item(0).firstChild.nodeValue;
				var NumUsers = training[l].getElementsByTagName('numUsersameLevel').item(0).firstChild.nodeValue;
				//var NumUsers2 = training[l].getElementsByTagName('numUsersameSecondLevel').item(0).firstChild.nodeValue;
				var NumUsers3 = training[l].getElementsByTagName('numUsersameThirdLevel').item(0).firstChild.nodeValue;
				if(level == userLevels[l]) {
					var bOpen = "<b>";
					var bClose = "</b>";
				} else {
					var bOpen = "";
					var bClose = "";
				}
				
				if(TrainingLevelArray > 0) {
					htmlTrainingLevelContent += "<li>" + bOpen + Levels[l] + ": " + Math.round(TrainingLevelArray) + " out of " + qnum + " - " + Math.round((TrainingLevelArray / qnum) * 100) + bClose + "% ("+NumUsers+" respondents)</li>";
				} else {
					htmlTrainingLevelContent += "<li>" + bOpen + Levels[l] + ": N/A" + bClose + "</li>";
				}
				/*if(TrainingLevel2Array > 0) {
					htmlTrainingLevel2Content += "<li>" + bOpen + Levels[l] + ": " + Math.round(TrainingLevel2Array) + " out of " + qnum + " - " + Math.round((TrainingLevel2Array / qnum) * 100) + bClose + "% ("+NumUsers2+" respondents)</li>";
				} else {
					htmlTrainingLevel2Content += "<li>" + bOpen + Levels[l] + ": N/A" + bClose + "</li>";
				}*/
				if(TrainingLevel3Array > 0) {
					htmlTrainingLevel3Content += "<li>" + bOpen + Levels[l] + ": " + Math.round(TrainingLevel3Array) + " out of " + qnum + " - " + Math.round((TrainingLevel3Array / qnum) * 100) + bClose + "% ("+NumUsers3+" respondents)</li>";
				} else {
					htmlTrainingLevel3Content += "<li>" + bOpen + Levels[l] + ": N/A" + bClose + "</li>";
				}
			}
			
			htmlTrainingLevelContent += "</ul>";
			//htmlTrainingLevel2Content += "</ul>";
			htmlTrainingLevel3Content += "</ul>";
			
			
			var htmlContent = "<h3>Results</h3>";
				
				if(score3 > 0) {
					htmlContent += "<p>Congratulations you have successfully completed the bronchial anatomy quiz. Additionally you have gained access the protected articles portion of this website, which you will be able to access with your username and password at any time plus you can now purchase CME credits and certificate <a href=\"http://www.uhn.ca/BSCME09.asp\"  target=\"_blank\">here.</a>  Below you will find an analysis of your quiz performance relative to others.</p>";
				} else {
					htmlContent += "<p>Congratulations you have successfully completed the bronchial anatomy quiz. Additionally you have gained access the protected articles portion of this website, which you will be able to access with your username and password at any time. Below you will find an analysis of your quiz performance relative to others.</p>";
				}
				
				htmlContent += "<p>Thank you for your participation.</p>";
				
				htmlContent += "<h4>On your first attempt you scored " + score + " out of " + qnum;
				if(score3 > 0) {
					htmlContent += ", on your second attempt you socred " + score3 + " out of " + qnum + "!</h4>";
				} 
				
				/*if(score2 > 0) {
					htmlContent += " and on your third attempt you scored " + score2 + " out of " + qnum + "!</h4>";
				} else {
					htmlContent += "!</h4>";
				}*/
				
				htmlContent += "<p>In total there have been "+firstTimeQuiz+" first attempts, "+thirdTimeQuiz+" second quiz attempts.</p>";
				htmlContent += "<p>This is the average score users received on their first attempt:<br />";
				htmlContent += Math.round(averageScoreFirstTimeQuiz) + " out of " + qnum + " - " + Math.round((averageScoreFirstTimeQuiz / qnum) * 100) + "%</p>";
				htmlContent += "<p>This is the average score users received on their second attempt after using the simulator:<br />";
				htmlContent += Math.round(averageScoreThridTimeQuiz) + " out of " + qnum + " - " + Math.round((averageScoreThridTimeQuiz / qnum) * 100) + "%</p>";
				
				/*htmlContent += "<p>This is the average score users received on their third attempt:<br />";
				if(averageScoreSecondTimeQuiz > 0) {
					htmlContent += Math.round(averageScoreSecondTimeQuiz) + " out of " + qnum + " - " + Math.round((averageScoreSecondTimeQuiz / qnum) * 100) + "%</p>";
				} else {
					htmlContent += "N/A</p>";
				}*/
				
				htmlContent += "<hr noshade='noshade' size='1' />";
				htmlContent += "<p>This is the average score users received on their first attempt in relation to their training level:<br />";
				if(averageScoreTrainingLevel > 0) {
					htmlContent += Math.round(averageScoreTrainingLevel) + " out of " + qnum + " - " + Math.round((averageScoreTrainingLevel / qnum) * 100) + "%</p>";
				} else {
					htmlContent += "N/A</p>";
				}
				htmlContent += htmlTrainingLevelContent;
				htmlContent += "<p>This is the average score users received on their second attempt in relation to their training level:<br />";
				if(averageScoreTrainingLevel > 0) {
					htmlContent += Math.round(averageScoreTrainingLevel3) + " out of " + qnum + " - " + Math.round((averageScoreTrainingLevel3 / qnum) * 100) + "%</p>";
				} else {
					htmlContent += "N/A</p>";
				}
				htmlContent += htmlTrainingLevel3Content;
				
				/*htmlContent += "<p>This is the average score users received on their third attempt in relation to their training level:<br />";
				if(averageScoreTrainingLevel2 > 0) {
					htmlContent += Math.round(averageScoreTrainingLevel2) + " out of " + qnum + " - " + Math.round((averageScoreTrainingLevel2 / qnum) * 100) + "%</p>";
				} else {
					htmlContent += "N/A</p>";
				}
				htmlContent += htmlTrainingLevel2Content;*/
				
				
				//Examination List 
				htmlContent += "<hr noshade='noshade' size='1' />";
	
				htmlExaminationContent = "<p>This is the average score users received on their first attempt based on the number of examinations:</p><ul>";
				htmlExamination2Content = "<p>This is the average score users received on their attempt after using the simulator based on the number of examinations:</p><ul>";
				//htmlExamination3Content = "<p>This is the average score users received on their second attempt based on the number of examinations:</p><ul>";
				
				
				for (var m = 0; m < examination.length; m++) {
			
					var ExaminationArray = examination[m].getElementsByTagName('averageScoreNumOfExaminationArray').item(0).firstChild.nodeValue;
					var Examination2Array = examination[m].getElementsByTagName('averageScoreNumOfExamination2Array').item(0).firstChild.nodeValue;
					//var Examination3Array = examination[m].getElementsByTagName('averageScoreNumOfExamination3Array').item(0).firstChild.nodeValue;
					var Examinations = examination[m].getElementsByTagName('ExaminationArray').item(0).firstChild.nodeValue;
					
					if(userNumOfExamination == Examinations) {
						var bOpen = "<b>";
						var bClose = "</b>";
					} else {
						var bOpen = "";
						var bClose = "";
					}
					
					htmlExaminationContent += "<li>" + bOpen + Examinations + " Examinations - "; 
					if(ExaminationArray > 0) {
						htmlExaminationContent += Math.round(ExaminationArray) + " out of " + qnum + " - " + Math.round((ExaminationArray / qnum) * 100) + "%" + bClose + "</li>";
					} else {
						htmlExaminationContent += "N/A" + bClose + "</li>";
					}
					
					htmlExamination2Content += "<li>" + bOpen + Examinations + " Examinations - ";
					if(Examination2Array > 0) {
						htmlExamination2Content += Math.round(Examination2Array) + " out of " + qnum + " - " + Math.round((Examination2Array / qnum) * 100) + "%" + bClose + "</li>";
					} else {
						htmlExamination2Content += "N/A" + bClose + "</li>";
					}
					
					/*htmlExamination3Content += "<li>" + bOpen + Examinations + " Examinations - ";
					if(Examination3Array > 0) {
						htmlExamination3Content += Math.round(Examination3Array) + " out of " + qnum + " - " + Math.round((Examination3Array / qnum) * 100) + "%" + bClose + "</li>";
					} else {
						htmlExamination3Content += "N/A" + bClose + "</li>";
					}
					*/
				}
				
				htmlExaminationContent += "</ul>";
				htmlExamination2Content += "</ul>";
				//htmlExamination3Content += "</ul>";		
				htmlContent += htmlExaminationContent;
				htmlContent += htmlExamination2Content;
				//htmlContent += htmlExamination3Content;
				
				
			document.getElementById('userContainer_div').innerHTML = htmlContent;
			document.getElementById('userContainer_div').style.display = 'block';
			
		}
			
}

function getCertificate(uid) {

	cpCertificate.call('../../../wp-includes/php/lib_getCertificate.php', 'Certificate', handleCertificate, uid);
	//cpCertificate.call('wp-includes/php/lib_getCertificate.php', 'Certificate', handleCertificate, uid);
	
}

function handleCertificate(result) {
	
	
	var Err = result.getElementsByTagName('err').item(0).firstChild.nodeValue;
	var ErrorType = result.getElementsByTagName('errType').item(0).firstChild.nodeValue;
		
	if(Err != 0) {
			
		alert(ErrorType);
			
	} else {
		
		var fname = result.getElementsByTagName('txtFirstName').item(0).firstChild.nodeValue;
		var lname = result.getElementsByTagName('txtLastName').item(0).firstChild.nodeValue;
		var email = result.getElementsByTagName('txtEmail').item(0).firstChild.nodeValue;
		var id = result.getElementsByTagName('id').item(0).firstChild.nodeValue;
		var ComplitionDate = result.getElementsByTagName('dtmQuizComplitionDate').item(0).firstChild.nodeValue;
		
		window.location.href = "http://www.thoracic-anesthesia.com/cert/certificate.php?fname="+fname+"&lname="+lname+"&date="+ComplitionDate+"&email="+email+"&id="+id;
		//window.location.href = "http://www.thoracic-anesthesia.com/dev/certificate.php?fname="+fname+"&lname="+lname+"&date="+ComplitionDate+"&email="+email+"&id="+id;
	}

}

function validateContactForm(e) {
	
	var Valid = true;
	var formName = document.getElementById('txtContactFormName');
	var formEmail = document.getElementById('txtContactFormEmail');
	var formComment = document.getElementById('txtContactFormComment');
	
	if(Valid) {
		if(formName.value == '') {
			alert ( "Please enter your first and last name." );
			formName.select();
			formName.focus();
			Valid = false;
		}
	}
	if(Valid) {
		if(formEmail.value == '') {
			alert ( "Please enter your e-mail address." );
			formEmail.select();
			formEmail.focus();
			Valid = false;
		}
	}
	var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	if(Valid) { // Email regular expression
		if(!re.test(formEmail.value)) {	
			alert ( "Invalid e-mail address! Please re-enter." );
			Valid = false;
			formEmail.select();
			formEmail.focus();
		}
	}
	if(Valid) {
		if(formComment.value == '') {
			alert ( "Please enter your comment." );
			formComment.select();
			formComment.focus();
			Valid = false;
		}
	}
	
	if(Valid) {
		cpEmail.call('../../../wp-includes/php/lib_setEmail.php', 'Email', handleEmail, formName.value, formEmail.value, formComment.value);
		//cpEmail.call('wp-includes/php/lib_setEmail.php', 'Email', handleEmail, formName.value, formEmail.value, formComment.value);
	}
}
	
	
function handleEmail(result) {

	document.getElementById('emailMsg').style.display = "block";
	document.getElementById('txtContactFormName').value = "";
	document.getElementById('txtContactFormEmail').value = "";
	document.getElementById('txtContactFormComment').value=  "";
	
}

function logout() {
	
	cpEmail.call('../../../wp-includes/php/lib_logout.php', 'Logout', handleLogout);
	//cpEmail.call('wp-includes/php/lib_logout.php', 'Logout', handleLogout);
	
}

function handleLogout(result) {
	
	window.location.href = "http://www.thoracic-anesthesia.com/";
	//window.location.href = "http://www.thoracic-anesthesia.com/dev";
	
}

function validateForgotLogin() {
	var Valid = true;
	var formEmail = document.getElementById('txtEmail');
	
	
	if(Valid) {
		if(formEmail.value == '') {
			alert ( "Please enter your e-mail address." );
			formEmail.select();
			formEmail.focus();
			Valid = false;
		}
	}
	var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	if(Valid) { // Email regular expression
		if(!re.test(formEmail.value)) {	
			alert ( "Invalid e-mail address! Please re-enter." );
			Valid = false;
			formEmail.select();
			formEmail.focus();
		}
	}
	
	
	if(Valid) {
		document.getElementById('frmLogin').submit();
	}

}

function validateChangePassword() {
	var Valid = true;
	var password = document.getElementById('txtPassword');
	var confirmpassword = document.getElementById('txtConfirmPassword');
	
	
	if(Valid) {
		if(password.value == '') {
			alert ( "Please enter your password" );
			password.select();
			password.focus();
			Valid = false;
		}
	}
	if(Valid) {
		if(confirmpassword.value == '') {
			alert ( "Please confirm your password" );
			confirmpassword.select();
			confirmpassword.focus();
			Valid = false;
		}
	}
	if(Valid) {
		if(password.value != confirmpassword.value) {
			alert ( "Password does not match! Please confirm your password" );
			confirmpassword.select();
			confirmpassword.focus();
			Valid = false;
		}
	}
	
	
	
	if(Valid) {
		document.getElementById('frmLogin').submit();
	}

}

function getCurrentLanguage() {
	url = window.location.search.substring(1);
	urlArray = url.split("&");
	if(urlArray.length > 0) {
		for (i=0;i < urlArray.length;i++) {
			query = urlArray[i].split("=");
			if (query[0] == 'langswitch_lang') {
				if(query[1] == 'undefined') {
					query[1] = 'en';
				} 
				value = query[1];
			} else {
				value = 'en';
			}
		}
	} else {
		value = 'en';
	}
	return value;
}

