//The length of the route card
var routelength=50

var y=0; var x=0; var loop=0;

function next(x,y) {
var xx=0; var yy=0;
var gridrefmap=''; var zero="0"
y=height-y
xx=Math.floor((xoriginmap+((x-xorigin)/km))*10)
yy=Math.floor((yoriginmap+((y-yorigin)/km))*10)
if (yy<0){yy=yy*1+1000}
if (xx<0){xx=xx*1+1000}
if (yy<100){
if (yy<10){yy=zero+zero+yy}
else{yy=zero+yy}}
if (xx<100){
if (xx<10){xx=zero+zero+xx}
else{xx=zero+xx}}
gridrefmap=gridrefmap + xx + yy



document.checks.CurrentRef.value = gridrefmap
mywindow=open('gridrefs4.htm','window2','resizable=no,width=500,height=250');
}


function newWindow(file,window) {
document.outputform1.gridrefno.value=
document.outputForm1.gridrefno.value*1+9
msgWindow=open(file,window,'resizable=no,width=450,height=200');
if (msgWindow.opener == null) msgWindow.opener = self;
}


function getdetails(){



var places=new Array("074673", "Road T Junction", "10", "083659", "Skeoch Wood", "10", "085549", "Corner in Road", "50",  "068673", "Port Banatyne Scout Hut", "0", "035664", "Ettrick Bay Cafe", "0", "088648", "Rothesay Ferry Terminal", "0", "084670", "Ardbeg Point", "0", "107633", "Ascog Bay Telephone Box", "0", "106614", "Kerrycroy Bay telephone Box", "0", "100598",  "Scoulag", "60", "093564", "Kingarth hotel", "20", "069585", "Road T Junction", "30", "061632", "Road T Junction", "40", "088645", "Rothesay Castle", "20", "095527", "St. Blanes Hill", "120", "043699", "Windy Hill", "280", "027742", "Ferry Terminal", "0", "009707", "Torran Turach", "230", "054709", "Shalunt Cott", "0", "005683", "Clate Point Car Park", "10", "033678", "Kilbride", "60", "025698", "Glenmore", "80", "072690", "Ardmaleish", "30", "056673", "Road T Junction", "20", "037666", "Road T Junction", "10", "042656", "Etrick Bay Picnic Area", "0", "045673", "St Colmac", "30", "084666", "Ardbeg", "0", "083658", "Skeoch Wood", "10", "041648", "Road Corner", "10", "048627", "Road T junction", "30", "037644", "Watch Hill", "40", "071642", "Corner on Road, 'Little Barone'", "60", "069631", "Barone Hill", "162", "079639", "Road Junction", "30", "088644", "Rothesay", "20", "088647", "Rothesay Car Park", "0", "097644", "Corner in Road", "110", "079628", "End of Road, near Kirk Dam", "30", "087638", "Rothesay hospital", "20", "106651", "Road Junction", "20", "106621", "Road junction", "10", "043620", "Straad Phone Box", "10", "041617", "St Ninians Bay", "0", "050611", "Meikle Kilmory", "30", "079609", "Meikle Grenach", "40", "093584", "Memorial", "100", "060587", "Car Park", "10", "057589", "Scalpsie", "20", "051584", "Tarmore Hill Picnic Area", "50", "050583", "Corner in Road", "50", "044594", "View Point", "20", "094588", "Road T Junction", "90", "072577", "Gallachan", "20", "090562", "Kingarth Cemetery and Road Junction", "20", "091557", "Stone Circle", "20", "097553", "Corner in Road", "0", "086549", "Corner In Road", "50", "106546", "End of Road, Kilchattan Bay", "0", "092531", "Car Park, near St. Blanes Church", "20", "093526", "Garrochty Farm, St. Blanes Hill", "30", "105654", "Bogany Point", "10", "107632", "Ascog Point", "10", "047658", "cranslagvourity", "10", "430099", "Kirklevington Village Hall", "50", "433100", "The Crown Pub (Kirklevington)", "50", "428101", "End of Dents Lane", "50", "443105", "Road - Path Junction", "50", "440106", "Trig Point", "52", "437108", "Spell Close Farm", "50", "423103", "Road - Path Junction", "40", "429110", "'S' Bends", "35", "426114", "Roundabout", "40", "428115", "Road T junction", "30", "420112", "Yarm Station", "30", "433110", "Road - Path Junction", "40", "430108", "Road - Path Junction", "30", "424098", "Railway Bridge", "40", "421097", "Farm, Road - Path Junction", "40", "421093", "Corner in Path", "40", "418093", "Corner in Path", "50", "428093", "Path Junction", "50", "430088", "Moor House Farm", "50", "431086", "End of Track to Moor House farm", "40", "422080", "The Station Pub (Picton)", "40", "419079", "Picton Church", "50")




document.inputForm1.gridref.value=opener.document.checks.CurrentRef.value
document.inputForm1.routepoint.value=opener.document.checks.gridrefno.value*1+9

var ref=document.inputForm1.gridref.value
var placeno=0
for (var placeno=0; placeno<places.length; placeno=placeno+3) {
if (ref==places[placeno]){
document.inputForm1.elements[2].value=places[placeno*1+1]
document.inputForm1.elements[3].value=places[placeno*1+2]}
}
}


