function getListBox() {
	var theForm = parent.document.forms[0];
	for (var i=0;i<theForm.length;i++) {
		if (theForm.elements[i].id.indexOf("lbxPostalCodes") != -1) { //find list box
			ri = theForm.elements[i].id;
			break;
		} 
	}
	return ri
}

/* ##############################################################
##########     Interactive Map / Select Box Script     ##########
############################################################## */

// toggleZip() Function is called from the select Zip function.
// This function loops through the passed in value(s) and compares them to the options() values.
function toggleZip(obj, val) {

	var arrayZips=val.split(","),arrayMatched=new Array(),mc=0,sc=0,strMessage;
	//Build Confirm Message
	strMessage = 'You Have De-Selected The Zip Code(s)';
	for (var i=0;i<arrayZips.length;i++) {
		strMessage += '\n            -';
		strMessage += ' '+ arrayZips[i];
	}
	strMessage += '\n\nClick OK if this IS what you intended.';
	strMessage += '\nClick CANCEL if this is NOT what you intended.';
	
	//Compare Passed Zips to Zips in List Box and placed matched options into array
	for (var n=0;n<arrayZips.length;n++) {
		var selValue = arrayZips[n].toString().replace(/^\s*/,"").replace(/\s*$/,"");
		for (var i=0;i<obj.length;i++) {
			var zipValue = obj.options[i].value.toString().replace(/^\s*/,"").replace(/\s*$/,"");
			if (zipValue == selValue) {
				arrayMatched[mc]=obj.options[i];
				mc++
			}
		}
	}
	
	//Check selected state (true/false) of matched options and increment selectedcount (sc)
	for(var i=0;i<arrayMatched.length;i++){
		if(arrayMatched[i].selected){
			sc++;
		}else{
			arrayMatched[i].selected=true;
			changeGoButton();
		}
	}
	
	//Check selectedcount (sc) to see if all passed zips were previously selected...if so
	//present user with confirm message to confirm they want to de-select the zip codes...
	//otherwise, it exits the function and leaves things alone.
	if(sc==arrayMatched.length){
		if(confirm(strMessage)){
			for(var i=0;i<arrayMatched.length;i++){
				arrayMatched[i].selected=false;
				changeGoButton();
			}
		} else {
			return;
		}
	}
}

// Main Function - Calls the toggle() function to select or deselect
// zip codes within the select box.
// This function also defines the path to the select box object.
function selectZip(zip) {
	var theListBoxId = getListBox();
	var obj = eval('parent.document.getElementById("'+ theListBoxId +'")');
	toggleZip(obj, zip);
}



/* ##########################################################################################
#####################        Map / List Box Control Functions       ######################
########################################################################################## */

//Is called from the Select County page when a county is clicked.
function enableListBox(strId1,strId2) {
	var showItem = eval("parent.document.getElementById('" + strId1 + "')");
	var hideItem = eval("parent.document.getElementById('" + strId2 + "')");
	if (showItem.style.display != 'inline') {
		showItem.style.display = 'inline';
		if (hideItem.style.display != 'none') {
			hideItem.style.display = 'none';
		}
	}
}


//Runs from the Select County page
function toggleListBox() {
	var theListBoxId	= getListBox();
	var PostalCodes		= eval('parent.document.getElementById("'+ theListBoxId +'")');
	var ListBox			= parent.document.getElementById('ListBox');	
	var MapIntro		= parent.document.getElementById('MapIntro');
	var count			= 0;
	
	for (var i = 0; i < PostalCodes.length; i++) {
		if (PostalCodes.options[i].selected == true && PostalCodes.options[i].value.length != 0) {
			count++;
		}
	}

	if (count == 0) {
		if (ListBox.style.display == 'inline' || ListBox.style.display == '') {
			if (parent.frames['mapFrame'].location.pathname.toLowerCase().indexOf('selectcounty.htm')>-1) {
				ListBox.style.display = 'none';
				MapIntro.style.display = 'inline';
			}
		}
	} else {
			ListBox.style.display = 'inline';
			MapIntro.style.display = 'none';
	}
}

function getPrevMap() {
	var theFrame	= parent.frames['mapFrame'];
	var stopUrl		= "/idx/search/maps/selectCounty.htm";
	var prevBtn		= document.getElementById('BackBtn');
	if (theFrame.location.pathname != stopUrl && (prevBtn.style.display == 'inline' || prevBtn.style.display=='')) {
		theFrame.history.back();
	} else {
		if (theFrame.location.pathname == stopUrl && (prevBtn.style.display == 'inline' || prevBtn.style.display=='')) {
			prevBtn.style.display = 'none';
		}
	}
}

//Shows & Hides the previous button depending on which map they are on.
function checkPrevBtn() {
	var prevBtn		= parent.document.getElementById("BackBtn");
	var theFrame	= parent.frames['mapFrame'];
	var stopUrl		= "/idx/search/maps/selectCounty.htm";
	if (theFrame.location.pathname == stopUrl) {
		prevBtn.style.display = 'none';
	} else {
		if (theFrame.location.pathname != stopUrl && prevBtn.style.display == 'none') {
				prevBtn.style.display = 'inline';
		}
	}
}

// Shows or Hides the go button depending on whether any valid selections in the ListBox have been made.
function changeGoButton() {
	var theListBoxId = getListBox();
	var goButton  = parent.document.getElementById("TopGoButton");
	var goButton1 = parent.document.getElementById("BottomGoButton");
	var selObject = eval('parent.document.getElementById("'+ theListBoxId +'")');
	var counter = 0;
	for (var i=0;i<=selObject.length-1;i++) {
		var k = i;
		if (selObject.options[i].selected == false) {
			counter++;
		} else {
			if ((selObject.options[i].selected == true) && (selObject.options[i].value == "")) {
				counter++;
			}
		}
	}
	if (counter-1 == k) {
		goButton.style.display = 'none';
		goButton1.style.display = 'none';
	} else {
		if (counter-1 != k) {
			goButton.style.display = 'inline';
			goButton1.style.display = 'inline';
		}
	}
}


function doInit() {
	checkPrevBtn();
	changeGoButton();
}

window.onload=doInit;