/* Sumbrero check-up routines
// Copyright 2006, A.E.Veltstra
// Part of the Sumbrero solver
// These routines work no matter how large the sumbrero puzzle nor the amount of answer boxes
// Embedding page must supply own data, filling in the Vectors and Crossing arrays.
*/

var Vectors = new Array();
/* Vectors are lines formed by the text / number inputs (only 'answer's) on the form.
// Each Vector is defined by the name attribute of a 'given' input on the form,
// and holds an array of names of those 'answer' inputs on the form, that form its line.
// The vectorSumCheck() routine checks the sum of the 'answer' inputs against the value of the 'given' input.
*/
/* sample data for Vectors */
//Vectors['Given1'] = new Array('Answer1','Answer2');
//Vectors['Given2'] = new Array('Answer2','Answer3');
//Vectors['Given3'] = new Array('Answer2','Answer4','Answer5');


var Crossings = new Array();
/* Crossings are the text / number inputs (both 'answer's and 'given's) on the form.
// Each Crossing is defined by the name attribute of an input on the form,
// and holds an array of the names of those Vectors in the Vectors array,
// in which line it participates.
// The CheckCrossing() routine takes a Crossing and checks each of its Vectors.
*/
/* sample data for Crossings */
//Crossings['Answer2'] = new Array('Given1','Given2','Given3');


function vectorSumCheck(strVectorName) {
// The vectorSumCheck() routine checks the sum of the 'answer' inputs against the value of the 'given' input.
  var q = new Number();
  if ((strVectorName)&&(Vectors[strVectorName])) {
    var frm = document.forms["Som"].elements;
		if (frm[strVectorName]) {
    	var arrCoords = Vectors[strVectorName];
      var j = new Number(arrCoords.length);
      for (var i = 0; i < j; i++) {
    		if (frm[arrCoords[i]]) {
    			 q += Number(frm[arrCoords[i]].value);
    		}
  		}
  		return (q == Number(frm[strVectorName].value));
    } else {
     	alert("Vector '" + strVectorName + "' not on form.");
     	return false;
    }
  } else {
    alert("Vector '" + strVectorName + "' not in Vectors array.");
    return false;
  }
  return false;
}
function CheckCrossing(box) {
// The CheckCrossing() routine takes a Crossing and checks each of its Vectors.
 if (Crossings[box]) {
    var frm = document.forms["Som"].elements;
 		var arrVectors = Crossings[box];
    var strVName = new String();
 		var j = new Number(arrVectors.length);
 		for (var i = 0; i < j; i++) {
 		  strVName = String(arrVectors[i]);
 		  if (frm[strVName]) {
  			if (vectorSumCheck(strVName)) {
          //these are the black boxes only
  				frm[strVName].style['borderColor'] = 'black';
  			} else {
  				frm[strVName].style['borderColor'] = 'red';
  			}
     } else {
   	 	 alert("Vector '" + strVName + "' not on form.");
     }
 		}
 } else {
   alert("Box '" + box + "' not in Crossings array.");
 }
 return true;
}
function CheckAll() {
  //Check the entire form in one go (f.i. invoke at form.onsubmit())
  //Override this in local script file: this generic script doesn't know the form data.
	/* sample */
  //CheckCrossing('Answer1');
  //CheckCrossing('Answer2');
  //CheckCrossing('Answer3');
	/* end sample */
  return false;
}