/******************************** JavaScript Code ******************************************/

/*
   Module of JavaScript code to accompany the city2see website for cities other than the act98
   coverage of Canberra/Queanbeyan which has its own javascript file. The reason for the split
   is that the act98 coverage is basically too low in quality. The routines in this module
   control the functionality of the DisplayImage applet in the ????_air_tiles_init.pl scripts
   by calling various methods of the applet.
*/

/*******************************************************************************************/

     var hideState = 0;                             // Applet data image: visible
     var clipboard, clearClipboardID;
     var infoWindow;
     var scaleInfoWindow = null;
/*
   Absolute zoom in/out levels (working off the base image) in percentages of base image.
   Numerical levels go from -4 (max zoom out level) to 4 (max zoom in level). When added to an
   index conversion factor (4), these levels represent indexes to an array of level percentages.
*/
     var zoominLvl0 = 0, zoominLvl1 = 20, zoominLvl2 = 40, zoominLvl3 = 60,
         zoominLvl4 = 75;
     var zoomoutLvl1 = -15, zoomoutLvl2 = -30, zoomoutLvl3 = -50,
         zoomoutLvl4 = -75;

     var zoomLevels = new Array(zoomoutLvl4, zoomoutLvl3, zoomoutLvl2,
                                zoomoutLvl1, zoominLvl0, zoominLvl1,
                                zoominLvl2, zoominLvl3, zoominLvl3);

     var zoomMaxIn = 4;                             // Max zoom in level
     var zoomMaxOut = -4;                           // Max zoom out level
     var indexConversion = 4;                       // Converts level to array index
/*
   Relative brightness darkness levels working off the current image in percentages of previous
   image
*/
     var brightenLvl0 = 0, brightenLvl1 = 10, brightenLvl2 = 20,
         brightenLvl3 = 30;
     var darkenLvl1 = -10, darkenLvl2 = -20, darkenLvl3 = -30;

     var brightenIncrement = brightenLvl1;          // Brighten increment, oval button
     var darkenIncrement = darkenLvl1;              // Darken increment, oval button


     var on = 1; off = 0;
     var busy = "Busy", ready = "Ready";            // Zoom button states
     var show = "Show", hide = "Hide";              // X-hair/property edges button states
     var prevZoomLvl = 0;
     var noScaleBar;                                // Show the scalebars?

     var zoomButtonImages = new Array(2);           // Zoom button images
     var zoomScaleImages = new Array(10);           // Number of scale images/buttons
     var xhairButtonImages = new Array(2);          // Cross hair button images
     var bdryButtonImages = new Array(2);           // Boundary button images

     var xhairsDisplay, vectorsDisplay;
     var intDirn, numScales;

     xhairsDisplay = on;
     vectorsDisplay = off;
/*
   Preload the xhair button and property boundary button images for hide/show, the zoom buttons
   ready/busy images and the zoom scale images relating to the zoom buttons
*/
     xhairButtonImages[show] = new Image();
     xhairButtonImages[hide] = new Image();
     xhairButtonImages[show].src = "images/ss_b_showxhair.gif";
     xhairButtonImages[hide].src = "images/ss_b_hidexhair.gif";

     bdryButtonImages[show] = new Image();
     bdryButtonImages[hide] = new Image();
     bdryButtonImages[show].src = "images/ss_b_showbdry.gif";
     bdryButtonImages[hide].src = "images/ss_b_hidebdry.gif";

     zoomButtonImages[ready] = new Image();
     zoomButtonImages[busy] = new Image();
     zoomButtonImages[ready].src = "images/ss_icn_zoomdot1.gif";
     zoomButtonImages[busy].src = "images/ss_icn_zoomdot2.gif";

     numScales = zoomScaleImages.length;

     for(i = 0; i < numScales; i++)
     {
        zoomScaleImages[i] = new Image();
     }

     zoomScaleImages[0].src = "images/scl110.gif";  // Scale bar image assignment
     zoomScaleImages[1].src = "images/scl95.gif";
     zoomScaleImages[2].src = "images/scl80.gif";
     zoomScaleImages[3].src = "images/scl70.gif";
     zoomScaleImages[4].src = "images/scl60.gif";
     zoomScaleImages[5].src = "images/scl50.gif";
     zoomScaleImages[6].src = "images/scl40.gif";
     zoomScaleImages[7].src = "images/scl25.gif";
     zoomScaleImages[8].src = "images/scl15.gif";
     zoomScaleImages[9].src = "images/sclnone.gif";

     var usedImgDown  = new Image(116, 22);
     var usedImgJwg   = new Image(116, 22);
     var usedImgEmail = new Image(116, 22);

     var tLeftX = 550;                            // Scale adjustment window parameters
     var tLeftY = 20;
     var width  = 350;
     var height = 250;

