/*
JS for MrAuto booking
*/
// XHR XS proxy
var ajaxurl = '/api/';
var rrq = null; // running request
var ccq = null; // current quote
var cci = null; // current id
var ccp = null; // current params
var shFew = "Hide alternatives in ";
var shMor = "Show alternatives in ";
function dateToString(d) {
var months = ['January', 'February', 'March', 'April', 'May', 'June','July', 'August', 'September', 'October', 'November', 'December'];
var days = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ];
return (days[d.getDay()]+" "+d.getDate()+", "+months[d.getMonth()]+" "+d.getFullYear());
}
function selValue(e) {
return e.options[e.selectedIndex].value;
}
function check(dateText) {
var d1 = $('#pickupDate').datepicker("getDate");
var d2 = $('#returnDate').datepicker("getDate");
if (d1 >= d2) {
d2.setTime(d1.getTime());
d2.setDate(d1.getDate()+1);
$('#returnDate').datepicker("setDate", d2);
}
$('#returnDate').datepicker("option", { minDate: d1 } );
$('#pickupDateString').html(dateToString(d1));
$('#returnDateString').html(dateToString(d2));
}
function updateCities(cc, callback) {
$('#pickupCity').html('');
$('#returnCity').html('');
$.get(ajaxurl+"c.php", { 'cc': cc, 'lang': 'EN' },
function(data) {
$('#pickupCity').html(data);
$('#returnCity').html(data);
if (callback) callback();
}
);
}
function secureBook()
{
if ($('.dropoff').is(':checked') && $('.pickup').is(':checked')) {
$('#book').submit();
}
else {
alert("You have to select both pick-up and drop-off location.");
}
}
/**
LOCATIONS
**/
var rc = null;
function showPlaces(q, c)
{
$('#message').html("Loading locations... please wait");
$('#classes').hide();
$('#indicator').show();
$('#progress').show();
var bestprices = $.getJSON(ajaxurl+"bo.php",
{
qid: q,
car: c
},
function (pla, s) {
$.history( { 'action': 'places', 'places': pla });
renderPlaces(pla);
}
);
}
function renderPlaces(places)
{
var ctx = $('#locations');
ctx.setTemplate($("#selectLocation").html());
ctx.processTemplate(places);
$('div.round', ctx).corner();
$('#feeOneWay', ctx).hide();
$('#progress').hide();
$('#classes').hide();
$('#options').hide("slide", { direction: "left" }, 500, function() {
$('#locations').show();
});
}
function disableDropoffs(g) {
var ctx = $('#bg');
$('.dropoff', ctx).each(function(i) {
var el = $(this);
if (!el.hasClass('group_'+g)) {
el.attr("disabled", true);
el.attr("checked", false);
}
else {
el.removeAttr("disabled");
}
});
$('.pickup', ctx).each(function(i) {
var el = $(this);
el.removeAttr("disabled");
});
oneWayFee(ctx);
}
function disablePickups(g) {
var ctx = $('#bg');
$('.pickup', ctx).each(function(i) {
var el = $(this);
if (!el.hasClass('group_'+g)) {
el.attr("disabled", true);
el.attr("checked", false);
}
else {
el.removeAttr("disabled");
}
});
$('.dropoff', ctx).each(function(i) {
var el = $(this);
el.removeAttr("disabled");
});
oneWayFee(ctx);
}
function oneWayFee(ctx)
{
var p = $('input.pickup:checked', ctx).val();
var d = $('input.dropoff:checked', ctx).val();
if (p>0 && d>0)
{
if (p != d)
$('#feeOneWay').show();
else
$('#feeOneWay').hide();
}
else
$('#feeOneWay').hide();
}
function showBP(q) {
var bestprices = $.getJSON(ajaxurl+"quote.php",
{
qid: q,
op: 'bp'
},
function (data, status) {
$.history({ 'action':'search', 'cq': data, 'q': q, 'p': ccp });
setRenderQuote(data, q, ccp)
}
)
}
function expandAllCats()
{
var cl = $('#classes');
$.each(ccq, function(n, v) {
var sec = $('#cars_'+n, cl);
sec.setTemplate($("#moreCars").html());
sec.setParam("hide", v.id);
sec.setParam("qid", cci);
sec.setParam("code", n);
sec.processTemplate(ccq[n]["cars"]);
$('div.roundbtn', sec).corner();
sec.show();
$('#bp_'+n, cl).hide();
// $('#t_'+n, cl).attr('src','/img/cmp.gif');
});
}
//
// Render quote search results. Can be called for redisplay of old quote.
//
// cq = current quote (hittade bilar)
// q = quote id
// p = parametrar (land, stad, etc.)
//
function setRenderQuote(cq, q, p)
{
// Save old quote id
oldi = cci;
// Set new quote
ccq = cq;
cci = q;
ccp = p;
// Get context for jTemplate
var ctx = $('#classes');
ctx.setTemplate($("#classBestPrice").html());
ctx.setParam("info", ccq["info"]);
ctx.setParam("qid", cci);
ctx.processTemplate(ccq);
// Fix corners before display
$('div.round', ctx).corner();
// Hide other components
$('#progress').hide();
$('#locations').hide();
// If we are displaying a new quote, ensure correct form values are displayed
if (q != oldi)
{
// Quote id has changed
var ctq = $('#quote');
// Current country code
var ccc = $('#country', ctq).val();
var ncc = '';
// Which one is the new country
$.each(p, function(i, f) {
if (f.name == 'country') {
ncc = f.value;
}
});
// Country code changed, update city list before deserialize
if (ncc != ccc)
{
// Quick update (not needed but will improve looks)
$('#country', ctq).val(ncc);
updateCities(ncc, function(){ $('#quote').deserialize(ccp, {overwrite: true}); });
}
else
{
$('#quote').deserialize(ccp, {overwrite: true});
}
}
// TEMP SPLIT TESTING
expandAllCats();
// Show components
$('#options').show();
$('#classes').show();
}
$.history.callback = function ( reinstate, cursor ) {
if (typeof(reinstate) == 'undefined')
{
//if (console) console.info("first load");
}
else
{
//if (console) console.log(reinstate);
if (reinstate.action == 'places') {
renderPlaces(reinstate.places);
}
if (reinstate.action == 'search') {
setRenderQuote(reinstate.cq, reinstate.q, reinstate.p);
}
}
};
function showMore(base, element, skip) {
var sec = $('#cars_'+element, base.parentNode);
if (sec.is(':visible'))
{
sec.hide('fast');
//sec.slideUp('fast');
$('#t_'+element, base).attr('src','/img/exp.gif');
/*
$('.category').each(function(i) {
if (this.id != 'cat_'+element) $(this).show();
});
*/
}
else
{
/*
$('.category').each(function(i) {
if (this.id != 'cat_'+element) $(this).fadeOut('fast');
});
*/
sec.setTemplate($("#moreCars").html());
sec.setParam("hide", skip);
sec.setParam("qid", cci);
sec.setParam("code", element);
sec.processTemplate(ccq[element]["cars"]);
/* $('div.round', sec).corner(); */
$('div.roundbtn', sec).corner();
sec.show('fast');
//sec.slideDown('fast');
$('#t_'+element, base).attr('src','/img/cmp.gif');
}
}
function getQuotes() {
var pickup = $("#pickupCity").val();
var dropoff = $("#dropoffCitY").val();
if (pickup == '' || (dropoff == '' && !$("#sameAsPickup").is(":checked"))) {
alert("You have to select pickup and dropoff location");
return;
}
$('#search').attr("disabled", true);
$('#welcome').hide();
$('#boffers').hide();
$('#guide').hide();
$('#message').html("Searching... please wait");
$('#classes').hide();
$('#classes').html('');
$('#indicator').show();
$('#progress').show();
var pdata = $('#quote').serialize();
ccp = $('#quote').serializeArray();
ccq = null;
rrq = $.ajax({
type: "POST",
url: ajaxurl+"checkq.php",
data: pdata,
dataType: 'json',
success: gotQuote,
error: failQuote
}
);
//if ($(
}
function gotQuote(q, status)
{
rrq = null;
$('#search').attr("disabled", false);
if (q.qid > 0)
{
//document.location = "test.php?qid="+q.qid;
$('#message').html("Loading results...");
showBP(q.qid);
}
else
{
if (q.error < 0)
failQuote(null, "input", null);
else
failQuote(null, "empty", null);
}
}
function failQuote(xhr, status, error)
{
rrq = null;
ccq = null;
ccp = null;
/* $('#progress').hide(); */
$('#indicator').hide();
//if (console) console.log(status);
switch(status)
{
case 'input':
$('#message').html("Invalid input.");
break;
case 'empty':
$('#message').html("Unfortunately we did not find any available vehicles for the selected destination and period.");
break;
case 'error':
case 'parsererror':
$('#message').html("Server error. Please try again shortly.");
break;
case 'timeout':
$('#message').html("Communication error. Please try again shortly.");
break;
default:
$('#message').html("Unknown error. Please try again.");
break;
}
$('#search').attr("disabled", false);
}
/*
Page ready/loaded events
*/
$(function() {
var beg = new Date();
var end = new Date();
var begstart = 7;
var endstart = begstart + 4;
beg.setDate(beg.getDate()+begstart);
end.setDate(end.getDate()+endstart);
$('#pickupDate').datepicker({
constrainInput: true,
changeMonth: true,
changeYear: true,
defaultDate: +1,
dateFormat: 'yy-mm-dd',
minDate: +1,
firstDay: 1,
buttonImageOnly: true,
onSelect: check
});
$('#returnDate').datepicker({
constrainInput: true,
changeMonth: true,
changeYear: true,
defaultDate: +3,
dateFormat: 'yy-mm-dd',
firstDay: 1,
minDate: +3,
onSelect: check
});
$('#pickupDate').datepicker("setDate", beg);
$('#returnDate').datepicker("setDate", end);
$('#pickupCal').click(function() { $('#pickupDate').datepicker("show") });
$('#returnCal').click(function() { $('#returnDate').datepicker("show") });
$("#sameAsPickup").attr('checked', true);
$("#returnCity").attr('disabled', true);
$('#pickupTime').timePicker();
$('#returnTime').timePicker();
$("#sameAsPickup").click(
function() {
if (this.checked)
$("#returnCity").attr("disabled", true);
else
$("#returnCity").attr("disabled", false);
}
);
$('#country').change(
function() {
if (city != '')
updateCities(this.value, function() { $("#pickupCity").val(city); } );
else
updateCities(this.value);
}
).change();
$('#pickupCity').change(
function() {
var pc = $('#pickupCity');
var rc = $('#returnCity');
if (rc.val() == '' || rc.val() == pc.val() || $("#sameAsPickup").is(":checked") ) {
rc.val(pc.val());
}
}
).change();
$('#returnCity').change(
function() {
$('#rc').val(this.val);
}
);
$('#search').click(getQuotes);
//$('#welcome').hide();
//$('#boffers').hide();
if (mode == 'guide')
{
$('#welcome').hide();
$('#boffers').hide();
$('#guide').show();
}
if (mode == 'landing')
{
var x1 = new Date();
x1.setFullYear(2009,5,8);
var x2 = new Date();
x2.setFullYear(2009,5,14);
$('#pickupDate').datepicker("setDate", x1);
$('#returnDate').datepicker("setDate", x2);
$('#welcome').hide();
$('#boffers').hide();
$('#guide').show();
}
$('#details').hide();
$('#classes').hide();
$('#progress').hide();
//updateCities("SE");
});