﻿// JScript File

var holidaySearchControls = []

holidaySearch.prototype.constructor = holidaySearch;

holidaySearch.prototype.onRegionSelectChange = function() {
    $("hidHolidaySelectedCountryId").value = 0;
    $("hidHolidaySelectedProvinceId").value = 0;
    $("hidHolidaySelectedLocationId").value = 0;
}

holidaySearch.prototype.setRegionValues = function(inputElement, selectedElement) {

    values = selectedElement.value.evalJSON();
    $("hidHolidaySelectedCountryId").value = values.c;
    $("hidHolidaySelectedProvinceId").value = values.p;
    $("hidHolidaySelectedLocationId").value = values.l;

    this.loadOptions($("hidHolidaySelectedCountryId").value, $("hidHolidaySelectedProvinceId").value, $("hidHolidaySelectedLocationId").value);
} 

holidaySearch.prototype.RoomsRequiredChange = function(){
    holShowRooms();
}

holidaySearch.prototype.ChildChange = function(){
    holShowChildAges();   
}

holidaySearch.prototype.blankEstabId = function(e){
    // Blank estab id if we change the estab name or destination
    this.inpEstabId.value = "";
}

holidaySearch.prototype.loadOptions = function(iCountry, iProvince, iLocationId) {
    var empty;
    var sDeparturePoint = "";
    var sDepartureDate = "";
    var sDuration = "";

    if (this.selDeparturePointList.options.length > 0) {
        sDeparturePoint = this.selDeparturePointList.options[this.selDeparturePointList.selectedIndex].value;
    }

    if (this.selDepartureDateList.options.length > 0) {
        sDepartureDate = this.selDepartureDateList.options[this.selDepartureDateList.selectedIndex].value;
    }

    if (this.selDurationList.options.length > 0) {
        sDuration = this.selDurationList.options[this.selDurationList.selectedIndex].value;
    }

    $("hidHolidaySelectedCountryId").value = iCountry;
    $("hidHolidaySelectedProvinceId").value = iProvince;
    $("hidHolidaySelectedLocationId").value = iLocationId;
    $("hidHolidaySelectedDeparturePoint").value = sDeparturePoint;
    $("hidHolidaySelectedDepartureDate").value = sDepartureDate;
    $("hidHolidaySelectedDuration").value = sDuration;

    loadHolidayOptions(iCountry, iProvince, iLocationId, sDeparturePoint, sDepartureDate, sDuration, this.selDeparturePointList, this.selDepartureDateList, this.selDurationList);

    this.blankEstabId(this);
}

holidaySearch.prototype.DeparturePointChange = function() {
    var iCountry = $("hidHolidaySelectedCountryId").value;
    var iProvinceId = $("hidHolidaySelectedProvinceId").value;
    var iLocationId = $("hidHolidaySelectedLocationId").value;

    sDeparturePoint = this.selDeparturePointList.options[this.selDeparturePointList.selectedIndex].value;

    if (this.selDepartureDateList.options.length > 0) {
        sDepartureDate = this.selDepartureDateList.options[this.selDepartureDateList.selectedIndex].value;
    }

    if (this.selDepartureDateList.options.length > 0) {
        sDepartureDate = this.selDepartureDateList.options[this.selDepartureDateList.selectedIndex].value;
    }

    if (this.selDurationList.options.length > 0) {
        sDuration = this.selDurationList.options[this.selDurationList.selectedIndex].value;
    }

    $("hidHolidaySelectedDeparturePoint").value = sDeparturePoint;
    $("hidHolidaySelectedDepartureDate").value = sDepartureDate;
    $("hidHolidaySelectedDuration").value = sDuration;

    var empty;
    loadHolidayOptions(iCountry, iProvinceId, iLocationId, sDeparturePoint, sDepartureDate, sDuration, empty, this.selDepartureDateList, this.selDurationList);
}

