var map;
var marker;
var mapListener;
function loadMap(){
    map = new GMap2(document.getElementById("map"));
    map.setCenter(new GLatLng(DEFAULT_LAT,DEFAULT_LONG), 8);
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.setMapType(G_HYBRID_MAP);
    map.enableScrollWheelZoom();
    mapListener = GEvent.addListener(map, 'click', function(overlay, point) {
        if (point){
            zoomLevel = map.getZoom();
            if (zoomLevel < 17) ++zoomLevel;
            locate(point.lat(), point.lng());
            setCoords(point);
         }
    });
}
function initialLocation(latY, longX, zoomLevel){
    var point = new GLatLng(latY, longX);
    setCoords(point);
    locate(latY, longX, zoomLevel);
}
function locate(latY, longX, zoomL){
    var point = new GLatLng(latY, longX);
    if (typeof zoomL != "undefined"){
        map.setZoom(zoomL);
    }
    map.panTo(point);
    if (marker) map.removeOverlay(marker);
    marker = new GMarker(point);
    map.addOverlay(marker);
    updateLatLongLabels(point);
    return point;
}
function updateMap(){
    theForm = window.document.forms["user"];
    locate(theForm.geo_lat_deg_only.value, theForm.geo_long_deg_only.value);
}

//Show labels to the user
function updateLatLongLabels(point){
        MM_setTextOfLayer("latDeg", "", point.lat());
        MM_setTextOfLayer("longDeg", "", point.lng());
}

//Set fields and update labels
var PRECISION = 10;
function setCoords(point){  
//    point = new GLatLng(point.lat().toFixed(PRECISION), point.lng().toFixed(PRECISION));
    updateLatLongLabels(point);
    theForm = window.document.forms["user"];
    theForm.geo_lat_deg_only.value = point.lat();
    theForm.geo_long_deg_only.value = point.lng();
    calcLatLongFromDegs(theForm);
}

//Show/hides latlongLayers
function showhideLatLongLayers(){
    theForm = window.document.forms["user"];
    MM_showHideLayers2("latlongManually", "", getShowHideStr(theForm.latlongType[1].checked));
    MM_showHideLayers2("latlongMap", "", getShowHideStr(theForm.latlongType[0].checked));
}
function getShowHideStr(showbool){
    return showbool ? "show" : "hide";
}
function setUsrSysFields(theForm){
    theForm.user_sys_browser.value = window.navigator.userAgent;
    theForm.user_sys_os.value = window.navigator.platform;
    theForm.user_sys_screen.value = window.screen.availWidth + " X " + window.screen.availHeight;
}
function needAffiliationCode(theForm){
    if (typeof theForm.affiliation_id.options == 'undefined') return null;
    return NETWORKS[theForm.affiliation_id.options[theForm.affiliation_id.selectedIndex].value].user_code_needed == "1";
}
function inspectAffiliationCodeNeed(theForm){
    var affCode = needAffiliationCode(theForm);
    if (affCode == null) return;
    showHideAffiliationCodeLayer(affCode);
    if (affCode){
        MM_setTextOfLayer("affiliationCodeTitle", "", NETWORKS[theForm.affiliation_id.options[theForm.affiliation_id.selectedIndex].value].user_code_title);
        MM_setTextOfLayer("affiliationContact", "", NETWORKS[theForm.affiliation_id.options[theForm.affiliation_id.selectedIndex].value].ntwk_contact_name);
    }
}
function showHideAffiliationCodeLayer(boolValue){
    //affiliationCodeLyr
    MM_showHideLayers2("affiliationCodeLyr", "", getShowHideStr(boolValue));
}
function switchGaugeImg(gaugeNo){
 var url =  '../images/gauges/' + gaugeNo  + '.png';
 setGaugeImg(url);
}
function setGaugeImg(gaugeUrl){
    var m=MM_findObj('gaugeImgLyr');
    m.style.background='url(' + gaugeUrl +') no-repeat top left';
}
function chooseGaugeImg(){
    if (typeof theForm.gauge_category.length == 'undefined') return;
    for (var i=1; i<=theForm.gauge_category.length;i++){
        if (theForm.gauge_category[i-1].checked){ 
             switchGaugeImg(i);
        }
    }
}
function addGaugeEvent(){
    if (typeof theForm.gauge_category.length == 'undefined') return;
    for (var i=0; i<theForm.gauge_category.length;i++){
        theForm.gauge_category[i].onclick=function (){chooseGaugeImg();};
    }

}

function chooseGaugeBrand(){
    if (typeof theForm.gauge_brand.options == 'undefined') return;
    for (var i=0; i < theForm.gauge_brand.options.length; i++){
        if (theForm.gauge_brand.options[i].value == theForm.gauge_brand_tmp.value) theForm.gauge_brand.options[i].selected = true;
    }
}