
/*
 ***************************************************
 * MAP QUEST
 ***************************************************
 */

		function getInfoTitleHTML(resultId){
			var infoTitleHTML = null;
			try {
				var infoTitleHTMLTemplate = document.getElementById('infoTitleHTMLTemplate_'+resultId);
				infoTitleHTML = infoTitleHTMLTemplate.innerHTML;
			} catch(error){
			}
			return infoTitleHTML;
		}

		function getInfoContentHTML(resultId){
			var infoContentHTML = null;
			try {
				var infoContentHTMLTemplate = document.getElementById('infoContentHTMLTemplate_'+resultId);
				infoContentHTML = infoContentHTMLTemplate.innerHTML;
			} catch(error){
			}
			return infoContentHTML;
		}
		
		function recenterOnClick(e) {
			// To animate the map panning to the new center point, use...
			myMap.panToLatLng(e.ll);
			// To instantly move the map to the new center point, use...
			// myMap.setCenter(e.ll);
		};
		
		function startMapWithWaiting() {
			startMapForHotelsSearch();
			
			//hideAndUnblockElement('waitZoneForMap');
			//displayAndBlockElement('displayableMap');
		}
	
		//Create the base MQA.TileMap object and pass in the id of the DIV you want to hold the map 
		function startMapForHotelsSearch() {
			var lat = document.getElementById('lat');
			var lng = document.getElementById('lng');

			//########## LA MAP
			if (lat != null && lat.value != "" && lng != null && lng.value != "" && document.getElementById('mapWindow') != null) {
				try{
					myMap = new MQA.TileMap(document.getElementById('mapWindow'), 11, new MQA.LatLng(lat.value, lng.value), "map");
				} catch(e){
					
				}
				MQA.EventManager.addListener(myMap, 'dblclick', recenterOnClick);
				// Create a new Large Zoom control
		        myLZControl = new MQA.LargeZoomControl;
				//tell the map to display the control we just attached
		        myMap.addControl(myLZControl);
				//Create a map view type control    
				myVControl = new MQA.ViewControl(myMap);
				// A Shadow
				myMap.setMapShadowState(false);
	
				myHotelCollection = new MQA.ShapeCollection();
				myHotelCollection.Name = "myHotelsCollection";
				
				var containersCount = document.getElementById('containersCount');
				var latLngs = new Array;
				// Les points qui indique les hotels.
				for (var i = 1; i < containersCount.value + 1; i++) {
					var lati = document.getElementById('lat' + i);
					var lngi = document.getElementById('lng' + i);
	
					if (lati != null && lati.value != "" && lngi != null && lngi.value != "") {
						var newCenterLL = new MQA.LatLng(lati.value, lngi.value);
						myPoint = new MQA.Poi(newCenterLL);
						
						latLngs.push(newCenterLL);
						
						//set any rollover and pop-up text
						var infoTitleHTML = getInfoTitleHTML(i);
						if (infoTitleHTML != null){
							myPoint.setValue('infoTitleHTML', infoTitleHTML);
						}
						var infoContentHTML = getInfoContentHTML(i);
						if (infoContentHTML != null){
							myPoint.setValue('infoContentHTML', infoContentHTML);
						}

						// Pour l'image et le chiffre.
						var poiDivi = document.getElementById('poiDivContainer' + i);
						myPoint.setValue('HTMLContent', poiDivi.innerHTML);

						//you can set a Key value to help keep track of the POIs you put on the map
						myPoint.setValue('key', i);
	
						//Add the Hotel to the collection
						myHotelCollection.add(myPoint);
					}
				}
	
				// Les points qui indique le point d'interet
				var poiName = document.getElementById('poiName');
				var poiAddress = document.getElementById('poiAddress');
					
				var newCenterLL = new MQA.LatLng(lat.value, lng.value);
				myMap.setCenter(newCenterLL);
				myPoi = new MQA.Poi(newCenterLL);
				//set any rollover and pop-up text
				if (poiName != null) {
					myPoi.setValue('infoTitleHTML', poiName.value);
				}
				if (poiAddress != null) {
					myPoi.setValue('infoContentHTML', poiAddress.value);
				}
				//you can set a Key value to help keep track of the POIs you put on the map
				myPoi.setValue('key','poi');
				//Add the POI to the collection
				myHotelCollection.add(myPoi);
				
				myMap.bestFitLL(latLngs, false, 5, 10);
				
				myMap.addShapeCollection(myHotelCollection);
				
				// On sauvegarde l'état pour recentrer plus tard.
				myMap.saveState();
			}
			
			return true;
		}

		function reCenterOnPoi(){
			myMap.restoreState();
		}

		function reCenter(poiKey){
			var shapes = myMap.getShapeCollections();
			var colec = shapes[0];
			var poi = colec.getByKey(poiKey);

			poi.showInfoWindow();
		}
		
		//Create the base MQA.TileMap object and pass in the id of the DIV you want to hold the map 
		function startMapForDesc() {
			var lat = document.getElementById('lat');
			var lng = document.getElementById('lng');
			var hName = document.getElementById('hName');
			var hAddress = document.getElementById('hAddress');

			//########## LA MAP
			if (lat != null && lat.value != "" && lng != null && lng.value != "") {
				myMapDesc = new MQA.TileMap(document.getElementById('mapDesc'), 12, new MQA.LatLng(lat.value, lng.value), "map");
				
				MQA.EventManager.addListener(myMapDesc, 'dblclick', recenterOnClick);
				// Create a new Large Zoom control
	            myLZControl = new MQA.LargeZoomControl;
				//tell the map to display the control we just attached
	            myMapDesc.addControl(myLZControl);
				//Create a map view type control    
				//myVControl = new MQA.ViewControl(myMapDesc);
				/*    
				Display control - use the Corner placement constant to specify 
				which corner, plus an MQA.Size object to specify X and Y "indent" 
				offset
				*/    
				//myMapDesc.addControl(myVControl, new MQA.MapCornerPlacement(MQA.MapCorner.BOTTOM_RIGHT, new MQA.Size(20,20)));
				// A Shadow
				myMapDesc.setMapShadowState(false);
	
				// Le point qui indique l'hotel.
				//Create a new MQA.Poi object and set its location
				var newCenterLL = new MQA.LatLng(lat.value, lng.value);
				myMapDesc.setCenter(newCenterLL);
				myPoint = new MQA.Poi(newCenterLL);
				//set any rollover and pop-up text
				myPoint.setValue('infoTitleHTML', hName.value);
				myPoint.setValue('infoContentHTML', hAddress.value);
				//you can set a Key value to help keep track of the POIs you put on the map
				myPoint.setValue('key','1A');
				//Add the POI to the map
				myMapDesc.addShape(myPoint);
			}
		}

