/*
*  Copyright (c)  2007, Stephen Young, stephen.young@chello.at * Unless required by applicable law or agreed to in writing, this software
* is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
* either express or implied.
*/
var MLOST = {};
MLOST.marker =[];
MLOST.lat =[];
MLOST.lng =[];
MLOST.icounter = 0;
MLOST.jcounter = 0;
MLOST.heights=[];
MLOST.mark=0;
MLOST.newZoom = 14*1;
MLOST.from = 0;
MLOST.leg = 0;
//MLOST.jsontimer = setTimeout(noconnection, 5000);
function loadJSON(result) {
    clearTimeout(MLOST.jsontimer);
    MLOST.heights = result;
    heightToString(result);     
};
function joinHeights2(result) {
    clearTimeout(MLOST.jsontimer);
    MLOST.leg = MLOST.leg + 1;
    MLOST.heights = MLOST.heights.concat(result);    
    if (MLOST.leg === 2) {
        heightToString(MLOST.heights);
        MLOST.leg = 0;
    }
};
function joinHeights3(result) {
    clearTimeout(MLOST.jsontimer);
    MLOST.leg = MLOST.leg + 1;
    MLOST.heights = MLOST.heights.concat(result);
    if (MLOST.leg === 3) {
        heightToString(MLOST.heights);
        MLOST.leg = 0;
    }
};
function joinHeights4(result) {
    clearTimeout(MLOST.jsontimer);
    MLOST.leg = MLOST.leg + 1;
    MLOST.heights = MLOST.heights.concat(result);
    if (MLOST.leg === 4) {
        heightToString(MLOST.heights);
        MLOST.leg = 0;
    }
};
function joinHeights5(result) {
    clearTimeout(MLOST.jsontimer);
    MLOST.leg = MLOST.leg + 1;
    MLOST.heights = MLOST.heights.concat(result);
    if (MLOST.leg === 5) {
        heightToString(MLOST.heights);
        MLOST.leg = 0;
    }
};
function scriptTag(points, callback, legs) {
    var script = document.createElement('script');
    //MLOST.jsontimer = setTimeout(noconnection, 5000);
    document.body.appendChild(script);
    script.src = 'http://' + geoSpokeAddressTest + '?spoke=' + points + '&callback=' + callback;
};   
function getElevationIntermediate(points) {
    var points1, points2, points3, points4, points5;
    if (points.length < 45) {
        scriptTag(points, "loadJSON");
    }
    else if (points.length < 90) {
        points1 = points.slice(0, 45);
        points2 = points.slice(46, points.length - 1);
        scriptTag(points1, "joinHeights2");
        scriptTag(points2, "joinHeights2");
    }
    else if (points.length < 135) {
        points1 = points.slice(0, 45);
        points2 = points.slice(46, 91);
        points3 = points.slice(92, points.length - 1);
        scriptTag(points1, "joinHeights3");
        scriptTag(points2, "joinHeights3");
        scriptTag(points3, "joinHeights3");
    }
    else if (points.length < 180) {
        points1 = points.slice(0, 45);
        points2 = points.slice(46, 91);
        points3 = points.slice(92, 137);
        points4 = points.slice(138, points.length - 1);
        scriptTag(points1, "joinHeights4");
        scriptTag(points2, "joinHeights4");
        scriptTag(points3, "joinHeights4");
        scriptTag(points4, "joinHeights4");
    }
    else if (points.length < 225) {
        points1 = points.slice(0, 45);
        points2 = points.slice(46, 91);
        points3 = points.slice(92, 137);
        points4 = points.slice(138, 183);
        points5 = points.slice(183, points.length - 1);
        scriptTag(points1, "joinHeights5");
        scriptTag(points2, "joinHeights5");
        scriptTag(points3, "joinHeights5");
        scriptTag(points4, "joinHeights5");
        scriptTag(points5, "joinHeights5");
    }
    else if (points.length > 225) {
    alert("Links of more than 22.5km are not possible with this version of the tool");
    }
};
function extendedEncode(values) {
    var encodingString = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.',
    chartData = [''],
	 esln = encodingString.length,
	 firstChar, secondChar,i,currentValue;
	 for (i = 0; i < values.length; i++) {
        currentValue = values[i];
        if (!isNaN(currentValue) && currentValue >= 0) {
            firstChar = encodingString.charAt(Math.floor(currentValue/esln));
            secondChar = encodingString.charAt((currentValue % esln));
            chartData.push(firstChar+secondChar);
        } else {
            chartData.push('__');
        }
	 }
	 return chartData.join('');
};
function scaling(values) {
   var mastAorB = Math.max(document.forms.coordinates.mast1.value,document.forms.coordinates.mast2.value),		
	maxHeight = Math.max.apply(null, MLOST.heights),
	maxRound = Math.ceil(maxHeight/10) * 10,
	ofst=0,ymin=0,ymax=4095,
	arraylength = values.length,
	newvalues = [],i,v,y;
	MLOST.minHeight = Math.min.apply(null, MLOST.heights);
	MLOST.max = maxRound + mastAorB + 10;
	MLOST.min = Math.floor(MLOST.minHeight/10)*10;
	MLOST.min+=ofst; MLOST.max+=ofst;
	for (i=0; i<arraylength;i++){
		v = values[i];
		v+=ofst;
		y=Math.round((((ymin-ymax)/(MLOST.max-MLOST.min))*(MLOST.max-v))+ymax);
		newvalues.push(y);
	}
	return newvalues;
};
function heightToString() { 
	 var heightsNewValues = scaling(MLOST.heights), 
	 abHeights,abValues,
    distString=0,dist=[],i,xaxis,yaxis,
    fresnelHi=[],fresnelLo=[],heightDiff,slope,wl,dis8,x,s,intervalHeight,
    points,pointsSqrt,fresHi,fresLo,fresnelHiScale,fresnelLoScale,dis2,dis2Fix;     
    MLOST.chartBaseUrl = "http://chart.apis.google.com/chart?";
    MLOST.chartParams = "cht=lc&chs=450x220&chm=B,008800,0,0,0&chls=1,1,0|1,1,0|1,1,0|1,1,0&chco=008800,ff0000,000000,000000&chf=c,ls,90,999999,0.25,CCCCCC,0.25,FFFFFF,0.25&chg=0,";
    MLOST.values = "&chd=e:";
    MLOST.altA = (document.forms.coordinates.alt1.value)/1 + (document.forms.coordinates.mast1.value)/1;
    MLOST.altB = (document.forms.coordinates.alt2.value)/1 + (document.forms.coordinates.mast2.value)/1;
    abHeights=[MLOST.altA,MLOST.altB];
    abValues = scaling(abHeights);
    for(i=0.25;i<=1;i+=0.25) {
        dis2=MLOST.dis/1000*i;
        dis2Fix=dis2.toFixed(2);
        dist.push(dis2Fix + "km");
    } 
    distString=dist.join("|");   
    xaxis="0:|A|" + distString + "|";
    yaxis="1:|" + MLOST.min + "m" + "|" + ((MLOST.max-MLOST.min)*0.25 + MLOST.min) + "m" + "|"+  ((MLOST.max-MLOST.min)*0.5 + MLOST.min) + "m" + "|" + ((MLOST.max-MLOST.min)*0.75 + MLOST.min) + "m" + "|" + MLOST.max + "m";
    MLOST.legend = "&chxt=x,y&chxl="+ xaxis + yaxis;
    MLOST.title = "&chtt=Steves%20Path%20Profile|(Click%20anywhere%20in%20chart%20area%20for%20a%20bigger,%20printable%20version)";
    if(MLOST.altA > MLOST.altB) {
        heightDiff=(MLOST.altA-MLOST.altB);
        MLOST.minHeight=MLOST.altB;    
    }
    else {
        heightDiff=(MLOST.altB-MLOST.altA);
        MLOST.minHeight=MLOST.altA;
    }
    slope=heightDiff/MLOST.dis;
    MLOST.freq= document.getElementById("Freq1").value;
    wl= 0.3/MLOST.freq ;
    dis8 = MLOST.dis/8;
    for(x=0;x<=8;x++) {
        s = x*dis8;     
        intervalHeight=(slope*s) + MLOST.minHeight;
        points=wl*s*(MLOST.dis-s)/MLOST.dis;
        pointsSqrt = Math.sqrt(points);
        fresHi=(intervalHeight + pointsSqrt);
        fresnelHi.push(fresHi);
        fresLo=(intervalHeight - pointsSqrt);
        fresnelLo.push(fresLo);
    }
    if(MLOST.altA > MLOST.altB) {
        fresnelHi.reverse();
        fresnelLo.reverse();
    }
    fresnelHiScale=scaling(fresnelHi);
    fresnelLoScale=scaling(fresnelLo);
    MLOST.values = MLOST.values + extendedEncode(heightsNewValues) + "," +  extendedEncode(abValues) + "," +  extendedEncode(fresnelLoScale) + "," +  extendedEncode(fresnelHiScale);
    document.getElementById('chart').innerHTML = "<img src=" + MLOST.chartBaseUrl + MLOST.chartParams + MLOST.title + MLOST.legend + MLOST.values +">";   
};
MLOST.copyright = "Steve Young 2007";
function popup(url, high, wide) {
    var leftPos = screen.width/4;
	 newwindow=window.open(url,'','height='+high+',width='+wide+',left='+leftPos+',top=200');
};
function clearFields() {
	document.forms.coordinates.reset();
};
function clearFieldsExcept1() {
	document.getElementById('lat2').value="";
	document.getElementById('lng2').value="";
	document.getElementById('lat2Dms').value="";
	document.getElementById('lng2Dms').value="";
	document.getElementById('distance').value="";
	document.getElementById('a2b').value="";
   document.getElementById('b2a').value="";
};
function createCookie(name,value,days) {
   var expires,date;	
	if (days) {
		date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = "; expires="+date.toGMTString();
	}
	else {
	    expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
};
function readCookie(name) {
	var nameEQ = name + "=",
	ca = document.cookie.split(';'),i,c;
	for(i=0;i < ca.length;i++) {
		c = ca[i];
		while (c.charAt(0)===' ') {
		    c = c.substring(1,c.length);
		}
		if (c.indexOf(nameEQ) === 0){ 
		    return c.substring(nameEQ.length,c.length);
		}
	}
	return null;
};
MLOST.cR = MLOST.copyright; 
function drawCircle(point, radius) {
    var circles = [],
    currentRadius =[],
    zoom,
    cColor="#ff6633",
    cWidth = 5,
    Cradius = radius,
    d2r = Math.PI/180,
    r2d = 180/Math.PI,
    Clat = (Cradius/6371)*r2d,
    Clng = Clat/Math.cos(point.lat()*d2r),
    Cpoints = [],
    i,theta,CPlng,CPlat,circle,blackhtml,P;  
    if (radius < 6.6) {
        zoom = 12;
    }
    else {
        if (radius < 13) {
            zoom = 11;
        }
        else {
            zoom = 10;
        }
    }
    MLOST.map.setZoom(zoom);
    MLOST.map.panTo(point);
    for (i=0; i < 33; i++) {
        theta = Math.PI * (i/16);
        CPlng = point.lng() + (Clng * Math.cos(theta));
        CPlat = point.lat() + (Clat * Math.sin(theta));
        P = new GLatLng(CPlat,CPlng);
        Cpoints.push(P);
    }
    circle = {radiusy:radius};
    circles.push( circle);
    currentRadius[MLOST.jcounter] = new GPolyline(Cpoints,cColor,cWidth);
    MLOST.map.addOverlay(currentRadius[MLOST.jcounter]); 
    GEvent.addListener(currentRadius[MLOST.jcounter], "click", function(point) {
    blackhtml = "<span class='gmapsinfowin'>" + "A radius of " + circle.radiusy + "km" + "</span>"; 
    MLOST.map.openInfoWindowHtml(point, blackhtml);
    });
    MLOST.jcounter++;
}; 
function displayMenu(currentMenu) {
    var thisMenu = document.getElementById(currentMenu).style;
    if (thisMenu.display == "block") {
        thisMenu.display = "none";
    }
    else {
        thisMenu.display = "block";
    }
    return false;
};
Number.prototype.toRad = function() {
  return this * Math.PI / 180;
};
Number.prototype.toBrng = function() {
  return (this.toDeg()+360) % 360;
};
Number.prototype.toDeg = function() {
  return this * 180 / Math.PI;
};
MLOST.ns = 1;
MLOST.ew = 1;
function northSouth() {
    var noSo,i;
    for (i=0; i < 2; i++) {
        if (document.ipcoords.ns[i].checked) {
            noSo = document.ipcoords.ns[i].value;
            if(noSo=="S") {
                MLOST.ns= (-1);
            }
            else {
               MLOST. ns= 1;
            }
        }
    }
};
function eastWest() {
    var eaWe,i;
    for (i=0; i < 2; i++) {
        if (document.ipcoords.ew[i].checked) {
            eaWe = document.ipcoords.ew[i].value;
            if(eaWe=="W") {
                MLOST.ew= (-1);
            }
            else {
                MLOST.ew= 1;
            }
        }
    }
};
function moveMarker2() {
	MLOST.map.removeOverlay(MLOST.myLine);
	MLOST.lata = MLOST.addrpnt.getPoint().lat();
	MLOST.lnga = MLOST.addrpnt.getPoint().lng();
	point = new GLatLng(MLOST.lata,MLOST.lnga);
	MLOST.icounter = 1;
	dotherest(point);
};
function makeDrag(point) {
    clearFieldsExcept1();
    MLOST.map.removeOverlay(MLOST.marker[2]);
    if(MLOST.addrpnt) {MLOST.map.removeOverlay(MLOST.addrpnt);}      
    MLOST.addrpnt = new GMarker(point, {draggable: true}) ;
    MLOST.map.addOverlay(MLOST.addrpnt) ;
    GEvent.addListener(MLOST.addrpnt,'dragend',function() {moveMarker2(MLOST.addrpnt);}) ;
};
function dec2dms(dec) {
   var deg,min,sec,secFix;
	if (dec < 0) {
	deg = Math.round(dec + 0.5);
	}
	else {
	deg = Math.round(dec - 0.5);
	}
	min = Math.round(((dec - deg)*60) - 0.5);
   sec = (dec - deg - min/60)* 3600;
   secFix = sec.toFixed(3);
   MLOST.dms = deg + '° ' + min + "\' " + secFix + '\"';
   return MLOST.dms;
};
function getElevationAB() {
    var i,script;
    if(MLOST.dis>80000) {
        alert("link lengths over 80km are too long to collect height data.");
        return;
    }
    for (i=1; i<3; i++) {
        script = document.createElement('script');
        document.body.appendChild(script);
        script.src = 'http://' + geonamesAddress + '?lat=' + MLOST.lat[i] + '&lng=' + MLOST.lng[i] + '&callback=loadJSONab'; 
        if (i==2) {
            MLOST.f=0;
            MLOST.heights=[];
            getProfile();
        }
    }    
}; 
function drawLine() {
    var R = 6371,dLat,dlng,a,c,dFix,y,x,bearing1,bearing1Fix,dlng2,y2,x2,bearing2,bearing2Fix,lat1,lat2,disFixKm;
	 MLOST.myLine = new GPolyline([MLOST.marker[1].getPoint(), MLOST.marker[2].getPoint()]); 
	 MLOST.map.addOverlay(MLOST.myLine);
    GEvent.addListener(MLOST.marker[1], 'click', function() {
	     drawCircle(MLOST.marker[1].getPoint(),document.getElementById('rad').value);
	 });
	 GEvent.addListener(MLOST.marker[2], 'click', function() {
	     makeDrag(MLOST.marker[2].getPoint());
	 });
	 document.getElementById('lat1').value=MLOST.lat[1].toFixed(8)+ "°";
	 document.getElementById('lng1').value=MLOST.lng[1].toFixed(8)+ "°";
	 dec2dms(MLOST.lat[1]);
	 document.getElementById('lat1Dms').value=MLOST.dms;
	 dec2dms(MLOST.lng[1]);
	 document.getElementById('lng1Dms').value=MLOST.dms;
	 document.getElementById('lat2').value=MLOST.lat[2].toFixed(8)+ "°";
	 document.getElementById('lng2').value=MLOST.lng[2].toFixed(8)+ "°";
	 dec2dms(MLOST.lat[2]);
	 document.getElementById('lat2Dms').value=MLOST.dms;
	 dec2dms(MLOST.lng[2]);
	 document.getElementById('lng2Dms').value=MLOST.dms;
	 dLat = (MLOST.lat[2]-MLOST.lat[1]).toRad();
    dlng = (MLOST.lng[2]-MLOST.lng[1]).toRad(); 
	 a = Math.sin(dLat/2) * Math.sin(dLat/2) +
     Math.cos(MLOST.lat[1].toRad()) * Math.cos(MLOST.lat[2].toRad()) * 
     Math.sin(dlng/2) * Math.sin(dlng/2); 
	 c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
	 MLOST.dis = R * c * 1000;
	 dFix = MLOST.dis.toFixed (3);
    disFixKm=MLOST.dis.toFixed(3)/1000;
	 document.getElementById('distance').value=disFixKm;
	 lat1 = MLOST.lat[1].toRad(); 
	 lat2 = MLOST.lat[2].toRad();
    dlng = (MLOST.lng[2]-MLOST.lng[1]).toRad();
	 y = Math.sin(dlng) * Math.cos(lat2);
	 x = Math.cos(lat1)*Math.sin(lat2) -
    Math.sin(lat1)*Math.cos(lat2)*Math.cos(dlng);
	 bearing1 = Math.atan2(y, x).toBrng();
	 bearing1Fix = bearing1.toFixed(3);
	 dlng2 = (MLOST.lng[1]-MLOST.lng[2]).toRad();
	 y2 = Math.sin(dlng2) * Math.cos(lat1);
	 x2 = Math.cos(lat2)*Math.sin(lat1) -
     Math.sin(lat2)*Math.cos(lat1)*Math.cos(dlng);
    bearing2 = Math.atan2(y2, x2).toBrng();
    bearing2Fix = bearing2.toFixed(3);
    document.getElementById('a2b').value=bearing1Fix + "°";
    document.getElementById('b2a').value=bearing2Fix + "°";
    getElevationAB();
};
function dotherest(point) {
   var letter,myIcon,latc,lngc,zoom;
	MLOST.icounter = ++MLOST.icounter;
	if (MLOST.icounter >= 3) {
	    MLOST.map.removeOverlay(MLOST.marker[1]);
	    MLOST.map.removeOverlay(MLOST.marker[2]);
	    MLOST.map.removeOverlay(MLOST.myLine); 
	    MLOST.icounter = 1;
	    MLOST.jcounter = 1;
	    clearFields();
	}
   letter = String.fromCharCode("A".charCodeAt(0) + (MLOST.icounter-1));
   myIcon = new GIcon(G_DEFAULT_ICON, "http://www.google.com/mapfiles/marker" + letter + ".png");
	MLOST.lat[MLOST.icounter] = point.lat();
	MLOST.lng[MLOST.icounter] = point.lng();
	MLOST.marker[MLOST.icounter] = new GMarker(point, {icon:myIcon}); 
	MLOST.map.addOverlay(MLOST.marker[MLOST.icounter]);
   latc = MLOST.marker[MLOST.icounter].getPoint().lat();
   lngc = MLOST.marker[MLOST.icounter].getPoint().lng();
   zoom= MLOST.map.getZoom();
   createCookie('dblat',latc,30);
   createCookie('dblng',lngc,30);
	createCookie('dbzoom',zoom,30);
	document.getElementById('lat1').value=MLOST.lat[1].toFixed(8)+ "°";
	document.getElementById('lng1').value=MLOST.lng[1].toFixed(8)+ "°";
	dec2dms(MLOST.lat[1]);
	document.getElementById('lat1Dms').value=MLOST.dms;
	dec2dms(MLOST.lng[1]);
	document.getElementById('lng1Dms').value=MLOST.dms;
	if (MLOST.icounter > 1) {
	    drawLine();
	}
}; 
function makeMarkersFromForm(lata,lnga){
    point = new GLatLng(lata,lnga);
    MLOST.map.panTo(point);
    dotherest(point);
};
function dms2dec() {
    var lngdeg = document.getElementById("lngInputDeg").value * MLOST.ew,
    lngmin = document.getElementById("lngInputMin").value / 60,
    lngsec = document.getElementById("lngInputSec").value / 3600,
    latdeg = document.getElementById("latInputDeg").value * MLOST.ns,
    latmin = document.getElementById("latInputMin").value / 60,
    latsec = document.getElementById("latInputSec").value / 3600;
    MLOST.lata = (document.getElementById("latInputDeg").value * MLOST.ns) + (document.getElementById("latInputMin").value / 60) + (document.getElementById("latInputSec").value / 3600);     
    if (lngdeg < 0) {
        MLOST.lnga = lngdeg - lngmin + lngsec;
    }
    else {
        MLOST.lnga = lngdeg + lngmin + lngsec;
    }  
    if (latdeg < 0) {
        MLOST.lata = latdeg - latmin + latsec;
    }
    else {
        MLOST.lata = latdeg + latmin + latsec;
    }
    makeMarkersFromForm(MLOST.lata, MLOST.lnga);
};
function dec() {
    MLOST.lata = document.getElementById("latInput").value * MLOST.ns;
    MLOST.lnga = document.getElementById("lngInput").value * MLOST.ew;
    makeMarkersFromForm(MLOST.lata,MLOST.lnga);
};
function correctAddress() {
	MLOST.lata = MLOST.addrpnt.getPoint().lat();
	MLOST.lnga = MLOST.addrpnt.getPoint().lng();
	point = new GLatLng(MLOST.lata,MLOST.lnga);
	dotherest(point);
};
function getAddress(){
    var address = (document.getElementById("address").value),
    geocoder = new GClientGeocoder();
    geocoder.getLatLng(address,
    function(point){
        MLOST.map.panTo(point);
        MLOST.addrpnt = new GMarker(point, {draggable: true}) ;
        alert ("Geocoders are not really accurate so please drag the marker to the exact location");
        MLOST.map.addOverlay(MLOST.addrpnt) ;
        GEvent.addListener(MLOST.addrpnt,'dragend',function() {correctAddress(MLOST.addrpnt);});
    }); 
};      
function log10(x) { 
return Math.LOG10E * Math.log(x); 
};
function exportLink() {
    var lat1 = MLOST.marker[1].getPoint().lat(),
    lng1 = MLOST.marker[1].getPoint().lng(),
    lat2 = MLOST.marker[2].getPoint().lat(),
    lng2 = MLOST.marker[2].getPoint().lng(),
    zoom= MLOST.map.getZoom(),
    center= MLOST.map.getCenter();
    MLOST.linkA= document.getElementById("linkA").value;
    MLOST.linkB= document.getElementById("linkB").value;
    createCookie('from',1,30);
    createCookie('exlat1',lat1,30);
    createCookie('exlng1',lng1,30);
    createCookie('exlat2',lat2,30);
    createCookie('exlng2',lng2,30);
	 createCookie('exzoom',zoom,30);
	 createCookie('excenter',center,30);
	 createCookie('linka',MLOST.linkA,30);
	 createCookie('linkb',MLOST.linkB,30);
	 createCookie('freq',MLOST.freq,30);
	 window.open("indexMW_Distance20.html");
};
function importLink() { 
    function readImport() {
        var exZoom,point1,myIconA,point2,myIconB;
        MLOST.lat[1]= readCookie('exlat1')*1;
        MLOST.lng[1]= readCookie('exlng1')*1;
        MLOST.lat[2]= readCookie('exlat2')*1;
        MLOST.lng[2]= readCookie('exlng2')*1;
        MLOST.freq = readCookie('freq');        
        document.getElementById(MLOST.freq).selected = true;    
        exZoom= readCookie('exzoom')*1;
        point1 = new GLatLng(MLOST.lat[1],MLOST.lng[1]);
        myIconA = new GIcon(G_DEFAULT_ICON, "http://www.google.com/mapfiles/marker" + "A.png");
        MLOST.marker[1] = new GMarker(point1, {icon: myIconA});
        MLOST.map.addOverlay(MLOST.marker[1]);
        point2 = new GLatLng(MLOST.lat[2],MLOST.lng[2]);
        myIconB = new GIcon(G_DEFAULT_ICON, "http://www.google.com/mapfiles/marker" + "B.png");
        MLOST.marker[2] = new GMarker(point2, {icon: myIconB});
        MLOST.map.addOverlay(MLOST.marker[2]); 
        MLOST.map.setCenter(point1,exZoom);
        createCookie('from',0,30);
        MLOST.icounter=2;
        drawLine();
    }
    setTimeout(readImport,2000);
}; 
function load() { 
    document.ipcoords.ns[0].checked="true";
    document.ipcoords.ew[0].checked="true";
    clearFields();
	 var target = document.getElementById("myheader").innerHTML,
	 ziel = target.substring(3, 19),
	 newLat,newLng,
	 example = "<markers><marker><name>Site190_Tower</name><lng>16.339458</lng><lat>48.226406</lat></marker><marker><name>Site191_Tower</name><lng>16.324917</lng><lat>48.210269</lat></marker><marker><name>Site196_Tower</name><lng>16.292203</lng><lat>48.176786</lat></marker><marker><name>Site197_Tower</name><lng>16.257661</lng><lat>48.202122</lat></marker><marker><name>Site198_Tower</name><lng>16.340931</lng><lat>48.256719</lat></marker><marker><name>Site199_Tower</name><lng>16.29825</lng><lat>48.251358</lat></marker></markers>";
	 document.getElementById("file2read").value = example;
	 if(ziel == MLOST.cR) { 
        MLOST.map = new GMap2(document.getElementById("map"), {draggableCursor: 'crosshair'});
        MLOST.map.addMapType(G_PHYSICAL_MAP);
        MLOST.map.addControl(new GMapTypeControl());
	     MLOST.map.addControl(new GLargeMapControl());        
        MLOST.map.enableScrollWheelZoom();
        MLOST.from = readCookie('from');
	     if(MLOST.from == 2) {
	         importLink();
	     }
        newLat = readCookie('dblat');
        newLng = readCookie('dblng');
        MLOST.newZoom = readCookie('dbzoom')*1;
        if(newLat === null || newLat == "undefined" || isNaN(newLat)) {
            newCenter = new GLatLng(48.21, 16.375);
            MLOST.map.setCenter(newCenter, 14);
        }
        else {
            newCenter = new GLatLng(newLat, newLng);   
            MLOST.map.setCenter(newCenter, MLOST.newZoom);
        }
        GEvent.addListener(MLOST.map, "click", function (overlay, point) {
	         if(point) {
	             dotherest(point);
	         }
	     });
    }
    else {
        alert("Feel free to use my work but have the decency to credit me for it. Steve Young.");
    }
};	
function getProfile() {
    var start = MLOST.marker[1].getPoint(),
    end = MLOST.marker[2].getPoint(),
    dLat = end.lat() - start.lat(),
    dLng = end.lng() - start.lng(),
    latPoints, lngPoints, point, percentDone, percentDoneFix, text, endHeight,
    points = [start],
    mark = 0;   
    while (mark < MLOST.dis) {
        latPoints = start.lat() + dLat * mark / MLOST.dis;
        lngPoints = start.lng() + dLng * mark / MLOST.dis;
        point = new GLatLng(latPoints, lngPoints);            
        points.push(point);     
        percentDone = 100 * mark / MLOST.dis;
        percentDoneFix = percentDone.toFixed(0);
        text = "getting height data..... " + percentDoneFix + "%";
        document.getElementById('chart').innerHTML = text;
        mark = mark + 100;
    }
    getElevationIntermediate(points);    
};	  
function loadJSONab(result) {
    MLOST.f++;
    var alt="alt" + MLOST.f,
    high= result.srtm3;
    if(high<0) {
        high= 0;
    }
    document.forms.coordinates.elements[alt].value=high;   
};
function noconnection(){
    alert("Sorry, height data is temporarily unavailable");
    MLOST.mark = MLOST.dis;
    getProfile();
};  
function enterName() {
    createCookie('linka',MLOST.linkA,30);
    createCookie('linkb',MLOST.linkB,30);
};
function newWindow() {
    MLOST.linkA= document.getElementById("linkA").value;
    MLOST.linkB= document.getElementById("linkB").value;
    popup=window.open('','myconsole','width=1010,height=350');
    popup.document.write('<input type="button" value="Print" onclick="window.print()"/>');
    popup.focus();
    MLOST.chartParams = "cht=lc&chs=1000x300&chm=B,008800,0,0,0&chls=1,1,0|1,1,0|1,1,0|1,1,0&chco=008800,ff0000,000000,000000&chf=c,ls,90,999999,0.25,CCCCCC,0.25,FFFFFF,0.25&chg=0,";
    popup.document.writeln('<html><head><title>Steves Path Profile"</title></head>');
    MLOST.title = "&chtt=Steves%20Path%20Profile%20for:|" + MLOST.linkA + "%20--%20" + MLOST.linkB;
    popup.document.writeln("<img src=" + MLOST.chartBaseUrl + MLOST.chartParams + MLOST.title + MLOST.legend + MLOST.values +">");
    popup.document.close();
};
