var citylistnr=0;
var citylistdiv=false;

function ajaxcall(url) {
	var req = null;
	try {
		req = new ActiveXObject("Msxml2.XMLHTTP");
	} catch(b) {
		try {
			req = new ActiveXObject("Microsoft.XMLHTTP");
		} catch(c) {
			req = null;
		}
	}
	if(!req && typeof XMLHttpRequest != "undefined") {
		req = new XMLHttpRequest;
	}
	if(req) {
		req.open('GET',url,true);
		return req;
	}
	return false;
}

function counter() {
	var now = new Date();
	var req = ajaxcall('count.php?u='+now.getTime());
	req.onreadystatechange = function() {countercallback(req)};
	req.send(null);
}

function countercallback(req) {
	if(req.readyState == 4) {
		if(req.status == 200) {
			span = document.getElementById("count");
			span.innerHTML = req.responseText;
			if(location.pathname.indexOf("petitie") > 0) {
				document.title = req.responseText + ' petities getekend, teken jij de volgende? - teruggespoeld.nl';
			}
		}
		lreq = null;
	}
}

function loader() {
	var inputs = document.getElementsByTagName("INPUT");
	for(var i in inputs) {
		if(inputs[i].type == "checkbox" && inputs[i].className == "othercheck") {
			inputs[i].onclick = function() {
				if(this.checked == false) {
					this.parentNode.getElementsByTagName("INPUT")[1].value = "";
					refresh();
				}
			}
		} else if(inputs[i].className == "other") {
			inputs[i].onkeyup = refresh;
		}
	}
	refresh();
	setInterval('counter()',10000);
	var form = document.getElementById("petition");
	if(form != null) {
		form.setAttribute("autocomplete","off");
	}
}

function refresh() {
	var inputs = document.getElementsByTagName("INPUT");
	var emptyfound = false;
	for(var i=0;i<inputs.length;i++) {
		if(inputs[i].className == "other") {
			if(inputs[i].value == "") {
				inputs[i].parentNode.getElementsByTagName("INPUT")[0].checked = false;
				if(emptyfound) {
					inputs[i].parentNode.parentNode.style.display = "none";
				} else {
					inputs[i].parentNode.parentNode.style.display = "";
					emptyfound = true;
				}
			} else {
				inputs[i].parentNode.getElementsByTagName("INPUT")[0].checked = true;
				inputs[i].parentNode.parentNode.style.display = "";
			}
		}
	}
}

function showinfo(serie) {
	var info = document.getElementById('info');
	if(typeof info == 'object') {
		info.innerHTML = '<p>Bezig met laden van informatie...</p>';
		var req = ajaxcall('ajax-'+serie+'.html');
		if(req) {
			req.onreadystatechange = function() {fillinfo(req)};
			req.send(null);
			return false;
		}
	}
	location.href = 'info-'+serie+'.html';
}

function fillinfo(req) {
	if(req.readyState == 4) {
		if(req.status == 200) {
			var info = document.getElementById('info');
			if(typeof info == 'object') {
				info.innerHTML = '<a name="info"></a>' + req.responseText;
				location.href = '#info';
			}
		}
		req = null;
	}
}

function citylisting(el) {
	document.getElementById("cityid").value = "";
	if(el.value.length > 0) {
		citylistnr++;
		var localnr = citylistnr;
		var req = ajaxcall('city.php?q='+el.value);
		req.onreadystatechange = function() {citylist(req,localnr)};
		req.send(null);
	} else {
		removecitylistdiv(true);
	}
}

function createcitylistdiv() {
	var box = document.getElementById("city");
	var obj = box;
	var boxleft=0;
	var boxtop=0;
	if (obj.offsetParent) {
		boxleft = obj.offsetLeft;
		boxtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			boxleft += obj.offsetLeft;
			boxtop += obj.offsetTop;
		}
	}
	citylistdiv = document.createElement("DIV");
	citylistdiv.className = "citylist";
	citylistdiv.style.top = boxtop+box.offsetHeight+"px";
	citylistdiv.style.left = boxleft+"px";
	document.body.appendChild(citylistdiv);
}

function removecitylistdiv(doit) {
	if(typeof doit != "undefined") {
		if(citylistdiv!=false) {
			document.body.removeChild(citylistdiv);
			citylistdiv = false;
		}
	} else {
		setTimeout("removecitylistdiv(true)",1000);
	}
}

function setcity(id,el) {
	document.getElementById("cityid").value = id;
	document.getElementById("city").value = el.innerHTML;
	removecitylistdiv(true);
}

function citylist(req,localnr) {
	if(req.readyState == 4) {
		if(req.status == 200 && localnr == citylistnr) {
			var cities = req.responseXML.getElementsByTagName("city");
			if(cities.length > 0) {
				if(citylistdiv==false) createcitylistdiv();
				citylistdiv.innerHTML = '';
				for(var c=0;c<cities.length;c++) {
					citylistdiv.innerHTML += '<a href="#" onmouseup="setcity('+cities[c].getAttribute('id')+',this)">'+cities[c].firstChild.nodeValue+'</a>';
				}
			} else if(citylistdiv!=false) {
				removecitylistdiv(true);
			}
		}
		req = null;
	}
}

function foreignchange(el) {
	var cityinput = document.getElementById("city");
	cityinput.disabled = el.checked;
	cityinput.style.backgroundColor = el.checked?"#BBBBBB":"";
	cityinput.style.color = el.checked?"#BBBBBB":"#000000";
}
