/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: James Crooke :: http://www.cj-design.com */

var useranswers = new Array();
var answered = 0;
var optCorrect = new Array();
var arrayOfParameters = new Array();
var nameOfExaminee = "";
var idOfExaminee = "";
var emailIdOfExaminee = "";

function renderQuiz() {
  for(i=0;i<questions.length;i++) {
    document.writeln('<br><p class="question">' + questions[i] + ' <span id="result_' + i + '"><img src="images/blank.gif" style="border:0" alt="" /></span></p>');
    for(j=0;j<choices[i].length;j++) { 
      if (selectOption[i]=="R")
      {
		  document.writeln('<input type="radio" name="answer_' + i + '" value="' + choices[i][j] + '" id="answer_' + i + '_' + j + '" class="question_' + i + '" onclick="submitAnswer(' + i + ', this, \'question_' + i + '\', \'label_' + i + '_' + j + '\', \'result1_' + i + '_' + j + '\', \'answer_' + i + '_' + j + '\')" /><label id="label_' + i + '_' + j + '" for="answer_' + i + '_' + j + '"> ' + choices[i][j] + '</label><span id="result1_' + i + '_' + j + '"><img src="images/blank.gif" style="border:0" alt="" /></span><br/>');
      }else{
		  document.writeln('<input type="checkbox" name="answer_' + i + '" value="' + choices[i][j] + '" id="answer_' + i + '_' + j + '" class="question_' + i + '" onclick="submitAnswer(' + i + ', this, \'question_' + i + '\', \'label_' + i + '_' + j + '\', \'result1_' + i + '_' + j + '\', \'answer_' + i + '_' + j + '\')" /><label id="label_' + i + '_' + j + '" for="answer_' + i + '_' + j + '"> ' + choices[i][j] + '</label><span id="result1_' + i + '_' + j + '"><img src="images/blank.gif" style="border:0" alt="" /></span><br/>');
 	  }
    }
  }
  //document.writeln('<input type="button" value="Submit Test" onclick="certificate()" />&nbsp;&nbsp;&nbsp;<input type="button" value="Try Again" onclick="reset()" /></p>');
}
function resetQuiz(showConfirm) {
  if(showConfirm)
    if(!confirm("Are you sure you want to reset your answers and start from the beginning?"))
      return false;
  document.location = document.location;
  //document.getElementById('loginTable').style.display = 'none';
  //document.getElementById('questions').style.display = '';
}
function submitAnswer(questionId, obj, classId, labelId, result1, optionId ) {
 
  useranswers[questionId] = obj.value;
  //document.getElementById(labelId).style.fontWeight = "normal";

  showResult(questionId, classId, labelId, result1, optionId);
  answered++;	
  attempted[questionId] = 1;
}
function showResult(questionId, classId, labelId, result1, optionId) {
  
	if(selectOption[questionId] == "R"){
	  if(answers[questionId] == useranswers[questionId]) {
		document.getElementById(result1).innerHTML = '';
		
	  } else {
		
		
		/*if(chances[questionId] < 1){
		  disableQuestion(classId);
		}else{
			chances[questionId] = chances[questionId] - 1;
		}*/
		
		document.getElementById(result1).innerHTML = '';
		
	  }
	  countForOptions[questionId] = countForOptions[questionId] + 1;
  }else{
		
		var isAvailable = true;
		for(i = 0 ; i < answers[questionId].length ; i ++){

			if(answers[questionId][i] == useranswers[questionId]) {
				isAvailable= true;
				break;
			}else{
				isAvailable= false;
			}
		}
		
		if( isAvailable == true){
			document.getElementById(result1).innerHTML = '<img src="images/correct.gif" style="border:0" alt="Correct!" />';
			countForOptions[questionId] = countForOptions[questionId] + 1;
		} else {
						
			/*if(chances[questionId] < 1){
			//alert(chances[questionId]);
			  disableQuestion(classId);
			}else{
				chances[questionId] = chances[questionId] - 1;
			}*/
			
			document.getElementById(optionId).checked = false;
			document.getElementById(result1).innerHTML = '<img src="images/incorrect.gif" style="border:0" alt="Incorrect!" />';
			
		  }
	  }
    
}
function showScore() {
  if(answered < 1){//!= answers.length) {
    alert("You have not answered any of the questions yet!");
    return false;
  }
  for(i = 0 ; i < attempted.length ; i ++){
	if(attempted[i] == 0){
		alert('You have not answered all of the questions');	
		return false;
	}
  }
  correct = 0;
  incorrect = 0;
	for(i = 0 ; i < questions.length ; i ++){
		
		if(selectOption[i] == "R"){
			if(useranswers[i] == answers[i]){
				correct++;
			}else{
				incorrect++;
			}
		}else{
			if(countForOptions[i] == answers[i].length){
				correct++;
			}else{
				incorrect++;
			}
		}
		
	}




  questionCount = questions.length;//answers.length;
  /*
  for(i=0;i<questionCount;i++) {
    if(useranswers[i] == answers[i])
      correct++;
    else
      incorrect++;
  }*/
  pc = Math.round((correct / questionCount) * 100);
  alertMsg = "You scored " + correct + " out of " + questionCount + "\n\n";
  alertMsg += "You correctly answered " + pc + "% of the questions! \n\n";
  /*if(pc == 100)
    alertMsg += response[0];
  else if(pc >= 90)
    alertMsg += response[1];
  else if(pc >= 70)
    alertMsg += response[2];
  else if(pc > 50)
    alertMsg += response[3];
  else if(pc >= 40)
    alertMsg += response[4];
  else if(pc >= 20)
    alertMsg += response[5];
  else if(pc >= 10)
    alertMsg += response[6];
  else
    alertMsg += response[7];*/
	
  
    alert(alertMsg);
  
}