/*
 ***************************************************
 * navigation dans les onglets type de recherche
 ***************************************************
 */
function showSearchTypeTab(tabName){
	reinitialise();
	if (tabName == "formulaireCityAndPoi"){
		//affichage / masquage
		showElement('formulaireCityAndPoi');
		hideElement('formulaireAdresse');
		
		//mise à jour style
		changeElementCssName("cityandpoitab", "searchtypetabactive");
		changeElementCssName("addresstab", "searchtypetabdisabled");
		
		//mise à jour champ searchType
		setValue("searchType", SEARCH_ACCOMS_WITH_CODES);
		
	} else if (tabName == "formulaireAdresse"){
		//affichage / masquage
		hideElement('formulaireCityAndPoi');
		showElement('formulaireAdresse');
		
		//mise à jour style
		changeElementCssName("cityandpoitab", "searchtypetabdisabled");
		changeElementCssName("addresstab", "searchtypetabactive");
		
		//mise à jour champ searchType
		setValue("searchType", SEARCH_ACCOMS_WITH_ADDRESS);
	}
}


var cityTabInnerHtml ="<li class='city-active'>"+
"<div>Ville ou point d'int&eacute;r&ecirc;t</div>"+
"</li>"+
"<li class='address-inactive address'>"+
"<a href='javascript:showAddressView();'>Adresse</a>"+
"</li>";

