// JavaScript Document


var xmlHttp_IsEmailRegistered;
//checks the answer of the given question against the selected answer
function isEmailRegistered(email, theDiv){	
	
	xmlHttp_IsEmailRegistered=GetXmlHttpObject();
	if (xmlHttp_IsEmailRegistered==null){
		alert ("Browser does not support HTTP Request")
		return
	} 
	
	document.getElementById(theDiv).innerHTML = "Validating E-Mail...";
		
	var url="includes/xmlHttp.php?action=is_email_registered&email=" + email;
	url=url+"&sid="+Math.random();
	xmlHttp_IsEmailRegistered.onreadystatechange=function(){
		
		if (xmlHttp_IsEmailRegistered.readyState==4 || xmlHttp_IsEmailRegistered.readyState=="complete"){ 
			var tDiv = document.getElementById(theDiv);
			if(xmlHttp_IsEmailRegistered.responseText.replace(/ /gi,'')==1){
				tDiv.innerHTML = '<br /><span	style="color:#FF0000; font-weight:bold;">Sorry, but this e-mail address is already registered!</span>';
			}else{
				tDiv.innerHTML = '<img src="images/checkMark.gif" />';
			}
	}	
		
	}
	
	xmlHttp_IsEmailRegistered.open("GET",url,true);
	xmlHttp_IsEmailRegistered.send(null);	
}
	




function selectAnswer(ans){
	var selAns = document.getElementById('selected_answer');
	selAns.value = ans;
}


var xmlHttp_CheckAnswer;
//checks the answer of the given question against the selected answer
function checkAnswer(q_id, subject, users_id, start_time){	
	
	xmlHttp_CheckAnswer=GetXmlHttpObject();
	if (xmlHttp_CheckAnswer==null){
		alert ("Browser does not support HTTP Request")
		return
	} 
	var ansEle = document.getElementById('selected_answer');
	var selAns = ansEle.value;
	
	if(selAns == ""){
		return;	
	}
		
	var theBtn = document.getElementById('checkBtn');
	theBtn.value = "Checking answer..";
	theBtn.disabled = "disabled";
		
	var url="includes/xmlHttp.php?action=check_answer&q_id="+q_id+"&subject="+subject+"&chosen_choice="+selAns+"&users_id="+users_id+"&start_time="+start_time;
	url=url+"&sid="+Math.random();
	xmlHttp_CheckAnswer.onreadystatechange=function(){
		if (xmlHttp_CheckAnswer.readyState==4 || xmlHttp_CheckAnswer.readyState=="complete"){ 
			var theResult = xmlHttp_CheckAnswer.responseText;
			theResult = theResult.replace(" ","");
			
			if(theResult == "error"){
				alert("Sorry, an error occured while checking the answer.  Please try again.");
				return;
			}else if(theResult == 1){
				alert("Correct!  Please move on to the next question.");
				if(navigator.appName == "Microsoft Internet Explorer"){
					var btnTD = document.getElementById('btnTD');
					var checkBtn = document.getElementById('checkBtn');
					btnTD.removeChild(checkBtn);//removes the 'Check Answer' button
					
					var nextBtn = document.createElement("<input type='submit' name='nextBtn' id='nextBtn' value='Next Question =>'>")
					btnTD.appendChild(nextBtn);//add the 'Next Question' button
				}else{
					var checkBtn = document.getElementById('checkBtn');
					checkBtn.setAttribute('type','hidden');
					
					var nextBtn = document.getElementById('nextBtn');
					nextBtn.setAttribute('type','submit');/* enable the Next button */
				}
			}else{
				alert("Sorry, your answer is incorrect.  Please try again.");			
				document.getElementById('checkBtn').disabled = false;
				document.getElementById('checkBtn').value = 'Check Answer';
				var hasHint = document.getElementById('hasHint');
				if(hasHint.value == 1){
					hintBtn = document.getElementById('hintBtn');
					hintBtn.setAttribute('type', 'button');
				}
			}	
		}
	};
	
	xmlHttp_CheckAnswer.open("GET",url,true);
	xmlHttp_CheckAnswer.send(null);	
}



