//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.moveUp=function (sel) {return xmlReq(this.dsn,"moveUp",sel);}
	this.moveDown=function (sel) {return xmlReq(this.dsn,"moveDown",sel);}
	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;
}

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");
//--- 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(base_dom+folder+"site/xserver.asp");
		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);
			if (res.selectSingleNode("/xmlres/@status").text=="0")
				return 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("site");
			}
			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;
}

