//googlemaps code

function initialize() {      
//if (GBrowserIsCompatible()) {              
//map.setCenter(new GLatLng(37.4419, -122.1419), 13,G_PHYSICAL_MAP);        
//map.setUIToDefault();      } 
 // Add 10 markers to the map at random locations  
 //var bounds = map.getBounds();  
 //var southWest = bounds.getSouthWest();  
 //var northEast = bounds.getNorthEast();  
 //var lngSpan = northEast.lng() - southWest.lng();  
 //var latSpan = northEast.lat() - southWest.lat();  
 //for (var i = 0; i < 10; i++) {    
 //point = new GLatLng(southWest.lat() + latSpan * Math.random(),        southWest.lng() + lngSpan * Math.random());    map.addOverlay(new GMarker(point));  
// }
 }
 
 
 // The GGeoXml constructor takes a URL pointing to a KML or GeoRSS file.
 // You add the GGeoXml object to the map as an overlay, and remove it as an overlay as well.
 // The Maps API determines implicitly whether the file is a KML or GeoRSS file.

var geoXml,networkLink;
   
function gen_points(){
lyr1.style.visibility="visible";
lyr2.style.visibility="hidden";
lyr3.style.visibility="hidden";
}

function lvaerial(){
lyr1.style.visibility="hidden";
lyr2.style.visibility="visible";
lyr3.style.visibility="hidden";
}

function lv3d(){
lyr1.style.visibility="hidden";
lyr2.style.visibility="hidden";
lyr3.style.visibility="visible";
}

function view2(rad1,latz,lonz){
var point1=new GLatLng(parseFloat(latz),parseFloat(lonz));
map.setCenter(point1); 
_mPreferMetric=true;
 
 
 
////map
     
 
 
 
////pan and zoom to fit
 
var bounds = new GLatLngBounds();
function fit(){
map.panTo(bounds.getCenter()); 
map.setZoom(map.getBoundsZoomLevel(bounds));
}
 
 
///Geo
 
var geocoder = new GClientGeocoder();
 
	//calling circle drawing function
	function draw(pnt){
	//view1a();
	bounds = new GLatLngBounds();
	var givenRad = rad1;
	var givenQuality = 40*1;
	var centre = pnt || map.getCenter()
	drawCircle(centre, givenRad, givenQuality);
	fit();
	}
 
	////////////////////////// circle///////////////////////////////
	
function drawCircle(center, radius, nodes, liColor, liWidth, liOpa, fillColor, fillOpa)
{
// Esa 2006
	//calculating km/degree
	var latConv = center.distanceFrom(new GLatLng(center.lat()+0.1, center.lng()))/100;
	var lngConv = center.distanceFrom(new GLatLng(center.lat(), center.lng()+0.1))/100;
 
	//Loop 
	var points = [];
	var step = parseInt(360/nodes)||10;
	for(var i=0; i<=360; i+=step)
	{
	var pint = new GLatLng(center.lat() + (radius/latConv * Math.cos(i * Math.PI/180)), center.lng() + 
	(radius/lngConv * Math.sin(i * Math.PI/180)));
	points.push(pint);
	bounds.extend(pint); //this is for fit function
	}
	points.push(points[0]); // Closes the circle, thanks Martin
	fillColor = fillColor||liColor||"#0055ff";
	liWidth = liWidth||2;
	var poly = new GPolygon(points,liColor,liWidth,liOpa,fillColor,fillOpa);
	map.addOverlay(poly);
}
/////////////////////////////////////////////////////////////////////
 
draw();
 
//]]>

}
//calling circle drawing function
	
var ge1=0;


