
//Add Load Event
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

//Prepare Images to change on the Legend Navigation
function prepareImages() {
	if (!document.getElementById) return false;
	if (!document.getElementsByTagName) return false;
	var content = document.getElementById("legend");
	// Find all of the affected list items
	var items = content.getElementsByTagName("li");
	for (var i=0; i<items.length; i++) {
		items[i].onmousedown = function () {
			// Invoke changeImage
			return changeImage(this,"click");
		}
	}
}

//Change the Appropriate image in the Legend Navigation
function changeImage(whichList,state) {
	
	var item = whichList.getAttribute("id");
	var underScore = item.indexOf("_");
	
	// Check to see if the _ is in the string
	if (underScore >= 0) { // The current id is active, change it
				
		var newId = "";
		
		// Loop through the string to remove the _
		for (var i=0; i<underScore; i++) {
			if (newId.Length >= 1) {
				newId = item.charAt(i);
			} else {
				var newId = newId + item.charAt(i);
			}
		}
					
		whichList.setAttribute("id",newId);	
		
	} else { // The current id is inactive, change it
		
		
			var newId = item + "_active";

			whichList.setAttribute("id",newId);
		
	}
}

function legendClick(object)
{
	object.checked = !object.checked;
	setSecondaryFilter();
	toggleLocations(object);
	
}

function toggleSecondary()
{
	toggleLocations(document.getElementById("mts"));
	toggleLocations(document.getElementById("dealer"));
}

function setSecondaryFilter()
{
	var disabled = true;
	if(document.getElementById("dealer").checked == true || document.getElementById("mts").checked == true)
	{//changed mtsDealer to dealer
		disabled = false;
	}

	if(disabled){
		document.getElementById("secondaryFilter").style.display="none";
	}
	else{
		document.getElementById("secondaryFilter").style.display="block";
	}
}

function toggleSelectClick(object)
{
	var objString = object.name;
	
	var activeString = "";
	var newActiveString = "_active";
	if(!object.checked)
	{
		activeString = "_active";
		newActiveString = "";
	}

	document.getElementById(objString + activeString).id = objString + newActiveString ;
	setSecondaryFilter();
	toggleLocations(object);
}
addLoadEvent (prepareImages);
addLoadEvent (setSecondaryFilter);
