var myDirectory = "http://" + document.domain;
var myBizDirectory = myDirectory + '/green-events';

var javascriptReady = false;

function countryChanged()
{	
   xmlHttp=GetXmlHttpObject()
   
   if (xmlHttp==null)
   {
       alert ("Browser does not support HTTP Request");
       return;
   }

   var countryID =  document.getElementById("COUNTRY_ID");
   var cityName =  document.getElementById("CITY_NAME");
   
   cityName.value = '';

   url=myDirectory+"/scripts/state-list.cfm?COUNTRY_ID="+countryID.value;
   url=url+"&FIELD_SIZE=text-small&MYFUNCTION=stateChanged()&SHOW_ANY=1&sid="+Math.random();
   xmlHttp.open("GET",url,false) ;
   xmlHttp.send(null);

   if (xmlHttp.responseText != '')
   {
       document.getElementById("MY_AVAILABLE_STATES").innerHTML= xmlHttp.responseText;
   }
  
   var num = countryID.selectedIndex;
   var locName = countryID.options[num].text;    
  
   url=myBizDirectory+"/map-search-coords.cfm?LOC_TYPE=COUNTRY&LOC_NAME="+locName;
   url=url+"&sid="+Math.random();
   xmlHttp.open("GET",url,false) ;
   xmlHttp.send(null);

   var vars = xmlHttp.responseText.split("&");
  
   var latitude = -1;
   var longitude = -1;
  
   for (var i=0;i<vars.length;i++) 
   {
   	   var pair = vars[i].split("=");
    
	   if (pair[0] == "latitude") 
	   {
           latitude = pair[1];
       }
	   else if (pair[0] == "longitude")
	   {
		   longitude = pair[1];
	   }
   }
   
   if (latitude != -1 && longitude != -1 && javascriptReady == true)
   {
	   thisMovie("myFlashMap").updateLatitude(latitude);
	   thisMovie("myFlashMap").updateLongitude(longitude);
	   thisMovie("myFlashMap").updateMapCoords();
	   thisMovie("myFlashMap").updateZoom(4);	
   }
  
   updateMap();
}

function stateChanged()
{	
   xmlHttp=GetXmlHttpObject()
   
   if (xmlHttp==null)
   {
       alert ("Browser does not support HTTP Request");
       return;
   }

   var countryID = document.getElementById("COUNTRY_ID");
   var stateID =  document.getElementById("STATE_ID");
   var cityName =  document.getElementById("CITY_NAME");
   
   cityName.value = '';

   var num = countryID.selectedIndex;
   var countryName = countryID.options[num].text;  

   var num = stateID.selectedIndex;
   var stateName = stateID.options[num].text;   

   url=myBizDirectory+"/map-search-coords.cfm?LOC_TYPE=STATE&LOC_NAME="+stateName;
   url=url+"&COUNTRY="+countryName+"&sid="+Math.random();
   xmlHttp.open("GET",url,false) ;
   xmlHttp.send(null);

   var vars = xmlHttp.responseText.split("&");
  
   var latitude = -1;
   var longitude = -1;
  
   for (var i=0;i<vars.length;i++) 
   {
   	   var pair = vars[i].split("=");
    
	   if (pair[0] == "latitude") 
	   {
           latitude = pair[1];
       }
	   else if (pair[0] == "longitude")
	   {
		   longitude = pair[1];
	   }
   }
   
   if (latitude != -1 && longitude != -1 && javascriptReady == true)
   {
	   thisMovie("myFlashMap").updateLatitude(latitude);
	   thisMovie("myFlashMap").updateLongitude(longitude);
	   thisMovie("myFlashMap").updateMapCoords();
	   thisMovie("myFlashMap").updateZoom(5);	 
   }
   
   updateMap();
}

function cityChanged2()
{
	setTimeout("cityChanged()",1250);
	document.getElementById("CITY_NAME").focus();
}

function cityChanged()
{	
   xmlHttp=GetXmlHttpObject()
   
   if (xmlHttp==null)
   {
       alert ("Browser does not support HTTP Request");
       return;
   }

   var countryID = document.getElementById("COUNTRY_ID");
   var stateID =  document.getElementById("STATE_ID");
   var cityName =  document.getElementById("CITY_NAME");

   var num = countryID.selectedIndex;
   var countryName = countryID.options[num].text;  

   var num = stateID.selectedIndex;
   var stateName = stateID.options[num].text; 

   if (stateName == "Any")
   {
       stateName = "";   
   }

   url=myBizDirectory+"/map-search-coords.cfm?LOC_TYPE=CITY&LOC_NAME="+cityName.value;
   url=url+"&COUNTRY="+countryName+"&STATE="+stateName+"&sid="+Math.random();
   xmlHttp.open("GET",url,false) ;
   xmlHttp.send(null);

   var vars = xmlHttp.responseText.split("&");
  
   var latitude = -1;
   var longitude = -1;
  
   for (var i=0;i<vars.length;i++) 
   {
   	   var pair = vars[i].split("=");
    
	   if (pair[0] == "latitude") 
	   {
           latitude = pair[1];
       }
	   else if (pair[0] == "longitude")
	   {
		   longitude = pair[1];
	   }
   }

   if (latitude != -1 && longitude != -1 && javascriptReady == true)
   {
	   thisMovie("myFlashMap").updateLatitude(latitude);
	   thisMovie("myFlashMap").updateLongitude(longitude);
	   thisMovie("myFlashMap").updateMapCoords();
	   thisMovie("myFlashMap").updateZoom(10);	   
   }
   
   updateMap();
}

