﻿var Mode = 'packa';


function GetMonth(i) {


    switch (i) {
        case 0: return 'Jan'; break;
        case 1: return 'Feb'; break;
        case 2: return 'Mar'; break;
        case 3: return 'Apr'; break;
        case 4: return 'May'; break;
        case 5: return 'Jun'; break;
        case 6: return 'Jul'; break;
        case 7: return 'Aug'; break;
        case 8: return 'Sep'; break;
        case 9: return 'Oct'; break;
        case 10: return 'Nov'; break;
        case 11: return 'Dec'; break;
    }
}

function Populate() {   
    if ($F('dd_resort') > 0) {
        PopulateAccommodation($('dd_accommodation'), $F('dd_resort'));
    }
    else {
        PopulateAccommodation($('dd_accommodation'), 0);
    }
    PopulateLocation($('dd_resort'), 'Resort', 0);
   

    PopulateLocation($('dd_depapt'), 'DepartureAirport', 0);
    PopulateLocation($('dd_arrapt'), 'ArrivalAirport', 0);

    PopulateDay($('dd_day'));
    PopulateMonthYear($('dd_monthyear'));
    PopulateDuration($('dd_duration'));
}

function PopulateMonthYear(dd) {
    var max = new Date($('def_maxdate').value);
    var currval;

    currval = dd.value;

    dd.options.length = 0;
    for (var i = 0; i < 24; i++) {
        var d = new Date($('def_today').value);
        var temp = new Date(d.setMonth(d.getMonth() + i));


        if (temp < max) {
            dd.options.add(new Option(GetMonth(temp.getMonth()) + ' ' + temp.getFullYear(), GetMonth(temp.getMonth()) + ' ' + temp.getFullYear()));
        }
    }

    dd.value = currval;
}

function PopulateDuration(dd) {
    var Max;
    var d;
    var currval;

    currval = dd.value;


    try {
        d = new Date($('dd_day').value + " " + $('dd_monthyear').value);
    }
    catch (e) {
        d = new Date();
    }

    switch (d.getMonth()) {
        case 0: case 1: case 2: case 3: case 10: case 11: Max = 28; break;
        case 4: case 5: case 6: case 7: case 8: case 9: Max = 14; break;
    }


    dd.options.length = 0;

    for (var i = 2; i < Max + 1; i++) {
        if (i % 7 == 0) {
            if (i == 7) {
                dd.options.add(new Option('1 Week', 7, true));
            }
            else {
                dd.options.add(new Option((i / 7) + ' Weeks', i));
            }
        }
        else {
            dd.options.add(new Option(i + ' Nights', i));
        }
    }

    dd.value = currval;
}

function PopulateLocation(dd, locationtype, parentid) {
    var currval;
    currval = dd.value;

    dd.options.length = 0;
    dd.options.add(new Option('Loading... '));

    new Ajax.Request('/SearchBox.ashx?type=' + locationtype + '&parentID=' + parentid, {
        method: 'get',
        onSuccess: function(transport) {
            try {
                dd.options.length = 0;

                if (locationtype == 'DepartureAirport' || locationtype == 'ArrivalAirport') {
                    dd.options.add(new Option('Select Airport', 0));
                } else {
                    if (Mode == 'carhi') {
                        dd.options.add(new Option('Select ' + locationtype, 0));
                    } else {
                        dd.options.add(new Option('Any ' + locationtype, 0));
                    };
                };

                if (locationtype == 'Resort') {
                    dd.options.add(new Option("" ,"0"));
                    var o=new Option("Popular Resorts", "0");
                    o.style.fontWeight='bold';
                    dd.options.add(o);
                    }

                transport.responseText.evalJSON().options.each(function(item) {
                
                if (item.name == 'Gatwick' || item.name == 'Manchester' || item.name == 'Alpe d\'Huez' || item.name == 'Alpe D Huez' || item.name == 'La Tania' || item.name == 'La Plagne'|| item.name == 'Les Arcs 2000'|| item.name == 'Courchevel'|| item.name == 'Meribel'|| item.name == 'Tignes'|| item.name == 'Val Thorens'|| item.name == 'Val D Isere'|| item.name == 'Les Menuires') {
                    dd.options.add(new Option("\xA0\xA0\xA0\xA0" + item.name, item.value));
                    }
                }                
                );
                  if (locationtype == 'Resort') {
                    var o=new Option("", "0");                 
                    o.style.fontWeight='bold';
                    dd.options.add(o);
                    var o=new Option("All Resorts", "0");
                    o.style.fontWeight='bold';
                    dd.options.add(o);
                }
                 if (locationtype == 'DepartureAirport') {
                    var o=new Option("", "0");                 
                    o.style.fontWeight='bold';
                    dd.options.add(o);
                    
                }
                
                  transport.responseText.evalJSON().options.each(function(item) {
                  
                    dd.options.add(new Option("\xA0\xA0\xA0\xA0" + item.name, item.value));
                    
                }                
                );
                
                try {
                    dd.value = currval;
                } catch (e) {
                };                
            } catch (e) {
                //Website.HandleError('Error Populating: ' + e);
            };
        },
        onFailure: function() {
            Website.HandleError('Error Populating');
        }
    });        
};

