// JavaScript Document

function smartinfo() {
	
	var  self = this;
	
	this.serverurl = "";
	this.serverrouteurl = "";
	this.reloadinterval = 20000;
	this.timer;
	this.env = new Array();
	this.env["txt_noservice"] = "kein Service";
	this.env["txt_nohst"] = "keine Haltestelle gefunden";
	this.env["txt_routesloading"] = "Bitte warten ...";
	this.env["max_hstselect"] = 10;
	this.env["min_hstselect"] = 10;
	this.env["routetimer"] = null;
	this.env["routeloader"] = null;
	
	this.setenv = function (key, val) {
		this.env[key] = val;
	};
	
	this.setserverrouteurl = function (url) {
		this.serverrouteurl = url;
	};
	
	this.setserverurl = function (url) {
		this.serverurl = url;
	};
	
	this.getserverurl = function() {
		return this.serverurl;
	};
	
	this.setinterval = function(sec) { // Intervall in Sekunden
		this.reloadinterval =  sec *1000;
	};
	
	this.startupdateinterval = function() {
		this.timer = window.setInterval(self.getsmartinfoxml, this.reloadinterval);
	};
	
	this.stopupdateinterval = function() {
		window.clearInterval(this.timer);
	};
	
	this.getsmartinfoxml = function() {
		self.XMLReq = new xmlHttpRequestNode("POST",self.serverurl,true,'',self,'getsmartinfoxmlComplete',false,'settoofflinemode');
	};
	
	this.getsmartinfoxmlComplete = function(XML) {
		this.XMLdata = XML.responseXML;
		this.setupnewsmartinfooutput();
	};
	
	this.setupnewsmartinfooutput = function() {
		//alert("UPDATE");
		if (this.XMLdata != null) {
			var smartinfos = this.XMLdata.getElementsByTagName("smartinfo");
			//outputobj = document.getElementById("sioutput");
			//if (outputobj != undefined) {
				for (var i=0;i<smartinfos.length;i++) {
					pelem = document.getElementById("siroute_"+i);
					if (pelem != undefined) {
						while (celem=pelem.lastChild) pelem.removeChild(celem);
						route = smartinfos[i].getElementsByTagName("route")[0].firstChild.nodeValue;
						routetext = this.cleanVlisData(route).trim();
						if (routetext == "") {
							route_elem = document.createTextNode('\u00a0');
						} else {
							route_elem = document.createTextNode(routetext);
						};
						pelem.appendChild(route_elem);
					};
					pelem = document.getElementById("sidir_"+i);
					if (pelem != undefined) {
						while (celem=pelem.lastChild) pelem.removeChild(celem);
						dir = smartinfos[i].getElementsByTagName("direction")[0].firstChild.nodeValue;
						dirtext = this.cleanVlisData(dir).trim();
						if (dirtext == "") {
							dir_elem = document.createTextNode('\u00a0');
						} else {
							dir_elem = document.createTextNode(dirtext);
						};
						pelem.appendChild(dir_elem);
					};
					pelem = document.getElementById("sitime_"+i);
					if (pelem != undefined) {
						while (celem=pelem.lastChild) pelem.removeChild(celem);
						time = smartinfos[i].getElementsByTagName("time")[0].firstChild.nodeValue;
						timetext = this.cleanVlisData(time).trim();
						if (timetext == "") {
							time_elem = document.createTextNode('\u00a0');
						} else {
							time_elem = document.createTextNode(timetext);
						};		
						pelem.appendChild(time_elem);
					};
				};
			//};
		} else {
			this.settoofflinemode();
		};
	};
	
	this.settoofflinemode = function() {
		outputobj = document.getElementById("sioutput");
		if (outputobj != undefined) {
			l = 0;
			while (trobj = document.getElementById('sirow_'+l)) {
				route_elem = document.createTextNode('\u00a0');
				dir_elem = document.createTextNode('\u00a0');
				time_elem = document.createTextNode('\u00a0');
				pelem = document.getElementById("siroute_"+l);
				while (celem=pelem.lastChild) pelem.removeChild(celem);
				pelem.appendChild(route_elem);
				pelem = document.getElementById("sidir_"+l);
				while (celem=pelem.lastChild) pelem.removeChild(celem);	
				pelem.appendChild(dir_elem);
				pelem = document.getElementById("sitime_"+l);
				while (celem=pelem.lastChild) pelem.removeChild(celem);	
				pelem.appendChild(time_elem);
				l++;
			};
			if (l>0) {
				l--;
				pelem = document.getElementById('sidir_'+l);
				if (pelem) {
					while (celem=pelem.lastChild) pelem.removeChild(celem);	
					pelemnew = document.createTextNode(this.env["txt_noservice"]);
					pelem.appendChild(pelemnew);
				};
			};
		};
	};
	
	this.cleanVlisData = function(str) {
		var strneu = str.replace(/&nbsp;/g," ");
		strneu = strneu.replace(/&nbsp/g," ");
		strneu = strneu.replace(/\+/g,"");
		strneu = strneu.replace(/\*/g,"");
		strneu = strneu
		return strneu;
	};
	
	this.startajaxaid = function() {
		this.env["obj_stopsearchdiv"] = document.getElementById("si_stopsearchdiv");
		this.env["obj_stopsearch"] = document.getElementById("si_stopsearch");
		this.env["obj_stopid"] = document.getElementById("si_stopid");
		this.env["obj_routediv"] = document.getElementById("si_routediv");
		this.env["obj_route"] = document.getElementById("si_route");
		this.env["arr_stops"] = new Array();
		if (this.env["obj_stopsearchdiv"]) {
			this.env["obj_stopsearchdiv"].style.display = "block";
		};
		if (this.env["obj_routediv"]) {
			this.env["obj_routediv"].style.display = "block";
		}
		if (this.env["obj_stopid"]) {
			for (var i = 0 ; i< this.env["obj_stopid"].options.length ; i++) {//
				this.env["arr_stops"].push(new Array(this.env["obj_stopid"].options[i].value,this.env["obj_stopid"].options[i].firstChild.nodeValue));
			};
		};
		this.env["obj_stopsearch"].onkeyup=this.dohstsearch;
		this.env["obj_stopid"].onchange=this.doroutesearch;
		this.env["obj_stopid"].onkeyup=this.doroutesearch;
		this.env["obj_stopid"].onclick=this.doroutesearch;
		this.env["obj_stopid"].size = this.env["max_hstselect"];
	};
	
	this.dohstsearch = function() {
		self.dohstsearch_2();
	};
	
	this.dohstsearch_2 = function() {
		if (this.env["obj_stopid"]) {
			while (this.env["obj_stopid"].hasChildNodes() == true) {
				this.env["obj_stopid"].removeChild(this.env["obj_stopid"].firstChild);
			};
			var RE = new RegExp(this.env["obj_stopsearch"].value,"i");
			var counter = 0;
			for (var i = 0; i < this.env["arr_stops"].length ; i++) {
				if (this.env["arr_stops"][i][1].search(RE) != -1) {
					obj_option=document.createElement("option");
					obj_optiontext=document.createTextNode(this.env["arr_stops"][i][1]);
					obj_option.value = this.env["arr_stops"][i][0];
					obj_option.appendChild(obj_optiontext);
					this.env["obj_stopid"].appendChild(obj_option);
					counter ++;
				};
			};
			//this.env["obj_stopid"].style.display="inline";
			if (counter >= 1 ) {
				this.env["obj_stopid"].size = counter;
				this.env["obj_stopid"].disabled = false;
			} else {
				//this.env["obj_stopid"].style.display="none";
				obj_option=document.createElement("option");
				obj_optiontext=document.createTextNode(this.env["txt_nohst"]);
				obj_option.value = "";
				obj_option.appendChild(obj_optiontext);
				this.env["obj_stopid"].appendChild(obj_option);
				this.env["obj_stopid"].disabled = true;
			};
			if (counter > this.env["max_hstselect"]) {
				this.env["obj_stopid"].size = this.env["max_hstselect"];
			} else if (counter < this.env["min_hstselect"]) {
				this.env["obj_stopid"].size = this.env["min_hstselect"];
			};
		};
	};
	
	this.doroutesearch = function() {
		if (self.env["routetimer"] != null) {
			window.clearTimeout(self.env["routetimer"]);
		};
		self.env["routetimer"] = window.setTimeout("if (ivbsmartinfo) {ivbsmartinfo.doroutesearch_2();};",150);
	};
	
	this.doroutesearch_2 = function() {
		if (this.serverrouteurl != "") {
			if (this.env["routeloader"]) {
				try {
					this.env["routeloader"].abort();
				} catch (error) {
				};
			};
			this.env["obj_stopsearch"].value = this.env["obj_stopid"].options[this.env["obj_stopid"].selectedIndex].firstChild.nodeValue;
			var srvurl = this.serverrouteurl.replace(/(###STOPID###)/,this.env["obj_stopid"].options[this.env["obj_stopid"].selectedIndex].value);
			this.env["routeloader"] = new xmlHttpRequestNode("POST",srvurl,true,'',self,'getroutesxmlComplete',false,'getroutesxmlError');
			
			var obj_option=document.createElement("option");
			var obj_optiontext=document.createTextNode(this.env["txt_routesloading"]);
			obj_option.value = "";
			obj_option.appendChild(obj_optiontext);
			this.env["obj_route"].appendChild(obj_option);				
			this.env["obj_route"].selectedIndex = (this.env["obj_route"].options.length-1);
			this.env["obj_route"].disabled = true;
		};
	};
	
	this.getroutesxmlComplete = function(XML) {
		var XMLdata = XML.responseXML;
		if (this.env["obj_route"]) {
			while (this.env["obj_route"].childNodes.length > 2) {
				this.env["obj_route"].removeChild(this.env["obj_route"].lastChild);
			};
			if (XMLdata) {
				var smartinfos = XMLdata.getElementsByTagName("route");
				var obj_option;
				var obj_optiontext;
				for (var i=0;i<smartinfos.length;i++) {
					// smartinfos[i].getElementsByTagName("route")[0].firstChild.nodeValue;
					obj_option=document.createElement("option");
					obj_optiontext=document.createTextNode(smartinfos[i].firstChild.nodeValue);
					obj_option.value = smartinfos[i].firstChild.nodeValue;
					obj_option.appendChild(obj_optiontext);
					this.env["obj_route"].appendChild(obj_option);					
				};
			};
			this.env["obj_route"].disabled = false;
		};
	};
	
	this.getroutesxmlError = function() {
		
	};
	
	
	// ***********************
	// *** XML HTTPREQUEST ***
	// ***********************
	function xmlHttpRequestNode(method,url,async,para,robj,rfunction,rpara,rerrfunction) {
		var xmlHttp;
		var rfunction = rfunction;
		var rpara = rpara;
		var robj= robj;
		
		try {
			xmlHttp=new XMLHttpRequest(); 
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
					alert("Your browser does not support AJAX!");
					return false;
				};
			};
		};
		this.oreadystatechange = function() {
			switch (xmlHttp.readyState) {
				case 4:
					robj[rfunction](xmlHttp,rpara);				
				break;
				default:
					
				break;
			};
		};
		xmlHttp.onreadystatechange = this.oreadystatechange;
		try {
			xmlHttp.open(method,url,async);
		} catch (err) {
			robj[rerrfunction]();
		};
		xmlHttp.send(null);
	};
};
if(typeof String.prototype.trim=="undefined") {
	String.prototype.trim=function() {
		return this.replace (/^\s+/, '').replace (/\s+$/, '');
	};
};
