var dbug=0
/*Need to discover which school is taking the quiz and modify all links respective to that school first directory is schools*/
rawurl=location.href
fslash=rawurl.indexOf("/",8) //This is the first slash we need the next one
lslash=rawurl.indexOf("/",fslash+1)
//unless this is a lite site then we need the next one...
var schoolurl=rawurl.substring(0,lslash)
var schooldir=rawurl.substring(fslash+1,lslash)
//if (schooldir=="lite"){
//	fslash=lslash
//	lslash=rawurl.indexOf("/",fslash+1)
//	var schoolurl=rawurl.substring(0,lslash)
//	var schooldir=rawurl.substring(fslash+1,lslash)
//}

function doQuestion(questionnum){
    //Get Participant (Using Session ID)
    //Get School (Derived from the url above)
    school=schooldir
    //Get quizname (Form Name)
    quiz=document.forms[0].name
    //Get Question (passed)
    questionName='radioQuestion' + questionnum
    //Get the actual text of the question
    iputs=document.getElementsByTagName("div")
    for (i=0;i<iputs.length;i++){
      icls=iputs[i].className
      iHTM=iputs[i].innerHTML.toString()
      if (icls=='quiz_question'){
          pdloc=iHTM.indexOf(".")
          qnum=iHTM.substring(0,pdloc)
          if (qnum==questionnum){
            question=iHTM.substring(pdloc+2) //add one for the period and one for the space after
          }
      }
    }
    //Get Answer (Collect from inputs collection
        iputs=document.getElementsByTagName("input")
        //GET the identifier for the button (using the function in the onclick)
         for (i=0;i<iputs.length;i++){
           iname=iputs[i].name
           isel=iputs[i].checked
           iclk=iputs[i].outerHTML
           iclk=iclk.toString()
           if (iclk){//
               if (questionName==iname){
                    if(isel==true){
                        stid=iclk.indexOf("onclick=")
                        edid=iclk.indexOf(")")+1
                        qid=iclk.substring(stid+9,edid) 

                    }
               }
           }
        }
        //FIND THE ANSWER
        iputs=document.getElementsByTagName("div")
        for (i=0;i<iputs.length;i++){
          icls=iputs[i].className
          iHTM1=iputs[i].innerHTML
          iHTM=iHTM1.toString()
          tiHTM=iHTM.toLowerCase()
          if (icls==''){
            exDiv=iHTM.search(/<div>/gi)
            if (exDiv==-1){
                incInp=iHTM.search(/<input/gi)
                if (incInp!=-1){
                   nrwAns=tiHTM.indexOf(qid.toLowerCase(),0)
                    if (nrwAns!=-1){
                        stans=tiHTM.indexOf("<span>")
                        edans=tiHTM.indexOf("</span>")
                        answer=iHTM.substring(stans+6,edans)
                    }
                }
            }
          }
        }
    //TimeStamp (Happens in the ASP code behind)
    //Get The correct and dialog
    fndcomma=qid.indexOf(",")
    fndend=qid.indexOf(")")
    //alert(fndcomma + ' - '  + qid)    
    
    intArrNum=qid.substring(fndcomma+1,fndend)
    var strResult = arrQuiz[questionnum][intArrNum];
    var arrResult = strResult.split("|");
    if (arrResult.length > 0) {
        correct = arrResult[0];
        ansHead = arrResult[1];
        ansText = arrResult[2];
        ansdialog=ansHead + ' ' + ansText
    }
    
    CheckforRecord(school,quiz,question,answer,correct,ansdialog,questionnum)
}

function CheckforRecord(school,quiz,question,answer,correct,ansdialog,questionnum){
    //alert('In Function:' + participant + ',' + school + ',' + quiz + ',' + question + ',' + answer + ' ' + ansdialog + ')')
    CheckforRecordHTTP = getHTTPObject();
	var newURL = "/public/quizes/checksaveanswer.asp?hidesec45x=1xj&s=" + school + "&z=" + quiz + "&q=" + question + "&a=" + answer + "&c=" + correct + "&d=" + ansdialog + "&n=" + questionnum
	CheckforRecordHTTP.open("GET", newURL, true);
	if (dbug==1){
	    CheckforRecordHTTP.onreadystatechange = function doCheck(){ if (CheckforRecordHTTP.readyState == 4){alert(CheckforRecordHTTP.ResponseText)}};  
	}else{
	    CheckforRecordHTTP.onreadystatechange = function doCheck(){ if (CheckforRecordHTTP.readyState == 4){/*nothing to do all done in checksaveanswer.asp*/}};
    }
    CheckforRecordHTTP.send(null);
}

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;
}

function goResults(){
   	//Get quizname (Form Name)
   	quiz=document.forms[0].name;
	//newurl=schoolurl + '/quizes/quizresults.asp?quizname=' + quiz + '&school=' + schooldir + '&schoolurl' + schoolurl;
	newurl='/public/quizes/quizresults.asp?quizname=' + quiz + '&school=' + schooldir + '&schoolurl=' + schoolurl;
	//alert(newurl)
	this.location.href=newurl;
}
//###############################################
//alert(navigator.appName)
/*FireFox Stuff*/
if(navigator.appName!='Microsoft Internet Explorer'){ 
var _emptyTags = {
   "IMG":   true,
   "BR":    true,
   "INPUT": true,
   "META":  true,
   "LINK":  true,
   "PARAM": true,
   "HR":    true
};

HTMLElement.prototype.__defineGetter__("outerHTML", function () {
   var attrs = this.attributes;
   var str = "<" + this.tagName;
   for (var i = 0; i < attrs.length; i++)
      str += " " + attrs[i].name + "=\"" + attrs[i].value + "\"";

   if (_emptyTags[this.tagName])
      return str + ">";

   return str + ">" + this.innerHTML + "</" + this.tagName + ">";
});

}