//-------------------------------------------------------------------------------------------

     function brightenImage_js(pctBrighten)
     {
/*
     This function organizes display of a brightened or darkened version of the currently
     displayed image by calling the DisplayImage applet's brighten image method. Any cadastral
     data overlayed on the image will be retained in the new image.
*/
        document.DisplayImageApplet.brightenImage(pctBrighten);
        hideState = 0;                                          // Applet data image: visible
     }

//-------------------------------------------------------------------------------------------

     function clearRegForm()
     {

/* Clears the fields of the bume.pl script registration form */

        var countryIndex;

        document.rego_form.name.value     = "";
        document.rego_form.username.value = "";
        document.rego_form.pwd.value      = "";
        document.rego_form.pwd2.value     = "";
        document.rego_form.email.value    = "";
        document.rego_form.address1.value = "";
        document.rego_form.address2.value = "";
        document.rego_form.address3.value = "";
        document.rego_form.state.value    = "";
        document.rego_form.postcode.value = "";

        countryIndex = document.rego_form.country.selectedIndex;
        if(document.rego_form.country[countryIndex].text != "Australia")
        {
           for(i = 0; i < document.rego_form.country.length; i++)
           {
              if(document.rego_form.country[i].text == "Australia")
              {
                 document.rego_form.country[i].selected = true;
                 break;
              }
           }
        }
     }

//------------------------------------------------------------------------------

     function delay(delay_ms)
     {
/*
     Enables a delay of 'delay_ms' to be inserted into operations.
*/
        var future, limit, future_ms, limit_ms;

        limit    = new Date();
        limit_ms = limit.getTime() + delay_ms;

        do
        {
           future = new Date();
           future_ms = future.getTime();
        }  while ((limit_ms - future_ms) > 0);
     }

//-------------------------------------------------------------------------------------------

     function getFocus()
     {
/*
     Restores the ability of the applet to listen to keyboard events when the browser page
     gains focus.
*/
        document.DisplayImageApplet.restoreFocus();
     }

//-------------------------------------------------------------------------------------------

     function getState()
     {
/*
     This function is called on initial load of the page or after use of the browser 'back'
     button. Its purpose is to load the correct scale image at startup, but to blank out the
     scale thereafter if the user uses the back button which will cause the scale to become
     invalid. The state is retained by writing into a hidden field in the page (non-functional
     form).
*/
        noScaleBar = document.stateForm.scale_state.value;

        if(noScaleBar >= 1)                                     // Above intial zoom out/in max
        {                                                       // Set no image now
           document.scale.src = zoomScaleImages[numScales -  1].src;
           document.stateForm.scale_state.value = noScaleBar;
        }
        else                                                    // Set midpoint scale image
        {
           document.scale.src = zoomScaleImages[zoominLvl0 + indexConversion].src;
           document.stateForm.scale_state.value = 0;
        }
     }

//------------------------------------------------------------------------------

     function getUsedImages(downloadImgName, jwgImgName, emailImgName)
     {
/*
     Called by the 'onload' event of the 'make_world_file.pl' script to pre-load images when that
     page loads. The images may be used as part of the button functionality of that page. They
     provide a greying-out capability to indicate an operation has been made
*/
        usedImgDown.src  = downloadImgName;
        usedImgJwg.src   = jwgImgName;
        usedImgEmail.src = emailImgName;
     }

//-------------------------------------------------------------------------------------------

     function initialize()
     {
/*
     This function initializes a clipboard object that can be cleared at periodic intervals to
     prevent users from copying images to the clipboard. It also binds the event handler that
     oversees the window getting focus to a global focus handler. It is called by the onload
     event handler when the page is loaded.
*/
//        clipboard = window.clipboardData;
//        clearClipboardID = setInterval("clipboard.clearData()", 250);
        window.onfocus = getFocus;
     }