function gridrefthispoint(){
var gridrefthispoint = '';
l = opener.document.checks.gridrefno.value
gridrefthispoint = opener.document.checks.CurrentRef.value
return gridrefthispoint;}


function inputdata() {
//window.open('gridrefs2.htm','hiddenframe')
//window.open('gridrefs4.htm','window2', //'toolbar=yes,status=yes,width=450,height=200')
//if (msgWindow.opener == null) msgWindow.opener = self;
}





function setForm() {

opener.document.checks.gridrefno.value = document.inputForm1.routepoint.value;

l = opener.document.checks.gridrefno.value

opener.document.checks.elements[l-1].value = document.inputForm1.gridref.value;

opener.document.checks.elements[l].value = document.inputForm1.inputField1.value;
var oldheight=opener.document.checks.oldheight.value
var newheight=document.inputForm1.inputField2.value
var heightchange=0
var heightchange1=document.inputForm1.inputField4.value*1 - document.inputForm1.inputField5.value
if(newheight ==""){
newheight=oldheight*1 + heightchange1}
if(oldheight == ""){
oldheight=newheight; opener.document.checks.oldheight.value=newheight}
heightchange=newheight*1-oldheight*1
if (heightchange>0){
opener.document.checks.elements[l*1+1].value=heightchange
opener.document.checks.elements[l*1+2].value=""}
if (heightchange<0){
opener.document.checks.elements[l*1+1].value=""
opener.document.checks.elements[l*1+2].value=heightchange*(-1)}
if (heightchange==0){
opener.document.checks.elements[l*1+1].value=""
opener.document.checks.elements[l*1+2].value=""}

opener.document.checks.oldheight.value = newheight

opener.document.checks.elements[l*1+3].value = document.inputForm1.inputField3.value;

if(document.inputForm1.inputField4.value!="")
{opener.document.checks.elements[l*1+1].value=document.inputForm1.inputField4.value}
if(document.inputForm1.inputField5.value!="")
{opener.document.checks.elements[l*1+2].value=document.inputForm1.inputField5.value}

window.close();
return false;
}


function group(file,window) {
msgWindow=open(file,window,'resizable=no,width=750,height=500');
if (msgWindow.opener == null) msgWindow.opener = self;
}


function setForm2() {
for (loop1=0; loop1<28; loop1++){
opener.document.groupdetails.elements[loop1].value = document.groupdetails.elements[loop1].value;}
opener.document.checks.elements[8].value = document.groupdetails.elements[23].value;
opener.document.checks.elements[7].value = 0
opener.document.checks.elements[6].value = 0
opener.document.checks.elements[5].value = 0
opener.document.checks.elements[4].value = 0
opener.document.checks.elements[3].value = 0
opener.document.checks.oldheight.value = document.groupdetails.elements[21].value;
self.close();
return false;
}


function groupformlist(){
var details = '';
for (loop2=0; loop2<28; loop2++){
details +='<input type=hidden name=groupdetail' + loop2 + '>'}
return details;}


function routedetaillist(){
var routedetails = '';
for (loop3=0; loop3<(routelength*9); loop3++){
routedetails +='<input type=hidden name=ref' + loop3 + '>'
}
return routedetails;}


