/*  dlbExCheck.js
Basic interactive exercise checking script.
Written by Daryl L. Beres (bere0046@umn.edu).
Last updated 8/17/04
*/
if(document.forms.length==0)
window.alert('There are no forms to check on this page. Try putting the dlbExCheck script at the end of the document.');else
{for(var i=0;i<document.forms.length;i++)
{foundCheckButton=false;for(j=0;j<document.forms[i].elements.length;j++)
{if(document.forms[i].elements[j].type=="button")
{var theButtonName=document.forms[i].elements[j].name;if(theButtonName.search(/check/i)!=-1)
{document.forms[i].elements[j].onclick=function(){check(this.form);return false;};document.forms[i].onsubmit=function(){check(this);return false;};foundCheckButton=true;break;}}}
if(foundCheckButton==false)
window.alert("No Check button found in Form #"+(i+1)+" on this page.  User will not be able to check the form.");setPreferences(document.forms[i]);}}
function check(theForm){var theFeedback="";var numOfQuestions=0;var numWrong=0;var theElem=theForm.elements;var x=0;while(x<theElem.length)
{var theResponse="";var theAnswers=new Array();if(theElem[x].type=="text"||theElem[x].type=="textarea")
{theResponse=theElem[x].value;}
else if(theElem[x].type=="radio")
{while((x<theElem.length)&&(theElem[x].type=="radio"))
{if(theElem[x].checked)
theResponse=theElem[x].value;x++;}
x--;}
else if(theElem[x].type=="checkbox")
{if(theElem[x].checked)
theResponse="yes";else
theResponse="no";}
else if(theElem[x].type=="select-one")
theResponse=theElem[x].options[theElem[x].selectedIndex].value;else
{x++;continue;}
QuesElementIndex=x;x++;if(theElem[x].type!="hidden")
{alert("No answer defined for input #"+x+". Cannot check this answer.");continue;}
else
{numOfQuestions++;var i=0;while((x<theElem.length)&&(theElem[x].type=="hidden"))
{theAnswers[i]=theElem[x].value;i++;x++;}
if(!(compare(theResponse,theAnswers,theForm.dlbExCheck.caseSensitive,theForm.dlbExCheck.checkPunctuation)))
{numWrong++;if(theForm.dlbExCheck.detailedFeedback==true)
theFeedback+="\""+theResponse+"\" is not correct.\n";if(theForm.dlbExCheck.useGraphicFeedback==true)
{if(theElem[QuesElementIndex].feedbackImage)
theElem[QuesElementIndex].feedbackImage.src=document.wrongGraphic.src;else
alert('Use graphic feedback preference is chosen, but this question does not have an associated image.  Add the image and script code after each question.');}}
else
{if(theForm.dlbExCheck.useGraphicFeedback==true)
{if(theElem[QuesElementIndex].feedbackImage)
theElem[QuesElementIndex].feedbackImage.src=document.correctGraphic.src;else
alert('Use graphic feedback preference is chosen, but this question does not have an associated image.  Add the image and script code after each question.');}}}}
var theScore=Math.round((numOfQuestions-numWrong)/numOfQuestions*100);if(numWrong>0)
var theMessage="Please try again.  Your score is:  "+theScore+"%.\n("+numWrong+" wrong out of "+numOfQuestions+").\n";else
var theMessage="Great job!  You've got them all correct!\nYour score is 100%.\n";if(theForm.dlbExCheck.trackTries==true)
{theForm.dlbExCheck.numTries++;theMessage+="This was your ";if(theForm.dlbExCheck.numTries==1)
theMessage+="1st try.\n\n";else if(theForm.dlbExCheck.numTries==2)
theMessage+="2nd try.\n\n";else if(theForm.dlbExCheck.numTries==3)
theMessage+="3rd try.\n\n";else
theMessage+=theForm.dlbExCheck.numTries+"th try.\n\n"}
else
theMessage+="\n"
theFeedback=theMessage+theFeedback;if(theForm.dlbExCheck.useAlertFeedback==true)
{theFeedback+="\n";window.alert(theFeedback);}}
function setPreferences(theForm){theForm.dlbExCheck=new Object;theForm.dlbExCheck.caseSensitive=true;theForm.dlbExCheck.checkPunctuation=true;theForm.dlbExCheck.useAlertFeedback=true;theForm.dlbExCheck.useFeedbackFrame=false;theForm.dlbExCheck.detailedFeedback=false;theForm.dlbExCheck.useGraphicFeedback=false;theForm.dlbExCheck.trackTries=false;theElem=theForm.elements;if(theElem['caseSensitive'])
if(theElem['caseSensitive'].value=='false')
theForm.dlbExCheck.caseSensitive=false;if(theElem['checkPunctuation'])
if(theElem['checkPunctuation'].value=='false')
theForm.dlbExCheck.checkPunctuation=false;if(theElem['useAlertFeedback'])
if(theElem['useAlertFeedback'].value=='false')
theForm.dlbExCheck.useAlertFeedback=false;if(theElem['detailedFeedback'])
if(theElem['detailedFeedback'].value=='true')
theForm.dlbExCheck.detailedFeedback=true;if(theElem['useFeedbackFrame'])
{if(theElem['useFeedbackFrame'].value=='true')
{theForm.dlbExCheck.useFeedbackFrame=true;}}
if(theElem['useGraphicFeedback'])
{if(theElem['useGraphicFeedback'].value=='true')
{theForm.dlbExCheck.useGraphicFeedback=true;if(document.images)
{if(!theElem['blankGraphicURL'])
theForm.dlbExCheck.useGraphicFeedback=false;else
{theForm.dlbExCheck.blankGraphicURL=theElem['blankGraphicURL'].value;if(!theElem['correctGraphicURL'])
theForm.dlbExCheck.useGraphicFeedback=false;else
{theForm.dlbExCheck.correctGraphicURL=theElem['correctGraphicURL'].value;if(!theElem['wrongGraphicURL'])
theForm.dlbExCheck.useGraphicFeedback=false;else
{theForm.dlbExCheck.wrongGraphicURL=theElem['wrongGraphicURL'].value;document.blankGraphic=new Image;document.blankGraphic.src=theForm.dlbExCheck.blankGraphicURL;document.correctGraphic=new Image;document.correctGraphic.src=theForm.dlbExCheck.correctGraphicURL;document.wrongGraphic=new Image;document.wrongGraphic.src=theForm.dlbExCheck.wrongGraphicURL;}}}}}}
if(theElem['trackTries'])
if(theElem['trackTries'].value=='true')
{theForm.dlbExCheck.trackTries=true;theForm.dlbExCheck.numTries=0;}}
function compare(resp,ans,caseSensitive,checkPunctuation){if(caseSensitive==false)
resp=resp.toLowerCase();if(checkPunctuation==false)
resp=stripPunc(resp);for(var i=0;i<ans.length;i++)
{if(caseSensitive==false)
ans[i]=ans[i].toLowerCase();if(checkPunctuation==false)
ans[i]=stripPunc(ans[i]);if(resp==ans[i])
return true;}
return false;}
function stripPunc(str){var tempStr='';for(var i=0;i<str.length;i++)
if((str.charAt(i)>='A'&&str.charAt(i)<='Z')||(str.charAt(i)>='a'&&str.charAt(i)<='z')||(str.charAt(i)>='0'&&str.charAt(i)<='9'))
tempStr+=str.charAt(i);return tempStr;}
