var runningMapChange = false;
var countyIndex = 0
var typeIndex = 2
var theString = ""
var stateAbrev
//alert(document.propsearchform.state.options[document.propsearchform.state.selectedIndex].value)

//WHAT?!?!
if (document.propsearchform.state.type == 'select-one') {
	//select box
	stateAbrev = document.propsearchform.state.value }
else {
	//hidden text field
	stateAbrev = document.propsearchform.state.value
}

var areaList = new Array();
var arAreaList = new Array();

function initAreas() {
	areaList = Array(1);
	areaList[0] = "select county in"
	
	arAreaList[0] = new Array();   //county array Value
	arAreaList[0][0] = new Array();//cities
	arAreaList[0][1] = new Array();//school districts
	arAreaList[0][2] = new Array();//townships
	arAreaList[0][3] = new Array();//zipcodess
	
	arAreaList[0][0][0] =  "< None >$--*None Available*--" 
	arAreaList[0][1][0] =  "< None >$--*None Available*--" 
	arAreaList[0][2][0] =  "< None >$--*None Available*--" 
	arAreaList[0][3][0] =  "< None >$--*None Available*--"
}

initAreas();

//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
// All functions start here
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
// Save Property AJAX /--->

// read in the text file
function getList(httpPath,state,searchType) {

//function getList(state,searchType) {
	var couldProcess = false;
	//document.getElementById("imgSaveProp" + rowID).src = "images/lnk_saveprop_t.gif";
	//document.getElementById("aSaveProp" + rowID).onclick = doNothing;
	
	if (!couldProcess && httpRequester) {
	    httpRequester.open("GET", httpPath + "/scripts/areaLists/getList.cfm?state=" + state + "&searchType=" + searchType, true);
	//httpRequester.open("POST", "/scripts/areaLists/getList.cfm?state=" + state + "&searchType=" + searchType, true);
	    httpRequester.onreadystatechange = processResponse;
	    couldProcess = true;
	    httpRequester.send(null);
	}
}
	
// wait until the whole file's been read, then run the script
function processResponse() {

	if ( httpRequester.readyState == 4 ) {// Complete
		if ( httpRequester.responseText.indexOf('invalid') == -1 ) {
			//alert(httpRequester.responseText); // parse the server response
			//document.getElementById("imgSaveProp" + httpRequester.responseText).src = "images/lnk_saveprop_o.gif";
			eval(httpRequester.responseText);
			//alert(eval(httpRequester.responseText));
			buildOnChange();
			couldProcess = false;
			countyEnabled = true;
	    }
	}
}

// load the county drop down	
function buildOnChange(){

	// clear out the county drop down
	document.propsearchform.county.options.length = 0;

	//alert(areaList.length);
	for (i=0;i < areaList.length;i++) {
		var aString = areaList[i];		
		var newopt = new Option( aString, escape( i ) );
		document.propsearchform.county.options[i] = newopt;
	}

	document.getElementById('county').options[0].selected = true;
	
	//window.document.map_flash.SetVariable('msgState', 'select county in');
	document.propsearchform.county.selectedIndex = 0;
	refreshLocations();
	changeType(2);
}

// Build Region Select Box
function refreshLocations() {

	var selectedArea = parseInt(document.propsearchform.county.options[document.propsearchform.county.selectedIndex].value);
	selectedArea = isNaN(selectedArea) ? 0 : selectedArea; // Zero selectedArea in case it isn't a number
	document.propsearchform.version.options.length = 0;

 	for (i=0; i < arAreaList[eval(selectedArea)][eval(typeIndex)].length; i++) {
		var aString = arAreaList[eval(selectedArea)][eval(typeIndex)][i];
		// 0 = value
		// 1 = Label
		stringArray = new Array()
		stringArray = aString.split('$')

		var newopt = new Option( stringArray[1], escape(stringArray[0]))
		
		document.propsearchform.version.options[i] = newopt;
		
		runningMapChange=false;
	}
}


