function XDB()
{
	var el = document.createElement("SCRIPT");
	el.query = XDB_query;
	el.recordCount = 0;
	el.currentRecord = 0;
	//el.defer = true;
	el.data = new Array();
	el.stoData = new Array(); // enthält die ungeänderten daten wird für update verwendet
	el.open = XDB_open;
	el.move = XDB_move;
	el.moveFirst = XDB_moveFirst;
	el.moveLast = XDB_moveLast;
	el.movePrev = XDB_movePrev;
	el.moveNext = XDB_moveNext;
	el.addNew = XDB_addNew;
	el.remove = XDB_delete;
	el.update = XDB_update;
	el.xevent = new XEvent();
	el.onLoadingComplete = XDB_queryDone;
	el.print = XDB_print
	return el;
}

function XDB_print(msg)
{
	if(this.onPrint)
		this.onPrint(msg);
}

function XDB_accessDB(db, urlParam)
{
	db.src = "/utils/dbaccess.php?objID="+db.uniqueID+"&"+urlParam;
	//db.defer = true;
	db.print(urlParam+","+db.dbHandle);
	if(db.dbHandle)
		db.src += "&dbHandle="+db.dbHandle;
	
	db.queryDone = false;
	void(window.document.getElementsByTagName("head")[0].appendChild(db));
	if(!db.onQueryDone) {
		db.print("create event "+db.onQueryDone);
		db.xevent.Lock(200000);
		
	}
	return db.error == 0;

}
/*
	openParams: database=&host=&user=&password=
	return: true or false
*/
function XDB_open(openParams)
{
	return XDB_accessDB(this, "open=1&"+openParams);
}

function XDB_queryDone()
{
	this.print("XDB_queryDone");
	void(window.document.getElementsByTagName("head")[0].removeChild(this));
	if(this.dataArr) {
		this.recordCount = this.dataArr.length / this.fields.length;
	}
	this.moveFirst();
	//this.xevent.Unlock();
	if(this.onQueryDone)
		this.onQueryDone();
	else
		this.xevent.Unlock();
}

function XDB_query(qry)
{
	
	if(ret = XDB_accessDB(this, "sql="+qry))
		this.moveFirst();
	return ret;
}

function XDB_addNew()
{
	for(var i=0;i<this.fields.length;i++) {
		this.data[this.fields[i].fieldName] = '';//this.dataArr[i+((this.currentRecord-1)*this.fields.length)];
	}
	this.action = "new";
	this.currentRecord = this.recordCount;
}

function XDB_delete()
{
	this.action = "delete";
}

function XDB_updateDone()
{
	if(this.onUpdateDone)
		this.onUpdateDone();
	else
		this.xevent.Unlock();
}

function XDB_update()
{
	var addNew = (this.currentRecord == this.recordCount)
	var qry = "";
	var update = "";
	var where = "";
	var bHasPrimaryKey = false;
	var insertIDKey;
	if(this.updateDB == null) {
		this.updateDB = new XDB;
		this.updateDB.onLoadingComplete = XDB_updateDone;
		this.updateDB.onPrint = this.onPrint;
		this.updateDB.dbHandle = this.dbHandle;
	}
		//this.updateDB.onQueryDone
	
	for(var i = 0; i < this.fields.length; i++) {
		//inp = row.cells[i+cellOffset].firstChild;
		var fieldName = this.fields[i].fieldName;
		this.print("field "+fieldName+" flags="+this.fields[i].flags);
		if(this.fields[i].flags.indexOf("primary_key") >= 0 && this.fields[i].flags.indexOf("auto_increment") >= 0)
			insertIDKey = fieldName;
		if(this.fields[i].flags.indexOf("primary_key") == -1) {
			if(this.data[fieldName] == this.stoData[fieldName])
				continue;
			this.print(fieldName+" vn="+this.data[fieldName]+" vo="+this.stoData[fieldName]);
			if(update.length)
				update+=",";
			update += "`"+fieldName+"`='"+this.data[fieldName].split("'").join("''")+"'";
		} else if(this.action != "new") { // where clause mit primarykeys erstellen
			
			
			if(where.length) 
				where += " and ";
			where += "`"+fieldName+"`='"+this.stoData[fieldName].split("'").join("''")+"'";
		}
	}
			
	this.print("XDB_update "+update+" action="+this.action);
	if(update.length == 0)
		return false;
	if(where.length == 0 && this.action != "new") { // kein primarykey für where
		for(var i = 0; i < this.fields.length; i++) {
			var fieldName = this.fields[i].fieldName;
			if(where.length) 
				where += " and ";
			where += "`"+fieldName+"`='"+this.stoData[fieldName].split("'").join("''")+"'";
		}			
	}
	var tableName = this.fields[0].table;
	switch(this.action) {
	case "new":
		qry = "insert into "+tableName+" set " + update; break;
	case "delete":
		qry = "delete from "+tableName+" where "+where; break;
	default:
		qry = "update "+tableName+" set " + update + " where " + where;
		break;
	}
	//this.print(qry+"call query");
	/*
	owner.updateDB.onQueryDone = function anonymus() { updateDone(row); }
	row.saving = true;
	owner.updateDB.query(qry.split("=").join("%3D"));
		*/
	if(this.updateDB.query(qry.split("=").join("%3D")) == false)
		alert("query failed "+this.updateDB.errorStr);
	else {
		if(this.action == "new") {
			newID = this.updateDB.newID;
			this.updateDB.onLoadingComplete = XDB_queryDone;
			this.updateDB.query("select * from "+tableName+" where "+insertIDKey+"="+newID);
			this.updateDB.onLoadingComplete = XDB_updateDone;
			this.print("id="+newID+" "+this.updateDB.data.Nr);
			this.recordCount++;
			for(key in this.fields) {
				this.dataArr[key+((this.recordCount)*this.fields.length)] = this.updateDB.data[this.fields[key].fieldName];
			}
			//this.moveLast;
			//this.updateDB.query(
			//this.dataArray
		}
		return true;
	}
	return false;
}

function XDB_move(pos)
{
	if(pos < 0 || pos >= this.recordCount)
		return false;
	this.currentRecord = pos;
	for(var i=0;i<this.fields.length;i++) {
		this.data[this.fields[i].fieldName] = this.dataArr[i+((this.currentRecord)*this.fields.length)];
		this.stoData[this.fields[i].fieldName] = this.dataArr[i+((this.currentRecord)*this.fields.length)];
		//this.print(this.fields[i].fieldName+" idx="+(i+((this.currentRecord)*this.fields.length))+" "+this.dataArr[i+((this.currentRecord)*this.fields.length)]);
	}
	return true;
}

function XDB_moveFirst()
{
	return this.move(0);
}

function XDB_moveNext()
{
	return this.move(this.currentRecord+1);
}

function XDB_movePrev()
{
	return this.move(this.currentRecord-1);
}

function XDB_moveLast()
{
	return this.move(this.recordCount-1);
}