var addressTabInnerHtml ="<li class='city-inactive'>"+
"<a href='javascript:showCityView();'>Ville ou point d'int&eacute;r&ecirc;t</a>"+
"</li>"+
"<li class='address-active address'>"+
"<div>Adresse</div>"+
"</li>";

var leftCityTabInnerHtml ="<li class='city-active'>"+
"<div>Ville / int&eacute;r&ecirc;t</div>"+
"</li>"+
"<li class='address-inactive'>"+
"<a href='javascript:showLeftAddressView();'>Adresse</a>"+
"</li>";

var leftAddressTabInnerHtml ="<li class='city-inactive'>"+
"<a href='javascript:showLeftCityView();'>Ville / int&eacute;r&ecirc;t</a>"+
"</li>"+
"<li class='address-active'>"+
"<div>Adresse</div>"+
"</li>";



function showGenericAddressView(){
	reinitialise();
	
	//affichage / masquage
	hideElement('formulaireCityAndPoi');
	showElement('formulaireAdresse');
	
	//mise à jour champ searchType
	setValue("searchType", SEARCH_ACCOMS_WITH_ADDRESS);
	
}

function showAddressView(){
	showGenericAddressView();
	
	var ulElem = document.getElementById("search-tabs-ul");
	ulElem.innerHTML = addressTabInnerHtml;
}

function showLeftAddressView(){
	showGenericAddressView();
	
	var ulElem = document.getElementById("search-tabs-ul");
	ulElem.innerHTML = leftAddressTabInnerHtml;
}

function showLeftAddressViewWithoutInitialization(){
	//affichage / masquage
	hideElement('formulaireCityAndPoi');
	showElement('formulaireAdresse');
	
	//mise à jour champ searchType
	setValue("searchType", SEARCH_ACCOMS_WITH_ADDRESS);
	
	var ulElem = document.getElementById("search-tabs-ul");
	ulElem.innerHTML = leftAddressTabInnerHtml;
}

function showGenericCityView(){	
	reinitialise();
	
	//affichage / masquage
	showElement('formulaireCityAndPoi');
	hideElement('formulaireAdresse');
	
	//mise à jour champ searchType
	setValue("searchType", SEARCH_ACCOMS_WITH_CODES);
	
}

function showCityView(){
	
	showGenericCityView();
	
	var ulElem = document.getElementById("search-tabs-ul");
	ulElem.innerHTML = cityTabInnerHtml;
	
}


function showLeftCityView(){
	
	showGenericCityView();
	
	var ulElem = document.getElementById("search-tabs-ul");
	ulElem.innerHTML = leftCityTabInnerHtml;
	
}

function showLeftCityViewWithoutInitialization(){
	//affichage / masquage
	showElement('formulaireCityAndPoi');
	hideElement('formulaireAdresse');
	
	//mise à jour champ searchType
	setValue("searchType", SEARCH_ACCOMS_WITH_CODES);
	
	var ulElem = document.getElementById("search-tabs-ul");
	ulElem.innerHTML = leftCityTabInnerHtml;	
}


/*
 ***************************************************
 * Fonctions utilisées dans la page recherche si on veut plusieurs champ de recherhe.
 ***************************************************
 */
function showMoreCity() {
	var lastCityLine = document.getElementById("nbOfCity").value;
	var maxNbOfPossibleCityJS = document.getElementById("maxNbOfPossibleCity").value;

	if (lastCityLine < maxNbOfPossibleCityJS - 1) {
		lastCityLine++;
		showElement('villePlus' + lastCityLine);
	}
	manageButtonVisibility();
}

