﻿function isValidDate(day,month,year)
{          
    var dteDate;
    //set up a Date object based on the day, month and year arguments
    //javascript months start at 0 (0-11 instead of 1-12)
    dteDate=new Date(year,month,day);  
    return ((day==dteDate.getDate()) && (month==dteDate.getMonth()) && (year==dteDate.getFullYear()));
}

function VaildateQuickSearch()
{
    // make sure they have selecta country
    var startDay = document.getElementById(clientId + 'SelectDate_lstDay');
    var startMonth = document.getElementById(clientId + 'SelectDate_lstMonth');
    var startYear = document.getElementById(clientId + 'SelectDate_lstYear');
    var country = document.getElementById(clientId + 'lstCountry');
    var city = document.getElementById(clientId + 'lstCity');
    var roomQuantity = document.getElementById(clientId + 'lstNoOfRooms_lstNumber');
    var boardBasis = document.getElementById(clientId + 'lstBoardBasis'); 
    var divError = document.getElementById('divErrorSearch');     
    // check the date that has been selected
    var userDate = ParseDate(startDay.value + '/' + startMonth.value + '/'+ startYear.value + '/');
    var currDate = new Date();    
    currDate.setDate( currDate.getDate() - 1 );
    if ( userDate < currDate )
    {
        divError.innerHTML = 'You have entered an incorrect date. Please amend to meet your chosen dates of travel';
        divError.style.display = 'inline';
        return false;
    } 
    
    var vaidtoDate = new Date(); 
    vaidtoDate.setDate( vaidtoDate.getDate());
    if ( userDate < vaidtoDate )
    {
        divError.innerHTML = 'Please ensure you search a day in advance.';
        divError.style.display = 'inline';
        return;
    } 
                            
    if(!isValidDate(startDay.value,startMonth.value -1,startYear.value))
    {   
       divError.innerHTML = 'You have entered an incorrect date. Please amend to meet your chosen dates of travel';
       divError.style.display = 'inline';
       return false;
    }

    // check the user has slected a country
    if (country.value == '-1')
    {
        divError.innerHTML = 'Please select a country';
        divError.style.display = 'inline';
        return false;
    }
   
    if (city.value == '-1')
    {
        divError.innerHTML = 'Please select a city / resort';
        return false;
    }
    
    // fix if the page is still re-loading
    if (city.value == '')
    {
        divError.innerHTML = 'Please wait for the page to load before re-searching';
        return false;
    }

    // count the total amount of passengers
    var passCount =0;   
    passCount += eval(document.getElementById(clientId + 'lstNoAdults_lstNumber').value);
    passCount += eval(document.getElementById(clientId + 'lstNoChildren_lstNumber').value);
    passCount = passCount * roomQuantity.value;
    if(passCount >12)
    {
       divError.innerHTML = 'Information: Please phone ' + phoneNumber + ' for group bookings';
       divError.style.display = 'inline';
       return false;
    }    
    divError.style.display = 'none';
    return true;
}

function searchButton()
{   
    var startDay = document.getElementById(clientId + 'SelectDate_lstDay');
    var startMonth = document.getElementById(clientId + 'SelectDate_lstMonth');
    var startYear = document.getElementById(clientId + 'SelectDate_lstYear');
    var numNight = document.getElementById(clientId + 'lstNumberOfNights_lstNumber');
    var country = document.getElementById(clientId + 'lstCountry');
    var city = document.getElementById(clientId + 'lstCity');
    var roomQuantity = document.getElementById(clientId + 'lstNoOfRooms_lstNumber');
    var boardBasis = document.getElementById(clientId + 'lstBoardBasis'); 
    var rating = document.getElementById(clientId + 'lstStar');
    var location = document.getElementById(clientId + 'lstLocation');
     
    if ( VaildateQuickSearch())
    {
        // check the date that has been selected
        var userDate = ParseDate(startDay.value + '/' + startMonth.value + '/'+ startYear.value + '/');
        var currDate = new Date(); 
        currDate.setDate( currDate.getDate() - 1 );

        // all ok create the string and do search
        var queryString = '?startday=' + startDay.value;
        queryString += '&startmonth=' + startMonth.value;
        queryString += '&startyear=' + startYear.value;
        queryString += '&numnights=' + numNight.value;
        queryString += '&country=' + country.value;
        queryString += '&city=' + city.value;        
        queryString += '&numrooms=' + roomQuantity.value;
    
        queryString += '&numAdults=' + document.getElementById(clientId + 'lstNoAdults_lstNumber').value;
        queryString += '&numChildren=' + document.getElementById(clientId + 'lstNoChildren_lstNumber').value;
        
        var noOfChildren = document.getElementById(clientId + 'lstNoChildren_lstNumber'); 
        for (var i = 0; i<roomQuantity.value;i++)
        {            
            if(noOfChildren.value >=1)
            { 
                queryString += '&child1Age' + i + '=' + document.getElementById(clientId + 'lst1Child' + i + '_lstNumber').value;
            }

            if (noOfChildren.value  >= 2)
            {
                queryString += '&child2Age' + i + '=' + document.getElementById(clientId + 'lst2Child' + i + '_lstNumber').value;
            }

            if (noOfChildren.value  >= 3)
            {
                queryString += '&child3Age' + i + '=' + document.getElementById(clientId + 'lst3Child' + i + '_lstNumber').value;
            }

            if (noOfChildren.value  >= 4)
            {
                queryString += '&child4Age' + i + '=' + document.getElementById(clientId + 'lst4Child' + i + '_lstNumber').value;
            }
        }
        
        if (boardBasis.value != '-1')
        {
            queryString += '&boardbasis=' + boardBasis.value ;  
        }

        if (rating.value != '-1')
        {
            queryString += '&starrating=' + rating.value ;  
        }

        currDate.setDate( currDate.getDate() + 14 )
        if ( userDate <= currDate )
        {
            queryString += '&availability=immediate';
        }
        
        if(location.value != '-1')
        {
            queryString += '&location=' + location.value ;  
        }  
        
        queryString += '&agentid=' + agentid;  
        
        // add on the promocode if not empty
        if(promotionalCode != '')
        {
              queryString += '&PromotionalCode=' + promotionalCode;  
        }
        
        // if we already have the hotel id, pass it on - this will be used by 
        // results.aspx to highlight a specific hotel
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++)
        {
            var pair = vars[i].split("=");
            if (pair[0].toLowerCase() == "hotelid")
            {
                queryString += '&hotelID=' + pair[1];
            }
        }
        window.location.href = '/results.aspx' + queryString;
    }
}

function ChangeFilterButtonsTop()
{
    document.getElementById('ctl00_phMain_resultsContainer_butFilterTop').src = '/Images/Results/apply-filter.gif';
}

function ChangeFilterButtonsBottom()
{    
    document.getElementById('ctl00_phMain_resultsContainer_butFilterBottom').src = '/Images/Results/apply-name-filter.gif';
}