function PopulateDay(dd) {
    var currval;
    currval = dd.value;

    dd.options.length = 0;

    for (var i = 1; i < 32; i++) {
        dd.options.add(new Option(i, i));
    }
    dd.value = currval;
}

function PopulateAccommodation(dd, locationid) {

    var currval;
    currval = dd.value;

    dd.options.length = 0;
    dd.options.add(new Option('Loading... '));
    
    new Ajax.Request('/SearchBox.ashx?type=accommodation&locationID=' + locationid, {
        method: 'get',
        onSuccess: function(transport) {
            try {
                dd.options.length = 0;

                dd.options.add(new Option('Any Accommodation', ''));

                transport.responseText.evalJSON().options.each(function(item) {
                    dd.options.add(new Option(item.name, item.value));
                });

                try {
                    dd.value = currval;
                } catch (e) {
                };

                return true;
            } catch (e) {
                //Website.HandleError('Error Populating: ' + e);
            };
        },
        onFailure: function() {
            Website.HandleError('Error Populating');
        }
    });     
};

function isDate(strDate) {
    //var strDatestyle = "US"; //United States date style
    var strDatestyle = "EU";  //European date style
    var strDate;
    var strDateArray;
    var strDay;
    var strMonth;
    var strYear;
    var intday;
    var intMonth;
    var intYear;
    var booFound = false;

    var strSeparatorArray = new Array("-", " ", "/", ".");
    var intElementNr;
    var err = 0;
    var strMonthArray = new Array(12);
    strMonthArray[0] = "Jan";
    strMonthArray[1] = "Feb";
    strMonthArray[2] = "Mar";
    strMonthArray[3] = "Apr";
    strMonthArray[4] = "May";
    strMonthArray[5] = "Jun";
    strMonthArray[6] = "Jul";
    strMonthArray[7] = "Aug";
    strMonthArray[8] = "Sep";
    strMonthArray[9] = "Oct";
    strMonthArray[10] = "Nov";
    strMonthArray[11] = "Dec";

    if (strDate.length < 1) {
        return true;
    }

    for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
        if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
            strDateArray = strDate.split(strSeparatorArray[intElementNr]);
            if (strDateArray.length != 3) {
                err = 1;
                return false;
            }
            else {
                strDay = strDateArray[0];
                strMonth = strDateArray[1];
                strYear = strDateArray[2];
            }
            booFound = true;
        }
    }

    if (booFound == false) {
        if (strDate.length > 5) {
            strDay = strDate.substr(0, 2);
            strMonth = strDate.substr(2, 2);
            strYear = strDate.substr(4);
        }
    }

    if (strYear.length == 2) {
        strYear = '20' + strYear;
    }

    // US style
    if (strDatestyle == "US") {
        strTemp = strDay;
        strDay = strMonth;
        strMonth = strTemp;
    }

    intday = parseInt(strDay, 10);
    if (isNaN(intday)) {
        err = 2;
        return false;
    }

    intMonth = parseInt(strMonth, 10);
    if (isNaN(intMonth)) {
        for (i = 0; i < 12; i++) {
            if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
                intMonth = i + 1;
                strMonth = strMonthArray[i];
                i = 12;
            }
        }

        if (isNaN(intMonth)) {
            err = 3;
            return false;
        }
    }

    intYear = parseInt(strYear, 10);
    if (isNaN(intYear)) {
        err = 4;
        return false;
    }

    if (intMonth > 12 || intMonth < 1) {
        err = 5;
        return false;
    }

    if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
        err = 6;
        return false;
    }

    if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
        err = 7;
        return false;
    }

    if (intMonth == 2) {
        if (intday < 1) {
            err = 8;
            return false;
        }

        if (LeapYear(intYear) == true) {
            if (intday > 29) {
                err = 9;
                return false;
            }
        }
        else {
            if (intday > 28) {
                err = 10;
                return false;
            }
        }
    }

    return true;
}

