function Records () {
	
	this.fieldNames = new Array();
	this.tableName = 'adresses';
	this.recordClass = Adress;
	this.records = null;
	this.lastUpdate = 0;
	this.lastRecordUpdate = 0;
}

Records.prototype.getAllEntries = function(noDeleted) {
	if (this.records) {
		return this.records;
	}
	this.records = new Object();
	
	if (gdb.db) {
		var q = 'select id from ' + this.tableName + ' where name != "" ';
		if (noDeleted) {
			q += ' and not(deleted = "yes") or deleted isnull';
		}
		
		var rs = gdb.db.execute(q);
		var es = new Object;
		while (rs.isValidRow()) {
			var id = rs.fieldByName('id');
			var r = this.getById(id);
			es[id] = r;
			rs.next();
		}
		rs.close();
	
	this.records = es;
	}
	return this.records;
}

Records.prototype.getAllEntriesAsArray =  function(since, noDeleted) {
	
	var recs = this.getAllEntries(noDeleted);
	var es = new Array();
	
	for (var i in recs) {
		if (!since || since < recs[i].fields['changed']) {
			es.push(recs[i].fields);
		} 
	}
	return es;
}

Records.prototype.deleteAllEntries = function() {
	this.records = new Object();
	if (gdb.db) {	
		var rs = gdb.db.execute("delete  from " + this.tableName);
		rs.close();
	}
	return true;
}

Records.prototype.getById = function(id) {
	
	if (this.records && this.records[id]) {
		
	} else {
		var r = new  this.recordClass();
		r.getById(id);
		this.records[id] = r;
	}
	return this.records[id];
}

Records.prototype.getMaxLastChanged = function() {
	if (gdb.db) {	
		var rs = gdb.db.execute('select max(changed) from ' + this.tableName);
		if(rs.isValidRow()) {
			this.lastRecordUpdate = rs.field(0);
		}
		rs.close();
	}
	
	return this.lastRecordUpdate;
}

Records.prototype.setLastUpdate = function(date) {
	
	this.lastUpdate = date;
	if (gdb.db) {
		var rs = gdb.db.execute('delete from  ' + this.tableName + '_last');
		rs.close();
		rs = gdb.db.execute('insert into ' + this.tableName + '_last (changed) values (?)',[date]);
		rs.close();
	} 
	
	
	
	
}


Records.prototype.getLastUpdate = function(date) {
	if (gdb.db) {
		var rs = gdb.db.execute('select changed from ' + this.tableName + '_last');
		if(rs.isValidRow()) {
			this.lastUpdate = rs.field(0);
			if (this.lastUpdate == null) {
				this.lastUpdate = 0;
			}
		}
		rs.close();
	}
	
	return this.lastUpdate;
}

Records.prototype.setAndSave= function(r,fields) {
	if (fields['changed'] > this.lastRecordUpdate) {
		this.lastRecordUpdate = fields['changed'] ;
	}
	
	
	
	
	r.set(fields);
	r.save();
	this.records[r.fields['id']] = r;
	
}

Records.prototype.add = function(fields) {
	var id = null;
	if (gdb.db) {
		var q = "select id from " + this.tableName + " where ";
		var f = new Array();
		for (var i in this.fieldsUnique) {
			q += this.fieldsUnique[i] + " = ? and ";
			f.push(fields[this.fieldsUnique[i]]);
		}
		if (f.length > 0) {
			q += " 1 = 1";
		
			var rs = gdb.db.execute(q, f);
			if (rs.isValidRow()) {
				id = rs.field(0);
			}
			rs.close();
		}
	}
	if (!id) {
		id = hex_md5(new Date() + " " + Math.random());
	}
	var r = this.getById(id);
	fields['changed'] =Math.floor( new Date().getTime() / 1000);
	this.setAndSave(r,fields);
	
	
	return r;
}


