﻿Type.registerNamespace("WirMap");

WirMap.VirtualEarth = function(element) {
    // Call the constructor of the base class.
    WirMap.VirtualEarth.initializeBase(this, [element]);
    
    this._instance = null;
   
    this._baseLayerName = "VEBASELAYER";
    this._initialZoomLevel = 9;
    this._initialDashBoardSize = null;
    this._routeDirectionsContainerId = null;
    this._routeDistanceContainerId = null;
    this._pushPins = null;
    this._jobAddresses = null;
    this._routes = null;
    
    this._shapeLayers = null;
    this._routeLayers = null;
 
    this._LayerInitId = null;
    this._curLayerId = null;
    
    
    this._shapeIndexHelper = null;
    this._class = "VirtualEarthMap";
    this._veToken = "";
    this._timeoutCounter = 0;
    this._extTimeoutCounter = 0;
    this._elemId = null;
    
}
WirMap.VirtualEarth.prototype = {
    // Perform the initial setup of the instance.
    initialize: function() {
        // Call the base implementation.
        WirMap.VirtualEarth.callBaseMethod(this, "initialize");
        this._shapeIndexHelper = [];
        this._elemId = this.get_element().id;
        //assigning correct css class
        $get(this._elemId).className = this._class;
        //loading... message:
        $get(this._elemId).innerHTML = '<div style="font-size:11px; font-family:Arial; padding-top:8px; padding-left:8px; color:#333333;"><strong>Loading Map...</strong></div>';

        //setting up objects from JSON
        if (this._shapeLayers != null) {
            eval(this._shapeLayers);
        }

        if (this._routeLayers != null) {
            eval(this._routeLayers);
        }

        if (this._pushPins != null) {
            eval(this._pushPins);
        }

        if (this._routes != null) {
            eval(this._routes);
        }

        if (this._jobAddresses != null) {
            eval(this._jobAddresses);
        }

        //setup the map
        this._setupMap();
    },

    _setupMap: function() {
        if (this._timeoutCounter >= 6) {
            //we are not able to load the map for some reason
            //alert('Could not load the map at this time (err1)');
            $get(this._elemId).innerHTML = '<div style="font-size:11px; font-family:Arial; padding-top:8px; padding-left:8px; color:#cc0000;"><strong>Could not load the map at this time (err1)</strong></div>';
        }
        else if (window.VEMap === undefined) {
            //not loaded ms map yet, try again in a few seconds
            this._timeoutCounter += 1;
            var t = this;
            setTimeout(function() { t._setupMap(); }, 1500);
        }
        else {
            var firstLoad = false;

            if (!this._instance) {
                firstLoad = true;
                this._instance = new VEMap(this._elemId);
                if (this._veToken != "") { this._instance.SetClientToken(this._veToken); } //###Virtual Earth Customer ID###  
            }


            if (this._initialDashBoardSize != null) {
                if (this._initialDashBoardSize == 0)
                    this._instance.SetDashboardSize(VEDashboardSize.Tiny);
                else if (this._initialDashBoardSize == 1)
                    this._instance.SetDashboardSize(VEDashboardSize.Small);
                else if (this._initialDashBoardSize == 2)
                    this._instance.SetDashboardSize(VEDashboardSize.Normal);
                else if (this._initialDashBoardSize == 3)
                    this._instance.HideDashboard();
            }
            else {
                this._instance.SetDashboardSize(VEDashboardSize.Small);
            }



            //Don't Load the Map More Than Once...
            if (firstLoad) {
                //this._instance.LoadMap(latlonobj, this._initialZoomLevel, VEMapStyle.Road, false);
                //this._instance.LoadMap();

                this._instance.LoadMap();
                this._instance.SetZoomLevel(this._initialZoomLevel);
                //this._instance.ClearInfoBoxStyles();

            }
            else {
                //CB.VEMapWindow.ClearPushpins();
                //this._instance.SetCenter(latlonobj);
            }



            //lets add the shapelayers for the map
            this._shapeIndexHelper[this._baseLayerName] = 0;
            for (var i = 1; i < this._shapeLayers.length; i++) {
                this._shapeIndexHelper[this._shapeLayers[i].ShapeLayerId] = i;
                var sl = new VEShapeLayer();
                sl.SetTitle(this._shapeLayers[i].ShapeLayerId);
                this._instance.AddShapeLayer(sl);
                //the map now has all the shapelayers added it needs
            }


            //lets setup all data needed (pushpins, routes, addresses) - addresses are actually setup on the fly when
            //showShapeLayer is called
            if (this._pushPins.length > 0) {
                //we have lat/lon coords
                this._setDataFromCoords(this._pushPins);
            }

            this._createMessageBox();
            this._createFullScreenMessageBox();


            if (this._LayerInitId != "") {
                var bShowShapeLayer = true;
                //is it routelayer?
                for (var i = 0; i < this._routeLayers.length; i++) {
                    if (this._routeLayers[i] == this._LayerInitId)
                        bShowShapeLayer = false;    //but show routelayer first, as requested
                }

                this._curLayerId = this._LayerInitId;

                if (bShowShapeLayer)
                    this.showShapeLayer(this._curLayerId);
                else
                    this.showRouteLayer(this._curLayerId);

            }

        }

    },

    _createMessageBox: function() {
        var el = document.createElement("div");
        el.id = "myControl";
        var myHeight = parseInt($get(this._elemId).style.height) - 20;
        el.style.top = myHeight + "px";
        el.style.left = 0;
        el.style.fontSize = "11px";
        el.style.lineHeight = "20px";
        el.style.height = "20px";
        el.style.textAlign = "center";
        el.style.width = parseInt($get(this._elemId).style.width) + "px";
        el.style.filter = 'alpha(opacity=' + 90 + ')'; // (integer number between 0 and 100)
        el.style.MozOpacity = "0.90"; // (floating number between 0 and 1
        el.style.display = "block";
        el.style.clear = "both";
        el.style.visibility = "hidden";
        el.style.position = "absolute";
        el.style.zIndex = 400;
        $get(this._elemId).appendChild(el);
    },

    _createFullScreenMessageBox: function() {

        var el = document.createElement("div");
        el.id = "myFSControl";
        el.style.top = 0;
        el.style.left = 0;
        el.style.fontSize = "12px";
        el.style.lineHeight = parseInt($get(this._elemId).style.height) + "px";
        el.style.height = parseInt($get(this._elemId).style.height) + "px";
        el.style.textAlign = "center";
        el.style.width = parseInt($get(this._elemId).style.width) + "px";
        el.style.filter = 'alpha(opacity=' + 85 + ')'; // (integer number between 0 and 100)
        el.style.MozOpacity = "0.85"; // (floating number between 0 and 1
        el.style.overflow = "hidden";
        el.style.display = "block";
        el.style.clear = "both";
        el.style.visibility = "hidden";
        el.style.position = "absolute";
        el.style.zIndex = 410;
        $get(this._elemId).appendChild(el);
    },

    // Perform the disposing of the instance.
    dispose: function() {
        // Nothing to dispose.
        WirMap.VirtualEarth.callBaseMethod(this, "dispose");
    },

    showShapeLayer: function(shapelayerid) {
        //show the correct layer
        //we have to make sure our objects are referencable when someone is trying to call this from the outside.
        if (this._extTimeoutCounter >= 6) {
            //we are not able to load the map for some reason
            //alert('Could not load the map at this time(err2)');
            $get(this._elemId).innerHTML = '<div style="font-size:11px; font-family:Arial; padding-top:8px; padding-left:8px; color:#cc0000;"><strong>Could not load the map at this time (err2)</strong></div>';
        }
        else if (this._instance == null || window.VEMap === undefined) {
            //not loaded ms map yet, try again in a few seconds
            this._extTimeoutCounter += 1;
            var t = this;
            setTimeout(function() { t.showShapeLayer(shapelayerid); }, 1500);
        }
        else {
            this._extTimeoutCounter = 0;
            this._hideMessage();


            //before we go ahead and show the layer, we need to figure out if there is an address already
            //setup for the requested shapelayer. If so, we need to plot it on the layer

            if (this._jobAddresses != null) {
                //there can only be one address per layer!
                this._setDataFromAddress(this._jobAddresses, shapelayerid);
            }

            if (this._shapeIndexHelper[shapelayerid] != null) {
                this._instance.HideAllShapeLayers();
                var visibleLayer = this._instance.GetShapeLayerByIndex(this._shapeIndexHelper[shapelayerid]);
                visibleLayer.Show();
                this._curLayerId = shapelayerid;
                //this._instance.SetZoomLevel(this._initialZoomLevel);
                this._adjustMapViewForLayer(shapelayerid);
            }


        }
    },

    showRouteLayer: function(routelayerid) {
        //show the correct layer
        //we have to make sure our objects are referencable when someone is trying to call this from the outside.
        if (this._extTimeoutCounter >= 6) {
            //we are not able to load the map for some reason
            //alert('Could not load the map at this time(err3)');
            $get(this._elemId).innerHTML = '<div style="font-size:11px; font-family:Arial; padding-top:8px; padding-left:8px; color:#cc0000;"><strong>Could not load the map at this time (err3)</strong></div>';
        }
        else if (this._instance == null || window.VEMap === undefined) {
            //not loaded ms map yet, try again in a few seconds
            this._extTimeoutCounter += 1;
            var t = this;
            setTimeout(function() { t.showRouteLayer(routelayerid); }, 1500);
        }
        else {

            this._extTimeoutCounter = 0;
            this._hideMessage();

            var bSuccess = true;
            if (this._routes[routelayerid] != null) {
                //there can only be one route per layer!
                var routeObj = null;
                var fromAddress = "";
                var toAddress = "";

                routeObj = this._routes[routelayerid];
                fromAddress = this._getFullAddressString(routeObj["FromAddress"]);
                toAddress = this._getFullAddressString(routeObj["ToAddress"]);

                if (fromAddress != "" && toAddress != "") {
                    var distContainerId = this._routeDistanceContainerId;
                    if (distContainerId != null && $get(distContainerId) != null) {
                        $get(distContainerId).innerHTML = "Calculating..."
                    }

                    this._showFullScreenMessage('Plotting Route...');
                    this._instance.DeleteRoute(); //clear routes
                    this._instance.HideAllShapeLayers();

                    var locations = [];
                    locations.push(fromAddress);
                    locations.push(toAddress);
                    var options = new VERouteOptions;
                    options.DrawRoute = true;
                    options.ShowDisambiguation = false; //leave it false for now - might revisit, also check for bug in safari (panel not showing)
                    options.SetBestMapView = true;

                    options.RouteCallback = Function.createDelegate(this, this.routeCallback);

                    var t = this;
                    setTimeout(function() { t._instance.GetDirections(locations, options); }, 1);
                    this._curLayerId = routelayerid;

                }

            }

        }

    },

    _setDataFromAddress: function(arrAddresses, layerid) {
        var theAddress = "";
        layerindex = this._shapeIndexHelper[layerid];

        if (arrAddresses[layerid] != null) {
            theAddress = this._getFullAddressString(arrAddresses[layerid]);
            if (theAddress != "") {
                this._instance.DeleteRoute(); //clear routes
                this._addressFind(theAddress);
            }
            else {
                var layerobj = this._instance.GetShapeLayerByIndex(layerindex);
                if (layerobj == null || layerobj.GetShapeCount() == 0) {
                    this._showMessage("<strong>Location(s) not found.<strong>", "#296692", "#ffffff");
                    return;
                }
            }
        }

    },

    _addressFind: function(theAddress) {
        this._instance.Find(null,    // what
                          theAddress, // where
                          null,    // VEFindType (always VEFindType.Businesses)
                          null,    // VEShapeLayer (base by default)
                          null,    // start index for results (0 by default)
                          null,    // max number of results (default is 10)
                          null,    // show results? (default is true)
                          null,    // create pushpin for what results? (ignored since what is null)
                          false,    // use default disambiguation? (default is true)
                          null,    // set best map view? (default is true)
                          Function.createDelegate(this, this.resultCallback));


    },

    resultCallback: function(shapeLayer, findResults, places, moreResults, errorMsg) {
        // if there are no results, display any error message and return
        if (places == null) {
            var layerindex = this._shapeIndexHelper[this._curLayerId];
            var layerobj = this._instance.GetShapeLayerByIndex(layerindex);
            if (layerobj == null || layerobj.GetShapeCount() == 0) {
                this.showMessage(errorMsg, "#296692", "#ffffff");
                return;
            }
        }

        this._plotMapWithAddress(places[0].LatLong, places[0].MatchCode, this._jobAddresses[this._curLayerId]);
        places = null;
    },

    _plotMapWithAddress: function(latlonObj, iMatchQuality, addrObj) {
        if (latlonObj != null) {
            //lets examine for which shapelayer this address is meant to be for:
            var layerindex = this._shapeIndexHelper[this._curLayerId];
            var layerobj = this._instance.GetShapeLayerByIndex(layerindex);

            //this._instance.LoadMap(this._latlonObject, this._initialZoomLevel, VEMapStyle.Road, false);
            var newShape = new VEShape(VEShapeType.Pushpin, latlonObj);

            if (addrObj.Desc != null)
                newShape.SetDescription(addrObj.Desc);

            if (addrObj.Title != null)
                newShape.SetTitle(addrObj.Title);


            if (layerobj != null) {
                layerobj.AddShape(newShape);

                this._instance.SetZoomLevel(this._initialZoomLevel);

                if (iMatchQuality != 1)
                    this._showMessage("<strong>Pushpin reflects approximate location<strong>", "#296692", "#ffffff");
            }
            else {
                this._showMessage("No Location(s) found", "#296692", "#ffffff");
            }

        }
        else {
            this._showMessage("No Location(s) found", "#296692", "#ffffff");
        }
    },

    _showMessage: function(message, bgColor, fontColor) {
        var el = null;
        el = $get('myControl');
        if (el != null) {
            el.innerHTML = message;
            el.style.color = fontColor;
            el.style.background = bgColor;
            el.style.visibility = 'visible';
        }
    },

    _hideMessage: function() {
        var el = null;
        el = $get('myControl');
        if (el != null) {
            el.style.visibility = 'hidden';
        }
    },

    _showFullScreenMessage: function(message) {
        var el = $get('myFSControl');
        if (el != null) {
            el.innerHTML = "<strong>" + message + "</strong>";
            el.style.background = "#e7e7e7";
            el.style.color = "#000000"; ;
            el.style.visibility = 'visible';
        }
    },

    _hideFullScreenMessage: function() {
        var el = null;
        el = $get('myFSControl');
        if (el != null) {
            el.style.visibility = 'hidden';
        }
    },


    _setDataFromCoords: function(dataArr) {
        var zindex = 1000 + dataArr.length + 1;

        var colOfShapeArrays = [];
        for (var i = 0; i < this._shapeLayers.length; i++) {
            colOfShapeArrays[this._shapeLayers[i].ShapeLayerId] = [];
        }

        var overlapCount = 0
        var arrSeperations = [8];
        arrSeperations[0] = 0.0005;
        arrSeperations[1] = 0.0005;
        arrSeperations[2] = -0.0005;
        arrSeperations[3] = -0.0005;
        arrSeperations[4] = 0.0005;
        arrSeperations[5] = -0.0005;
        arrSeperations[6] = 0.0005;
        arrSeperations[7] = -0.0005;

        for (var i = 0; i < dataArr.length; i++) {
            var currPushpin = dataArr[i];

            for (var ii = 0; ii < i; ii++) {
                if (currPushpin.Lat == dataArr[ii].Lat && currPushpin.Lon == dataArr[ii].Lon) {
                    //exact same lat/lon... lets seperate them apart a little bit so pins wont get hidden under each other
                    currPushpin.Lat += arrSeperations[overlapCount % 4];
                    currPushpin.Lon += arrSeperations[(overlapCount % 4) + 4];
                    overlapCount++;
                    break;
                }
            }

            var shape = new VEShape(VEShapeType.Pushpin, new VELatLong(currPushpin.Lat, currPushpin.Lon));

            zindex = zindex - 1;
            shape.SetZIndex(zindex, null);
            shape.SetTitle(currPushpin.Title);
            shape.SetDescription(currPushpin.Desc);

            shape.SetCustomIcon("<div class='pin" + currPushpin.ColorValue + "'><div class='pinText'>" + currPushpin.Text + "</div></div>");

            if (currPushpin.ShapeLayerId != null && currPushpin.ShapeLayerId != "") {
                colOfShapeArrays[currPushpin.ShapeLayerId].push(shape);
            }
        }

        if (this._shapeLayers.length > 1) {
            for (var i = 1; i < this._shapeLayers.length; i++) {
                var shapelayer = this._instance.GetShapeLayerByIndex(i);
                if (shapelayer != null) {
                    shapelayer.AddShape(colOfShapeArrays[this._shapeLayers[i].ShapeLayerId]);

                }
            }
        }

    },


    routeCallback: function(route) {

        this._hideFullScreenMessage();

        //calc time
        var travelSeconds = route.Time;
        var days = Math.floor(travelSeconds / (60 * 60 * 24));
        travelSeconds = travelSeconds % (60 * 60 * 24)
        var hours = Math.floor(travelSeconds / (60 * 60));
        travelSeconds = travelSeconds % (60 * 60);
        var minutes = Math.floor(travelSeconds / 60) + 1;

        var totalTravelTimeText = "";
        if (days != 0) {
            if (days > 1)
                totalTravelTimeText += days + " days, ";
            else
                totalTravelTimeText += days + " day, ";
        }
        if (hours != 0) {
            if (hours > 1)
                totalTravelTimeText += hours + " hours, ";
            else
                totalTravelTimeText += hours + " hours, ";
        }
        if (minutes != 0) {
            if (minutes > 1)
                totalTravelTimeText += minutes + " mins";
            else
                totalTravelTimeText += minutes + " min";
        }


        var distContainerId = this._routeDistanceContainerId;
        if (distContainerId != null && $get(distContainerId) != null) {
            var distancetext = "";
            distancetext += route.Distance.toFixed(1) + " miles - " + totalTravelTimeText;
            $get(distContainerId).innerHTML = distancetext;
        }

        var dirContainerId = this._routeDirectionsContainerId;
        if (dirContainerId != null && $get(dirContainerId) != null) {
            //dirContainerId is the div or element the developer has requested
            //us to put the directions for the route in.

            //we make a table containing the directions...

            var tbl = document.createElement("table");
            tbl.className = "wirDirectionsTable"
            tbl.id = "dirtable"

            var tbody = document.createElement("tbody");
            tbl.appendChild(tbody);

            var legs = route.RouteLegs;
            var turns = "Turn-by-Turn Directions\n";
            var leg = null;
            var turnNum = 0;  // The turn #
            var totalDistance = 0;  // The sum of all leg distances



            // Get intermediate legs
            for (var i = 0; i < legs.length; i++) {
                // Get this leg so we don't have to derefernce multiple times
                leg = legs[i];  // Leg is a VERouteLeg object

                // Unroll each intermediate leg
                var turn = null;  // The itinerary leg
                var legDistance = null;  // The distance for this leg
                var warnings = null; //array of warnings for this leg

                for (var j = 0; j < leg.Itinerary.Items.length; j++) {
                    turn = leg.Itinerary.Items[j];
                    warnings = turn.Warnings;
                    legDistance = turn.Distance;
                    totalDistance += legDistance;
                    var tr = document.createElement("tr");
                    var td1 = document.createElement("td");
                    td1.className = "turnNumber";
                    td1.appendChild(document.createTextNode(turnNum + "."));
                    var td2 = document.createElement("td");
                    td2.className = "turnText";
                    td2.appendChild(document.createTextNode(turn.Text));

                    if (warnings != null) {
                        for (var k = 0; k < warnings.length; k++) {
                            var br = document.createElement("br");
                            td2.appendChild(br);
                            var span = document.createElement("span");
                            span.className = "Warning";
                            span.appendChild(document.createTextNode(warnings[k].Text));
                            td2.appendChild(span);
                        }
                    }

                    // turn is a VERouteItineraryItem object
                    var td3 = document.createElement("td");
                    td3.className = "turnDistance";
                    td3.appendChild(document.createTextNode(legDistance.toFixed(1) + " mi"));
                    tr.appendChild(td1);
                    tr.appendChild(td2);
                    tr.appendChild(td3);
                    tbody.appendChild(tr);
                    turnNum++;

                }
            }



            var summaryDiv = document.createElement("div");
            summaryDiv.className = "routeSummary";
            //var strong = document.createElement("strong");
            //var strong2 = document.createElement("strong");
            //summaryDiv.appendChild(strong);
            summaryDiv.appendChild(document.createTextNode(totalDistance.toFixed(1) + " miles"));
            summaryDiv.appendChild(document.createTextNode(" - about "));
            summaryDiv.appendChild(document.createTextNode(totalTravelTimeText));
            //summaryDiv.appendChild(strong2);

            // Show directions
            $get(dirContainerId).appendChild(summaryDiv);
            $get(dirContainerId).appendChild(tbl);


        }
    },

    _adjustMapViewForLayer: function(shapelayerid) {
        if (this._shapeIndexHelper[shapelayerid] != null) {

            if (this._shapeLayers[this._shapeIndexHelper[shapelayerid]].CenterPointLat != -200.0) {
                //we are going to have the map centered according to its layer center points and put a zoom level
                this._instance.SetCenter(new VELatLong(this._shapeLayers[this._shapeIndexHelper[shapelayerid]].CenterPointLat, this._shapeLayers[this._shapeIndexHelper[shapelayerid]].CenterPointLon));
                this._instance.SetZoomLevel(9); //20 mile radius
            }
            else {
                //this is the default mode - we will make sure all pins fit in the view, with no center point
                layer = this._instance.GetShapeLayerByIndex(this._shapeIndexHelper[shapelayerid]);
                rect = layer.GetBoundingRectangle();
                count = layer.GetShapeCount();
                this._instance.SetMapView(rect);

                //alert(this._instance.GetZoomLevel());

                if (this._instance.GetZoomLevel() > 9)
                    this._instance.SetZoomLevel(9);
            }

        }

    },

    _getFullAddressString: function(addrObj) {

        if (addrObj.City == null && addrObj.State == null && addrObj.Zip == null) {
            return "";
        }

        var theAddress = "";
        if (addrObj.Address1 != null && addrObj.Address1.trim() != "") {
            theAddress += addrObj.Address1.trim() + ", ";
            if (addrObj.Address2 != null && addrObj.Address2.trim() != "")
                theAddress += addrObj.Address2.trim() + ", ";
        }

        if (addrObj.City != null && addrObj.City.trim() != "")
            theAddress += addrObj.City.trim() + ", ";

        if (addrObj.State != null && addrObj.State.trim() != "") {
            theAddress += addrObj.State.trim() + ", ";
        }

        if (addrObj.Zip != null && addrObj.Zip.trim() != "")
            theAddress += addrObj.Zip.trim();

        return theAddress;
    },

    // Properties.

    get_class: function() {
        return this._class;
    },

    set_class: function(value) {
        this._class = value;
    },

    get_pushPins: function() {
        return this._pushPins;
    },

    set_pushPins: function(value) {
        this._pushPins = value;
    },

    get_jobAddresses: function() {
        return this._jobAddresses;
    },

    set_jobAddresses: function(value) {
        this._jobAddresses = value;
    },

    get_shapeLayers: function() {
        return this._shapeLayers;
    },

    set_shapeLayers: function(value) {
        this._shapeLayers = value;
    },

    get_routes: function() {
        return this._routes;
    },

    set_routes: function(value) {
        this._routes = value;
    },

    get_routeLayers: function() {
        return this._routeLayers;
    },

    set_routeLayers: function(value) {
        this._routeLayers = value;
    },

    get_LayerInitId: function() {
        return this._LayerInitId;
    },

    set_LayerInitId: function(value) {
        this._LayerInitId = value;
    },

    //get_zoomLevel : function() {
    //    return this._initialZoomLevel;
    //},

    //set_zoomLevel : function(value) {
    //    this._initialZoomLevel = value;
    //},

    get_dashBoardSize: function() {
        return this._initialDashBoardSize;
    },

    set_dashBoardSize: function(value) {
        this._initialDashBoardSize = value;
    },

    get_routeDirectionsContainerId: function() {
        return this._routeDirectionsContainerId;
    },

    set_routeDirectionsContainerId: function(value) {
        this._routeDirectionsContainerId = value;
    },

    get_routeDistanceContainerId: function() {
        return this._routeDistanceContainerId;
    },

    set_routeDistanceContainerId: function(value) {
        this._routeDistanceContainerId = value;
    }

}
// Register the constructor as a client Control.
WirMap.VirtualEarth.registerClass("WirMap.VirtualEarth", Sys.UI.Control); 

Sys.Application.add_init(
  function() {
    if( document.createElement && document.childNodes ) 
    {
        var head = document.getElementsByTagName("head")[0];
        script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = "http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2";
        head.appendChild(script); 
    }
  }
);

if (typeof(Sys) !== "undefined")
  Sys.Application.notifyScriptLoaded();	