/*	SEARCHHELPER.JS
//	This file contains functions that are called when performing searches from the client side. They
//	send http requests to a proxy page which then sends back Jscript to get eval'd on the client side.
*/
// Object for handling HTTP requests to the VE Search aspx page
var xmlhttp=false;

// Variables used to pass to the VESearch aspx page for performing the MWS proximity searches
var city = "";
var statePar = "";
var country = "";

/* This function is used to initialize the xmlHttp object */
function InitXmlHttp() {
	// Attempt to initialize xmlhttp object
	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		// Try to use different activex object
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E)
		{
			xmlhttp = false;
		}
	}
  // If not initialized, create XMLHttpRequest object
	if (!xmlhttp && typeof XMLHttpRequest!='undefined')
	{	
		xmlhttp = new XMLHttpRequest();
	}
	
	// Define function call for when Request obj state has changed
	xmlhttp.onreadystatechange=searchHandler;
}

/* Initializes search Parameter values from input fields */
function InitSearchParams() {
	city = document.getElementById("txtCity").value;
	stateElem = document.getElementById( "listStates" );
	statePar = stateElem.options[ stateElem.selectedIndex ].value;
	
	countryElem = document.getElementById( "listCountry" );
	country = countryElem.options[ countryElem.selectedIndex ].value;
}

function RequiredFields() {
	city = document.getElementById("txtCity").value;
	if( city == "" ) {
		return false;
	}
	return true;
}

function GeocodeTrip( originSet, destSet ) {
	InitXmlHttp();
	if( originSet && destSet ) {
		var parms = "bothEndPoints=true&origLat="+routeStart.Latitude+"&origLon="+routeStart.Longitude+"&destLat="+routeEnd.Latitude+"&destLon="+routeEnd.Longitude+"&countryDS="+countryDS;
		xmlhttp.open("GET","MWSGeocode.aspx?"+parms,true );
		xmlhttp.send(null);
		return;
	}
	var originStreet = document.getElementById("controlAddressOrigin_txtStreet").value;
	var originAd3 = document.getElementById("controlAddressOrigin_txtAd3").value;
	var listAirportOrigin = document.getElementById( "controlAddressOrigin_selectAirport" );
	
	var destStreet = document.getElementById("controlAddressDestination_txtStreet").value;
	var destAd3 = document.getElementById("controlAddressDestination_txtAd3").value;
	var listAirportDest = document.getElementById( "controlAddressDestination_selectAirport" );

	if( ((originStreet == "" && originAd3 == "") && (listAirportOrigin.selectedIndex == 0) && originSet == false)
			|| ((destStreet == "" && destAd3 == "" ) && (listAirportDest.selectedIndex == 0)) ) {

		// Show required fields message
		return;
	}
	
	var listCountryOrigin = document.getElementById( "controlAddressOrigin_listCountryCompact" );
	var originCountry = listCountryOrigin.options[ listCountryOrigin.selectedIndex ].value;
	var originAirport = listAirportOrigin.options[ listAirportOrigin.selectedIndex ].value;

	var listCountryDest = document.getElementById( "controlAddressDestination_listCountryCompact" );
	var destCountry = listCountryDest.options[ listCountryDest.selectedIndex ].value;
	var destAirport = listAirportDest.options[ listAirportDest.selectedIndex ].value;
	
	var originParams = "";
	if( originSet == false ) {
		originParams = "airport_o="+originAirport+"&street_o="+originStreet+"&ad3_o="+originAd3+"&country_o="+originCountry;
	}
	else
	{
		originParams = "originFound=true&origLat="+routeStart.Latitude+"&origLon="+routeStart.Longitude+"&countryDS="+countryDS;
	}
	var destParams = "airport_d="+destAirport+"&street_d="+destStreet+"&ad3_d="+destAd3+"&country_d="+destCountry;
	
	InitXmlHttp();
	xmlhttp.open("GET","MWSGeocode.aspx?"+originParams+"&"+destParams,true );
	xmlhttp.send(null);
}

function ShowLandmarks( latitude, longitude )
{
	InitXmlHttp();
	xmlhttp.open("GET","MWSLandmarkSearch.aspx?origLat="+latitude+"&origLon="+longitude,true );
	xmlhttp.send(null);
}

