//debug
function dump(o) {
	if (typeof(o)=="object") {
		var t="";
		var c=0;
		for(var n in o) {
			c++;
			t+=n+": "+o[n];
			if (c>4) {
				c=0;
				t+="\n";
			} else t+="\t";
		}
		alert(t);
	} else alert(o);
}

function xdb(n) {
	this.dsn=n;
	//read functions
	this.getNode=function(sel) {return xmlReq(this.dsn,"get",sel);}
	this.getText=function(sel) {return xmlReq(this.dsn,"getText",sel).xml;}
	this.getNodes=function(sel,tag) {return xmlReq(this.dsn,"getNodes",sel,"",tag?tag:"xql").xml;}
	// write functions
	this.setAttr=function(sel,dados) {return xmlReq(this.dsn,"setAttr",sel,dados);}
	this.langSync_setAttr=function(sel,dados) {return xmlReq(this.dsn,"langSync_setAttr",sel,dados);}
	this.setText=function(sel,dados)  {return xmlReq(this.dsn,"setText",sel,"<![CDATA["+dados+"]]>");}
	this.langSync_setText=function(sel,dados)  {return xmlReq(this.dsn,"langSync_setText",sel,"<![CDATA["+dados+"]]>");}
	this.replaceNode=function(sel,dados) {return xmlReq(this.dsn,"replaceNode",sel,dados);}
	this.setNode=function(sel,dados) {return xmlReq(this.dsn,"setNode",sel,dados);}
	this.overNode=function(sel,dados) {return xmlReq(this.dsn,"overNode",sel,dados);}
	this.insertBefore=function(sel,dados) {return xmlReq(this.dsn,"insertBefore",sel,dados);}
	this.insertAfter=function(sel,dados) {return xmlReq(this.dsn,"insertAfter",sel,dados);}
	this.insertNode=function(sel,dados) {return xmlReq(this.dsn,"insertNode",sel,dados);}
	this.appendNode=function(sel,dados) {return xmlReq(this.dsn,"appendNode",sel,dados);}
	this.removeNode=function(sel) {return xmlReq(this.dsn,"removeNode",sel);}
	this._removeNode=function(sel) {return xmlReq(this.dsn,"_removeNode",sel);}
	this.moveUp=function (sel) {return xmlReq(this.dsn,"moveUp",sel);}
	this._moveUp=function (sel) {return xmlReq(this.dsn,"_moveUp",sel);}
	this.moveDown=function (sel) {return xmlReq(this.dsn,"moveDown",sel);}
	this._moveDown=function (sel) {return xmlReq(this.dsn,"_moveDown",sel);}
	this.moveNode=function (src,dest) {return xmlReq(this.dsn,"moveNode",src,"<![CDATA["+dest+"]]>");}
	this.aquireId=function (sel) {return xmlReq(this.dsn,"aquireId",sel);}
	this.delMedia=function (sel) {return xmlReq(this.dsn,"delMedia",sel);}
	this.imgInfo=function (sel) {return xmlReq(this.dsn,"imgInfo",sel);}
	return this;
}

function get_dsn(e) {
	e=unescape(e);
    var dsn=site;
	if (e.substr(0,1)=="#") {
	    var t=e.indexOf(":");
	    dsn=e.substr(1,t-1);
	    return dsns[dsn]?dsns[dsn]:site;
    }
    return site;
}

function getNode(sel) {return get_dsn(sel).getNode(sel);}
function getText(sel) {return get_dsn(sel).getText(sel);}
function getNodes(sel,tag) {return get_dsn(sel).getNodes(sel,tag);}
	// write functions
