/*
	2008.01.16	gricke
				Corrected bugs
					Check for specific letters or true false before recording cookie - backup if text instead of radio is clicked (which was logging "undefined")
					Hide "This is my final answer" after it's clicked - this diables the ability to log answer multiple times
	NOTES
		Testing
		SetGetCookie('movieQuiz','B|B|D|D|C|FALSE|FALSE|');	//WRONG ANSERS
		SetGetCookie('movieQuiz','B|C|D|C|B|FALSE|TRUE|');	//WRITE ANSWERS
		quizComplete();	
	
		Yes this is my final answer
			bytefx.fade(document.getElementById(showFinBut),100,50,5);	
			*This seems to enable multiple clicking - hide it now instead of trying to disable
*/
var testing = false;
var incorrectColor = '#ff0000';
var quizCookieName = 'movieQuiz';
var incorrectImage = '/images/interface/answerCorrect.png';
var correctImage = '/images/interface/answerInCorrect.png';
var correctIncorrectImgHeight = 28;
var correctIncorrectImgWidth = 301;
var flashWidth = '300';
var flashHeight = '255'; /* this doesn't seem to matter */

function initiateQuiz(){
	var HTMLDisp = '';
	SetGetCookie(quizCookieName,'');
	HTMLDisp = createUniqueId();
	//document.getElementById('testId').innerHTML = HTMLDisp;

	if(testing == true){
		document.getElementById('tempDisplayButton').style.display = 'block';
		document.getElementById('finalQuiz').style.display = 'block';
		document.getElementById('quizSummaryOfResponses').style.display = 'block';
	}
}
function beginQuiz(){
	nextQuestion('0','1');
}
function showFinalButton(id){
	//Yes this is my final answer button
	var showFinBut = 'finalButtonId' + id;
	document.getElementById(showFinBut).style.display = 'block';
	bytefx.alpha(document.getElementById(showFinBut),0);
	bytefx.fade(document.getElementById(showFinBut),0,100,5);
}
function nextQuestion(quesHide,quesShow){
	var hideQues = 'quizItemId' + quesHide;
	var showQues = 'quizItemId' + quesShow;
	var totalQues = document.quizForm.var1.value;
	
	if(parseInt(quesShow) <= parseInt(totalQues)){
		document.getElementById(showQues).style.display = 'block';
	}else{
		quizComplete();
	}
	document.getElementById(hideQues).style.display = 'none';
	if(document.getElementById("quizCount") != null){
		var HTMLDisp = '';
		if(quesShow <= totalQues){
			HTMLDisp = HTMLDisp + quesShow + ' of ' + totalQues;
		}else{
			HTMLDisp = HTMLDisp + 'Congratulations!';
		}
		document.getElementById('quizCount').innerHTML = HTMLDisp;
		document.getElementById('quizCount').style.display = 'block';
	}
}
function chooseAnswer(questionBaseNum,questionCorrect){
	var questionNum = 'qNum' + questionBaseNum;
	var answerNum = 'answerId' + questionBaseNum;
	var videoNum = answerNum.replace(/answerId/,'videoId');
	var answerCorMsgNum = 'answerFullCopyId' + questionBaseNum;
	var showFinBut = 'finalButtonId' + questionBaseNum;
	var nextBar = 'nextBarId' + questionBaseNum;
	var radioQues = eval('document.quizForm.' + questionNum);
	var choosenAnswer;
	var recordChoice;
	var answerIsCorrectMsg = document.getElementById(answerCorMsgNum);
	var answerScoreMsg = document.getElementById('answerScore');
	var HTMLDisp = '';
	var HTMLDisp2 = '';
	
	for (i=radioQues.length-1; i > -1; i--){
		if (radioQues[i].checked) {
			choosenAnswer = radioQues[i].value;
		}
	}
	if((choosenAnswer == 'A' || choosenAnswer == 'B' || choosenAnswer == 'C' || choosenAnswer == 'D' || choosenAnswer == 'TRUE' || choosenAnswer == 'FALSE') && typeof(choosenAnswer) != 'undefined'){    
		if(getCookie(quizCookieName) != null){
			recordChoice = getCookie(quizCookieName) + choosenAnswer + '|'; 
		}else{
			recordChoice = choosenAnswer + '|';
		}
		if(choosenAnswer == questionCorrect){
			HTMLDisp = HTMLDisp + '<img src=\"' + incorrectImage + '\" width=\"' + correctIncorrectImgWidth + '\" height=\"' + correctIncorrectImgHeight + '\" alt=\"Correct\" />';
		}else{
			HTMLDisp = HTMLDisp + '<img src=\"' + correctImage + '\" width=\"' + correctIncorrectImgWidth + '\" height=\"' + correctIncorrectImgHeight + '\" alt=\"InCorrect\" />';
		}

		answerIsCorrectMsg.innerHTML = HTMLDisp;
		document.getElementById(answerNum).style.visibility = 'visible';
		if(document.getElementById(videoNum)){
			document.getElementById(videoNum).style.visibility = 'visible';
		}	
		
		bytefx.alpha(document.getElementById(answerNum),0);
		bytefx.fade(document.getElementById(answerNum),0,100,5);
		
		/* ~~~~~~~~~~~~~~~~~[record to cookie] ~~~~~~~~~~ */
		SetGetCookie(quizCookieName,recordChoice);
		answerScoreMsg.innerHTML = getCookie(quizCookieName);
	
		/* ~~~~~~~~~~~~~~~~~[disalble but and selections] ~~~~~~~~~~ */
		for (i=radioQues.length-1; i > -1; i--){
			radioQues[i].disabled = true;
		}
		/* ~~~~~~~~~~~~~~~~~[next button] ~~~~~~~~~~ */
		document.getElementById(nextBar).style.display = 'block';
		bytefx.alpha(document.getElementById(nextBar),0);
		bytefx.fade(document.getElementById(nextBar),0,100,5);
		document.getElementById(showFinBut).style.display = 'none'; //Yes my final answer
		return false;		
	}
}
function quizComplete(){
	var quizAnswers = getCookie(quizCookieName);
	var answerScoreMsg = document.getElementById('answerScore');
	var HTMLDisp = '';
	var quizCorrectAnswers = document.quizForm.var2.value;
	var correctAnswers = 0;
	var quizAnswersArray = quizCorrectAnswers.split("|");
	var quizSelectedArray = quizAnswers.split("|");
	var quizId = 0;

	for(z=0;z<=quizAnswersArray.length-2;z=z+1){
		quizId = z + 1;
		var incorrect = 'final' + quizId;
		if(quizSelectedArray[z] == quizAnswersArray[z]){
			correctAnswers = correctAnswers + 1;
		}else{
			//mark incorrect
			document.getElementById(incorrect).style.color = incorrectColor;
		}
	}
	
	document.getElementById('answerScore').style.display = 'block';
	HTMLDisp = HTMLDisp + "Your Score: " +  + correctAnswers + ' of ' + (parseInt(quizAnswersArray.length) - 1) + '<br />';
	HTMLDisp = HTMLDisp + Math.round((parseInt(correctAnswers)/(parseInt(quizAnswersArray.length) - 1))*100) + '% <br />';
	
	answerScoreMsg.innerHTML = HTMLDisp;
	
	document.getElementById('finalQuiz').style.display = 'block';
	document.getElementById('quizSummaryOfResponses').style.display = 'block';
	callAjax(quizAnswers,quizCorrectAnswers);
}
function createUniqueId(){
	var dateID = new Date()
	var idDate = dateID.getTime()
	return idDate; 
}
/* ~~~~~~~~~~~~~~~~~~~~~~ [ AJAX ] ~~~~~~~~~~~~~~~~~~~~~~~ */
var dataUrl = '/include/processQuizResults.asp';
var ajaxResultsDivId = 'httpReqResults';

