// main.js

function confirmation() {
	var answer = confirm("Are you sure you want to submit the exam?")

	if (answer){
		return true;

	}

	else{
	    return false;
	    
	}
}


// check all the questions are anwered

function radio_button_checker(radioname)
{


// set var radio_choice to false
var radio_choice = false;

// Loop from zero to the one minus the number of radio button selections

var radio_length

eval("radio_length=document.exam."+radioname+".length");


for (counter = 0; counter < radio_length; counter++)
{

	// If a radio button has been selected it will return true
	// (If not it will return false)

	var radioChecked

	eval("radioChecked=document.exam."+radioname+"[counter].checked");


	if (radioChecked)
		  radio_choice = true;
}

if (!radio_choice)

{
	// If there were no selections made display an alert box
	//alert("Please select at least one option.")
	return (false);
}

return (true);

}



// check all the HTML form elementes are either selected or entered

function checkscript() {

var i =0;

 // loop all the elements
	for (i=0;i<document.exam.elements.length;i++) {

		var box = document.exam.elements[i];

	    // check all the options buttons
	if (document.exam.elements[i].type=="radio") {
       // make sure all questions are answered
	   if (radio_button_checker(box.name) == false) {
	   alert ("Please answer to all the questions");
	   box.focus();
	   return false;
	   }
   } // for


	if (!box.value) {
			alert('You haven not answered question: ' + box.name + '!');
			box.focus()
			return false;
		}
	}
	
	
	return confirmation();
}


function submitForm() {
 var topicID = exam.topic.value;
 var levelID = exam.level.value;

if (topicID == "" || levelID == "")  {
	return false;
}
else {
	   return checkscript();
	}

}

// function will display Topic drop down box
function handleHttpResponseTopic() {

  if (http.readyState == 4) {

    // Split the comma delimited response into an array

	  document.getElementById("topic").innerHTML =http.responseText;

     // alert(http.responseText);

    //results = http.responseText.split(",");

   // document.getElementById('city').value = results[0];

   // document.getElementById('state').value = results[1];

  }

}


// function will display Leve drop down box
function handleHttpResponseLevel() {

  if (http.readyState == 4) {
  
  
  

    // Split the comma delimited response into an array

      // on change update level dropdown box and clear the question form
	  document.getElementById("level").innerHTML =http.responseText;
	  document.getElementById("question").innerHTML ="";
      document.getElementById("submitbutton").innerHTML= ""

     // alert(http.responseText);

    //results = http.responseText.split(",");

   // document.getElementById('city').value = results[0];

   // document.getElementById('state').value = results[1];

  }

}



// function will display exam question form
function handleHttpResponseQuestion() {

  if (http.readyState == 4) {
  
  
  

    // Split the comma delimited response into an array

    // alert(http.responseText);

	  document.getElementById("question").innerHTML =http.responseText;

    // show submitbutton
     document.getElementById("submitbutton").innerHTML ="<input type='submit' value='Submit' name='Submit'>";


    //results = http.responseText.split(",");

   // document.getElementById('city').value = results[0];

   // document.getElementById('state').value = results[1];

  }

}


// on Load page fill the topic dropdown box.
function fillTopic() {


var url = "topic.php"; // The server-side script

 // var topicValue = document.getElementById("topic").value;

  http.open("GET", url, true);

  http.onreadystatechange = handleHttpResponseTopic;

  http.send(null);

}


// on Exam topic selection this function will be called.
function updateExamLevel() {

var url = "level.php?topic="; // The server-side script


  //var topicValue = document.getElementById("topic").value;
 var topicID = exam.topic.value;

 if (topicID =="") {


  // if level is default clear the level dropdown and question form , and submitbutton
  document.getElementById("level").innerHTML ="";

  document.getElementById("question").innerHTML ="";
  document.getElementById("submitbutton").innerHTML= ""

  return;

}
  http.open("GET", url + escape(topicID), true);

  http.onreadystatechange = handleHttpResponseLevel;

  http.send(null);

}


// on Exam Level selection this function will be called.
function updateExamQuestion() {

var url = "question.php?"; // The server-side script

   //var topicID = document.getElementById("topic").value;

   var levelID = exam.level.value;
   var topicID = exam.topic.value;

if (levelID =="") {
  // if level is default clear the question form
  document.getElementById("question").innerHTML ="";
  document.getElementById("submitbutton").innerHTML= ""
 return;
}


   http.open("GET", url+"topic="+escape(topicID)+"&level="+escape(levelID), true);

  http.onreadystatechange = handleHttpResponseQuestion;

  http.send(null);

}


// obtain HTTP object

function getHTTPObject() {

  var xmlhttp;

  /*@cc_on

  @if (@_jscript_version >= 5)

    try {

      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

    } catch (e) {

      try {

        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

      } catch (E) {

        xmlhttp = false;

      }

    }

  @else

  xmlhttp = false;

  @end @*/

  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {

    try {

      xmlhttp = new XMLHttpRequest();

    } catch (e) {

      xmlhttp = false;

    }

  }

  return xmlhttp;

}

var http = getHTTPObject(); // We create the HTTP Object

 
