windowCount = 0;
regPlanWindow = 0;

var bPublicMap = true;

bExternalUserMap = false;
strSessionID = "";

iServiceSource = 1;

var strServiceLocation = '';
var bMetric = false;

var strFrag = '';

function MapSite() {
	this.base = "";
	this.mapsource = 0;
}

var MapSites = new Array();

for (var i = 0; i < 26; i++) {
	MapSites[i] = new MapSite();
}

MapSites[0].base = "webmap";
MapSites[0].mapsource = 0;

MapSites[1].base = "cityweb";
MapSites[1].mapsource = 1;

MapSites[2].base = "webmap-vm";
MapSites[2].mapsource = 2;

MapSites[3].base = "127";
MapSites[3].mapsource = 3;

MapSites[4].base = "colwebworkshop";
MapSites[4].mapsource = 16;

MapSites[5].base = "intramap";
MapSites[5].mapsource = 1;

MapSites[6].base = "localhost";
MapSites[6].mapsource = 8;

MapSites[7].base = "clintradev";
MapSites[7].mapsource = 12;

MapSites[8].base = "cityconnect";
MapSites[8].mapsource = 15;

MapSites[9].base = "lpsintrawrkshp";
MapSites[9].mapsource = 1;

MapSites[10].base = "lpsweb";
MapSites[10].mapsource = 15;

MapSites[11].base = "192";
MapSites[11].mapsource = 1;

MapSites[12].base = "clwebmapdmz";
MapSites[12].mapsource = 6;

MapSites[13].base = "j1393";
MapSites[13].mapsource = 5;

MapSites[14].base = "v1599";
MapSites[14].mapsource = 13;

MapSites[15].base = "www";
MapSites[15].mapsource = 0;

MapSites[16].base = "clintradev.col";
MapSites[16].mapsource = 12;

MapSites[17].base = "d070848";
MapSites[17].mapsource = 9;

MapSites[18].base = "d070877";
MapSites[18].mapsource = 10;

MapSites[19].base = "10.1.10.219";
MapSites[19].mapsource = 13;

MapSites[20].base = "10";
MapSites[20].mapsource = 1;

MapSites[21].base = "D070877";
MapSites[21].mapsource = 10;

MapSites[22].base = "clintramap";
MapSites[22].mapsource = 15;

MapSites[23].base = "clintradev";
MapSites[23].mapsource = 12;

MapSites[24].base = "cityconnecttest";
MapSites[24].mapsource = 11;

MapSites[24].base = "cityconnectprodntlm";
MapSites[24].mapsource = 15;


var baseURL = "http://webmap.london.ca/mapclient/";
var baseScript = "http://webmap.london.ca/MapServer/default.aspx";

var winURL = new String();	
winURL = window.location.href;

winURL = winURL.toLowerCase();

if (winURL.indexOf('https://') > -1) {
	pos = winURL.indexOf("/", 8);
	strFrag = winURL.slice(8,pos);
} else {
	pos = winURL.indexOf("/", 7);
	strFrag = winURL.slice(7,pos);
}

if (strFrag != "10.1.10.219") {
    pos = strFrag.indexOf(".", 0);
    if (pos > -1) {
	    strFrag = strFrag.slice(0,pos);
    }
}

var bMapService = true;

bAdvancedServer = true;

try {
	if (bAdvancedServer) {
		bMapService = true;
	}
} catch (ex) {

}

var topValue = '0';

