window.onresize = function() { resize(); }

function getID(id) {
	return document.getElementById(id);
}

function load() { // google map loader
	if (window.attachEvent) { // internet explorer
		window.attachEvent("onload", function() {
		});
	} else {
		window.addEventListener("load", function() {
			load(); // firefox und standard browser
		}, false);
	}
	if(getID('waituntilloaded')) document.getElementById('waituntilloaded').style.display = 'none';
	
}
function disablesubmit(e) {
	e.disabled = true;
	e.value = 'Even geduld';
}

function createMarker(point,html,drag) { // puts a default marker on the map
	var zeusicon = new GIcon();
	zeusicon.image = "/site/gfx/zeusmarker.png";
	zeusicon.shadow = "/site/gfx/zeusshadow.png";
	zeusicon.iconSize = new GSize(27, 32);
	zeusicon.shadowSize = new GSize(36, 33);
	zeusicon.iconAnchor = new GPoint(12, 29);
	zeusicon.infoWindowAnchor = new GPoint(8, 29);

	if(drag) {
		var marker = new GMarker(point, {icon: zeusicon, draggable: true});
		GEvent.addListener(marker, "dragend", function() {
		  var center = marker.getPoint();
		  var lat = center.lat();
		  var lon = center.lng();
		  if(getID('locationtype')) {
			  document.getElementById('latform').value = lat;
			  document.getElementById('lonform').value = lon;
			  document.getElementById('locationtype').value = 'latlon';
			  map.setCenter(new GLatLng(lat,lon));
			  document.getElementById('locationform').submit();
		   } else {
			   window.location = '/page/analyse/action/newcenter/lat/'+lat+'/lon/'+lon;
		   }
		});

	} else {
		var marker = new GMarker(point, {icon: zeusicon});
	}
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});
	return marker;
}

function createTNOMarker(point,html) { // puts a default marker on the map
	var zeusicon = new GIcon();
	zeusicon.image = "/site/gfx/tnomarker.png";
	zeusicon.iconSize = new GSize(7, 49);
	zeusicon.iconAnchor = new GPoint(3, 49);
	zeusicon.infoWindowAnchor = new GPoint(3, 0);

	var marker = new GMarker(point, {icon: zeusicon});
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});
	
	return marker;
}

function createReportMarker(point,code) { // puts a default marker on the map
	var zeusicon = new GIcon();
	zeusicon.image = "/site/gfx/zeusmarker.png";
	zeusicon.shadow = "/site/gfx/zeusshadow.png";
	zeusicon.iconSize = new GSize(23, 30);
	zeusicon.shadowSize = new GSize(36, 33);
	zeusicon.iconAnchor = new GPoint(8, 29);
	zeusicon.infoWindowAnchor = new GPoint(12, 0);

	var marker = new GMarker(point, {icon: zeusicon});

	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml("<iframe frameborder=0 scrolling=no src='/site/popup/reportdetail.php?code="+code+"' style='border:none;width:200px;height:110px'></iframe>");
	});
	return marker;
}

function resize() {
	if(document.getElementById('map')) {
		w = getWindowWidth() - 345;
		h = getWindowHeight() - 80;
		document.getElementById('map').style.width = w+'px';
		document.getElementById('map').style.height = h+'px';
		document.getElementById('control').style.height = h+'px';
	}
	if(document.getElementById('reportmap')) {
		w = getWindowWidth() - 445;
		h = getWindowHeight() - 80;
		document.getElementById('reportmap').style.width = w+'px';
		document.getElementById('reportmap').style.height = h+'px';
		document.getElementById('reportcontrol').style.height = h+'px';
	}
	if(document.getElementById('projectsmap')) {
		h = getWindowHeight() - 125;
		document.getElementById('projectsmap').style.height = h+'px';
	}
	if(document.getElementById('projectlist')) {
		h = getWindowHeight() - 90;
		document.getElementById('projectlist').style.height = h+'px';
	}
}

