var polygon;
var postcodeTabel = new Array();

// initialiseert de polygon, dit wil je pas doen als de pagina geladen is omdat je dan pas kan bepalen of reparatieGebiedFout op de pagina staat
function initialiseerPolygon()
{
	// De polygon data, gemaakt met http://www.birdtheme.org/useful/googletool.html
	// de data is verschillend voor installatie en reparatie
	if( !document.getElementById('reparatieGebiedFout') )
	{
		// Dit is het groene gebied voor de landelijke installatie
		polygon = new GPolygon(
		[
		new GLatLng(51.722945,4.677429),
		new GLatLng(51.709332,4.600525),
		new GLatLng(51.691458,4.514008),
		new GLatLng(51.716139,4.412384),
		new GLatLng(51.737405,4.354706),
		new GLatLng(51.75356,4.297028),
		new GLatLng(51.76376,4.240723),
		new GLatLng(51.796896,4.192657),
		new GLatLng(51.813879,4.155579),
		new GLatLng(51.823217,4.092407),
		new GLatLng(51.885138,4.020996),
		new GLatLng(51.917337,3.982544),
		new GLatLng(51.951206,4.003143),
		new GLatLng(51.970669,3.999023),
		new GLatLng(51.987587,4.020996),
		new GLatLng(51.990124,4.041595),
		new GLatLng(51.985049,4.095154),
		new GLatLng(52.005343,4.129486),
		new GLatLng(52.074611,4.22699),
		new GLatLng(52.131128,4.306641),
		new GLatLng(52.185047,4.371185),
		new GLatLng(52.236379,4.420624),
		new GLatLng(52.302769,4.476929),
		new GLatLng(52.345575,4.505768),
		new GLatLng(52.382473,4.53186),
		new GLatLng(52.41934,4.553833),
		new GLatLng(52.435251,4.563446),
		new GLatLng(52.462034,4.559326),
		new GLatLng(52.465716,4.586792),
		new GLatLng(52.462369,4.638977),
		new GLatLng(52.460695,4.662323),
		new GLatLng(52.438097,4.707642),
		new GLatLng(52.413812,4.688416),
		new GLatLng(52.394123,4.673996),
		new GLatLng(52.359751,4.677429),
		new GLatLng(52.324933,4.691162),
		new GLatLng(52.300585,4.719315),
		new GLatLng(52.25185,4.733734),
		new GLatLng(52.21905,4.796219),
		new GLatLng(52.218209,4.915695),
		new GLatLng(52.242181,5.021439),
		new GLatLng(52.302265,5.092163),
		new GLatLng(52.319057,5.164261),
		new GLatLng(52.318217,5.246658),
		new GLatLng(52.302685,5.300217),
		new GLatLng(52.273704,5.360641),
		new GLatLng(52.248908,5.409393),
		new GLatLng(52.20222,5.456772),
		new GLatLng(52.146215,5.445099),
		new GLatLng(52.045903,5.593414),
		new GLatLng(51.982512,5.609894),
		new GLatLng(51.920725,5.376434),
		new GLatLng(51.823217,5.30777),
		new GLatLng(51.739956,5.197906),
		new GLatLng(51.711034,5.048218),
		new GLatLng(51.714437,4.843597),
		new GLatLng(51.722945,4.677429)
		], "#3ff300", 2, 1, "#00ff00", 0.2);
		
		// Dit zijn de postcode's voor het groene gebied van de landelijke installatie
		postcodeTabel[10] = 0;
		postcodeTabel[11] = 1;
		postcodeTabel[12] = 2;
		postcodeTabel[13] = 2;
		postcodeTabel[14] = 1;
		postcodeTabel[15] = 1;
		postcodeTabel[16] = 0;
		postcodeTabel[17] = 0;
		postcodeTabel[18] = 1;
		postcodeTabel[19] = 1;
		postcodeTabel[20] = 3;
		postcodeTabel[21] = 3;
		postcodeTabel[22] = 3;
		postcodeTabel[23] = 3;
		postcodeTabel[24] = 3;
		postcodeTabel[25] = 3;
		postcodeTabel[26] = 3;
		postcodeTabel[27] = 3;
		postcodeTabel[28] = 3;
		postcodeTabel[29] = 3;
		postcodeTabel[30] = 3;
		postcodeTabel[31] = 3;
		postcodeTabel[32] = 2;
		postcodeTabel[33] = 2;
		postcodeTabel[34] = 3;
		postcodeTabel[35] = 2;
		postcodeTabel[36] = 2;
		postcodeTabel[37] = 2;
		postcodeTabel[38] = 1;
		postcodeTabel[39] = 2;
		postcodeTabel[40] = 1;
		postcodeTabel[41] = 2;
		postcodeTabel[42] = 2;
		postcodeTabel[43] = 0;
		postcodeTabel[44] = 0;
		postcodeTabel[45] = 0;
		postcodeTabel[46] = 1;
		postcodeTabel[47] = 1;
		postcodeTabel[48] = 1;
		postcodeTabel[49] = 1;
		postcodeTabel[50] = 1;
		postcodeTabel[51] = 1;
		postcodeTabel[52] = 1;
		postcodeTabel[53] = 1;
		postcodeTabel[54] = 1;
		postcodeTabel[55] = 0;
		postcodeTabel[56] = 0;
		postcodeTabel[57] = 0;
		postcodeTabel[58] = 0;
		postcodeTabel[59] = 0;
		postcodeTabel[60] = 0;
		postcodeTabel[61] = 0;
		postcodeTabel[62] = 0;
		postcodeTabel[63] = 0;
		postcodeTabel[64] = 0;
		postcodeTabel[65] = 1;
		postcodeTabel[66] = 1;
		postcodeTabel[67] = 1;
		postcodeTabel[68] = 1;
		postcodeTabel[69] = 1;
		postcodeTabel[70] = 0;
		postcodeTabel[71] = 0;
		postcodeTabel[72] = 1;
		postcodeTabel[73] = 1;
		postcodeTabel[74] = 0;
		postcodeTabel[75] = 0;
		postcodeTabel[76] = 0;
		postcodeTabel[77] = 0;
		postcodeTabel[78] = 0;
		postcodeTabel[79] = 0;
		postcodeTabel[80] = 1;
		postcodeTabel[81] = 1;
		postcodeTabel[82] = 1;
		postcodeTabel[83] = 0;
		postcodeTabel[84] = 0;
		postcodeTabel[85] = 0;
		postcodeTabel[86] = 0;
		postcodeTabel[87] = 0;
		postcodeTabel[88] = 0;
		postcodeTabel[89] = 0;
		postcodeTabel[90] = 0;
		postcodeTabel[91] = 0;
		postcodeTabel[92] = 0;
		postcodeTabel[93] = 0;
		postcodeTabel[94] = 0;
		postcodeTabel[95] = 0;
		postcodeTabel[96] = 0;
		postcodeTabel[97] = 0;
		postcodeTabel[98] = 0;
		postcodeTabel[99] = 0;
			} else {
		// Dit is het blauwe gebied voor de reparatie
		polygon = new GPolygon(
		[
		new GLatLng(51.985895,4.092407),
		new GLatLng(52.007879,4.130859),
		new GLatLng(52.078831,4.228363),
		new GLatLng(52.126912,4.301147),
		new GLatLng(52.158096,4.342346),
		new GLatLng(52.211983,4.395905),
		new GLatLng(52.276645,4.453583),
		new GLatLng(52.327031,4.493408),
		new GLatLng(52.308143,4.566193),
		new GLatLng(52.256894,4.516068),
		new GLatLng(52.228304,4.508514),
		new GLatLng(52.181174,4.549713),
		new GLatLng(52.16652,4.737854),
		new GLatLng(52.083894,4.787292),
		new GLatLng(51.994352,4.743347),
		new GLatLng(51.973207,4.696655),
		new GLatLng(51.989278,4.577179),
		new GLatLng(51.975237,4.507141),
		new GLatLng(51.913017,4.489975),
		new GLatLng(51.895308,4.398651),
		new GLatLng(51.899546,4.360199),
		new GLatLng(51.891919,4.31076),
		new GLatLng(51.915643,4.237976),
		new GLatLng(51.954591,4.159698),
		new GLatLng(51.985895,4.092407)
		], "#003ff3", 2, 1, "#0000ff", 0.2);
		
		// De postcode's voor het blauwe gebied
		// Dit zijn de postcode's voor het groene gebied van de landelijke installatie
		postcodeTabel[10] = 0;
		postcodeTabel[11] = 0;
		postcodeTabel[12] = 0;
		postcodeTabel[13] = 0;
		postcodeTabel[14] = 0;
		postcodeTabel[15] = 0;
		postcodeTabel[16] = 0;
		postcodeTabel[17] = 0;
		postcodeTabel[18] = 0;
		postcodeTabel[19] = 0;
		postcodeTabel[20] = 0;
		postcodeTabel[21] = 0;
		postcodeTabel[22] = 3;
		postcodeTabel[23] = 3;
		postcodeTabel[24] = 3;
		postcodeTabel[25] = 3;
		postcodeTabel[26] = 3;
		postcodeTabel[27] = 3;
		postcodeTabel[28] = 3;
		postcodeTabel[29] = 0;
		postcodeTabel[30] = 3;
		postcodeTabel[31] = 3;
		postcodeTabel[32] = 0;
		postcodeTabel[33] = 0;
		postcodeTabel[34] = 0;
		postcodeTabel[35] = 0;
		postcodeTabel[36] = 0;
		postcodeTabel[37] = 0;
		postcodeTabel[38] = 0;
		postcodeTabel[39] = 0;
		postcodeTabel[40] = 0;
		postcodeTabel[41] = 0;
		postcodeTabel[42] = 0;
		postcodeTabel[43] = 0;
		postcodeTabel[44] = 0;
		postcodeTabel[45] = 0;
		postcodeTabel[46] = 0;
		postcodeTabel[47] = 0;
		postcodeTabel[48] = 0;
		postcodeTabel[49] = 0;
		postcodeTabel[50] = 0;
		postcodeTabel[51] = 0;
		postcodeTabel[52] = 0;
		postcodeTabel[53] = 0;
		postcodeTabel[54] = 0;
		postcodeTabel[55] = 0;
		postcodeTabel[56] = 0;
		postcodeTabel[57] = 0;
		postcodeTabel[58] = 0;
		postcodeTabel[59] = 0;
		postcodeTabel[60] = 0;
		postcodeTabel[61] = 0;
		postcodeTabel[62] = 0;
		postcodeTabel[63] = 0;
		postcodeTabel[64] = 0;
		postcodeTabel[65] = 0;
		postcodeTabel[66] = 0;
		postcodeTabel[67] = 0;
		postcodeTabel[68] = 0;
		postcodeTabel[69] = 0;
		postcodeTabel[70] = 0;
		postcodeTabel[71] = 0;
		postcodeTabel[72] = 0;
		postcodeTabel[73] = 0;
		postcodeTabel[74] = 0;
		postcodeTabel[75] = 0;
		postcodeTabel[76] = 0;
		postcodeTabel[77] = 0;
		postcodeTabel[78] = 0;
		postcodeTabel[79] = 0;
		postcodeTabel[80] = 0;
		postcodeTabel[81] = 0;
		postcodeTabel[82] = 0;
		postcodeTabel[83] = 0;
		postcodeTabel[84] = 0;
		postcodeTabel[85] = 0;
		postcodeTabel[86] = 0;
		postcodeTabel[87] = 0;
		postcodeTabel[88] = 0;
		postcodeTabel[89] = 0;
		postcodeTabel[90] = 0;
		postcodeTabel[91] = 0;
		postcodeTabel[92] = 0;
		postcodeTabel[93] = 0;
		postcodeTabel[94] = 0;
		postcodeTabel[95] = 0;
		postcodeTabel[96] = 0;
		postcodeTabel[97] = 0;
		postcodeTabel[98] = 0;
		postcodeTabel[99] = 0;
	}
}

