﻿// JScript File
var _busy = false;
var _stuckWatchdog = null;
var _browser = null;
var _element = null;
var _weatherSettings = null;
var _chart = null;
var _callsigns = null;
var _vessels = null;
var _offices = null;
var _radioStations = null;
var _popup = null;
var _track = null;
var _currentTrackField = null;
var _EnableDisplayMode = false;
var _VesselListPinned = false;
var _DebugMode = 0;
var _FirstScreenShow = true;
var _MapWidth = 900;
var _MapHeight = 600;

var _loadLastMapView;
var _displayVesselNames;
var _displayGwOffices;
var _displayGwRadioStations;

var restoreMapScale = "";
var restoreMapLongitude = "";
var restoreMapLatitude = "";

var _LoginRememberMe = false;
var _LoginAcctNo = "";
var _LoginLocNo = "";

function RefreshLastUpdateTime()
{
    try
    {
    if ( _FirstScreenShow == true)
        {
        window.focus();
        _FirstScreenShow = false;
        }

    var monthNames = new Array( "January", "February", "March", "April", "May", "June", 
                                "July", "August", "September", "October", "November", "December");

    //  Get the current date and time parts in UTC format.    
    var d = new Date();
    var currentDay = d.getUTCDate();
    var currentMonth = d.getUTCMonth();
    var currentYear = d.getUTCFullYear();
    var currentHour = d.getUTCHours();
    var currentMinutes = d.getUTCMinutes();
    var currentSeconds = d.getUTCSeconds();

    if ( currentHour <= 9)
        currentHour = "0" + currentHour;
    if ( currentMinutes <= 9)
        currentMinutes = "0" + currentMinutes;
    if ( currentSeconds <= 9)
         currentSeconds = "0" + currentSeconds;

    document.getElementById( "lblLastUpdate").innerHTML = "Last updated: " 
                               + monthNames[ currentMonth] + " " + currentDay + ", " + currentYear
                               + " at " + currentHour + ":" + currentMinutes + ":" + currentSeconds + " UTC";
    }
    catch( e)
        {
//        alert( "Error refreshing last update time.\nError: " + e);
        }
    
}


function EnableDisableDisplayMode(state) 
{
    _element.ShowControls(!state);
    if (state) {
        if (_popup != null && _popup != undefined)
            _popup.HidePopup('ZoomOptionsPopup');
        _EnableDisplayMode = true;
        _vessels._displayModePopup = -1;
        _vessels._displayPreviousPopup = -1;
        _vessels.DisplayModeScroll();
        }
    else {
        _EnableDisplayMode = false;
        if (_popup != null && _popup != undefined)
            _popup.HidePopup('displayModePopup');
        _vessels.DisplayModeScroll();
    }
} 
  
function ShowHint(hint)
{
    try
    {
        if(hint.length > 0)
        {
            //alert(hint);
            eval("var response = "+hint);
            ProcessResponse(response);
        }
        else
            _element.SetMessage(false);
    }
    catch(e)
    {
        /*alert("Error in showHint: "+e+"\nHint: "+hint);*/
    }
    finally
    {
    _busy = false;
    }
  
}


function setCookie(name, value, expire) {
    document.cookie = name + "=" + value + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()));
}


function getCookie(Name) {
    var search = Name + "=";

    if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search)
        if (offset != -1) { // if cookie exists
            offset += search.length
            // set index of beginning of value
            end = document.cookie.indexOf(";", offset)
            // set index of end of cookie value
            if (end == -1)
                end = document.cookie.length
            return document.cookie.substring(offset, end);
        }
    }
}