//-------------------------------------------------------------------------------------------

     function killScaleWindow()
     {
/*
     Displays info on how to adjust the scale of a downloaded image on City2See in a popup window if
     it can be opened.
*/
        if(scaleInfoWindow)                    // Kill window from previous use in case it is minimized
        {
           scaleInfoWindow.close();
           scaleInfoWindow = null;
        }
     }

//-------------------------------------------------------------------------------------------

     function newBrightenLevel(lightenDarken)
     {
/*
     This function organizes a new brightened/darkened image based on the use of the brighten or
     darken oval buttons. The image is brightened/darkened by a set amount.
*/
        if(lightenDarken == "brighten")                         // Wants to increment brightening
        {
           brightenImage_js(brightenIncrement);
        }
        else                                                    // Wants to increment darkening
        {
           brightenImage_js(darkenIncrement);
        }
     }

//-------------------------------------------------------------------------------------------

     function newZoomLevel(inOrOut)
     {
/*
     This function organizes zooming of the image based on use of the oval brighten and darken
     buttons at each end of the bank of zoom round buttons. It checks whether the existing zoom
     setting can still be incremented (zoom in) or decremented (zoom out). If it can, it requests
     the applet to execute the operation and updates the status of the display round buttons. If
     no operation can be performed, the request is ignored, at present, for zooming in. For
     zooming out, the next multiple of 100% above the maximum zoom out level is used as the new
     zoom out level and the zoom round buttons are organized so that the central one is set busy
     and the zoom base coordinates are reset in the applet to the new zoomed out coordinates.
*/
        var incZoomLvl, canZoom, pctlvlIndex, pctZoom, gotPctZoom;

// Calculate the new zoom level if it is possible to change it

        canZoom = false;                    // Preset cannot zoom
        gotPctZoom = false;                 // Preset no zoom pct level yet

        if(inOrOut == "in")                 // Wants to zoom in a level
        {
           if(prevZoomLvl < zoomMaxIn)      // Not at maximum zoom in?
           {
              incZoomLvl = prevZoomLvl + 1; // No, increment zoom to next level
           }
           else                             // Max zoom in+: Down a level of zoom: always < 100%
           {
              pctZoom = (zoominLvl4 + 5) + 100;  // 5% on top of max zoom in + indicator
              gotPctZoom = true;                 // Preset got zoom pct level
              incZoomLvl = 0;                    // For centre round button to be busy
              noScaleBar = 1;                    // Turn off scale bar
           }

           canZoom = true;
        }
        else                                // Wants to zoom out a level
        {
           if(prevZoomLvl > zoomMaxOut)     // Not at maximum zoom out?
           {
              incZoomLvl = prevZoomLvl - 1; // No, decrement zoom to next level
           }
           else                             // Needs to go up a level of zoom
           {
              strPctZoom = String(zoomoutLvl4 / 100);
              pctZoom = (parseInt(strPctZoom) - 1) * 100;
              gotPctZoom = true;            // Preset got zoom pct level
              incZoomLvl = 0;               // For centre round button to be busy
              noScaleBar = 1;               // Turn off scale bar
           }

           canZoom = true;
        }
/*
   Now get the percentage zoom corresponding to the zoom level if we have not already got it.
   Convert the new zoom level to an index into the array of zoom levels.
*/
        if(canZoom)                         // We can zoom!
        {
           if(!gotPctZoom)
           {
              pctlvlIndex = incZoomLvl + indexConversion;
              pctZoom = zoomLevels[pctlvlIndex];
           }
           zoomImage_js(pctZoom, incZoomLvl);
        }
     }