/* Performs a Search based on entered search fields */
function DoFind()
{
	if( !RequiredFields() ) {
		ShowNoAddressFound();
		return;
	}
	// Initialize xmlHttp object
	InitXmlHttp();
	// Get the values of Address input boxes
	InitSearchParams();
	// Set the criteria value(category)
	var criteria = "Hotel Search: 1";// + Msn.VE.API.Globals.vecurrentsearchindex;

	// Send the search request
	xmlhttp.open("GET","MWSSearch.aspx?city="+city+"&state="+statePar+"&country="+country+"&criteria="+criteria+"&searchIndex="+1,true);
	xmlhttp.send(null);
}

function DoFindBIDs( bids ) {
	// Initialize xmlHttp object
	InitXmlHttp();
	// Set the criteria value(category)
	var criteria = "Hotel Search: 1"; //+ Msn.VE.API.Globals.vecurrentsearchindex;
	// Send the search request
	xmlhttp.open("GET","MWSSearch.aspx?BID="+bids,true);
	xmlhttp.send(null);
}

function ShowManeuvers( show ) {
	if( show )
	{
		document.getElementById( "showManeuvers" ).style.visibility = "hidden";
		document.getElementById( "showManeuvers" ).style.display = "none";
		document.getElementById( "maneuverHeader" ).style.visibility = "visible";
		document.getElementById( "maneuverHeader" ).style.display = "";
		document.getElementById( "showTextDirections" ).style.visibility = "visible";
		document.getElementById( "showTextDirections" ).style.display = "";
	}
	else
	{
		document.getElementById( "showManeuvers" ).style.visibility = "visible";
		document.getElementById( "showManeuvers" ).style.display = "";
		document.getElementById( "maneuverHeader" ).style.visibility = "hidden";
		document.getElementById( "maneuverHeader" ).style.display = "none";
		document.getElementById( "showTextDirections" ).style.visibility = "hidden";
		document.getElementById( "showTextDirections" ).style.display = "none";
	}
	onGotRoute( currRoute );
}

function ShowManeuversDriveResults( show ) {
	if( show )
	{
		document.getElementById( "showManeuvers" ).style.visibility = "hidden";
		document.getElementById( "showManeuvers" ).style.display = "none";
		document.getElementById( "maneuverHeader" ).style.visibility = "visible";
		document.getElementById( "maneuverHeader" ).style.display = "";
		document.getElementById( "showTextDirections" ).style.visibility = "visible";
		document.getElementById( "showTextDirections" ).style.display = "";
	}
	else
	{
		document.getElementById( "showManeuvers" ).style.visibility = "visible";
		document.getElementById( "showManeuvers" ).style.display = "";
		document.getElementById( "maneuverHeader" ).style.visibility = "hidden";
		document.getElementById( "maneuverHeader" ).style.display = "none";
		document.getElementById( "showTextDirections" ).style.visibility = "hidden";
		document.getElementById( "showTextDirections" ).style.display = "none";
	}
	onGotRouteDriveResults( currRoute );
}

function GetLocationsAroundRoute( ) {
	DeleteHotels();
	
	// Initialize xmlHttp object
	InitXmlHttp();
	var startLatLong = "startLat="+routeStart.Latitude+"&startLon="+routeStart.Longitude;
	var endLatLong = "endLat="+routeEnd.Latitude+"&endLon="+routeEnd.Longitude;
	var radiusItem = document.getElementById( "selectRadius" );
	var radiusVal = radiusItem.options[ radiusItem.selectedIndex ].text;
	var distanceUnit = document.getElementById( "radioMiles" ).value;
	if( document.getElementById( "radioKms" ).checked ) {
		distanceUnit = document.getElementById( "radioKms" ).value;
	}
	// Send the search request
	xmlhttp.open("GET","MWSFindNearRoute.aspx?"+startLatLong+"&"+endLatLong+"&radius="+radiusVal+"&distanceUnit="+distanceUnit+"&countryDS="+countryDS,true);
	xmlhttp.send(null);
}

/* Handles and evals the Response from VESearch aspx page */
function searchHandler()
{
	if (xmlhttp.readyState==4)
	{
		try {
			eval( xmlhttp.responseText );
		} catch( E ) {//ShowNoResultsTrip( "Sorry, we couldn't find the specified address. Please check it and try again." );
		}
	}
}