/* ======================================================================
DESC: Various functions to be used to construct and check truth tables.


USAGE NOTES: 1/27/99;  modified 
====================================================================== */


function atom() {

// next the atomic components are given in an array.  e.g., atomComp[0]=='A'
sentence = sent.join(',');
	 var j = atomComp.length; 
	for ( var i=0; i<sentence.length; i++ ) {
		var t = sentence.charAt(i);
   	if ( atomic.indexOf(t)>-1 && atomComp.join('').indexOf(t)<0 ) {
   atomComp[j]= sentence.charAt(i); j++;
		}
	}
	atomComp.sort();
	
	// finally a, an associative array is defined with arguments: atomComp+row#. E.g., a[A0] == T.  Generally, a[Xrow#]==tVal
	 n = atomComp.length;  l = Math.pow(2, n); 
	for ( var j=0; j<n; j++ ) {
   	for ( var i=0; i<l; i++ ) {
   		if ( i % (l * Math.pow(2, -j) ) < (l * Math.pow(2, -j-1) ) ){
   			a[atomComp[j] + i] = 'T'
			}
			else {
   			a[atomComp[j] + i] = 'F'
			}
		}
	}

}  

		// first, text is entered, checked for membership in SL, and its component structure CS is determined.  CS
		// is a function from non-parens to determinates of truth value: e.g., CS[3]=='2|4' for [(AvB)>C]
function define(sentence) {


//  to be placed at the end of define

	
		// next sentComp is defined to determine the values and structure of the form elements, e.g., sentComp[2]=='[(A'
	var k = 0; var comp = ''; var j; sentComp = new Array;
	for ( var i=0; i<sentence.length; i++ ) {
		if ( sentence.charAt(i) == ']' || sentence.charAt(i) == ')' ) 
   		continue;		
		comp += sentence.charAt(i);
	   if ( sentence.charAt(i) != '[' && sentence.charAt(i) != '('  ) {
   		j = i+1;
   		while ( sentence.charAt(j) == ')' || sentence.charAt(j) == ']' ) {
   			comp += sentence.charAt(j);
   			j++;
			}
		sentComp[i] = comp;
		comp = '';
		}
	}
}

// this may need cleaning up:
function wrong(elmt) {
	elmt.value = 'X'; elmt.focus(); elmt.blur()
	elmt.value = '?';
	//elmt.focus(); elmt.select()

}

//the main validation check of truth values...
function check(elmt) {
	var nm = elmt.name;
	var r = nm.substring(1, nm.indexOf('|')); var n = nm.substring(nm.indexOf('|') + 1);
	if ( elmt.value == 't' || elmt.value == 'f' ) {
   elmt.value = elmt.value.toUpperCase()
	}
	if ( elmt.value != '' && elmt.value != 'T' && elmt.value != 'F' ) {
	   wrong(elmt)
	}
	else if ( atomic.indexOf(sentence.charAt(n))>-1 ) {
   	if ( elmt.value != a[sentence.charAt(n) + r] ) {
   wrong(elmt)
		}
	}
	else if ( sentence.charAt(n) == '~' ) {
		if ( elmt.value == parent.tabl.document.f.elements['c' + r + '|' + CS[n]].value || parent.tabl.document.f.elements['c' + r + '|' + CS[n]].value == '' ){
   		wrong(elmt);
		}
	}
	else {
		var c = CS[n]
		var c1 = c.substring(0,c.indexOf('|')); var c2 = c.substring(c.indexOf('|')+1);
		var c1val = parent.tabl.document.f.elements['c' + r + '|' + c1].value; var c2val = parent.tabl.document.f.elements['c' + r + '|' + c2].value
		if ( (c1val != 'T' && c1val != 'F') || ( c2val != 'T' && c2val != 'F' )  ) {
   		wrong(elmt);
		}  
		else {
		var c1val = ( c1val  == 'T') ? true : false ;
		var c2val = ( c2val == 'T') ? true : false ; 
		if ( sentence.charAt(n) == 'v' ) {
			var tv = (eval(c1val || c2val)) ? 'T' : 'F'
				if ( elmt.value != tv ) {
   				wrong(elmt);
				}
		}
		else if ( sentence.charAt(n) == '&' ) {
			var tv = (eval(c1val && c2val)) ? 'T' : 'F'
				if ( elmt.value != tv ) {
   				wrong(elmt);
				}
		}
   	else if ( sentence.charAt(n) == '>' ) {
			var tv = (eval(!c1val || c2val)) ? 'T' : 'F'
				if ( elmt.value != tv ) {
   				wrong(elmt);
				}
		}
	
		else if ( sentence.charAt(n) == '=' ) {
			var tv = ((c1val == c2val)) ? 'T' : 'F'
				if ( elmt.value != tv ) {
   				wrong(elmt);
				}
		}
	
 		else {
 			wrong(elmt);
		}
	}}
}    
// onFocus event handler.  To be used when auto entry is requried.
function change(x) {
if (document.f.Tvalue[0].checked)  {
	if ( x.value == 't' ) {
   	x.value = 'f';
   	x.blur();
	}
	else {
   x.value="t";
		x.blur()}
}
else if ( document.f.Tvalue[1].checked ) {
   if ( x.value == 'f' ) {
   	x.value = 't';
   	x.blur();
	}
	else {
   x.value="f";
		x.blur()
		}
	}
}

// This next function draws the truth table in the top frame.  "c1|3" is the name for the text input box for the second row (row 1) of the number 3 sentence element.
function drw(text) {
	parent.tabl.document.open();
	parent.tabl.document.write('<html><head><title>Truth Table</title></head><body><form name="f"><table border="1"><tr>')
	for ( var i=0; i<atomComp.length; i++ ) {
   	parent.tabl.document.write('<td>' + atomComp[i] + '</td>')
	}
	parent.tabl.document.write('<td></td>')
	for ( var i=0; i<sentComp.length; i++ ) {
   	if ( sentComp[i] != null ) {
   		parent.tabl.document.write('<td>' + sentComp[i] + '</td>')
		}
	}
	parent.tabl.document.write('</tr>')
	
for ( var i=0; i<l; i++ ) {
	parent.tabl.document.write('<tr>')
  for ( var j=0; j<atomComp.length; j++ ) {
   	parent.tabl.document.write('<td>' + a[atomComp[j] + i] + '</td>')
	}
	parent.tabl.document.write('<td></td>')
	for ( var j=0; j<sentComp.length; j++ ) {
		if (  sentComp[j] == ',' || sentComp[j] == '/' ) {
   		parent.tabl.document.write('<td></td>')
		}
   	else if ( sentComp[j] != null ) {
   		parent.tabl.document.write('<td><input type="text" name="c'  + i + '|' + j + '" size="1" maxlength="1" onFocus="parent.frames[1].change(this)" onChange="parent.frames[1].check(this)"></td>')
		}
	}
parent.tabl.document.write('</tr>')
}
	parent.tabl.document.write('</table></form></body></html>')
	parent.tabl.document.close();
}