function hideMoreCity() {
	var lastCityLine = document.getElementById("nbOfCity").value;

	if (lastCityLine > 0) {
		var code = document.getElementById('code' + lastCityLine);
		var name = document.getElementById('name' + lastCityLine);

		code.value="";
		name.value="";

		hideElement('villePlus' + lastCityLine);
		lastCityLine--;
	}
	manageButtonVisibility();
}


/*
 ***************************************************
 * Fonctions utilisées dans la page results-list.jsp
 ***************************************************
 */
function processTri(sortingMode, sortingDirection){
    var triForm = document.getElementById("triForm");
    var triFormSortingMode = document.getElementById("sortingMode");
    var triFormSortingDirection = document.getElementById("sortingDirection");
    triFormSortingMode.value = sortingMode;
    triFormSortingDirection.value = sortingDirection;
	
	var isListView = triForm.isListView;
	var mapViewStyleVisibility = document.getElementById("mapview").style.visibility;
	if (mapViewStyleVisibility == "visible") {
		isListView.value = false;
	} else {
		isListView.value = true;
	}
	
    triForm.submit();
}

function processFilterForm(){
	showWaitDisplay();
	var hotelsResultsForm = document.getElementById("hotelsResultsForm");
	var isListView = hotelsResultsForm.isListView;
	var mapViewStyleVisibility = document.getElementById("mapview").style.visibility;
	if (mapViewStyleVisibility == "visible") {
		isListView.value = false;
	} else {
		isListView.value = true;
	}
	
	hotelsResultsForm.submit();
}

function processFilterActivation(filterActivated){
	showWaitDisplay();
	var filterActivationForm = document.getElementById("filterActivationForm");
    var filterActivationFormFilterActivated = document.getElementById("filterActive");
    if (filterActivated == "false") {
        filterActivationFormFilterActivated.value = false;
    } else {
        filterActivationFormFilterActivated.value = true;
    }
	
	var isListView = filterActivationForm.isListView;
	var mapViewStyleVisibility = document.getElementById("mapview").style.visibility;
	if (mapViewStyleVisibility == "visible") {
		isListView.value = false;
	} else {
		isListView.value = true;
	}
	
    filterActivationForm.submit();
}

function resetFilterForm(){
    var filterForm = document.getElementById("hotelsResultsForm");
    if (filterForm != null) {
        filterForm.elements.filterHotelName.value = '';
        if (filterForm.elements.filterOnRequest != null) {
            filterForm.elements.filterOnRequest.value = '';
        }
        if (filterForm.elements.filterSupplierCode != null) {
            filterForm.elements.filterSupplierCode.selectedIndex = 0;
        }
        filterForm.elements.filterCategory.selectedIndex = 0;
        filterForm.elements.filterSituation.selectedIndex = 0;
		if (filterForm.elements.filterDistance != null) {
			filterForm.elements.filterDistance.selectedIndex = 0;
		}
        filterForm.elements.filterBudget.selectedIndex = 0;
        filterForm.elements.filterMealPlan.selectedIndex = 0;
        for (var i = 0; i < filterForm.elements.length; i++) {
            if (filterForm.elements[i].id.indexOf("filterFacilities") > -1) {
                var chkBox = filterForm.elements[i];
                chkBox.checked = false;
            }
            if (filterForm.elements[i].id.indexOf("filterOnRequest") > -1) {
                var radioBox = filterForm.elements[i];
                if (radioBox.value == "true") {
                    radioBox.checked = false;
                }
                else {
                    radioBox.checked = true;
                }
            }
        }
        filterForm.razFilter.value = "true";
        filterForm.submit();
    }
}

function refreshResultList(pageId, nbOfResult) {
	var pageForm = document.getElementById("pageForm");
	pageForm.nbOfResultPerPage.value = nbOfResult;
	showWaitDisplay();
    pageForm.submit();
}