function disableQuestion(classId) {
  var alltags=document.all? document.all : document.getElementsByTagName("*")
  for (i=0; i<alltags.length; i++) {
    if (alltags[i].className == classId) {
      alltags[i].disabled = true;
    }
  }
}

function mailResult(){

if(answered < 1){//!= answers.length) {
    alert("You have not answered any of the questions yet!");
	return false;
  }
  for(i = 0 ; i < attempted.length ; i ++){
	if(attempted[i] == 0){
		alert('You have not answered all of the questions');	
		return false;
		
	}
  }
  correct = 0;
  incorrect = 0;
	for(i = 0 ; i < questions.length ; i ++){
		
		if(selectOption[i] == "R"){
			if(useranswers[i] == answers[i]){
				correct++;
			}else{
				incorrect++;
			}
		}else{
			if(countForOptions[i] == answers[i].length){
				correct++;
			}else{
				incorrect++;
			}
		}
		
	}


  questionCount = questions.length;
  pc = Math.round((correct / questionCount) * 100);
  	
location.href='mailto:sreeprada@globusfamily.in?Subject=Online Test : ' + correct.toString(2) + ' : ' + correct + '/' + questions.length + ' &body=\%0ANumber of Questions : ' + questions.length + '\%0ACorrect Answers : ' + correct + '\%0AIncorrect Answers : ' + incorrect + '\%0APercentage : ' + pc + '%' ;
}


function validateLogin(){
  
		document.getElementById('questions').style.display = '';
		document.getElementById('loginTable').style.display = 'none';
		document.getElementById('certificate').style.display = 'none';
	   //window.location="quiz.htm";
	  
   
}

function initElement(methodRef, name){
    
    document.getElementsByName(name)[0].onkeydown = methodRef;
    //alert(methodRef);
    //document.onkeydown = methodRef;
}

function isNumericKeyStroke(e) {
    var returnValue = false;
    if(!e)
		e = event;
    var keyCode = (e.which) ? e.which : e.keyCode;
    
    if (((keyCode >= 48) && (keyCode <= 57)) || // All numerics
    ((keyCode >= 96) && (keyCode <= 105)) || // all Num Lock pad Numaricc 			
    ((keyCode >= 33) && (keyCode <= 40)) || // all Num Lock pad Numaricc 								
    (keyCode ==  8) ||     // Backspace 
    (keyCode ==  9) ||    // Tab Button
    (keyCode ==  46) ||   // Delete Button		
    (keyCode == 13) )     // Carriage Return
        returnValue = true;
    
    if ( e.returnValue )
        e.returnValue = returnValue;
    
    //alert(returnValue);
    return returnValue;
}

function isCharacterKeyStroke(e) {
    var returnValue = false;
        
    var keyCode = (e.which) ? e.which : e.keyCode;
 
    if ( ((keyCode >= 65) && (keyCode <= 90)) || 
    //((keyCode >= 97) && (keyCode <= 1)) ||
    ((keyCode >= 33) && (keyCode <= 40)) ||
    (keyCode ==  8) ||    // Backspace      
    (keyCode ==  9) ||    // Tab Button
    (keyCode ==  46)||    // Delete Button							
    (keyCode ==  37)||   
    (keyCode ==  32) ||    // Space
    (keyCode ==  188) ||    // Space
    (keyCode ==  39) )   
        returnValue = true;
    if ( e.returnValue )
        e.returnValue = returnValue;
    return returnValue;
}