var map;

// Start de google map
function begin()
{
	var canvas = document.getElementById("googleMapsCanvas");
	// hou gelijk op als niet compatibel met google maps of het canvas niet op de pagina staat
	if ( !GBrowserIsCompatible() || !canvas)
		return;
	
	initialiseerPolygon();
	
	map = new GMap2( canvas );
	map.setUIToDefault();
	
	resetMap();
}

// Reset de map, haal alle markers enzo eraf en zet de polygon er weer op
function resetMap()
{
	map.clearOverlays();
	map.setCenter( new GLatLng(52.268157,5.240479), 6 );
	map.addOverlay( polygon );
}

// Checkt de postcode in welke gebied die valt, zet het in element met id "kleurGebied" en zet een marker op de map
function checkPostcode( postcode )
{
	// Kijk eerst of het een geldige postcode is
	if( !postcode.match(/^[1-9][0-9]{3}[\s]?([A-Z]|[a-z]){2}$/) )
	{
		return;
	}
	
	// Kijk in welk gebied die valt
	var postcodeKlein = postcode.substr(0,2);
	switch( postcodeTabel[postcodeKlein] )
	{
		case 0:
		case 1:
			// Als dit element er is is dit het reparatie formulier en kan je niet bestellen in het rode gebied
			if ( document.getElementById('reparatieGebiedFout') )
			{
				document.getElementById('kleurGebied').innerHTML = 'uiten ons werkgebied, Sorry wij repareren hier niet.';
			} else {
				document.getElementById('kleurGebied').innerHTML = "<span style=\"color: red;\">Rood</span>";
			}
		break;
		case 2:
		case 3:
			if ( document.getElementById('reparatieGebiedFout') )
			{
				document.getElementById('kleurGebied').innerHTML = '<span style=\"color: blue;\">Blauw</span>';
			} else {
				document.getElementById('kleurGebied').innerHTML = '<span style=\"color: green;\">Groen</span>';
			}
		break;
	}
	
	// Zet nu een marker op de map
	var geocoder = new GClientGeocoder();
	geocoder.getLocations( postcode+' netherlands', handleGeocodeCallback );
}

