/*   js/cookie.js

CREATED 6/6/00, modified 8/25/00
INCLUDES ALL TABLE OBJECT FILES!!!
note that these cookie functions and updating are also in mutiplechoice.js!

Various functions for cookies. "update()" sets records. (It uses "sort"; as does multiplechoice.js...)

"setCookie(name, value, expire)": Sets cookie values.  Expiration date is optional. expire is the number of DAYS. Or: Set it to "now" for no date. Give a number (no quotes) for number of days. Give it "now" for expiraton after current session. Otherwise a date ten years from now is set!

"update()"
"getCookie()", 

"register()" ?

ALSO: tableObj FUNCTIONS ARE INCLUDED
*/

function setCookie(name, value, expire) { // see above for 'expire' options
	var today = new Date()  
	var expires = new Date()   // note dif from expire (no 's')
 if (expire == null || expire == '') {
	expire = "in 10 years!"
	expires.setTime(today.getTime() + 10000*60*60*24*365)
 	}  
 else if (expire == 'today') expire = ''
 else if (expire == 'now') expires = 0
 else {   
	expires.setTime(today.getTime() + 1000*60*60*24*expire)
 }
var g = name + "=" + escape(value)   + ((expire == "") ? "" : ("; expires=" + expires.toGMTString()))
document.cookie = g
}

function getCookie(Name) {   
var search = Name + "="  
 if (document.cookie.length > 0) { // if there are any cookies      
offset = document.cookie.indexOf(search)      
 if (offset != -1) { // if cookie exists          
offset += search.length          // set index of beginning of value        
 end = document.cookie.indexOf(";", offset)          // set index of end of cookie value         
if (end == -1)             end = document.cookie.length        
 return unescape(document.cookie.substring(offset, end))      }    
}}

// update cookie after problem is correct. Also save the current state of the derivation.
function update() {
var exName = document.title
var Name
if (exName.charAt(0)=='Q') {
	Name = 'Ch' + exName.substring(1,exName.indexOf('.'))
	}
else if (exName.indexOf('revEx')>-1) { // review exercise named "3.revExII" or the like
	Name = 'Ch' + exName.substring(0,exName.indexOf('.'))
	exName = exName.substring(exName.indexOf('.') + 1)
	}
else if (exName.charAt(0)=='T') {
	Name = 'Ch' + exName.substring(1,exName.indexOf('.'))
	exName = exName.substring(0,exName.indexOf(' '))
	}
else Name = 'Ch' + exName.substring(0,exName.indexOf('.'))
cookie = getCookie(Name)
	if (cookie == null) cookie=exName
	else if ( cookie.indexOf(exName)>=0 ) { //checks to make sure that the match is not merely partial
		var l = exName.length
		var c = cookie.charAt(cookie.indexOf(exName) + l)
		cookie+=(c != '' && c != ',' ) ? ',' + exName : ''
	}
	else cookie+= ',' + exName
cookie = cookie.split(',').sort().toString()
if (cookie.charAt(0)==',') cookie=cookie.substring(1)
setCookie(Name,cookie,'')
}


// a function to track numbers. Used in symbolizations, derivations, ...
var track = new Array
var count = 0
var affirm = new Array ('Congrats,', 'Good,', 'Well done,', 'Yup,')
function keepTrack(n) { // updates records when all is correct, n is problem number OR OTHER IDENTIFIER (for a derivation, the problem number is not defined. So, each problem is correlated with the goal sentence and the line number where this is reached. This should be unique.
if ( typeof document.der == 'object' ) window.status += ' So, this problem is finished.'  // Adds to the status line when problem is correct.
if ( typeof currProbNum == 'string' ) n = currProbNum
if (typeof totalNum == "undefined") { // defines total number of problems to complete for the given exercise
	if ( typeof cor == 'object' ) totalNum = cor.length
	else if ( typeof prbs != 'undefined') totalNum = prbs.length - 1
	else if ( typeof document.der == 'object') { // counts total number of buttons -- or ANY elements -- with value beginning with "Problem"
		totalNum = 0
		for ( var i=0; i<document.der.elements.length;i++ ) {
			var el = document.der.elements[i].value
   			if ( typeof  el == 'string' && el.indexOf('Problem') == 0 ) totalNum++
			}
				}
	else {
		totalNum = 5
		}
}
if (track[n]==null) { // saves current state for new problems and keeps count (NOT JUST FOR DERIVATIONS -- SEE COUNTER BELOW)
	if (typeof document.der == 'object' ) { // for canned derivations -- to later print finished work
		saveProblem()
		}
	track[n]=true  // THIS FOR ALL PROBLEMS
	count++
	}
		
if (count == 1 && (typeof finishedProbs == 'string') && finishedProbs.indexOf(document.title)<0 ) finishedProbs = document.title + finishedProbs

if (count>=totalNum) {
		update()
		var r = Math.random()
		r *= 3
		r = Math.floor(r)
		var congrats = affirm[r]
		alert( congrats + " ...this exercise, " + document.title + ", is finished.")
		if (typeof finished == 'function') finished()
		}
}

// set a cookie to expire in ONE year.

function register(name, text) {   
var today = new Date()   
var expires = new Date()   
expires.setTime(today.getTime() + 1000*60*60*24*365)
setCookie(name, text, expires)
}


 
