//
// edit.js   QRZ.COM   December 2, 2008
//

var mw = '540px';
var mh = '400px';
var map;
var marker;
var polylineClick;


function newPos() {
    var lat = e('lat').value;
    var lon = e('lon').value;
    if (Math.abs(lat) > 90) {
        alert("Latitude must be no greater than +/- 90 degrees");
	return;
    }
    if (Math.abs(lon) > 180) {
        lon = 0;
        alert("Longitude must be no greater than +/- 180 degrees");
	return;
    }
    var np = new GLatLng(lat,lon);
    marker.setPoint(np);
    setGrid();
}


function newGrid(grid) {
       var pos = grid2ll(grid);
       var np = new GLatLng(pos.lat+0.02,pos.lon+0.04);
       marker.setPoint(np);
       setGrid();
}


function toGrid(mpLat,mpLon) {

       /* Long/Lat to QTH locator conversion largely        */
       /* inspired from the DL4MFM code found here :        */
       /* http://members.aol.com/mfietz/ham/calcloce.html   */
     
       var ychr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
       var ynum = "0123456789";
       var yqth, yi, yk, ydiv, yres, ylp, y;
       var y     = 0;
       var ycalc = new Array(0,0,0);
       var yn    = new Array(0,0,0,0,0,0,0);

       ycalc[1] = mpLon + 180;
       ycalc[2] = mpLat +  90;

       for (yi = 1; yi < 3; ++yi) {
	for (yk = 1; yk < 4; ++yk) {
	  if (yk != 3) {
	    if (yi == 1) {
	      if (yk == 1) ydiv = 20;
	      if (yk == 2) ydiv = 2;
	    }
	    if (yi == 2) {
	      if (yk == 1) ydiv = 10;
	      if (yk == 2) ydiv = 1;
	    }

	    yres = ycalc[yi] / ydiv;
	    ycalc[yi] = yres;
	    if (ycalc[yi]>0)
	      ylp = Math.floor(yres)
	    else
	      ylp = Math.ceil(yres);
	    ycalc[yi] = (ycalc[yi] - ylp) * ydiv;
	  }
	  else {
	    if (yi == 1)
	      ydiv = 12
	    else
	      ydiv = 24;

	    yres = ycalc[yi] * ydiv;
	    ycalc[yi] = yres;
	    if (ycalc[yi] > 0)
	      ylp = Math.floor(yres)
	    else
	      ylp = Math.ceil(yres);
	  }

	++y;
	yn[y] = ylp;
	}
       }

       yqth = ychr.charAt(yn[1]) + ychr.charAt(yn[4]) + ynum.charAt(yn[2]);
       yqth += ynum.charAt(yn[5]) + ychr.charAt(yn[3]).toLowerCase() + ychr.charAt(yn[6]).toLowerCase();

       return(yqth);
}


function toDMS(mpLat,mpLon) {
    var Result = new Object;
    var longDir = "E";
    var longDeg;
    var longMin;
    var latDir = "N";
    var latDeg;
    var latMin;

    if (mpLon < 0) longDir = "W";
    if (mpLat < 0)  latDir = "S";

    if (mpLon > 0) {
	longDeg = Math.floor(mpLon);
	longMin = (mpLon - longDeg) * 100;
    } else {
	longDeg = Math.ceil(mpLon);
	longMin = (longDeg - mpLon) * 100;
    }
    if (mpLat > 0) {
	latDeg = Math.floor(mpLat);
	latMin = (mpLat - latDeg) * 100;
    } else {
	latDeg = Math.ceil(mpLat);
	latMin = (latDeg - mpLat) * 100;
    }

    var longMin2 = longMin * 60 / 100;
    var longSec = Math.round((longMin2 - Math.floor(longMin2)) * 60);
    var latMin2 = latMin * 60 / 100;
    var latSec = Math.round((latMin2 - Math.floor(latMin2)) * 60);

    Result.lonDMS = longDeg + "&deg; " + Math.floor(longMin2) + "' " + longSec + "'' "+ longDir;
    Result.latDMS = latDeg + "&deg; " + Math.floor(latMin2) + "' " + latSec + "'' "+ latDir;

    return Result;
}