function LeapYear(intYear) {
    if (intYear % 100 == 0) {
        if (intYear % 400 == 0) {
            return true;
        }
    }
    else {
        if ((intYear % 4) == 0) {
            return true;
        }
    }
    return false;
}

function Validates() {
    if (!isDate($('dd_day').value + ' ' + $('dd_monthyear').value)) {
        alert('Invalid date');
        return false;
    }



    if (Date.parse($('dd_day').value + ' ' + $('dd_monthyear').value) < Date.parse($('def_today').value)) {
        alert('Date in the past');
        return false;
    }

    var pax = 0;
    var adults = 0;
    for (var i = 1; i <= $('dd_rooms').value; i++) {
        pax += parseInt($('dd_adults_' + i).value)
        adults += parseInt($('dd_adults_' + i).value)
        pax += parseInt($('dd_children_' + i).value)
        pax += parseInt($('dd_infants_' + i).value)
    }

    if (Mode != 'carhi') {
        if (adults == 0) {
            alert('All bookings must include at least one adult.');
            return false;
        }
    }

   // if (pax > 9) {
    //    alert('A maximum of nine passengers can be booked online, please contact us directly');
    //    return false;
    //}

    if (Mode == 'packa') {
        if ($('dd_depapt').value == 0) {
            alert('Please select a departure airport');
            return false;
        }
    }

    if (Mode == 'carhi') {
        if ($('dd_resort').value == 0) {
            alert('Please select a resort');
            return false;
        }
    }


    if (Mode == 'fligh') {
        if ($('dd_depapt').value == 0) {
            alert('Please select a departure airport');
            return false;
        }

        if ($('dd_arrapt').value == 0) {
            alert('Please select an arrival airport');
            return false;
        }
    }
    return true;
}


function DoSearch() {
    var s = '';

    if (Validates()) {
        var startdate;
        startdate = $('dd_day').value + ' ' + $('dd_monthyear').value;


        s += 'mode=';
        switch (Mode) {
            case 'packa': s += 16; break;
            case 'accom': s += 1; break;
            case 'fligh': s += 8; break;
            case 'carhi': s += 2; break;
        }
        s += '&startdate=';



        s += startdate
        s += '&duration=';
        s += $('dd_duration').value;
        s += '&departurelocationid=';
        s += $('dd_depapt').value;
        s += '&destinationlocationid=';

        if ($('dd_resort').value > 0) {
            s += $('dd_resort').value;
        }
        else {
            s += '0';
        }

        s += '&arrivallocationid='
        s += $('dd_arrapt').value;

        var adults = 0;
        var children = 0;
        var infants = 0;
        var strocc = '';

        for (var i = 1; i <= $('dd_rooms').value; i++) {
            adults += parseInt($('dd_adults_' + i).value)
            children += parseInt($('dd_children_' + i).value)
            infants += parseInt($('dd_infants_' + i).value)

            strocc += $('dd_adults_' + i).value + ',' + $('dd_children_' + i).value + ',' + $('dd_infants_' + i).value;
            if (i < $('dd_rooms').value) {
                strocc += '|'
            }
        }

        s += '&adults=';
        s += adults;
        s += '&children=';
        s += children;
        s += '&infants=';
        s += infants;
        s += '&AccommodationName=';
        s += $('dd_accommodation').value;
        s += '&Rooms=';
        s += $('dd_rooms').value;
        s += '&Occupancy=';
        s += strocc;
        s += '&Rating=';
        s += $('dd_rating').value;
        s += '&ReturnTransfer=';
        s += 'false';
        s += '&PlusMinus=';
        s += $('dd_plusminus').value;
        s += '&OneWay=';
        s += $('chk_oneway').checked;

        try {
            top.wait();
        }
        catch (e) {
        }
        top.location.href = '/Search.aspx?Searching=true&' + s;
    }
}