holidaySearch.prototype.DepartureDateChange = function(){
    sDepartureDate = this.selDepartureDateList.value;
    saveHolidaySelection(this);
}

holidaySearch.prototype.DurationChange = function(){
    saveHolidaySelection(this);
}

holidaySearch.prototype.disableOptionList  = function(optionList, text) {
    // Empty the list, add a message option and disable
    if (optionList.options.length == 0) {
        //optionList.options.length = 0;
        optionList.options.add(this.addOption(text, ""));
        optionList.disabled = true;
    }
}

holidaySearch.prototype.addOption = function(text, value){
    var elOptNew = document.createElement('option');
    elOptNew.text = text;
    elOptNew.value = value;
    
    return elOptNew;
}

function saveHolidaySelection(objThis){

    var sDeparturePoint = "";
    var sDepartureDate="";
    var sDuration="";
    
    if(objThis.selDeparturePointList.options.length>0){
        sDeparturePoint = objThis.selDeparturePointList.options[objThis.selDeparturePointList.selectedIndex].value; }
            
    if(objThis.selDepartureDateList.options.length>0){
        sDepartureDate = objThis.selDepartureDateList.options[objThis.selDepartureDateList.selectedIndex].value; }
    
    if(objThis.selDurationList.options.length>0){
        sDuration = objThis.selDurationList.options[objThis.selDurationList.selectedIndex].value; }
           
    $("hidHolidaySelectedDeparturePoint").value = sDeparturePoint;
    $("hidHolidaySelectedDepartureDate").value = sDepartureDate;
    $("hidHolidaySelectedDuration").value = sDuration;
}

function holidaySearch(idDeparturePoint, idDepartureDate, idDuration, idRoomsRequired, idRm1Children, idRm2Children, idRm3Children, idEstabId, idEstabTitle, destinationAutoCompleterVariable) {

    holidaySearchControls[holidaySearchControls.length] = this;

    this.selDeparturePointList = $(idDeparturePoint);
    this.selDepartureDateList = $(idDepartureDate);
    this.selDurationList = $(idDuration);
    
    this.selRoomsRequired = $(idRoomsRequired);
    this.selRoom1Children = $(idRm1Children);
    this.selRoom2Children = $(idRm2Children);
    this.selRoom3Children = $(idRm3Children);
        
	this.selDeparturePointList.observe('change', this.DeparturePointChange.bindAsEventListener(this)); 
	this.selDepartureDateList.observe('change', this.DepartureDateChange.bindAsEventListener(this)); 
	this.selDurationList.observe('change', this.DurationChange.bindAsEventListener(this));
	
	this.selRoomsRequired.observe('change', this.RoomsRequiredChange.bindAsEventListener(this)); 
	this.selRoom1Children.observe('change', this.ChildChange.bindAsEventListener(this)); 
	this.selRoom2Children.observe('change', this.ChildChange.bindAsEventListener(this)); 
	this.selRoom3Children.observe('change', this.ChildChange.bindAsEventListener(this));
	
	this.inpEstabId = $(idEstabId);
	this.inpEstabTitle = $(idEstabTitle);

	this.inpEstabTitle.observe('change', this.blankEstabId.bindAsEventListener(this));

	this.destinationAutoCompleter = window[destinationAutoCompleterVariable];
	this.destinationAutoCompleter.options.onChange = this.onRegionSelectChange.bind(this);
	this.destinationAutoCompleter.options.afterUpdateElement = this.setRegionValues.bind(this);

	this.disableOptionList(this.selDeparturePointList, "[Enter destination above]");
	this.disableOptionList(this.selDepartureDateList, "[Enter destination above]");

}
 
function preInitHolidaySearch(){

  if($("HOL_searchInitialised").value=="0"){
    $("HOL_searchInitialised").value="1";
    return;
   }
  initHolidaySearch()
}