function view4(x,y,z,a,b,c){
if(!ge1){
//setup ge map
google.load("earth", "1"); 

google.earth.createInstance("ge_map_canvas", initCallback, failureCallback);  
ge1=1;
}
else if(!view4b){
  //createFullScreenIcon();
  //createNormalScreenIcon();
  //google.earth.addEventListener(ge.getWindow(), "click", handleMouseClick);
  view4b=1;
}
else{
ge.getFeatures().removeChild(networkLink);
}
lv3d();

var link = ge.createLink('');
var href = 'http://www.spaldingtonstop.org.uk/mapping/farm1.pl?'+x+"|"+y+"|"+z+"|"+a+"|"+b+"|"+c;
link.setHref(href);
networkLink = ge.createNetworkLink('');
networkLink.set(link, true, true); 
//ge.getFeatures().appendChild(networkLink);

ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO);
if(b=="on")ge.getSun().setVisibility(true);
else ge.getSun().setVisibility(false);
ge.getFeatures().appendChild(networkLink);
}

function view5(){
ge.getFeatures().removeChild(networkLink);
//var href = 'http://www.spaldingtonstop.org.uk/mapping/farm1.pl?'+x+"|"+y+"|"+z+"|"+a+"|"+b;
}
 
function initCallback(object) {
  ge = object;
  ge.getWindow().setVisibility(true);
  ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO);
}
 
function failureCallback(object) {
}

function showpcaddress(postcode) {
  
  localSearch.setSearchCompleteCallback(null, 
    function() {
      if (localSearch.results[0]) {    
        var resultLat = localSearch.results[0].lat;
        var resultLng = localSearch.results[0].lng;
        var point = new GLatLng(resultLat,resultLng);
        map.setCenter(point, 13,G_PHYSICAL_MAP);
			  map.clearOverlays()
var homeIcon = new GIcon(G_DEFAULT_ICON);
homeIcon.image = "/mapping/home.png";                
var markerOptions = { icon:homeIcon }; 
homeIcon.iconSize = new GSize(50, 50);
homeIcon.shadowSize = new GSize(100, 50);
//homeIcon.iconAnchor = new GPoint(6, 20);
//homeIcon.infoWindowAnchor = new GPoint(5, 1);
			  
var marker = new GMarker(point, markerOptions);
map.addOverlay(marker);



			  point1=point;
            adcheck2(0)
			checkvars();
      }else{
       adcheck2(1);
      }
    });  
    
  localSearch.execute(postcode + ", UK");
}


function showAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
             adcheck2(1)
            } else {
              map.setCenter(point, 13,G_PHYSICAL_MAP);
			  map.clearOverlays()
var homeIcon = new GIcon(G_DEFAULT_ICON);
homeIcon.image = "/mapping/home.png";                
var markerOptions = { icon:homeIcon }; 
homeIcon.iconSize = new GSize(50, 50);
homeIcon.shadowSize = new GSize(100, 50);
//homeIcon.iconAnchor = new GPoint(6, 20);
//homeIcon.infoWindowAnchor = new GPoint(5, 1);
			  
var marker = new GMarker(point, markerOptions);
map.addOverlay(marker);

			  point1=point;
            adcheck2(0)
			checkvars();
            }
          }
        )
      }   
	  }

function checkvars(){
eval("var point2=new Array"+point1+";");
r2.lat.value=point2[0];
r2.lon.value=point2[1];
topoGetAltitude(point2[0],point2[1],
function(altitude) {
r2.alt.value=altitude;
}
)
}

//dynamic text control routines
function replaceText(el, text)
{
if(el!=null){
clearText(el);
var newNode = document.createTextNode(text);
el.appendChild(newNode);
}
}
//for control frame
function replaceText1(el, text)
{
if(el!=null){
clearText(el);
var newNode = top.document.createTextNode(text);
el.appendChild(newNode);
}
}

function clearText(el)
{
if(el!=null){
if(el.childNodes){
for(var i=0;i<el.childNodes.length;i++){
var childNode = el.childNodes[i];
el.removeChild(childNode);
}
}
}
}

