function mouseOverRegion ( region ) {
	document.getElementById('UK_map').src = '../clients/www.explorelearning.co.uk/img/map/map_mo_'+ region + '.gif';
}
function mouseOutRegion (){
	document.getElementById('UK_map').src = '../clients/www.explorelearning.co.uk/img/map/map_mo.gif';

}

function isValidPostcode(p) {
	var postcodeRegEx = /[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}/i;
	return postcodeRegEx.test(p);
}

function isPartialPostcode(p){
	var postcodeRegEx = /[A-Z]{1,2}[0-9]{1,2}/i;
	return postcodeRegEx.test(p);
}

function isValidPostcodeOrName(s) {
	var validPostCode = isValidPostcode(s);
	if ( !validPostCode && isPartialPostcode(s)){
	    return false;
	}else{
	    return true;
	}
}

/*	formats a VALID postcode nicely: AB120XY -> AB1 0XY */
function formatPostcode(p) {
    if (isValidPostcode(p)) {
	var postcodeRegEx = /(^[A-Z]{1,2}[0-9]{1,2})([0-9][A-Z]{2}$)/i;
	return p.replace(postcodeRegEx,"$1 $2");
    } else {
	return p;
    }
}

function load() {
			  if (GBrowserIsCompatible()) {
			    var map = new GMap2(document.getElementById("map"));
			    map.addControl(new GSmallMapControl());
			    map.addControl(new GMapTypeControl());
			    map.setCenter(new GLatLng(53.75796,-2.705729), 6);
			  }
			}

			var map;
			var icon0;
			var newpoints = new Array();
			var markers = new Array();


			function addLoadEvent(func) {
			    var oldonload = window.onload;
			    if (typeof window.onload != "function"){
			        window.onload = func
			    } else {
			        window.onload = function() {
			            oldonload();
			            func();
			        }
			    }
			}

			function loadMap() {
			    map = new GMap2(document.getElementById("map"));
			    map.addControl(new GLargeMapControl());
			    map.addControl(new GMapTypeControl());
			    map.setCenter(new GLatLng( 53.75796,-2.705729), 6);
			    icon0 = new GIcon();
			    icon0.image = "http://www.google.com/mapfiles/marker.png";
			    icon0.shadow = "http://www.google.com/mapfiles/shadow50.png";
			    icon0.iconSize = new GSize(20, 34);
			    icon0.shadowSize = new GSize(37, 34);
			    icon0.iconAnchor = new GPoint(9, 34);
			    icon0.infoWindowAnchor = new GPoint(9, 2);
			    icon0.infoShadowAnchor = new GPoint(18, 25);

			}

			function createMarker(point, icon, strTitle, popuphtml) {
			    var popuphtml = "<div id=\"popup\">" + popuphtml + "<\/div>";
			    var marker = new GMarker(point, {title:strTitle});
			    GEvent.addListener(marker, "click", function() {
			        marker.openInfoWindowHtml(popuphtml);
			    });
			    return marker;
			}

			function closestMarkerSearch(map, markers, zoom, address, strRedirect ) {
			    if (markers.length == 0) {
			        throw new Exception("No markers given");
			    }
			    arrDistances = new Array;
				objMap = document.getElementById( "mpContent" );
				objGoogleMap = document.getElementById( "map" );
				if (address.indexOf(" ") == "-1") {
					if (address.length == 7){
						address = (address.substring(0,4) + " " + address.substring(4));
					}
					if (address.length == 6){
						address = (address.substring(0,3) + " " + address.substring(3));
					}
				}

			    new GClientGeocoder().getLatLng(address, function(latLng) {
			        if (latLng === null) {
			            alert("Please enter a postcode");
			        } else {
			            var distance;
			            var closestDistance = null;
			            var closestMarker = null;
				    var strAddress = strRedirect;
				    var arrCentres = [];
				    var arrCoords = [];
			            for (var i = 0; i < markers.length; i++) {
			                distance = latLng.distanceFrom(markers[i].getLatLng());
			                arrDistances[arrDistances.length] = new Array( distance, markers[i] );
			            }
			            arrDistances.sort(function(a,b){return a[0] - b[0]});

				   for ( var i = 0; (i < arrDistances.length && i < 5 ); i++ ){
					     arrTitleInfo = arrDistances[i][1].getTitle().split('|');
					     strTitle = arrTitleInfo[0];
					     idWidgetApp = arrTitleInfo[1];

					      arrCentres[i] = idWidgetApp;
					      arrCoords[i] = arrDistances[i][0];
				    }
				    document.location = strRedirect + "&arrCentres=" + arrCentres + "&arrCoords=" + arrCoords;
			        }
			    });
			}

			function closestMarkerPointsSearch(markers, address) {
			    if (markers.length == 0) {
			        throw new Exception("No markers given");
			    }
			    arrDistances = new Array;
				if (address.indexOf(" ") == "-1") {
					if (address.length == 7){
						address = (address.substring(0,4) + " " + address.substring(4));
					}
					if (address.length == 6){
						address = (address.substring(0,3) + " " + address.substring(3));
					}
				}
			    new GClientGeocoder().getLatLng(address, function(latLng) {
			        if (latLng === null || ! isValidPostcode( address )){
			            alert("Please enter a full valid uk postcode");
			        } else {
			            var distance;
			            var closestDistance = null;
			            var closestMarker = null;
				    var elementTitle = null;
				    var arrTitleInfo = null;
				    var strTitle = null;
				    var idWidgetApp = null;
			            for (var i = 0; i < markers.length; i++) {
			                distance = latLng.distanceFrom(markers[i].getLatLng());
			                arrDistances[arrDistances.length] = new Array( distance, markers[i] );
			            }
			            arrDistances.sort(function(a,b){return a[0] - b[0]});
				    //document.getElementById('centreSelect').style.display = 'table-row';
				    if ( arrDistances.length > 0 ){
					 arrTitleInfo = arrDistances[0][1].getTitle().split('|');
					 strTitle = arrTitleInfo[0];
					 idWidgetApp = arrTitleInfo[1];
					 objOption = document.getElementById('idCentreWidget');
					 objOption.value = idWidgetApp;
//					for ( var i = 0; (i < arrDistances.length && i < 5 ); i++ ){
//					     arrTitleInfo = arrDistances[i][1].getTitle().split('|');
//					     strTitle = arrTitleInfo[0];
//					     idWidgetApp = arrTitleInfo[1];
//					     objOption = new Option( strTitle )
//					     objOption.value = idWidgetApp;
//					     document.getElementById( 'idCentreWidget' ).options[ i+1 ] = objOption;
//					}
				    }
//				    else{
//					objOption = new Option( 'No centres found' );
//					document.getElementById('strCentre').options[0] = objOption;
//				    }
				}
			    });
			}

			function closestMarkerPointsRedirect(markers, address, strRedirect, intMilesBoundary) {
			   if (markers.length == 0) {
			        throw new Exception("No markers given");
			    }
			    if ( address === null || ! ( address.length ) || address === "Enter your postcode" ){
				alert("Please enter a postcode/place name");
			    }else{

				if ( ! isValidPostcodeOrName( address ) ){
				    alert('Please enter a full valid uk postcode/place name');
				    return false;
				}else{

				    var formattedAddress = formatPostcode(address);
				    arrDistances = new Array;
                    var geo = new GClientGeocoder();
                    geo.setBaseCountryCode( 'GB' );
                    var searchFriendlyAddress = formattedAddress;
                    formattedAddress += ' GB';
				    geo.getLatLng(formattedAddress, function(latLng) {

					var distance;
					var closestDistance = null;
					var closestMarker = null;
					var strAddress = strRedirect;
					var arrCentres = [];
					var arrCoords = [];
					var intMetresBoundary = intMilesBoundary * 1609.344;
					for (var i = 0; i < markers.length; i++) {
					    distance = latLng.distanceFrom(markers[i].getLatLng());
//					    if ( distance <= intMetresBoundary ){
						arrDistances[arrDistances.length] = new Array( distance, markers[i] );
//					    }
					}

					arrDistances.sort(function(a,b){return a[0] - b[0]});

				       for ( var i = 0; (i < arrDistances.length && i < 5 ); i++ ){
						 arrTitleInfo = arrDistances[i][1].getTitle().split('|');
						 strTitle = arrTitleInfo[0];
						 idWidgetApp = arrTitleInfo[1];

						  arrCentres[i] = idWidgetApp;
						  arrCoords[i] = arrDistances[i][0];
					}
					document.location = strRedirect + "&arrCentres=" + arrCentres + "&arrCoords=" + arrCoords + "&strCentreSearch=" + searchFriendlyAddress;

				    });
				}
			    }
			}

//			function hideCentreOptions(){
//			    document.getElementById('centreSelect').style.display = 'none';
//			}
