﻿var Mode = 'packa';


function Populate() {
    if ($F('dd_resort') > 0) {
        PopulateAccommodation($('dd_accommodation'), $F('dd_resort'));
    }
    else {
        PopulateAccommodation($('dd_accommodation'), 0);
    }

    if ($F('dd_region') > 0) {
        PopulateLocation($('dd_resort'), 'Resort', $F('dd_region'));
    }
    else {
        PopulateLocation($('dd_resort'), 'Resort', 0);
    }

    PopulateLocation($('dd_region'), 'Region', 0);

    PopulateLocation($('dd_depapt'), 'DepartureAirport', 0);
    PopulateLocation($('dd_arrapt'), 'ArrivalAirport', 0);
};

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' || locationtype == 'Region') {
                        dd.options.add(new Option('Select ' + locationtype, 0));
                    } else {
                        dd.options.add(new Option('Any ' + locationtype, 0));
                    };
                };

                transport.responseText.evalJSON().options.each(function(item) {
                    dd.options.add(new Option(item.name, item.value));
                });

                try {
                    dd.value = currval;
                } catch (e) {
                };
            } catch (e) {
                //Website.HandleError('Error Populating: ' + e);
            };
        },
        onFailure: function() {
            Website.HandleError('Error Populating');
        }
    });
};

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 Validates() {
    if (Date.parse($F('sb_dd')) < Date.parse($F('def_today'))) {
        alert('Date in the past');
        return false;
    };

    if (Date.parse($F('sb_rd')) < Date.parse($F('sb_dd'))) {
        alert('Return date before departure date');
        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 ($('dd_region').value == 0 && $('dd_resort').value == 0 && $('dd_accommodation').value == '') {
            alert('Please select a destination');
            return false;
        }
    }


    if (Mode == 'accom') {



        if ($('dd_region').value == 0 && $('dd_resort').value == 0 && $('dd_accommodation').value == '') {
            alert('Please select a destination');
            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()) {
        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 += $F('sb_dd');
        s += '&duration=';
        s += dateDiff(new Date($F('sb_rd')), new Date($F('sb_dd')));
        s += '&departurelocationid=';
        s += $('dd_depapt').value;
        s += '&destinationlocationid=';

        if ($('dd_resort').value > 0) {
            s += $('dd_resort').value;
        }
        else {
            if ($('dd_region').value > 0) {
                s += $('dd_region').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';

    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_depdate').style.display = 'none';
            $('pan_retdate').style.display = 'none';
            $('pan_pm').style.display = 'none';
            $('pan_rating').style.display = 'none';
            $('pan_rooms').style.display = 'none';
            $('pan_passengers').style.display = 'none';
            $('pan_change').style.display = 'none';
            $('img_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_depdate').style.display = '';
            $('pan_retdate').style.display = '';
            $('pan_pm').style.display = 'none';
            $('pan_rating').style.display = '';
            $('pan_rooms').style.display = '';
            $('pan_passengers').style.display = '';

            if ($('def_locked').value == 'True') {
                $('pan_change').style.display = '';
            };
            $('img_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_depdate').style.display = '';
            $('pan_retdate').style.display = '';
            $('pan_pm').style.display = 'none';
            $('pan_rating').style.display = 'none';
            $('pan_rooms').style.display = 'none';
            $('pan_passengers').style.display = '';

            if ($('def_locked').value == 'True') {
                $('pan_change').style.display = '';
            };
            $('img_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_depdate').style.display = '';
            $('pan_retdate').style.display = '';
            $('pan_pm').style.display = 'none';
            $('pan_rating').style.display = '';
            $('pan_rooms').style.display = '';
            $('pan_passengers').style.display = '';

            if ($('def_locked').value == 'True') {
                $('pan_change').style.display = '';
            };
            $('img_search').style.display = '';
            break;
        case 'tours':
            break;
        case 'carhi':
            break;
        case 'trans':
            break;
        case 'extra':
            break;
    };

    Mode = NewMode;

    if (NewMode != 'none') {
        $('switch_' + NewMode).checked = true;
    }

    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_region').options.length = 0;
    $('dd_region').options.add(new Option($('def_region_name').value, $('def_region_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));
    */
    $('sb_dd').value = $F('def_dd');
    $('sb_rd').value = $F('def_rd');

    $('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') {
        $('dd_rooms').disabled = true;
        $('dd_plusminus').disabled = true;
        $('pan_change').style.display = '';
    } else {
        $('pan_change').style.display = 'none';
    };
};

function SetRooms() {
    for (var i = 2; i < 7; i++) {
        $('room_' + i).style.display = 'none';
    };

    for (var i = 2; i <= $('dd_rooms').value; i++) {
        $('room_' + i).style.display = '';
    };

    if ($('dd_rooms').value > 1) {
        $('lbl_rm').style.display = '';
    } else {
        $('lbl_rm').style.display = 'none';
    };
};