var arrEncodeUserTryAddresses = new Array(4);
var TPAddUrl = "";

	function addTPEventUsingXML(name, add1, add2, city, state, zip) {
			var strQuery = "";
			strQuery = "frmAddAnAddress=1&fldAddName=" + escape(name) + "&fldAddAddress1=" + escape(add1) + "&fldAddAddress2=" + escape(add2) + "&fldAddCity=" + escape(city) + "&fldAddState=" + escape(state) + "&fldAddZip=" +  escape(zip)
		
			TPAddUrl = '/maps/scripts/add_member_to_plan.asp?' + strQuery;
			
			//Get lat lng from google first then call page to add
			//Store possible address attempts in array
			arrEncodeUserTryAddresses[0] = add1 + "," + add2 + "," + city + "," + state + " " + zip;
			arrEncodeUserTryAddresses[1] = add1 + "," + city + "," + state + " " + zip;
			arrEncodeUserTryAddresses[2] = add2 + "," + city + "," + state + " " + zip;
			arrEncodeUserTryAddresses[3] = city + "," + state + " " + zip;
				
			encodeThenAddUserAddress(0);
	}
	
	function addTPMemberUsingXML(memberID) {
		//use logic like this to refresh list ==> http://www.captain.at/howto-ajax-form-post-request.php		
		if(parseInt(memberID) > 0) { //add the member
			TPAddUrl = '/maps/scripts/add_member_to_plan.asp?fldMemberID=' + memberID;
	
	
			$.ajax({
				type: "POST",
				url: '/maps/scripts/add_member_to_plan.asp?fldMemberID=' + memberID,
				data: "",//"name=John&location=Boston",
				success: processTPMemberAddXMLData
			 });
			
		} else{ //add the user specified address	
			var strQuery = "";
			strQuery = "frmAddAnAddress=1&fldAddName=" + escape(document.getElementById('fldAddName').value) + "&fldAddAddress1=" + escape(document.getElementById('fldAddAddress1').value) + "&fldAddAddress2=" + escape(document.getElementById('fldAddAddress2').value) + "&fldAddCity=" + escape(document.getElementById('fldAddCity').value) + "&fldAddState=" + escape(document.getElementById('fldAddState').value) + "&fldAddZip=" +  escape(document.getElementById('fldAddZip').value)
		
			TPAddUrl = '/maps/scripts/add_member_to_plan.asp?' + strQuery;
			
			//Get lat lng from google first then call page to add
			//Store possible address attempts in array
			arrEncodeUserTryAddresses[0] = document.getElementById('fldAddAddress1').value + "," + document.getElementById('fldAddAddress2').value + "," + document.getElementById('fldAddCity').value + "," + document.getElementById('fldAddState').value + " " + document.getElementById('fldAddZip').value;
			arrEncodeUserTryAddresses[1] = document.getElementById('fldAddAddress1').value + "," + document.getElementById('fldAddCity').value + "," + document.getElementById('fldAddState').value + " " + document.getElementById('fldAddZip').value;
			arrEncodeUserTryAddresses[2] = document.getElementById('fldAddAddress2').value + "," + document.getElementById('fldAddCity').value + "," + document.getElementById('fldAddState').value + " " + document.getElementById('fldAddZip').value;
			arrEncodeUserTryAddresses[3] = document.getElementById('fldAddCity').value + "," + document.getElementById('fldAddState').value + " " + document.getElementById('fldAddZip').value;
				
			encodeThenAddUserAddress(0);
		}
			
	}
		
	function processTPMemberAddXMLData(msg) {
		var objXML;
				
		objXML = msg.documentElement;
		try {
			intStatus = parseInt(objXML.getElementsByTagName('status')[0].firstChild.data);
			intMessage = parseInt(objXML.getElementsByTagName('message')[0].firstChild.data);
			if (intStatus == 1){
				//This code is also used on the events page and possibly member pages so check if travel planner window and map exist first
				if (typeof getTravelPlannerListUsingXML == 'function' && typeof resetTravelPlannerMarkers == 'function'){			
					resetTravelPlannerMarkers();
					getTravelPlannerListUsingXML();				
				//alert("update map view here. Reloading for now.");
				//window.location.reload(true);
				}
			} else {
				alert(intMessage);
			}
			
		} catch (e) {
			alert(e);
		}
	}


	//********************************************
	//*
	//********************************************
	var intEncodeThenAddAddressCnt = 0;
	function encodeThenAddUserAddress(index) {
		//Get the lat and long. When done, call back function is called with valid/invalid point
		intEncodeThenAddAddressCnt = index;
		var geocoder = new GClientGeocoder();
		geocoder.getLatLng(arrEncodeUserTryAddresses[index], encodeThenAddUserAddressCallBack);
	}
	
	//********************************************
	//*
	//********************************************
	function encodeThenAddUserAddressCallBack(point){
		if (!point) {
			if (intEncodeThenAddAddressCnt == arrEncodeUserTryAddresses.length){
				//No more addresses to try, use the visitors center
				alert("Unable to map supplied address. Use a valid street address.");
			} else {
				//Try the next address
				intEncodeThenAddAddressCnt++;
				encodeThenAddUserAddress(intEncodeThenAddAddressCnt);
			}
		} else {
			//Point Found
			//update the add url then call our function to add the address	
			TPAddUrl += "&lat=" + point.lat() + "&lng=" + point.lng();
			//alert('adding address\n' + TPAddUrl);
			
			$.ajax({
				type: "POST",
				url: TPAddUrl,
				data: "",//"name=John&location=Boston",
				success: processTPMemberAddXMLData
			 });
		}
	}
