﻿// Common utility functions
// from trackme.com

function getQueryParams() {
    var qsParmArray = new Array();
    var query = window.location.search.substring(1);
    var parms = query.split('&');
    for (var i = 0; i < parms.length; i++) {
        var pos = parms[i].indexOf('=');
        if (pos > 0) {
            var key = parms[i].substring(0, pos);
            var val = parms[i].substring(pos + 1);
            qsParmArray[key] = val;
        }
    }
    return qsParmArray;
}

function createXMLHttpRequest() {
    try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { }
    try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { }
    try { return new XMLHttpRequest(); } catch (e) { }
    alert("This browser does not support Ajax");
    return null;
}

function loadDivHTML(url, div) {
    var xmlHttpRequest = createXMLHttpRequest();

    var handler = function() {
        if (xmlHttpRequest.readyState == 4) {
            if (xmlHttpRequest.status == 200) {
                div.innerHTML = xmlHttpRequest.responseText;
            } else {
                alert('Error - LoadHTML failed.');
            }
        }
    }

    xmlHttpRequest.onreadystatechange = handler;
    xmlHttpRequest.open("GET", url, true);
    xmlHttpRequest.send(null);
}


function JsonRequest(id, url) {
    
    var bodyTag = document.getElementsByTagName("body").item(0);
    this.divObjectReference = document.createElement("div");
    this.divObjectReference.id = id;
    this.id = id;
    this.url = url;

    bodyTag.appendChild(this.divObjectReference);
}

JsonRequest.prototype =
{
    doRequest: function(callback) {
        var headTag = document.getElementsByTagName("head").item(0);

        var Node = $get(this.id + "_script");
        if (Node != null) {
            Node.parentNode.removeChild(Node);
        }

        var requestUrl = this.url + ((this.url.indexOf("?") == -1) ? "?" : "&");
        requestUrl += "callback=" + escape(callback);

        var scriptObj = document.createElement("script");
        scriptObj.setAttribute("type", "text/javascript");
        scriptObj.setAttribute("src", requestUrl);
        scriptObj.setAttribute("id", this.id + "_script");

        headTag.appendChild(scriptObj);
    }
}

function makeValidHTMLId(str) {
    return str.replace(/\W/g, "__");
}

// Notify ScriptManager that this is the end of the script.
if (typeof (Sys) !== 'undefined') {
    Sys.Application.notifyScriptLoaded();
}

//figures out the dimensions for the passed map passes back coords

function GetMapBounds(myMap) {

    //build the bounding box
    var myCoords = new ShareMyRoutes.webservice.Coords();
    var BottomRightLatLong;
    var TopLeftLatLong;

    if (map.GetMapMode() == VEMapMode.Mode2D) {

        //2D mode for birds eye - there's a few different sorts of birdseye
        if (map.GetMapStyle() == VEMapStyle.Birdseye | map.GetMapStyle() == VEMapStyle.BirdseyeHybrid | map.GetMapStyle() == VEMapStyle.Oblique) {

            //2D birdseye
            var birdseye = map.GetBirdseyeScene();
            var rect = birdseye.GetBoundingRectangle();

            TopLeftLatLong = new VELatLong(rect.TopLeftLatLong.Latitude, rect.TopLeftLatLong.Longitude);
            BottomRightLatLong = new VELatLong(rect.BottomRightLatLong.Latitude, rect.BottomRightLatLong.Longitude);
          
        }
        else {
            //2D mode for regular view
     
            //convert pixels to lat/longs
            var pixel = new VEPixel(0, 0);
            TopLeftLatLong = new VELatLong(map.PixelToLatLong(pixel).Latitude, map.PixelToLatLong(pixel).Longitude);

            //figure out the bounds of the map
            var mapBox = Sys.UI.DomElement.getBounds($get(myMap));

            pixel = new VEPixel(mapBox.width, mapBox.height);
            BottomRightLatLong = new VELatLong(map.PixelToLatLong(pixel).Latitude, map.PixelToLatLong(pixel).Longitude);
        }
        
    }
    else {

        //3D mode - bit of a hack as the view actually has 4 corners
        //not just a top right and bottom left
        var view = map.GetMapView();
        TopLeftLatLong = view.TopLeftLatLong;
        BottomRightLatLong = view.BottomRightLatLong;
        
        //TODO - work out a better way of figuring out bounds
    }
    
    //get the coords into the args
    myCoords.topLeftLat = TopLeftLatLong.Latitude;
    myCoords.topLeftLon = TopLeftLatLong.Longitude;

    myCoords.bottomRightLat = BottomRightLatLong.Latitude;
    myCoords.bottomRightLon = BottomRightLatLong.Longitude;

    return myCoords;
    
}

//strip HTML
function stripHTML(stringWithHTML) {

    while (stringWithHTML != (stringWithHTML = stringWithHTML.replace(/<[^>]*>/g, "")));
    stringWithHTML = stringWithHTML.replace(/More Details.../g, "");
//while (stringWithHTML != (stringWithHTML = stringWithHTML.replace(/<[^<>]*>/g, "")));

return  unescape(stringWithHTML);

}


