/*
* 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, geosource){
	
	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!");
		document.getElementById("HeaderError").style.display="block";
		document.getElementById("labelResults").style.display="block";
		if (geosource=="zip")
			document.getElementById("labelResults").innerHTML='Zip code not found. Please try again.';
		else
			document.getElementById("labelResults").innerHTML='Address not found. Please try again.';
		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("aqui");
			alert("Address couldn't be geocoded!");*/
			document.getElementById("HeaderError").style.display="block";
			document.getElementById("labelResults").style.display="block";
			document.getElementById("labelResults").innerHTML='Address Not Found. <br>This	location was not found in our database,	please verify the information entered.';
			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 geocodeResults(addr){
	
	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!");
		document.getElementById("HeaderError").style.display="block";
		document.getElementById("labelResults").style.display="block";
		document.getElementById("labelResults").innerHTML='Address Not Found. <br>This	location was not found in our database,	please verify the information entered.';		
		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("aqui");
			alert("Address couldn't be geocoded!");*/
			
			//location.href='PrxInput.aspx';
			//document.getElementById("labelHeader").innerHTML='';
			//document.getElementById("labelResults").innerHTML='Address Not Found. <br>This	location was not found in our database,	please verify the information entered.';
			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 onFormSubmit(frmid, source){
	
	var frm = document.getElementById(frmid);
 var addr, geoAddress, name, city, state, province, postalcode;
 //Hidden field containing search type is read
 //alert("frmid:" + frmid);
 addr = new MQAddress();
 /*addr.setStreet(frm.txtAddress.value);
 addr.setCity(frm.txtCity.value);
 addr.setState(frm.selStateProvince.options[frm.selStateProvince.selectedIndex].value);
 addr.setPostalCode(frm.txtPostalCode.value);*/

 if (source=="zip")
 {
 //if (StringFunctions.isBlank(addr.getPostalCode()))
 if (frm.txtPostalCode.value=="")
 {
	document.getElementById("HeaderError").style.display="block";
	document.getElementById("labelResults").style.display="block";
	document.getElementById("labelResults").innerHTML= 'Zip code not found. Please try again.';
	return false;
 }
 else
 {
 addr.setPostalCode(frm.txtPostalCode.value);
 }
 }
 else
 {
  // if entire address is blank
 if (frm.txtAddress.value=="" && frm.txtCity.value=="" && frm.selStateProvince.options[frm.selStateProvince.selectedIndex].value=="")
 {
	document.getElementById("HeaderError").style.display="block";
	document.getElementById("labelResults").style.display="block";
	document.getElementById("labelResults").innerHTML= 'Address not found. Please try again.';
	return false;
 } 
 // if state is NOT blank and city IS blank
 else if ((frm.txtCity.value=="")&&(frm.selStateProvince.options[frm.selStateProvince.selectedIndex].value!="")) 
 {
	document.getElementById("HeaderError").style.display="block";
	document.getElementById("labelResults").style.display="block";
	document.getElementById("labelResults").innerHTML= 'Please enter both a city and state.';
	return false;

 }
  // if state IS blank and city is NOT blank
  else if ((frm.txtCity.value!="")&&(frm.selStateProvince.options[frm.selStateProvince.selectedIndex].value=="")) 
  {
 	document.getElementById("HeaderError").style.display="block";
 	document.getElementById("labelResults").style.display="block";
 	document.getElementById("labelResults").innerHTML= 'Please enter both a city and state.';
	return false;
 
 }
 else
 {
 addr.setStreet(frm.txtAddress.value);
 addr.setCity(frm.txtCity.value);
 addr.setState(frm.selStateProvince.options[frm.selStateProvince.selectedIndex].value);
 }
 }
 addr.setCountry("US");
 ga = geocode(addr, frm, "divFrmInput", "PrxResults.aspx", source);

//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.
if(ga){
//alert("onFormSubmit");
processMap(frm, ga);
return true;
}
else {
return false;
}
}

/*
* 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("txtMatchesperPage", frm.txtMatchesperPage.options[frm.txtMatchesperPage.selectedIndex].value));
			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.rdoUnit.value="Mi";
			frm.submit();
			break;
	}
}