var xmlHttp_loadSelectNames;
//loads the names from given classroom
function loadSelectNames(classrooms_id){
		
	xmlHttp_loadSelectNames=GetXmlHttpObject();
	if (xmlHttp_loadSelectNames==null){
		alert ("Browser does not support HTTP Request")
		return
	} 
	var url="includes/xmlHttp.php?action=get_students&classrooms_id="+classrooms_id;
	url=url+"&sid="+Math.random();
	xmlHttp_loadSelectNames.onreadystatechange=stateChanged_loadSelectNames;
	xmlHttp_loadSelectNames.open("GET",url,true);
	xmlHttp_loadSelectNames.send(null);	
}
	
function stateChanged_loadSelectNames(){ 
	var selNames = document.getElementById('selectNames');
	selNames.innerHTML = "<img src='images/loading.gif' border='0' />";
	
	if (xmlHttp_loadSelectNames.readyState==4 || xmlHttp_loadSelectNames.readyState=="complete"){ 
		selNames.innerHTML = xmlHttp_loadSelectNames.responseText;
		//modelField.options[0] = new Option("Loading...", "");
	} 
}




var xmlHttp_loadSelectClassrooms;
//loads the names from given classroom
function loadSelectClassrooms(schools_id){
		
	xmlHttp_loadSelectClassrooms=GetXmlHttpObject();
	if (xmlHttp_loadSelectClassrooms==null){
		alert ("Browser does not support HTTP Request")
		return
	} 
	var url="includes/xmlHttp.php?action=get_classrooms&schools_id="+schools_id;
	url=url+"&sid="+Math.random();
	xmlHttp_loadSelectClassrooms.onreadystatechange=stateChanged_loadSelectClassrooms;
	xmlHttp_loadSelectClassrooms.open("GET",url,true);
	xmlHttp_loadSelectClassrooms.send(null);	
}
	
function stateChanged_loadSelectClassrooms(){ 
	var selClassrooms = document.getElementById('selectClassrooms');
	selClassrooms.innerHTML = "<img src='images/loading.gif' border='0' />";
	
	if (xmlHttp_loadSelectClassrooms.readyState==4 || xmlHttp_loadSelectClassrooms.readyState=="complete"){ 
		selClassrooms.innerHTML = xmlHttp_loadSelectClassrooms.responseText;
		//modelField.options[0] = new Option("Loading...", "");
		selNames = document.getElementById('users_email');
		selNames.options[0].innerHTML = "Select Your Teacher";
	} 
}



var xmlHttp_loadSelectStandards;
//loads the standards by the given subject
function loadSelectStandards(subject,selected_standard,grade){
		
	xmlHttp_loadSelectStandards=GetXmlHttpObject();
	if (xmlHttp_loadSelectStandards==null){
		alert ("Browser does not support HTTP Request")
		return
	} 
	var url="includes/xmlHttp.php?action=get_standards&subject="+subject+"&selected_standard="+selected_standard+"&grade="+grade;
	url=url+"&sid="+Math.random();
	xmlHttp_loadSelectStandards.onreadystatechange=stateChanged_loadSelectStandards;
	xmlHttp_loadSelectStandards.open("GET",url,true);
	xmlHttp_loadSelectStandards.send(null);	
}
	
function stateChanged_loadSelectStandards(){ 
	var selStandards= document.getElementById('selectStandards');
	selStandards.innerHTML = "<img src='images/loading.gif' border='0' />";
	
	if (xmlHttp_loadSelectStandards.readyState==4 || xmlHttp_loadSelectStandards.readyState=="complete"){ 
		selStandards.innerHTML = xmlHttp_loadSelectStandards.responseText;
		//modelField.options[0] = new Option("Loading...", "");
	} 
}







var xmlHttp_loadTestReport;
//checks the answer of the given question against the selected answer
function loadTestReport(tests_id, reportDiv, oDiv){	
	if(tests_id < 1){
		alert('Sorry, but a test was not selected!');
		return;
	}
	
	xmlHttp_loadTestReport=GetXmlHttpObject();
	if (xmlHttp_loadTestReport==null){
		alert ("Browser does not support HTTP Request")
		return;
	}
		
	var url="includes/xmlHttp.php?action=get_test_report&tests_id=" + tests_id;
	url=url+"&sid="+Math.random();
	xmlHttp_loadTestReport.onreadystatechange=function(){
		if (xmlHttp_loadTestReport.readyState==4 || xmlHttp_loadTestReport.readyState=="complete"){ 
			document.getElementById(reportDiv).innerHTML = xmlHttp_loadTestReport.responseText;
			overlayDiv(this, oDiv);
		}
		
	}
	xmlHttp_loadTestReport.open("GET",url,true);
	xmlHttp_loadTestReport.send(null);	
}




