var question = "What command do you use to change directories? (Type in cd and hit the Enter key.)".bold();
var instructorStr = "cd";
var explStr = "This is where you'll find an explanation of the question and answer. To change to your home directory&#44; you type cd (change directory). As you may already know&#44; Linux is case-sensitive. For example&#44; the cd is not the same as CD.";  
// Global convenience strings to simplify modifying The Question and alert feedback messages html formatting
var begQF = "<FONT SIZE=2' FACE='Helvetica, Arial' COLOR='#003366'>";
var endQF = "<\/FONT><BR>&nbsp;<BR>";
var dialogStr =" Try again?\n\nClick 'OK' to try again.\nClick 'Cancel' to see the answer.";
var begAlrt = '<TR><TD><\/TD><TD><\/TD><TD COLSPAN="2" VALIGN="BOTTOM"><IMG SRC="../common/images/alert.gif" WIDTH="16" HEIGHT="17" ALIGN="LEFT"><\/TD><TD VALIGN="BOTTOM"><FONT SIZE="3" FACE="Helvetica, Arial">';
var endAlrt = '<\/FONT><\/TD><\/TR>';

var d = this.document;

function validateCom (studentStr) 
{ 
 // Convenience function to format alert feedback
 function addSummary (theText)
 {
  summaryStr += begAlrt;
  summaryStr += theText;
  summaryStr += endAlrt;
 }
 // Creates pop-up window with validation report
 function displayResult()
 {
  var w = window.open ("", "theResult", "height=400,width=770,resizable=yes,scrollbars=yes,status=no,menubar=no");
  w.focus();        // brings result window to top
  var d = w.document;
  // Convenience function to format console feedback output
	function writeTTY (theText)
  {
		d.write ('<TD BGCOLOR="#000033"><IMG SRC="../common/images/blank.gif" WIDTH="5"><\/TD>');
		d.write ('<TD ALIGN="LEFT" BGCOLOR="#000033" COLSPAN="2"><TT>' + theText.bold().fontsize(3) + '<\/TT><\/TD>');
		d.write ('<TD BGCOLOR="#000033"><IMG SRC="../common/images/blank.gif" WIDTH="5"><\/TD><\/TR>');
	}
  // Begin writing out pop-up feedback window
	d.write ( ' <head><meta name="DESCRIPTION" content="Configure Key Linux Components" />' + 
	'<meta name="KEYWORDS" content="Red Hat Linux Admin" /><TITLE>Quick Check Results<\/TITLE><\/HEAD>' +
  ' <BODY BGCOLOR="#FFFFFF">' +
  ' <TABLE WIDTH="700" BGCOLOR="#FFFFFF" CELLSPACING="0" CELLPADDING="0" BORDER="0">' +
  '  <TR>' +
  '   <TD><IMG SRC="../common/images/blank.gif" HEIGHT="10" WIDTH="32"><\/TD>' +  // Q
  '   <TD><IMG SRC="../common/images/blank.gif" HEIGHT="10" WIDTH="128"><\/TD>' +  // corr & your answer labels
  '   <TD><IMG SRC="../common/images/blank.gif" HEIGHT="10" WIDTH="5"><\/TD>' +   // space in beginning result
  '   <TD><IMG SRC="../common/images/blank.gif" HEIGHT="10" WIDTH="15"><\/TD>' +  // extra space for alert icon column in validation
  '   <TD><IMG SRC="../common/images/blank.gif" HEIGHT="10" WIDTH="515"><\/TD>' +  // result output
  '   <TD><IMG SRC="../common/images/blank.gif" HEIGHT="10" WIDTH="5"><\/TD>' +   // final space result
  '  <\/TR>' +
  '  <TR VALIGN="TOP">' +
  '   <TD><IMG SRC="../common/images/q_w.gif" ALT="Q" WIDTH="22" HEIGHT="25" ALIGN="LEFT"><\/TD>' +
  '   <TD COLSPAN="4">' + begQF + question + endQF + '<\/TD>' +
  '  <\/TR>' +
  '  <TR>' +
  '   <TD COLSPAN="5"><IMG SRC="../common/images/blank.gif" HEIGHT="15"><\/TD>' +
  '  <\/TR>' +
  '  <TR VALIGN="TOP">' +
  '   <TD ROWSPAN="8"><IMG SRC="../common/images/a_w.gif" ALT="A" WIDTH="22" HEIGHT="25" ALIGN="LEFT">' +
  '   <TD><\/TD>' +
  '   <TD COLSPAN="4" BGCOLOR="#000033"><IMG SRC="../common/images/blank.gif" HEIGHT="3"><\/TD>' +
  '  <\/TR>' +
  '  <TR>' + 
  '   <TD ALIGN="LEFT"><IMG SRC="../common/images/corr_ans.gif" ALT="Correct answer:" WIDTH="118" HEIGHT="8">'
  );
  
  writeTTY (instructorStr.fontcolor("#FFFFFF"));
  
  d.write
  (
  '  <TR>' +
  '   <TD><\/TD>' +
  '   <TD COLSPAN="4" BGCOLOR="#000033"><IMG SRC="../common/images/blank.gif" HEIGHT="3"><\/TD>' +
  '  <\/TR>' +
  '  <TR>' +
  '   <TD COLSPAN="5"><IMG SRC="../common/images/blank.gif" HEIGHT="5"><\/TD>' +
  '  <\/TR>' +
  '  <TR>' +
  '   <TD><\/TD>' +
  '   <TD COLSPAN="4" BGCOLOR="#000033"><IMG SRC="../common/images/blank.gif" HEIGHT="3"><\/TD>' +
  '  <\/TR>' +
  '  <TR>' +
  '   <TD ALIGN="LEFT"><IMG SRC="../common/images/your_ans.gif" ALT="Your answer:" WIDTH="118" HEIGHT="8">'
  );
  
  writeTTY (resultStr);
  
  d.write
  (
  '  <TR>' +
  '   <TD><\/TD>' +
  '   <TD COLSPAN="4" BGCOLOR="#000033"><IMG SRC="../common/images/blank.gif" HEIGHT="3"><\/TD>' +
  '  <\/TR>' +
  '  <TR>' +
  '   <TD COLSPAN="5"><IMG SRC="../common/images/blank.gif" HEIGHT="10"><\/TD>' +
  '  <\/TR>'
  );
  
  d.write (summaryStr);
  
  if (explStr.length > 0)
  {
   d.write 
   (
   ' <TR>' +
   '  <TD><\/TD>' +
   '  <TD><\/TD>' +
   '  <TD COLSPAN="3"><FONT SIZE="4" FACE="Helvetica, Arial" COLOR="#003366"><BR>Explanation:<\/FONT><BR>' +
   '  <FONT SIZE="3" FACE="Helvetica, Arial">' + explStr + '<\/FONT><\/TD>' +
   ' <\/TR>'
   );
  }
  
  d.write ('<\/TABLE><BR>&nbsp;<BR>');
  d.close();
 }
 
 var resultStr = "";  // initialize feedback string of studentStr validation
 var summaryStr = ""; // initialize explanation string, results of validation
 var errCount = 0;  // how many errors found on first pass?
 var instructorArr = instructorStr.split(/\s+/);
 var studentArr = studentStr.split(/\s+/);
 // For nav bug, initial "space" word
	if (studentArr[0].length == 0) 
	{
  	studentArr = studentArr.slice (1); // drop the first spurious NaN element
	} 
 // Convenience variables
 var instructLen = instructorArr.length;
 var studentLen = studentArr.length;
 // Validation flags -- if flags are true, validated successfully for that condition
 var answerV = true; 
 var underV = !(studentLen < instructLen);
 var overV = !(studentLen > instructLen);
 var orderV = true;  // order correct for terms? Assume not a problem
 var caseV = true;  // case correct for terms? Assume not a problem. 
 var matchesV = true; // ...means everything is hunky-dory. Assume it is.
 // Validation checks
 if (!underV)
 {
  var errString = "Not enough terms.";
  if (confirm (errString + dialogStr))
  {
   return false;
  }
  addSummary (errString);
 }
 if (!overV)
 {
  var errString = "Too many terms.";
  if (confirm (errString + dialogStr))
  {
   return false;
  }
  addSummary (errString);
 }
 // Check for no student response
 if (!studentArr[0])
 {
  answerV = false;
  resultStr = "/s";
 }
 else if (studentArr[0].length == 0)
 {
  answerV = false;
  resultStr = "/s";
 } 
 else 
// Check term for term, build color-coded result string, check for case in order
 {
  for (var i = 0; i < studentArr.length; i++)
  {
   if (i >= instructLen)
   {
    matchesV = false;
    resultStr += (studentArr[i].fontcolor ("#FFCC00") + " ");
    errCount++;
    if ((i < instructLen) && 
     ((instructorArr[i] != studentArr[i]) &&
      (instructorArr[i].toLowerCase() == studentArr[i].toLowerCase())))
    {
     caseV = false;
    }
   }
   else if (instructorArr[i] != studentArr[i])
   {
    matchesV = false;
    resultStr += (studentArr[i].fontcolor ("#FFCC00") + " ");
    errCount++;
    if ((i < instructLen) && 
     ((instructorArr[i] != studentArr[i]) &&
      (instructorArr[i].toLowerCase() == studentArr[i].toLowerCase())))
    {
     caseV = false;
    }
   }  
   else
   {
    resultStr += (studentArr[i].fontcolor ("#FFFFFF") + " ");
   }
  }
 } 
 if (!caseV && answerV)
 {
  var errString = "At least one term is incorrectly capitalized.";
  if (underV && overV)
  {
   if (confirm (errString + dialogStr))
   {
    return false;
   }
  }
  addSummary (errString);
 }
 // Validation check for out of order
 if (!matchesV && answerV)
 {
  checkAll:
  for (var i = 0; i < instructLen; i++)
  {
   checkStudent:
   for (var j = 0; j < studentLen; j++)
   {
    if ((instructorArr[i].toLowerCase() == studentArr[j].toLowerCase()) && (i != j))
    {
     for (var k = 0; k < j; k++)  // don't want to flag repetition of an out of order twice for 2 occurences same word
     {
      if (studentArr[k].toLowerCase() == studentArr[j].toLowerCase())
      {
       continue checkStudent;
      }
     }
     orderV = false;
     if (instructorArr[i] != studentArr[j])
     {
      var errString = "At least one term is out of order and incorrectly captitalized.";
      if (underV && overV && caseV)
      {
       if (confirm (errString + dialogStr))
       {
        return false;
       }
      }
      addSummary (errString);
     }
     else 
     {
      var errString = "At least one term is correct but out of order.";
      if (underV && overV && caseV)
      {
       if (confirm (errString + dialogStr))
       {
        return false;
       }
      }
      addSummary (errString);
     }
     break checkAll;
    }
   }
  }
 }

 // Default validation check: wrong for no other obvious reason
 if (!matchesV && (overV && underV && caseV && orderV))
 { 
  if (errCount == 1)
  {
   if (confirm ("One term is incorrect." + dialogStr))
   {
    return false;
   }
  }
  else
  {
   if (confirm ("Incorrect." + dialogStr))
   {
    return false;
   }
  }
 }
 if (!matchesV)
 {
  addSummary ("Terms indicated in <FONT COLOR='#CC9900'>yellow<\/FONT> do not match.");
 }
 else if (summaryStr.length == 0)
 {
  summaryStr += '<TR><TD><\/TD><TD><\/TD><TD COLSPAN="3"><FONT SIZE="3" FACE="Helvetica, Arial">Validated.<\/FONT><\/TD><\/TR>';
 }
 displayResult();
 return false;
}
