// periboea.js v.1.0 - script by andersen - 2008 
// http://home5.inet.tele.dk/nyboe/javascript/periboea

var xmlHttp, xmlDoc, tagList, httpFlag, theFile, theTag, tmpStr, node;
var msg1 = "Error handling the script";
var msg2 = "Error retrieving XML data";
var tagArr = new Array();

function debug() { return(location.hash == "#debug"); }

function stealth() { return(location.hash == "#stealth"); }

if (debug()) { 
	window.onerror = function handleErr(msg, url, lin) { 
		var txt= msg + "\n";
		if (url) { txt += "URL: " + url + "\n"; }
		if (lin) { txt += "Line: " + lin + "\n\n"; }
		alert(txt); 
		return false; 
	} 
} else if (stealth()) { window.onerror = function () { return true; } }


function importXML(file, flag) {
	xmlDoc = null; 
	xmlHttp = null; 	
	theFile = file;

	if ((flag)
	||(theFile.substring(0,4).toLowerCase() == "http")
	||(location.protocol.substring(0,4).toLowerCase() == "http")) { 
		httpFlag = true;
		httpXMLDoc(theFile); 
	} else { 
		httpFlag = false;
		loadXMLDoc(theFile); 
	}
}


function loadXMLDoc(file) { if (debug()) { alert("xmlDoc.load: "+file); }
	if ((document.implementation)&&(document.implementation.createDocument)) { 
		xmlDoc = document.implementation.createDocument("", "", null);  
		xmlDoc.onload = done;	
	} else if (window.ActiveXObject) { 
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 
		xmlDoc.onreadystatechange = loadStat; 
	} 

	if (xmlDoc != null) {xmlDoc.load(file);} 
	else if (! stealth) { alert(msg1); return; } 
}


function loadStat() { if (xmlDoc.readyState == 4) done(); }


function httpXMLDoc(file) { if (debug()) { alert("xmlHttp.open: "+file); }
	if (window.XMLHttpRequest) { 
		xmlHttp = new XMLHttpRequest(); 
	} else if (window.ActiveXObject) { 
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
	}

 	if (xmlHttp != null) {
		xmlHttp.onreadystatechange = getStat;
		xmlHttp.open("GET", file, true);
		xmlHttp.send(null);
	} else if (! stealth) { alert(msg1); return; } 
}


function getStat() { if(checkReadyState(xmlHttp)) { xmlDoc = xmlHttp.responseXML.documentElement; done(); } }


function checkReadyState(obj) { 
	if(obj.readyState == 4) {
		if(obj.status == 200) { return true; } 
		else { if (! stealth) { alert(msg2); } return; }
	} 
}


function done() { 
	if (debug()){ 
		if (httpFlag) { var tmp = xmlDoc; } else { var tmp = xmlDoc.documentElement; } 
		if (window.ActiveXObject) { alert(tmp.xml); } else { alert(tmp.textContent); } 
	}
	try { xmlDocReady(); } catch(er) { } 
}


function notEmpty(arg) { return ((arg != null)&&(arg != "")&&(arg != undefined)&&(arg != 'undefined')); }


function getListOf(tag) { tagList = xmlDoc.getElementsByTagName(tag); theTag = tag; }


function nameSpace(index, tag) {
	if ((tag.indexOf(":") > -1)&&(! window.ActiveXObject))  { 
		var theLocalName = tag.substring(tag.indexOf(":")+1, tag.length); 
		node=tagList[index].getElementsByTagName(theLocalName);
	} else { node=tagList[index].getElementsByTagName(tag); }
}


function getTag(index, tag) {
	var tmp=""; 
	nameSpace(index, tag); 
	try { tmp=node[0].firstChild.data; } catch(er) { }
	if (notEmpty(tmp)) { return(tmp); } else { return(""); }
}


function getAttr(index, tag, attr) {    
	var tmp="";
	nameSpace(index, tag); 
	for (var j=0; j<node.length; j++) {
		try { tmp = node[j].getAttribute(attr) } catch(er) { } 
	}
	if (notEmpty(tmp)) { return(tmp); } else { return(""); }
}


function getTagByAttrVal(index, tag, attr, attrval) {    
	var tmp="";  
	nameSpace(index, tag); 
	for (var j=0; j<node.length; j++) {
		if (node[j].getAttribute(attr) == attrval) { 
			try { tmp = node[j].firstChild.data; } catch(er) { } 
		} 
	}
	if (notEmpty(tmp)) { return(tmp); } else { return(""); }
}


function getValueByAttrVal(index, tag, attr, attrval, att) {    
	var tmp="";  
	nameSpace(index, tag); 
	for (var j=0; j<node.length; j++) {
		if (node[j].getAttribute(attr) == attrval) { 
			try { tmp = node[j].getAttribute(att) } catch(er) { } 
		} 
	}
	if (notEmpty(tmp)) { return(tmp); } else { return(""); }
}


function asLink(url, trgt, txt) { 
	var tmp = "<a href=\"" + url + "\"";

	if (notEmpty(trgt)) { tmp += "target=\"" + trgt + "\">"; } 
	else { tmp += ">"; }

	if (notEmpty(txt)) { tmp += txt + "</a>"; } 
	else { tmp += url + "</a>"; }
	return(tmp);
}


function asOnClick(func, txt) { 
	var tmp = "<a href=\"#\" onclick=\"" + func + "\">" + txt + "</a>";
	return(tmp);
}


function asImage(url, w, h, b, txt) { 
	var tmp = "<img src=\"" + url + "\""; 
	if (notEmpty(w)) { tmp += " width=\"" + w + "\""; }
	if (notEmpty(h)) { tmp += " height=\"" + h + "\""; }
	if (notEmpty(b)) { tmp += " border=\"" + b + "\""; }
	if (notEmpty(txt)) { tmp += " alt=\"" + txt + "\""; }
	tmp += ">"; 
	return(tmp);
}


function asCell(txt) { 
	var tmp = "<td nowrap>" + txt + "</td>";
	return(tmp);
}


function clearRow(s, delim) { 
	if (notEmpty(s)) { 
		if (notEmpty(delim)) { var dlm = delim; } else { var dlm = "~~~"; } 
		tmpStr = s + dlm; 
	} else { tmpStr = ""; }
}


function addToRow(s, delim) { if (notEmpty(delim)) { tmpStr += s + delim; } else { tmpStr += s + "~~~"; } }


function addRowToArr(delim) {
	tagArr[tagArr.length] = new Array();
	
	if (notEmpty(delim)) { 
		tagArr[tagArr.length-1] = tmpStr.substring(0,tmpStr.length-3).split(delim); 
	} else { tagArr[tagArr.length-1] = tmpStr.substring(0,tmpStr.length-3).split("~~~"); }
}