var xmlHttp_loadClassroomTestDetails;
//loads the standards by the given subject
function loadClassroomTestDetails(tests_id){
	if(tests_id < 1){
		return;	
	}
	xmlHttp_loadClassroomTestDetails=GetXmlHttpObject();
	if (xmlHttp_loadClassroomTestDetails==null){
		alert ("Browser does not support HTTP Request");
		return;
	} 
	var url="includes/xmlHttp.php?action=get_class_test_details&tests_id="+tests_id;
	url=url+"&sid="+Math.random();
	xmlHttp_loadClassroomTestDetails.onreadystatechange=stateChanged_loadClassroomTestDetails;
	xmlHttp_loadClassroomTestDetails.open("GET",url,true);
	xmlHttp_loadClassroomTestDetails.send(null);	
}
	
function stateChanged_loadClassroomTestDetails(){ 
	var testDiv= document.getElementById('test_details');
	testDiv.innerHTML = "<img src='images/loading.gif' border='0' />";
	
	if (xmlHttp_loadClassroomTestDetails.readyState==4 || xmlHttp_loadClassroomTestDetails.readyState=="complete"){ 
		testDiv.innerHTML = xmlHttp_loadClassroomTestDetails.responseText;
		//modelField.options[0] = new Option("Loading...", "");
	} 
}


var xmlHttp_loadAnalyzeQuestion;
//loads the standards by the given subject
function loadAnalyzeQuestion(q_id, subject, type /*practice or test*/, classrooms_id, users_id){
	if(q_id < 1){
		alert("Sorry, but a question was not selected!");	
	}
	
	if(subject==''){
		alert("Sorry, but a subject was not indicated!");
	}
	
	//alert(q_id + " : " + subject + " : " + classrooms_id);
	xmlHttp_loadAnalyzeQuestion=GetXmlHttpObject();
	if (xmlHttp_loadAnalyzeQuestion==null){
		alert ("Browser does not support HTTP Request");
		return;
	} 
	var url="includes/xmlHttp.php?action=get_analyze_question&q_id="+q_id+"&subject="+subject+"&type="+type+"&classrooms_id="+classrooms_id+"&users_id="+users_id;
	url=url+"&sid="+Math.random();
	xmlHttp_loadAnalyzeQuestion.onreadystatechange=stateChanged_loadAnalyzeQuestion;
	xmlHttp_loadAnalyzeQuestion.open("GET",url,true);
	xmlHttp_loadAnalyzeQuestion.send(null);	
}
	
function stateChanged_loadAnalyzeQuestion(){ 
	var qDiv= document.getElementById('divAnalyzeQuestion');
	qDiv.innerHTML = "<img src='images/loading.gif' border='0' />";
	
	if (xmlHttp_loadAnalyzeQuestion.readyState==4 || xmlHttp_loadAnalyzeQuestion.readyState=="complete"){ 
		qDiv.innerHTML = xmlHttp_loadAnalyzeQuestion.responseText;
		//modelField.options[0] = new Option("Loading...", "");
	} 
}




var xmlHttp_loadAnalyzeTestQuestions;
//loads the standards by the given subject
function loadAnalyzeTestQuestions(tests_id){
	if(tests_id < 1){
		return;	
	}
	//alert(q_id + " : " + subject + " : " + classrooms_id);
	xmlHttp_loadAnalyzeTestQuestions=GetXmlHttpObject();
	if (xmlHttp_loadAnalyzeTestQuestions==null){
		alert ("Browser does not support HTTP Request");
		return;
	} 
	var url="includes/xmlHttp.php?action=get_classroom_test_questions&tests_id="+tests_id;
	url=url+"&sid="+Math.random();
	xmlHttp_loadAnalyzeTestQuestions.onreadystatechange=stateChanged_loadAnalyzeTestQuestions;
	xmlHttp_loadAnalyzeTestQuestions.open("GET",url,true);
	xmlHttp_loadAnalyzeTestQuestions.send(null);	
}
	