function callAjax(quizAnswers,quizCorrectAnswers){
	dataUrl = dataUrl + '?quizAnswers=' + quizAnswers + '&quizCorrectAnswers=' + quizCorrectAnswers;
	sendData(dataUrl)
}
function sendData(strURL) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            returnHttpReqResults(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send('asdf');	//must send something or get "length required" in FF
}
function returnHttpReqResults(results){
	var HTMLDisp = '';
	if (document.getElementById(ajaxResultsDivId)){
		var httpReqResultsHtml = document.getElementById(ajaxResultsDivId);
		HTMLDisp = HTMLDisp + results;
		httpReqResultsHtml.innerHTML = HTMLDisp; 
	}
}
/* ~~~~~~~~~~~~~~~~~~~~~~ [  ] ~~~~~~~~~~~~~~~~~~~~~~~ */

/* ~~~~~~~~~~~~~~~~~~~~~~ [ from Javascripto2/formProcess.js ] ~~~~~~~~~~~~~~~~~~~~~~~ */
function formvalidationCustom(thisform)
{
	var string = "";
	var els = document.forms[thisform].elements;
	for(var no=0;no<els.length;no++){
	    string+="Name: "+els[no].name + ", Value: "+els[no].value + "\n";
	}
	//Variables set from custom file: FormValidationVars.js
	var ValidateVarsArray = ValidateVars.split('|');
	with (thisform)
	{
		for (var i=0;i<ValidateVarsArray.length-1;i++) {
			
			var ValidateVarPieces = ValidateVarsArray[i].split('=');
			if(typeof(eval('document.' + thisform + '.' + ValidateVarPieces[1])) != 'undefined'){			
				if(eval(ValidateVarPieces[0] + '(document.' + thisform + '.' + ValidateVarPieces[1] + ',"' + ValidateVarPieces[2] + '")')==false){
					eval('document.' + thisform + '.' + ValidateVarPieces[1]).focus();
					return false;
				}else{
					//Everything is good 
				}
			}else{
				alert('FormValidationVars ERROR: \r Trying to validate >>>' + ValidateVarPieces[1] + '<<< which is not in this form \r \r Check that the validation var is spelled correctly  \r Or \r Remove the validation check \r Or \r Add the form element that is missing');
			}
		}	
	}
	
	GetAllInputs(thisform);
	ProcessInputs();
	setTimeout('beginQuiz()',1000);
}
