var DFLTZOOM=1;
var NDEC=5;
var DFLTTYPE=G_HYBRID_MAP;
var MAPDIV="gmap";
var LCOL="#00FFFF";
var LWGT=3;
var LOPC=1.00;
var FCOL="#00FFFF";
var FOPC=0.50;

var map;
var ptList = Array();

function initgmap() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById(MAPDIV));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GScaleControl());
		map.enableDoubleClickZoom();
		map.setCenter(new GLatLng(0.0, 0.0), DFLTZOOM, DFLTTYPE);
	} else {
		alert('Your browser is not configured (or maybe is not able) to run Google Maps');
	}
}

function initPoly() {
	ptList = [];
	updateMap();
	GEvent.clearInstanceListeners(map);
	GEvent.addListener(map, 'click', onMapClick);
}

function onMapClick(ovl, clkPt) {
	ptList.push(clkPt);
	updateMap();
}

function killLast() {
	ptList.pop();
	updateMap();
}

function closePoly() {
	ptList.push(ptList[0]);
	updateMap();
}

function updateMap() {
	map.clearOverlays();
	if (ptList.length > 0) {
	        map.addOverlay(new GPolygon(ptList,LCOL,LWGT,LOPC,FCOL,FOPC));
		map.addOverlay(new GMarker(ptList[ptList.length - 1]));
	}
        updateList();
}

function llstr(pt) {
	return pt.lng().toFixed(NDEC) + ' ' + pt.lat().toFixed(NDEC);
}

function updateList() {
	var listStr = '';
	for (var p=0; p<ptList.length; p++)
		listStr += llstr(ptList[p]) + ';';
	document.request.aoi.value = listStr;
}

function unloadgmap() {
        GUnload();
}

function isblank(x) {
    return (x.value==null||x.value=='');
}

function validateform(f) {
    var maxNamelen = 50;
    var maxContactlen = 75; // technically email can be up to 320 but...
    var invalRE = /[^a-zA-Z0-9_\- ]+/; // let's keep 'em simple -- just letters, numbers, _, -, and space
    var emailRE = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
    var phoneRE = /[0-9 ():\.ext,+-]+/i;
    var missing = '';
    var inval = '';
    with (f) {
        if (isblank(requester))
            missing += 'Name';
        else if (invalRE.test(requester.value))
            inval = 'Invalid characters in Name';
        else if (requester.value.length > maxNamelen)
            inval = 'Name too long (max ' + maxNamelen.toString + ' chars)';
        if (isblank(contact))
            missing += (missing.length > 0 ? ', ' : '') + 'Email/Phone';
        else if (!emailRE.test(contact.value) && (contact.value.match(phoneRE) != contact.value))
            inval = (inval.length > 0 ? '\n' : '') + 'Invalid Email/Phone';
        else if (contact.value.length > maxContactlen)
            inval = 'Email/Phone too long (max ' + maxContactlen.toString + ' chars)';
    }
    if (ptList.length == 0)
        missing += (missing.length > 0 ? ', ' : '') + 'Area of Interest (on map)';
    if (missing.length + inval.length > 0) {
        alert(inval + '\n' + (missing.length > 0 ? 'The following required item(s) are missing: ' + missing : ''));
        return false;
    } else
        return true;
}

if (window.addEventListener) {
    window.addEventListener("load", initgmap, false);
    window.addEventListener("unload", unloadgmap, false);
} else if (window.attachEvent) {
    window.attachEvent("onload", initgmap);
    window.attachEvent("onunload", unloadgmap);
} else {
    window.onload=initgmap;
    window.onunload=unloadgmap;
}


