﻿// ------------------------------------------------------
// Global variables
// ------------------------------------------------------
var m_bFarTravels = null;						// Boolean that indicated if only far travels should be shown
var m_sSelectedCountryID = "";					// Variable that will hold the currently selected CountryID
var m_sSelectedRegionID = "";					// Variable that will hold the currently selected RegionID
var m_sSelectedPlaceID = "";					// Variable that will hold the currently selected PlaceID
var m_sSelectedAccommodationTypeID = "";		// Variable that will hold the currently selected AccommodationTypeID
var m_sSelectedPeriodID = "";					// Variable that will hold the currently selected PeriodID
// ------------------------------------------------------
// Initializes the Search and book form
// ------------------------------------------------------
function InitSearchAndBookForm(){	
	// Reset all variables with selected values
	m_sSelectedCountryID = "";
	m_sSelectedRegionID = "";
	m_sSelectedPlaceID = "";
	m_sSelectedAccommodationTypeID = "";
	m_sSelectedPeriodID = "";
	// Disable all dropdownlists except for countries
	DisableDropDownList("DropDownListRegions");
	DisableDropDownList("DropDownListPlaces");
	DisableDropDownList("DropDownListAccommodationTypes");
	DisableDropDownList("DropDownListPeriods");
	// Fill country dropdownlist
	FillCountries();
}
// ------------------------------------------------------
// Function that fires when the user changes a dropdownlist selection
// ------------------------------------------------------
function ActOnChange(bResetAllSelectionsButCountry){
	if (bResetAllSelectionsButCountry){
		// Read selected country
		SaveCurrentSelectedCountryID();		
		// Reset the current other values to the initial empty value
		m_sSelectedRegionID = "";
		m_sSelectedPlaceID = "";
		m_sSelectedAccommodationTypeID = "";
		m_sSelectedPeriodID = "";
	}
	else{
		// Save all currently selected values, because when we disable them we cannot read the value 
		// so we have to do this first
		SaveAllCurrentSelectedValues();
	}
	// Disable all dropdownlists except for countries
	DisableDropDownList("DropDownListRegions");
	DisableDropDownList("DropDownListPlaces");
	DisableDropDownList("DropDownListAccommodationTypes");
	DisableDropDownList("DropDownListPeriods");	
    
	if (m_sSelectedCountryID.length > 0){
		// Country selected, so start filling other dropdownlists
		FillRegions();
	}
}
// ------------------------------------------------------
// Function that fires when the user clicks the submit button
// ------------------------------------------------------
function ActOnSubmit(){
	// Get the travel code input element
	var oTravelCodeElement = GetElement('m_oTextBoxBookCode', 'input');
	if (oTravelCodeElement.value.length == 0){
		// No travelcode supplied, check the required dropdownlists
		SaveAllCurrentSelectedValues();
		
		if (m_sSelectedCountryID.length == 0){
			// No country selected, show message to user
			alert('Kies a.u.b. een land.');
			return false;
		}
		else{
			return true;
		}
	}
	else{
		// Travelcode supplied, no need to check dropdownlists
		return true;
	}
}
// ------------------------------------------------------
// Fills the countries dropdownlist
// ------------------------------------------------------
function FillCountries (){
	// Prepare dropdownlist countries for filling
	PrepareDropDownListForFilling('DropDownListCountries');
	// Call WebService, and set callback function
	SearchAndBookFormWebService.GetCountries(m_bFarTravels, FillCountriesCallBack);
}
// ------------------------------------------------------
// Fills the regions dropdownlist
// ------------------------------------------------------
function FillRegions (){
	// Prepare dropdownlist countries for filling
	PrepareDropDownListForFilling('DropDownListRegions');
	// Strong-type criteria values (selections from other dropdownlists)	
	var iCountryID = GetIntegerOrNull(m_sSelectedCountryID);
	var iAccommodationTypeID = GetIntegerOrNull(m_sSelectedAccommodationTypeID);
	var iPeriodID = GetIntegerOrNull(m_sSelectedPeriodID);
	// Call WebService, and set callback function
	SearchAndBookFormWebService.GetRegions(
		iCountryID,
		iAccommodationTypeID,
		iPeriodID,
		m_bFarTravels, 		
		FillRegionsCallBack
	);
}
// ------------------------------------------------------
// Fills the places dropdownlist
// ------------------------------------------------------
function FillPlaces (){
	// Prepare dropdownlist countries for filling
	PrepareDropDownListForFilling('DropDownListPlaces');
	// Strong-type criteria values (selections from other dropdownlists)
	var iCountryID = GetIntegerOrNull(m_sSelectedCountryID);
	var iRegionID = GetIntegerOrNull(m_sSelectedRegionID);
	var iAccommodationTypeID = GetIntegerOrNull(m_sSelectedAccommodationTypeID);
	var iPeriodID = GetIntegerOrNull(m_sSelectedPeriodID);
	// Call WebService, and set callback function
	SearchAndBookFormWebService.GetPlaces(
		iCountryID,
		iRegionID,
		iAccommodationTypeID,
		iPeriodID,
		m_bFarTravels, 		
		FillPlacesCallBack
	);
}
// ------------------------------------------------------
// Fills the accommodation types dropdownlist
// ------------------------------------------------------
function FillAccommodationTypes (){
	// Prepare dropdownlist countries for filling
	PrepareDropDownListForFilling('DropDownListAccommodationTypes');
	// Strong-type criteria values (selections from other dropdownlists)
	var iCountryID = GetIntegerOrNull(m_sSelectedCountryID);
	var iRegionID = GetIntegerOrNull(m_sSelectedRegionID);
	var iPlaceID = GetIntegerOrNull(m_sSelectedPlaceID);
	var iPeriodID = GetIntegerOrNull(m_sSelectedPeriodID);
	// Call WebService, and set callback function
	SearchAndBookFormWebService.GetAccommodationTypes(
		iCountryID,
		iRegionID,
		iPlaceID,
		iPeriodID,
		m_bFarTravels, 		
		FillAccommodationTypesCallBack
	);
}
// ------------------------------------------------------
// Fills the periods dropdownlist
// ------------------------------------------------------
function FillPeriods (){
	// Prepare dropdownlist countries for filling
	PrepareDropDownListForFilling('DropDownListPeriods');
	// Strong-type criteria values (selections from other dropdownlists)
	var iCountryID = GetIntegerOrNull(m_sSelectedCountryID);
	var iRegionID = GetIntegerOrNull(m_sSelectedRegionID);
	var iPlaceID = GetIntegerOrNull(m_sSelectedPlaceID);
	var iAccommodationTypeID = GetIntegerOrNull(m_sSelectedAccommodationTypeID);
	// Call WebService, and set callback function
	SearchAndBookFormWebService.GetPeriods(
		iCountryID,
		iRegionID,
		iPlaceID,
		iAccommodationTypeID,
		m_bFarTravels, 		
		FillPeriodsCallBack
	);
	// Show the number of travel for the current selected values (criteria)
	SetTravelCount();
}
// ------------------------------------------------------
// Shows the number of travels for the selected criteria
// ------------------------------------------------------
function SetTravelCount(){
	// Strong-type criteria values (selections from other dropdownlists)
	var iCountryID = GetIntegerOrNull(m_sSelectedCountryID);
	var iRegionID = GetIntegerOrNull(m_sSelectedRegionID);
	var iPlaceID = GetIntegerOrNull(m_sSelectedPlaceID);
	var iAccommodationTypeID = GetIntegerOrNull(m_sSelectedAccommodationTypeID);
	var iPeriodID = GetIntegerOrNull(m_sSelectedPeriodID);
	// Call WebService, and set callback function
	SearchAndBookFormWebService.GetTravelCount(
		iCountryID,
		iRegionID,
		iPlaceID,
		iAccommodationTypeID,
		iPeriodID,
		m_bFarTravels, 		
		SetTravelCountCallBack
	);
}
// ------------------------------------------------------
// Callback function when the GetCountries WebService method is ready.
// ------------------------------------------------------
function FillCountriesCallBack(result){
	var oSelect = GetElement('DropDownListCountries', 'select');
    // Clear old options
    oSelect.options.length = 0; 
    // Add new options
    oSelect.options[0] = new Option('Kies uw land', '');
    for (var iCount = 0; iCount < result.length; iCount++){        
        oSelect.options[oSelect.options.length] = new Option(result[iCount].name, result[iCount].value);
    }   
    // Select the previous selected option again
    SetSelectedValueDropDownList('DropDownListCountries', m_sSelectedCountryID);
    // Fill ready so enable dropdownlist again
    EnableDropDownList('DropDownListCountries');
    // Update global vaiable with selected value
    m_sSelectedCountryID = GetSelectedValueDropDownList('DropDownListCountries');
}
// ------------------------------------------------------
// Callback function when the GetRegions WebService method is ready.
// ------------------------------------------------------
function FillRegionsCallBack(result){
	var oSelect = GetElement('DropDownListRegions', 'select');
    // Clear old options
    oSelect.options.length = 0; 
    // Add new options
    oSelect.options[0] = new Option('Kies uw streek', '');
    for (var iCount = 0; iCount < result.length; iCount++){        
        oSelect.options[oSelect.options.length] = new Option(result[iCount].name, result[iCount].value);
    }   
    // Select the previous selected option again
    SetSelectedValueDropDownList('DropDownListRegions', m_sSelectedRegionID);
    // Fill ready so enable dropdownlist again
    EnableDropDownList('DropDownListRegions');
    // Update global vaiable with selected value
    m_sSelectedRegionID = GetSelectedValueDropDownList('DropDownListRegions');    
    // Fill all places
    FillPlaces();    
}
// ------------------------------------------------------
// Callback function when the GetPlaces WebService method is ready.
// ------------------------------------------------------
function FillPlacesCallBack(result){
	var oSelect = GetElement('DropDownListPlaces', 'select');
    // Clear old options
    oSelect.options.length = 0; 
    // Add new options
    oSelect.options[0] = new Option('Geen voorkeur', '');
    for (var iCount = 0; iCount < result.length; iCount++){        
        oSelect.options[oSelect.options.length] = new Option(result[iCount].name, result[iCount].value);
    }   
    // Select the previous selected option again
    SetSelectedValueDropDownList('DropDownListPlaces', m_sSelectedPlaceID);
    // Fill ready so enable dropdownlist again
    EnableDropDownList('DropDownListPlaces');
    // Update global vaiable with selected value
    m_sSelectedPlaceID = GetSelectedValueDropDownList('DropDownListPlaces');      
    // Fill all accommodation types
    FillAccommodationTypes();
}
// ------------------------------------------------------
// Callback function when the GetAccommodationTypes WebService method is ready.
// ------------------------------------------------------
function FillAccommodationTypesCallBack(result){
	var oSelect = GetElement('DropDownListAccommodationTypes', 'select');
    // Clear old options
    oSelect.options.length = 0; 
    // Add new options
    oSelect.options[0] = new Option('Geen voorkeur', '');
    for (var iCount = 0; iCount < result.length; iCount++){        
        oSelect.options[oSelect.options.length] = new Option(result[iCount].name, result[iCount].value);
    }   
    // Select the previous selected option again
    SetSelectedValueDropDownList('DropDownListAccommodationTypes', m_sSelectedAccommodationTypeID);
    // Fill ready so enable dropdownlist again
    EnableDropDownList('DropDownListAccommodationTypes');
    // Update global vaiable with selected value
    m_sSelectedAccommodationTypeID = GetSelectedValueDropDownList('DropDownListAccommodationTypes');     
    // Fill all periods
    FillPeriods();
}
// ------------------------------------------------------
// Callback function when the GetPeriods WebService method is ready.
// ------------------------------------------------------
function FillPeriodsCallBack(result){
	var oSelect = GetElement('DropDownListPeriods', 'select');
    // Clear old options
    oSelect.options.length = 0; 
    // Add new options
    for (var iCount = 0; iCount < result.length; iCount++){        
        oSelect.options[oSelect.options.length] = new Option(result[iCount].name, result[iCount].value);
    }   
    // Select the previous selected option again
    SetSelectedValueDropDownList('DropDownListPeriods', m_sSelectedPeriodID);
    // Fill ready so enable dropdownlist again
    EnableDropDownList('DropDownListPeriods');
    // Update global vaiable with selected value
    m_sSelectedPeriodID = GetSelectedValueDropDownList('DropDownListPeriods');
}
// ------------------------------------------------------
// Callback function when the GetTravelCount WebService method is ready.
// ------------------------------------------------------
function SetTravelCountCallBack(result){
	var oTextSpan = GetElement('LabelTravelCount', 'span');
	if (result == "1"){
		oTextSpan.innerText = 'Er is ' + result + ' vakantie gevonden';	
	}
	else{
		oTextSpan.innerText = 'Er zijn ' + result + ' vakanties gevonden';	
	}
}
// ------------------------------------------------------ 
// Set all the currently selected values in the corresponding global variables
// ------------------------------------------------------ 
function SaveAllCurrentSelectedValues(){
	SaveCurrentSelectedCountryID();
	SaveCurrentSelectedRegionID();
	SaveCurrentSelectedPlaceID();
	SaveCurrentSelectedAccommodationTypeID();
	SaveCurrentSelectedPeriodID();
}
// ------------------------------------------------------ 
// Sets the global variable that holds the currently selected CountryID
// ------------------------------------------------------ 
function SaveCurrentSelectedCountryID(){
	m_sSelectedCountryID = GetSelectedValueDropDownList('DropDownListCountries');
}
// ------------------------------------------------------ 
// Sets the global variable that holds the currently selected Region
// ------------------------------------------------------ 
function SaveCurrentSelectedRegionID(){
	m_sSelectedRegionID = GetSelectedValueDropDownList('DropDownListRegions');
}
// ------------------------------------------------------ 
// Sets the global variable that holds the currently selected Place
// ------------------------------------------------------ 
function SaveCurrentSelectedPlaceID(){
	m_sSelectedPlaceID = GetSelectedValueDropDownList('DropDownListPlaces');
}
// ------------------------------------------------------ 
// Sets the global variable that holds the currently selected AccommodationType
// ------------------------------------------------------ 
function SaveCurrentSelectedAccommodationTypeID(){
	m_sSelectedAccommodationTypeID = GetSelectedValueDropDownList('DropDownListAccommodationTypes');
}
// ------------------------------------------------------ 
// Sets the global variable that holds the currently selected Period
// ------------------------------------------------------ 
function SaveCurrentSelectedPeriodID(){
	m_sSelectedPeriodID = GetSelectedValueDropDownList('DropDownListPeriods');
}
// ------------------------------------------------------ 
// Prepares a dropdownlist for filling.
// Clears all the options, add's a message that
// data is being retrieved and disables the dropdownlist
// ------------------------------------------------------
function PrepareDropDownListForFilling(sDropDownListID){
	var oSelect = GetElement(sDropDownListID, 'select');
	// Clear old options
    oSelect.options.length = 0; 
    oSelect.options[0] = new Option('Bezig met laden...', '');
    oSelect.disabled = true;
}
// ------------------------------------------------------ 
// Returns a element by name (ID)
// ------------------------------------------------------ 
function GetElement(sElementID, sElementType){
	var oElement;	
    var oElements = document.getElementsByTagName(sElementType);
	for (nCnt = 0; (nCnt < oElements.length); nCnt++){
	    if (oElements[nCnt].id){
	        var sFld = oElements[nCnt].id;
	        if (sFld.indexOf(sElementID) != -1){
	            oElement = oElements[nCnt];
	            break;
	        }
	    }
	}
	if (!oElement){
		// Element not found, show message
		alert('Er is een fout opgetreden:\nElement niet gevonden.');
	}
	return oElement;
}
// ------------------------------------------------------ 
// Disables a dropdownlist
// ------------------------------------------------------
function DisableDropDownList(sDropDownListID){
	var oSelect = GetElement(sDropDownListID, 'select');
    oSelect.options.length = 0; 	
    oSelect.disabled = true;
}
// ------------------------------------------------------ 
// Enables a dropdownlist
// ------------------------------------------------------
function EnableDropDownList(sDropDownListID){
    var oSelect = GetElement(sDropDownListID, 'select');          
    oSelect.disabled = false;
}
// ------------------------------------------------------
// Return null if the value is empty, otherwise it returns the given value as integer
// ------------------------------------------------------
function GetIntegerOrNull(sSelectedValue){
	if (sSelectedValue.length == 0) {
		return null;
	}
	else {
		return parseInt(sSelectedValue);
	}  
}
// ------------------------------------------------------ 
// Returns the selected value of a dropdownlist
// ------------------------------------------------------
function GetSelectedValueDropDownList(sDropDownListID){
	var sSelectedValue = "";
	var oSelect = GetElement(sDropDownListID, 'select');
	for (var iCnt = 0; iCnt < oSelect.options.length; iCnt++){
		if (oSelect.options[iCnt].selected){
			sSelectedValue = oSelect.options[iCnt].value;
		}
	}
	return sSelectedValue;
}
// ------------------------------------------------------ 
// Selected the correct option in a dropdownlist based on value
// ------------------------------------------------------
function SetSelectedValueDropDownList(sDropDownListID, sSelectedValue){
	var oSelect = GetElement(sDropDownListID, 'select');
	for (var iCnt = 0; iCnt < oSelect.options.length; iCnt++){
		if (oSelect.options[iCnt].value == sSelectedValue){
			oSelect.options[iCnt].selected = true;
			break;
		}
	}
}