function emailCheck(control) {
    var at="@";
    var dot=".";
    var msg = "Invalid E-mail ID." 
    var str = document.getElementById(control).value;
	if(str == ""){
		alert('E-mail ID field cannot be blank');
		document.getElementById(control).focus();
		return false;
	}
    var lat=str.indexOf(at);
    var lstr=str.length;
    var ldot=str.indexOf(dot);
    
    if (str.indexOf(at)==-1){
        alert(msg);
		document.getElementById(control).focus();
        return false;
    }
    
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
        alert(msg);
		document.getElementById(control).focus();
        return false;
    }
    
    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        alert(msg);
		document.getElementById(control).focus();
        return false;
    }
    
    if (str.indexOf(at,(lat+1))!=-1){
        alert(msg);
		document.getElementById(control).focus();
        return false;
    }
    
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        alert(msg);
		document.getElementById(control).focus();
        return false;
    }
    
    if (str.indexOf(dot,(lat+2))==-1){
        alert(msg);
		document.getElementById(control).focus();
        return false;
    }
    
    if (str.indexOf(" ")!=-1){
        alert(msg);
		document.getElementById(control).focus();
        return false;
    }
    
    return true;			
}

function showOkButton(){
	if(document.getElementById('chkAgreement').checked == true){
		document.getElementById('showOkButton').style.display = '';
	}else{
		document.getElementById('showOkButton').style.display = 'none';
	}
}

function certificate(){
	if(answered < 1){//!= answers.length) {
		alert("You have not answered any question yet");
		return false;
	}
	for(i = 0 ; i < attempted.length ; i ++){
		if(attempted[i] == 0){
			alert('You have not answered all the questions');	
			return false;
		}
	}
	correct = 0;
  incorrect = 0;
	for(i = 0 ; i < questions.length ; i ++){
		
		if(selectOption[i] == "R"){
			if(useranswers[i] == answers[i]){
				correct++;
			}else{
				incorrect++;
			}
		}else{
			if(countForOptions[i] == answers[i].length){
				correct++;
			}else{
				incorrect++;
			}
		}
		
	}


  questionCount = questions.length;
  pc = Math.round((correct / questionCount) * 100);
  var valAnswer ="";
  //document.getElementById('hidAnswers').value=valAnswer;
  
  if(questionCount == correct){
    alert('You have answered all questions correctly');   
    
    document.getElementById('hidAnswers').value = correct;
    
    //alert(document.getElementById('hidAnswers').value);
    
    //window.open ('congrats.aspx?correct=' + correct,"mywindow");
    window.location ='congrats.aspx?correct=' + correct;
  }else{
  //alert('u have answered ' + correct + ' questions correctly ');
    alert('Sorry You have answered ' + incorrect + ' questions incorrect. Try Again! ');
  }
  //if(pc < 80){
	//alert('You need to score 80% to be entitled to a Certificate');  
  //}
  //if()
  //{
	 var currentDate = new Date();
	 var date = currentDate.getDate();
	 var month = currentDate.getMonth() + 1;
	 var year = currentDate.getFullYear();
	 
  	document.getElementById('certificate').style.display = '';
	//document.getElementById('questions').style.display = 'none';
	//document.getElementById('top').style.display = 'none';
	/*if(document.all){
		document.getElementById('nameOfExaminee').innerText = nameOfExaminee;
		document.getElementById('percentageAndDate').innerText = pc + '% on ' + date + '/' + month + '/' + year;
	}else{
		document.getElementById('nameOfExaminee').textContent = nameOfExaminee;
		document.getElementById('percentageAndDate').textContent = pc + '% on ' + date + '/' + month + '/' + year;
	}*/
  //}
}

function reset(){
	//alert('hiiii');
	//renderQuiz();
	if(confirm('Are you sure you want to reset the test ?')){
		//alert('Reseted the test');
		for(i = 0 ; i < questions.length ; i ++){
	
			for (j = 0 ; j < choices[i].length ; j ++){
			
				document.getElementById('answer_' + i + '_' + j ).checked = false;
				document.getElementById('answer_' + i + '_' + j ).disabled = false;
				document.getElementById('result1_' + i + '_' + j ).innerHTML = '<img src="images/blank.gif" style="border:0" alt="Correct!" />';

			}
		
			chances[i] = 0;
		
			
			countForOptions[i] = 0;
			attempted[i] = 0;
			useranswers[i] = 0;

		}
	
		answered = 0;
	}
}