function initHolidaySearch(){

   var countryId = 0;
   
   countryId = $("hidHolidaySelectedCountryId").value; 
   provinceId = $("hidHolidaySelectedProvinceId").value;
   locationId = $("hidHolidaySelectedLocationId").value;
   departurePoint = $("hidHolidaySelectedDeparturePoint").value;
   departureDate = $("hidHolidaySelectedDepartureDate").value;
   duration = $("hidHolidaySelectedDuration").value;

   var empty;
   
   // BD: Don't bother loading dropdowns unless we have a province
   if (provinceId > 0) {
        loadHolidayOptions(countryId, provinceId, locationId, departurePoint, departureDate, duration, objHolidaySearch.selDeparturePointList, objHolidaySearch.selDepartureDateList, objHolidaySearch.selDurationList);    
   }
   holShowRooms();
        
}


// JScript File
var saveDeparturePointList;
var saveDeptartureDateList;
var saveDurationList;
var winHoInt=0;


function loadHolidayOptions(CountryId, ProvinceId, LocationId, DeparturePoint, DepartureDate, Duration, objDeparturePointLst, objDepartureDateLst, objDurationLst) {

    var commandList = "";
    var nothing;

    commandList = "CountryId=" + CountryId + "&";
    commandList += "ProvinceId=" + ProvinceId + "&";
    commandList += "LocationId=" + LocationId + "&";
    commandList += "DeparturePoint=" + DeparturePoint + "&";
    commandList += "DepartureDate=" + DepartureDate + "&";
    commandList += "Duration=" + Duration + "&";

    if (objDeparturePointLst != nothing) {
        commandList += "&LoadDeparturePoints=1";
        setLoad(objDeparturePointLst);
    }

    if (objDepartureDateLst != nothing) {
        commandList += "&LoadDepartureDates=1";
        setLoad(objDepartureDateLst);
    }

    if (objDurationLst != nothing) {
        commandList += "&LoadDurations=1";
        setLoad(objDurationLst);
    }

    commandList += "&Mst=" + mstHolidaySearchType;

    iFrame = $("optLoad")
    if (iFrame.contentWindow.document.getElementById("Complete") != undefined) {
        $("optLoad").contentWindow.document.getElementById("Complete").value = '';
    }

    saveDeparturePointList = objDeparturePointLst;
    saveDepartureDateList = objDepartureDateLst;
    saveDurationList = objDurationLst;

    eval(iFrame.contentWindow.location.replace(prePath + "/Include/DynamicSearchOptions/HolidayOptions.aspx?" + commandList));

    if (winHoInt != 0) { window.clearInterval(winHoInt); }
    winHoInt = window.setInterval("checkHolLoadComp()", 100);
}

function checkHolLoadComp() {

    var undefined;
    var iFrame = $("optLoad")
	if (iFrame.contentWindow.document.getElementById("Complete") != undefined){
		if(iFrame.contentWindow.document.getElementById("Complete").value!=''){
			window.clearInterval(winHoInt);
					
			if(iFrame.contentWindow.document.getElementById("lstDeparturePoints")!=undefined){
				CopyDropDown(iFrame.contentWindow.document.getElementById("lstDeparturePoints"),saveDeparturePointList, $("hidHolidaySelectedDeparturePoint"));}							

			if(iFrame.contentWindow.document.getElementById("lstDepartureDates")!=undefined){
				CopyDropDown(iFrame.contentWindow.document.getElementById("lstDepartureDates"),saveDepartureDateList, $("hidHolidaySelectedDepartureDate"));}							

			if(iFrame.contentWindow.document.getElementById("lstDurations")!=undefined){
				CopyDropDown(iFrame.contentWindow.document.getElementById("lstDurations"),saveDurationList, $("hidHolidaySelectedDuration"));}											
		}
		//$("reloadOptLists").value="1";    
	}
}