function LoadUserSettings()
    {
    var tempMode;

    tempMode = getCookie("GWLG-LoadLastMapView");
    _loadLastMapView = (tempMode == "true") ? true : false;
    document.getElementById("LoadLastMapViewCheckbox").checked = _loadLastMapView;

    tempMode = getCookie("GWLG-EnableDisplayMode");
    _EnableDisplayMode = (tempMode == "true") ? true : false;
    document.getElementById("chkShow").checked = _EnableDisplayMode;
    EnableDisableDisplayMode(_EnableDisplayMode);
    
    tempMode = getCookie("GWLG-ShowVesselNames");
    _displayVesselNames = (tempMode == "true") ? true : false;
    document.getElementById("VesselNamesCheckbox").checked = _displayVesselNames;

    tempMode = getCookie("GWLG-ShowGlobeOffices");
    _displayGwOffices = (tempMode == "true") ? true : false;
    document.getElementById("showOffices").checked = _displayGwOffices;

    tempMode = getCookie("GWLG-ShowGlobeRadioStations");
    _displayGwRadioStations = (tempMode == "true") ? true : false;
    document.getElementById("showRadioStations").checked = _displayGwRadioStations;

/*
    tempMode = getCookie("GWLG-ChartDetailLevel");
    if (tempMode == 0 || tempMode == 1 || tempMode == 2)
        _chart._detailLevel = tempMode;
    else
        _chart._detailLevel = 1;
    if (_chart._detailLevel == 0)
        _chart.ChangeDetailLevel("base");
    else if (_chart._detailLevel == 1)
        _chart.ChangeDetailLevel("standard");
    else if (_chart._detailLevel == 2)
        _chart.ChangeDetailLevel("full");
    else
        _chart.ChangeDetailLevel("standard");
*/

    var tempWeather = getCookie("GWLG-WeatherCheckbox");
    document.getElementById("WeatherCheckbox").checked = (tempWeather == "true") ? true : false;

    var tempTemperature = getCookie("GWLG-TemperatureCheckbox");
    document.getElementById("TemperatureCheckbox").checked = (tempTemperature == "true") ? true : false;
    _weatherSettings.setArea((tempTemperature == "true") ? 'temperature' : 'none');

    var tempPressure = getCookie("GWLG-PressureCheckbox");
    document.getElementById("PressureCheckbox").checked = (tempPressure == "true") ? true : false;
    _weatherSettings.setLineOption('pressure', (tempPressure == "true") ? true : false);

    var tempCyclones = getCookie("GWLG-TropicalCyclonesCheckbox");
    document.getElementById("TropicalCyclonesCheckbox").checked = (tempCyclones == "true") ? true : false;
    _weatherSettings.showCyclone((tempCyclones == "true") ? true : false);

    if (_loadLastMapView == true) {
        restoreMapScale = getCookie("GWLG-RestoreMapScale");
        restoreMapLongitude = getCookie("GWLG-RestoreMapLongitude");
        restoreMapLatitude = getCookie("GWLG-RestoreMapLatitude");

        if (restoreMapLatitude == null || restoreMapLatitude == undefined || restoreMapLongitude == null || restoreMapLongitude == undefined) {
            restoreMapScale = "145333439";
            restoreMapLatitude = "0.0";
            restoreMapLongitude = "0.0";
        }
    }
    else {
        restoreMapScale = "145333439";
        restoreMapLatitude = "0.0";
        restoreMapLongitude = "0.0";
    }
    _vessels._currentScale = restoreMapScale;
    _vessels._mapCenterLongitude = restoreMapLongitude;
    _vessels._mapCenterLatitude = restoreMapLatitude;

    StoreRequest("action=restoremapview;Scale=" + restoreMapScale + ";Longitude=" + restoreMapLongitude + ";Latitude=" + restoreMapLatitude + ";",
                          true, "Restore Previous Map View");

    StoreRequest("action=configuresettings;" + _weatherSettings.toConfigString() +
                    "detailLevel=" + _chart._detailLevel + ";enableWeather=" + tempWeather + ";", true, "Applying settings");

    if (tempCyclones == "true")
        _chart.StartTropicalTimer();

}

function SaveUserSettings() {
    var today = new Date();
    var expiryDateTime = new Date();
    expiryDateTime.setTime(today.getTime() + 1000 * 60 * 60 * 24 * 365);

    setCookie("GWLG-EnableDisplayMode", ((_EnableDisplayMode == true) ? "true" : "false"), expiryDateTime);
    setCookie("GWLG-LoadLastMapView", ((_loadLastMapView == true) ? "true" : "false"), expiryDateTime);

    setCookie("GWLG-ShowVesselNames", ((_displayVesselNames == true) ? "true" : "false"), expiryDateTime);
    setCookie("GWLG-ShowGlobeOffices", ((_displayGwOffices == true) ? "true" : "false"), expiryDateTime);
    setCookie("GWLG-ShowGlobeRadioStations", ((_displayGwRadioStations == true) ? "true" : "false"), expiryDateTime);

/*
    setCookie("GWLG-ChartDetailLevel", _chart._detailLevel.toString(), expiryDateTime);
*/

    setCookie("GWLG-WeatherCheckbox", document.getElementById("WeatherCheckbox").checked.toString(), expiryDateTime);
    setCookie("GWLG-TemperatureCheckbox", document.getElementById("TemperatureCheckbox").checked.toString(), expiryDateTime);
    setCookie("GWLG-PressureCheckbox", document.getElementById("PressureCheckbox").checked.toString(), expiryDateTime);
    setCookie("GWLG-TropicalCyclonesCheckbox", document.getElementById("TropicalCyclonesCheckbox").checked.toString(), expiryDateTime);

    restoreMapScale = _vessels._currentScale;
    restoreMapLongitude = _vessels._mapCenterLongitude;
    restoreMapLatitude = _vessels._mapCenterLatitude;

    setCookie("GWLG-RestoreMapScale", restoreMapScale, expiryDateTime);
    setCookie("GWLG-RestoreMapLongitude", restoreMapLongitude, expiryDateTime);
    setCookie("GWLG-RestoreMapLatitude", restoreMapLatitude, expiryDateTime);
}