function getText(el)
{
var text = "";
if(el!=null){
if(el.childNodes){
for(var i=0;i<el.childNodes.length;i++){
var childNode=el.childNodes[i];
if(childNode.nodeValue!=null){
text=text+childNode.nodeValue;
}
}
}
}
return text;
}

//validate telephone number
function telval(str)
{
//counts digits only
var filter=/^[0-9]*$/;
var str1=str.split(" ");
str=str1.join("");
if ((filter.test(str)&&(str.length)&&(str.length>10)&&(str.length<40))||(str==""))
testresults=true
else{
testresults=false
}
return (testresults)
}

//validate email
function emval(str){
var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
if (filter.test(str)||(str=="tbs"))
testresults=true
else{
testresults=false
}
return (testresults)
}

//validate number
function numval(str)
{
var str1=str.split(" ");str=str1.join("");
var filter=/^[0-9]*$/;
if ((filter.test(str)&&(str.length)&&(str.length)<10)||(str=="tbs"))
testresults=true
else{
testresults=false
}
return (testresults)
}

//validate alpha
function aval(str)
{
var str1=str.split(" ");str=str1.join("");
var filter=/^[\-\/A-Za-z]*$/;
if (filter.test(str)&&(str.length)&&(str.length<40))
testresults=true
else{
testresults=false
}
return (testresults)
}

//validate alphanumeric
function anval(str)
{
var filter=/^[\-\/A-Za-z0-9()&.,\s]*$/;
if (filter.test(str)&&(str.length)&&(str.length<40))
testresults=true
else{
testresults=false
}
return (testresults)
}

//validate post code
function pcval(z)
{
if(!z)return false;
z=z.toUpperCase();
var z5=z.length;
var z1=z.split(" ");
if(z1.length>2||z5<5||z5>8)return false;
else if(z1.length>1){var ptw2a=z1[0];var ptw2b=z1[1]}
else if(z5>4){var ptw3=z5-3,ptw2a=z.substring(0,ptw3),ptw2b=z.substring(ptw3,z5)}
if(aval(ptw2a.substring(0,1))&& numval(ptw2b.substring(0,1))&& aval(ptw2b.substring(1,3))){
if(ptw2a.length>3)return (ptw2a+ptw2b);
else return(ptw2a+" "+ptw2b)
}
else return false;
}

function ercheck(x,y){
var ad1=document.getElementById("er"+x);
ad1.src=(y)?"images/tick_sml.gif":"images/cross_sml.gif";
ad1.style.visibility="visible";
}

function adcheck(){
lytop.style.display="block";
lyad9.style.display="none";
if(!adverify){
//if(r2.zip.value && !pcval(r2.zip.value)){alert("Please enter a correctly formatted post code or leave blank");r2.zip.focus();return;}
//if(r2.zip.value)r2.zip.value=pcval(r2.zip.value);
lyad6.style.display="none";
lyintro.style.display="none";
var ad2=r2.add1.value+","+r2.add2.value+","+r2.town.value+","+r2.city.value+","+r2.county.value+","+r2.country.options[r2.country.selectedIndex].text+","+r2.zip.value;
if(r2.zip.value)showpcaddress(r2.zip.value);
else showAddress(ad2);
}
else{
for(n=4;n<11;n++)ercheck(n,1);
userverify=1;
for(n=0;n<4;n++)ercheck(n,1);
ercheck(11,1);
if(!anval(r2.username.value)){ercheck(0,0);userverify=0;}
if(!anval(r2.password.value)){ercheck(1,0);userverify=0;}
if(r2.email1.value!=r2.email2.value){ercheck(2,0);ercheck(3,0);userverify=0;}
else if(!emval(r2.email1.value)){ercheck(2,0);ercheck(3,0);userverify=0;}
if(r2.tc.checked==false){ercheck(11,0);userverify=0;}

lyad6.style.display=(userverify)?"none":"block";
lyreg.style.display=(userverify)?"none":"block";

if(userverify){
r2.submit();
}
}
}