function showLocTypeInput(x) {
	document.getElementById('latloninput').style.display = 'none';
	document.getElementById('nlzipinput').style.display = 'none';
	document.getElementById('nladresinput').style.display = 'none';
	document.getElementById('googlegeoinput').style.display = 'none';
	document.getElementById(x+'input').style.display = 'block';
}

function openPopup(URLStr)
{
    var maxx = screen.width;
    var maxy = screen.height;
    windowprops = "toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,width=200,height=200,left=100,top=100";
    window.open(URLStr+"&maxx="+maxx+"&maxy="+maxy,"",windowprops);
}

function openWin(URLStr)
{
    var maxx = screen.width;
    var maxy = screen.height;
    windowprops = "toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,width=300,height=500,left=100,top=100";
    window.open(URLStr,"",windowprops);
}


function dec2gra(x, y) {

	if(x>0) result = "N:"; else result = "S:";
	x = Math.abs(x);
	dxg = Math.floor(x);
	dxm = Math.floor((x-dxg)*60);
	dxs = Math.floor((((x-dxg)*60)-dxm)*60);
	dx = dxg + '&deg;' + dxm + '\'' + dxs + '"';
	result += dx;

	if(y>0) result += " E:"; else result += " W:";
	y = Math.abs(y);
	dyg = Math.floor(y);
	dym = Math.floor((y-dyg)*60);
	dys = Math.floor((((y-dyg)*60)-dym)*60);
	dy = dyg + '&deg;' + dym + '\'' + dys + '"';
	result += dy;
	
	return result;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function breakeveryheader(){
	for (i=0; i<document.getElementsByTagName("H2").length; i++)
	document.getElementsByTagName("H2")[i].style.pageBreakBefore='always'
	for (i=0; i<document.getElementsByTagName("H4").length; i++)
	document.getElementsByTagName("H4")[i].style.pageBreakBefore='always'
}

// Sets mouseX and mouseY variables continously
if (document.layers) { document.captureEvents(Event.MOUSEMOVE); document.onmousemove = cmp; } else if (document.all) { document.onmousemove = cmp; } else if (document.getElementById) { document.onmousemove = cmp; }
function cmp(e) { if(document.layers) {mouseX=e.pageX;mouseY=e.pageY;} else if(document.all) {mouseX=window.event.x+document.body.scrollLeft;mouseY=window.event.y+document.body.scrollTop;} else if(document.getElementById) {mouseX=e.pageX;mouseY=e.pageY;}}


var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
@else
	xmlhttp = false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp = false;
	}
}
function setPopup(text, script) {	
	if (xmlhttp) {
		xmlhttp.open("GET",'/site/popup/'+script,true);
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) {
				if(getID('popup_content')) {
					getID('popup_content').innerHTML=xmlhttp.responseText;
					p = getID('popup');
					if(getID('popup_content').innerHTML == '') {
						p.style.display = 'none';
					} else if(p.style.display != 'block') {
						p.style.display = 'block';
						xpos = mouseX;
						ypos = mouseY;
						if(xpos>(getWindowWidth()-270)) xpos = getWindowWidth()-270;
						if(ypos>(getWindowHeight()-250)) ypos = getWindowHeight()-250;
						p.style.left = xpos+'px';
						p.style.top = ypos+'px';
						p.style.height = "";
						if(p.scrollHeight > 220) p.style.height = "220px"
					}
				}
			}
		}
		xmlhttp.send(null)
	}
}


function collapseExpand(id, imgid) {
	if (getID(id)) {
		if(getID(id).style.display == 'none') {
			getID(id).style.display	= 'block';
			getID(imgid).src = '/site/gfx/collapse.gif';
		} else {
			getID(id).style.display	= 'none';
			getID(imgid).src = '/site/gfx/expand.gif';
		}
	}
}

