﻿$(document).ready(function () {
    InitButtons();
    SetListLayer();
    setTimeout("InitButtons()", 1000);

    $(window).resize(function () {
        InitButtons();
    });

    // Show the calender images
    $('#arrivalcal').show();
    $('#departcal').show();
    $('#airportsbutton').show();

    // Setup clicks for the calenders
    $('#arrivalcal').click(function () {
        $('.arrival-date').focus();
    });
    $('#departcal').click(function () {
        $('.depart-date').focus();
    });
    $('.AirportSelect').click(function () {
        // Show airport popup
        ShowAirportList();
    });
    $('#airportsbutton').click(function () {
        // Show airport popup
        ShowAirportList();
    });
    $('#airportlistclose').click(function () {
        // Show airport popup
        HideAirportList();
    });

    $('.airportlink').click(function () {
        // Get inner text
        $val = $(this).text();
        $acode = $val.substring(0, 3);
        $aname = $val.substring(6, $val.length);
        $('.AirportSelect').val($val);
        $('.SelectedAirportCode').val($acode);
        $('.SelectedAirportName').val($aname);
        HideAirportList();
    });

    $('.SearchButton').click(function () {
        // Validate
        if (!validateairport()) {
            alert('Please select an airport or seaport to search');
            return false;
        }
        if (!validatearrivedate()) {
            alert('Please enter a valid car drop off date');
            return false;
        }
        if (!validatedepartdate()) {
            alert('Please enter a valid car pick up date');
            return false;
        }
        if (!validadate()) {
            alert('Car drop off should be sometime in the future');
            return false;
        }
        if (!validdates()) {
            alert('Car pick up should be greater than the drop off date and time');
            return false;
        }

        return true; // Form is valid
    });
});

function InitButtons() {

    // Position the calender images properly
    $adateobj = $('.arrival-date').offset();
    $ddateobj = $('.depart-date').offset();
    $asobj = $('.AirportSelect').offset();

    // Get top and left coords for each date obj and position accordingly
    $('#arrivalcal').offset({ top: $adateobj.top + 6, left: $adateobj.left + 101 });
    $('#departcal').offset({ top: $ddateobj.top + 6, left: $ddateobj.left + 101 });
    $('#airportsbutton').offset({ top: $asobj.top + 8, left: $asobj.left + 243 });
}
function SetListLayer() {
    $dobj = $(this);
    $('#AirportListLayer').offset({ top: 50, left: ($dobj.width() / 2) - ($('#AirportListLayer').width() / 2) });
}
function ShowAirportList() {
    $('#AirportListLayer').fadeIn();
}

function HideAirportList() {
    $('#AirportListLayer').fadeOut();
}

function PopWindow(url) {
    var prams = 'menubar=0,location=0,resizable=1,scrollbars=1,width=600,height=550,border=0';
    newWin = window.open(url, '', prams);
    newWin.focus();
    return;
}