function adcheck2(x){
lyad4.style.display=(x)?"none":"block";
lyad5.style.display=(x)?"none":"none";
lymap.style.visibility=(x)?"hidden":"visible";
lyad3.style.display=(x)?"block":"none";
lyreg.style.display=(x)?"block":"none";
if(x){for(n=4;n<11;n++){
ercheck(n,0);
}
}
}

function adcheck3(x){
lyad3.style.display=(x)?"none":"none";
lyad4.style.display=(x)?"none":"none";
lyad5.style.display=(x)?"none":"block";
lyreg.style.display=(x)?"none":"block";
lymap.style.visibility="hidden";
adverify=x;
if(x)adcheck();
else{
for(n=4;n<10;n++){
var ad1=document.getElementById("er"+n);
ad1.src="images/cross_sml.gif";ad1.style.visibility="visible";
}
}
}

function regon(){
lywreg.style.display="none";
lylog.style.display="none";
lyad7.style.display="none";
lyad9.style.display="none";
if(lyad10!=null)lyad10.style.display="none";
lytop.style.display=(reg1)?"block":"none";
replaceText(lyad8,(reg1)?"Cancel":"Register")
lyreg.style.display=(reg1)?"block":"none";
lyintro.style.display=(reg1)?"none":"block";
if(lyintro2!=null)lyintro2.style.display="none";
reg1=(reg1)?0:1;
if(reg1){
lymap.style.visibility="hidden";
lytop.style.display="none";
lyad9.style.display="none";
}
}
function whyregister(){
reg1=0;
regon();
lyintro.style.display=(lwreg1)?"none":"block";
lyreg.style.display="none";
lywreg.style.display=(lwreg1)?"block":"none";
replaceText(lwreg0,(lwreg1)?"The facts!":"Why Register?");
lwreg1=(lwreg1)?0:1;
}

function log_on(){
replaceText(lyad8,"Register");
lylog.style.display="block";
lyreg.style.display="none";
lyintro.style.display="none";
lywreg.style.display="none";
if(lyintro2!=null)lyintro2.style.display="none";
lymap.style.visibility="hidden";
lytop.style.display="none";
lyad9.style.display="none";
if(lyad10!=null)lyad10.style.display="none";
}

function logout(){
if(confirm("Are you sure you wish to log out?")){
Set_Cookie("blog",1);
location.href="../logout.pl";
}
}

function sub1(){
if(fpa)fpsend();
else{
r3.action="cgi-bin/login.pl";
lyad9.style.display="none";
ercheck(12,1);ercheck(13,1);logverify=1;
if(!anval(r3.username1.value)){ercheck(12,0);logverify=0;}
if(!anval(r3.password1.value)){ercheck(13,0);logverify=0;}
if(!logverify){
lytop.style.display="block";
lyad7.style.display="block";
lyad3.style.display="none";
if(lyad10!=null)lyad10.style.display="none";
}
else r3.submit();
}
}

var MM_contentVersion = 6;
var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
if ( plugin ) {
		var words = navigator.plugins["Shockwave Flash"].description.split(" ");
	    for (var i = 0; i < words.length; ++i)
	    {
		if (isNaN(parseInt(words[i])))
		continue;
		var MM_PluginVersion = words[i]; 
	    }
	var MM_FlashCanPlay = MM_PluginVersion >= MM_contentVersion;
}
else if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 
   && (navigator.appVersion.indexOf("Win") != -1)) {
	document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n'); //FS hide this from IE4.5 Mac by splitting the tag
	document.write('on error resume next \n');
	document.write('MM_FlashCanPlay = ( IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & MM_contentVersion)))\n');
	document.write('</SCR' + 'IPT\> \n');
}

function res1(){
for(n=0;n<r2.elements.length-2;n++)if(r2.elements[n]!=r2.country)r2.elements[n].value="";
adverify=0;
userverify=0;
logverify=0;
}

