function translateToPirate() {
	document.forms['pirate_translator'].pirate.value = 'Translating...';
	sendForm('http://pirate.monkeyness.com/cgi-bin/translator.pl', translateToPirateFinish, document.forms['pirate_translator']);
}

function translateToPirateFinish(_data) {
	document.forms['pirate_translator'].pirate.value = _data.pirateAPI.pirate;
}

/*
=====================================================
Common Routines
=====================================================
*/

reqList = new Array();

function sendForm(_url, _function, _form) {
	var args = new Array();
	for (var field = 0; field < _form.elements.length; field++) {
		if (_form.elements[field].name) {
			if (_form.elements[field].type == 'checkbox') {
				if (_form.elements[field].checked) {
					args.push(encodeURIComponent(_form.elements[field].name) + '=1');
				} else {
					args.push(encodeURIComponent(_form.elements[field].name) + '=');
				}
			} else {
				args.push(encodeURIComponent(_form.elements[field].name) + '=' + encodeURIComponent(_form.elements[field].value));
			}
		}
	}
	var query_string = args.join('&');
	sendRequest(_url, _function, _form, query_string);
}


function sendRequest(_url, _function, _object, _data) {
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		var req = new XMLHttpRequest();
		req.processFunction = _function;
		req.callingObject = _object;
		req.url = _url;
		req.onreadystatechange = processRequest;
//		debug('GET ' + _url);
		if (_data) {
			req.open("POST", _url, true);
			req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			req.send(_data);
		} else {
			req.open("GET", _url, true);
			req.send(null);
		}
		reqList.push(req);
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		var req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = processRequest;
			if (_data) {
				req.open("POST", _url, true);
				req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				req.send(_data);
			} else {
				req.open("GET", _url, true);
				req.send(null);
			}
//			req.processFunction = _function;
//			req.callingObject = _object;
			reqList.push(req);
		}
	}
}


function processRequest() {
//	only if req shows "loaded"
	var complete = false;
	for (var i = 0; i < reqList.length; i++) {
		var req = reqList[i];
//		debug('--> check ' + req.url);
		if (req.complete) {
			complete = true;
		} else if (req.readyState == 4) {
//			debug('----> ready 4 ' + req.url);
			// only if "OK"
			if (req.status == 200) {
				req.complete = true;
				complete = true;
				var results = req.responseText;
				var response = convertXMLtoObj(req.responseXML, 0);
//				debug('------> 200: processRequest(' + req.url + ')');
//				debug(printObject(response, 0));
				if (window.ActiveXObject) {
					translateToPirateFinish(response);
				} else {
					req.processFunction(response, req.callingObject);
				}
			} else {
				alert("There was a problem retrieving the XML data:\n" + req.statusText);
			}
		}
	}
	if (complete) {
		var tmpArray = new Array();
		for (var j = 0; j < reqList.length; j++) {
			if (!reqList[j].complete) {
				debug('--> keeping ' + reqList[j].url);
				tmpArray.push(reqList[j]);
			}
		}
		reqList = tmpArray;
	}
}


function convertXMLtoObj(_xml, _depth) {
	var xml = _xml;
	var hash = new Object();
	var tabs = '';
	for (var tab = 0; tab < _depth; tab++) { tabs += '- '; }
	if (typeof xml == 'object') {
		var cnt = 0;
		for (var a = 0; a < xml.childNodes.length; a++) {
			var name = xml.childNodes[a]['nodeName'];
			if (xml.childNodes[a]['nodeName'] == '#text') {
			} else if (typeof xml.childNodes[a] == 'object') {
				var type = xml.childNodes[a]['type'];
				if (type == 'array') {
					if (!hash[name]) {
//						debug(tabs + name);
						if ((typeof xml.childNodes[a] == 'object') && (xml.childNodes[a].childNodes.length > 1)) {
							var value = xml.childNodes[a].firstChild.data;
//							debug(tabs + '- |' + cnt + '| ' + value + ' (' + typeof xml.childNodes[a] + ', ' + xml.childNodes[a].childNodes.length + ')');
							hash[name] = [convertXMLtoObj(xml.childNodes[a], Number(_depth) + 2)];
						} else {
							var value = xml.childNodes[a].firstChild.data;
//							debug(tabs + '- |' + cnt + '| ' + value);
							hash[name] = [value];
						}
					} else {
						if ((typeof xml.childNodes[a] == 'object') && (xml.childNodes[a].childNodes.length > 1)) {
							var value = xml.childNodes[a].firstChild.data;
//							debug(tabs + '- [' + cnt + '] ' + value + ' (' + typeof xml.childNodes[a] + ', ' + xml.childNodes[a].childNodes.length + ')');
							hash[name].push(convertXMLtoObj(xml.childNodes[a], Number(_depth) + 2));
						} else {
							var value = xml.childNodes[a].firstChild.data;
//							debug(tabs + '- [' + cnt + '] ' + value);
							hash[name].push(value);
						}
					}
					cnt++;
				} else if ((typeof xml.childNodes[a] == 'object') && (xml.childNodes[a].childNodes.length > 1)) {
					var value = '';
					if (xml.childNodes[a] && xml.childNodes[a].childNodes[0]) { value = xml.childNodes[a].childNodes[0]['data']; }
//					debug(tabs + name + ': ' + value + ' (' + typeof xml.childNodes[a] + ', ' + xml.childNodes[a].childNodes.length + ')');
					hash[name] = convertXMLtoObj(xml.childNodes[a], Number(_depth) + 1);
					cnt++;
				} else {
					var value = '';
					if (xml.childNodes[a] && xml.childNodes[a].childNodes[0]) { value = xml.childNodes[a].childNodes[0]['data']; }
//					debug(tabs + name + ': ' + value);
					hash[name] = value;
					cnt++;
				}
			}
		}
	}
	return hash;
}


function displayPNG(_url, _width, _height, _alt) {
	var isWinIE = false;
	var IEvers = 0;
	if (navigator.userAgent) {
		var index = navigator.userAgent.indexOf("MSIE");
		if (index != -1) {
			isWinIE = true;
			IEvers = parseFloat(navigator.userAgent.substring(index+5));
		}
	}
		
	if (isWinIE) {
		document.write('<img src="http://www.sitemason.com/images/trans.gif" width="'+_width+'" height="'+_height+'" border="0" alt="'+_alt+'" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+_url+'\', sizingMethod=\'scale\')" />');
	} else {
		document.write('<img src="'+_url+'" width="'+_width+'" height="'+_height+'" border="0" alt="'+_alt+'" />');
	}
}

function displayButton(_url, _function, _label) {
	var isWinIE = false;
	var IEvers = 0;
	if (navigator.userAgent) {
		var index = navigator.userAgent.indexOf("MSIE");
		if (index != -1) {
			isWinIE = true;
			IEvers = parseFloat(navigator.userAgent.substring(index+5));
		}
	}
	
	var buttonClass = 'button';
	if (isWinIE) { buttonClass = 'buttonIE'; _url = '#'; }
	document.write('<a class="'+buttonClass+'" href="'+_url+'" onclick="javascript:'+_function+'; return false;">'+_label+'</a>');
}