function calc(){
for (var l=0; l<6; l++)
{document.checks.elements[routelength*9+l].value = 0}

if (document.groupdetails.elements[23].value==""){document.groupdetails.elements[23].value="10:00"}
document.checks.elements[8].value=document.groupdetails.elements[23].value

var rad=57.2958; var y=9;
for (var y=9; y<((routelength*9)-9); y=y+9) {
if (document.checks.elements[y].value!="") {
var ref0=document.checks.elements[y-9].value;
var ref1=document.checks.elements[y].value;
var ea0=Math.round(ref0/1000);
var ea1=Math.round(ref1/1000)
var up=document.checks.elements[y+2].value
var down=document.checks.elements[y+3].value
var grade1=(document.checks.steep.value)
if ((ea0*1000)>ref0) {ea0=ea0-1};
if ((ea1*1000)>ref1) {ea1=ea1-1}
var no1=Math.round(ref1-(ea1*1000));
var no0=Math.round(ref0-(ea0*1000))
eta0=document.checks.elements[y-1].value
var speed=6/(document.groupdetails.elements[20].value)
if ((ea1-ea0)<-500) {ea1=(1000+(ea1*1))}
if ((ea1-ea0)>500) {ea0=(1000+(ea0*1))}
if ((no1-no0)<-500) {no1=(1000+(no1*1))}
if ((no1-no0)>500) {no0=(1000+(no0*1))}
var northings=no1-no0; var eastings=ea1-ea0
var number = Math.sqrt((northings*northings)+(eastings*eastings));
var degsin=Math.round((Math.asin(northings/number))*rad)
var degcos=Math.round((Math.asin(northings/number))*rad)
var number2 = Math.round(number*(1+(document.checks.wiggle.value/100)))
if ((ea1)>=(ea0)) {degs=90-degsin}
if ((ea0)>=(ea1)) {degs=270+degcos}
if ((no1)>=(no0)&&(ea1)==(ea0)) {degs=0}
var time = Math.round((document.checks.elements[y+4].value)*1 + up/10 + number2*(speed))
if ((down/(number2))>=(grade1)-1)
{var time = Math.round((document.checks.elements[y+4].value)*1 + up/10 + number2*(speed) + down/20)}
var hours=Math.round((time)/60)
var mins=Math.round((time)-(Math.round((time)/60))*60)
var time2=hours + ":" + mins
var t1 = document.checks.elements[y-1].value;
t2 = time2;
var m = (t1.substring(0,t1.indexOf(':'))-0) * 60 +
(t1.substring(t1.indexOf(':')+1,t1.length)-0) +
(t2.substring(0,t2.indexOf(':'))-0) * 60 +
(t2.substring(t2.indexOf(':')+1,t2.length)-0);
var h = Math.floor(m / 60);
var min2=(m-(h*60))
var eta = (h + ':' + (min2));
if ((m - (h * 60))<10) {eta=(h + ":0" + min2)}
document.checks.totaldistance.value = Math.round((document.checks.totaldistance.value)*10 + number2)/10
document.checks.totaltime.value = (document.checks.totaltime.value)*1 + (time)*1
document.checks.totalup.value = (document.checks.totalup.value)*1 + (up)*1
document.checks.totaldown.value = (document.checks.totaldown.value)*1 + (document.checks.elements[y+3].value)*1
document.checks.height2.value = (document.checks.oldheight.value)
document.checks.endref.value = document.checks.elements[y].value
document.checks.elements[y+5].value=(number2)/10
document.checks.elements[y+6].value=degs
document.checks.elements[y+7].value=time
document.checks.elements[y+8].value=eta}
document.checks.elements[y+17].value=document.checks.elements[y+8].value}}


function message1() {
alert ("This will produce 5 new windows. Print at least 2 copies of the Route Card and the group list. Complete the Group List by hand. Give one copy of these to your Home Contact. Print 1 copy of the other 3 sheets. Refer to the kit list before you go and carry the other sheets with you.")
}


function message2() {
alert ("This will produce just your route card. Print at least 2 copies of it and give one copy to your Home Contact. Carry the other copy with you.")}


function magvariation() {
var test=document.groupdetails.elements[22].value
if (test=="dd/mm/yyyy"){test="01/01/2004"}
var test2=(test.substring((test.length-2),test.length))*1
var test3=3+11/12
if (test2>=0){if (test2<=99){var test2=(test2*1)+2000}}
else {var today = new Date();test2 = today.getYear()}
test3=(test3)-(test2-2004)*13/60
if (test3<0){test3=test3*(-1)}
if (test3==0){test3=0}
document.groupdetails.elements[18].value=Math.round(test3)}