function sub2(x,y){
if(!x){alert("This section is in development");return}
r4.page.value=x;
r4.type.value=y;
r4.submit();
}

function sub3(){
var r6=document.form6;
if(!aval(r6.name.value) && r6.name.value){alert("Please supply a valid name or leave blank");r6.name.focus();return;}
if(!anval(r6.add1.value) && r6.add1.value){alert("Please supply a valid address or leave blank");r6.add1.focus();return;}
if(!anval(r6.add2.value) && r6.add2.value){alert("Please supply a valid address or leave blank");r6.add2.focus();return;}
if(!aval(r6.town.value) && r6.town.value){alert("Please supply a valid town or leave blank");r6.town.focus();return;}
if(!aval(r6.county.value) && r6.county.value){alert("Please supply a valid county or leave blank");r6.county.focus();return;}
if(!aval(r6.country.value) && r6.country.value){alert("Please supply a valid Country or leave blank");r6.country.focus();return;}
if(!emval(r6.email.value) || !r6.email.value){alert("Please supply a valid email address");r6.email.focus();return;}
if(!telval(r6.tel.value) && r6.tel.value){alert("Please supply a valid telephone number or leave blank");r6.tel.focus();return;}
if(!r6.details.value){alert("Please supply some details to your request");r6.deatils.focus();return;}
if(r6.reason.selectedIndex==0){alert("Please indicate the reason for your contact");r6.reason.focus();return;}
r6.submit();
}



 var fullScreenState = false;
    var noFullScreenIcon = null;
    var fullScreenIcon = null;


function handleMouseClick(event){
var INSET_PIXELS_X = document.getElementById("ge_map_canvas").offsetWidth - event.getClientX();
var INSET_PIXELS_Y = event.getClientY();
if (INSET_PIXELS_X < 32){
if (INSET_PIXELS_Y < 32){ 
toggleFullScreen();}
}  
}
 
function toggleFullScreen(){
  if (fullScreenState == true) {makeNormalScreen();}
  else {makeFullScreen();}
}
 
function makeFullScreen(){
   var samplecontainer = document.getElementById('fullscreencontainer');
   var container = document.getElementById('ge_map_canvas');
   container.style.left = 0; 
   container.style.top = 0;
   container.style.width = samplecontainer.offsetWidth + 'px'; 
   container.style.height = samplecontainer.offsetHeight + 'px';
   fullScreenState = true;
   noFullScreenIcon.setVisibility(fullScreenState);
   fullScreenIcon.setVisibility(!fullScreenState);  
}
 
function makeNormalScreen(){
   var samplecontainer = document.getElementById('sizecontainer');
   var container = document.getElementById('ge_map_canvas');
   container.style.left = samplecontainer.style.left; 
   container.style.top = samplecontainer.style.top;
   container.style.width = samplecontainer.offsetWidth + 'px'; 
   container.style.height = samplecontainer.offsetHeight + 'px';
   fullScreenState = false;
   noFullScreenIcon.setVisibility(fullScreenState);
   fullScreenIcon.setVisibility(!fullScreenState);
}
 
