﻿var map = null;
var centerLat = "40.20824570152502";
var centerLong = "-100.61553955078125";
var zoomLevel = "4"; //default
var windowHeight = document.documentElement.clientHeight;
var windowWidth = document.documentElement.clientWidth;
var screenWidth = screen.availWidth;
var screenHeight = screen.availHeight;
var mapLayer = new VEShapeLayer();
var currentActivityLevel = "0";
var currentActivityDesc = "";
var currentView = "";
var pinImagePath = "http://www.ducks.org/migmap/images/markers/";
var isKeyPanning = false;
var maxRecentSearches = 4;
var isAveryMap = false;
//var runClearFormOnLoad = false;

window.onresize = SetMapSize;

function GetMap()
{
    map = new VEMap('migrationMap');
    var style = "a";
    var isFixed = false;
    
    if ((screenWidth > 1500) && (screenHeight > 800))
    {
        zoomLevel = "5";
        
        if ((windowHeight < 800) || (windowWidth < 1500))
        {
            zoomLevel = "4";
        }
    }
    
    obj('form1').txtScreenSize.value = screenWidth + 'W x ' + screenHeight;
    
    SetMapSize();

    HideDashboard();
    
    map.LoadMap(new VELatLong(centerLat, centerLong), zoomLevel, style, isFixed, VEMapMode.Mode2D, false);
    map.AttachEvent("onendzoom", SetBirdsEye);
    map.AttachEvent("onendzoom", SetPushPins);
    map.AttachEvent("onclick", ShowMarker);
    map.AddShapeLayer(mapLayer);
    
    SetBirdsEye();

    AddDots();
    
    ShowHideStates();
        
    obj('recentSearches').innerHTML = ProcessRecentCookie('MIGMAP_RECENT_COOKIE');
}

function SetMapSize()
{
    var height = "";
    var width = "";
    
    if (typeof window.innerWidth != 'undefined')
    {
        width = window.innerWidth,
        height = window.innerHeight
    }

    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

    else if (typeof document.documentElement != 'undefined'
    && typeof document.documentElement.clientWidth !=
    'undefined' && document.documentElement.clientWidth != 0)
    {
        width = document.documentElement.clientWidth,
        height = document.documentElement.clientHeight
    }
    
    var div = obj('migrationMap');

    div.style.width = width;
    div.style.height = height;
}

function ShowMarker(e)
{
    var marker = map.GetShapeByID(e.elementID);
    
    if (marker != null)
    {
        marker.Show();
    }
}

function HideMarker(mapMarker)
{
//    var numLayers = map.GetShapeLayerCount();
//    
//    for(var s=0; s < numLayers; ++s)
//    {
//        mapLayer = map.GetShapeLayerByIndex(s);
//        
//        var numShapes = mapLayer.GetShapeCount();

//        for(var i=0; i < numShapes; ++i)
//        {
//            var shape = mapLayer.GetShapeByIndex(i);
//            
//            if (shape.GetID == mapMarker)
//            {
//                shape.Hide();
//            }
//        }
//    }
}