for (i = 0; i < MapSites.length; i++) {

	if (strFrag == MapSites[i].base) {
	
		switch(MapSites[i].mapsource) {

		    case 0:

		        if ((winURL.toUpperCase().indexOf('SITEPLAN', 0) > -1) ||
			        (winURL.toUpperCase().indexOf('CADPERMIT', 0) > -1)) {
		            baseURL = "https://www.london.ca/mapclient/";
		            baseScript = "https://www.london.ca/MapServer/default.aspx";
		            bPublicMap = false;
		        } else {
		            baseURL = "http://webmap.london.ca/mapclient/";
		            baseScript = "http://webmap.london.ca/MapServer/default.aspx";
		        }

		        topValue = '25';
		        iServiceSource = 1;

		        break;

		    case 1:

		        baseURL = "http://clintramap/mapclient/";
		        baseScript = "http://clintramap/MapServer/default.aspx";

		        break;
				
			case 2:
				baseURL = "http://webmap-vm/mapclient/";
				baseScript =  "http://webmap-vm/MapServer/default.aspx";

				break;
				
			case 3:
				baseURL = "http://127.0.0.1/mapclient/";
				baseScript =  "http://127.0.0.1/MapServer/default.aspx";
				break;
				
			
			case 7:
				baseURL = "http://www.london.ca/mapclient/";
				baseScript = "http://www.london.ca/MapServer/default.aspx";				
				break;

			case 8:
				baseURL = "http://localhost/mapclient/";
				baseScript =  "http://localhost/MapServer/default.aspx";
				iServiceSource = 1;
				
				break;

			case 9:
				baseURL = "http://D070848/mapclient/";
				baseScript = "http://D070848/MapServer/default.aspx";

				break;
				
			case 10:
			    baseURL = "http://D070877/mapclient/";
			    baseScript = "http://D070877/MapServer/default.aspx";
				iServiceSource = 2;

				break;
				
			case 11:
				baseURL = "http://CityConnectTest/mapclient/";
				baseScript = "http://CityConnectTest/MapServer/default.aspx";
				break;
				
			case 12:
			    if (winURL.indexOf('clintradev.col') > -1) {
				    baseURL = "http://clintradev.col/mapclient/";
				    baseScript = "http://clintradev.col/MapServer/default.aspx";
                } else {
				    baseURL = "http://clintradev/mapclient/";
				    baseScript = "http://clintradev/MapServer/default.aspx";
				}
				break;
				
			case 13:
				baseURL = "http://10.1.10.219/mapclient/";
				baseScript = "http://10.1.10.219/MapServer/default.aspx";
				
				break;
				
            case 14:
				baseURL = "http://www.london.ca/mapclient/";
				baseScript = "http://www.london.ca/MapServer/default.aspx";
				
				break;				
			
            case 15:
				baseURL = "http://clintramap/mapclient/";
				baseScript = "http://clintramap/MapServer/default.aspx";
				break;
				
			case 16:
				baseURL = "http://clintradev/mapclient/";
				baseScript = "http://clintradev/MapServer/default.aspx";
				break;
						
				
			default:
				var baseURL = "http://webmap.london.ca/mapclient/";
				baseScript = "http://webmap.london.ca/MapServer/default.aspx";
				
				topValue = '25';
				break;
		}
		break;	
	}
}

var zoneCode = '';
var authUser = '';
var mapAddress = '';
var planID = '';
var planBitPos = '';
var pollID = '';
var pollName = '';

var iMenuID = 0;
var	iLocID = 0;