function displayPage(pageId) {
    var pageForm = document.getElementById("pageForm");
    pageForm.page.value = pageId;
	showWaitDisplay();
    pageForm.submit();
}

function switchToResult(accomId, newResultId, newSupplierAccomCode){
	var actionLabelInput = "";
	
	var viewType ="";
	if (document.getElementById != null && document.getElementById.listview !=null && document.getElementById.listview.style != null && document.getElementById.listview.style.visibility == "visible"){
		actionLabelInput = document.getElementById("listView_linkAction_" + accomId);
	}else {
		actionLabelInput = document.getElementById("mapView_linkAction_" + accomId);
	}
	
    if (actionLabelInput != null) {
        var actionLabel = actionLabelInput.value;
        if (actionLabel == "show") {
            actionLabelInput.value = "hide";
        }
        else {
            actionLabelInput.value = "show";
        }
        // Retrouve le champ caché du conteneur contenant la valeur du result sélectionné
        var hiddenSelectedResult = document.getElementById("currentlySelectedResultId_" + accomId);
        if (hiddenSelectedResult != null) {
            hiddenSelectedResult.value = newResultId;
            newSupplierAccomCodeInput = document.getElementById("supplierAccomCodeInput_" + newResultId);
            if (newSupplierAccomCodeInput != null) {
                newSupplierAccomCodeInput.value = newSupplierAccomCode;
            }
            
            //refreshContainerHeader(accomId);
            hideAndUnblockElement('lightboxbg');
            hideAndUnblockElement('lightboxbgcontent');
            showWaitDisplay();
        }
    }
}

function showTableRowElement(elementObject){ 
	if (elementObject != null){
		elementObject.style.visibility="visible";
		var appname = window.navigator.appName;
		if (appname == "Microsoft Internet Explorer"){
			elementObject.style.display="block";
		}else{
			elementObject.style.display="table-row";  /* si on met un display-block, l'affichage n'est pas bon dans firefox*/
		}
	}				
}

function showViewTypeResultsTab(tabName){ 
	var form = document.getElementById("pageForm");
	if (form != null) {
		var isListView = form.isListView;
		
		if (tabName == "mapview"){
			//affichage / masquage
			showElement('mapview');
			hideElement('listview');
			showElement('displayableMap');

			showElement('listButtonContainer');
			hideElement('mapButtonContainer');
			
			showElement('aide');
			showElement('recentre');
			
			//mise à jour style
			changeElementCssName("mapviewtab", "resultstabactive");
			changeElementCssName("listviewtab", "resultstabdisabled");
			
			isListView.value="false";
		} else if (tabName == "listview"){
			//affichage / masquage
			hideElement('mapview');
			showElement('listview');
			hideElement('displayableMap');

			hideElement('listButtonContainer');
			showElement('mapButtonContainer');
			
			hideElement('aide');
			hideElement('recentre');
			
			//mise à jour style
			changeElementCssName("mapviewtab", "resultstabdisabled");
			changeElementCssName("listviewtab", "resultstabactive");
			
			isListView.value="true";
		}
	}
}

function switchViewType(tabName){
	var form = document.getElementById("pageForm");
	if (form != null) {
		var isListView = form.isListView;
		
		if (tabName == "mapview"){
			//affichage 
			showElement('mapview');
			showElement('displayableMap');

			showElement('listButtonContainer');
			hideElement('mapButtonContainer');
			
			showElement('aide');
			showElement('recentre');
			
			isListView.value="false";
		} else if (tabName == "listview"){
			//masquage
			hideElement('mapview');
			hideElement('displayableMap');

			hideElement('listButtonContainer');
			showElement('mapButtonContainer');
			
			hideElement('aide');
			hideElement('recentre');
			
			isListView.value="true";
		}
	}
}