function showMarkers() {

	url = "/site/xml/reports.xml.php";
	
	GDownloadUrl(url , function(data, responseCode) {
								
		if(GXml) {
			
			document.getElementById('waituntilloaded').style.display = 'block';
			
			var xml = GXml.parse(data);
			var markers = xml.documentElement.getElementsByTagName("marker");
			var points = [];
			var info = '';
			
			reports = Array();
		
			for (var i = 0; i < markers.length; i++) {
				xc = parseFloat(markers[i].getAttribute("lat"));
				yc = parseFloat(markers[i].getAttribute("lon"));
				code = markers[i].getAttribute("code");
	
				var point = new GLatLng(xc,yc);
				points.push(point);
				reports[i] = createReportMarker(point,code);
				map.addOverlay(reports[i]);
			}
			
			document.getElementById('waituntilloaded').style.display = 'none';
		}
		
	});
}

function showProjects() {

	url = "/site/xml/projects.xml.php";
	
	GDownloadUrl(url , function(data, responseCode) {
								
		if(GXml) {
			
			document.getElementById('waituntilloaded').style.display = 'block';
			
			var xml = GXml.parse(data);
			var markers = xml.documentElement.getElementsByTagName("marker");
			var points = [];
			var info = '';
			
			reports = Array();
		
			for (var i = 0; i < markers.length; i++) {
				xc = parseFloat(markers[i].getAttribute("lat"));
				yc = parseFloat(markers[i].getAttribute("lon"));
				size = parseInt(markers[i].getAttribute("size"));
				color = parseInt(markers[i].getAttribute("color"));
				id = parseInt(markers[i].getAttribute("id"));
	
				var point = new GLatLng(xc,yc);
				points.push(point);
				reports[i] = createProjectMarker(point,id,size,color);
				map.addOverlay(reports[i]);
			}
			
			document.getElementById('waituntilloaded').style.display = 'none';
		}
		
	});
}

function createProjectMarker(point,id,size,color) { // puts a default marker on the map
	var zeusicon = new GIcon();
	zeusicon.image = "/site/gfx/zeusmarker_"+size+"_"+color+".png";
	zeusicon.shadow = "/site/gfx/zeusshadow.png";
		
	if(size==3) {
		zeusicon.iconSize = new GSize(23, 30);
		zeusicon.shadowSize = new GSize(36, 33);
		zeusicon.iconAnchor = new GPoint(8, 30);
		zeusicon.infoWindowAnchor = new GPoint(12, 0);
	} else 	if(size==2) {
		zeusicon.iconSize = new GSize(18, 24);
		zeusicon.shadowSize = new GSize(28, 26);
		zeusicon.iconAnchor = new GPoint(6, 24);
		zeusicon.infoWindowAnchor = new GPoint(10, 0);
	} else 	if(size==1) {
		zeusicon.iconSize = new GSize(14, 18);
		zeusicon.shadowSize = new GSize(22, 20);
		zeusicon.iconAnchor = new GPoint(4, 18);
		zeusicon.infoWindowAnchor = new GPoint(8, 0);
	} else {
		zeusicon.iconSize = new GSize(9, 12);
		zeusicon.shadowSize = new GSize(14, 13);
		zeusicon.iconAnchor = new GPoint(2, 12);
		zeusicon.infoWindowAnchor = new GPoint(6, 0);
	}

	var marker = new GMarker(point, {icon: zeusicon});

	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml("<iframe frameborder=0 scrolling=no src='/site/popup/projectdetail.php?id="+id+"' style='border:none;width:200px;height:120px'></iframe>");
	});
	return marker;
}

function formatValutaField(obj) {
	value = obj.value;
	if(value.substr(0,2)=='€ ') value = value.substr(1);
	if(value.substr(0,1)==' ') value = value.substr(1);
	value = parseFloat(value.replace(/,/g,'.')).toFixed(2).replace(/\./g,',');
	if(value=='NaN') value = '0,00';
	obj.value = '€ '+value;
}