function createFullScreenIcon() {
    // create an image for the screen overlay
    var icon = ge.createIcon('');
    icon.setHref('http://www.spaldingtonstop.org.uk/mapping/images/tofull.png');
    // create the screen overlay
    fullScreenIcon = ge.createScreenOverlay('');
    fullScreenIcon.setDrawOrder(60);
    fullScreenIcon.setIcon(icon);
    // anchor point in top left of icon.
    fullScreenIcon.getScreenXY().setXUnits(ge.UNITS_FRACTION);
    fullScreenIcon.getScreenXY().setYUnits(ge.UNITS_FRACTION);
    fullScreenIcon.getScreenXY().setX(1);
    fullScreenIcon.getScreenXY().setY(1);
    // place icon in top left of screen.
    fullScreenIcon.getOverlayXY().setXUnits(ge.UNITS_INSET_PIXELS);
    fullScreenIcon.getOverlayXY().setYUnits(ge.UNITS_INSET_PIXELS);
    fullScreenIcon.getOverlayXY().setX(2);
    fullScreenIcon.getOverlayXY().setY(4);
    // Set icon size.
    fullScreenIcon.getSize().setXUnits(ge.UNITS_PIXELS);
    fullScreenIcon.getSize().setYUnits(ge.UNITS_PIXELS);
    fullScreenIcon.getSize().setY(32);
    fullScreenIcon.getSize().setX(32);
    // add the screen overlay to Earth
    ge.getFeatures().appendChild(fullScreenIcon);
}
 
function createNormalScreenIcon() {
    // create an image for the screen overlay
    var icon = ge.createIcon('');
    icon.setHref('http://www.spaldingtonstop.org.uk/mapping/images/tonormal.png');
    // create the screen overlay
    noFullScreenIcon = ge.createScreenOverlay('');
    noFullScreenIcon.setDrawOrder(62);
    noFullScreenIcon.setIcon(icon);
    // anchor point in top left of icon.
    noFullScreenIcon.getScreenXY().setXUnits(ge.UNITS_FRACTION);
    noFullScreenIcon.getScreenXY().setYUnits(ge.UNITS_FRACTION);
    noFullScreenIcon.getScreenXY().setX(1);
    noFullScreenIcon.getScreenXY().setY(1);
    // place icon in top right of screen.
    noFullScreenIcon.getOverlayXY().setXUnits(ge.UNITS_INSET_PIXELS);
    noFullScreenIcon.getOverlayXY().setYUnits(ge.UNITS_INSET_PIXELS);
    noFullScreenIcon.getOverlayXY().setX(2);
    noFullScreenIcon.getOverlayXY().setY(4);
    // Set icon size.
    noFullScreenIcon.getSize().setXUnits(ge.UNITS_PIXELS);
    noFullScreenIcon.getSize().setYUnits(ge.UNITS_PIXELS);
    noFullScreenIcon.getSize().setY(32);
    noFullScreenIcon.getSize().setX(32);
    noFullScreenIcon.setVisibility(false);
    // add the screen overlay to Earth
    ge.getFeatures().appendChild(noFullScreenIcon);
}
 
function handleFullScreen(){
  if (window.innerWidth == screen.width){
    if (window.innerHeight > screen.height - 10){
      //this is likely caused by pressing F11 on the browser
      makeFullScreen();
    } else if (fullScreenState == true) { makeNormalScreen();}
  } else {makeNormalScreen();}
}
 
 
 //read cookie
function Get_Cookie(name) { 
   var start = document.cookie.indexOf(name+"="); 
   var len = start+name.length+1; 
   if ((!start) && (name != document.cookie.substring(0,name.length))) return null; 
   if (start == -1) return null; 
   var end = document.cookie.indexOf(";",len); 
   if (end == -1) end = document.cookie.length; 
   return unescape(document.cookie.substring(len,end)); 
} 
//set cookie
function Set_Cookie(name,value,expires,path,domain,secure) { 
    var cookieString = name + "=" +escape(value) + 
       ( (expires) ? ";expires=" + expires.toGMTString() : "") + 
       ( (path) ? ";path=" + path : "") + 
       ( (domain) ? ";domain=" + domain : "") + 
       ( (secure) ? ";secure" : ""); 
    document.cookie = cookieString; 
} 
//delete cookie
function Delete_Cookie(name,path,domain) { 
   if (Get_Cookie(name)) document.cookie = name + "=" + 
      ( (path) ? ";path=" + path : "") + 
      ( (domain) ? ";domain=" + domain : "") + 
      ";expires=Thu, 01-Jan-70 00:00:01 GMT"; 
} 
