function getAJAX(strTyp, strArgs, obj) {
  var ajaxRequest;  //  infamous AJAX variable
  var err;  // error
  var strOut = '';  // return value

  // browser support code
  try { ajaxRequest = new XMLHttpRequest(); /* Opera 8.0+, Firefox, Safari */ }
  catch(err) {
    try { ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); /* IE Browsers */ }
    catch(err) {
      try { ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); }
      catch(err) {
        // no AJAX support
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
  }

  // receive data from server via AJAX when complete
  ajaxRequest.onreadystatechange = function() {
    if(ajaxRequest.readyState==4) {
      switch(strTyp) {
        case 'def':
          obj.innerHTML = '{'+ajaxRequest.responseText+'}';
          break;
        case 'max':
          obj.length = 1;
          for(intIDX=1; intIDX<=ajaxRequest.responseText; intIDX++) {
            var objOpt = document.createElement('option');
            objOpt.text = intIDX;
            objOpt.value = getBufferedNumber(intIDX);
            try {
              obj.add(objOpt, null);  // not-IE
            }
            catch(ex) {
              obj.add(objOpt);  // IE-only
            }
          }
          break;
        case 'pos':
          if(ajaxRequest.responseText.indexOf('adjective')>=0)
            obj.className = 'adj';
          else if(ajaxRequest.responseText.indexOf('adverb')>=0)
            obj.className = 'adv';
          else if(ajaxRequest.responseText.indexOf('conj')>=0 || ajaxRequest.responseText.indexOf('prep')>=0)
            obj.className = 'conjprep';
          else if(ajaxRequest.responseText.indexOf('noun')>=0 && ajaxRequest.responseText.indexOf('article')==-1)
            obj.className = 'nounpro';
          else if(ajaxRequest.responseText.indexOf('verb')>=0)
            obj.className = 'verb';
          break;
        case 'str':
          alert(ajaxRequest.responseText);
          break;
        case 'trn':
          obj.innerHTML = ajaxRequest.responseText;
      }
    }
  }

  // request data from the server via AJAX
  ajaxRequest.open('GET', 'ajax/dynamic.php?trn='+strTyp+strArgs, true);
  ajaxRequest.send(null);
}

function getBufferedNumber(intIn) {
  // zero-pad left side of number to three positions
  strOut = intIn.toString();

  while(strOut.length<3)
    strOut = '0'+strOut;

  return strOut;
}

function setChapters() {
  // set the chapter
  obj = document.getElementById('c');
  getAJAX('max', '&c='+document.getElementById('b').value, document.getElementById('c'))
}

function setSession() {
  // set the session variable for keeping the notes
  if(document.getElementById('mynotes')) {
    tinyMCE.triggerSave(true, true);
    getAJAX('ses', '&n='+encodeURIComponent(tinyMCE.getContent()));
  }
}

function validate() {
  // validate form submittal
  if(document.frm.c.value=='') {
    alert('You must specify a book and a chapter');
    return false;
  }
  else
    return true;
}