function Init()
{
    try 
    {
        if (_browser == null)
            _browser = new BrowserHelper();
        if ( _element == null)
            _element = new ElementHelper();

        if (_weatherSettings == null)
            _weatherSettings = new WeatherSettings();
        if (_chart == null)
            _chart = new ChartHelper();

        if (_vessels == null)
            _vessels = new VesselPlotter();

        if (_callsigns == null)
            _callsigns = new CallsignList();

        var callsignContainer = _element.GetElement("callsignContainer");
        if (callsignContainer != null)
            callsignContainer.style.height = _MapHeight + "px";

        var temp = _element.GetElement("callsigns");
        if (temp != null)
            temp.style.height = (_MapHeight - 52) + "px";

        var callsignExpand = _element.GetElement("callsignExpand");
        if (callsignExpand != null) {
            callsignExpand.style.height = _MapHeight + "px";
            callsignExpand.style.paddingTop = ((_MapHeight - 48) / 2) + "px";
        }
                
        StoreRequest("action=initialize;width=" + _MapWidth + ";height=" + _MapHeight + ";", true, "Loading chart software");
    }
    catch(e)
    { alert("Init: "+e); }
}


function OnInitialized()
{
    var _step = 0;
    try
    {
        if (_callsigns == null) {
            _callsigns = new CallsignList();
            _step++;
        }

        if (_vessels == null) {
            _vessels = new VesselPlotter();
            _step++;
        }


        if (acct == 'ALL')
            _vessels._vesselRefreshInterval = 60;
            
        _offices = new OfficePlotter(); _step++;
        _radioStations = new RadioStationPlotter();_step++;

        _popup = new PopupHelper(); _step++;
        _track = new TrackPlotter(); _step++;
        if ( restoreMapLongitude == "" && restoreMapLatitude == "")
            _chart.ShowWorld();
        _callsigns.GetCallsigns();         
    }
    catch(e)
    { alert("OnInitialized: "+e+"\nstep: "+_step); }
}


function ProcessResponse(response)
{
    try
    {
    
        //  Since we are processing the callback command, we can clear the
        //  watchdog timer that we created when we submitted it in case 
        //  we had a failed network connection.
        if ( _stuckWatchdog != null)
           {
           clearTimeout( _stuckWatchdog);
           _stuckWatchdog = null;
           }
           
        if(response.Response.Error != null)
            alert("Error from server: "+response.Response.Error);
        if(response.Response.Initialized != null)
        {
            _element.SetMessage(false, "");
            //_browser.Timeout(OnInitialized,0); 
            _browser.Timeout(OnInitialized,0);
        }
        if(response.Response.Callsigns != null)
        {
            _callsigns.UpdateCallsignList(response.Response.Callsigns);
        }
        if(response.Response.Vessels != null)
        {
            _vessels.PlotVessels(response.Response.Vessels, "mapOverlay");
            _browser.Timeout("_track.DisplayTracking()",0); 
            _element.SetElementInnerHTML("vslLoad", "");
        }
        if (response.Response.Offices != null)
        {
            _offices.PlotOffices(response.Response.Offices, "officeOverlay");
        }
        if (response.Response.RadioStations != null) {
            _radioStations.PlotRadioStations(response.Response.RadioStations, "radioStationOverlay");
        }

        if (response.Response.GPSString != null)
        {
            _chart.ShowGPS(response.Response.GPSString);
        }
        if(response.Response.Weather != null)
        {
            if(response.Response.Weather.length > 0)
            {
                _popup.ShowWeatherPopup(response.Response.Weather);
            }
        }
        if(response.Response.Refresh != null)
        {
            _element.SetMessage(false);
            //_browser.Timeout(RefreshImage,0); 
            _browser.Timeout("_chart.RefreshImage()",0); 
        }
        if(response.Response.Tracking != null)
        {
            _track.PlotTracking(response.Response.Tracking, "trackOverlay");
        }
    }
    catch(e)
    { alert("Error: "+e); }
}
function showAboutBox()
{
try
    {
    var nWidth = (screen.availWidth - 784) / 2;
    var nHeight = (screen.availHeight - 480) / 2;
    window.open('GlobeLocatorGoldInfo.aspx', '_aboutBox', 'resizable=no,menubar=no,toolbar=no,location=no,scrollbars=no,status=no,width=784,height=478,left=' + nWidth + ',top=' + nHeight);
    }
catch( e)
    {
    }
}

function showHelpWindow()
{
try
    {
    var nWidth = (screen.availWidth - 1000) / 2;
    var nHeight = (screen.availHeight - 700) / 2;
    window.open('Help/Overview/Overview.aspx', '_blank', 'resizable=yes,menubar=no,toolbar=no,location=no,scrollbars=yes,status=yes,width=1000,height=700,left=' + nWidth + ',top=' + nHeight);
    }
catch( e)
    {
    }
}

function  showPrivacyNotice()
{
try
    {
    var nWidth = (screen.availWidth - 800) / 2;
    var nHeight = (screen.availHeight - 600) / 2;
    window.open('GlobeLocatorPrivacyNotice.aspx', '_privacyBox', 'resizable=yes,menubar=no,toolbar=no,location=no,scrollbars=yes,status=no,width=800,height=600,left=' + nWidth + ',top=' + nHeight);
    }
catch( e)
    {
    }
}