function updateMap()
{
	var myCountryID = document.getElementById("COUNTRY_ID");
	var myStateID = document.getElementById("STATE_ID");
	var myCityName = document.getElementById("CITY_NAME");
	var myMiles = document.getElementById("MILES");
	var myMaxMarkers = document.getElementById("MARKERS");
	var myMapProvider = document.getElementById("MAP_PROVIDER");

	if (javascriptReady == true)
    {
	   thisMovie("myFlashMap").updateCountry(myCountryID.value);
	   thisMovie("myFlashMap").updateState(myStateID.value);
	   thisMovie("myFlashMap").updateCity(myCityName.value);
	   thisMovie("myFlashMap").updateMiles(myMiles.value);
	   thisMovie("myFlashMap").updateMaxMarkers(myMaxMarkers.value);
	   thisMovie("myFlashMap").updateMapProvider(myMapProvider.value);
	   thisMovie("myFlashMap").updateMyMap();
    }   
}

function thisMovie(movieName) 
{
   if (navigator.appName.indexOf("Microsoft") != -1) 
   {
      return window[movieName]
   }
   else 
   {
     return document[movieName]
   }
}

function isReady()
{
    return javascriptReady;
}

function initJavascript()
{
	javascriptReady = true;	
}

function updateIFrame(myURL)
{
	var myIFrame = document.getElementById("MAP_EVENTS");
	myIFrame.src = myURL;
}

var savedStates=new Array();
var savedStateCount=0;

function saveBackgroundStyle(myElement)
{
	saved=new Object();
	saved.element=myElement;
	saved.className=myElement.className;
	saved.backgroundColor=myElement.style["backgroundColor"];
	return saved;   
}

function restoreBackgroundStyle(savedState)
{
	savedState.element.style["backgroundColor"]=savedState.backgroundColor;
	
	if (savedState.className)
	{
		savedState.element.className=savedState.className;    
	}
}

function findNode(startingNode, tagName)
{
	myElement=startingNode;
	
	var i=0;
	
	while (myElement && (!myElement.tagName || (myElement.tagName && myElement.tagName!=tagName)))
	{
		myElement=startingNode.childNodes[i];
		i++;
	}  
  
	if (myElement && myElement.tagName && myElement.tagName==tagName)
	{
		return myElement;
	}
	else if (startingNode.firstChild)
	{
		return findNode(startingNode.firstChild, tagName);
	}
	else
	{
  		return 0;
	}
}

function trackTableHighlight(mEvent, highlightColor)
{
	if (!mEvent)
	{
    	mEvent=window.event;
  	}
	
	if (mEvent.srcElement)
	{
    	highlightTableRow( mEvent.srcElement, highlightColor);
	}
	else if (mEvent.target)
	{
		highlightTableRow( mEvent.target, highlightColor);		
	}
}

function highlightTableRow(myElement, highlightColor)
{
 	var i=0;

	for (i; i<savedStateCount; i++)
	{
		restoreBackgroundStyle(savedStates[i]); 
	}
  
	savedStateCount=0;

	while (myElement && ((myElement.tagName && myElement.tagName!="TR") || !myElement.tagName))
	{
		myElement=myElement.parentNode;
	}

	if (!myElement || (myElement && myElement.id && myElement.id=="myTableHeader") )
	{
		return;
	}

	if (myElement)
	{
		var tableRow=myElement;

		if (tableRow)
		{
			savedStates[savedStateCount]=saveBackgroundStyle(tableRow);
			savedStateCount++;
		}

		var tableCell=findNode(myElement, "TD"); 

		var i=0;
		
		while (tableCell)
		{
			if (tableCell.tagName=="TD")
			{
				if (!tableCell.style)
				{
					tableCell.style={};
				}
				else
				{
					savedStates[savedStateCount]=saveBackgroundStyle(tableCell); 
					savedStateCount++;
				}
        
				tableCell.style["backgroundColor"]=highlightColor;
				tableCell.style.cursor='default';
        		i++;
			}
      
			tableCell=tableCell.nextSibling;
		}
	}
}

function showThumbnail(propertyID)
{
	window.parent.document.getElementById("myFlashMap").showThumbnail(propertyID);
}
 
function showProperty(propertyID)
{
	window.parent.document.getElementById("myFlashMap").showProperty(propertyID);
}
 
function hideThumbnail()
{
	window.parent.document.getElementById("myFlashMap").hideThumbnail();
}

function hideThumbnail2()
{
	thisMovie("myFlashMap").hideThumbnail();
}

function showMarker(address, latitude, longitude)
{
   thisMovie("myFlashMap").updateLatitude(latitude);
   thisMovie("myFlashMap").updateLongitude(longitude);
   thisMovie("myFlashMap").updateMapCoords();	
   thisMovie("myFlashMap").showMarker(address);	
}

function showMapLocator(latitude, longitude)
{
	var myLink = myDirectory + "/flash/swf/locMap2.swf";
	var so = new SWFObject(myLink, "myFlashMap", "215", "215", "9", "#FFFFFF");
	so.addParam("id", "myFlashMap");
	so.addParam("wmode","transparent");
	so.addParam("allowFullScreen", "true");
	so.addParam("allowScriptAccess", "sameDomain");
	so.addVariable("NEW_CENTER_LATITUDE", latitude);
	so.addVariable("NEW_CENTER_LONGITUDE", longitude);
	so.addVariable("ZOOM_MAP", 4);				
	so.write("flashMap");	
	initJavascript();
}

function viewProfile(profileLink)
{
	window.parent.document.location.href = profileLink;
}