﻿
// Function used for displaying google map on the page
function A2SGMaps( id )
{
    //================================== Global variable declaration starts here ==============================
    
    A2SGMaps.id = id; 
    A2SGMaps.container = null;
    A2SGMaps.map = null;
    A2SGMaps.bounds = null;
    A2SGMaps.geocoder = null;
    A2SGMaps.clusterer = null;
    A2SGMaps.icon = null;
    A2SGMaps.clusterIcon = null;
    A2SGMaps.startAddressIcon = null;
    A2SGMaps.endAddressIcon = null;
    A2SGMaps.loader = null;
    A2SGMaps.recordCount = 0;
    A2SGMaps.dataCounter = 0;
    A2SGMaps.isErrorDislayed = false;    
    A2SGMaps.data = new Array( new Object() );
    A2SGMaps.dataLength = 0;
    
    A2SGMaps.drivingOverlayInst = null;
    A2SGMaps.propertyLocationMarker = null;
    A2SGMaps.drivingVisibility = false;
    A2SGMaps.drivingDivID = null;
    A2SGMaps.loaderTemplate = null;   
    A2SGMaps.drivingStartTemplate = "<table cellpadding=\"2\" cellspacing=\"0\" width=\"100%\" border=\"0\" style=\"border-left:solid 1px grey; border-top:solid 1px grey; color:black;\"><tr><td style=\"border-right:solid 1px grey; background-color:lightgrey; color:black; vertical-align:middle; width:10%\">##GMAPS_FROM_ADDRESS_ICON##</td><td style=\"color:black; vertical-align:middle; word-break: break-all; word-wrap:break-word; width:80%\">##GMAPS_FROM_ADDRESS##</td><td style=\"border-right:solid 1px grey; color:black; width:10%; text-align:right; vertical-align:top;  padding-right:5px; padding-top:5px;\">##GMAPS_DRIVE_REMOVE##</td></tr></table>";    
    A2SGMaps.drivingEndTemplate = "<table cellpadding=\"2\" cellspacing=\"0\" width=\"100%\" border=\"0\" style=\"border-left:solid 1px grey; border-top:solid 1px grey; color:black;\"><tr><td style=\"border-right:solid 1px grey; border-bottom:solid 1px grey; background-color:lightgrey; color:black; vertical-align:middle; width:10%\">##GMAPS_TO_ADDRESS_ICON##</td><td style=\"border-right:solid 1px grey; border-bottom:solid 1px grey; color:black; vertical-align:middle; word-break: break-all; word-wrap:break-word; width:90%\">##GMAPS_TO_ADDRESS##</td></tr></table>";
    A2SGMaps.drivingHeaderTemplate = "<table cellpadding=\"2\" cellspacing=\"0\" width=\"100%\" border=\"0\" style=\"border-left:solid 1px grey; border-top:solid 1px grey; color:black;\"><tr><td style=\"color:black; vertical-align:middle; width:30%\">Driving guidelines:</td><td style=\"border-right:solid 1px grey; color:black; vertical-align:middle; text-align:right; word-break: break-all; word-wrap:break-word; width:70%;\">##GMAPS_DRIVE_INFO_HEADER##</td></tr></table>";
    A2SGMaps.drivingDetailsTemplate = "<table cellpadding=\"2\" cellspacing=\"0\" width=\"100%\" border=\"0\" style=\"color:black;\"><tr><td style=\"border-top:solid 1px grey; background-color:lightgrey; vertical-align:middle; text-align:right; width:10%;\">##GMAPS_DRIVE_NUMBER##</td><td style=\"border-top:solid 1px grey; vertical-align:middle; word-break: break-all; word-wrap:break-word; width:70%\">##GMAPS_DRIVE_INSTRUCTION##</td><td style=\"border-top:solid 1px grey; border-right:solid 1px grey; vertical-align:middle; text-align:right; width:20%\"><b>##GMAPS_DRIVE_DISTANCE##</b></td></tr></table>";
    
    //Variables holds clusterer settings
    A2SGMaps.Clusterer = new Object();    
	
    //Variable holds xml proxy url
    A2SGMaps.xmlProxyUrl = null;
    A2SGMaps.SearchType = "";//kc061908

    //================================== Global variable declaration ends here ===============================
    
    //================================== Global properties declaration ends here =============================
    
    //Set xml proxy url
    this.setXMLProxyUrl = function( url )
    { A2SGMaps.xmlProxyUrl = encodeURI(url); }
    
    //Set geo location data length
    this.setDataLength = function( length )
    { A2SGMaps.dataLength = parseInt(length); }    
    
    // Set google map's loader template
    this.setLoaderTemplate = function( templateHTML )
    { A2SGMaps.loaderTemplate = templateHTML;}
    
    // Set google map's driving direction start address template
    this.setDrivingStartAddressTemplate = function( templateHTML )
    { A2SGMaps.drivingStartTemplate = templateHTML; }

    // Set google map's driving direction end address template
    this.setDrivingEndAddressTemplate = function( templateHTML )
    { A2SGMaps.drivingEndTemplate = templateHTML; }

    // Set google map's driving direction header template
    this.setDrivingHeaderTemplate = function( templateHTML )
    { A2SGMaps.drivingHeaderTemplate = templateHTML; }

    /// Set google map's driving direction details template
    this.setDrivingDetailsTemplate = function( templateHTML )
    { A2SGMaps.drivingDetailsTemplate = templateHTML; }
   
    //================================== Global properties declaration ends here =============================
    
    // Function used to validate the input data
    this.Validate = function()
    {
        try
        {
            if ( ! GBrowserIsCompatible() )
            {
                if ( ! A2SGMaps.isErrorDislayed )
                {
                    throw "Validate:- Sorry, the Google Maps API is not compatible with this browser";
                    A2SGMaps.isErrorDislayed = true;
                }
            }
            else
            {
                if ( ! A2SGMaps.id )
                {
                    if ( ! A2SGMaps.isErrorDislayed )
                    {
                        throw "Please google map container not available.";
                        A2SGMaps.isErrorDislayed = true;
                    }
                }
                else
                    return true;
            }
        }
        catch(e)
        {
            throw "Validate:- : " + e.message;
            A2SGMaps.isErrorDislayed = true;
        }
        return false;
    }

    // Function used for initialise google map object
    this.Init = function()
    {
        try
        {            
            if ( this.Validate() )
            {   
                //Reset google map data variables
                this.Reset();
                
                // Get object of parent container
                A2SGMaps.container = document.getElementById( A2SGMaps.id );

                // Create object of Google Map container division
                A2SGMaps.map = new GMap2( A2SGMaps.container );

                // Create object of google map loader
                var loaderImg = document.createElement("img");
                loaderImg.src = "http://assist2sell.com/App_Themes/UserThemeDefault/Img/indicator.gif";
                
                var loaderSpan = document.createElement("span");
                loaderSpan.innerHTML = "<br/><b><font face='arial' size='2' color='black'>Please wait while loading... </font></b>";
                                
                A2SGMaps.loaderTemplate = document.createElement("div");
                A2SGMaps.loaderTemplate.style.marginTop = "130px";                
                A2SGMaps.loaderTemplate.appendChild(loaderImg);
                A2SGMaps.loaderTemplate.appendChild(loaderSpan);
                A2SGMaps.loader = new this.Loader( A2SGMaps.container, "lightgrey", "black" );

                // Create object of geo bound for centering the map
                A2SGMaps.bounds = new GLatLngBounds();

                // Create object of Geocoder for getting lognitde and latitude based on address
                A2SGMaps.geocoder = new GClientGeocoder();
                
                // Add controls onto map
                A2SGMaps.map.addControl( new GLargeMapControl() );
                A2SGMaps.map.addControl( new GMapTypeControl() );                
                A2SGMaps.map.addControl( new GScaleControl() );                
                A2SGMaps.map.enableContinuousZoom();
                
                // Set default center
                A2SGMaps.map.setCenter( new GLatLng( 36.894181, - 96.43579549999998 ), 3 );

                // Create object of clusterer
                A2SGMaps.clusterer = new Clusterer( A2SGMaps.map );

                // Create object of default icon
                A2SGMaps.icon = new GIcon();
                A2SGMaps.icon.image = "http://assist2sell.com/App_Themes/UserThemeDefault/Img/Icon_HomeRed.gif";
                A2SGMaps.icon.shadow = "http://assist2sell.com/App_Themes/UserThemeDefault/Img/Icon_HomeShadow.gif";
                                                
                // GSize( width, height )
                A2SGMaps.icon.iconSize = new GSize( 15, 18 ); //kc042508, kc090908 bring house bk
                A2SGMaps.icon.shadowSize = new GSize( 22, 20 );
                A2SGMaps.icon.iconAnchor = new GPoint( 6, 20 );
//                A2SGMaps.icon.iconSize = new GSize( 15, 15 ); //kc072908 was 15x20
//                A2SGMaps.icon.shadowSize = new GSize( 18, 20 );
//                A2SGMaps.icon.iconAnchor = new GPoint( 10, 10);
                A2SGMaps.icon.infoWindowAnchor = new GPoint( 8, 8 );

                // Create object of cluster icon
                A2SGMaps.clusterIcon = new GIcon();
                A2SGMaps.clusterIcon.image = "http://assist2sell.com/App_Themes/UserThemeDefault/Img/Icon_HomeBlue.gif";
                A2SGMaps.clusterIcon.shadow = "http://assist2sell.com/App_Themes/UserThemeDefault/Img/Icon_HomeShadow.gif";
                A2SGMaps.clusterIcon.iconSize = new GSize( 25, 30 );
                A2SGMaps.clusterIcon.shadowSize = new GSize( 22, 20 );
                A2SGMaps.clusterIcon.iconAnchor	 = new GPoint( 10, 20 );
                A2SGMaps.clusterIcon.infoWindowAnchor = new GPoint( 5, 1 );

                //Create object of start address icon
                A2SGMaps.startAddressIcon = new GIcon();
                A2SGMaps.startAddressIcon.image = "http://assist2sell.com/App_Themes/UserThemeDefault/Img/Icon_ddStart.gif";
                A2SGMaps.startAddressIcon.shadow = "http://assist2sell.com/App_Themes/UserThemeDefault/Img/Icon_HomeShadow.gif";
                A2SGMaps.startAddressIcon.iconSize = new GSize( 20, 34 );
                A2SGMaps.startAddressIcon.shadowSize = new GSize( 22, 20 );
                A2SGMaps.startAddressIcon.iconAnchor = new GPoint( 9, 34 );
                A2SGMaps.startAddressIcon.infoWindowAnchor = new GPoint( 9, 2 );
                A2SGMaps.startAddressIcon.infoShadowAnchor = new GPoint(18,25);
                
                //Create object of end address icon
                A2SGMaps.endAddressIcon = new GIcon();				 
                A2SGMaps.endAddressIcon.image = "http://assist2sell.com/App_Themes/UserThemeDefault/Img/Icon_ddEnd.gif";
                A2SGMaps.endAddressIcon.shadow = "http://assist2sell.com/App_Themes/UserThemeDefault/Img/Icon_HomeShadow.gif";
                A2SGMaps.endAddressIcon.iconSize = new GSize( 20, 34 );
                A2SGMaps.endAddressIcon.shadowSize = new GSize( 22, 22 );
                A2SGMaps.endAddressIcon.iconAnchor = new GPoint( 9, 34 );
                A2SGMaps.endAddressIcon.infoWindowAnchor = new GPoint( 9, 2 );
                A2SGMaps.endAddressIcon.infoShadowAnchor = new GPoint(18,25);
                
                // Set the clusterer parameters if you dont like the defaults
                A2SGMaps.clusterer.icon = A2SGMaps.clusterIcon;
                A2SGMaps.clusterer.gridSize = 3;
                A2SGMaps.clusterer.SetMaxVisibleMarkers(1);
                A2SGMaps.clusterer.SetMinMarkersPerCluster(2);
                
                // Set callback function after getting geo location
                A2SGMaps.geocoder.getLocations.prototype.thisObj = this;

                // Clear previously generated overlay markers
                this.ClearOverlay();                
            }
        }
        catch(e)
        {
            // Hide loader window
            try
            { A2SGMaps.loader.Hide(); }
			catch(e){}
        }
    }
    
    // Entity function used to store entity information
    A2SGMaps.Entity	 = function()
    {
        this.entID = 0;
        this.entName = "";
        this.entPhoto = "";
        this.entAddress = "";
        this.entCity = "";
        this.entState = "";
        this.entPostalCode = "";
        this.entCountry = "";
        this.entPhone1 = "";
        this.entPhone2 = "";
        this.entFax = "";
        this.entEmail = "";
        this.entWebsite = "";
        this.entInfoWndHTML1 = "";
        this.entInfoWndHTML2 = "";     
        this.entLatitude = 0 ;
        this.entLongitude = 0;
        this.entPoint = 0;        
        this.entMarkerType = "";
        this.entMarker = null;
    }

    // Function used to data field
    this.AddDataFields = function( id, name, photo, address, city, state, postalCode, country, phone1, phone2, fax, email, website, infoWndHTML1, infoWndHTML2, latitude, longitude )
    {
        try
        {
            var objEntityInst = new A2SGMaps.Entity();
            objEntityInst.entID = id;
            objEntityInst.entName = name;
            objEntityInst.entPhoto = photo;
            objEntityInst.entAddress = address;
            objEntityInst.entCity = city;
            objEntityInst.entState = state;
            objEntityInst.entPostalCode = postalCode;
            objEntityInst.entCountry = country;
            objEntityInst.entPhone1 = phone1;
            objEntityInst.entPhone2 = phone2;
            objEntityInst.entFax = fax;
            objEntityInst.entEmail = email;
            objEntityInst.entWebsite = website;
            objEntityInst.entInfoWndHTML1 = infoWndHTML1;
            objEntityInst.entInfoWndHTML2 = infoWndHTML2;            
            objEntityInst.entLatitude = latitude;
	        objEntityInst.entLongitude = longitude;
            A2SGMaps.data[A2SGMaps.recordCount] = objEntityInst;
            
            A2SGMaps.recordCount ++ ;
            objEntityInst = null;
        }
        catch(e)
        {
            // Hide loader window
            try
            { A2SGMaps.loader.Hide(); }
			catch(e){}
        }
    }

	// Function used to show google map loader
    this.Loader = function( container, backgroundColor, borderColor )
    {
        // Set loader's parent element
        this.container = container;
        this.container.style.zIndex = 1;
                       
        //Validate if wrapper already exists or not
        this.wrapperWnd = null;
          
        // Function used to show loader window
        this.Show = function(callBackFnc)
        {   
           try
           {
                if(!this.wrapperWnd)
                {
					//Set callback function while showing the loader
					if( callBackFnc )
						A2SGMaps.loader.callBackFnc = callBackFnc;
	                    
					//Validate if wrapper already exists or not
					this.wrapperWnd = document.getElementById("divGMapWrapper");
	            
					//Validate wrapper window existence
					if(!this.wrapperWnd)
					{
						// Create wrapper on parent element
						this.wrapperWnd = document.createElement( "div" );        
						this.wrapperWnd.id = "divGMapWrapper";
					}
	                
					// Set style information of wraper
					this.wrapperWnd.style.position = "absolute";
					this.wrapperWnd.style.display = "none";
					this.wrapperWnd.style.left = this.container.offsetLeft + "px";
					this.wrapperWnd.style.top = this.container.offsetTop + "px";
					this.wrapperWnd.style.width = (this.container.offsetWidth - 2) + "px";
					this.wrapperWnd.style.height = (this.container.offsetHeight - 2) + "px";
	                
					this.wrapperWnd.style.zIndex = this.container.style.zIndex + 1;
					this.wrapperWnd.style.backgroundColor = backgroundColor;
					this.wrapperWnd.style.opacity = ".75";
					this.wrapperWnd.style.filter = "alpha(opacity=75)";
					this.wrapperWnd.style.border = "solid 1px " + borderColor;
					this.wrapperWnd.style.textAlign = "center";

					// Set inner html of loader
					var loaderTemplate = A2SGMaps.loaderTemplate;
					this.wrapperWnd.appendChild(loaderTemplate);					
	                
					// Show wrapper window
					this.wrapperWnd.style.display = "";
	                
					// Add wrapper into parent element                
					if(A2SGMaps.loader.container.parentNode)
						A2SGMaps.loader.container.parentNode.appendChild( this.wrapperWnd );
				}
            }
            catch(e)
            {
                // Hide loader window
                try
				{ A2SGMaps.loader.Hide(); }
				catch(e){}
            }
        };

        // Function used to set timer for hiding wrapper window
        this.Hide = function(nSeconds)
        {
            try
            {
                if ( this.wrapperWnd )
                {
                    if(nSeconds)
                    {
                        if(isNaN(nSeconds))
                        {
                            //Set timer to hide loader window after user defined time in seconds
                            this.timer = setTimeout(this.TimerHide, nSeconds);
                        }
                        else
                        {
                            //Set timer to hide loader window after 1 seconds
                            this.timer = setTimeout(this.TimerHide, 1000);                    
                        }    
                    }
                    else
                    {
                        //Set timer to hide loader window after 1 seconds
                        this.timer = setTimeout(this.TimerHide, 1000);                    
                    }
                }
            }
            catch(e)
            {
                // Hide loader window
                try
				{ A2SGMaps.loader.Hide(); }
				catch(e){}
            }
        };
        
        // Function used to hide wrapper window
        this.TimerHide = function()
        {               
            try
            {
                if ( A2SGMaps.loader.wrapperWnd )
                {   
                    // Remove wrapper window
                    if(A2SGMaps.loader.container.parentNode)
                    {
                        A2SGMaps.loader.container.parentNode.removeChild(A2SGMaps.loader.wrapperWnd);
                        A2SGMaps.loader.wrapperWnd = null;
                    }
                    
                    // Clear timer interval
                    clearTimeout(A2SGMaps.loader.timer);
                    A2SGMaps.loader.timer = null;                    
                    
                    //Call callback function
                    if(A2SGMaps.loader.callBackFnc)
                        A2SGMaps.loader.callBackFnc();
                }
            }
            catch(e)
            {
                // Hide loader window
                try
				{ A2SGMaps.loader.Hide(); }
				catch(e){}
            }
        }
    }
    
    // Function used to show overlay markers on the map
    this.Render	 = function(sSearchType)
    {
        try
        {
            var iRecCount = A2SGMaps.recordCount;
            var i = 0;
            
            if(A2SGMaps.SearchType == "") A2SGMaps.SearchType = sSearchType;//kc061908
            if ( this.Validate() )
            {
                // Show loader window
                A2SGMaps.loader.Show();
                
                //kc070808 .. big chgs here, elim recursive calls
                for(i=0; i < iRecCount; i++){
                    this.GetGeoLocation( A2SGMaps.dataCounter );
                    A2SGMaps.dataCounter ++
                }

                var nCnt = 0;                    
                var objEntityInst = null;
                                    
                for ( nCnt = 0; nCnt < A2SGMaps.data.length; nCnt++ )
                {                           
                    objEntityInst = A2SGMaps.data[nCnt];                        
                    if(objEntityInst)
                    {
                        if ( objEntityInst.entMarker )
                        {   
                            A2SGMaps.propertyLocationMarker = objEntityInst.entMarker;
                            A2SGMaps.propertyLocationMarker.infoWndHTML = objEntityInst.entMarker.infoWndHTML;
                            A2SGMaps.propertyLocationMarker.hasTabs = objEntityInst.entMarker.hasTabs;
                            
                            A2SGMaps.clusterer.AddMarker( objEntityInst.entMarker, objEntityInst.entAddress );
                            GEvent.addListener( objEntityInst.entMarker, "click", function()
                            {	                                    
                                if (this.infoWndHTML && this.hasTabs)
									this.openInfoWindowTabsHtml(this.infoWndHTML);
								else
									this.openInfoWindowHtml( this.infoWndHTML );
                            });
                         }                            
                    }
                    else
                    {
                        //Remove null data from collection
                        A2SGMaps.data.splice(nCnt, 1);
                        --nCnt;                           
                    }
                 }

                // Center and zoom all geo points
                if(A2SGMaps.SearchType.substr(0,4) == "Sell") this.centerAndZoomOnBoundsFran(A2SGMaps.SearchType);
                else this.centerAndZoomOnBounds();

                // Hide loader window
                try
				{ A2SGMaps.loader.Hide(); }
				catch(e){}

                nCnt = null;
                nLength = null;
                objEntityInst = null;
            }        
        }
        catch(e)
        {
            // Hide loader window
            try
            { A2SGMaps.loader.Hide(); }
			catch(e){}
        }
    }

    // Function used to get geo location
    //kc070808 .. remove recursive calls to parent
    this.GetGeoLocation = function( dataCounter )
    {
       try
       {
            var objEntityInst = A2SGMaps.data[dataCounter];
            
            if(objEntityInst)
            {	                
                if((objEntityInst.entLatitude == "" || parseInt(objEntityInst.entLatitude) == 0) && (objEntityInst.entLongitude == "" || parseInt(objEntityInst.entLongitude) == 0))
                {                
					var geoAddress = objEntityInst.entAddress + ", " + objEntityInst.entCity + ", " + objEntityInst.entState + ", " + objEntityInst.entCountry;
					
					// A function to get geo location from address
					A2SGMaps.geocoder.getLatLng( geoAddress, function( geoPoint )
					{
						if ( geoPoint )
						{
							//Create marker overlay
							A2SGMaps.geocoder.getLocations.prototype.thisObj.createMarkerOverlay(geoPoint, objEntityInst); 
						}

					});
				}
				else
				{
					//Get geo point from Latitude and Longitude available via dupe feed
					var geoPoint = new GLatLng(objEntityInst.entLatitude, objEntityInst.entLongitude);
					//Create marker overlay
					A2SGMaps.geocoder.getLocations.prototype.thisObj.createMarkerOverlay(geoPoint, objEntityInst); 
				}
            }
        }
        catch(e)
        {   
            // Hide loader window
            try
            { A2SGMaps.loader.Hide(); }
			catch(e){}
        }
    }
    
    
    // Function used to create marker overlay
    this.createMarkerOverlay = function(geoPoint, objEntityInst)
    {			
		if(geoPoint && objEntityInst)
		{			
			// Create marker
            		var sTip = "";  //kc072208 nxt 2
		    	sTip = objEntityInst.entAddress + "\n" + objEntityInst.entCity + ", " +  objEntityInst.entState + " " +  objEntityInst.entPostalCode + 
		            "\n " +  objEntityInst.entPhone1 + "\n " +  objEntityInst.entEmail 
			// Create marker
			var geoMarker = new GMarker( geoPoint, {title:objEntityInst.entAddress? sTip : "", icon:A2SGMaps.icon});
					
			// Set data entity values
			objEntityInst.entLatitude = geoPoint.lat();
			objEntityInst.entLongitude = geoPoint.lng();
			objEntityInst.entPoint = geoPoint;
			objEntityInst.entMarker = geoMarker; 
	        
			//Set merker overlay information window variables
			var name = objEntityInst.entName? objEntityInst.entName: "";
			var photo = objEntityInst.entPhoto? objEntityInst.entPhoto: "";
			var address = objEntityInst.entAddress? objEntityInst.entAddress: "";
			var city = objEntityInst.entCity? objEntityInst.entCity: "";
			var state = objEntityInst.entState? objEntityInst.entState: "";
			var postalCode = objEntityInst.entPostalCode? objEntityInst.entPostalCode: "";
			var phone1 = objEntityInst.entPhone1? objEntityInst.entPhone1: "";
			var phone2 = objEntityInst.entPhone2? objEntityInst.entPhone2: "";
			var fax = objEntityInst.entFax? objEntityInst.entFax: "";
			var email = objEntityInst.entEmail? objEntityInst.entEmail: "";
			var website = objEntityInst.entWebsite? objEntityInst.entWebsite: "";
			var photo = objEntityInst.entPhoto? objEntityInst.entPhoto: "";
			var navigationLink = objEntityInst.entNavigationLink? objEntityInst.entNavigationLink: "";
			var infoWndHTML1 = objEntityInst.entInfoWndHTML1? objEntityInst.entInfoWndHTML1: "";
			var infoWndHTML2 = objEntityInst.entInfoWndHTML2? objEntityInst.entInfoWndHTML2: "";
	        
			geoMarker.photo = photo;
			geoMarker.title = address;
	        
			if( infoWndHTML1.len() > 0 && infoWndHTML2.len() > 0)
			{
				objEntityInst.entMarker.infoWndHTML = [new GInfoWindowTab("Property", infoWndHTML1.decodeHTML()), new GInfoWindowTab("Contact", infoWndHTML2.decodeHTML())];
				objEntityInst.entMarker.hasTabs = true;	
			}
			else if(infoWndHTML1.len() > 0 && infoWndHTML2.len() == 0)
			{
				objEntityInst.entMarker.infoWndHTML = infoWndHTML1.decodeHTML();
				objEntityInst.entMarker.hasTabs = false;	
			}
			else
			{								
				infoWndHTML1 = "<font face=arial size=2>";								
				if ( photo.len() > 0 )								
				infoWndHTML1 += "<table><tr><td><img src=\"" + photo + "\"/></td><td>";
				
				if ( name.len() > 0 && address.len() > 0 )
					infoWndHTML1 += "<b>" + name + "</b><br/>";
				else
					infoWndHTML1 += "<b>" + name + "</b>";

				if ( address.len() > 0 && city.len() > 0 )
					infoWndHTML1 += address + "<br/>";
				else
					infoWndHTML1 += address;

				if ( city.len() > 0 && state.len() > 0 )
					infoWndHTML1 += city + ", ";
				else
					infoWndHTML1 += city;

				if ( state.len() > 0 && postalCode.len() > 0 )
					infoWndHTML1 += state + " ";
				else
					infoWndHTML1 += state;

				if ( postalCode.len() > 0 && phone1.len() > 0 )
					infoWndHTML1 += " " + postalCode + "<br/>";

				if ( phone1.len() > 0 && email.len() > 0 )
					infoWndHTML1 += phone1 + "<br/>";
				else
					infoWndHTML1 += phone1;

				if ( email.len() > 0 )
					infoWndHTML1 += "<a href=\"mailto : " + email + "\">" + email + "</a>";
					
				if ( photo.len() > 0 )
					infoWndHTML1 += "</td></tr></table>";
					
				infoWndHTML1 += "</font>"
				objEntityInst.entMarker.infoWndHTML = infoWndHTML1;
				objEntityInst.entMarker.hasTabs = false;
			}			

			// Extend boundary of map for geo point
			A2SGMaps.bounds.extend( geoPoint );

			// Remove memory allocation
			geoMarker = null;
			name = null;
			address = null;
			city = null;
			state = null;
			postalCode = null;
			phone = null;
			email = null;
		}
    }
       
    // Function used to center and zoom map based all geo points
    //kc062408 add this, processing separately for Office srch
    this.centerAndZoomOnBoundsFran = function(sMapGeo)
    {
        var ZoomLvl = 3;
        sMapGeo = sMapGeo.substr(5);
        if(sMapGeo.substring(0,sMapGeo.indexOf(",")) == 1) ZoomLvl = 12 //2 mi zoom lvl
        else if (sMapGeo.substring(0,sMapGeo.indexOf(",")) == 2) ZoomLvl = 9 //20 mi zoom lvl
        else if (sMapGeo.substring(0,sMapGeo.indexOf(",")) == 3) ZoomLvl = 9 //20 mi zoom lvl
        else ZoomLvl = Number(sMapGeo.substring(0,sMapGeo.indexOf(",")))
        
        try
        {
              A2SGMaps.map.setCenter( new GLatLng( sMapGeo.substr(sMapGeo.lastIndexOf(",")+1), sMapGeo.substring(2,sMapGeo.lastIndexOf(","))), ZoomLvl ); //kcTest
        }
        catch(e)
        {
            // Hide loader window
            try
            { A2SGMaps.loader.Hide(); }
			catch(e){}
        }
    }
     
    // Function used to center and zoom map based all geo points
    this.centerAndZoomOnBounds	 = function()
    {
        try
        {
            var geoCenter = A2SGMaps.bounds.getCenter();
            var geoZoom = A2SGMaps.map.getBoundsZoomLevel( A2SGMaps.bounds );
 
            if ( A2SGMaps.map.getZoom() != geoZoom )
                A2SGMaps.map.setCenter( geoCenter, geoZoom );//kc061308
            else
                A2SGMaps.map.panTo( geoCenter );
        }
        catch(e)
        {
            // Hide loader window
            try
            { A2SGMaps.loader.Hide(); }
			catch(e){}
        }
    }
     
    // Function used to store direction overlay information
    A2SGMaps.DirectionOverlay	 = function()
    {           
		this.overlayStartAddress = null;
        this.overlayEndAddress = null;
        this.overlayStartGPoint = null;
        this.overlayStartGMarker = null;
        this.overlayEndGPoint = null;
        this.overlayEndGMarker = null;        
        this.overlayStartIcon = null;
        this.overlayEndIcon = null;
        this.overlayGPloyLine = null;
        this.overlayDirectionType = null;
        
         //Function used to add overlay information
        this.addOverLay = function(startAddress, endAddress, startGeoPoint, endGeoPoint, lineString, directionType)
        {
            var nCnt = null;
            var nLength = null;         
            var gLatLngs = null;
            var directionOverlay = null;
            var overlayLineString = null;
                             
            try
            {                
                //Create direction overlay object instance
                A2SGMaps.drivingOverlayInst = new A2SGMaps.DirectionOverlay();

                //Set direction overlay start and end addresses and geo points                    
                A2SGMaps.drivingOverlayInst.overlayStartAddress = startAddress;
                A2SGMaps.drivingOverlayInst.overlayEndAddress = endAddress;
                A2SGMaps.drivingOverlayInst.overlayStartGPoint = startGeoPoint;
                A2SGMaps.drivingOverlayInst.overlayEndGPoint = endGeoPoint;                        
                A2SGMaps.drivingOverlayInst.overlayStartIcon = A2SGMaps.startAddressIcon;
                A2SGMaps.drivingOverlayInst.overlayEndIcon = A2SGMaps.endAddressIcon;
                A2SGMaps.drivingOverlayInst.overlayDirectionType = directionType;                    
                
                //Create and set start geo marker
                A2SGMaps.drivingOverlayInst.overlayStartGMarker = new GMarker( new GLatLng(A2SGMaps.drivingOverlayInst.overlayStartGPoint.split(",")[0], A2SGMaps.drivingOverlayInst.overlayStartGPoint.split(",")[1]), A2SGMaps.drivingOverlayInst.overlayStartIcon);
                A2SGMaps.drivingOverlayInst.overlayStartGMarker.infoWindowHTML = "<table><tr><td style=\"color:#6600ff; padding:0px 0px 0px 0px; align;left;\"><b>Represents start address location : </b></td></tr><tr><td style=\"padding:0px 0px 0px 0px; align;left;\">" + A2SGMaps.drivingOverlayInst.overlayStartAddress + "</td></tr></table>";
                A2SGMaps.map.addOverlay(A2SGMaps.drivingOverlayInst.overlayStartGMarker);
                GEvent.addListener( A2SGMaps.drivingOverlayInst.overlayStartGMarker, "click", function()
                {
                    this.openInfoWindowHtml( this.infoWindowHTML );
                });
                
                //Create and set end geo marker
                A2SGMaps.drivingOverlayInst.overlayEndGMarker = new GMarker( new GLatLng(A2SGMaps.drivingOverlayInst.overlayEndGPoint.split(",")[0], A2SGMaps.drivingOverlayInst.overlayEndGPoint.split(",")[1]), A2SGMaps.drivingOverlayInst.overlayEndIcon);
                A2SGMaps.drivingOverlayInst.overlayEndGMarker.infoWindowHTML = "<table><tr><td style=\"color:#6600ff; padding:0px 0px 0px 0px; align;left;\"><b>Represents end address location : </b></td></tr><tr><td style=\"padding:0px 0px 0px 0px; align;left;\">" + A2SGMaps.drivingOverlayInst.overlayEndAddress + "</td></tr></table>";
                A2SGMaps.map.addOverlay(A2SGMaps.drivingOverlayInst.overlayEndGMarker);
                GEvent.addListener( A2SGMaps.drivingOverlayInst.overlayEndGMarker, "click", function()
                {
                    this.openInfoWindowHtml( this.infoWindowHTML );
                });

                // Extend boundary of map for geo point
                A2SGMaps.bounds.extend( new GLatLng(A2SGMaps.drivingOverlayInst.overlayStartGPoint.split(",")[0], A2SGMaps.drivingOverlayInst.overlayStartGPoint.split(",")[1]) );
                                
                // Extend boundary of map for geo point
                A2SGMaps.bounds.extend( new GLatLng(A2SGMaps.drivingOverlayInst.overlayEndGPoint.split(",")[0], A2SGMaps.drivingOverlayInst.overlayEndGPoint.split(",")[1]) );
                                                                        
                //Create overlay geo polyline                    
                overlayLineString = new Array( new Object() );
                overlayLineString = lineString.split(" ");
                
                //Draw direction overlay geo polyline
                nCnt = 0;
                nLength = overlayLineString.length;         
                gLatLngs = new Array( new Object() );
                
                for(nCnt=0; nCnt<nLength; nCnt++)
                {         
                    try
                    {
                        if(overlayLineString[nCnt].toString().length > 0)
                            gLatLngs.push(new GLatLng(parseFloat(overlayLineString[nCnt].toString().split(",")[1].toString()), parseFloat(overlayLineString[nCnt].toString().split(",")[0].toString())));
                    }
                    catch(e)
                    {}
                }
                                    
                A2SGMaps.drivingOverlayInst.overlayGPloyLine = new GPolyline(gLatLngs, "#F1012E", 6, 0.6);
                A2SGMaps.map.addOverlay(A2SGMaps.drivingOverlayInst.overlayGPloyLine);
            }
            catch(e)
            {
                // Hide loader window
                try
				{ A2SGMaps.loader.Hide(); }
				catch(e){}
            }
            finally
            {
                 nCnt = null;
                 nLength = null;             
                 gLatLngs = null;                 
                 overlayLineString = null;
            }            
        }
        
        //Function used to remove overlay information
        this.removeOverLay = function(isRestore)
        {	            
            try
            {					
				var drivingDirectionMainDiv = document.getElementById(A2SGMaps.drivingDivID);				
				
				//Remove start geo marker
				A2SGMaps.map.removeOverlay(A2SGMaps.drivingOverlayInst.overlayStartGMarker);
                
                //Remove end geo marker
                A2SGMaps.map.removeOverlay(A2SGMaps.drivingOverlayInst.overlayEndGMarker);
                                                
                //Remove direction overlay geo ployline
                A2SGMaps.map.removeOverlay(A2SGMaps.drivingOverlayInst.overlayGPloyLine);
                
                //Remove driving guidelines division
                if(A2SGMaps.drivingVisibility && drivingDirectionMainDiv)
                {                             
                    if(drivingDirectionMainDiv.childNodes.length > 0)
						drivingDirectionMainDiv.removeChild(drivingDirectionMainDiv.childNodes[0]);
                }
                 
                //Display original icon                       
                if(!isRestore)
                {
					A2SGMaps.map.removeOverlay(A2SGMaps.propertyLocationMarker);
                }
                else
				{
					if(A2SGMaps.propertyLocationMarker)
					{
						A2SGMaps.clusterer.AddMarker( A2SGMaps.propertyLocationMarker, "PropertyLocation" );
                        GEvent.addListener( A2SGMaps.propertyLocationMarker, "click", function()
                        {	                                    
                            if (this.infoWndHTML && this.hasTabs)
								this.openInfoWindowTabsHtml(this.infoWndHTML);
							else
								this.openInfoWindowHtml( this.infoWndHTML );
                        });
					}					
				}                
                                
                // Center and zoom all geo points            
                A2SGMaps.gMapInst.centerAndZoomOnBounds();                  
            }
            catch(e)
            {
                // Hide loader window
                try
				{ A2SGMaps.loader.Hide(); }
				catch(e){}
            }
            finally
            {
                directionOverlay = null;
            }
        }
        
        //Function used to add driving direction information 
        this.addDrivingInstruction = function(xmlDoc, divID)
        {
            var nCnt = null;
            var placemarks = null;
            var nLength = null;
            var tempStr = "";
            var innerHTML = "";
            var tempHTML = "";
            var startGPoint = null;
            var endGPoint = null;            
            var drivingDiv = null;
            var drivingDirectionMainDiv = document.getElementById(A2SGMaps.drivingDivID);           
            
            try
            {
                if(drivingDirectionMainDiv)
                {
                    startGPoint = xmlDoc.getElementsByTagName("startPoint")[0].text;
                    endGPoint = xmlDoc.getElementsByTagName("endPoint")[0].text;
                                        
                    //Get collection of Placemarks
                    nCnt = 0;
                    placemarks = xmlDoc.getElementsByTagName("Placemark");
                    nLength = placemarks.length - 1;
								  
				    //Generate driving direction information start address
                    tempHTML = A2SGMaps.drivingStartTemplate.replace("##GMAPS_FROM_ADDRESS_ICON##", "<img border=\"0\" src=\"http://maps.google.com/mapfiles/dd-start.png\" style=\"border:0px: width:20px; height:34px;\"> Start:");                    
                    tempHTML = tempHTML.replace("##GMAPS_FROM_ADDRESS##", xmlDoc.getElementsByTagName("name")[0].text.split(" to ")[0]);
					 
                    if(A2SGMaps.drivingVisibility)
                        tempHTML = tempHTML.replace("##GMAPS_DRIVE_REMOVE##", "<input type=\"image\" action=\"close\" style=\"border:0px;\" border=\"0\" src=\"http://maps.google.com/mapfiles/close.gif\" width=\"14\" height=\"13\" title=\"Remove this direction\" onclick=\"javascript: A2SGMaps.drivingOverlayInst.removeOverLay(true);\"/>");
                    else
                        tempHTML = tempHTML.replace("##GMAPS_DRIVE_REMOVE##", "<input type=\"image\" action=\"close\" border=\"0\" src=\"http://maps.google.com/mapfiles/close.gif\" width=\"14\" height=\"13\" title=\"Remove this direction\" onclick=\"javascript: A2SGMaps.drivingOverlayInst.removeOverLay(true);\"/>");
                    
                    innerHTML += tempHTML;
                    
                    //Generate driving direction information end address
                    tempHTML = A2SGMaps.drivingEndTemplate.replace("##GMAPS_TO_ADDRESS_ICON##", "<img border=\"0\" src=\"http://maps.google.com/mapfiles/dd-end.png\" style=\"border:0px: width:20px; height:34px;\"> End:");
                    tempHTML = tempHTML.replace("##GMAPS_TO_ADDRESS##", xmlDoc.getElementsByTagName("name")[0].text.split(" to ")[1]);
                    innerHTML += tempHTML;
                    
					//Generate driving direction information header
                    innerHTML += A2SGMaps.drivingHeaderTemplate.replace("##GMAPS_DRIVE_INFO_HEADER##", placemarks[nLength].childNodes[1].text.split("<br/>")[0]);
                     
                    //Generate driving direction information details
                    innerHTML += A2SGMaps.drivingDetailsTemplate.substr(0, A2SGMaps.drivingDetailsTemplate.indexOf("##GMAP_DRIVE_DETAIL_LOOP_START##"));
                    tempStr = A2SGMaps.drivingDetailsTemplate.substr((A2SGMaps.drivingDetailsTemplate.indexOf("##GMAP_DRIVE_DETAIL_LOOP_START##") + String("##GMAP_DRIVE_DETAIL_LOOP_START##").length), (A2SGMaps.drivingDetailsTemplate.indexOf("##GMAP_DRIVE_DETAIL_LOOP_END##") - A2SGMaps.drivingDetailsTemplate.indexOf("##GMAP_DRIVE_DETAIL_LOOP_START##") - String("##GMAP_DRIVE_DETAIL_LOOP_START##").length));
                    for(nCnt=0; nCnt<nLength-1; nCnt++)
                    {
                        tempHTML = tempStr;
                        tempHTML = tempHTML.replace("##GMAPS_DRIVE_NUMBER##", (nCnt+1).toString() + ".");
                        tempHTML = tempHTML.replace("##GMAPS_DRIVE_INSTRUCTION##", placemarks[nCnt].childNodes[0].text);
                        tempHTML = tempHTML.replace("##GMAPS_DRIVE_DISTANCE##", placemarks[nCnt].childNodes[1].text.split("go ")[1].toString());
                        innerHTML += tempHTML;
                    }
                    innerHTML += A2SGMaps.drivingDetailsTemplate.substr(A2SGMaps.drivingDetailsTemplate.indexOf("##GMAP_DRIVE_DETAIL_LOOP_END##") + String("##GMAP_DRIVE_DETAIL_LOOP_END##").length);
                     
                    //Set driving direction information html
                    drivingDiv = document.createElement("div");
                    drivingDiv.id = divID;
                    drivingDiv.style.paddingTop = "10px";
                    drivingDiv.innerHTML = innerHTML;
                    drivingDirectionMainDiv.appendChild(drivingDiv);                    
                }                
            }
            catch(e)
            {
                // Hide loader window
                try
				{ A2SGMaps.loader.Hide(); }
				catch(e){}
            }
            finally
            {
                xmlDoc = null;
                nCnt = null;
                placemarks = null;
                nLength = null;
                tempStr = null;
                innerHTML = null;
                tempHTML = null;
                drivingDiv = null;
                drivingDirectionMainDiv = null;
                startGPoint = null;
                endGPoint = null;
            }
        }
    }
        
//*****  kcTest *****************
//the code below is test code ... to be removed once mapping fully works properly.
//*******************************
    //Function used to create overlay direction request
    A2SGMaps.overlayDirectionKc = function(command, startAddress, endAddress, directionType)
    {         
    
alert("***command is: " + command  + ", startAddr: " + startAddress + ", endAddr: " + endAddress+ ", dirType: " + directionType);    
        var params = {};
        var xmlDoc = null;

        try
        {   
            // Show loader window
            A2SGMaps.loader.Show();                     
                         
            //Create xml request document
            params["Command"] = command;	
            params["startAddress"] = startAddress;
            params["endAddress"] = endAddress;
            params["directionType"] = directionType;
            xmlDoc = A2SGMaps.createXML( "overlayDirection", params);
            
            var gXmlHttpReq = GXmlHttp.create();            
alert("before open, A2SGMaps.xmlProxyUrl is: " + A2SGMaps.xmlProxyUrl);
//kcTest testing below
//             gXmlHttpReq.open("POST", A2SGMaps.xmlProxyUrl, true);
        gXmlHttpReq.open("POST", "http://assist2sell.com/XMLProxyKc.aspx", true);
            gXmlHttpReq.onreadystatechange = function() 
            {
                if (gXmlHttpReq.readyState == 4) 
                {                       
alert("in onreadystatechange");
                    try
                    {                           
                        xmlDoc = gXmlHttpReq.responseXML.documentElement;                        
			            if(xmlDoc)
			            {
                            if(xmlDoc.getElementsByTagName("error").length == 0)
                            {
alert("in error function");
                                //Get google map instance
                                var gMapInst = A2SGMaps.GetMapInstance();                            
                                var nCnt = null;
                                var nLength = null;
                                
                                var startAddress = null;
                                var endAddress = null;
                                var startGeoPoint = null;
                                var endGeoPoint = null;
                                var directionType = null;                        
                                var lineString = "";
                                var lstLineString = null;
                                
                                try
                                {                                       
                                    //Get response data
                                    startAddress = xmlDoc.getElementsByTagName("startAddress")[0].text;
                                    endAddress = xmlDoc.getElementsByTagName("endAddress")[0].text;
                                    startGeoPoint = xmlDoc.getElementsByTagName("startPoint")[0].text;
                                    endGeoPoint = xmlDoc.getElementsByTagName("endPoint")[0].text;
                                    directionType = xmlDoc.getElementsByTagName("directionType")[0].text;
                                    
                                    //Create instance of direction overlay                                    
                                    if(!A2SGMaps.drivingOverlayInst)
										A2SGMaps.drivingOverlayInst = new A2SGMaps.DirectionOverlay();
                                    
									//Clear map
									A2SGMaps.drivingOverlayInst.removeOverLay(false);                                    
                                  
                                    //Get all "ListingString" elements containing road information
                                    lstLineString = new Array( new Object() );                
                                    lstLineString = xmlDoc.getElementsByTagName("LineString");
                                    nLength = lstLineString.length;

                                    for (nCnt=0; nCnt<nLength; nCnt++) 
                                    {   
                                        lineString += " " + lstLineString[nCnt].firstChild.text;                    
                                    } 
                                                
                                    //Add driving direction overlays
                                    A2SGMaps.drivingOverlayInst.addOverLay(startAddress, endAddress, startGeoPoint, endGeoPoint, lineString.substring(1), directionType);
alert("*** below addOverLay... startAddr:" + startAddress  + ", \nendAddr: " + endAddress + ",\n StartGeoPoint: " + startGeoPoint + ", endGeoPoint: " + endGeoPoint + ",\nLineString.substring(1): "); // + lineString.substring(1));
                                    //Display driving information
                                    if(A2SGMaps.drivingVisibility)
                                    {
                                        A2SGMaps.drivingOverlayInst.addDrivingInstruction(xmlDoc, "divDiving");                    
                                    }

                                    // Center and zoom all geo points            
                                    gMapInst.centerAndZoomOnBounds(); 
                                                
                                    //Hide loading wrapper window
                                    A2SGMaps.loader.Hide();                                    
                                }
                                catch(e)
                                {
                                    // Hide loader window
                                    try
									{ A2SGMaps.loader.Hide(); }
									catch(e){}
                                }
                                finally
                                {
                                    nCnt = null;
                                    nLength = null;            
                                    startAddress = null;
                                    endAddress = null;
                                    startGeoPoint = null;
                                    endGeoPoint = null;
                                    directionType = null;                            
                                    lineString = null;
                                    lstLineString = null;                            
                                }
                            }
                            else if(xmlDoc.getElementsByTagName("error").length > 0)
                            {
                                //Show server information message
                                alert(xmlDoc.getElementsByTagName("error")[0].text);
                                
                                // Hide loader window
                                try
								{ A2SGMaps.loader.Hide(); }
								catch(e){}
                            }
                            else
                            {
                                //Show information message
                                alert("Direction information not available.");
                                
                                // Hide loader window
                                try
								{ A2SGMaps.loader.Hide(); }
								catch(e){}
                            }
			            }
			            else
			            {
			                //Show information message
                            alert("Direction information not available.");
                                
			                // Hide loader window
                            try
							{ A2SGMaps.loader.Hide(); }
							catch(e){}   
			            }
                    }
                    catch(e)
                    {
                        //Show information message
                        alert("Direction information not available.");
                        
                        // Hide loader window
                        try
						{ A2SGMaps.loader.Hide(); }
						catch(e){}
                    }
                }
            }
            gXmlHttpReq.send(xmlDoc);                    
        }
        catch(e)
        { 
            // Hide loader window
            try
            { A2SGMaps.loader.Hide(); }
			catch(e){}
        }       
        finally
        {
            params = null;
            xmlDoc = null;            
        }  
    }
      
        
    //Function used to create overlay direction request
    A2SGMaps.overlayDirection = function(command, startAddress, endAddress, directionType)
    {         
        var params = {};
        var xmlDoc = null;

        try
        {   
            // Show loader window
            A2SGMaps.loader.Show();                     
                         
            //Create xml request document
            params["Command"] = command;	
            params["startAddress"] = startAddress;
            params["endAddress"] = endAddress;
            params["directionType"] = directionType;
            xmlDoc = A2SGMaps.createXML( "overlayDirection", params);
            
            var gXmlHttpReq = GXmlHttp.create();            
            gXmlHttpReq.open("POST", A2SGMaps.xmlProxyUrl, true);
            gXmlHttpReq.onreadystatechange = function() 
            {
                if (gXmlHttpReq.readyState == 4) 
                {                       
                    try
                    {                           
                        xmlDoc = gXmlHttpReq.responseXML.documentElement;                        
			            if(xmlDoc)
			            {
                            if(xmlDoc.getElementsByTagName("error").length == 0)
                            {
                                //Get google map instance
                                var gMapInst = A2SGMaps.GetMapInstance();                            
                                var nCnt = null;
                                var nLength = null;
                                
                                var startAddress = null;
                                var endAddress = null;
                                var startGeoPoint = null;
                                var endGeoPoint = null;
                                var directionType = null;                        
                                var lineString = "";
                                var lstLineString = null;
                                
                                try
                                {                                       
                                    //Get response data
                                    startAddress = xmlDoc.getElementsByTagName("startAddress")[0].text;
                                    endAddress = xmlDoc.getElementsByTagName("endAddress")[0].text;
                                    startGeoPoint = xmlDoc.getElementsByTagName("startPoint")[0].text;
                                    endGeoPoint = xmlDoc.getElementsByTagName("endPoint")[0].text;
                                    directionType = xmlDoc.getElementsByTagName("directionType")[0].text;
                                    
                                    //Create instance of direction overlay                                    
                                    if(!A2SGMaps.drivingOverlayInst)
										A2SGMaps.drivingOverlayInst = new A2SGMaps.DirectionOverlay();
                                    
									//Clear map
									A2SGMaps.drivingOverlayInst.removeOverLay(false);                                    
                                  
                                    //Get all "ListingString" elements containing road information
                                    lstLineString = new Array( new Object() );                
                                    lstLineString = xmlDoc.getElementsByTagName("LineString");
                                    nLength = lstLineString.length;

                                    for (nCnt=0; nCnt<nLength; nCnt++) 
                                    {   
                                        lineString += " " + lstLineString[nCnt].firstChild.text;                    
                                    } 
                                                
                                    //Add driving direction overlays
                                    A2SGMaps.drivingOverlayInst.addOverLay(startAddress, endAddress, startGeoPoint, endGeoPoint, lineString.substring(1), directionType);

                                    //Display driving information
                                    if(A2SGMaps.drivingVisibility)
                                    {
                                        A2SGMaps.drivingOverlayInst.addDrivingInstruction(xmlDoc, "divDiving");                    
                                    }

                                    // Center and zoom all geo points            
                                    gMapInst.centerAndZoomOnBounds(); 
                                                
                                    //Hide loading wrapper window
                                    A2SGMaps.loader.Hide();                                    
                                }
                                catch(e)
                                {
                                    // Hide loader window
                                    try
									{ A2SGMaps.loader.Hide(); }
									catch(e){}
                                }
                                finally
                                {
                                    nCnt = null;
                                    nLength = null;            
                                    startAddress = null;
                                    endAddress = null;
                                    startGeoPoint = null;
                                    endGeoPoint = null;
                                    directionType = null;                            
                                    lineString = null;
                                    lstLineString = null;                            
                                }
                            }
                            else if(xmlDoc.getElementsByTagName("error").length > 0)
                            {
                                //Show server information message
                                alert(xmlDoc.getElementsByTagName("error")[0].text);
                                
                                // Hide loader window
                                try
								{ A2SGMaps.loader.Hide(); }
								catch(e){}
                            }
                            else
                            {
                                //Show information message
                                alert("Direction information not available.");
                                
                                // Hide loader window
                                try
								{ A2SGMaps.loader.Hide(); }
								catch(e){}
                            }
			            }
			            else
			            {
			                //Show information message
                            alert("Direction information not available.");
                                
			                // Hide loader window
                            try
							{ A2SGMaps.loader.Hide(); }
							catch(e){}   
			            }
                    }
                    catch(e)
                    {
                        //Show information message
                        alert("Direction information not available.");
                        
                        // Hide loader window
                        try
						{ A2SGMaps.loader.Hide(); }
						catch(e){}
                    }
                }
            }
            gXmlHttpReq.send(xmlDoc);                    
        }
        catch(e)
        { 
            // Hide loader window
            try
            { A2SGMaps.loader.Hide(); }
			catch(e){}
        }       
        finally
        {
            params = null;
            xmlDoc = null;            
        }  
    }
      
      
      
    // Function used to returns an XML string with root node
    A2SGMaps.createXML = function ( root, params )
    {
        var xml = "";        	
        if( root )
            xml += "<" + root + ">";
                    	
        for( param in params )
            xml += "<" + param + ">" + params[param] + "</" + param + ">";

        if( root )
            xml += "</" + root + ">";        	
        return xml;
    }
      
    // Function used for refresh map markers
    this.Reset = function()
    {
        try
        {
            if ( this.Validate() )
            {
               // Clear overlay markers
               this.ClearOverlay();
               
               //Reset data object
               A2SGMaps.data = null;
               A2SGMaps.data = new Array(new Object());              
            }
        }
        catch(e)
        {
            // Hide loader window
            try
            { A2SGMaps.loader.Hide(); }
			catch(e){}
        }
    }

    // Function used for refresh map markers
    this.ClearOverlay = function()
    {
        try
        {
            if ( this.Validate() )
            {
                // Clear overlay markers
                if ( A2SGMaps.clusterer )
                {
                    A2SGMaps.clusterer.ClearCluster( A2SGMaps.clusterer );
                }
            }
        }
        catch(e)
        {
            // Hide loader window
            try
            { A2SGMaps.loader.Hide(); }
			catch(e){}
        }
    }

    // Function used to global variables
    this.Destroy	 = function()
    {
        A2SGMaps.id = null;    
        A2SGMaps.container = null;
        A2SGMaps.map = null;
        A2SGMaps.bounds = null;
        A2SGMaps.geocoder = null;
        A2SGMaps.clusterer = null;
        A2SGMaps.icon = null;
        A2SGMaps.clusterIcon = null;
        A2SGMaps.startAddressIcon = null;
        A2SGMaps.endAddressIcon = null;
        A2SGMaps.loader = null;
        A2SGMaps.loaderTemplate = null;
        A2SGMaps.recordCount = null;
        A2SGMaps.dataCounter = null;
        A2SGMaps.isErrorDislayed = null;
        A2SGMaps.data = null;        
        A2SGMaps.drivingOverlayInst = null;
        A2SGMaps.drivingVisibility = null;
        A2SGMaps.drivingDivID = null;
        A2SGMaps.drivingStartTemplate = null;    
        A2SGMaps.drivingEndTemplate = null;
        A2SGMaps.drivingHeaderTemplate = null;
        A2SGMaps.drivingDetailsTemplate = null;        
        A2SGMaps.xmlHTTPReq = null;
        A2SGMaps.xmlProxyUrl = null;
        
        A2SGMaps.gMapInst = null;
        setCookie("gMapInst", null);
        deleteCookie("gMapInst");   
        
        //Call google map upload event
        GUnload();
    }    
}

//Global variable which holds the google map object instance
A2SGMaps.gMapInst = null;

//Function used to get instance of google map
A2SGMaps.GetMapInstance = function(containerID)
{
    try
    {
        if (!getCookie("gMapInst"))
        {
            A2SGMaps.gMapInst = new A2SGMaps(containerID);
            setCookie("gMapInst", A2SGMaps.gMapInst);                    
        }
        else
        {
            if (!A2SGMaps.gMapInst)
            {
                if (getCookie("gMapInst"))
                {
                    A2SGMaps.gMapInst = getCookie("gMapInst");
                }                
            }
        }
    }
    catch(e)
    {
        // Hide loader window
        try
        { A2SGMaps.loader.Hide(); }
		catch(e){}
    }    
    return A2SGMaps.gMapInst;
}

//Function used to remove the google map object instance
A2SGMaps.RemoveInstance = function()
{
    try
    {	        
        //Remove object from cookie        
        A2SGMaps.GetMapInstance().Destroy();
    }
    catch(e)
    {}
}