var net = new Object();
net.READY_STATE_UNINITIALIZED	= 0;
net.READY_STATE_LOADING			= 1;
net.READY_STATE_LOADED			= 2;
net.READY_STATE_INTERACTIVE		= 3;
net.READY_STATE_COMPLETE		= 4;

//	CONTENTLOADER Object
net.ContentLoader = function(url,onload,onerror,method,params,contentType) {
	this.url = url;
	this.req = null;
	this.onload = onload;
	this.onerror = ( onerror) ? onerror : this.defaultError;
	this.loadXMLDoc(url,method,params,contentType);
}
net.ContentLoader.msgId = 1;
net.ContentLoader.prototype = {
	loadXMLDoc:function(url,method,params,contentType) {
		if ( !method )
			method = "GET";
		if ( method == "GET" ) {
			var dts = new Date().getTime();
			if ( url.indexOf("?") != -1 )
				url += "&dts=" + dts;
			else
				url += "?dts=" + dts;
		}
		if ( method == "POST" && !contentType )
			contentType = "application/x-www-form-urlencoded";
		if ( window.XMLHttpRequest )
			this.req = new XMLHttpRequest();
		else if ( window.ActiveXObject )
			this.req = new ActiveXObject("Microsoft.XMLHTTP");
		if ( this.req ) {
			try {
				var loader = this;
				this.req.onreadystatechange = function() {
					loader.onReadyState.call(loader);
				}
				this.req.open(method,url,true);
				if ( contentType )
					this.req.setRequestHeader("Content-Type", contentType);
				net.ContentLoader.msgId++;
				this.req.send(params);
			}
			catch ( err ) {
				this.onerror.call(this);
			}
		}
	},
	onReadyState:function() {
		var req = this.req;
		var ready = req.readyState;
		if ( ready == net.READY_STATE_COMPLETE ) {
			var httpStatus = req.status;
			if ( httpStatus == 200 || httpStatus == 0 ) {
				this.onload.call(this);
				if ( this.notification )
					this.notification.clear();
			}
			else
				this.onerror.call(this);
		}
	},
	defaultError:function() {
		var msgTxt = "error fetching data!" +
					 "<ul><li>readyState: " + this.req.readyState + "</li>" +
					 "<li>status: " + this.req.status + "</li>" +
					 "<li>headers: " + this.req.getAllResponseHeaders() + "</li>" +
					 "</ul>";
		if ( this.notification )
			this.notification.clear();
		this.notification = new msg.Message("net_err00" + net.ContentLoader.msgId, msgTxt, msg.PRIORITY_DEFAULT.id);
		net.contentLoader.msgId++;
	}
}
//	COMMANDQUEUE Object
net.CommandQueue = function(id,url,freq) {
	this.id = id;
	net.cmdQueues[id] = this;
	this.url = url;
	this.queued = new Array();
	this.sent = new Array();
	if ( freq )
		this.repeat(freq);
}
net.CommandQueue.prototype.addCommand = function(command) {
	if ( this.isCommand(command) )
		this.queue.append(command,true);
}
net.CommandQueue.prototype.fireRequest = function() {
	if ( this.queued.length == 0 )
		return;
	var data = "data=";
	for ( var i=0; i < this.queued.length; i++ ) {
		var cmd = this.queued[i];
		if ( this.isCommand(cmd) ) {
			data += cmd.toRequestString();
			this.sent[cmd.id] = cmd;
		}
	}
	this.queued = new Array();
	this.loader = new net.ContentLoader(this.url,net.CommandQueue.onload,net.CommandQueue.onerror,"POST",data);
}
net.CommandQueue.prototype.isCommand = function(obj) {
	return (obj.implementsProp("id") && obj.implementsFunc("toRequestString") && obj.implementsFunc("parseResponse"));
}
net.CommandQueue.prototype.onload = function(loader) {
	var xmlDoc = net.req.responseXML;
	var elDocRoot = xmlDoc.getElementsByTagName("commands")[0];
	if ( elDocRoot ) {
		for ( i=0; i < elDocRoot.childNodes.lenght; i++ ) {
			elChild = elDocRoot.childNodes[i];
			if ( elChild.nodeName == "command" ) {
				var attrs = elChild.attributes;
				var id = attrs.getNamedItem("id").value;
				var command = net.commandQueue.sent[id];
				if ( command )
					command.parseResponse(elChild);
			}
		}
	}
}
net.CommandQueue.onerror = function(loader) {
	alert("Problem sending the data to the server");
}
net.CommandQueue.prototype.repeat = function(freq) {
	this.unrepeat();
	if ( freq > 0 ) {
		this.freq = freq;
		var cmd = "net.cmdQueues[" + this.id + "].fireRequest()";
		this.repeater = setInterval(cmd,freq*1000);
	}
}
net.CommandQueue.prototype.unrepeat = function() {
	if ( this.repeater )
		clearInterval(this.repeater);
	this.repeater = null;
}
net.CreatePostString = function(frm,arr) {
	var poststr = "";
	var arrLen = arr.length;
	var fobj = frm;
	if ( arr ) {
		for ( var i = 0; i < arrLen; i++ ) {
			if ( xGetElementById(arr[i]) && xGetElementById(arr[i]).type ) {
				if ( poststr != "" ) poststr += "&";
				switch ( xGetElementById(arr[i]).type ) {
					case "checkbox" :
						break;
					case "radio" :
						if ( fobj.elements[arr[i]] ) {
							var vals = "";
							if ( fobj.elements[arr[i]] ) {
								var el = fobj.elements[arr[i]];
								for ( j = 0; j < el.length; j++) {
									if ( el[j].checked )
										vals = vals + fobj.elements[arr[i]][j].value + ',';
								}
								if ( vals.length > 0 )
									poststr += arr[i] + "=" + encodeURI(vals.substr(0,vals.length -1));
							}
						}
						break;
					case "select-one" :
						if ( fobj.elements[arr[i]] ) {
							var vals = "";
							for ( j = 0; j < xGetElementById(arr[i]).length; j++ ) {
								if ( xGetElementById(arr[i])[j].selected )
									vals = vals + fobj.elements[arr[i]][j].value + ',';
							}
							if ( vals.length > 0 )
								poststr += arr[i] + "=" + encodeURI(vals.substr(0,vals.length -1));
						}
						break;
					default :
						poststr += arr[i] + "=" + encodeURI(xGetElementById(arr[i]).value);
						break;
				}
			}
		}
	}
	return poststr;
}
net.CleanResponse = function(str) {
	str = str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
	return str.replace(new RegExp('\\n','g'),'').replace(new RegExp('\\r','g'),'');
}
