
var SPEED_FILE_PREFIX_NI='Speed10i';var SPEED_FILE_PREFIX_GB='Speed10';var postcode;var allGrids=new Array();function isNI(pCode){if(pCode.substring(0,2)=='bt'||pCode.substring(0,2)=='BT'){return true;}else{return false;}}
function getAllGridWindspeeds(){return allGrids;}
function pcaGeocodePostcodeBegin(postcode,building,accuracy,account_code,license_code,callback)
{var scriptTag=document.getElementById("pcaScript");var headTag=document.getElementsByTagName("head").item(0);var strUrl="";strUrl="http://services.postcodeanywhere.co.uk/inline.aspx?";strUrl+="&action=geocode";strUrl+="&postcode="+escape(postcode);strUrl+="&building="+escape(building);strUrl+="&accuracy="+escape(accuracy);strUrl+="&account_code="+escape(account_code);strUrl+="&license_code="+escape(license_code);strUrl+="&callback="+callback;if(scriptTag)
{try
{headTag.removeChild(scriptTag);}
catch(e)
{}}
scriptTag=document.createElement("script");scriptTag.src=strUrl
scriptTag.type="text/javascript";scriptTag.id="pcaScript";headTag.appendChild(scriptTag);}
function pcaGeocodePostcodeEnd()
{if(pcaIsError)
{alert(pcaErrorMessage);}
else
{if(pcaRecordCount==0)
{alert("Sorry, no matching items found");}
else
{var mainCoord=new postCodeCoordinates(pca_location,pca_grid_east_m,pca_grid_north_m,pca_longitude,pca_latitude,pca_os_reference,pca_wgs84_longitude,pca_wgs84_latitude);allGrids=generateWindSpeed(mainCoord);}}}
function postCodeCoordinates(location,easting,northing,lat,lng,wgs84_latitude,wgs84_longitude,osref){this.location=location;this.easting=easting;this.northing=northing;this.lng=lng;this.lat=lat;this.osref=osref;this.wgs84_lng=wgs84_longitude;this.wgs84_lat=wgs84_latitude;}
function OSRef(east,north){this.easting=east;this.northing=north;this.toString=OSRefToString(this);}
function generateWindSpeed(coord,pc){postcode=pCode;var osRef;if(isNI(postcode)){gridRef=getGridRef(coord.wgs84_lat,coord.wgs84_lng,pc);osRef=new OSRef(gridRef.easting,gridRef.northing);}else{osRef=new OSRef(coord.easting,coord.northing);}
var mainDataKey=generateKeyFromOsRef(osRef);var mainDataFile=getDataFileName(osRef,pCode);var mainWindSpeed=getWindSpeedFromDatakeyAndFile(mainDataKey,mainDataFile);var osX=osRef.easting.toString();var osY=osRef.northing.toString();var x3=osX.substring(0,3);var y3=osY.substring(0,3);var x2=osX.substring(0,2);var y2=osY.substring(0,2);var xroot;var yroot;if(osX.length==6){xroot=x3;}else if(osX.length==5){xroot=x2;}
if(osY.length==6){yroot=y3;}else if(osY.length==5){yroot=y2;}
var os2temp=new OSRef(xroot+'500',yroot+'500');var grid2LatTemp;var grid2LngTemp;if(isNI(postcode)){grid2LatTemp=convertOSRefToIrelandLatLng(os2temp).lat;grid2LngTemp=convertOSRefToIrelandLatLng(os2temp).lng;}else{grid2LatTemp=convertOsRefToLatLng(os2temp).lat;grid2LngTemp=convertOsRefToLatLng(os2temp).lng;}
var gridOriginal=new Array();gridOriginal[0]='0';gridOriginal[1]=coord.wgs84_lat;gridOriginal[2]=coord.wgs84_lng;var grid0=new Array();grid0[0]=mainWindSpeed;grid0[1]=grid2LatTemp;grid0[2]=grid2LngTemp;allGrids[0]=gridOriginal;allGrids[1]=grid0;getOtherGridWindSpeeds(osRef);return allGrids;}
function getGridRef(lat,lng,postcode){var gridRef;if(isNI(postcode)){gridRef=LLtoIrelandNE(lat,lng);}else{var osref=getOSGridValue(lat,lng);gridRef=new GridRef();gridRef.easting=osref.easting;gridRef.northing=osref.northing;}
return gridRef;}
function getWindSpeedFromDatakeyAndFile(datakey,datafile){var doc=CJL_loadXmlDocument("data/"+datafile);var windSpeed=processDocument(doc,datakey);return windSpeed;}
function getOtherGridWindSpeeds(osRef){var osX=osRef.easting.toString();var osY=osRef.northing.toString();var x3=osX.substring(0,3);var y3=osY.substring(0,3);var x2=osX.substring(0,2);var y2=osY.substring(0,2);var xroot;var yroot;if(osX.length==6){x=x3
xroot=x3;}else if(osX.length==5){x=x2
xroot=x2;}
if(osY.length==6){y=y3
yroot=y3;}else if(osY.length==5){y=y2
yroot=y2;}
var grid1=new Array();var grid1X=Number(x)-1;var grid1Y=Number(y)+1;var grid1Key=generateKeyFromXY(grid1X,grid1Y);var grid1DataFile=getDataFileNameFromY(grid1Y);var grid1WindSpeed=getWindSpeedFromDatakeyAndFile(grid1Key,grid1DataFile);grid1X=Number(xroot)-1;grid1Y=Number(yroot)+1;var os1=new OSRef(grid1X+'500',grid1Y+'500');var grid1Lat;var grid1Lng;if(isNI(postcode)){grid1Lat=convertOSRefToIrelandLatLng(os1).lat;grid1Lng=convertOSRefToIrelandLatLng(os1).lng;}else{grid1Lat=convertOsRefToLatLng(os1).lat;grid1Lng=convertOsRefToLatLng(os1).lng;}
grid1[0]=grid1WindSpeed;grid1[1]=grid1Lat;grid1[2]=grid1Lng;var grid2=new Array();var grid2Y=Number(y)+1;var grid2Key=generateKeyFromXY(x,grid2Y);var grid2DataFile=getDataFileNameFromY(grid2Y);var grid2WindSpeed=getWindSpeedFromDatakeyAndFile(grid2Key,grid2DataFile);grid2Y=Number(yroot)+1;var os2=new OSRef(x3+'500',grid2Y+'500');var grid2Lat;var grid2Lng;if(isNI(postcode)){grid2Lat=convertOSRefToIrelandLatLng(os2).lat;grid2Lng=convertOSRefToIrelandLatLng(os2).lng;}else{grid2Lat=convertOsRefToLatLng(os2).lat;grid2Lng=convertOsRefToLatLng(os2).lng;}
grid2[0]=grid2WindSpeed;grid2[1]=grid2Lat;grid2[2]=grid2Lng;var grid3=new Array();var grid3X=Number(x)+1;var grid3Y=Number(y)+1;var grid3Key=generateKeyFromXY(grid3X,grid3Y);var grid3DataFile=getDataFileNameFromY(grid3Y);var grid3WindSpeed=getWindSpeedFromDatakeyAndFile(grid3Key,grid3DataFile);grid3X=Number(xroot)+1;grid3Y=Number(yroot)+1;var os3=new OSRef(grid3X+'500',grid3Y+'500');var grid3Lat;var grid3Lng;if(isNI(postcode)){grid3Lat=convertOSRefToIrelandLatLng(os3).lat;grid3Lng=convertOSRefToIrelandLatLng(os3).lng;}else{grid3Lat=convertOsRefToLatLng(os3).lat;grid3Lng=convertOsRefToLatLng(os3).lng;}
grid3[0]=grid3WindSpeed;grid3[1]=grid3Lat;grid3[2]=grid3Lng;var grid4=new Array();var grid4X=Number(x)+1;var grid4Key=generateKeyFromXY(grid4X,y);var grid4DataFile=getDataFileNameFromY(y);var grid4WindSpeed=getWindSpeedFromDatakeyAndFile(grid4Key,grid4DataFile);grid4X=Number(xroot)+1;var os4=new OSRef(grid4X+'500',yroot+'500');var grid4Lat;var grid4Lng;if(isNI(postcode)){grid4Lat=convertOSRefToIrelandLatLng(os4).lat;grid4Lng=convertOSRefToIrelandLatLng(os4).lng;}else{grid4Lat=convertOsRefToLatLng(os4).lat;grid4Lng=convertOsRefToLatLng(os4).lng;}
grid4[0]=grid4WindSpeed;grid4[1]=grid4Lat;grid4[2]=grid4Lng;var grid5=new Array();var grid5X=Number(x)+1;var grid5Y=Number(y)-1;var grid5Key=generateKeyFromXY(grid5X,grid5Y);var grid5DataFile=getDataFileNameFromY(grid5Y);var grid5WindSpeed=getWindSpeedFromDatakeyAndFile(grid5Key,grid5DataFile);grid5X=Number(xroot)+1;grid5Y=Number(yroot)-1;var os5=new OSRef(grid5X+'500',grid5Y+'500');var grid5Lat;var grid5Lng;if(isNI(postcode)){grid5Lat=convertOSRefToIrelandLatLng(os5).lat;grid5Lng=convertOSRefToIrelandLatLng(os5).lng;}else{grid5Lat=convertOsRefToLatLng(os5).lat;grid5Lng=convertOsRefToLatLng(os5).lng;}
grid5[0]=grid5WindSpeed;grid5[1]=grid5Lat;grid5[2]=grid5Lng;var grid6=new Array();var grid6Y=Number(y)-1;var grid6Key=generateKeyFromXY(x,grid6Y);var grid6DataFile=getDataFileNameFromY(grid6Y);var grid6WindSpeed=getWindSpeedFromDatakeyAndFile(grid6Key,grid6DataFile);grid6Y=Number(yroot)-1;var os6=new OSRef(x3+'500',grid6Y+'500');var grid6Lat;var grid6Lng;if(isNI(postcode)){grid6Lat=convertOSRefToIrelandLatLng(os6).lat;grid6Lng=convertOSRefToIrelandLatLng(os6).lng;}else{grid6Lat=convertOsRefToLatLng(os6).lat;grid6Lng=convertOsRefToLatLng(os6).lng;}
grid6[0]=grid6WindSpeed;grid6[1]=grid6Lat;grid6[2]=grid6Lng;var grid7=new Array();var grid7X=Number(x)-1;var grid7Y=Number(y)-1;var grid7Key=generateKeyFromXY(grid7X,grid7Y);var grid7DataFile=getDataFileNameFromY(grid7Y);var grid7WindSpeed=getWindSpeedFromDatakeyAndFile(grid7Key,grid7DataFile);grid7X=Number(xroot)-1;grid7Y=Number(yroot)-1;var os7=new OSRef(grid7X+'500',grid7Y+'500');var grid7Lat;var grid7Lng;if(isNI(postcode)){grid7Lat=convertOSRefToIrelandLatLng(os7).lat;grid7Lng=convertOSRefToIrelandLatLng(os7).lng;}else{grid7Lat=convertOsRefToLatLng(os7).lat;grid7Lng=convertOsRefToLatLng(os7).lng;}
grid7[0]=grid7WindSpeed;grid7[1]=grid7Lat;grid7[2]=grid7Lng;var grid8=new Array();var grid8X=Number(x)-1;var grid8Key=generateKeyFromXY(grid8X,y);var grid8DataFile=getDataFileNameFromY(y);var grid8WindSpeed=getWindSpeedFromDatakeyAndFile(grid8Key,grid8DataFile);grid8X=Number(xroot)-1;var os8=new OSRef(grid8X+'500',yroot+'500');var grid8Lat;var grid8Lng;if(isNI(postcode)){grid8Lat=convertOSRefToIrelandLatLng(os8).lat;grid8Lng=convertOSRefToIrelandLatLng(os8).lng;}else{grid8Lat=convertOsRefToLatLng(os8).lat;grid8Lng=convertOsRefToLatLng(os8).lng;}
grid8[0]=grid8WindSpeed;grid8[1]=grid8Lat;grid8[2]=grid8Lng;allGrids[2]=grid1;allGrids[3]=grid2;allGrids[4]=grid3;allGrids[5]=grid4;allGrids[6]=grid5;allGrids[7]=grid6;allGrids[8]=grid7;allGrids[9]=grid8;return allGrids;}
function getDataFileName(osRef){var osY=osRef.northing.toString();var y;if(osY.length==6){y=osY.substring(0,3);}else if(osY.length==5){y=osY.substring(0,2);}
return getDataFileNameFromY(y);}
function getDataFileNameFromY(y){var yroot=(Math.floor(Number(y/100)))*100;var fileRoot='';if(yroot>=0&&yroot<100){fileRoot='0';}else if(yroot>=100&&yroot<200){fileRoot='100';}else if(yroot>=200&&yroot<300){fileRoot='200';}else if(yroot>=300&&yroot<400){fileRoot='300';}else if(yroot>=400&&yroot<500){fileRoot='400';}else if(yroot>=500&&yroot<600){fileRoot='500';}else if(yroot>=600&&yroot<700){fileRoot='600';}else if(yroot>=700&&yroot<800){fileRoot='700';}else if(yroot>=800&&yroot<900){fileRoot='800';}else if(yroot>=900&&yroot<1000){fileRoot='900';}else if(yroot>=1000&&yroot<1100){fileRoot='1000';}else if(yroot>=1100&&yroot<1200){fileRoot='1100';}else if(yroot>=1200&&yroot<1300){fileRoot='1200';}
var speedRegion='';if(postcode.substring(0,2)=='bt'||postcode.substring(0,2)=='BT'){speedRegion=SPEED_FILE_PREFIX_NI;}else{speedRegion=SPEED_FILE_PREFIX_GB;}
var keyFile=speedRegion+"-"+fileRoot+".xml";return keyFile;}
function generateKeyFromOsRef(osRef){var osX=osRef.easting.toString();var osY=osRef.northing.toString();var x;var y;if(osX.length==6){x=osX.substring(0,3);}else if(osX.length==5){x=osX.substring(0,2);}
if(osY.length==6){y=osY.substring(0,3);}else if(osY.length==5){y=osY.substring(0,2);}
return generateKeyFromXY(x,y);}
function generateKeyFromXY(x,y){var rem=x%100;var xroot=(Math.floor(Number(x/100)))*100;var key='key_'+xroot+'-'+y;var keyRem=new Array();keyRem[0]=key;keyRem[1]=rem;return keyRem;}
function getOSGridValue(lat,lng){var ll2=new LatLng(lat,lng);var os2=ll2.toOSRef();var sixDigitOs2=os2.toSixFigureString();var os6=sixDigitOs2;alert("LR Value:"+os6+" This is only for testing will be removed when live.");var os6x=getOSRefFromSixFigureReference(os6);return os6x;}
function getWGSGridValue(lat,lng){var ll2=new LatLng(lat,lng);ll2.OSGB36ToWGS84();var os2=ll2.toOSRef();var sixDigitOs2=os2.toSixFigureString();var os6=sixDigitOs2;var os6x=getOSRefFromSixFigureReference(os6);return os6x;}
function processDocument(doc,key){var holders=new Array();holders=doc.getElementsByTagName(key[0]);var valueHolder=holders[0];var val=valueHolder.getAttribute("v_"+key[1]);return val;}
function CJL_loadXmlDocument(xmlFile,cbFun)
{var xdoc;if(window.ActiveXObject&&/Win/.test(navigator.userAgent))
{xdoc=new ActiveXObject("Msxml2.DOMDocument.5.0");xdoc.async=false;xdoc.load(xmlFile);return xdoc;}
else if(document.implementation&&document.implementation.createDocument)
{xdoc=document.implementation.createDocument("","",null);xdoc.async=false;var loaded=xdoc.load(xmlFile);if(loaded){return xdoc;}}
else
{return false;}}
function convertOSRefToIrelandLatLng(osref){var major=6377340.189;var minor=6356034.447;var ecc=((major*major)-(minor*minor))/(major*major);var OSGB_F0=1.000035;var N0=250000;var E0=200000;var phi0=0.93375114981696632365417456114141;var lambda0=-0.13962634015954636615389526147909;var a=major;var b=minor;var eSquared=ecc;var phi=0.0;var lambda=0.0;var E=osref.easting;var N=osref.northing;var n=(a-b)/(a+b);var M=0.0;var phiPrime=((N-N0)/(a*OSGB_F0))+phi0;do{M=(b*OSGB_F0)*(((1+n+((5.0/4.0)*n*n)+((5.0/4.0)*n*n*n))*(phiPrime-phi0))
-(((3*n)+(3*n*n)+((21.0/8.0)*n*n*n))*Math.sin(phiPrime-phi0)*Math.cos(phiPrime+phi0))
+((((15.0/8.0)*n*n)+((15.0/8.0)*n*n*n))*Math.sin(2.0*(phiPrime-phi0))*Math.cos(2.0*(phiPrime+phi0)))
-(((35.0/24.0)*n*n*n)*Math.sin(3.0*(phiPrime-phi0))*Math.cos(3.0*(phiPrime+phi0))));phiPrime+=(N-N0-M)/(a*OSGB_F0);}while((N-N0-M)>=0.001);var v=a*OSGB_F0*Math.pow(1.0-eSquared*sinSquared(phiPrime),-0.5);var rho=a*OSGB_F0*(1.0-eSquared)*Math.pow(1.0-eSquared*sinSquared(phiPrime),-1.5);var etaSquared=(v/rho)-1.0;var VII=Math.tan(phiPrime)/(2*rho*v);var VIII=(Math.tan(phiPrime)/(24.0*rho*Math.pow(v,3.0)))*(5.0
+(3.0*tanSquared(phiPrime))
+etaSquared
-(9.0*tanSquared(phiPrime)*etaSquared));var IX=(Math.tan(phiPrime)/(720.0*rho*Math.pow(v,5.0)))*(61.0
+(90.0*tanSquared(phiPrime))
+(45.0*tanSquared(phiPrime)*tanSquared(phiPrime)));var X=sec(phiPrime)/v;var XI=(sec(phiPrime)/(6.0*v*v*v))*((v/rho)+(2*tanSquared(phiPrime)));var XII=(sec(phiPrime)/(120.0*Math.pow(v,5.0)))*(5.0
+(28.0*tanSquared(phiPrime))
+(24.0*tanSquared(phiPrime)*tanSquared(phiPrime)));var XIIA=(sec(phiPrime)/(5040.0*Math.pow(v,7.0)))*(61.0
+(662.0*tanSquared(phiPrime))
+(1320.0*tanSquared(phiPrime)*tanSquared(phiPrime))
+(720.0*tanSquared(phiPrime)*tanSquared(phiPrime)*tanSquared(phiPrime)));phi=phiPrime
-(VII*Math.pow(E-E0,2.0))
+(VIII*Math.pow(E-E0,4.0))
-(IX*Math.pow(E-E0,6.0));lambda=lambda0
+(X*(E-E0))
-(XI*Math.pow(E-E0,3.0))
+(XII*Math.pow(E-E0,5.0))
-(XIIA*Math.pow(E-E0,7.0));return new LatLng(rad2deg(phi),rad2deg(lambda));}
function convertOsRefToLatLng(osref){var major=6377563.396;var minor=6356256.909;var ecc=((major*major)-(minor*minor))/(major*major);var OSGB_F0=0.9996012717;var N0=-100000.0;var E0=400000.0;var phi0=deg2rad(49.0);var lambda0=deg2rad(-2.0);var a=major;var b=minor;var eSquared=ecc;var phi=0.0;var lambda=0.0;var E=osref.easting;var N=osref.northing;var n=(a-b)/(a+b);var M=0.0;var phiPrime=((N-N0)/(a*OSGB_F0))+phi0;do{M=(b*OSGB_F0)*(((1+n+((5.0/4.0)*n*n)+((5.0/4.0)*n*n*n))*(phiPrime-phi0))
-(((3*n)+(3*n*n)+((21.0/8.0)*n*n*n))*Math.sin(phiPrime-phi0)*Math.cos(phiPrime+phi0))
+((((15.0/8.0)*n*n)+((15.0/8.0)*n*n*n))*Math.sin(2.0*(phiPrime-phi0))*Math.cos(2.0*(phiPrime+phi0)))
-(((35.0/24.0)*n*n*n)*Math.sin(3.0*(phiPrime-phi0))*Math.cos(3.0*(phiPrime+phi0))));phiPrime+=(N-N0-M)/(a*OSGB_F0);}while((N-N0-M)>=0.001);var v=a*OSGB_F0*Math.pow(1.0-eSquared*sinSquared(phiPrime),-0.5);var rho=a*OSGB_F0*(1.0-eSquared)*Math.pow(1.0-eSquared*sinSquared(phiPrime),-1.5);var etaSquared=(v/rho)-1.0;var VII=Math.tan(phiPrime)/(2*rho*v);var VIII=(Math.tan(phiPrime)/(24.0*rho*Math.pow(v,3.0)))*(5.0
+(3.0*tanSquared(phiPrime))
+etaSquared
-(9.0*tanSquared(phiPrime)*etaSquared));var IX=(Math.tan(phiPrime)/(720.0*rho*Math.pow(v,5.0)))*(61.0
+(90.0*tanSquared(phiPrime))
+(45.0*tanSquared(phiPrime)*tanSquared(phiPrime)));var X=sec(phiPrime)/v;var XI=(sec(phiPrime)/(6.0*v*v*v))*((v/rho)+(2*tanSquared(phiPrime)));var XII=(sec(phiPrime)/(120.0*Math.pow(v,5.0)))*(5.0
+(28.0*tanSquared(phiPrime))
+(24.0*tanSquared(phiPrime)*tanSquared(phiPrime)));var XIIA=(sec(phiPrime)/(5040.0*Math.pow(v,7.0)))*(61.0
+(662.0*tanSquared(phiPrime))
+(1320.0*tanSquared(phiPrime)*tanSquared(phiPrime))
+(720.0*tanSquared(phiPrime)*tanSquared(phiPrime)*tanSquared(phiPrime)));phi=phiPrime
-(VII*Math.pow(E-E0,2.0))
+(VIII*Math.pow(E-E0,4.0))
-(IX*Math.pow(E-E0,6.0));lambda=lambda0
+(X*(E-E0))
-(XI*Math.pow(E-E0,3.0))
+(XII*Math.pow(E-E0,5.0))
-(XIIA*Math.pow(E-E0,7.0));return new LatLng(rad2deg(phi),rad2deg(lambda));}
function LatLng(lat,lng){this.lat=lat;this.lng=lng;}
function sinSquared(x){return Math.sin(x)*Math.sin(x);}
function cosSquared(x){return Math.cos(x)*Math.cos(x);}
function tanSquared(x){return Math.tan(x)*Math.tan(x);}
function sec(x){return 1.0/Math.cos(x);}
function deg2rad(x){return x*(Math.PI/180);}
function rad2deg(x){return x*(180/Math.PI);}
function chr(x){var h=x.toString(16);if(h.length==1)
h="0"+h;h="%"+h;return unescape(h);}
function ord(x){var c=x.charAt(0);var i;for(i=0;i<256;++i){var h=i.toString(16);if(h.length==1)
h="0"+h;h="%"+h;h=unescape(h);if(h==c)
break;}
return i;}
function OSRefToString(osref){return"("+osref.easting+", "+osref.northing+")";}