/*
Deze classe haalt de data op voor een Article
De methode print kun je overrulen indien nodig.
*/
function Viewobj(){
	this.get = function(id) {
		var loc = "viewobj.jsp?id="+id;
		var getData = true;
		var article = new Article();
		if (historyStorage.hasKey(loc)){
			article = historyStorage.get(loc);
			if (this.getClassName(article) == "Article"){
				getData = false;
				parent.viewobj.print(article);
			} else article = new Article(); //reset dat klote Object
		}
		if (getData){
			var req = new XMLHttpRequest();
			if (req) {
		 		req.onreadystatechange = function() {
			    	if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
						var xml = req.responseXML;
						if (xml.documentElement) {
							var root = xml.getElementsByTagName('article')[0];
							article.loadXML(xml);
							article.setTekst(parent.viewobj.anchorTxt(article.getTekst()));
							historyStorage.put(loc,article);
							parent.viewobj.print(article);
						}
			    	}
				}
		  	}
		 	req.open('GET', '/scripts/classes/kmonet/xml/Article.jsp?id='+id+'&noCache='+new Date().getTime());
		 	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=ISO-8859-15');
			req.send(null);
		}	
	}
	this.print = function (article) {
		document.getElementById('viewobjtitel').innerHTML=article.getTitel();
		document.getElementById('viewobjtekst').innerHTML=article.getTekst();
	}
	/* private stuff */
	this.getClassName = function(obj) {
		if (typeof obj != "object" || obj === null) return false;
		return /(\w+)\(/.exec(obj.constructor.toString())[1];
	}
	
	this.getNodeValue = function (el,name){
		if (el.getElementsByTagName(name)[0].firstChild == null) return '';
		else return el.getElementsByTagName(name)[0].firstChild.nodeValue;
	}
	this.anchorTxt = function (txt){
		return this.anchorDefault(txt);
	}
	this.anchorDefault = function (txt){
		//replace "viewobj.jsp?id=xx with "#" onclick="javascript:display(xx)
		// no we are using rsh, so this will trigger an event, so actually all we need to do is add a #
		var href_default = "\"viewobj.jsp?id=";
		var pos = txt.indexOf(href_default);
		var buf = "";
		var id = "";
		while (pos != -1){
			buf+= txt.substring(0,pos);
			txt = txt.substring(pos+16);
			pos = txt.indexOf("\"");
			id = txt.substring(0,pos);
			buf+= "\"#viewobj.jsp?id="+id+"\" ";
			txt = txt.substring(pos);
			pos = txt.indexOf(href_default);
		}
		buf+=txt;
		return buf;
	}
}