<!--Define JavaScript functions.-->

function tinabr(rawtextareaString, inPar, sqAMatrix, bvec){			
  // Sub-routine for getting [A](x) = (b) system data from a textareabox:
  // N, the size of the system,
  // A components, and 
  // b components.
  // Assumes input string comes from a textarea box and that all entries represent real numbers.
  
 var MAXDIM = 12;             		// Maximum size, N, of N x N matrix accepted by this script.
 
 var rawArray = new Array(); 		// Array for holding raw data entry from textarea box
 var rawArrayLength = 0; 		// The length of the raw array entered from textarea box
 var numCoeff = 0;  			//Indicates the number of data entries that have been entered
 var k = 0;   					// Array index
 var tempx = 0.0; 				// Dummy variable
  
 if (rawtextareaString.length == 0){
  alert("The length of textareaString is 0. No data has been entered. No further action taken.");
  inPar.inEr = 1;
  return;
 }

 // At this point, rawtextareaString is a big long string containing the entire field entered in the textarea box.
 // Must now clean up the data: remove leading and trailing spaces, identify the inputs, etc.

 // Check to see that string contains digits; if not, no point continuing
 if (rawtextareaString.search(/\d/) == -1){
  alert("textareaString does not contain any digits. No further action taken.");
  inPar.inEr = 1;
  return;
 }

 // Check to see that string contains only digits, decimal points, "+" or "-" sign, or white space; if not, no point continuing
 if (rawtextareaString.search(/[^\d\.\s\+-]/) != -1){
  alert("textareaString contains an invalid character. Please edit data so that it is in the appropriate format. No further action taken.");
  inPar.inEr = 1;
  return;
 }

 // Check to see that string contains newline characters;
 // if not, then there are not AT LEAST two entries, and the algorithm won't work--no point continuing
  if (rawtextareaString.search(/\n/) == -1){
   alert("This utility requires more than two entries to be entered, but two entries have not been detected. Either fewer than two entries have been entered, or the data is not in the appropriate format. No further action taken.");
   inPar.inEr = 1;
   return;
 }

 // Do some rough clean up
 rawtextareaString = rawtextareaString.replace(/\s*$/, ''); // Remove trailing whitespace
 rawtextareaString = rawtextareaString.replace(/^\s*/, ''); // Remove leading whitespace

 // Divide the string up into its individual entries, which--presumably--are separated by whitespace
 rawArray = rawtextareaString.split(/\s+/g);
 rawArrayLength = rawArray.length;

 // Check to see if at least three entries are present
  if (rawArrayLength < 3){
   alert("This utility requires AT LEAST three entries to be entered, but fewer than three entries have been detected. No further action taken.");
   inPar.inEr = 1;
   return;
  }

// A maximum of 157 entries may be entered (first entry for the matrix dimension, up to 144 for the a-coefficients, up to 12 for the b-coefficients).

if (rawArrayLength > 157){
   alert("This utility accepts input of up to 157 entries; however, more than 157 entries have been input. No further action taken.");
   inPar.inEr = 1;
   return;
}

 numCoeff = rawArrayLength - 1;

// Now check the individual data entries, confirm they are valid numbers, and place the entries in the array

 k = parseInt(rawArray[0]);
 if (!isNaN(k)){ // Degree field contains a valid number; otherwise ignore
   inPar.mDim = k;
  }//End if !isNaN
  else {
    alert("Invalid input for matrix dimension. No further action taken");
    inPar.inEr = 1;
    return;
 } // End else

 if (k > MAXDIM){
      alert("This utility accepts matrices of dimension " + MAXDIM + " or less. However, a higher number has been input. No further action taken.");
      inPar.inEr = 1;
      return;
 }

 if (k <= 0 ){
   alert("Negative values for the matrix dimension are not accepted. No further action taken.");
   inPar.inEr = 1;
   return;
 }

 if (numCoeff != (k*k + k)){
   alert("The number of coefficients entered does not correspond to the matrix dimension entered. Check the data. No further action taken.");
   inPar.inEr = 1;
   return;
 }

// First input the elements of the A matrix: A[0][0], A[0][1], A[0][2], . . .  A[1][0], A[1][1], A[1][2], . . . etc.

 k = 1;
 for (var i = 0; i < inPar.mDim; i++) { // Examine the data fields
   for (var j = 0; j < inPar.mDim; j++) {
     tempx = parseFloat(rawArray[k]);
     if (!isNaN(tempx)){ // Field contains a valid number
      sqAMatrix[i][j] = tempx;
     }//End if !isNaN
     else {
      alert("Invalid input for entry " + k + ". No further action taken");
      inPar.inEr = 1;
      return;
     } // End else
     k++;
   } // End for j 
 } // End for i
 
// Now input the elements of b: b[0], b[1], b[2], . . .

for (var i = 0; i < inPar.mDim; i++) { // Examine the data fields
  tempx = parseFloat(rawArray[k]);
  if (!isNaN(tempx)){ // Field contains a valid number
   bvec[i] = tempx;
  }//End if !isNaN
  else {
    alert("Invalid input for entry " + k + ". No further action taken");
    inPar.inEr = 1;
    return;
  } // End else
  k++;
 } // End for i
 
 //  *****************************************************************
 // At this point, inPar.mDim should be the matrix dimension, N,
 // sqAMatrix should be the square matrix A, of size N x N, and
 // bvec should be the b vector, of size, N.
 //  *****************************************************************

return;
}  //End of tinabr

// end of JavaScript-->
