// BEGIN CHECK FOR PHP SESSION ** DO NOT REMOVE
// This check prevents javascript for grading incorrectly when a php session is used.
phpSession = 0;

// Call this function using onLoad for any quiz that uses phpSessions
function phpSessionFix() {
	for (phpCheck=0;phpCheck<document.quizEngine.elements.length;phpCheck++) {
	   	if (document.quizEngine.elements[phpCheck].name == 'PHPSESSID') {
			phpSession = 1;
		} // END IF
	} // END FOR
} // END phpSession

// END CHECK FOR PHP SESSION ** DO NOT REMOVE 
	
function isValidQuiz() {
        
		// Second fix for php session ** DO NOT REMOVE
		if (phpSession == 0) {
			startValue = 0;
		} else {
			startValue = 1;
		} // END IF
		
		var i
        var radioName='new';
        //var oldName=document.quizEngine.elements[0].name
		var oldName=document.quizEngine.elements[startValue].name;
        var noValFound= true;
		
		// startValue from php session fix added to for loop 
		// Doug Fuerst 12/17/2001
		//alert(document.quizEngine.elements.length);
		var numElements=0;
		var endValue=0;
		
		numElements = 0;
		for (i=startValue; i<document.quizEngine.elements.length; i++) {
		    if ( document.quizEngine.elements[i].name == 'ks' ) {
			    i = document.quizEngine.elements.length;
			} else {
			    numElements++; 
			}	
		}
		endValue = startValue + numElements;
		for (i=startValue; i<endValue; i++) {
            radioName= document.quizEngine.elements[i].name;
            if (oldName != radioName) {
                if (noValFound == true) {
				    return 0;
                } else {
                    oldName=radioName;
                    noValFound= true;
                }
            }
            if (document.quizEngine.elements[i].checked) {
			    noValFound= false;
			}
        }
        if (noValFound == true) {
		    return 0;
		} else {
		    return -1;
		}

} // End isValid

function goNextElement(howmany) {
   var theElements;
   theElements = howmany;
   if (isValidQuiz(theElements) == 0 ) {
       alert('Please select an answer for all questions.');
   } else {
	   document.quizEngine.submit();
   }	   
} // End goNextElement

function goNextElement2(howmany) {
   var theElements;
   theElements = howmany;
   if (isValidQuiz(theElements) <= 0 ) {
       if ( confirm('You have not answered all questions.\n Click OK to score the test now.') == true ) {
	       document.quizEngine.wp.value = 999;
		   document.quizEngine.sp.value = 2;
		   document.quizEngine.submit();
	   }
   } else {
	   document.quizEngine.submit();
   }	   
} // End goNextElement2

function goNextElement3(howmany) {
   var theElements;
   theElements = howmany;
   if (isValidQuiz(theElements) == 0 ) {
       if ( confirm('You have not answered all questions.\n Click Cancel if you want to return to the test.\n Click OK to score the test now.') == true ) {
	       document.quizEngine.wp.value = 999;
		   document.quizEngine.sp.value = 2;
		   document.quizEngine.submit();
	   }
   } else {
	   document.quizEngine.submit();
   }	   
} // End goNextElement3

function checkEmail() {
  if ((document.quizEngine.studentName.value == '')||(document.quizEngine.studentName.value == null))
    {
	    alert('Please enter your name.')
   		return
	}
	lb_noemail = true
	if (document.quizEngine.studentEmail.value != '' ) lb_noemail = false
	if (document.quizEngine.instructorEmail.value != '' ) lb_noemail = false
	if (lb_noemail)
	  alert('Please enter an email receiver')
	else {
	  document.quizEngine.sp.value = "3";
	  document.quizEngine.submit();
	}  
} // End checkEmail


function retryQuiz(isRandom) {
    if ( isRandom == '0' ) {
	    document.quizEngine.sp.value="9999";
	} else {
	    document.quizEngine.sp.value = '1';
	}		
	document.quizEngine.wp.value = '';
	document.quizEngine.ss.value = '';
	document.quizEngine.submit();
}	
  
function openHintPopUp(url) {
    //alert(url);
	var popUpWin = window.open(url,"Hint","resizable=yes,menubar=no,toolbar=no,width=400,height=200,scrollbars=yes");
	popUpWin.focus();  
}// End openHintPopUp			


function openPrintable(url) {
	var popUpWin = window.open(url,"Print","resizable=yes,menubar=yes,toolbar=yes,width=700,height=550,scrollbars=yes");
	popUpWin.focus();  
}// End openPrintable




var popUpWin;

function openCorrelationPopUp(correlationText) {
    //alert(url);
	if (( !popUpWin )||(popUpWin.closed )) {
		popUpWin = window.open("","Correlation","resizable=yes,menubar=no,         toolbar=no,width=400,height=150,scrollbars=yes");
		popUpWin.focus(); 
		popUpWin.document.write('<html>');
		popUpWin.document.write('<head>');
		writeLine = '<link rel=\"stylesheet\" href=\"css-bin/quizEngine.css\" type=\"text/css\">';
		popUpWin.document.write(writeLine);
		popUpWin.document.write('</head>');
		popUpWin.document.write('<body>');
		writeLine = '<span class=\'normText\'>' + correlationText + '</span>';
		popUpWin.document.write(writeLine);
		writeLine = "<br><br><div align='center'><span class='normText'><a href=\"javascript:window.close()\">Close</a></span></div>";
		popUpWin.document.write(writeLine);
		popUpWin.document.write('</body>');
		popUpWin.document.write('</html>');
	} else {
	    popUpWin.focus();
	}
}// End openCorrelationPopUp	