function setAttr(sel,dados) {return get_dsn(sel).setAttr(sel,dados);}
function langSync_setAttr(sel,dados) {return get_dsn(sel).langSync_setAttr(sel,dados);}
function setText(sel,dados)  {return get_dsn(sel).setText(sel,dados);}
function langSync_setText(sel,dados)  {return get_dsn(sel).langSync_setText(sel,dados);}
function replaceNode(sel,dados) {return get_dsn(sel).replaceNode(sel,dados);}
function setNode(sel,dados) {return get_dsn(sel).setNode(sel,dados);}
function overNode(sel,dados) {return get_dsn(sel).overNode(sel,dados);}
function insertBefore(sel,dados) {return get_dsn(sel).insertBefore(sel,dados);}
function insertAfter(sel,dados) {return get_dsn(sel).insertAfter(sel,dados);}
function insertNode(sel,dados) {return get_dsn(sel).insertNode(sel,dados);}
function appendNode(sel,dados) {return get_dsn(sel).appendNode(sel,dados);}
function removeNode(sel) {return get_dsn(sel).removeNode(sel);}
function _removeNode(sel) {return get_dsn(sel)._removeNode(sel);}
function moveUp (sel) {return get_dsn(sel).moveUp(sel);}
function _moveUp (sel) {return get_dsn(sel)._moveUp(sel);}
function moveDown (sel) {return get_dsn(sel).moveDown(sel);}
function _moveDown (sel) {return get_dsn(sel)._moveDown(sel);}
function aquireId (sel) {return get_dsn(sel).aquireId(sel);}
function delMedia (sel) {return get_dsn(sel).delMedia(sel);}
function imgInfo (sel) {return get_dsn(sel).imgInfo(sel);}

var bkoff=new xdb("bkoff");
var ctrl=new xdb("ctrl");
var log=new xdb("log");
var site=new xdb("site");
var news=new xdb("news");
var proto=new xdb("proto");
var db=new xdb("db");//sql?
var dsns={"bkoff":bkoff,"ctrl":ctrl,"log":log,"site":site,"news":news,"proto":proto,"db":db};

//--- core functions for xmlhttp
// tanto quanto possivel para FF e IE
function xmlhttp() {
	if (window.ActiveXObject)
		return new ActiveXObject("Microsoft.XMLHTTP");
	var t=new XMLHttpRequest();
	return t;
}

function loadXML(o,d) {
	if (window.ActiveXObject)
		o.loadXML(d);
	else
		o=new DOMParser().parseFromString(d,"text/xml");
	return o;
}

function xmldom() {
	if (window.ActiveXObject)
		return new ActiveXObject("Microsoft.XMLDOM");
	else
		return document.implementation.createDocument("","",null);
}

//Executa pedido xmlhttp ao url
function getURL(url,method) {
    if (!method) method="GET";
    var web = xmlhttp(); 
    web.open(method, url, 0); 
    web.send("");
	//alert("getUrl.responseText->\n"+web.responseText);
    return web;
}

function getXML(url) {
   //alert("getXML('"+url+"')");
   return loadXML( xmldom(), getURL(url).responseText );
}

//enviar pedidos ao server xml
function xmlReq(dsn,cmd,at,data,tag) {
	//alert("xmlReq 6.0");
	document.body.style.cursor='wait';
	try {
		var req=xmlhttp();
		//alert("calling server:"+base_dom+folder+"site/xserver.asp",false);
		req.open("POST",base_dom+folder+"site/xserver.asp",false);
		var xmlrec=xmldom();
		//b@KOffee 6.0 protocol ---------
		//alert(data);
		xmlrec.async=false;
		xmlrec=loadXML(xmlrec,"<xmlreq ver='6.0'><dsn>"+dsn+"</dsn><cmd>"+cmd+"</cmd><at>"+at+"</at><data>"+data+"</data><tag>"+tag+"</tag></xmlreq>");
		//alert(xmlrec.xml);
		req.send(xmlrec);
		switch (req.status) {
        case 200:break;
		case 302:alert('xclient: não foi possivel enviar o pedido');break;
		default:alert('xclient status:'+req.status+"\n"+xmlrec.xml);
		}
		var res=null;
		if (window.ActiveXObject) {
			res=req.responseXML.documentElement;
		} else {
			res=req.responseXML.firstChild;
		}			
		//alert(""+res);
		document.body.style.cursor='auto';
		if (!res) alert("xclient - xserver devolveu resultado nulo");
		else {
			//alert(res.selectSingleNode("/xmlres/@status").text);
			var resCode=typeof(res.selectSingleNode)=="undefined"?res.attributes.item(1).textContent:res.selectSingleNode("/xmlres/@status").text;
			//alert(resCode);
			if (resCode=="0")
				return res.firstChild;//res.selectSingleNode("/xmlres").firstChild;
			else {
				alert("xserver erro:#"+res.selectSingleNode("/xmlres/@status").text+"\n"+
					res.selectSingleNode("/xmlres").firstChild.text);
				if (res.selectSingleNode("/xmlres/@status").text=="8") navigate(".");
			}
			return null;
		}
		return res;
	} catch(e) {
		document.body.style.cursor='auto';
		alert("client side: "+e.message);
		//throw(e);
	}
	document.body.style.cursor='auto';		
	return null;
}
