// Copyright 2001-2002 GuideTools.com Ltd

// include baseFunctions.js...
// document.write('<script src="/js/baseFunctions.js"><\/script>');

function GT_multiChoice(theSelf, correctAnswerBlockID, incorrectAnswerBlockID, inputName, correctChoice) {
  // properties
  this._self = theSelf;
  this._correctAnswerBlock = GT_FindObject(correctAnswerBlockID);
  this._incorrectAnswerBlock = GT_FindObject(incorrectAnswerBlockID);
  this._correctChoice = correctChoice;  
  this._answerWindow = null;
  this._inputName = inputName;
  this._resultForm = 'multiForm';

  clearRadios(inputName);

  this.checkAnswerDB = GT_checkAnswerDB;
  this.checkAnswer = GT_checkAnswer;    
 }

function GT_checkAnswer(selectedItem) {  
  with (this) {
    if (selectedItem.value == _correctChoice)  {
      _answerWindow = GT_showBlockInWindow(_correctAnswerBlock, _answerWindow, "answerWin", "Multi Choice Question")
    } else {
      _answerWindow = GT_showBlockInWindow(_incorrectAnswerBlock, _answerWindow, "answerWin", "Multi Choice Question") }
    var e = window.event;
    if ((e) && (e.type=='click'))
      e.cancelBubble = true;
  }
}

function GT_checkAnswerDB(obj, objpageUrl) {  
   var qID = GT_GetMultiQID(objpageUrl);
   if (qID != '0') GT_GetMultiChoiceQ(qID, this._correctChoice);

   with (this) {
      if (obj.value == _correctChoice)  {
        _answerWindow = GT_showBlockInWindowNew(_correctAnswerBlock, _answerWindow, "answerWin", "Multi Choice Question", this._resultForm, objpageUrl, obj.value, "Y")
      } else {
        _answerWindow = GT_showBlockInWindowNew(_incorrectAnswerBlock, _answerWindow, "answerWin", "Multi Choice Question", this._resultForm, objpageUrl, obj.value, "N") }
      var e = window.event;
      if ((e) && (e.type=='click'))
        e.cancelBubble = true;
    }
    
//   var myform = GT_FindObject(this._resultForm);
//   var popUp = GT_openPopupWindow('popUpWin', 10, 10);
//   myform.target = 'popUpWin';
//   myform.action=objpageUrl;
//   myform.submit();
//   popUp.close();       

}

function GT_checkAnswerDB_Local(obj, objpageUrl) {  
  GT_checkAnswer(obj);
}

function GT_showBlockInWindowNew(theBlock, theWindow, windowName, windowTitle, answerForm, FormUrl, choiceValue, isCorrect) {
  var e = window.event;
  
  if ((e)&&(e.type=='click'))
    e.cancelBubble = true;

  if (theWindow) theWindow.close();
  
  var resultWindow = GT_openPopupWindow(windowName, 300, 10);
   
  /*	var myform = GT_FindObject(answerForm);
	if (myform) {
	   myform.target = windowName;
	   myform.action= FormUrl + '&MULTICHOICEANSWERNO=' + choiceValue + '&MULTICHOICECORRECT=' + isCorrect;
	   myform.submit();  
   } */
  
   resultWindow.document.clear();
  // accept either the object id, or the object itself...
  var showBlock = null;
  if (!theBlock.substr)
    showBlock = theBlock
  else
    showBlock = GT_findObj_v4(theBlock);
  GT_copyToWindow(showBlock, resultWindow, windowTitle);
  GT_resizeWindow(resultWindow, resultWindow.document.body.scrollWidth + 40, resultWindow.document.body.scrollHeight + 135, true);  
  resultWindow.focus();
  return resultWindow;
}

function GT_GetMultiQID(url)
{
  var str = url;
  var strlen = str.length;
  var pos = str.indexOf('MULTICHOICEQUESTIONID=');  
  if (pos != -1)
    { 
		str = str.substring(pos + 22,strlen);
		return str;
	}
  else
  {  return '0';}
}

function GT_GetMultiChoiceQ(qid, correntNumber)
{
  var questionID = 'GT_MultiChoice_' + qid;
  var correctID = questionID + '_CorrectBlock';
  var incorrectID = questionID + '_IncorrectBlock';
  var inputName = questionID + '_INP';

  qDiv = document.getElementById(questionID);
  if (qDiv) {
          var x=document.getElementsByName(inputName);
          //alert(x.length + " elements!");
          var i, radioElemnt;
          for (i=0; i<x.length;i++) 
          {
            radioElemnt = x[i];
            if (radioElemnt.value == correntNumber)
              radioElemnt.parentNode.parentNode.className = "correctBlock";
			else
			  radioElemnt.parentNode.parentNode.className = "";
            // alert(radioElemnt.checked);
          }          
    // document.getElementById('questionHiddenBlock').value = qDiv.innerHTML;
    // alert(document.getElementById('questionHiddenBlock').value);
  }
  else
  {
    // document.getElementById('questionHiddenBlock').value = '';
  }

  /*
  alert(qDiv.innerHTML); 
  qDiv = document.getElementById(correctID);
  alert(qDiv.innerHTML); 
  qDiv = document.getElementById(incorrectID);  
  alert(qDiv.innerHTML);     
  */

} 
