///
///	Notebook
///
function Notebook(name) {
	this.name = name;
	this.records = null;
	this.cursor = 0;
	this.load();
}

Notebook.daysExp = 5;
Notebook.debug = 0;

//*******************************************************************************************
Notebook.trace = function(str) {
    if(Notebook.debug)
		alert(str);
}	

//*******************************************************************************************
Notebook.prototype.setCookie = function(value) {
    setCookie(this.name, value, Notebook.daysExp);
}

//*******************************************************************************************
Notebook.prototype.save = function() {
	this.setCookie(this.serialize());
}

//*******************************************************************************************
Notebook.prototype.load = function() {
    var str = Cookies(this.name);
    this.records = new Array();

	if(str && str.length>0) {
		var records = str.split(':');
		for(var i=0;i<records.length;i++) {
		    var record = records[i];
		    for(var t=0;t<record.length;t++) {
		    	record[t] = unescape(record[t]);
		    }	
			MyArray.push( this.records, record.split('|'));
		}
	}
}

//*******************************************************************************************
Notebook.prototype.moveFirst = function() {
	this.cursor = 0;
}

//*******************************************************************************************
Notebook.prototype.moveNext = function() {
    if(!this.eof())
		this.cursor++;
}

//*******************************************************************************************
Notebook.prototype.movePrev = function() {
	if(!this.bof()) 
		this.cursor--;
}

//*******************************************************************************************
Notebook.prototype.moveLast = function() {
	this.cursor = this.records.length - 1;
}

//*******************************************************************************************
Notebook.prototype.recordCount = function() {
	return this.records.length;
}

//*******************************************************************************************
Notebook.prototype.fieldCount = function() {
    if(this.records.length>0) {
		return this.records[0].length;
	}
	else {
		return 0;
	}	
}

//*******************************************************************************************
Notebook.prototype.item = function(idx) {
    if(!this.eof() && !this.bof()) {
		return this.records[this.cursor][idx];
	}
	else {
		return null;
	}	
}

//*******************************************************************************************
Notebook.prototype.record = function() {
    if(!this.eof() && !this.bof()) {
		return this.records[this.cursor];
	}
	else {
		return null;
	}	
}

//*******************************************************************************************
Notebook.prototype.eof = function() {
	return (this.cursor>=this.records.length);
}

//*******************************************************************************************
Notebook.prototype.bof = function() {
	return (this.cursor<0);
}

//*******************************************************************************************
Notebook.prototype.add = function(record) {
    if(record.length!=this.fieldCount() && this.fieldCount()>0) {
    	Notebook.trace('add(): Warning record.length!=fieldCount()');
    }
	MyArray.push(this.records, record);
	this.save();
}

//*******************************************************************************************
Notebook.prototype.addFields = function() {
    var record = new Array();
    for(var i=0;i<arguments.length;i++) {
    	MyArray.push(record, arguments[i]);
    }

    if(record.length!=this.fieldCount() && this.fieldCount()>0) {
    	Notebook.trace('add(): Warning record.length!=fieldCount()');
    }
	MyArray.push(this.records, record);
	this.save();
}

//*******************************************************************************************
Notebook.prototype.delete_ = function(idx, value) {
    for(var i=0;i<this.records.length;i++) {
    	if(this.records[i][idx] == value) {
    		this.records = MyArray.popIdx(this.records, i);
    		i--;
    	}
    }
    this.save();
}

//*******************************************************************************************
Notebook.prototype.serialize = function() {
    var records = new Array();
    for(var i=0;i<this.records.length;i++) {
        var r = this.records[i];
        var record = new Array();
        for(var t=0;t<r.length;t++) {
        	MyArray.push(record, escape(r[t]));
        }
        MyArray.push(records, record.join('|'));
    }	

 	var s = records.join(':');

    Notebook.trace('serialize():\n\n' + s);

    return s;
}

//*******************************************************************************************
Notebook.prototype.find = function(idx, value, fromFirst) {
    if(fromFirst)
		this.moveFirst();

	var found = 0;
	while(!this.eof() || found) {
		if(this.item(idx) == value) {
			found = 1;
			break;
		}
		else {
			this.moveNext();
		}	
	}

	return found;
}

//*******************************************************************************************
Notebook.prototype.clear = function() {
	this.records = new Array();
	this.save();
}