window.onload = function () {
	if ($('mailform')) {
		$('name').onblur = checkEmpty;
		$('email').onblur = checkEmail;
		$('message').onblur = checkEmpty;
	}

	if ($('map')) startMap();

	if ($('nieuwsbrief')) {
		$('nieuwsbrief').onsubmit = function () {
			if (!(/^([a-z0-9_\-]+\.)*([a-z0-9\-_]+){1}@([a-z0-9\-]+\.)+([a-z]{2,}){1}$/i.test($('email').value))) {
				$('email').parentNode.className = 'error';
				return false;
			}

			window.open('http://www.terradon.nl/inuitpopup.php', 'nieuwsbrieven','toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0, resizable=1,width=350,height=250,left = 412,top = 334');
			return true;
		}
	}
}

function $(i) {
	return document.getElementById(i);
}

function checkEmpty () {
	this.parentNode.className = this.value == '' ? 'error' : '';
}

function checkEmail () {
	this.parentNode.className = /^([a-z0-9_\-]+\.)*([a-z0-9\-_]+){1}@([a-z0-9\-]+\.)+([a-z]{2,}){1}$/i.test(this.value) ? '' : 'error';
}

function addErrorMsg(msg) {
	if (!$('form_errors')) {
		errors = document.createElement('p');
		errors.id = 'form_errors';

		$('mailform').insertBefore(errors, $('mailform').firstChild);
	}

	if ($('form_errors').innerHTML == '') {
		$('form_errors').innerHTML = msg;
	} else {
		$('form_errors').innerHTML += '<br />' + msg;
	}
}

function startMap() {
	if (GBrowserIsCompatible()) {
		document.getElementsByTagName('body')[0].className = 'hasMap';

		var map = new GMap2($('map'));
		var studioLocation = new GLatLng(52.12763139688525, 4.584514796733856);
		var route = new GDirections(map, $('directions'));

		GEvent.addListener(route, 'load', function () {
			map.openInfoWindowHtml(map.getCenter(), '<ul id="options"><li><a href="javascript:window.print()">Print deze route</a></li><li><a href="javascript:void(0)" id="new_route">Plan een nieuwe route</a></li>')

			$('new_route').onclick = function () {
				$('directions').innerHTML = '';
				route.clear();
				map.setCenter(studioLocation, 15);
				studio.openInfoWindow(infoWindowContents);
			}
		});

		GEvent.addListener(route, 'error', function () {
			var status = route.getStatus();

			if (status.code == 602) {
				alert('Uw adres kon niet gevonden worden. Probeer een variatie.');
			}
		});

		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(studioLocation, 15);
		map.setMapType(G_HYBRID_MAP);

		var studioIcon = new GIcon();
		studioIcon.image = '/img/balletstudio.png';
		studioIcon.iconSize = new GSize(38, 38);
		studioIcon.iconAnchor = new GPoint(16, 22);
		studioIcon.infoWindowAnchor = new GPoint(16, 22);
		studioIcon.imageMap = [0,0,38,38];

		var studio = new GMarker(studioLocation, studioIcon);

		map.addOverlay(studio);

		var infoWindowContents = document.createElement('div');

		var strongNode = document.createElement('strong');
		strongNode.appendChild(document.createTextNode('Balletstudio Mirjam Ouwerkerk'));
		strongNode.id = 'kop';

		var inputNode  = document.createElement('input');
		inputNode.type = 'text';
		inputNode.name = 'location';
		inputNode.id   = 'location';
		inputNode.size = '50';

		var buttonNode     = document.createElement('input');
		buttonNode.type    = 'button';
		buttonNode.name    = 'do_route';
		buttonNode.id      = 'do_route';
		buttonNode.value   = 'Plan route';
		buttonNode.onclick = function () {
			route.clear();
			route.load('from: ' + $('location').value + ', Netherlands to: Rijndijk 125A, Hazerswoude Rijndijk, Netherlands', {locale: 'nl_NL'});
		}; 

		var pNode = document.createElement('p');
		pNode.appendChild(document.createTextNode('Voor een routebeschrijving vult u hier uw adres in.'));
		pNode.appendChild(document.createElement('br'));
		pNode.appendChild(document.createTextNode('De routebeschrijving is direct uit te printen.'));
		pNode.appendChild(document.createElement('br'));
		pNode.appendChild(inputNode);
		pNode.appendChild(document.createElement('br'));
		pNode.appendChild(buttonNode);

		infoWindowContents.appendChild(strongNode);
		infoWindowContents.appendChild(pNode);

		studio.openInfoWindow(infoWindowContents);

		GEvent.addListener(studio, 'click', function(marker){
			marker.openInfoWindow(infoWindowContents);
		});
	}
}