var nt="</table>"
var cll="</td><td>"
var wid="</td><td width="
var clll="<br>" + cll
var ccl="</td><td colspan="
var cllc=cll + "<center>"
var clle="<br></td><td class=ee>"
var cllr="<br></td><td align=right>"
var nd="</td></tr>"
var ndl="<br>" + nd
var ndsr=nd + "<tr><td align=right>"
var rule = "<tr><td width=100% colspan=4><hr>"
var typ="<input type=text name="


function members() {
DispWin = window.open('','NewWin4', 'toolbar=no,status=yes,resizable=yes,scrollbars=yes,menubar=yes')
message ="<ULli><lili><link rel=stylesheet type=text/css href=white2.css title=style><style type=text/css></style>"
message += "<center><b><u>Hike / Expedition Group Members Details</b></u></center><bR>Group Name: " + document.groupdetails.elements[0].value + "<br><table width=100% border=0 cellpadding=0 cellspacing=0><tr><td width=50%>Contact for group: " + document.groupdetails.elements[1].value + "."+ wid + "50%>Tel No. " + document.groupdetails.elements[2].value + nd + "</table><br><TABLE BORDER CELLSPACING=0 WIDTH=100%><TR><TD WIDTH=20%><center>Name"+ wid + "17%><CENTER>Telephone Nos.<br>Home / Mobile"+ wid + "25%><center>Home address</center>"+ wid + "14%><center>Name of Contact at Home"+ wid + "25%>Notes: (e.g. Medical conditions, home contact notes etc)</TD>"
var h=3
for (var h=3; h<11; h=h+1) {
if (document.groupdetails.elements[h].value!=""){
message += "</TR><TR><TD><center>" + document.groupdetails.elements[h].value + clll +clll +clll +clll + ndl +"<tr><td colspan=2 align=right>Outer Clothing colour: " + ccl + "3><br></td>"}}
message += "</tr></table><br><b>EQUIPMENT CARRIED<bR></b><table width=100% border=1 cellpadding=0 cellspacing=0><tr><td width=37%>Equipment"+ wid + "12%>With Group? "+ wid + "38%>Equipment"+ wid + "13%>With Group?" + nd 
var g=0
var carried = new Array('Maps and Compass (min 2 off each)','Waterproofs','Spare Clothing','Food and Drink','Emergency Rations','Watch','Emergency Card and Pencil','Sleeping Bags or Bivvi bags','Group Shelter','Overnight Equipment','Torch','Whistle','Stove','Matches','Confidence Rope','Ice Axe / Crampons')
for (var g=0; g<carried.length; g=g+2) {
message += "<TR><TD>" + carried[g] +cllc+ "YES / NO" + cll + carried[g+1] +cllc+ "YES / NO"+ nd}
message += "<tr><td>Mobile Phone No. for group:" + cll + "_____________" +clll + clll+ ndl + nt
message += "<br>Print and complete 2 Copies of this sheet, carry one with the group and leave the other with your 'Home Contact'"
if (document.groupdetails.elements[11].value!=0)
{message += ", who is " + document.groupdetails.elements[11].value;}
if (document.groupdetails.elements[13].value!=0)
{message += ", at " + document.groupdetails.elements[13].value;}
if (document.groupdetails.elements[14].value!=0)
{message += ", " + document.groupdetails.elements[14].value;}
if (document.groupdetails.elements[12].value!=0)
{message += ", Telephone " + document.groupdetails.elements[12].value;}
message += "<hr><center>FOR THE HOME CONTACT'S INFORMATION</center><br>Estimated Time to complete expedition: " + document.checks.elements[routelength*9-1].value
message += "<br>Try to contact the group if they have not reported completing the expedition at: __________<bR>If you have had no contact with the group by: __________, contact the Emergency services<br>(Dial 999 and ask for the Police or Mountain Rescue to report the group as missing. Have this sheet and the route card available for their information.)"
message += "</ULul>";
DispWin.document.write(message);}


