/**
 * Encapsulate XMLHTTPRequest, handle request queueing
 * Loads the given url into the div with the given id
 */

function CFW_HTTPRequest(url, div_id) {

	this.url = url;
	this.div_id = div_id;

	/*
	 * Callback function after completion
	 */
	this.notify = null;

	if (!CFW_HTTPRequest.req) {
		if (window.XMLHttpRequest) {
			// branch for native XMLHttpRequest object
			CFW_HTTPRequest.req = new XMLHttpRequest();
			CFW_HTTPRequest.ie = 0;
		} else if (window.ActiveXObject) {
			// branch for IE/Windows ActiveX version
			CFW_HTTPRequest.req = new ActiveXObject("Microsoft.XMLHTTP");
			CFW_HTTPRequest.ie = 1;
		}
	}

	if (!CFW_HTTPRequest.waiting) {
		CFW_HTTPRequest.waiting = new Array(10);
		window.setInterval("cfw_scheduledretry();", 1000);
	}

	/*
	 * Run the request or queue it if another is running
	 */
	this.run = function() 
        {
		with (CFW_HTTPRequest) {

			if (req.readyState != 4 && req.readyState != 0) {
				CFW_HTTPRequest.waiting.push(this);
				return false;
			}

			if (ie) {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}

			var cfwreqThis = this;
			req.onreadystatechange = function() { cfwreqThis.handle() };

			req.open("GET", this.url, true);
			if (ie) {
				req.send();
			} else {
				req.send(null);
			}

			return true;
		}

	}

	/*
	 * XMLHTTPRequest callback function for onreadystatechange
	 */
	this.handle = function() {
		with (CFW_HTTPRequest) {
			if (req.readyState == 4) {
				// only if "OK"
				if (req.status == 200) {
					if (this.div_id) {
						div_node = document.getElementById(this.div_id);
						if (div_node) {
							div_node.innerHTML = "";
							/*content = req.responseText;
							div_node.innerHTML = content;*/
							div_node.innerHTML = req.responseText;
						}
					}
					if (this.notify) {
						this.notify();
					}
				} else {
					// alert("There was a problem retrieving the XML data:\n" +
					//    req.statusText);
				}

			} else {
				// alert("Status: " + req.readyState);
			}
		}
	}
}

/*
 * Look for unhandled requests in CFW_HTTPRequest queue
 */
function cfw_scheduledretry() {
	while (CFW_HTTPRequest.waiting.length > 0 && (CFW_HTTPRequest.req.readyState == 0 || CFW_HTTPRequest.req.readyState == 4)) {
		newobj = CFW_HTTPRequest.waiting.shift();
		if (newobj) {
			newobj.run();
			return;
		}
	}
}

/**
 * URL-encode a string
 */
function cfw_urlencode(str) {
	// return escape(str).replace(/\+/g, '%2C').replace(/\"/g,'%22').replace(/\'/g, '%27');
	return encodeURI(str);
}


/**
 * Calculate the absolute position of an element
 */
function cfw_calculatePosition(obj) {
	var curleft = 0;
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	} else if (obj.x || obj.y) {
		curleft += obj.x;
		curtop += obj.y;
	}

	ret = new Array(2);
	ret[0] = curleft;
	ret[1] = curtop;

	return ret;
}

/**
 * Display div named "name" just after obj
 */
function cfw_showdiv(obj, name) {
	return cfw_showdiv(obj, name, 1);
}
function cfw_showdiv(obj, name, z) {
	pos = cfw_calculatePosition(obj);

	div = document.getElementById(name);

	div.style.left = String(pos[0]) + 'px';
	div.style.top = String(pos[1] + 15) + 'px';
	div.style.zIndex = z;
	div.style.visibility = "visible";
}

/**
 * Hide div named "name"
 */
function cfw_hidediv(name) {
	div = document.getElementById(name);
	div.style.visibility = "hidden";
}

/**
 * Collect a list of selected checkboxes
 */
function cfw_collectselection(name) {
        node_lst = document.getElementsByTagName("input");
        to_del = Array(1);
        for (i = 0; i < node_lst.length; i++) {
                node = node_lst.item(i);
                if (node.getAttribute("type") == "checkbox" && node.getAttribute("name") == name && node.checked) {
                        to_del.unshift(node.getAttribute("value"));
                }
        }

        buf = '';
        for (i = 0; i < to_del.length; i++) {
                if (to_del[i]) {
                        buf += ":" + to_del[i];
                }
        }
        return buf;
}