function SwitchMode(NewMode) {
    if ($('lnk_packa')) $('lnk_packa').className = 'Tab';
    if ($('lnk_fligh')) $('lnk_fligh').className = 'Tab';
    if ($('lnk_accom')) $('lnk_accom').className = 'Tab';
    if ($('lnk_tours')) $('lnk_tours').className = 'Tab';
    //if($('lnk_carhi')) $('lnk_carhi').className='Tab';
    if ($('lnk_trans')) $('lnk_trans').className = 'Tab';
    if ($('lnk_extra')) $('lnk_extra').className = 'Tab';

    switch (NewMode) {
        case 'none':
            $('pan_depapt').style.display = 'none';
            $('pan_arrapt').style.display = 'none';
            //$('pan_region').style.display='none';
            $('pan_resort').style.display = 'none';
            $('pan_accommodation').style.display = 'none';
            //$('pan_oneway').style.display = 'none';
            $('pan_date').style.display = 'none';
           // $('pan_pm').style.display = 'none';
           // $('pan_rating').style.display = 'none';
            $('pan_duration').style.display = 'none';
            $('pan_rooms').style.display = 'none';
            $('pan_passengers').style.display = 'none';
            $('pan_note').style.display = 'none';
            $('pan_change').style.display = 'none';
            $('but_search').style.display = 'none';

            break;
        case 'packa':
            $('pan_depapt').style.display = '';
            $('pan_arrapt').style.display = 'none';
            //$('pan_region').style.display='';
            $('pan_resort').style.display = '';
            $('pan_accommodation').style.display = '';
           // $('pan_oneway').style.display = 'none';
            $('pan_date').style.display = '';
           // $('pan_pm').style.display = 'none';
           // $('pan_rating').style.display = '';
            $('pan_duration').style.display = '';
            $('pan_rooms').style.display = '';
            $('pan_passengers').style.display = '';
            $('pan_note').style.display = '';
            if ($('def_locked').value == 'True') {
                $('pan_change').style.display = '';
            }
            $('but_search').style.display = '';
            break;
        case 'fligh':
            $('pan_depapt').style.display = '';
            $('pan_arrapt').style.display = '';
            //$('pan_region').style.display='none';
            $('pan_resort').style.display = 'none';
            $('pan_accommodation').style.display = 'none';
            //$('pan_oneway').style.display = '';
            $('pan_date').style.display = '';
            //$('pan_pm').style.display = '';
            //$('pan_rating').style.display = 'none';
            $('pan_duration').style.display = '';
            $('pan_rooms').style.display = 'none';
            $('pan_passengers').style.display = '';
            $('pan_note').style.display = '';
            if ($('def_locked').value == 'True') {
                $('pan_change').style.display = '';
            }
            $('but_search').style.display = '';
            break;
        case 'accom':
            $('pan_depapt').style.display = 'none';
            $('pan_arrapt').style.display = 'none';
            //$('pan_region').style.display='';
            $('pan_resort').style.display = '';
            $('pan_accommodation').style.display = '';
            //$('pan_oneway').style.display = 'none';
            $('pan_date').style.display = '';
           // $('pan_pm').style.display = 'none';
           // $('pan_rating').style.display = '';
            $('pan_duration').style.display = '';
            $('pan_rooms').style.display = '';
            $('pan_passengers').style.display = '';
            $('pan_note').style.display = '';
            if ($('def_locked').value == 'True') {
                $('pan_change').style.display = '';
            }
            $('but_search').style.display = '';
            break;
        case 'tours':
            break;
        case 'carhi':
            $('dd_resort').options.length = 0;
            $('dd_resort').options.add(new Option('Select Resort', '0'));

            $('pan_depapt').style.display = 'none';
            $('pan_arrapt').style.display = 'none';
            $('pan_resort').style.display = '';
            $('pan_accommodation').style.display = 'none';
            $('pan_oneway').style.display = 'none';
            $('pan_date').style.display = '';
            $('pan_pm').style.display = 'none';
            $('pan_rating').style.display = 'none';
            $('pan_duration').style.display = '';
            $('pan_rooms').style.display = 'none';
            $('pan_passengers').style.display = 'none';
            $('pan_note').style.display = 'none';

            $('pan_change').style.display = 'none';

            $('but_search').style.display = '';
            $('dd_day').disabled = false;
            $('dd_monthyear').disabled = false;
            $('dd_duration').disabled = false;
            break;
        case 'trans':
            break;
        case 'extra':
            break;
    }

    Mode = NewMode;

    if (NewMode != 'none') {
        $('lnk_' + NewMode).className = 'Tab_Selected';
    }
Populate();
    
}

