﻿function VehiclePopup(url, width, height) {
    width = typeof (width) != 'undefined' ? width : 940;
    height = typeof (height) != 'undefined' ? height : 700;

    popupWindow = window.open(
       url + "&popup=true", '_blank', 'height=' + height + ',width=' + width + ',left=0,top=10,resizable=no,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=no')
}
function Search() {

    pDate = $('#txtPickupDate').datepicker("getDate");
    dDate = $('#txtDropOffDate').datepicker("getDate");

    if (pDate > dDate) {
        $("#Warning").text("Dropoff should be later than or equal to the pickup date.")
        return false
    }
    if ($("#hdPickup").val() < 1) {
        $("#Warning").text("Please select a pickup location.")
        return false
    }
    if ($("#hdDropoff").val() < 1) {
        $("#Warning").text("Please select a dropoff location.")
        return false
    }
    v = 0
    if ($("#hdVehicleID").length) {
        v = $("#hdVehicleID").val()
    }
    redirect = '/Vehicle/Search/?b=' + $("#ddlBerths").val() + '&lp=' + $("#hdPickup").val() + '&ld=' + $("#hdDropoff").val()
    redirect += '&dp=' + $("#txtPickupDate").val() + '&dd=' + $("#txtDropOffDate").val()
    redirect += '&at=' + $("#cbAutomatic").is(":checked")
    redirect += '&sh=' + $("#cbShower").is(":checked")
    redirect += '&dlt=' + $("#DriversLicenseType").val()
    redirect += '&v=' + v
    $("#loading").toggle();
    $(".btn-form-search").toggle();
    setTimeout("window.parent.location.href = redirect", 100);
}
function BindCookies() {
    var b, lp, ld, lps, lds, dp, dd, dlt, at, sh;

    //GET

    //berth
    b = (getParameterByName('b')) ? getParameterByName('b') : $.cookie("b");
    if (b) {
        $("#ddlBerths").val(b);
        $.cookie("b", b, { expires: 1, path: '/' });
    } else {
        $.cookie("b", 2, { expires: 1, path: '/' });
    }
    //pickup
    lp = (getParameterByName('lp')) ? getParameterByName('lp') : $.cookie("lp");
    if (lp) {
        $("#spanPickup").text($("a[title='" + lp + "']:first").text());
        $("#hdPickup").val(lp);
    } else {
        $.cookie("lp", $("#hdPickup").val(), { expires: 1, path: '/' });

    }
    //dropoff
    ld = (getParameterByName('ld')) ? getParameterByName('ld') : $.cookie("ld");
    if (ld) {
        $("#spanDropoff").text($("a[title='" + ld + "']:first").text());
        $("#hdDropoff").val(ld);
    } else {
        $.cookie("ld", $("#hdDropoff").val(), { expires: 1, path: '/' });
    }
    //datepickup
    dp = (getParameterByName('dp')) ? getParameterByName('dp') : $.cookie("dp");
    if (dp) {
        $("#txtPickupDate").val(dp);
        $.cookie("dp", dp, { expires: 1, path: '/' });
    } else {
        $.cookie("dp", $("#txtPickupDate").val(), { expires: 1, path: '/' });
    }
    //datedropoff
    dd = (getParameterByName('dd')) ? getParameterByName('dd') : $.cookie("dd");
    if (dd) {
        $("#txtDropOffDate").val(dd);
        $.cookie("dd", dd, { expires: 1, path: '/' });
    } else {
        $.cookie("dd", $("#txtDropOffDate").val(), { expires: 1, path: '/' });
    }
    //license
    dlt = (getParameterByName('dlt')) ? getParameterByName('dlt') : $.cookie("dlt");
    if (dlt) {
        $("select#DriversLicenseType").val(dlt);
        $.cookie("dlt", dlt, { expires: 1, path: '/' });
    } else {
        $.cookie("dlt", $("#DriversLicenseType").val(), { expires: 1, path: '/' });
    }

    //vehicle type
    vt = (getParameterByName('vt')) ? getParameterByName('vt') : $.cookie("vt");
    if (vt) {
        $("#VehicleType").val(vt);
        $.cookie("vt", vt, { expires: 1, path: '/' });
    } else {
        $.cookie("vt", 0, { expires: 1, path: '/' });
    }

    //Automatic
    at = (getParameterByName('at') != "") ? getParameterByName('at') : $.cookie("at");
    if (eval(at)) {
        $("#cbAutomatic").attr('checked', 'checked');
        $.cookie("at", true, { expires: 1, path: '/' });
    } else {
        $.cookie("at", false, { expires: 1, path: '/' });
    }
    //Shower
    sh = (getParameterByName('sh') != "") ? getParameterByName('sh') : $.cookie("sh");
    if (eval(sh)) {
        $("#cbShower").attr('checked', 'checked');
        $.cookie("sh", true, { expires: 1, path: '/' });
    } else {
        $.cookie("sh", false, { expires: 1, path: '/' });
    }

    //SET

    //berth
    $("#ddlBerths").change(function () {
        b = $(this).val();
        $.cookie("b", b, { expires: 1, path: '/' });
    })
    //pickup
    $("#ddlDepotsPickup").change(function () {
        lp = $(this).val();
        $.cookie("lp", lp, { expires: 1, path: '/' });
    })
    $("#ddlStatesPickup").change(function () {
        lps = $(this).val();
        $.cookie("lps", lps, { expires: 1, path: '/' });
        GetPickupCities();
    })
    //dropoff
    $("#ddlDepotsDropOff").change(function () {
        ld = $(this).val();
        $.cookie("ld", ld, { expires: 1, path: '/' });
    })
    $("#ddlStatesDropOff").change(function () {
        lds = $(this).val();
        $.cookie("lds", lds, { expires: 1, path: '/' });
        GetDropOffCities();

    })
    //datepickup
    $("#txtPickupDate").change(function () {
        dp = $(this).val();
        $.cookie("dp", dp, { expires: 1, path: '/' });
    })
    //datedropoff
    $("#txtDropOffDate").change(function () {
        dd = $(this).val();
        $.cookie("dd", dd, { expires: 1, path: '/' });
    })

    //berth
    $("#DriversLicenseType").change(function () {
        dlt = $(this).val();
        $.cookie("dlt", dlt, { expires: 1, path: '/' });
    })

    //vehicle type
    $("#VehicleType").change(function () {
        vt = $(this).val();
        $.cookie("vt", vt, { expires: 1, path: '/' });
    })

    //automatic
    $("#cbAutomatic").click(function () {
        at = $(this).is(":checked");
        $.cookie("at", at, { expires: 1, path: '/' });
    })
    //shower
    $("#cbShower").click(function () {
        sh = $(this).is(":checked");
        $.cookie("sh", sh, { expires: 1, path: '/' });
    })

    var c, l, a, s, h, k, n;
    c = (getParameterByName('c')) ? getParameterByName('c') : $.cookie("c");
    if (c) { $.cookie("c", c, { expires: 30 }); }
    l = (getParameterByName('l')) ? getParameterByName('l') : $.cookie("l");
    if (l) { $.cookie("l", l, { expires: 30 }); }
    a = (getParameterByName('a')) ? getParameterByName('a') : $.cookie("a");
    if (a) { $.cookie("a", a, { expires: 30 }); }
    s = (getParameterByName('s')) ? getParameterByName('s') : $.cookie("s");
    if (s) { $.cookie("s", s, { expires: 30 }); }
    h = (getParameterByName('h')) ? getParameterByName('h') : $.cookie("h");
    if (h) { $.cookie("h", h, { expires: 30 }); }
    k = (getParameterByName('k')) ? getParameterByName('k') : $.cookie("k");
    if (k) { $.cookie("k", k, { expires: 30 }); }
    n = (getParameterByName('n')) ? getParameterByName('n') : $.cookie("n");
    if (n) { $.cookie("n", n, { expires: 30 }); }

}
$(document).ready(function () {
    $(".datepicker").datepicker({ numberOfMonths: 3, showButtonPanel: true, dateFormat: 'dd/mm/yy', changeMonth: true, changeYear: true, minDate: new Date(), maxDate: new Date().addYears(2), showAnim: '' })
    $("#ddlDepotsPickup").change(function () { $("#Warning").text("") })
    $("#ddlDepotsDropOff").change(function () { $("#Warning").text("") })
    $("#DriversLicenseType").change(function () { $("#Warning").text("") })
    $(".popuplink").removeAttr("href");
    $(".popuplink").attr("href", "#");
    BindCookies();

    $('#fgDropoff').menu({
		content: $('#divLocations').html(),
		backLink: false,
        textTarget: 'spanDropoff',
        valueTarget: 'hdDropoff',
        crumbDefaultText: ' '
	});
    $('#fgPickup').menu({
		content: $('#divLocations').html(),
		backLink: false,
        textTarget: 'spanPickup',
        valueTarget: 'hdPickup',
        crumbDefaultText: ' '
	});
});