//-------------------------------------------------------------------------------------------

     function panImage_js(strDirn)
     {
/*
     This function organizes display of a panned image by calling the DisplayImage applet's pan
     method. It also ensures that the cadastral overlay flag is set to off. A newly panned image
     will not display cadastral data if this was overlayed in the previous image.
*/
        if(strDirn == "N")
        {
           intDirn = 1;
        }
        else if(strDirn == "NE")
        {
           intDirn = 2;
        }
        else if(strDirn == "E")
        {
           intDirn = 3;
        }
        else if(strDirn == "SE")
        {
           intDirn = 4;
        }
        else if(strDirn == "S")
        {
           intDirn = 5;
        }
        else if(strDirn == "SW")
        {
           intDirn = 6;
        }
        else if(strDirn == "W")
        {
           intDirn = 7;
        }
        else if(strDirn == "NW")
        {
           intDirn = 8;
        }

        vectorsDisplay = off;                                // Maintain state: new pan, no vectors

        document.DisplayImageApplet.panImage(intDirn);
        hideState = 0;                                       // Applet data image: visible
                                                             // Switch boundaries button to show
        document.bdrybutton.src = bdryButtonImages[show].src;
     }

//-------------------------------------------------------------------------------------------

     function printWindow(contentFile, topLeftX, topLeftY)
     {

     /* Opens a new browser window, that has a printing option available, with
        the contents of the nominated URL.
     */

        if(infoWindow)
        {
           infoWindow.close();
           infoWindow = null;
        }

        infoWindow = window.open(contentFile,"infoWindow","left="+topLeftX+",top="
             +topLeftY+",toolbar=yes,resizable=yes,scrollbars=yes,width=760,height=480");
     }

//-------------------------------------------------------------------------------------------

     function scaleInfo(contentFile)
     {
/*
     Displays info on how to adjust the scale of a downloaded image on City2See in a popup window if
     it can be opened.
*/
        var status;

        status = scaleWindow(contentFile, tLeftX, tLeftY, width, height);

        return status;
     }

//-------------------------------------------------------------------------------------------

     function scaleWindow(contentFile, tlx, tly, w, h)
     {
/*
     Opens a new browser window, with printing option, that displays the information required for
     a client to adjust the scale of their downloaded image should their screen not be operating
     at 96dpi.
*/
        killScaleWindow();                     // Kill window from previous use in case it is minimized

        scaleInfoWindow = window.open(contentFile,"scaleInfoWin","left="+tlx+",top="
                            +tly+",width="+w+",height="+h+",toolbar=no,resizable=no,scrollbars=no");

        if(!scaleInfoWindow)                   // No popup window: continue action in calling script
        {
           return true;
        }

        return false;                          // Popup window opened: cancel action in calling script
     }

//-------------------------------------------------------------------------------------------

     function setSubmitSource(source)
     {
/*
     This function saves which submit button was used to submit the login form of the
     user_admin.pl script. It does so by storing the value 0 or 1 (login submit button or
     new account submit button respectively) in the new account query parameter that is a
     hidden field on the form.
*/

        if(source == 0)                              // Login submit button
        {
           document.login_form.na.value = "0";
        }
        else if(source == 1)                         // New account submit button
        {
           document.login_form.na.value = "1";
        }
     }

//-------------------------------------------------------------------------------------------

     function setZoomButton(zoomLvl, status)
     {
/*
     This function maintains the state of the zoom buttons: setting the just clicked button to
     the busy image (red centre) and the previous busy button to the ready (green centre) image.
     Simultaneously, the scale image represented by the zoom level is also changed, either to the
     new scale or blank if the maximum zoom in/out has been exceeded.
*/
        var imageIndex = zoomLvl + indexConversion; // Scale bar image array index: for odd ...
                                                    // ...# levels, 0 level = number/2 truncated
        if(noScaleBar >= 1)                         // Exceeded intial zoom out/in max
        {
           document.scale.src = zoomScaleImages[numScales -  1].src;  // Set no image now
           document.stateForm.scale_state.value = noScaleBar;
        }
        else
        {
           document.scale.src = zoomScaleImages[imageIndex].src;      // Set scale image
           document.stateForm.scale_state.value = 0;
        }

        if(zoomLvl == 0)                                              // Zoom in levels
        {
           document.zoomin0.src = zoomButtonImages[status].src;
        }
        else if(zoomLvl == 1)
        {
           document.zoomin1.src = zoomButtonImages[status].src;
        }
        else if(zoomLvl == 2)
        {
           document.zoomin2.src = zoomButtonImages[status].src;
        }
        else if(zoomLvl == 3)
        {
           document.zoomin3.src = zoomButtonImages[status].src;
        }
        else if(zoomLvl == 4)
        {
           document.zoomin4.src = zoomButtonImages[status].src;
        }
        else if(zoomLvl == -1)                                  // Zoom out levels
        {
           document.zoomout1.src = zoomButtonImages[status].src;
        }
        else if(zoomLvl == -2)
        {
           document.zoomout2.src = zoomButtonImages[status].src;
        }
        else if(zoomLvl == -3)
        {
           document.zoomout3.src = zoomButtonImages[status].src;
        }
        else if(zoomLvl == -4)
        {
           document.zoomout4.src = zoomButtonImages[status].src;
        }
     }