function changeType(typeValue){

	

	stateAbrev = document.propsearchform.state.value;

	//alert("typeIndex: " + typeIndex + "; stateAbrev: " + stateAbrev + "; ");
	//alert("selIndex: " + document.propsearchform.county.selectedIndex);

	// change the radio button labels
	if (stateAbrev == "DE"){
		radioBut2TextValue = "By Area"
		radioBut0TextValue = "By City"
		document.getElementById("rb2").disabled = false
	}
	else if ((stateAbrev == "PA")  && (document.propsearchform.county.value == 6)){
		radioBut0TextValue = "By City/Area"
		radioBut2TextValue = "By Philly Neighborhood"
		document.getElementById("rb2").disabled = false
	}
	else if ((stateAbrev=="NJ") && (document.propsearchform.county.value == 4)){
		//radioBut2TextValue = "N/A"
		//alert("here")
		document.getElementById("rb2").disabled = true
		
		//alert(document.getElementById("rb0").checked)
		//alert(document.getElementById("rb1").checked)
		//alert(document.getElementById("rb3").checked)
		

		if ( (document.getElementById("rb3").checked == false) && (document.getElementById("rb0").checked == false) && (document.getElementById("rb1").checked == false) ) {
				typeIndex=document.getElementById("rb0").value;
				document.getElementById("rb0").checked = true
		}
	
	}
	else {
		radioBut2TextValue = "By Township"
		radioBut0TextValue = "By City/Neighborhood"
		document.getElementById("rb2").disabled = false
		refreshLocations()
		//document.getElementById("rb0").checked = false
	}

	document.getElementById("lbl-rb2").innerHTML  = radioBut2TextValue
	document.getElementById("lbl-rb0").innerHTML  = radioBut0TextValue
	

		if(document.getElementById("rb0").checked==true)
			typeIndex=0;
		if(document.getElementById("rb1").checked==true)
			typeIndex=1;
		if(document.getElementById("rb2").checked==true)
			typeIndex=2;
		if(document.getElementById("rb3").checked==true)
			typeIndex=3;

	
	// change the label above the squirt box
	if (typeIndex == 0){
		if (stateAbrev == "DE"){
			indexTypeValue = "Select City"
		}
		else if ((stateAbrev == "PA")&& (document.propsearchform.county.options[document.propsearchform.county.selectedIndex].value == 6)){
			indexTypeValue = "Select City/Area"
		}
		else{
			indexTypeValue = "Select City/Neighborhood"
		}
	}
	
	else if (typeIndex == 1){
		indexTypeValue = "Select School Districts"
	}
	
	else if (typeIndex == 3){
		indexTypeValue = "Select Zip Codes"
	}
		
	else if (typeIndex == 2){
		//alert("in typeIndex2; stateAbrev=" + stateAbrev );	
		if (stateAbrev == "DE"){
			//alert("state=DE");
			indexTypeValue = "Select Area"
		}
		else if ((stateAbrev == "PA")&& (document.propsearchform.county.options[document.propsearchform.county.selectedIndex].value == 6)){
			//alert("state=PA and county=Philly");
			
			indexTypeValue = "Select Philly Neighborhood"
		}
		else{
			//alert("anything else");
			indexTypeValue = "Select Townships"
		}
	}
	
	if(document.getElementById){
		document.getElementById("selectValue").innerHTML  = indexTypeValue
	}
	
	if(!runningMapChange) {
		refreshLocations();
	}

}

sortitems = 1;  // Automatically sort items within lists? (1 or 0)

function additem(fbox,tbox) {
	move(fbox,tbox);
	removeitembyname(tbox,"None Selected")
	if (sortitems) SortD(tbox);
}

function additembyname(tbox, theName) {
	if (tbox.options.length == 0) {
			var no = new Option();
			no.value = theName;
			no.text = theName;
			tbox.options[0] = no;
			tbox.length++;
	}
	BumpUp(tbox);
}

function removeitembyname(fbox, theName) {
	for(var i=0; i<fbox.options.length; i++) {
	
		if(fbox.options[i].value == theName) {
			fbox.options[i].value = "";
			fbox.options[i].text = "";
		   }
	}
	BumpUp(fbox);
}

function removeitem(fbox) {
	for(var i=0; i<fbox.options.length; i++) {
		if(fbox.options[i].selected && fbox.options[i].value != "") {
			
			
			// var no = new Option();
			// no.value = fbox.options[i].value;
			// no.text = fbox.options[i].text;
			// tbox.options[tbox.options.length] = no;
			fbox.options[i].value = "";
			fbox.options[i].text = "";
		   }
	}
	BumpUp(fbox);
	additembyname (fbox,"None Selected")
}

// pc 12/05/06 i added this function to clear away the selected squirtbox when index.cfm loads
function flushIndex(fbox) {
	for(var i=0; i<fbox.options.length; i++) {
		if(fbox.options[i].value != "") {
		
			fbox.options[i].value = "";
			fbox.options[i].text = "";
		   }
	}
	BumpUp(fbox);
	additembyname (fbox,"None Selected")
}