function UseDefaults() {

    Mode = $('def_mode').value;
       Populate();
  
    $('dd_depapt').options.length = 0;
    $('dd_depapt').options.add(new Option($('def_depapt_name').value, $('def_depapt_id').value))

    $('dd_arrapt').options.length = 0;
    $('dd_arrapt').options.add(new Option($('def_arrapt_name').value, $('def_arrapt_id').value))

    $('dd_resort').options.length = 0;
   $('dd_resort').options.add(new Option($('def_resort_name').value, $('def_resort_id').value))

    $('dd_day').options.length = 0;
    $('dd_day').options.add(new Option($('def_depday').value, $('def_depday').value))

    $('dd_monthyear').options.length = 0;
    $('dd_monthyear').options.add(new Option($('def_depmonthyear').value, $('def_depmonthyear').value))

    $('dd_duration').options.length = 0;
    $('dd_duration').options.add(new Option($('def_duration').value + ' Nights', $('def_duration').value));

    $('dd_accommodation').options.length = 0;

    if ($('def_accommodation').value == '') {
       $('dd_accommodation').options.add(new Option('Any Accommodation', ''));
   }
    else {
        $('dd_accommodation').options.add(new Option($('def_accommodation').value, $('def_accommodation').value));
   }

    $('header').innerHTML = $('def_title').value;

    $('dd_rooms').value = $('def_rooms').value;



    var occupancy = $('def_occupancy').value.split('|');

    var count = 1;

    for (var i = 0; i < occupancy.size(); i++) {
        $('dd_adults_' + count).value = occupancy[i].split(',')[0];
        $('dd_children_' + count).value = occupancy[i].split(',')[1];
        $('dd_infants_' + count).value = occupancy[i].split(',')[2];

        if ($('def_locked').value == 'True') {
            $('dd_adults_' + count).disabled = true;
            $('dd_children_' + count).disabled = true;
            $('dd_infants_' + count).disabled = true;
        }

        count++;
    }

    $('dd_plusminus').value = $('def_plusminus').value;
    $('dd_rating').value = $('def_rating').value;

    if ($('def_rating').value == '') {
        $('dd_rating').value = ''
    }


    $('chk_oneway').checked = ($('def_oneway').value == 'True');

    if ($('def_locked').value == 'True') {
        if (Mode != 'carhi') {
            $('dd_day').disabled = true;
            $('dd_monthyear').disabled = true;
            $('dd_duration').disabled = true;
        }
        else {
            $('dd_day').disabled = false;
            $('dd_monthyear').disabled = false;
            $('dd_duration').disabled = false;
        }

        $('dd_rooms').disabled = true;
        $('dd_plusminus').disabled = true;
        $('pan_change').style.display = '';
    }
    else {
        $('pan_change').style.display = 'none';
    }

    if ($('def_showpacka').value == 'True') {
        $('lnk_packa').style.display = '';
    }
    else {
        $('lnk_packa').style.display = 'none';
    }

    if ($('def_showfligh').value == 'True') {
        $('lnk_fligh').style.display = '';
    }
    else {
        $('lnk_fligh').style.display = 'none';
    }

    if ($('def_showaccom').value == 'True') {
        $('lnk_accom').style.display = '';
    }
    else {
        $('lnk_accom').style.display = 'none';
    }

    if ($('def_showtours').value == 'True') {
        $('lnk_tours').style.display = '';
    }
    else {
        $('lnk_tours').style.display = 'none';
    }

    if ($('def_showtrans').value == 'True') {
        $('lnk_trans').style.display = '';
    }
    else {
        $('lnk_trans').style.display = 'none';
    }

    if ($('def_showextra').value == 'True') {
        $('lnk_extra').style.display = '';
    }
    else {
        $('lnk_extra').style.display = 'none';
    }


}

function SetRooms() {
    for (var i = 1; i < 7; i++) {
        $('room_' + i).style.display = 'none';
    }

    for (var i = 1; i <= $('dd_rooms').value; i++) {
        $('room_' + i).style.display = '';
    }

    if ($('dd_rooms').value > 1) {
        $('lbl_rm').style.display = '';
    }
    else {
        $('lbl_rm').style.display = 'none';
    }
}