var sortem = 1;  //true
function addtownbyname(tobox, name, id) {

	var bct=tobox.length;

			var newoption = new Option();
			newoption.value = id;
			newoption.text = name;
			tobox.options[bct] = newoption ;
			tobox.length++;
	KillSpace(tobox);
}

function removeallitems(frombox) {

	for(var i=0; i<frombox.options.length; i++) {
		frombox.options[i].value = "";
		frombox.options[i].text = "";
	}

	KillSpace(frombox);
	if (frombox.name != "townbox") {
		addtownbyname (frombox,"None Selected")
	}
}

function removeallitems2(frombox) {

	var repl = "<h2 style=\"margin: 0;font-style: normal\">Selections</h2>\n";
	repl += "<select id=\"thetowns\" name=\"thetowns[]\" multiple=\"multiple\" style=\"width: 160px; height: 200px; margin-top: 0;\" ondblclick=\"removeitem(document.getElementById('frmsearch').thetowns);\">\n";
	repl += "<option value=\"0\">None Selected</option>\n";
	repl += "</select>\n";

	document.getElementById("selectedtowns").innerHTML = repl;
}

function town(name) { this.name=name; } 

function addtown(name) {
	//alert("I am here: AddTown"); 	
	
	var tobox = document.getElementById("frmsearch").thetowns;
	var bct=tobox.length;

			var newoption = new Option();
			newoption.value = name;
			newoption.text = name;
			tobox.options[bct] = newoption ;
			tobox.length++;
			

			
	KillSpace(tobox);
}

function removetown(name) {
	var frombox = document.getElementById("frmsearch").thetowns;

	for(var i=0; i<frombox.options.length; i++) {
		if (frombox.options[i].value == name) {
			frombox.options[i].value = "";
			frombox.options[i].text = "";
	   }
	}
	KillSpace(frombox);
}

function cleartowns() {
	var frombox = document.getElementById("frmsearch").thetowns;

	for(var i=0; i<frombox.options.length; i++) {
		frombox.options[i].value = "";
		frombox.options[i].text = "";
	}

	KillSpace(frombox);
}


function KillSpace(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;
		KillSpace(box);
   }
}


function selecttowns() {
	// selects all towns in the thetowns list
	
	//alert("I am here"); 
	
	var box=document.getElementById("frmsearch").thetowns;
	for(var i=0; i<box.options.length; i++) {
		box.options[i].selected=true;
	}
		//alert(box.options.length); 
}

function deselecttowns() {
	// selects all towns in the thetowns list
	var box=document.getElementById("frmsearch").townbox;
	for(var i=0; i<box.options.length; i++) {
		box.options[i].selected=false;
	}
}


function additem(frombox,tobox) {
	move(frombox,tobox);
	removeitembyname(tobox,"None Selected")
}
function addallitems(frombox,tobox) {
	removeitembyname(tobox, "None Selected");
	// alert(frombox.name + " " + frombox.options.length);
	// return;
	for(var i=0; i<frombox.options.length; i++) {
		if(frombox.options[i].value != "") {
			var newoption = new Option();
			newoption.value = frombox.options[i].text;
			newoption.text = frombox.options[i].text;

			// check that this entry doesn't already exist.  If it doesn't, then add it

			var len = tobox.length;
			var found = false;
			for(var count = 0; count < len; count++) {
				if (tobox.options[count] != null) {
					if (newoption.text == tobox.options[count].text) {
					found = true;
					break;
					}
				}
			}

			if (found != true) {
				tobox.options[tobox.options.length] = newoption;
				len++;
			}
		   }
	}
	 if (sortem) SortThem(tobox);
}

function removeitem(frombox) {
	for(var i=0; i<frombox.options.length; i++) {
		if(frombox.options[i].selected && frombox.options[i].value != "") {
			frombox.options[i].value = "";
			frombox.options[i].text = "";
		   }
	}
	KillSpace(frombox);
	if (frombox.options.length<1){
		addtownbyname (frombox,"None Selected")
	}
}
function removeitembyname(frombox, zName) {
	// alert(zName);
	for(var i=0; i<frombox.options.length; i++) {
		// alert(frombox.options[i].text);
		if(frombox.options[i].text == zName) {
			frombox.options[i].value = "";
			frombox.options[i].text = "";
		   }
	}
	KillSpace(frombox);
}

function move(frombox,tobox) {
	for(var i=0; i<frombox.options.length; i++) {
		if(frombox.options[i].selected && frombox.options[i].value != "") {
			// alert(frombox.options[i].value);
			var newoption = new Option();
			newoption.value = frombox.options[i].text;
			newoption.text = frombox.options[i].text;
			
			// alert ("V: " + frombox.options[i].text+ "T: " + frombox.options[i].text);

			// check that this entry doesn't already exist.  If it doesn't, then add it

			var len = tobox.length;
			var found = false;
			for(var count = 0; count < len; count++) {
				if (tobox.options[count] != null) {
					if (newoption.text == tobox.options[count].text) {
					found = true;
					break;
					}
				}
			}

			if (found != true) {
				tobox.options[tobox.options.length] = newoption;
				len++;
			}
		   }
	}
	 if (sortem) SortThem(tobox);
}
function SortThem(lbox)  {
	// alert ("Sorting");
	var t_opts = new Array();
	var temp = new Object();

	for(var i=0; i<lbox.options.length; i++)  {
		t_opts[i] = lbox.options[i];
	}

	for(var x=0; x<t_opts.length-1; x++)  {
		for(var y=(x+1); y<t_opts.length; y++)  {
			if(t_opts[x].text > t_opts[y].text)  {
				temp = t_opts[x].text;
				t_opts[x].text = t_opts[y].text;
				t_opts[y].text = temp;
				temp = t_opts[x].value;
				t_opts[x].value = t_opts[y].value;
				t_opts[y].value = temp;
			}
	   	}
	}

	for(var i=0; i<lbox.options.length; i++)  {
		lbox.options[i].value = t_opts[i].value;
		lbox.options[i].text = t_opts[i].text;
   }
}

//======================================================================
function clearInput(e){
if(e.value=='MLS# or keywords')e.value="";
} 


function submitform() {
	// deselecttowns();  // for the townbox on left
	selecttowns();
	document.frmsearch.submit();
}

function clearform() {
	removeallitems2('selectedtowns');
	document.frmsearch.resetbutton.click();
}


function submitqs() {
	// alert("submitqs");
	selecttowns();
	document.frmsearch.submit();
}