/*
* Function to geocode an MQAddress object and return the MQGeoAddress object.
* If Ambiguous results are found, the ambiguous results are submitted to the provided page.
* Pre-Condtion: The address must be a MQGeoAddress object, the html from element must exist.
* Post-Condition: The MQAddress object passed is geocoded and location returned.
* Ambiguous results are passed to the appropriate page specified in multiResultsHTMLElementParentId.
*
* @param        addr - an MQAddress object
* @param        frm - id of the html form element providing the MQAddress object
* @param        multiResultsHTMLElementParentId - frmid for the Ambiguous results to be passed to.
* @param		multiResultsSubmtPage - page for the geocoded results to be submitted to when ambiguous.
* @return
* @author       Seisan Consulting   2-16-2007
*/
function geocode(addr, frm, multiResultsHTMLElementParentId, multiResultsSubmtPage){
	var locationcollection = new MQLocationCollection();
	var geoExec = new MQExec(geocodeServer, serverPath, serverPort, proxyServer, proxyPath, proxyPort);
	//Geocode address
	geoExec.geocode(addr, locationcollection, null);

	if(locationcollection.getSize()==0){
		//If results are 0 then the address can not be geocoded.
		alert("Address couldn't be geocoded!");
		return false;
	}
	else if(locationcollection.getSize()==1){
		//If a single result is returned from the geocoder, validate the result, return the geocoded address
		var location = locationcollection.getAt(0);;
		if(validateResultCode(location.getResultCode())){
			return location;
		}
		else {
			alert("Address couldn't be geocoded!");
			return false;
		}
	}
	else {
		//Otherwise multiple results were found and create a ambiguous results table on the specified page.
		if(multiResultsHTMLElementParentId){
			buildGeocodeResultTable(multiResultsHTMLElementParentId, locationcollection, multiResultsSubmtPage, getAdditionalParameters(frm));
		}
		return false;
	}

}

/*
* Function which handles the form submissions depending on the type of search requested.
* Pre-Condtion: the html form element must exist.
* Post-Condition: The approriate action is performed and the form is submitted to the corresponding page based on the search type.
*
* @param        frmid - id of the html form element being submitted.
* @return		a Boolean that evaluates whether the form was submitted or not. True if form submission was successful, false if not.
* @author       Seisan Consulting   2-16-2007
*/
function onFormSubmit(frmid){
	var frm = document.getElementById(frmid);
	var addr, geoAddress, name, city, state, province, postalcode,country;
	
			var city = frm.txtOriginCity.value;
			var state = frm.selOriginStateProvince.options[frm.selOriginStateProvince.selectedIndex].value;
			var postalcode = frm.txtOriginPostalCode.value;
			var country=frm.selOrigCountry.options[frm.selOrigCountry.selectedIndex].value;
			if((StringFunctions.isBlank(city) ||  StringFunctions.isBlank(state) || StringFunctions.isBlank(country)) && StringFunctions.isBlank(postalcode)){
				alert("Origin City, State, Country or Zip required!")
				return false;
			}

			var city = frm.txtDestCity.value;
			var state = frm.selDestStateProvince.options[frm.selDestStateProvince.selectedIndex].value;
			var postalcode = frm.txtDestPostalCode.value;
			var country=frm.selDestCountry.options[frm.selDestCountry.selectedIndex].value;
			if((StringFunctions.isBlank(city) ||  StringFunctions.isBlank(state)|| StringFunctions.isBlank(country)) && StringFunctions.isBlank(postalcode)){
				alert("Destination City, State, Country or Zip required!")
				return false;
			}

			frm.submit();
			return true;
	
	}
	//If this function has geocoded an address either by location or by poi category then assign the latitude and longitude to the hidden form fields for those values.
	


/*
* Function to read hidden form elements containing address information and build an array matrix of fieldnames and values.
* Pre-Condtion: The html form element must exist.
* Post-Condition: An array is built containing a matrix of field name-value pairs of address information.
*
* @param        frm - an html form element
* @return		params - an array matrix of address information in the form of field name-value pairs.
* @author       Seisan Consulting   2-16-2007
*/
function getAdditionalParameters(frm){
	var params = new Array();
	switch(frm.hdnType.value){
		case "ByLocation":
			params.push(new Array("txtAddress", frm.txtAddress.value));
			params.push(new Array("txtCity", frm.txtCity.value));
			params.push(new Array("selStateProvince", frm.selStateProvince.options[frm.selStateProvince.selectedIndex].value));
			params.push(new Array("txtPostalCode", frm.txtPostalCode.value));
			//params.push(new Array("selDisplay", frm.selDisplay.options[frm.selDisplay.selectedIndex].value));
			params.push(new Array("txtDistance", frm.txtDistance.value));
			if(document.getElementById("rdoUnitMi").checked){
				params.push(new Array("rdoUnit", "mi"));
			}
			else {
				params.push(new Array("rdoUnit", "min"));
			}
			params.push(new Array("hdnType", "ByLocation"));
			break;
		case "ByPOICategory":
			params.push(new Array("selCategory", frm.selCategory.options[frm.selCategory.selectedIndex].value));
			params.push(new Array("hdnType", "ByPOICategory"));
			break;
	}
	return params;
}

/*
* Function to assign hidden form fields containing Latitude and Longitude from the geocoded address.
* Pre-Condtion: The html form element must exist.
* Post-Condition: The hidden form fields for Latitude and Longitude will be assigned from the Geocoded Address.
*
* @param        frm - an html form element
* @param        ga - an MQGeoAddress object
* @author       Seisan Consulting   2-16-2007
*/
function processMap(frm, ga){
	//If the form is of type ByLocation or ByPoiCategory the hidden latitude and longitude
	//fields needs filled in once the address is geocoded.
	switch(frm.hdnType.value){
		case "ByLocation":
		case "ByPOICategory":
			frm.hdnLatitude.value = ga.getMQLatLng().getLatitude();
			frm.hdnLongitude.value = ga.getMQLatLng().getLongitude();
			frm.submit();
			break;
	}
}