function LaunchMap(scriptVersion) {

	try {
	
	    strBase = baseURL.toUpperCase();
	    strFrag = strFrag.toUpperCase();
	    
	    strScript = scriptVersion.toUpperCase();
	        
	    if ((strScript.indexOf('POLICEINTRAMAP') > -1) || (strScript.indexOf('POLICEINTRAMAP_TEST') > -1) || (strScript.indexOf('COLLISIONS') > -1)) {
    		baseScript = baseScript.replace('MapServer', 'PoliceMapServer');
    		baseURL = baseURL.replace('mapclient', 'PoliceMapClient');
        }

        //alert(baseURL);
        
		var newWindow = new Array();

		windowCount++;

		if (scriptVersion == '') {

// Default to public script for Netscape testing

			scriptVersion = 'Public';
		}

		if (document.getElementById) {
			browser = 'W3C';
		
		} else {
		
			window.location.href = 'http://www.london.ca/d.aspx?s=/Maps/HavingProblems.htm';
		}

		var width = screen.width;
		var height = screen.height;
		
		var optWidth = width - 5;
		var optHeight = height - 50;		
	
		if (optWidth > 800) {
			optWidth = 1019;
		}
		
		if (optHeight > 600) {
			optHeight = 705;
		}
		
		var sWidth = screen.width;

		switch (scriptVersion) {
	
			case 'LCC':
				var mapURL = baseURL + "main.asp?Script=" + scriptVersion + "&Browser=" + browser + "&Width=" + sWidth + "&L3=13-14";
				break;

			case 'ZoneSearch':
				var mapURL = baseURL + "main.asp?Script=" + scriptVersion + "&Browser=" + browser + "&Width=" + sWidth + "&L1=114~" + zoneCode;
				break;

			default:
				var mapURL = baseURL + "main.asp?Script=" + scriptVersion + "&Browser=" + browser + "&Width=" + sWidth;
				break;
		}
	
		strReferrer = window.document.location.href;
	
		if (strReferrer != "") {
	
			mapURL = mapURL + "&Referrer=" + strReferrer;
		}
	
		if (authUser != '') {
			authUser = encodeURIComponent(authUser);
			mapURL = mapURL + "&User=" + authUser;
		}
	
		if (mapAddress != '') {
			mapURL = mapURL + "&L3=29-" + mapAddress;
		}

		if (pollID != '') {
			mapURL = mapURL + "&L4=133-" + pollID + "&LABEL4=" + pollName;
		}
	
		if (strSessionID != '') {	
			mapURL = mapURL + "&Session=" + strSessionID;
		}
		
		var strProvider = '';
		if ((iServiceSource == 1) || ((strServiceLocation != '') && (strServiceLocation != 'localhost'))) {
			strProvider = "SVC";
		} else {
			strProvider = "TEST";
		}
		
		mapURL = mapURL + "&Provider=" + strProvider;
		
		if (strServiceLocation != '') {
			mapURL = mapURL + "&Server=" + strServiceLocation;
		}
		
		if (bMetric) {
			mapURL = mapURL + "&K10=1";
		} else {
			mapURL = mapURL + "&K10=0";		
		}

		if (planID != '') {
	
			mapURL = mapURL + "&L3=" + planBitPos + "-" + planID;

		// make sure the registered plans search app re-uses the same map window on every Launch
		
			if (regPlanWindow != 0) {
		
				windowCount--;
		
			} else {
		
				regPlanWindow = windowCount;		
			}
		
			newWindow[regPlanWindow] = window.open(mapURL,"CityMap"+regPlanWindow,"width=" + optWidth + ",height=" + optHeight + ",left=0,top='+topValue+',screenX=0,screenY='+topValue+',width="+ optWidth + "height=" + optHeight + ",resizable");
		
		} else if ((scriptVersion == "PropertyInfo") || (scriptVersion == "PropertyInfoPublic") || (bExternalUserMap)) {
		
			window.location.href = mapURL;
			window.resizeTo(optWidth, optHeight);
		
		} else {
		
			if (((iMenuID > 0) && (iLocID > 0)) || ((iMenuID != '') && (iLocID != ''))) {
				mapURL = mapURL + "&L3=" + iMenuID + "-" + iLocID;
			}
			
			//alert(mapURL);
	
			newWindow[windowCount] = window.open(mapURL,"CityMap"+windowCount,"width=" + optWidth + ",height=" + optHeight + ",left=0,top='+topValue+',screenX=0,screenY='+topValue+',resizable,status=no");
			newWindow[windowCount].focus();
		}
	
	} catch (error) {
	
		alert(error.message);
	
		window.location.href = 'http://www.london.ca/d.aspx?s=/Maps/HavingProblems.htm';
	}
}

function LaunchMapAuthenticated(scriptVersion, userID){
	authUser = userID;
	LaunchMap(scriptVersion);
}

