function getCookieVal (offset)
{
	var endstr = document.cookie.indexOf (";", offset)
	if (endstr == -1)
		endstr = document.cookie.length;
	return document.cookie.substring(offset, endstr);
}

function GetCookie (name)
{
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;

	var i = 0;
	while (i < clen)
	{
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0)
			break;
	}
	return null;
}

function DeleteCookie (name)
{
	var exp = new Date();
	var cval = GetCookie (name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

//value must be escaped beforehand!
function SetCookie (name, value, days)
{
	expires = new Date();
	expires.setTime(expires.getTime() + (24 * 60 * 60 * 1000 * days) ); // 1 day
	document.cookie = name + "=" + value + "; expires=" + expires.toGMTString();
	//debug
	//document.getElementById('debug').innerHTML=name + "=" + value + "; expires=" + expires.toGMTString();
	//alert(value)
}





//=============================================================================================
// PUBLIC METHODS

// the cookie will look something like this:
// sqlid¬|datestarted¬18:16,09.01.1974|datefinished¬19:20,09.01.1974|organisation¬jimmy|a0¬L7,H1,M1...
// where ¬ means =        and | means &
	
	// return a cookie as an array
	function loadcookie(name) {
		var arr=[]
	
		var cookie=GetCookie(name);
		if (cookie==null) return []
	
		// break each name:value pair into an array
		var separated_values = cookie.split("|");
	
		// loop through the list of name:values and load
		// up the associate array
		for (var loop = 0; loop < separated_values.length; loop++) {
	
			var broken_info = separated_values[loop].split("¬");
			arr[unescape(broken_info[0])] = unescape(broken_info[1]);
		}
		return arr;
	}
	
	
	// save an array as a cookie
	function savecookie(name,arr,days) {
		var pairs=new Array();
		for (var i in arr) {
			pairs.push(escape(i)+  '¬'  +escape(arr[i]));
		}
		var str=pairs.join('|');
		SetCookie(name,str,days);
	}





	// start with a new cookie
	function cookie_reset() {
		var state={
			sqlid: '',
			datestarted: '',
			datefinished: '',
			organisation: '',
			wizard: '',
			tmp: '',
			answers: '',
			a0: '', a1: '', a2: '',a3: '',a4: '',a5: '',a6: '',a7: '',a8: '',
			myact0: '', myact1: '', myact2: '', myact3: '', myact4: '', myact5: '', myact6: '', myact7: '', myact8: '',
			teamact0: '', teamact1: '', teamact2: '', teamact3: '', teamact4: '', teamact5: '', teamact6: '', teamact7: '', teamact8: ''
		}
		savecookie(db.id,state,365)
	}

	function cookie_setorganisation(name) {
		var state=loadcookie(db.id)
		state.organisation=name
		savecookie(db.id,state,365)
	}

	function getnow() {
		var now=new Date()
		var minutes = ((now.getMinutes() < 10) ? ":0" : ":") + now.getMinutes();
		return now.getHours()+":"+minutes+","+now.getDate()+"."+(now.getMonth() + 1)+"."+now.getFullYear()
	}
	function cookie_setdatestarted(text) {
		if (text==undefined) text=getnow()
		var state=loadcookie(db.id)
		state.datestarted=text
		savecookie(db.id,state,365)
	}
	function cookie_setdatefinished(text) {
		if (text==undefined) text=getnow()
		var state=loadcookie(db.id)
		state.datefinished=text
		savecookie(db.id,state,365)
	}
	function cookie_setwizard(id) {
		var state=loadcookie(db.id)
		state.wizard=id
		savecookie(db.id,state,365)
	}
	function cookie_setwizquestion(id) {
		var state=loadcookie(db.id)
		state.wizquestion=id
		savecookie(db.id,state,365)
	}
	function cookie_setanswers(criterion,a) {
		var state=loadcookie(db.id)
		state['a'+criterion]=a
		savecookie(db.id,state,365)
	}
	function cookie_setactions(criterion,type,actions) {
		var state=loadcookie(db.id)
		state[type+'act'+criterion]=actions
		savecookie(db.id,state,365)
	}
	function cookie_settmp(val) {
		var state=loadcookie(db.id)
		state.tmp=val
		savecookie(db.id,state,365)
	}
	function cookie_setsqlid(val) {
		var state=loadcookie(db.id)
		state.sqlid=val
		savecookie(db.id,state,365)
	}
	function cookie_setcookie(val) {
		SetCookie(db.id,val,365)
	}

	function cookie_exists(name) {
		var state=loadcookie(name)
		if (state.datestarted==undefined) return false
		return true
	}


	//===========================================================
	// THE MAJOR COOKIE FUNCTION, RUN ON MOST PAGES
	// copy all the data in the cookie into the database,
	// and return the cookie as an associative array.
	function importcookietodb(db) {
		var state=loadcookie(db.id)

		// import all the answers
		for (var i=0; i<db.criteria.length; i++) {
			if ((typeof(state['a'+i])=='string') && (state['a'+i].length>0)) {
				stringtoanswers(i, state['a'+i])
			}
		}

		// and all the actions
		for (var i=0; i<db.criteria.length; i++) {

			if (typeof(state['myact'+i])=='string') {
				stringtoactions(i, 'my', state['myact'+i])
			}
		}
		for (var i=0; i<db.criteria.length; i++) {
			if (typeof(state['teamact'+i])=='string') {
				stringtoactions(i, 'team', state['teamact'+i])
			}
		}

		return state
	}