function SetPushPins()
{
    var zoomLevel = map.GetZoomLevel();
    
    //added to clear red flags on certain zoom out
    if (zoomLevel <= 7)
    {
        ClearRedFlags();
    }
    
    var numLayers = map.GetShapeLayerCount();
    
    for(var s=0; s < numLayers; ++s)
    {
        mapLayer = map.GetShapeLayerByIndex(s);
        
        var numShapes = mapLayer.GetShapeCount();

        for(var i=0; i < numShapes; ++i)
        {
            var shape = mapLayer.GetShapeByIndex(i);
            
            if (shape)
            {
                var url = shape._customIcon;
                            
                for (var k=0; k < 5; ++k)
                {
                    if (url.indexOf('redFlag') == -1)
                    {
                        if ((url.indexOf('/' + k) != -1) || (url.indexOf('/_' + k) != -1))
                        {               
                            if ((zoomLevel >= 0) && (zoomLevel <= 4))
                            {
                                if (url.indexOf('/_' + k) != -1)
                                {
                                    shape.SetCustomIcon(pinImagePath + '_' + k + '_0.gif');
                                }
                                else
                                {
                                    shape.SetCustomIcon(pinImagePath + '_' + k + '_0.gif');
                                }
                            }
                            else if ((zoomLevel >= 5) && (zoomLevel <= 7))
                            {
                                if (url.indexOf('/_' + k) != -1)
                                {
                                    shape.SetCustomIcon(pinImagePath + '_' + k + '_1.gif');
                                }
                                else
                                {
                                    shape.SetCustomIcon(pinImagePath + '_' + k + '_1.gif');
                                }
                            }
                            else if ((zoomLevel >= 8) && (zoomLevel <= 10))
                            {
                                if (url.indexOf('/_' + k) != -1)
                                {
                                    shape.SetCustomIcon(pinImagePath + '_' + k + '_2.gif');
                                }
                                else
                                {
                                    shape.SetCustomIcon(pinImagePath + '_' + k + '_2.gif');
                                }
                            }
                            else if (zoomLevel >= 11)
                            {
                                if (url.indexOf('/_' + k) != -1)
                                {
                                    shape.SetCustomIcon(pinImagePath + '_' + k + '_3.gif');
                                }
                                else
                                {
                                    shape.SetCustomIcon(pinImagePath + '_' + k + '_3.gif');
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}


function HideDashboard()
{
    map.HideDashboard();
}

function SetCenter()
{
    map.SetCenter(new VELatLong(centerLat, centerLong));
}

function ZoomIn()
{
    map.ZoomIn();
    
    SetBirdsEye();

	pageTracker._trackEvent("MigrationMap", "ZoomIn");
}

function ZoomOut()
{
    map.ZoomOut();
    
    SetBirdsEye();

	pageTracker._trackEvent("MigrationMap", "ZoomOut");
}

function SetBirdsEye()
{
    if (map.IsBirdseyeAvailable())
    {
        //obj('birdsEye').innerHTML = "<a href=\"javascript:void(0)\" onclick=\"GetBirdsEye()\"><img alt=\"bird's eye\" src=\"images/birdsEyeOn.gif\" style=\"border:0px\" /></a>";
		obj('birdsEyeOffOverlay').style.visibility = "hidden";
	}
    else
    {
       //obj('birdsEye').innerHTML = "<img alt=\"zoom in for bird's eye\" src=\"images/birdsEyeOff.gif\" style=\"border:0px\" />"; 
		obj('birdsEyeOffOverlay').style.visibility = "visible";
	}
}

function GetBirdsEye()
{
    var be = map.GetBirdseyeScene();
  
    map.SetBirdseyeScene(be.GetID());
    
    obj('birdsEyeControls').style.display = "block";

	pageTracker._trackEvent("MigrationMap", "BirdsEyeView", "BirdseyeScene", be.GetID());
}

function RotateBirdsEye()
{
    var be = map.GetBirdseyeScene();
    
    if (be.GetOrientation() == "North")
    {
        map.SetBirdseyeOrientation(VEOrientation.East);

    }
    else if (be.GetOrientation() == "South")
    {
        map.SetBirdseyeOrientation(VEOrientation.West);
    }
    else if (be.GetOrientation() == "East")
    {
        map.SetBirdseyeOrientation(VEOrientation.South);
    }
    else if (be.GetOrientation() == "West")
    {
        map.SetBirdseyeOrientation(VEOrientation.North);
    }

}

function CloseBirdsEye()
{
    SetMapStyle('a');
    
    obj('birdsEyeControls').style.display = "none";
}

function DoZoom(level)
{
    map.SetZoomLevel(level);
}

function ResetMap()
{
    if ((screenWidth > 1500) && (screenHeight > 800))
    {
        zoomLevel = "5";
        
        if ((windowHeight < 800) || (windowWidth < 1500))
        {
            zoomLevel = "4";
        }
    }
    
    if (obj('birdsEyeControls').style.display = "block")
    {
        obj('birdsEyeControls').style.display = "none"
    }
    
    SetMapStyle('a');
    
    DoZoom(zoomLevel);
    
    SetCenter();
    
    SetBirdsEye();
    
    ClearRedFlags();

	pageTracker._trackEvent("MigrationMap", "ResetMap");

	reloadGoogleAd();
}





function Pan(dir)
{
    if (dir == "north")
    {
        map.Pan(0, 10);
    }
    else if (dir == "south")
    {
        map.Pan (0, -10)
    }
    else if (dir == "east")
    {
        map.Pan(10, 0);
    }
    else if (dir == "west")
    {
        map.Pan(-10, 0);
    }
}

function StartPan(dir)
{
    if (dir == "north")
    {
        map.StartContinuousPan(0, -25);
		pageTracker._trackEvent("MigrationMap", "PanNorth");
    }
    else if (dir == "south")
    {
        map.StartContinuousPan (0, 25)
			pageTracker._trackEvent("MigrationMap", "PanSouth");
    }
    else if (dir == "east")
    {
        map.StartContinuousPan(25, 0);
		pageTracker._trackEvent("MigrationMap", "PanEast");
    }
    else if (dir == "west")
    {
        map.StartContinuousPan(-25, 0);
		pageTracker._trackEvent("MigrationMap", "PanWest");
    }
}

function EndPan()
{
    map.EndContinuousPan();
}

function SetMapStyle(style)
{
    var currentStyle = map.GetMapStyle();
    
    if (style == "a")
    {
        if (currentStyle != VEMapStyle.Aerial)
        {
            map.SetMapStyle(VEMapStyle.Aerial);
        }
		pageTracker._trackEvent("MigrationMap", "AerialView");
    }
    else if (style == "r")
    {
        if (currentStyle != VEMapStyle.Road)
        {
            map.SetMapStyle(VEMapStyle.Road);
        }
		pageTracker._trackEvent("MigrationMap", "RoadView");
    }
    else if (style == "h")
    {
        if (currentStyle != VEMapStyle.Hybrid)
        {
            map.SetMapStyle(VEMapStyle.Hybrid);
        }
		pageTracker._trackEvent("MigrationMap", "HybridView");
    }
    
    if (obj('birdsEyeControls').style.display = "block")
    {
        obj('birdsEyeControls').style.display = "none"
    }

	reloadGoogleAd();
}

function SearchMap(txtSearch)
{
    if (txtSearch != "")
    {
        map.Find(null, txtSearch, null, null, null, null, null, null, false, null, SearchMapCallback);

		pageTracker._trackEvent("MigrationMap", "Search", "Text", txtSearch);

		reloadGoogleAd();
    }
}

function SearchMapCallback(shapeLayer, findResults, places, moreResults, errorMsg)
{
    if (places == null)
    {
        alert("Could not pinpoint a place on the map for your search.");
    }
    else
    {
        SetMapStyle('h');
        
        DoZoom(11);
        
        SetBirdsEye();
        
        var lat = "";
        var lon = "";
        
        if (places[0].MatchConfidence == 0)
        {
            var strSearch = obj('form1').txtSearchMap;
            if (strSearch.value.indexOf('State/Province') == -1)
            {
                CreateRecentCookie(strSearch.value, strSearch.value);
            }
            lat = places[0].LatLong.Latitude;
            lon = places[0].LatLong.Longitude;
        }
        else
        {
            if (places[0])
            {
                CreateRecentCookie(obj('form1').txtSearchMap.value, places[0].Name);
                
                lat = places[0].LatLong.Latitude;
                lon = places[0].LatLong.Longitude;
            }
        }
        
        obj('recentSearches').innerHTML = ProcessRecentCookie('MIGMAP_RECENT_COOKIE');
        
        obj('form1').txtSearchMap.blur();
        
        //see if there is a marker on this long/lat, if not, add a pushpin
        if (lat && lon)
        {
            var doesExist = DoesMarkerExists(lon, lat);
            
            if (!doesExist)
            {   
                var stateProvince = "state";
                var countryID = 1;
                if (places[0].Name.toString().indexOf("Canada") != -1)
                {
                    stateProvince = "province";
                    countryID = 2;
                }
                       
                var marker = new VEShape(VEShapeType.Pushpin, new VELatLong(lat, lon));
                marker.SetCustomIcon('<div style=\'position:relative;bottom:45px;left:6px\'><img src=\'http://www.ducks.org/migmap/images/markers/redFlag.gif\'><br/><br/></div>');
                var desc = "<div class=\"subscribeBox\"><strong>" + places[0].Name + "</strong></div><br/>";
                desc += "<table cellpadding=\"5\">";
				desc += "<tr>";
				desc += "<td>";
				desc += "<img src=\"http://www.ducks.org/migmap/images/detailsBullet.gif\" style=\"border:0px\" />";
				desc += "</td><td>";
				desc += "<a class=\"12\" href=\"javascript:LocationSearchReports('" + lat + "', '" + lon + "', '" + countryID + "')\">View reports near this area</a>";
				desc += "</td>";
				desc += "</tr>";
				if (countryID == 1)
                {
					desc += "<tr><td colspan=\"2\"></td></tr>";
                    desc += "<tr><td><img src=\"http://www.ducks.org/migmap/images/detailsBullet.gif\" style=\"border:0px\" /></td><td><a class=\"12\" href=\"javascript:LocationWeather('" + lat + "','" + lon + "','" + places[0].Name + "')\">View weather for this area</a></td></tr>";
                }
				desc += "<tr><td colspan=\"2\"></td></tr>";
                desc += "<tr><td><img src=\"http://www.ducks.org/migmap/images/detailsBullet.gif\" style=\"border:0px\" /></td><td><a class=\"12\" href=\"javascript:ShowSubscribeForm('" + lat + "', '" + lon + "', '" + places[0].Name + "', '')\">Notify me when reports are submitted</a></td></tr>";
                desc += "</table>";
				desc += "</div>";
				//desc += "<div class=\"subscribeBox\">";
                //desc += "<strong>Notify me</strong> via email when a report is submitted for:<br/><div style=\"margin-top:3px;margin-bottom:3px\"><input checked type=\"radio\" name=\"subsTo\" value=\"city\">this location<br/>";            
                //desc += "<input type=\"radio\" name=\"subsTo\" value=\"state\">anywhere in this " + stateProvince + "</div>Email address:<br/><input class=\"subscribeInput\" type=\"text\" name=\"email\"><br/>";
                //desc += "<input type=\"button\" value=\"subscribe\" onclick=\"alert('subscribe')\" class=\"subscribeButton\">";
                //desc += "</div>";
                
                marker.SetDescription(desc);
                map.AddShape(marker);
            }
        }
    }
}

function CreateRecentCookie(text, value)
{
    var cookie = GetCookie("MIGMAP_RECENT_COOKIE");
    var expDate = new Date();
    expDate.setDate(expDate.getDate() + 365);
    
    if (cookie)
    {
        if (CountChars(cookie, "|") == maxRecentSearches)
        {                         
            cookie = cookie.substring(0, cookie.lastIndexOf("|"));
        }
        
        if (cookie.toString().indexOf(value) == -1)
        {
            DeleteCookie("MIGMAP_RECENT_COOKIE");    
    
            document.cookie = "MIGMAP_RECENT_COOKIE=" + escape(text + "^" + value + "|" + cookie) + ";expires=" + expDate;
        }
    }
    else
    {
        document.cookie = "MIGMAP_RECENT_COOKIE=" + escape(text + "^" + value) + ";expires=" + expDate;
    }
}

function DeleteCookie(cookieName)
{
    var expDate = new Date ( );
    
    expDate.setTime(expDate.getTime() - 1);
    
    document.cookie = cookieName += "=; expires=" + expDate.toGMTString();
    
    obj('recentSearches').innerHTML = "";
}

function GetCookie(cookieName)
{
    var results = document.cookie.match('(^|;) ?' + cookieName + '=([^;]*)(;|$)');

    if (results)
    {
        return (unescape(results[2]));
    }
    else
    {
        return null;
    }
}

function ProcessRecentCookie()
{
    var html = "";
    
    var cookie = GetCookie("MIGMAP_RECENT_COOKIE");
    
    if (cookie)
    {
        cookie = cookie.toString().split('|');
    
        for (var s = 0; s < cookie.length; s++)
        {
            textValue = cookie[s].toString().split('^');
            
            //html += "= <a href=\"javascript:void(0)\" onclick=\"SearchMap('" + cookie[s] + "')\">" + cookie[s] + "</a><br/>"
            html += "= <a href=\"javascript:void(0)\" onclick=\"SearchMap('" + textValue[0] + "')\">" + textValue[1] + "</a><br/>"
        }
        
        html += "+ <a href=\"javascript:void(0)\" onclick=\"DeleteCookie('MIGMAP_RECENT_COOKIE')\">clear list</a><br/>"
    }
    
    return html;
}

function CheckForEnter(e)
{
    if (e)
    {
        e = e;
    }
    else
    {
        e = window.event;
    }
    
    if (e.which)
    {
        var keyCode = e.which;
    }
    else
    {
        var keyCode = e.keyCode;
    }
    
    if (keyCode == 13)
    {
        //SearchMap(document.form1.txtSearchMap.value);
        SearchMap(obj('form1').txtSearchMap.value);
    }
}

function ShowSubmitForm(isResubmit)
{
	pageTracker._trackPageview('/migrationMap/submitReport');
    
    ShowMask();

	MoveMask(0,65);
    
    ClearForm(isResubmit);
    
    currentView = "submitReport";
    
	obj('labelToolbarTitle').innerHTML = "Submit Reports";
    obj('formWrapper').style.display = "block";
    obj('formWrapper').style.width = "800px";
    //obj('formWrapper').style.height = "50px";
    obj('submitForm').style.display = "block";
	obj('helpContent').style.display = "none";
    obj('reportDetails').style.display = "none";
    obj('reportSearchDetails').style.display = "none";
    obj('panelReportStatus').style.display = "none";
    obj('weatherDetails').style.display = "none";
    obj('subscribeForm').style.display = "none";
    obj('panelReportDefault').style.display = "inline";
    obj('verifyLocationStatus').innerHTML = "";
    obj('reportErrors').style.display = "none";

	reloadGoogleAd(); 

}

function ClearForm(isResubmit)
{
    //don't clear some fields if resubmit is true since they are submitting another report and won't have to retype everything again

    if (!isResubmit)
    {
        //its not a resubmit, so clear these
        obj('form1').txtFirstName.value = "";
        obj('form1').txtLastName.value = "";
        obj('form1').txtEmail.value = "";
        obj('form1').chkContact.checked = false;
        obj('form1').txtCity.value = "";
        obj('form1').rbUs.checked = true;
        obj('form1').rbCa.checked = false;
        
        ShowHideStates();
    }
  
    //these get cleared regardless
    obj('form1').ddClass.selectedIndex = 0;
    obj('form1').ddTime.selectedIndex = 0;
    obj('form1').ddWeather.selectedIndex = 0;
    obj('form1').ddTemp.selectedIndex = 0;
    obj('form1').ddWind.selectedIndex = 0;
    obj('form1').ddWindDir.selectedIndex = 0;
    obj('form1').chkNewsletter.checked = false;
    obj('form1').txtComments.value = "";
    obj('form1').ddActivityLevel.selectedIndex = 0;
    //currentActivityLevel = "0";
    //currentActivityDesc = "";
    //obj('activityLabel').innerHTML = "";
    //obj('txtActivityLevel').value = "";
    
    //ClearSelectActivityLevels();
}

//function ClearFormOnLoad()
//{
//    if (runClearFormOnLoad)
//    {
//        ClearForm(true);
//        
//        runClearFormOnLoad = false;
//    }
//}

function HideSubmitForm()
{
    HideMask();
    
    obj('formWrapper').style.display = "none";
}

function ShowNavHelp()
{
    pageTracker._trackPageview('/migrationMap/helpContent');
    
    ShowMask();

	MoveMask(0,65);
   
    currentView = "helpContent";
    
	obj('labelToolbarTitle').innerHTML = "Using the navigation controls";
    obj('formWrapper').style.display = "block";
    obj('formWrapper').style.width = "540px";
    //obj('formWrapper').style.height = "50px";
    obj('helpContent').style.display = "block";
    obj('reportDetails').style.display = "none";
    obj('reportSearchDetails').style.display = "none";
    obj('panelReportStatus').style.display = "none";
    obj('weatherDetails').style.display = "none";
    obj('subscribeForm').style.display = "none";
    obj('panelReportDefault').style.display = "none";
    obj('verifyLocationStatus').innerHTML = "";
    obj('reportErrors').style.display = "none";
}

function ShowMask()
{
    div = obj('modalMask');
	MoveMask(0,0);
    div.style.display = "block";
}

function MoveMask(x,y)
{

    div = obj('modalMask');
	//move
	div.style.top = y + 'px';
    div.style.left = x + 'px';
	//adjust height
	div.style.height = (parseInt(GetClientWidthHeight()[1]) - y) + "px";
}

function MoveObj(o,x,y)
{
	objID = new String(o);
    div = obj(objID);
	//move
	div.style.top = y + 'px';
    div.style.left = x + 'px';

}

function HideMask()
{
    div = obj('modalMask');
    div.style.display = "none";
}

function ShowHideStates()
{
    var label = obj('stateProvinceLabel');
    label.innerHTML = 'State:';

    if (obj('form1').rbUs.checked)
    {
        obj('form1').ddUsStates.style.display = 'inline';
        obj('form1').ddCaProvinces.style.display = 'none';
    }
    
    if (obj('form1').rbCa.checked)
    {
        obj('form1').ddUsStates.style.display = 'none';
        obj('form1').ddCaProvinces.style.display = 'inline';
        label.innerHTML = 'Province:';
    }
    
    obj('form1').ddUsStates.selectedIndex = 0;
    obj('form1').ddCaProvinces.selectedIndex = 0;
}

//function ShowActivityLabel(desc)
//{
//    obj('activityLabel').innerHTML = desc;
//}

//function HideActivityLabel()
//{
//    try
//    {
//        if (currentActivityLevel == '0')
//        {
//            obj('activityLabel').innerHTML = '';
//        }
//        else
//        {
//            obj('activityLabel').innerHTML = currentActivityDesc;
//        }
//    }
//    catch(error)
//    {
//        // just so it doesn't show in the browser if the asp:panel is not visible when this runs
//    }
//}

//function SelectActivity(level, desc)
//{
//    ClearSelectActivityLevels();
//    
//    obj('activity' + level).className = 'activitySelected';
//    
//    isActivitySet = '1';
//    
//    currentActivityDesc = desc;
//    
//    currentActivityLevel = level;
//        
//    obj('txtActivityLevel').value = level;
//}

//function SetActivityLevelOnLoad()
//{
//    try
//    {
//        //used to persist the activity level on the form after a callback   
//        if (currentActivityLevel != "0")
//        {
//            SelectActivity(currentActivityLevel, currentActivityDesc);
//        }
//    }
//    catch(error)
//    {
//        // just so it doesn't show in the browser if the asp:panel is not visible when this runs
//    }
//}

//function ClearSelectActivityLevels()
//{
//    obj('activity1').className = '';
//    obj('activity2').className = '';
//    obj('activity3').className = '';
//    obj('activity4').className = '';
//    obj('activity5').className = '';
//    obj('activity6').className = '';
//    obj('activity7').className = '';
//    obj('activity8').className = '';
//    obj('activity9').className = '';
//}

//function EnableClearForResubmitOnLoad()
//{
//    runClearFormOnLoad = true;
//}

function ShowHideSendToFriend()
{
    div = obj('sendToFriend');
    
    if (div.style.display == "none")
    {
        div.style.display = "block";
    }
    else
    {
        div.style.display = "none";
    }
}

function CountChars(string, charToCount)
{
    cnt = 0;  
    hasChar = true;
    index = 0;
    
    while (hasChar == true)
    {
        index = string.toString().indexOf(charToCount,(index + charToCount.toString().length));
        
        if (index != -1)
        {
            cnt++;
        }
        else
        {
            hasChar = false;
        }
    }
    
    return (cnt + 1);
}

function DoesMarkerExists(lon, lat)
{
    //determines if a pushpin exists for a longitude latitude
    var exists = false;
    
    var numLayers = map.GetShapeLayerCount();
    
    for(var s=0; s < numLayers; ++s)
    {
        mapLayer = map.GetShapeLayerByIndex(s);
        
        var numShapes = mapLayer.GetShapeCount();

        for(var i=0; i < numShapes; ++i)
        {
            var shape = mapLayer.GetShapeByIndex(i);
            
            if ((shape.Latitude == lat) && (shape.Longitude == lon))
            {
                exists = true;
            }
        }
    }
    
    return exists;
}

function ConfirmHideForm()
{
    if (currentView == "submitReport")
    {
        if ((obj('form1').txtFirstName.value != "") || (obj('form1').txtLastName.value != "") || (obj('form1').txtEmail.value != "") ||
        (obj('form1').txtCity.value != "") || (obj('form1').txtComments.value != ""))
        {
            var exit = confirm("You have entered data. Are you sure you wish to close?")
            
            if (exit)
            {
                HideSubmitForm();
            }
        }
        else
        {
            HideSubmitForm();
        }
    }
    else
    {
        HideSubmitForm();
    }

	reloadGoogleAd();
}

function ClearRedFlags()
{
    //there is a bug somewhere in VE that will delete all shapes but the last one added, so we have to run this loop twice - crap
    var numLayers = map.GetShapeLayerCount();
    
    for(var s=0; s < numLayers; ++s)
    {
        mapLayer = map.GetShapeLayerByIndex(s);
        
        var numShapes = mapLayer.GetShapeCount();

        for(var i=0; i < numShapes; ++i)
        {
            var shape = mapLayer.GetShapeByIndex(i);
            
            if (shape)
            {
                var url = shape._customIcon;
                
                if (url.indexOf('redFlag.gif') != -1)
                {
                    mapLayer.DeleteShape(shape);
                }
            }
        }
    }
    
    var numLayers = map.GetShapeLayerCount();
    
    for(var s=0; s < numLayers; ++s)
    {
        mapLayer = map.GetShapeLayerByIndex(s);
        
        var numShapes = mapLayer.GetShapeCount();

        for(var i=0; i < numShapes; ++i)
        {
            var shape = mapLayer.GetShapeByIndex(i);
            
            if (shape)
            {
                var url = shape._customIcon;
                
                if (url.indexOf('redFlag.gif') != -1)
                {
                    mapLayer.DeleteShape(shape);
                }
            }
        }
    }
}

function BackToSearchResults()
{
    //hide report div and show reportsearch div
    obj('reportSearchDetails').style.display = "block";
    obj('reportDetails').style.display = "none";
}

function ShowMapLayer(layerIndex)
{
    if (layerIndex == 99)
    {
        //showing all activity
        var numLayers = map.GetShapeLayerCount();
        
        for(var s=0; s < numLayers; ++s)
        {
            layer = map.GetShapeLayerByIndex(s);
            
            if (!layer.IsVisible())
            {
                layer.Show();
            }
        }
    }
    else
    {
        layerIndex = parseInt(parseInt(layerIndex) + 1);
        
        //hide all layers
        map.HideAllShapeLayers();
        
        var layer = map.GetShapeLayerByIndex(layerIndex);
        
        if (layer != null)
        {
            if (!layer.IsVisible())
            {
                layer.Show();
            }
        }
    }
}

function ShowSubscribeForm(lat, lon, city, state)
{
    ShowMask();

	MoveObj('subscribeForm',0,100);

	MoveMask(0,65);

	reloadGoogleAd();
    
    currentView = "subscribeForm";
    
    obj('formWrapper').style.width = "400px";
    
    obj('formWrapper').style.height = "200px";
    
    obj('formWrapper').style.display = "block";
    
    obj('reportDetails').style.display = "none";

	obj('helpContent').style.display = "none";
    
    obj('reportSearchDetails').style.display = "none";
    
    obj('weatherDetails').style.display = "none";
    
    obj('submitForm').style.display = "none";
    
    obj('subscribeForm').style.display = "block";
    
    obj('labelToolbarTitle').innerHTML = "Subscribe to " + city + ", " + state;
    
    obj('txtVeLat').value = lat;
    
    obj('txtVeLon').value = lon;
    
    pageTracker._trackPageview('/migrationMap/subscribe/');
}

function CancelSubscription()
{
    HideMask();
    
    obj('formWrapper').style.display = "none";
    
    obj('txtVeLat').value = "";
    
    obj('txtVeLon').value = "";
}

function Toggle250()
{
    div125 = obj('adBlock125');
    div250 = obj('adBlock250');

    if (div250.style.display == "none")
    {
        div250.style.display = "inline";
        div125.style.display = "none";
        div125.style.height = "250px";
        div125.style.width = "250px";
    }
    else
    {
        div250.style.display = "none";
        div125.style.display = "inline";
        div125.style.height = "125px";
        div125.style.width = "125px";
    }
}

function SetActivityBlur(value)
{
    if (value == "0")
    {
        obj("ddActivityLevel").selectedIndex = 0;
    }
}

 function DateSelected(sender, eventArgs)
 {
    if (eventArgs.get_newValue())
    {
        var selectedDate = new Date(eventArgs.get_newValue());
        
        if (selectedDate)
        {
            var datePicker = $find("<%= archiveDatePicker.ClientID %>");
            
            var startDate = new Date().setFullYear(2008,7,31);
            var endDate = new Date();
            
            if (selectedDate >= startDate && selectedDate <= endDate)
            {
                var year = selectedDate.getFullYear().toString();
                var day = selectedDate.getDate().toString();
                var month = (selectedDate.getMonth() + 1).toString();
                
                if (day.length == 1)
                {
                    day = "0" + day;
                }
                
                if (month.length == 1)
                {
                    month = "0" + month;
                }
                
                var date = year + month + day;
                
                window.location = "http://www.ducks.org/migrationmap/?date=" + date;
            }
            else if (selectedDate < startDate)
            {
                alert("There is no data prior to September 1, 2008.");
            }
            else if (selectedDate > endDate)
            {
                alert("Please select a date between Sept 1, 2008 and today.");
            }
        }
        else
        {
            alert("Bad date");
        }
    }
 }

//////////////////// 3.0 Additions ///////////////////

 function obj(objID)
 {
	oid = new String(objID);
	return document.getElementById(oid);
 }

function GetScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function GetClientWidthHeight() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
    return [ myWidth, myHeight];
}

function ToggleToobar()
{
	if(obj('toolBarContent').style.display != "none")
	{
		obj('toolBarTogglerImage').src="images/toolbarExpand.jpg";
		if(!isAveryMap){obj('toolBarZip').style.display = "none";}
		obj('toolBarContent').style.display = "none";
		pageTracker._trackEvent("MigrationMap", "ToolbarCollapsed");
	}else{
		obj('toolBarTogglerImage').src="images/toolbarCollapse.jpg";
		if(!isAveryMap){obj('toolBarZip').style.display = "";}
		obj('toolBarContent').style.display = "";
		pageTracker._trackEvent("MigrationMap", "ToolbarExpanded");
	}
}

document.onkeydown = function()
{
	KeyDown(event);
}

document.onkeyup = function()
{
	KeyUp(event);
}

function KeyUp(e)
{
	var pressedKeyCode = CurrentPressedKeyCode(e);
	if(pressedKeyCode == "34") //page down
	{
		ZoomIn();
	}
	if(pressedKeyCode == "33") //page up
	{
		ZoomOut();
	}

	if(isKeyPanning)
	{
		EndPan();
		isKeyPanning = false;
	}
}
function KeyDown(e)
{
	var pressedKeyCode = CurrentPressedKeyCode(e);

	if(pressedKeyCode == "37") //arrow left
	{
		StartPan('west'); isKeyPanning = true;
	}
	if(pressedKeyCode == "38") //arrow up
	{
		StartPan('north'); isKeyPanning = true;
	}
	if(pressedKeyCode == "39") //arrow right
	{
		StartPan('east'); isKeyPanning = true;
	}
	if(pressedKeyCode == "40") //arrow down
	{
		StartPan('south'); isKeyPanning = true;
	}
}
function CurrentPressedKeyCode(e)
{
	var cpkc = e.keyCode? e.keyCode : e.charCode;
	return cpkc;
}