//-------------------------------------------------------------------------------------------

     function tidyUp()
     {
/*
     This function stops the process of clearing the clipboard at regular intervals of time and
     is called by the onunload() event handler.
*/
//        clearInterval(clearClipboardID);
     }

//------------------------------------------------------------------------------

     function usedImage(whichImage)
     {
/*
     Depending on the button clicked in the calling script 'make_world_file.pl', this function will
     substitute a greyed-out version of the image used as the button to indicate that that action
     has already taken place.
*/
        if(whichImage == "download")
        {
           document.downloadImgDown.src = usedImgDown.src;
        }
        else if(whichImage == "jgw")
        {
           document.downloadImgJgw.src = usedImgJwg.src;
        }
        else if(whichImage == "email")
        {
           document.downloadImgEmail.src = usedImgEmail.src;
        }
     }

//-------------------------------------------------------------------------------------------

     function vectorsImage_js()
     {
/*
     This function organizes display of cadastral (vector) data over the currently displayed
     image by calling the DisplayImage applet's overlay cadastral data method. It behaves
     essentially as a toggle switch. Note that if the overlayed image is changed by a change in
     coordinates of the viewed area (pan, zoom), the cadastral data is not displayed in the new
     image. The toggle flag has therefore to be set to off in those routines dealing with panning
     and zooming. The routine also selects the appropriate show/hide property boundaries button
     according to the display state of the boundaries eg hide boundaries button if the boundaries
     are currently displayed.
*/
        var buttonstate;

        if(vectorsDisplay == on)
        {
           buttonstate = show;
           vectorsDisplay = off;
        }
        else if(vectorsDisplay == off)
        {
           buttonstate = hide;
           vectorsDisplay = on;
        }

        document.bdrybutton.src = bdryButtonImages[buttonstate].src;
        document.DisplayImageApplet.vectorsImage(vectorsDisplay);
        hideState = 0;                                             // Applet data image: visible
     }

//-------------------------------------------------------------------------------------------

     function xhairsImage_js()
     {
/*
     Displays the currently displayed image without the cross-hairs showing. It merely makes a
     call to the DisplayImage applet to turn off the cross hairs.
*/
        var buttonstate;

        if(xhairsDisplay == on)
        {
           buttonstate = show;
           xhairsDisplay = off;
        }
        else if(xhairsDisplay == off)
        {
           buttonstate = hide;
           xhairsDisplay = on;
        }

        document.xhairbutton.src = xhairButtonImages[buttonstate].src;
        document.DisplayImageApplet.toggleCrossHairs(xhairsDisplay);
     }

//-------------------------------------------------------------------------------------------

     function zoomImage_js(pctZoom, newZoomLvl)
     {
/*
     This function organizes display of a zoomed image by calling the DisplayImage applet's zoom
     method. It also sets the GUI zoom buttons to reflect the newly selected zoom state, stores
     this button's 'id' as the new previous button and ensures that the cadastral overlay flag is
     set to off. A newly zoomed image will not display cadastral data if this was overlayed in
     the previous image.
*/
        vectorsDisplay = off;               // Maintain state: new zoom, no vectors

        setZoomButton(prevZoomLvl, ready);  // Previous zoom level button set ready
        setZoomButton(newZoomLvl, busy);    // New zoom level button set busy

        prevZoomLvl = newZoomLvl;           // New previous zoom level
        document.DisplayImageApplet.zoomImage(pctZoom);
        hideState = 0;                                       // Applet data image: visible
                                                             // Switch bondaries button to show
        document.bdrybutton.src = bdryButtonImages[show].src;
     }