function stateChanged_loadAnalyzeTestQuestions(){ 
	var qDiv= document.getElementById('divTestQuestions');
	qDiv.innerHTML = "<img src='images/loading.gif' border='0' />";
	
	if (xmlHttp_loadAnalyzeTestQuestions.readyState==4 || xmlHttp_loadAnalyzeTestQuestions.readyState=="complete"){ 
		qDiv.innerHTML = xmlHttp_loadAnalyzeTestQuestions.responseText;
		//modelField.options[0] = new Option("Loading...", "");
	} 
}



var xmlHttp_loadAnalyzeTestQuestion;
//loads the standards by the given subject
function loadAnalyzeTestQuestion(tests_id, q_id){
	if(tests_id < 1 || q_id < 1){
		return;	
	}
	//alert(q_id + " : " + subject + " : " + classrooms_id);
	xmlHttp_loadAnalyzeTestQuestion=GetXmlHttpObject();
	if (xmlHttp_loadAnalyzeTestQuestion==null){
		alert ("Browser does not support HTTP Request");
		return;
	} 
	var url="includes/xmlHttp.php?action=get_analyze_question&tests_id="+tests_id+"&q_id="+q_id;
	url=url+"&sid="+Math.random();
	xmlHttp_loadAnalyzeTestQuestion.onreadystatechange=stateChanged_loadAnalyzeTestQuestion;
	xmlHttp_loadAnalyzeTestQuestion.open("GET",url,true);
	xmlHttp_loadAnalyzeTestQuestion.send(null);	
}
	
function stateChanged_loadAnalyzeTestQuestion(){ 
	var qDiv= document.getElementById('divAnalyzeTestQuestion');
	qDiv.innerHTML = "<img src='images/loading.gif' border='0' />";
	
	if (xmlHttp_loadAnalyzeTestQuestion.readyState==4 || xmlHttp_loadAnalyzeTestQuestion.readyState=="complete"){ 
		qDiv.innerHTML = xmlHttp_loadAnalyzeTestQuestion.responseText;
		//modelField.options[0] = new Option("Loading...", "");
	} 
}





var xmlHttp_loadQuestionsByStandard;
//checks the answer of the given question against the selected answer
function loadQuestionsByStandard(grade, subject, standard, theDiv, showAdd){	
	
	xmlHttp_loadQuestionsByStandard=GetXmlHttpObject();
	
	if (xmlHttp_loadQuestionsByStandard==null){
		alert ("Browser does not support HTTP Request")
		return;
	}
	
	if(grade < 1){
		alert("Sorry, but a grade was not selected!");
		return;
	}else if(subject == ''){
		alert("Sorry, but a subject was not selected!");
		return;
	}else if(standard < 1){
		alert("Sorry, but a standard was not selected!");
		return;
	}
	
	var url="includes/xmlHttp.php?action=get_questions_by_standard&grade="+grade+"&subject="+subject+"&standard="+standard+"&show_add="+showAdd;
	url=url+"&sid="+Math.random();
	xmlHttp_loadQuestionsByStandard.onreadystatechange=function(){
		var qDiv= document.getElementById(theDiv);
		qDiv.innerHTML = "<img src='images/loading.gif' border='0' />";
		
		if (xmlHttp_loadQuestionsByStandard.readyState==4 || xmlHttp_loadQuestionsByStandard.readyState=="complete"){ 
			qDiv.innerHTML = xmlHttp_loadQuestionsByStandard.responseText;
			//modelField.options[0] = new Option("Loading...", "");
		} 
		
	}
	
	xmlHttp_loadQuestionsByStandard.open("GET",url,true);
	xmlHttp_loadQuestionsByStandard.send(null);	
}





function GetXmlHttpObject(){ 
	var objXMLHttp=null;
	if (window.XMLHttpRequest){
		objXMLHttp=new XMLHttpRequest();
	}else if (window.ActiveXObject){
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp;
}