var http = GetXmlHttpObject();

function GetXmlHttpObject() { 
	var x = null;
	if (window.XMLHttpRequest) {
		x = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		x = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return x;
}

function getcity(caller) {
	var pc = caller.value;	
	http.open("GET", "inc/postalcode.php?getcity=" + pc, true);
	http.onreadystatechange = getcity_callback;
	http.send(null);
	if (document.getElementById("valid3"))
		document.getElementById("valid3").src = "graphics/icon_ajax.gif";
}

function getcity_callback() {
	if (http.readyState == 4) {
		document.getElementById("city").value = http.responseText;
		
		var o = document.getElementById("valid3");
		if (http.responseText != 'Ugyldig' && http.responseText != '' && o)
			o.src = "graphics/icon_good.gif";
		else if (o)
			o.src = "graphics/icon_bad.gif";
	}
}

function checkup(num, caller) {
	var isvalid = false;
	switch (num) {
	case 0:
		isvalid = (/^.+@.+\..+$/.test(caller.value));
		break;
	case 1:
		isvalid = (/^.+ .+$/.test(caller.value));
		break;
	case 2:
		isvalid = caller.value.length > 3;
		break;
	case 4:
		isvalid = caller.value.length > 3;
		break;
	}
	if (document.getElementById("valid"+num))
		document.getElementById("valid"+num).src = "graphics/icon_" + (isvalid ? 'good' : 'bad') + ".gif";
}

window.onload = function() {
	if (!document.getElementById("valid0"))
		return;
	var a = new Array(
		"username",
		"fullname",
		"address",
		"postalcode",
		"name"
	);
	for (var i = 0; i < a.length; i++) {
		var o = document.getElementById(a[i]);
		if (o) checkup(i, o);
	}
	getcity(document.getElementById("postalcode"));
}