function holShowExtraOptions(){
    $(holPrefix + "lnkShowExtraOptions").hide();
    $(holPrefix + "lnkHideExtraOptions").show();

    $(holPrefix + "rowEstabName").show();
    $(holPrefix + "rowClass").show();
    $(holPrefix + "rowBoard").show();
    $(holPrefix + "rowPromo").show();
            
    $(holPrefix + "hidHolidayShowExtraOptions").value = "1";
}

function holHideExtraOptions(){
    $(holPrefix + "lnkShowExtraOptions").show();
    $(holPrefix + "lnkHideExtraOptions").hide();

    $(holPrefix + "rowEstabName").hide();
    $(holPrefix + "rowClass").hide();
    $(holPrefix + "rowBoard").hide();
    $(holPrefix + "rowPromo").hide();
    
    $(holPrefix + "hidHolidayShowExtraOptions").value = "0";
}  

function holShowRooms(){
  
    roomCount = $(holPrefix + "lstRoomsRequired").value;

    if(roomCount==1){
        $(holPrefix + "rm2Row").hide();
        $(holPrefix + "rm3Row").hide();
    }
    else if(roomCount==2){
        $(holPrefix + "rm2Row").show();
        $(holPrefix + "rm3Row").hide();
    }
    else if(roomCount==3){
        $(holPrefix + "rm2Row").show();
        $(holPrefix + "rm3Row").show();
    }    
 
    holShowChildAges(); 
}

function holShowChildAges(){

    var childFound = false;
    var maxChildCount = 0;

    // Get total rooms
    roomCount = $(holPrefix + "lstRoomsRequired").value;
   
    // Loop through all rooms
    for (iRoom=1; iRoom<4; iRoom++){
    
        // Get children required
        var childCount = $(holPrefix + "lstRm" + iRoom + "Children").value;
        
        // Get child row
        var childRow = $(holPrefix + "rm" + iRoom + "ChildAgesRow");
        
        // Show or hide child row
        if (childCount > 0 && iRoom <= roomCount) {
            childRow.show();
            childFound = true;
        }
        else {
            childRow.hide();
        }
            
        // Loop through children
        for (iChild=1; iChild<6; iChild++) {
        
            // Get child list
            var childList = $(holPrefix + "lstRm" + iRoom + "ChildAge" + iChild);
        
            // Show or hide child list
            if (iChild <= childCount) {
                if (iChild > maxChildCount){maxChildCount = iChild};
                childList.show()
            }
            else {
                childList.hide()
            } 
        }
    }   

    // Loop through children
    for (iChild=1; iChild<6; iChild++) {
        // Get child title
        var childTitle = $(holPrefix + "Child" + iChild + "AgeTitle").show();
        // Show or hide child title
        if (iChild <= maxChildCount){childTitle.show();}
        else {childTitle.hide();}
    }

    if(childFound==true){
        $(holPrefix + "rwChildAgesTitle").show();
        $(holPrefix + "rwChildAges").show();
    }
    else {
        $(holPrefix + "rwChildAgesTitle").hide();
        $(holPrefix + "rwChildAges").hide();
    }
}

function validateHolidaySearch(){
    var iTotPax=0;    
    roomCount = $(holPrefix + "lstRoomsRequired").value;
        
    for(i=1;i<=roomCount;i++){
        
        lstChild = $(holPrefix + "lstRm" + i + "Children");                
        iTotPax += parseInt($(holPrefix + "lstRm" + i + "Adults").value) + parseInt(lstChild.value);        
        if(lstChild.value > 0 ){
                       
            for(childNo=1; childNo <= lstChild.value; childNo++){
                eId = holPrefix + "lstRm" + i + "ChildAge" + childNo;               
                if ($(eId).value == ""){                    
                    alert("Missing Child Age\n\nYou must select an age for each child in your party.");
                    return false;
                }
            }
        } 
    }    
    
    if(iTotPax>9){    
        alert("The maximum permitted party size is 9 passengers.\n\nIf your party has more than 9 passengers please call our reservations team on 0845 6121 747 to check availability."); 
        return false;
    }
        
    return true;
}