﻿
var Utility = {
    /// <summary>
    ///   static Utility class
    /// </summary>

    OnFailed: function(error)
    {
        /// <summary>
        ///     This is the failed callback function for all webservices.
        /// </summary>  
        /// <param name="error">The error object from the webservice</param>          

        var stackTrace = error.get_stackTrace();
        var message = error.get_message();
        var statusCode = error.get_statusCode();
        var exceptionType = error.get_exceptionType();
        var timedout = error.get_timedOut();

        // Display the error.    
        var RsltElem =
            "Stack Trace: " + stackTrace + "<br/>" +
            "Service Error: " + message + "<br/>" +
            "Status Code: " + statusCode + "<br/>" +
            "Exception Type: " + exceptionType + "<br/>" +
            "Timedout: " + timedout;

        alert(RsltElem);
    },

    decodeLine: function(encoded)
    {
        /// <summary>
        ///     Decode an encoded string into a list of VE lat/lng.
        /// </summary>  
        /// <param name="encoded">The encoded string</param>       
        /// <returns>Array of VELatLong</returns>

        var len = encoded.length;
        var index = 0;
        var array = [];
        var lat = 0;
        var lng = 0;
        try
        {
            while (index < len)
            {
                var b;
                var shift = 0;
                var result = 0;
                do
                {
                    b = encoded.charCodeAt(index++) - 63;
                    result |= (b & 0x1f) << shift;
                    shift += 5;
                } while (b >= 0x20);
                var dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
                lat += dlat;

                shift = 0;
                result = 0;
                do
                {
                    b = encoded.charCodeAt(index++) - 63;
                    result |= (b & 0x1f) << shift;
                    shift += 5;
                } while (b >= 0x20);
                var dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
                lng += dlng;

                array.push(new VELatLong((lat * 1e-5), (lng * 1e-5)));
            }
        } catch (ex)
        {
            //error in encoding.
        }
        return array;
    },

    createEncodings: function(points)
    {
        /// <summary>
        ///     Create the encoded bounds.
        /// </summary>  
        /// <param name="points">Array of VELatLong</param>       
        /// <returns>The encoded string</returns>    
        var i = 0;
        var plat = 0;
        var plng = 0;
        var encoded_points = "";

        for (i = 0; i < points.length; ++i)
        {
            var point = points[i];
            var lat = point.Latitude;
            var lng = point.Longitude;

            var late5 = Math.floor(lat * 1e5);
            var lnge5 = Math.floor(lng * 1e5);

            dlat = late5 - plat;
            dlng = lnge5 - plng;

            plat = late5;
            plng = lnge5;

            encoded_points += this._encodeSignedNumber(dlat) + this._encodeSignedNumber(dlng);
        }
        return encoded_points;
    },

    _encodeSignedNumber: function(num)
    {
        /// <summary>
        ///     Encode a signed number in the encode format.
        /// </summary>  
        /// <param name="num">signed number</param>       
        /// <returns>encoded string</returns>       
        var sgn_num = num << 1;

        if (num < 0)
        {
            sgn_num = ~(sgn_num);
        }

        return (this._encodeNumber(sgn_num));
    },

    _encodeNumber: function(num)
    {
        /// <summary>
        ///     Encode an unsigned number in the encode format.
        /// </summary>  
        /// <param name="num">unsigned number</param>       
        /// <returns>encoded string</returns>        
        var encodeString = "";

        while (num >= 0x20)
        {
            encodeString += (String.fromCharCode((0x20 | (num & 0x1f)) + 63));
            num >>= 5;
        }

        encodeString += (String.fromCharCode(num + 63));
        return encodeString;
    },

    ToRadian: function(v)
    {
        return v * (Math.PI / 180);
    },
    DiffRadian: function(v1, v2)
    {
        return this.ToRadian(v2) - this.ToRadian(v1);
    },

    CalcDistance: function(lat1, lng1, lat2, lng2, radius)
    {
        return radius * 2 * Math.asin(
            Math.min(1,
                Math.sqrt(
                    (
                        Math.pow(Math.sin((this.DiffRadian(lat1, lat2)) / 2.0), 2.0) +
                        Math.cos(this.ToRadian(lat1)) * Math.cos(this.ToRadian(lat2)) *
                        Math.pow(Math.sin((this.DiffRadian(lng1, lng2)) / 2.0), 2.0)
                    )
               )
           )
       );
    },

    DetectBrowser: function()
    {
        var ret = "";
        var d, dom, ie, ie4, ie5x, moz, mac, win, lin, old, ie5mac, ie5xwin, op, chr;
        d = document;
        n = navigator;
        na = n.appVersion;
        nua = n.userAgent;
        win = (na.indexOf('Win') != -1);
        mac = (na.indexOf('Mac') != -1);
        lin = (nua.indexOf('Linux') != -1);
        if (!d.layers)
        {
            dom = (d.getElementById);
            op = (nua.indexOf('Opera') != -1);
            konq = (nua.indexOf('Konqueror') != -1);
            saf = (nua.indexOf('Safari') != -1);
            chr = (nua.indexOf('Chrome') != -1);
            moz = (nua.indexOf('Gecko') != -1 && !saf && !konq);
            ie = (d.all && !op);
            ie4 = (ie && !dom);
            ie5x = (d.all && dom);
            ie5mac = (mac && ie5x);
            ie5xwin = (win && ie5x);
            ie6 = (nua.indexOf('MSIE 6.0') != -1 && ie5x);
            ie7 = (nua.indexOf('MSIE 7.0') != -1 && ie5x);
        }

        if (moz)
            return "moz"
        else if (ie6)
            return "ie6"
        else if (ie7)
            return "ie7";
    },

    ConvertStringToBoolean: function(stringValue)
    {
        if (stringValue.toLowerCase() == "false")
            return false;
        else
            return true;
    },
    
    getWindowWidth: function()
    {
        //src: http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
        var myWidth = 0
        if (typeof (window.innerWidth) == 'number')
        {
            //Non-IE
            myWidth = window.innerWidth;
        } else if (document.documentElement && document.documentElement.clientWidth)
        {
            //IE 6+ in 'standards compliant mode'
            myWidth = document.documentElement.clientWidth;
        } else if (document.body && (document.body.clientWidth || document.body.clientHeight))
        {
            //IE 4 compatible
            myWidth = document.body.clientWidth;
        }
        return myWidth;
    },

    getWindowHeight: function()
    {
        var myHeight = 0
        if (typeof (window.innerHeight) == 'number')
        {
            //Non-IE
            myHeight = window.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight)
        {
            //IE 6+ in 'standards compliant mode'
            myHeight = document.documentElement.clientHeight;
        } else if (document.body && (document.body.clientWidth || document.body.clientHeight))
        {
            //IE 4 compatible
            myHeight = document.body.clientHeight;
        }
        return myHeight;
    }

}

Utility.EarthRadiusInMiles = 3956.0;
Utility.EarthRadiusInKilometers = 6367.0;

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