function setGrid() {

    var mPoint = marker.getPoint();

    var mpLat = Math.round(mPoint.lat() * 1000000)/1000000;
    var mpLon = Math.round(mPoint.lng() * 1000000)/1000000;

    e("lat").value = mpLat;
    e("lon").value = mpLon;
    e("grid").value = toGrid(mpLat,mpLon);

    var li = e('locInfo');
    var dms = toDMS(mpLat,mpLon);

    li.innerHTML = dms.latDMS + '&nbsp; &nbsp; &nbsp;' + dms.lonDMS;

    if (polylineClick) {
	map.removeOverlay(polylineClick);
    }
    map.setCenter(mPoint);

    // Square limits

    var bottomLeftLong = Math.floor(mpLon / 0.0833333333) * 0.0833333333;
    var bottomLeftLat = Math.floor(mpLat / 0.0416666666) * 0.0416666666;

    polylineClick = new GPolyline([
	new GLatLng(bottomLeftLat, bottomLeftLong),
	new GLatLng(bottomLeftLat, bottomLeftLong + 0.0833333333),
	new GLatLng(bottomLeftLat + 0.0416666666, bottomLeftLong + 0.0833333333),
	new GLatLng(bottomLeftLat + 0.0416666666, bottomLeftLong),
	new GLatLng(bottomLeftLat, bottomLeftLong)
	], "#FF0000", 3);

    map.addOverlay(polylineClick);
}


function setMS(oldz,newz) {
    if (ms = e('mapscale')) ms.value = newz;
}
    

function loadMap() {
    if (GBrowserIsCompatible()) {
	var panel = e('map');
	panel.style.height = mh;
	panel.style.width = mw;
	var zoom = 10;

	if (ms = e('mapscale')) zoom = parseInt(ms.value);

	point = new GLatLng(e('lat').value, e('lon').value);
	marker = new GMarker(point, {draggable: true, title:'fred'});

	map = new GMap2(e("map"));
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.addControl(new GScaleControl());
	  
	map.setCenter(point, zoom);
	map.addOverlay(marker);

	setGrid();

	GEvent.addListener(marker, 'dragend', setGrid);
	GEvent.addListener(map, 'zoomend', setMS);

    } else {
	// Display a warning if the browser is not compatible
	alert("Sorry, the Google Maps API is not compatible with your browser.");
    }
} 


function grid2ll(grid) {
    var result = new Object;
    grid = grid.toUpperCase();

    result.lon = (20*(grid.charCodeAt(0)-65) + 2*(grid.charCodeAt(2)-48) + 5*(grid.charCodeAt(4)-65)/60) - 180;
    result.lat = (10*(grid.charCodeAt(1)-65) +   (grid.charCodeAt(3)-48) + 5*(grid.charCodeAt(5)-65)/120) - 90;

    return(result);
}


function locateHelp() {
    window.open("http://www.qrz.com/locatehelp.html","_blank","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=450, height=400");
}



function nonblank(item,msg) {
    if (item.value.length < 1) {
        item.style.border = 'solid 2px red';
	return msg;
    } else {
        item.style.border = 'solid 1px #ccc';
	return '';
    }
}

function editor_submit() {

    var form = document.edform;
    var eclass = e('editclass').value;
    var ermsg = '';
    var red = 'solid 2px red';
    var norm = 'solid 1px #ccc';

    if (eclass == 'DX') {
        if (form.dxcc.value == 0) {
	    ermsg += 'A DXCC Land must be chosen.\r\n';
	    form.dxcc.style.border = red;

	} else if (form.dxcc.value == 291) {
	    ermsg += 'USA Callsigns cannot be added.\r\n';
	    form.dxcc.style.border = red;

	} else {
	    form.dxcc.style.border = norm;
	}

	ermsg += nonblank(form.fname, 'The FIRST NAME value cannot be blank.\r\n');
	ermsg += nonblank(form.name, 'The LAST NAME value cannot be blank.\r\n');
	ermsg += nonblank(form.addr1, 'The ADDRESS LINE 1 value cannot be blank.\r\n');
	ermsg += nonblank(form.addr2, 'The ADDRESS LINE 2 value cannot be blank.\r\n');

        if (form.country.value == 0) {
	    ermsg += 'A QSL Postal Country must be chosen.\r\n';
	    form.country.style.border = red;
	} else {
	    form.country.style.border = norm;
	}
    }
    if (ermsg.length > 0) {
        alert(ermsg);
	return;
    }
    document.edform.action.value = 'save';
    document.edform.submit();

}