function move(fbox,tbox) {
	for(var i=0; i<fbox.options.length; i++) {
		if(fbox.options[i].selected && fbox.options[i].value != "") {
			var no = new Option();
			no.value = fbox.options[i].value;
			no.text = fbox.options[i].text;

			// check that this entry doesn't already exist.  If it doesn't, then add it

			var len = tbox.length;
			var found = false;
			for(var count = 0; count < len; count++) {
				if (tbox.options[count] != null) {
					if (no.text == tbox.options[count].text) {
					found = true;
					break;
					}
				}
			}


			if (found != true) {
				tbox.options[tbox.options.length] = no;
				len++;
			}
			// tbox.options[tbox.options.length] = no;
			// fbox.options[i].value = "";
			// fbox.options[i].text = "";
		   }
	}
	// BumpUp(fbox);
	if (sortitems) SortD(tbox);
}


function BumpUp(box)  {
for(var i=0; i<box.options.length; i++) {
if(box.options[i].value == "")  {
for(var j=i; j<box.options.length-1; j++)  {
box.options[j].value = box.options[j+1].value;
box.options[j].text = box.options[j+1].text;
}
var ln = i;
break;
   }
}
if(ln < box.options.length)  {
box.options.length -= 1;
BumpUp(box);
   }
}

function SortD(box)  {
var temp_opts = new Array();
var temp = new Object();
for(var i=0; i<box.options.length; i++)  {
temp_opts[i] = box.options[i];
}
for(var x=0; x<temp_opts.length-1; x++)  {
for(var y=(x+1); y<temp_opts.length; y++)  {
if(temp_opts[x].text > temp_opts[y].text)  {
temp = temp_opts[x].text;
temp_opts[x].text = temp_opts[y].text;
temp_opts[y].text = temp;
temp = temp_opts[x].value;
temp_opts[x].value = temp_opts[y].value;
temp_opts[y].value = temp;
      }
   }
}
for(var i=0; i<box.options.length; i++)  {
box.options[i].value = temp_opts[i].value;
box.options[i].text = temp_opts[i].text;
   }
}

// This works with the body onLoad function 
//This function when invi=oked checks hidden field 'cityTempList' to see if it has any values
//if values are present the are inserted into the selectedVersion field
//this is to maintain state when using the back button. -tb
function fillSelectedBox(){

if (document.propsearchform.cityTempList.value != ""){
var theStringSelected = document.propsearchform.cityTempList.value


var locationArraySelected = new Array();
var regionArraySelected = new Array();

locationArraySelected = theStringSelected.split(",");
//alert(locationArraySelected.length)
//alert(locationArraySelected)
var counter = 0;
	for (i=0;i < locationArraySelected.length;i=i+2) {
		var aString = locationArraySelected[i];
		var aString2 = locationArraySelected[i+1];
	 			var newopt = new Option( decodeURI(aString2), decodeURI(aString) ) ;
			document.propsearchform.selectedVersion.options[counter] = newopt;
			counter++;
	 		
		}

	}

}
//this function takes the values in a selectedVesion field and adds them 
//to a hidden field so that they can be submitted and 
//readded to the selected Version field when using the back button  -tb
function fillTempList(){
var tList = ""
if (document.propsearchform.version != ''){

a = document.propsearchform.selectedVersion.options.length
for (var i=0; i<a; i++){
if ((i+1) != a){
optionValue = document.propsearchform.selectedVersion.options[i].value;
optionText = document.propsearchform.selectedVersion.options[i].text;

tList = tList + optionValue + "," + optionText + "," 
}
else{
optionValue = document.propsearchform.selectedVersion.options[i].value;
optionText = document.propsearchform.selectedVersion.options[i].text;

tList = tList + optionValue + "," + optionText
	}
}

document.propsearchform.cityTempList.value = tList
//alert(document.propsearchform.cityTempList.value)

	}

}



function changeChoice(tmpVal){
document.propsearchform.choices.value = tmpVal
}

//reloads page with new state parameter
function changeState(){
if (stateAbrev = document.propsearchform.state.options[document.propsearchform.state.selectedIndex].value != "" ){
stateAbrev = document.propsearchform.state.options[document.propsearchform.state.selectedIndex].value
location.href="/index.cfm?state=" + stateAbrev + "&newSearch=Y"
}

}



function changeShow(){
document.propsearchform.show.checked = false
}

//changes the index value for the county dropdown and reloads the map with the correct county
function changeIndexValue(newIndex){ 
//document.images.region.src = "/images/maps/" + stateAbrev + "/" + (newIndex) + ".gif"
document.propsearchform.county.options[newIndex].selected = document.propsearchform.county.options[newIndex]
refreshLocations()
document.propsearchform.location[0].checked = true
changeType(2)

}

//change form action to go to advance search page
function changeAction(newAct){
document.propsearchform.action = newAct
fillTempList()
document.propsearchform.submit();
}


