var location_alphabet = ["B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","X","Y","Z"];
function getdirsubmit() {
	var startaddress = document.getdir.txtStartStreet.value;
	startaddress = startaddress.replace(/^\s*/, "");
	startaddress = startaddress.replace(/\s*$/, "");
/*
	var startcity = document.getdir.txtStartCity.value;
	startcity = startcity.replace(/^\s* /, "");
	startcity = startcity.replace(/\s*$/, "");
	var startstate = document.getdir.txtStartState.value;
	startstate = startstate.replace(/^\s* /, "");
	startstate = startstate.replace(/\s*$/, "");
	var startzip = document.getdir.txtStartZip.value;
	startzip = startzip.replace(/^\s* /, "");
	startzip = startzip.replace(/\s*$/, "");
*/
	var countlocation = 0;
/*
	if (startaddress != "" || startcity != "" || startstate != ""  || startzip != "") {
	if (startcity == ""  && startstate == "" && startzip == "") {
		alert("City or State or Zipcode is required for Start Location");
		return;
	}
	else {
		countlocation++;
	}
	}
*/
	if (startaddress != "") {
		countlocation++;
	}
/*
	document.getdir.txtStartCity.value = startcity;
	document.getdir.txtStartState.value = startstate;
*/
	var selectedlocations = document.getdir["selLocations[]"];
	for(var i=0; i<selectedlocations.length; i++) {
		selectedlocations[i].selected = true;
		countlocation++;
	}
	if (countlocation < 2) {
		alert("Enter more than one location to get directions");
		return;
	}

	//var gdir = new GDirections;
	//gdir.load("from: "+startaddress+"to: "+selectedlocations[0]);
	//GEvent.addListener(gdir, "error", handleErrors);
//alert(gdir.getStatus().code);
		//if (response == null) {
			window.open("","NewWin","width=640,scrollbars=1,resizable=1,toolbar=0");
			document.getdir.submit();
		//}
		//else {
			//alert('x');
		//}
}

function adddirections(street,city,state,zip) {
	var newaddress = street != "" ? street + ", " : street;
	newaddress = newaddress + city + ", ";
	newaddress = newaddress + state + " " + zip;
	var selectedlocations = parent.document.getdir["selLocations[]"];
        for(var i=0; i<selectedlocations.length; i++) {
                if (newaddress == selectedlocations[i].value) {
                        alert(newaddress + ' is already in the list');
                        return;
                }
        }
	selectedlocations.options[selectedlocations.length] = new Option(location_alphabet[selectedlocations.length]+": "+newaddress, newaddress, false, false);
}

function addlocation() {
	var newstreet = document.getdir.txtAddStreet.value;
	var newcity = document.getdir.txtAddCity.value;
	var newstate = document.getdir.txtAddState.value;
	var newzip = document.getdir.txtAddZip.value;
	newcity = newcity.replace(/^\s*/, "");
	newcity = newcity.replace(/\s*$/, "");
	newstate = newstate.replace(/^\s*/, "");
	newstate = newstate.replace(/\s*$/, "");
	newstreet = newstreet.replace(/^\s*/, "");
	newstreet = newstreet.replace(/\s*$/, "");
	newzip = newzip.replace(/^\s*/, "");
	newzip = newzip.replace(/\s*$/, "");
	document.getdir.txtAddStreet.value = newstreet;
	document.getdir.txtAddCity.value = newcity;
	document.getdir.txtAddState.value = newstate;
	document.getdir.txtAddZip.value = newzip;
	if (newcity == "") {
		alert("City State or Zip is required for Start Location");
		return;
	}
	if (newzip == ""  && newstate == "") {
		alert("City State or Zip is required for Start Location");
		return;
	}
	var newaddress = newstreet != "" ? newstreet + "," : newstreet;
	newaddress = newaddress + newcity + ",";
	newaddress = newaddress + newstate + newzip;
	var selectedlocations = document.getdir["selLocations[]"];
	selectedlocations.options[selectedlocations.length] = new Option(location_alphabet[selectedlocations.length]+": "+newaddress, newaddress, false, false);
}

function deletelocation() {
	var selectedlocations = document.getdir["selLocations[]"];
	var selectedindex = selectedlocations.selectedIndex;
	if (selectedindex < 0) return;
	selectedlocations.options[selectedlocations.selectedIndex] = null;
	for(var i=0; i<selectedlocations.length; i++) {
		selectedlocations.options[i].text = location_alphabet[i]+": "+selectedlocations.options[i].value;
	}
/*
	document.getdir.txtAddStreet.value = "";
	document.getdir.txtAddCity.value = "";
	document.getdir.txtAddState.value = "";
	document.getdir.txtAddZip.value = "";
*/

}

function moveuplist() {
	var selectedlocations = document.getdir["selLocations[]"];
	var selectedindex = selectedlocations.selectedIndex;
	var selectedaddress = selectedlocations.options[selectedindex].value;
	if (selectedindex <= 0) return;
	selectedlocations.options[selectedindex].value = selectedlocations.options[selectedindex-1].value;
	selectedlocations.options[selectedindex].text = location_alphabet[selectedindex]+": "+selectedlocations.options[selectedindex-1].value;
	selectedlocations.options[selectedindex-1].value = selectedaddress;
	selectedlocations.options[selectedindex-1].text = location_alphabet[selectedindex-1]+": "+selectedaddress;
	selectedlocations.options[selectedindex-1].selected = true;
	selectedlocations.options[selectedindex].selected = false;
}

function movedownlist() {
        var selectedlocations = document.getdir["selLocations[]"];
        var selectedindex = selectedlocations.selectedIndex;
        var selectedaddress = selectedlocations.options[selectedindex].value;
        if (selectedindex == selectedlocations.length-1) return;
        selectedlocations.options[selectedindex].value = selectedlocations.options[selectedindex+1].value;
        selectedlocations.options[selectedindex].text = location_alphabet[selectedindex]+": "+selectedlocations.options[selectedindex+1].value;
        selectedlocations.options[selectedindex+1].value = selectedaddress;
        selectedlocations.options[selectedindex+1].text = location_alphabet[selectedindex+1]+": "+selectedaddress;
        selectedlocations.options[selectedindex+1].selected = true;
        selectedlocations.options[selectedindex].selected = false;
}

function clearlocations() {
        var selectedlocations = document.getdir["selLocations[]"];
	while(selectedlocations.length> 0) {
		selectedlocations.options[0] = null;
	}
}