function create() {
DispWin = window.open('','NewWin1', 'toolbar=no,status=yes,resizable=yes,scrollbars=yes,menubar=yes')
message ="<ULli><lili><link rel=stylesheet type=text/css href=white2.css title=style><style type=text/css></style>"
message += "<ULli><center><table width=95% border=1 cellpadding=0 cellspacing=0><tr><td><table width=100% border=0 cellpadding=1 cellspacing=2><tr><td width=25%>Walk Date: " + document.groupdetails.elements[22].value + wid + "50% colspan=2><center><b><font size=4>" + document.groupdetails.elements[0].value + "</b></font>"+ wid + "25% align=right>Page " + document.groupdetails.elements[25].value + " of " + document.groupdetails.elements[26].value + "</td></tr><tr><td align=right>Map: "+ wid + "50% colspan=2> " + document.groupdetails.elements[24].value + ", Map No. " + document.groupdetails.elements[15].value + wid + "25%>" + ndsr + "Map scale: </td><td>" + document.groupdetails.elements[16].value + wid + "50% align=right colspan=2><center>Group Members" + ndsr + "Start Grid Ref.</td><td>" + document.checks.elements[0].value + "</td><td rowspan=3 colspan=2><table width=100%><tr>"
for (var f=0; f<2; f=f+1) { 
message += "<td width=50%>"
for (var g=3; g<11; g=g+2) { 
message += [g-2+f] + ". " + document.groupdetails.elements[g+f].value + "<br>"}}
message += "</tr></table></td>" + ndsr + "End Grid Ref." + cll + document.checks.endref.value + ndsr + "Start Height:" + cll + document.groupdetails.elements[21].value + ndsr + "End Height: " + cll + document.checks.height2.value + ndsr + "Walking Speed: " + cll + document.groupdetails.elements[20].value + " km/h" + cllr + "Group Contact: " + cll + document.groupdetails.elements[1].value + ndsr + "Car Reg. No." + cll + "____________" +cllr+ "Telephone No." +cll+  document.groupdetails.elements[2].value + nd + rule + cll + "<tr><td align=right><b>Home Contact: " +cll+ document.groupdetails.elements[11].value + cllr + "</b>Tel No. " +cll + document.groupdetails.elements[12].value + nd +"<tr>" +cllr+ "Address: " + ccl + "3>" + document.groupdetails[13].value + ", " + document.groupdetails[14].value + nd + rule + ndsr + "Expedition Objective: " + ccl + "3>" + document.groupdetails.elements[27].value + nd + rule + ndsr + "Weather Forecast: <br>" + ccl + "3>"+ nd +"<tr><td align=right>Sunrise: " +cll+ cllr+ "Sunset: " +cll+ nd + nt
message += "<table width=100% border=1 cellpadding=0 cellspacing=0><tr><td width=10%>From: "+ wid + "10%>To: "+ wid + "55%>Description of place going to<br>Route description and notes"+ wid + "10% colspan=2>Height<br>Changes"+ wid + "5%>Breaks"+ wid + "5%>Dist. "+ wid + "5%>Bearing"+ wid + "5%>Time"+ wid + "5%>ETA"+ nd +"<tr><td>xxxxxx" +cll + document.checks.elements[0].value + cll + document.checks.elements[1].value + cllc + "+" +cllc+ "-" +cllc+ "(mins)" +cllc+ "(km)" +cllc+ "(Deg)" +cllc+ "(mins)" + cll + document.checks.elements[8].value + nd
for (var j=9; j<((routelength*9)-9); j=j+9) {
if (document.checks.elements[j].value!="") {
message += "<tr><td>" + document.checks.elements[j-9].value;
for (var k=0; k<9; k++) {
message += clll + document.checks.elements[j+k].value;
} + nd
var x=9}}
document.checks.totaltime.value=Math.round(document.checks.totaltime.value)
message += "<tr><td colspan=3 align=right>Totals: " +cllc + document.checks.totalup.value + cllc + document.checks.totaldown.value + cllc + "XXX" + cllc + document.checks.totaldistance.value + cllc+ "XXX" +cllc + document.checks.totaltime.value + cllc + document.checks.elements[(routelength*9)-1].value + nd + nt+ nd + nt
message += "<LIli><br></center>Magnetic Variation: The difference between Magnetic North and Grid North. For your expedition date, this is estimated as " + document.groupdetails.elements[18].value;
message += "<LIli><sup>o</sup>"
+ "</ULul>";
DispWin.document.write(message);
}