function LaunchZoningMap(codeStr) {

	zoneCode = codeStr;
	LaunchMap('ZoneSearch');

}

function LaunchMapAddress(scriptVersion, inputAddress) {

	mapAddress = inputAddress.replace(' ', '%20');
	LaunchMap(scriptVersion);
}

function LaunchMapRegPlans(drawingID) {

    try {
	
	    var winOpener = window.opener;
	    
	    try {
	        if (winOpener.name == 'MasterPlanWindow') {
	            winOpener = window.opener.opener;
	        }
	        winOpener.top.bShowFullDrawing = true;
    	    winOpener.document.Form1.V1.checked = true;
	    } catch (ex) {
	        winOpener = null;    
	    }
    	    
	    planID = drawingID;
    	
	    if (drawingID.substring(0,2) == 'RP') {
	        planBitPos = 129;	    
	        if (winOpener) {
	            if (!winOpener.closed) {
	                if (winOpener.top) {
	                    winOpener.top.L3 = planBitPos + '-' + drawingID;
	                    winOpener.top.FindDrawing(drawingID);
	                } else {
    	                LaunchMap('RegPlanSearch');	        
	                }
	            } else {
	                LaunchMap('RegPlanSearch');
	            }
	        } else {
	            LaunchMap('RegPlanSearch');
	        }
	    } else {
		    planBitPos = 70;	
	        if (winOpener) {
                if (!winOpener.closed) {
	                if (winOpener.top) {
    	                winOpener.top.L3 = planBitPos + '-' + drawingID;
                        winOpener.top.FindDrawing(drawingID);
                    } else {
    	                LaunchMap('MasterPlans');            
                    }
                } else {
    	            LaunchMap('MasterPlans');
                }
		    } else {
	            LaunchMap('MasterPlans');
	        }
	    }
	} catch (ex) {
	    alert('LaunchMapRegPlans: ' + ex.message);
	}
}

function LaunchMapDrawing(drawingID) {
	planID = drawingID;
	planBitPos = 70;
	LaunchMap('MasterPlans');
}

function LaunchElectionMap(inputAddress, inputPollID, inputName) {
	
	mapAddress = inputAddress;
	pollID = inputPollID;
	pollName = inputName;

	LaunchMap('Election');
	
}

function LaunchThinElectionMap(tmpAddress, inputPollID, inputName) {

    scriptVersion = "Election";
	var address = new String('');
	
	address = tmpAddress.replace(' ', '%20');
	pollName = inputName.replace(' ', '%20');
	newWindow = window.open("","CityMap","width=600,height=500,left=0,top=0,screenX=0,screenY=0,outerWidth=600,outerHeight=500,resizable,menubar=yes");
	newWindow.location = baseScript + '?name=WebMap&M=9&K8=' + iServiceSource + '&U=1&K9=0&L3=29-'+address+'&L4=133-' + inputPollID + '&LABEL4=' + pollName + '&Z2=7&O='+scriptVersion+'&K5='+scriptVersion;

}

function LaunchExternalUser(scriptVersion, sessionID) {

	bExternalUserMap = true;
	strSessionID = sessionID;
	LaunchMap(scriptVersion);
}

function LaunchLocation(scriptVersion, menuID, locID) {

	iMenuID = menuID;
	iLocID = locID;
	LaunchMap(scriptVersion);
}

function LaunchThinAddress(scriptVersion, tmpAddress, client) {

	var address = new String('');
	
	address = tmpAddress.replace(' ', '%20');

	newWindow = window.open("","CityMap","width=600,height=420,left=0,top=0,screenX=0,screenY=0,outerWidth=600,outerHeight=420,resizable,menubar=yes");
	
	newWindow.location = baseScript + '?name=WebMap&M=9&K8=' + iServiceSource + '&U=1&K9=' + client + '&L3=29-'+address+'&Z2=5&O='+scriptVersion+'&K5='+scriptVersion;

}

