function Notebook(name) {
    this.name = name;
    this.cookieName = 'nb:' + name;
    this.recordset = new NotebookRecordset();
    this.load();
}

Notebook.DAYS_EXP = 5;

Notebook.prototype.getExpires = function(days) {
	oneday = 1000*3600*24;
	s = 'expires=';
	d = new Date();
	expires = new Date(d.valueOf()+oneday*days);
	s += expires.toUTCString();
	return s;
}

Notebook.prototype.setCookie = function(name, val, days) {
	document.cookie = name + "=" + val + ";" + this.getExpires(days) + ";path=/;";
}

Notebook.prototype.load = function() {
	var cookie = document.cookie;
	var list_cookies = cookie.split(';');
	for(var i=0;i<list_cookies.length;i++) {
		var list_cookie = list_cookies[i].split('=');
		if(list_cookie.length==2) {
			var cookie_name = list_cookie[0];
			if(cookie_name.indexOf(this.cookieName)!=-1) {
				var value = list_cookie[1];
				this.recordset = new NotebookRecordset(value);
			}
		}
	}
}

Notebook.prototype.getRecordset = function() {
	return this.recordset;
}

Notebook.prototype.setRecordset = function(rs) {
	this.recordset = rs;
}

Notebook.prototype.save = function() {
	if(this.recordset==null) {
		alert("save(): Can't save null NotebookRecordset");
	}
	else {
		var cookieValue = this.recordset.serialize();
		this.setCookie(this.cookieName, cookieValue, Notebook.DAYS_EXP);
	}
}

//	class NotebookRecordset
function NotebookRecordset(str) {
	this.records = new Array();	
	this.cursor = 0;

	if(str && str.length>0) {
		var list_records = str.split(NotebookRecordset.RECORDS_DELIM);
		for(var i=0;i<list_records.length;i++) {
		    var record = list_records[i];
			var list_fields = record.split(NotebookRecordset.FIELDS_DELIM);
			var fields = new Array();
			for(var t=0;t<list_fields.length;t++) {
				fields.push(list_fields[t]);
			}

			this.records.push(fields);
		}
	}
}

NotebookRecordset.FIELDS_DELIM = '|';
NotebookRecordset.RECORDS_DELIM = '\\';

NotebookRecordset.prototype.moveFirst = function() {
	this.cursor = 0;
}

NotebookRecordset.prototype.moveNext = function() {
    if(!this.eof()) {
		this.cursor++;
	}
}

NotebookRecordset.prototype.movePrev = function() {
	if(!this.bof()) {
		this.cursor--;
	}
}

NotebookRecordset.prototype.moveLast = function() {
	this.cursor = this.records.length - 1;
}

NotebookRecordset.prototype.getRecordCount = function() {
	return this.records.length;
}

NotebookRecordset.prototype.getFieldCount = function() {
    if(this.records.length>0) {
		return this.records[0].length;
	}
	else {
		return 0;
	}	
}

NotebookRecordset.prototype.item = function(field_idx) {
    if(!this.eof() && !this.bof()) {
		return this.records[this.cursor][field_idx];
	}
	else {
		return null;
	}	
}

NotebookRecordset.prototype.record = function(s) {
    if(!this.eof() && !this.bof()) {
		return this.records[this.cursor].join(s);
	}
	else {
		return null;
	}	
}

NotebookRecordset.prototype.eof = function() {
	return (this.cursor>=this.records.length);
}

NotebookRecordset.prototype.bof = function() {
    return (this.cursor<0);
}

NotebookRecordset.prototype.add = function(rec) {
    if(rec.length!=this.getFieldCount() && this.getFieldCount()>0) {
    	alert('add(): Warning rec.length!=getFieldCount()');
    }
	this.records.push(rec);
}

NotebookRecordset.prototype.removeAt = function(ar, idx) {
	var newar = [];
	for (var i=0;i<ar.length;i++) {
		if (i!=idx) {
			newar.push( ar[i] );
		}
	}
	
	return newar;
}

NotebookRecordset.prototype.remove = function(field_idx, val) {
    for(var i=0;i<this.records.length;i++) {
    	if(this.records[i][field_idx] == val.toString()) {
    		this.records = this.removeAt(this.records, i);
    		i--;
    	}
    }	
}

NotebookRecordset.prototype.serialize = function() {
    var serial = new Array();
    for(var i=0;i<this.records.length;i++) {
        serial.push(this.records[i].join(NotebookRecordset.FIELDS_DELIM));
    }	
    var val = serial.join(NotebookRecordset.RECORDS_DELIM);
	return val;
}

NotebookRecordset.prototype.find = function(field_idx, val, b_first) {
    if(b_first) {
		this.moveFirst();
	}

	var found = false;
	while(!this.eof() || found) {
		if(this.item(field_idx) == val) {
			found = true;
		}
		else {
			this.moveNext();
		}	
	}

	return found;
}

NotebookRecordset.prototype.clear = function() {
	this.records = new Array();
}