// Handle de geocode callback, zet de marker op de kaart en return de woonplaats
function handleGeocodeCallback( location )
{
	// Kijk of het request goed is overgekomen (Ook slechte zoekrequest zit hierin)
	if( !location || location.Status.code!=200)
	{
		alert( 'Er ging wat fout!' );
		return;
	}

	// Maak zeker dat de map weer leeg is
	resetMap();	

	// Voeg nu de marker toe aan de map, dit moet van de google TOS
	var punt = new GLatLng( location.Placemark[0].Point.coordinates[1], location.Placemark[0].Point.coordinates[0] )
	map.addOverlay( new GMarker(punt) );
	map.setCenter( punt, 9 );
	
	// Zet de woonplaats in het formulier, als die er is
	var formulierWoonplaats = document.getElementById('woonplaats');
	if( formulierWoonplaats )
	{
		formulierWoonplaats.value = location.Placemark[0]['AddressDetails'].Country.AdministrativeArea.Locality.LocalityName;
	}
}

// Functie van http://www.dustindiaz.com/getelementsbyclass/
// Gebruikt om alle inputs te krijgen door ze simpelweg een class te geven.
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function valideerFormulier()
{
	var fout = false;
	
	// Valideer postcode
	if( !document.getElementById('postcode').value.match(/^[1-9][0-9]{3}[\s]?([A-Z]|[a-z]){2}$/) )
	{
		document.getElementById('postcodeFout').innerHTML = 'De postcode moet geldig zijn';
		fout = true;
	} else {
		document.getElementById('postcodeFout').innerHTML = '';
	}
	
	// Valideer woonplaats
	if( document.getElementById('woonplaats').value.length < 2 )
	{
		document.getElementById('woonplaatsFout').innerHTML = 'De woonplaats moet minstens 3 tekens lang zijn';
		fout = true;
	} else if( document.getElementById('woonplaats').value.length > 256 ) {
		document.getElementById('woonplaatsFout').innerHTML = 'De woonplaats mag maximaal 255 tekens lang zijn';
		fout = true;
	} else {
		document.getElementById('woonplaatsFout').innerHTML = '';
	}
	
	// Valideer adres
	if( document.getElementById('adres').value.length < 2 )
	{
		document.getElementById('adresFout').innerHTML = 'Het adres moet minstens 3 tekens lang zijn';
		fout = true;
	} else if( document.getElementById('adres').value.length > 256 ) {
		document.getElementById('adresFout').innerHTML = 'Het adres mag maximaal 255 tekens lang zijn';
		fout = true;
	} else {
		document.getElementById('adresFout').innerHTML = '';
	}
	
	// Valideer voorletters
	if( document.getElementById('voorletters').value.length == 0 )
	{
		document.getElementById('voorlettersFout').innerHTML = 'De voorletters moeten ingevuld worden';
		fout = true;
	} else if( document.getElementById('voorletters').value.length > 256 ) {
		document.getElementById('voorlettersFout').innerHTML = 'De voorletters mogen maximaal 255 tekens lang zijn';
		fout = true;
	} else {
		document.getElementById('voorlettersFout').innerHTML = '';
	}
	
	// Valideer achternaam
	if( document.getElementById('achternaam').value.length == 0 )
	{
		document.getElementById('achternaamFout').innerHTML = 'De achternaam moet ingevuld worden';
		fout = true;
	} else if( document.getElementById('achternaam').value.length > 256 ) {
		document.getElementById('achternaamFout').innerHTML = 'De achternaam mag maximaal 255 tekens lang zijn';
		fout = true;
	} else {
		document.getElementById('achternaamFout').innerHTML = '';
	}
	
	// Valideer email
	if( !document.getElementById('email').value.match(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i) )
	{
		document.getElementById('emailFout').innerHTML = 'Er moet een geldig email-adress worden ingevuldt';
		fout = true;
	} else {
		document.getElementById('emailFout').innerHTML = '';
	}
	
	// Valideer telefoonnummer
	if( !document.getElementById('telefoonnummer').value.match(/^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/) )
	{
		document.getElementById('telefoonnummerFout').innerHTML = 'Er moet een geldig telefoonnummer worden ingevuldt';
		fout = true;
	} else {
		document.getElementById('telefoonnummerFout').innerHTML = '';
	}
	
	// Kijk of er wel een element is gekozen
	gekozen = false;
	inputs = getElementsByClass('input', document.getElementById('inputsTop'), 'input');
	for( var i=0; i<inputs.length; i++ )
	{
		if( inputs[i].checked == true )
			gekozen = true;
	}
	
	if( !gekozen )
	{
		document.getElementById('inputsFout').innerHTML = 'Er moet een optie zijn aangevinkt';
		fout = true;
	} else {
		document.getElementById('inputsFout').innerHTML = '';
	}
	
	// Valideer opmerking
	if( document.getElementById('opmerking').value.length > 65535 ) {
		document.getElementById('opmerkingFout').innerHTML = 'De opmerking mag maximaal 65535 tekens lang zijn';
		fout = true;
	} else {
		document.getElementById('opmerkingFout').innerHTML = '';
	}
	
	// De volgende test alleen maar op de reparatie pagina
	var reparatieGebiedFout = document.getElementById('reparatieGebiedFout');
	if( reparatieGebiedFout )
	{
		// Als een reparatie niet in het groene gebied ligt kan het niet. Kijk dus of het een reparatie is en als dat zo is of die in het groene gebied ligt.
		var postcodeKlein = document.getElementById('postcode').value.substr(0,2);
		switch( postcodeTabel[postcodeKlein] )
		{
			default:
			case 0:
			case 1:
				reparatieGebiedFout.innerHTML = 'U valt niet in het blauwe gebied en wij repareren dus niet bij U in de buurt. U kunt wel onze reparatie-breng service gebruiken.';
				fout = true;
			break;
			case 2:
			case 3:
				reparatieGebiedFout.innerHTML = '';
			break;
		}
	} else {
		// Deze test alleen maar op installatie pagina
		// Kijk of er wel een bereikbaarheid is gekozen
		if( document.getElementById('bereikbaar').value == 'geen' )
		{
			document.getElementById('bereikbaarFout').innerHTML = 'Er moet een manier zijn gekozen';
			fout = true;
		} else {
			document.getElementById('bereikbaarFout').innerHTML = '';
		}
	}
	
	// Kijk of er iets fout is gegaan, als het zo is formulier niet versturen
	if( fout )
	{
		document.getElementById('fout').innerHTML = 'Er is iets fout gegaan met het versturen van het formulier, controleer of uw ingevulde waarden correct zijn';
	}
	
	return !fout;
}