function LaunchThinLocation(scriptVersion, tmpLocation, client) {

	var strLocation = new String('');
	
	strLocation = tmpLocation.replace(' ', '%20');

	newWindow = window.open("","CityMap","width=600,height=420,left=0,top=0,screenX=0,screenY=0,outerWidth=600,outerHeight=420,resizable,menubar=yes");
	
	tmpScript = scriptVersion.toUpperCase();
	
	if (tmpScript.indexOf('METHANE') > -1) {
		newWindow.location = baseScript + '?name=WebMap&M=9&U=3&K9=' + client + '&L3='+strLocation+'&Z2=7&O='+scriptVersion+'&K5='+scriptVersion;
	} else {
		newWindow.location = baseScript + '?name=WebMap&M=9&K8=' + iServiceSource + '&U=3&K9=' + client + '&L3='+strLocation+'&Z2=5&O='+scriptVersion+'&K5='+scriptVersion;
	}	
}

function LaunchThinLocationLabel(scriptVersion, tmpLocation, client, strLabel) {

	strLabel = strLabel.replace('&', '~&');
	strLabel = strLabel.replace('#', '~#');
	
	strLabel = escape(strLabel);
	var strLocation = new String('');
	
	strLocation = tmpLocation.replace(' ', '%20');

	newWindow = window.open("","CityMap","width=600,height=420,left=0,top=0,screenX=0,screenY=0,outerWidth=600,outerHeight=420,resizable,menubar=yes");
	
	tmpScript = scriptVersion.toUpperCase();
	
	newWindow.location = baseScript + '?name=WebMap&M=9&K8=' + iServiceSource + '&U=3&K9=' + client + '&L3='+strLocation+'&Z2=5&O='+scriptVersion+'&K5='+scriptVersion + '&LABEL3=' + strLabel;
}

function LaunchThinGifOnly(scriptVersion, tmpLocation, iMapScope, gWidth, gHeight) {

	var strLocation = new String('');
	
	strLocation = tmpLocation.replace(' ', '%20');

	newWindow = window.open("","CityMap","width=600,height=420,left=0,top=0,screenX=0,screenY=0,outerWidth=" + gWidth + ",outerHeight=" + gHeight + ",resizable,menubar=yes");
	
	tmpScript = scriptVersion.toUpperCase();
	
	var strURL = baseScript + '?name=WebMap&M=16&K8=' + iServiceSource + '&U=3&K9=' + iMapScope + '&L3='+strLocation+'&Z2=5&O='+scriptVersion+'&K5='+scriptVersion + '&H=' + gWidth + '&V=' + gHeight;
	alert(strURL);
	newWindow.location = strURL;
	
}

function LaunchGifRedirect(scriptVersion, tmpLocation, iMapScope, gWidth, gHeight) {

	var strLocation = new String('');
	
	strLocation = tmpLocation.replace(' ', '%20');

	newWindow = window.open("","CityMap","width=600,height=420,left=0,top=0,screenX=0,screenY=0,outerWidth=" + gWidth + ",outerHeight=" + gHeight + ",resizable,menubar=yes");
	
	tmpScript = scriptVersion.toUpperCase();
	
	var strURL = baseScript + '?name=WebMap&M=21&K8=' + iServiceSource + '&U=3&K9=' + iMapScope + '&L3='+strLocation+'&Z2=5&O='+scriptVersion+'&K5='+scriptVersion + '&H=' + gWidth + '&V=' + gHeight;
	newWindow.location = strURL;
	
}

function LaunchSitePlans(scriptVersion, inputAddress, strServer, bInMetric) {

	bMetric = bInMetric;
	strServiceLocation = strServer;
	mapAddress = inputAddress.replace(' ', '%20');
	LaunchMap(scriptVersion);
}

function LaunchSewerSearchMap(strGuid) {
    windowCount = 0;
	LaunchLocation('SewerSleuth', 220, strGuid);
}

