
/* XML Data */
function createXhrObject() {
    if(window.XMLHttpRequest)
	return new XMLHttpRequest();

	if (window.ActiveXObject) {
		var names = ["Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.3.0", "Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
		for(var i in names) {
			try{ return new ActiveXObject(names[i]); }
			catch(e){}
		}
	}

	window.alert("Uw browser kan het object XMLHTTPRequest niet laden");
	return null; // Unknown
}

function remove_item() {
	freez();

	obj = document.getElementById("location");
	obj.remove(obj.selectedIndex);

	saveList();
}

/* Refresh Actions */
function saveList() {
	req = createXhrObject();

	req.onreadystatechange = function() {
		if(req.readyState == 4)
			if(req.status == 200)
				updateList(req.responseText);
			else
				alert("Error");
	};

	temp = getList();
	req.open("GET", "/index.php?option=com_static&staticfile=realtime-rain90.php&Itemid=58&ajax=yes&job=save_loc&data="+temp, true);
	req.send(null);
}

function getList() {
	obj = document.getElementById("location");

	outval = "";
	for(i = 0; i < obj.length; i++)
		outval += obj.options[i].value + ",";

	return outval;
}

function updateList(data) {
	obj = document.getElementById("location");
	obj.options.length = 0;

	reg = new RegExp("[,]+", "g");
	reg2 = new RegExp("[///]+", "g");
	temp = data.split(reg);

	if(temp[temp.length - 1] == "")
		temp[temp.length - 1] = null;

	for(i = 0; i < temp.length; i++) {
		temp2 = temp[i].split(reg2);
		add(temp2[0]+" ("+temp2[1]+","+temp2[2]+")", temp2[0]+"///"+temp2[1]+"///"+temp2[2]);
	}

	unfreez();
}

/* List Actions */
function getdata() {
	obj = document.getElementById("location");
	outval = "";

	for(i = 0; i < obj.length; i++)
		if(obj.options[i].selected == true)
			outval += obj.options[i].value + ",";

	if(outval != "") {
		req = createXhrObject();

		req.onreadystatechange = function() {
			if(req.readyState == 4)
				if(req.status == 200)
						document.getElementById("print_zone").innerHTML = req.responseText;
				else
					alert("Error");
		};

		req.open("GET", "/index.php?option=com_static&staticfile=realtime-rain90.php&Itemid=58&ajax=yes&job=get_graph&data="+outval, true);
		req.send(null);

	} else alert("U moet een plaats selecteren uit de lijst.");
}

function preadd() {
	obj = document.getElementById("location");

	if(obj.length > 2) {
		alert("U kan slechts 3 plaatsen toevoegen.");
		return false;
	}

	window.open('/staticfiles/realtime/radar_pick.php', 'Map', 'width=615, height=620, toolbar=0, status=0');
}

function add(val, name) {
	obj = document.getElementById("location");
	obj.options[obj.length] = new Option(val, name);
	obj.selectedIndex = obj.length - 1;
}

function newadd(val, name) {
	obj = document.getElementById("location");
	obj.options[obj.length] = new Option(val, name);
	obj.selectedIndex = obj.length - 1;
	saveList();
}

/* GUI Actions (unused) */
function freez() {
	document.getElementById("location").enabled = false;
}

function unfreez() {
	document.getElementById("location").enabled = true;
}