function chooseHotel(index, maxResultPerPage){ 
	// Map Quest.
	reCenter(index);
	
	var poii = document.getElementById('poi-div-container' + index);
	poii.className='poi-div-container-selected';
		
	for (i=1; i<=maxResultPerPage; i++) {
		if (i != index) {
			var poij = document.getElementById('poi-div-container' + i);
			if (poij != null) {
				poij.className = 'poi-div-container';
			}
		}
	}
}

function locateHotelOnMap(index, maxResultPerPage){
	var forceMapView = "false";
	var form = document.getElementById("pageForm");
	if (form != null) {
		var isListView = form.isListView;
		if (isListView != null){
			if (isListView.value == "true"){ //il faut passer en mode carte
				switchViewType("mapview");
			}
		}else{
			forceMapView = "true";
		}
	}else{
		forceMapView = "true";
	}
	
	if (forceMapView == "true"){ //pas assez d'éléments pour savoir si on est déjà en mode carte ou pas,
		//dans le doute on force l'affichage
		switchViewType("mapview");
	}
	
	chooseHotel(index, maxResultPerPage);
	
	document.location.href="#displayableMap";
}

// Traitement du bouton "RESERVER"
function launchBooking(accomId){
	// Retrouve le champ caché du conteneur contenant la valeur du result sélectionné
    var hiddenSelectedResult = document.getElementById("currentlySelectedResultId_" + accomId);
    var resultId = hiddenSelectedResult.value;
    var supplierCodeInput = document.getElementById("supplierCodeInput_" + resultId);
    var supplierCode = supplierCodeInput.value.toUpperCase();
    var supplierAccomCodeInput = document.getElementById("supplierAccomCodeInput_" + resultId);
    var supplierAccomCode = supplierAccomCodeInput.value;
    //var bookingLaunchQueryString = "?accomId=" + accomId + "&supplierCode=" + supplierCode + "&resultId=" + resultId + "&supplierAccomCode=" + supplierAccomCode;
    //var bookingLaunchUrl = urlToComplete + bookingLaunchQueryString;
    //document.location.href = bookingLaunchUrl;
    
    var form = document.getElementById("bookingForm");
    form.accomId.value = accomId;
    form.supplierCode.value = supplierCode;
    form.resultId.value = resultId;
    form.supplierAccomCode.value = supplierAccomCode;
    
    //affichage du message d'attente
    setTimeout("showWaitDisplay()", 200); 
    
    form.submit();
}

function displayHotelDesc(accomId) {
	//displayAndBlockElement('lightboxbg');
	//displayAndBlockElement('lightboxbgcontent');
	
	var form = document.getElementById("descForm");
	
	form.actionRequested.value = "GET_DESCRIPTION";
	form.accomId.value = accomId;
	
	var hiddenSelectedResult = document.getElementById("currentlySelectedResultId_" + accomId);
	var resultId = hiddenSelectedResult.value;
	    
	var supplierCodeInput = document.getElementById("supplierCodeInput_" + resultId);
	var supplierCode = supplierCodeInput.value.toUpperCase();
	    
	var supplierAccomCodeInput = document.getElementById("supplierAccomCodeInput_" + resultId);
	var supplierAccomCode = supplierAccomCodeInput.value;
	
	form.resultId.value = resultId;
	form.supplierCode.value = supplierCode;
	form.supplierAccomCode.value = supplierAccomCode;
	
	form.submit();
}

function positionOnCurrentAccom(accomId){
	
	if (accomId != null && accomId.length > 0){
	
		var hrefAccomValue = "accom_"+accomId;
		var pageForm = document.getElementById("pageForm");
	    
		if (pageForm != null){
	    	var isListView = pageForm.isListView;
	    	if (isListView != null){
	    		if (isListView.value == "false"){
	    			hrefAccomValue = "mapView_accom_"+accomId;
	    		}
	    	}
	    }
		
		document.location.href = "#"+hrefAccomValue;
	}
}
