// JavaScript Document
 //<![CDATA[
			var map;
			var addressArray = new Array();
			var provinceArray = new Array();
			var cityArray = new Array();
			var markerArray = new Array();
			var matchedAddresses = new Array();
			var matchedPostalCodes = new Array();
			var ROPostalCodes = new Object();
			var selectedProvince;
			var selectedCity;
			var directionsPanel;
			var directions;
			var htmls = [];
  		var to_htmls = [];
  		var from_htmls = [];
  		var gmarkers = [];
  		var mapTxtOutput = new Array(); /* Required for text Map display - RANDY MUNS*/
		var mapTxtOutputOrder;
  		
				
				function fillCity() {
					// On selection of Province this function will work
				var cityName;
				
				removeAllOptions(document.drop_list.city);
				if(cityArray.length) {
				selectedProvince = this.document.getElementById("Province").value;
				addOption(document.drop_list.city, 'City', 'Ville', "");	
				for(arrayIndex=0; arrayIndex<cityArray.length; arrayIndex++){
    				try {
    					mData = cityArray[arrayIndex].split('|');
    					provinceAbbreviation = mData[1];
    					cityName = mData[0];
    					if(provinceAbbreviation == selectedProvince) {
    						addOption(document.drop_list.city, cityName, cityName, "");
							}
    				} catch (E) {
    					// bury error and continue
    				}
    			}
				headingHighlight("city");
				}
				}
				
				function removeAllOptions(selectbox)
				{
					var i;
					for(i=selectbox.options.length-1;i>=0;i--)
					{
						//selectbox.options.remove(i);
						selectbox.remove(i);
					}
				}
				
				function addOption(selectbox, value, text )
				{
					var optn = document.createElement("OPTION");
					optn.text = text;
					optn.value = value;
				
					selectbox.options.add(optn);
				}

		function displayAddresses() {
				//This function will be called when a city and province has been selected. 
				var searchAddress;
				var matchedIndex;
				matchedAddresses.length = 0;
				map.clearOverlays();
				selectedProvince = this.document.getElementById("Province").value;
				selectedCity = 	this.document.getElementById("city").value;
				geocoder = new GClientGeocoder();
				geocoder.setBaseCountryCode("ca");
				
				if(addressArray.length) {
					for (var i=0; i<addressArray.length; i++) {
						var currentLine = addressArray[i];
						if (currentLine.indexOf("|") != -1) {
							var cityName = currentLine.split("|")[0];
							var provinceAbbreviation = currentLine.split("|")[1];
							if (selectedCity == cityName && selectedProvince == provinceAbbreviation) {
								postalCode = currentLine.split("|")[4];
								recordType = currentLine.split("|")[9];
								if(recordType == 2 && ROPostalCodes[postalCode] == 1) {
									//Filter this record out
								} else {
									cityName = currentLine.split("|")[0];
									regionAbbreviation = currentLine.split("|")[1];
									officeName = currentLine.split("|")[2];  
									address = currentLine.split("|")[3];
									phoneNumber = currentLine.split("|")[5]; 
									firstName = currentLine.split("|")[6]; 
									lastName = currentLine.split("|")[7];
									emailAddress = currentLine.split("|")[8];
      						
									searchAddress = address.replace("<br/>", ", ");
									searchAddress =  searchAddress + ", " + cityName + ", " + postalCode + ", " + provinceAbbreviation;
									matchedIndex = matchedAddresses.length; 
									matchedContent = searchAddress + "|" + cityName + "|" + regionAbbreviation + "|" + officeName + "|" + address + "|" + postalCode + "|" + phoneNumber + "|" + firstName + "|" + lastName + "|" + emailAddress + "|";					
									matchedAddresses[matchedIndex] = matchedContent;
									
									geocoder.getLocations(searchAddress, function(response) {
										addToMarkerArray(response);		
									});
								}
							}
						} 
					}
				}
				waitFunction(400);
				if (matchedAddresses.length) {
					for (var i=0; i<matchedAddresses.length; i++) {
						var currentLine = matchedAddresses[i];
						if (currentLine.indexOf("|") != -1) {
							//Also send a request to geocode only the postal code of an address
							cityName = currentLine.split("|")[1];
							provinceAbbreviation = currentLine.split("|")[2];
							officeName = currentLine.split("|")[3];
							address = currentLine.split("|")[4];
							postalCode = currentLine.split("|")[5];
							phoneNumber = currentLine.split("|")[6]; 
							firstName = currentLine.split("|")[7]; 
							lastName = currentLine.split("|")[8]; 
							emailAddress = currentLine.split("|")[9];
							
							
							searchAddress = postalCode + ", " + cityName + ", " + provinceAbbreviation;
							matchedContent = searchAddress + "|" + cityName + "|" + provinceAbbreviation + "|" + officeName + "|" + address + "|" + postalCode + "|" + phoneNumber + "|" + firstName + "|" + lastName + "|" + emailAddress + "|";					
							matchedPostalCodes[i] = matchedContent;
							geocoder.getLocations(searchAddress, function(response) {
								addToMarkerArray(response);		
							});
						}
					}
				}
		}
		
		function waitFunction(millis) {
			var date = new Date();
			var curDate = null;
    
			do { curDate = new Date(); } 
			while(curDate-date < millis);
    
		}
		

		
		function addToMarkerArray(response) {
			
			if(response.Status.code == 200) {
				var originalAddress = response.name;
				place = response.Placemark[0];
				point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
				var mTitle = place.address;
				var marker = new GMarker(point,{mTitle:mTitle});
    		var foundAddressFlag = 0;
    		
    		//Find a match for the record that we have successfully geocoded from the array of all of the addresses
    		for(var i = 0; i<matchedAddresses.length; i++) {
    			var addressToMatch = matchedAddresses[i].split("|")[0];
   			if (originalAddress == addressToMatch) {
   					foundAddressFlag = 1;  //True - match found
    				//Retrieve detailed office information to include in the html info window of the marker
    				if (matchedAddresses[i].split("|")[11] == null) {
    					var infoWindowHtml = matchedAddresses[i].split("|")[4] + "<br/>" + matchedAddresses[i].split("|")[1] + ", " + matchedAddresses[i].split("|")[2];
    					GEvent.addListener(marker, "click", function() {
    					marker.openInfoWindowHtml(infoWindowHtml);
    					});
    					matchedAddresses[i] = matchedAddresses[i] + place.Point.coordinates[1] + "|" + place.Point.coordinates[0] + "|";
    					//map.panTo(point);
							map.setCenter(point,10);
							map.addOverlay(marker);
    				} 
    			}
    		}
				if(!foundAddressFlag) {
					//If detailed address not matched, Check for a match from the array of all of the postal codes
    			for(var i = 0; i<matchedPostalCodes.length; i++) {
    				var addressToMatch = matchedPostalCodes[i].split("|")[0];
   					if (originalAddress == addressToMatch) {
   						//Ensure that the corresponding full address query has not already been successfully geocoded
   						if(matchedAddresses[i].split("|")[11] == null) {
    						//Retrieve detailed office information to include in the html info window of the marker
    						if (matchedPostalCodes[i].split("|")[11] == null) {
    							var infoWindowHtml = matchedPostalCodes[i].split("|")[4] + "<br/>" + matchedPostalCodes[i].split("|")[1] + ", " + matchedPostalCodes[i].split("|")[2];
    							GEvent.addListener(marker, "click", function() {
    							marker.openInfoWindowHtml(infoWindowHtml);
    							});
    							matchedPostalCodes[i] = matchedPostalCodes[i] + place.Point.coordinates[1] + "|" + place.Point.coordinates[0] + "|";
    							//map.panTo(point);
									map.setCenter(point,10);
									map.addOverlay(marker);
    						} 
    					}
						/* clear out contents on new city choice*/
			
    			}
				}				
				
			} 
			mapTxtOutputOrder = "";
			var mapTxtOutput = new Array();
			if(matchedAddresses.length >=0){
			for(var i = 0; i<matchedAddresses.length; i++) {
			
			  /*initiate variables*/
			  var address =matchedAddresses[i].split("|")[4];
			  var phone = matchedAddresses[i].split("|")[6];
			  var area=phone.substring(0,3);
			  var firstPhone=phone.substring(3,6);
			  var secondPhone=phone.substring(6,10);
			  var phoneExt = phone.substring(10);
			  if (phoneExt != ""){
			    phoneExt=" p."+phoneExt;
			  }
			  var firstName=matchedAddresses[i].split("|")[7];
			  var lastName=matchedAddresses[i].split("|")[8];
			  var email=matchedAddresses[i].split("|")[9];
			  var atPos = email.indexOf("@");
			  var emailName = email.substring(0,atPos);
			  var postalCode=matchedAddresses[i].split("|")[5];
			  var firstPostal= postalCode.substring(0,3);
			  var secondPostal = postalCode.substring(3,6);
			  var city=selectedCity;
			  var prov=selectedProvince;
			  
				mapTxtOutput[i] ='<table cellpadding="3" cellspacing="0" width="180"><tr><td colspan="2">'+address+'</td></tr><tr><td colspan="2">'+city+',&nbsp;'+prov+'</td></tr><tr><td colspan="2">'+firstPostal+'&nbsp;'+secondPostal +'</td></tr><tr><td>T&eacute;l&eacute;phone :</td><td>'+'('+area+')&nbsp;'+firstPhone+'-'+secondPhone+phoneExt+'</td></tr>'+'<tr><td>Contact :</td><td><a href="http://www.investorsgroup.com/consult/'+emailName+'/francais/default.htm">'+firstName+'&nbsp;'+lastName+'</a></td></tr><tr><td colspan="2"><a href="mailto:'+email+'">'+email+'</a></td></tr></table><br /><hr />';
				
			}
			for(var i = 0; i<mapTxtOutput.length; i++) {
			
				if (i == 0){
				 mapTxtOutputOrder= mapTxtOutput[0];
				}
				else{
				  var ran_number=Math.floor(Math.random()*100);
				  if (ran_number <= 50){
				  	mapTxtOutputOrder= mapTxtOutputOrder + mapTxtOutput[i];
				  }
				  else{
				  	mapTxtOutputOrder= mapTxtOutput[i]+ mapTxtOutputOrder ;
				  }
				}  
    		 }
			 }
			
			document.getElementById("officeInfo").innerHTML = mapTxtOutputOrder;
			}

		}			
	

    process_addresses = function(documentString) {
    	
    	var infoLine = documentString.split("\n");
    	var currentLine;
    	var cityName;
      var regionAbbreviation;
      var officeName;
      var address;
      var postalCode;
      var phoneNumber;
      var firstName;
      var lastName;
      var emailAddress;
      var recordType;
    	  
			for (var i=0; i<infoLine.length; i++) {
				currentLine = infoLine[i];
				if (currentLine.indexOf("|") != -1) {
					cityName = currentLine.split("|")[0];
					regionAbbreviation = currentLine.split("|")[1];
					officeName = currentLine.split("|")[2];  
					address = currentLine.split("|")[3];
					postalCode = currentLine.split("|")[4];
					phoneNumber = currentLine.split("|")[5]; 
					firstName = currentLine.split("|")[6]; 
					lastName = currentLine.split("|")[7];
					emailAddress = currentLine.split("|")[8];
      		recordType = currentLine.split("|")[9];

      		if (recordType==1) {
      			ROPostalCodes[postalCode] = 1;
      		}      		
      		addressArray.push(cityName + "|" + regionAbbreviation + "|" + officeName + "|" + address + "|" + postalCode + "|" + phoneNumber + "|" + firstName + "|" + lastName + "|" + emailAddress + "|" + recordType + "|");
      	}
      }               
    }                 
                      
    process_provinces = function(documentString) {
    	
    	var infoLine = documentString.split("\n");
    	var currentLine;
    	var provinceName;
    	var provinceFrenchName;
    	var provinceAbbreviation;
    	//provinces.dat file layout
    	//Column 1 - provinceSequenceNumber
    	//Column 2 - provinceName
    	//Column 3 - provinceFrenchName
    	//Column 4 - provinceAbbreviation
    	
			for (var i=0; i<infoLine.length; i++) {
				currentLine = infoLine[i];
				if (currentLine.indexOf("|") != -1) {
					var provinceNumber = currentLine.split("|")[0]; 
					var provinceName = currentLine.split("|")[1]; 
					var provinceFrenchName = currentLine.split("|")[2];
					var provinceAbbreviation = currentLine.split("|")[3];
					provinceArray.push(provinceNumber + "|" + provinceName + "|" + provinceFrenchName + "|" + provinceAbbreviation + "|");
				} 
			}
    }
    
    process_cities = function(documentString) {
    	var infoLine = documentString.split("\n");
    	var currentLine;
    	//cities.dat file layout
    	//Column 1 - CityName
    	//Column 2 - provinceAbbreviation
    	
			for (var i=0; i<infoLine.length; i++) {
				currentLine = infoLine[i];
				if (currentLine.indexOf("|") != -1) {
					var cityName = currentLine.split("|")[0];
					var provinceAbbreviation = currentLine.split("|")[1];
					cityArray.push(cityName + "|" + provinceAbbreviation + "|");
				} 
			}
    }

    function headingHighlight(heading){
				  if (heading == "city"){
					  document.getElementById("cityHeading").style.border="1px solid black";
					  document.getElementById("cityHeading").style.padding="3px";
					  document.getElementById("cityHeading").style.backgroundColor="#fff";
					  document.getElementById("cityHeading").style.color="#000";
					  document.getElementById("provHeading").style.border="none";
					  document.getElementById("provHeading").style.backgroundColor="#99B3CC";
					  document.getElementById("provHeading").style.color="#fff";
				  }
				  if (heading == "province"){
					  document.getElementById("provHeading").style.border="1px solid black";
					  document.getElementById("provHeading").style.padding="3px";
					  document.getElementById("provHeading").style.backgroundColor="#fff";
					  document.getElementById("provHeading").style.color="#000";
					  document.getElementById("cityHeading").style.border="none";
					  document.getElementById("cityHeading").style.backgroundColor="#99B3CC";
					  document.getElementById("cityHeading").style.color="#fff";
				  }
	}
    function loadMarkers() {
			GDownloadUrl("/english/officeLocator/addresses.txt", process_addresses); 
    }
    
    function loadProvinces() {
    	GDownloadUrl("/english/officeLocator/provinces.txt", process_provinces);
    }
    
    function loadCities() {
    	GDownloadUrl("/english/officeLocator/cities.txt", process_cities);
    }
       
    function addMarkers() {
    	if(markerArray.length) {
    		
    		for(arrayIndex=0; arrayIndex<markerArray.length; arrayIndex++){
    			try {
    				var mData = markerArray[arrayIndex].split('|');
    				//var point = new GLatLng(mData[0],mData[1]);
    				//var displayAddress = formatHtmlAddress(arrayIndex);
    				//var marker = createMarker(point, mData[2], displayAddress, arrayIndex);
    				//map.addOverlay(marker);
    			} catch (E) {
    				// bury error and continue
    			}
    		}
    	}
    }
    
    function formatHtmlAddress(arrayIndex) {
    	var mData = markerArray[arrayIndex].split('|');
    	var htmlFormattedAddress = mData[3] + "<br/>" + mData[4] + mData[5] + "<br/>" + mData[6] + "<br/>" + mData[7] + "<br/>" + mData[8];
    	return htmlFormattedAddress;
    }
    
    function createMarker(point, title, html, arrayIndex) {
    	var marker = new GMarker(point,{title:title});
    	
    	GEvent.addListener(marker, "click", function() {
    		marker.openInfoWindowHtml(html);
    	});
    	gmarkers[arrayIndex] = marker;
    	return marker;
    }
    
    
    function centerMap() {
    	map.setCenter(new GLatLng(52.132085,-106.661996), 8);
    
    }
   
    //]]>