function getTVM(strTVM) {
  // display verb tense, voice, and mood
  var strMessage = "\nVerb Tense - the \"time\" in which the verb takes place:\n";

  if(strTVM.indexOf('Aor2')>=0)
    strMessage += "* Name: Ablaut/Second/Strong Aorist tense\n* Description: Past participle--action happened in the past\n* Example: \"it was 'done' by the dog\"\n";
  else if(strTVM.indexOf('Aor')>=0)
    strMessage += "* Name: Sigmatic/First/Weak Aorist tense\n* Description: Past preterite--action happened in the past\n* Example: \"the dog 'did' it\"\n";
  else if(strTVM.indexOf('Fut')>=0)
    strMessage += "* Name: Future tense\n* Description: Expresses intention, prediction, and other senses\n* Example: \"the dog 'will do' it\"\n";
  else if(strTVM.indexOf('Impf')>=0)
    strMessage += "* Name: Imperfect tense\n* Description: Action took place in the past and that it is not taking place now\n* Example: \"the dog 'used to do' it\"\n";
  else if(strTVM.indexOf('Perf')>=0)
    strMessage += "* Name: Perfect tense\n* Description: Action took place in the recent past or an unknown past time\n* Example: \"the dog 'has done' it\"\n";
  else if(strTVM.indexOf('Plupf')>=0)
    strMessage += "* Name: Pluperfect tense\n* Description: Action was completed before some other event\n* Example: \"the dog \'had done\' it\"\n";
  else if(strTVM.indexOf('Pres')>=0)
    strMessage += "* Name: Present tense\n* Description: Action is currently happening, is a habit, or an ability\n* Example: \"the dog 'does' it\" or \"the dog 'is doing' it\"\n";
  else
    strMessage += "* none specified";

  strMessage += "\nVerb Voice - the \"relationship\" of the verb's action/state to its participants\n";

  if(strTVM.indexOf('Act')>=0)
    strMessage += "* Name: Active voice\n* Description: Subject is agent/actor of verb\n* Example: \"the chef 'cooked' the food\"\n";
  else if(strTVM.indexOf('Mid')>=0)
    strMessage += "* Name: Middle voice\n* Description: Appears active, but expresses passive action\n* Example: \"the food 'cooked' in the kitchen\"\n";
  else if(strTVM.indexOf('Pas')>=0)
    strMessage += "* Name: Passive voice\n* Description: subject is patient/target/undergoer\n* Example: \"the food 'was cooked' by the chef\"\n";
  else
    strMessage += "* none specified";

  strMessage += "\nVerb Mood - the \"feeling\" of the verb:\n";

  if(strTVM.indexOf('Impr')>=0)
    strMessage += "* Name: Imperative mood\n* Description: Expresses direct commands, requests, and prohibitions to be obeyed without argument\n* Example: \"'do' it!\"\n";
  else if(strTVM.indexOf('Imps')>=0)
    strMessage += "* Name: Impersonal mood\n* Description: Verb independent of patient/target/undergoer\n* Example: \"the dog 'does'\"\n";
  else if(strTVM.indexOf('Ind')>=0)
    strMessage += "* Name: Indicative mood\n* Description: The \"normal\" mood, used for factual statements and positive beliefs\n* Example: \"the dog 'does' it\"\n";
  else if(strTVM.indexOf('Inf')>=0)
    strMessage += "* Name: Infinitive\n* Description: The verb's \"unmarked\" form (i.e., the base/root)\n* Example: \"the chef waited for the food to 'cook'\"\n";
  else if(strTVM.indexOf('Opt')>=0)
    strMessage += "* Name: Optative mood\n* Description: Expresses hopes, wishes or commands\n* Example: \"the dog 'should do' it\"\n";
  else if(strTVM.indexOf('Subj')>=0)
    strMessage += "* Name: Subjunctive mood\n* Description: Used for discussing hypothetical/unlikely events, expressing opinions/emotions, or making polite requests\n* Example: \"the dog 'may do' it\"\n";
  else
    strMessage += "* none specified";

  alert(strTVM+"\n"+strMessage);
}

function initSettings() {
  // initialize page settings
  setTranslation();
  setDef();
  setNotes();
}

function setColor() {
  // toggle the color-coding system and legend
  document.getElementById('vgcDiv').className = (document.getElementById('vgc').checked)?'color':'nocolor';
  document.getElementById('vgcLegend').style['display'] = (document.getElementById('vgc').checked)?'':'none';
}

function setDef() {
  // toggle definition
  var strNum;

  for(var obj = document.getElementsByTagName('span'), intIDX=0, intMax=obj.length; intIDX<intMax; intIDX++)
    if(obj[intIDX].id && obj[intIDX].id.indexOf('def')===0) {
      strNum = document.getElementById('num'+obj[intIDX].id.substr(3)).innerHTML.substr(4, 4);

      if(document.getElementById('def').checked) {
        if(obj[intIDX].innerHTML=='')
          getAJAX('def', '&n='+strNum, obj[intIDX]);

        obj[intIDX].style['display'] = '';
      }
      else
        obj[intIDX].style['display'] = 'none';
    }

  document.getElementById('vgcLegend').style['display'] = (document.getElementById('vgc').checked)?'':'none';
}

function setNotes() {
  // toggle MyNotes
  document.getElementById('ntsCell').style['display'] = (document.getElementById('nts').checked)?'':'none';
  document.getElementById('mce_editor_0').style['width'] = '325px';
  document.getElementById('mce_editor_0').style['height'] = '525px';
}

function setTranslation() {
  // toggle parallel translation
  if(document.getElementById('t').value) {
    getAJAX('trn', '&t='+document.getElementById('t').value+'&c='+document.getElementById('b').value+document.getElementById('c').value, document.getElementById('trn'));
    document.getElementById('trnCell').style['display'] = '';
  }
  else
    document.getElementById('trnCell').style['display'] = 'none';
}

function setStrongs(strNum) {
  // toggle Strong's definitions
  getAJAX('str', '&n='+strNum);
}

function setStyle(strTyp, strProp, strTrue, strFalse) {
  // toggle style for given type
  for(var a = document.getElementsByTagName('span'), i=0, n=a.length; i<n; i++)
    if(a[i].id && a[i].id.indexOf(strTyp)===0)
      a[i].style[strProp] = (document.getElementById(strTyp).checked)?strTrue:strFalse;
}

function setTVM() {
  // toggle verb tense, voice, and mood
  setStyle('tvm', 'display', '', 'none');
  document.getElementById('tvmLegend').style['display'] = (document.getElementById('tvm').checked)?'':'none';
}