var autosendform = false;

jq(function() {
	initLayout();									 
});
var focused = null;
var hidden = true;
var autocomp = new Array(4);

var scriptVars = {
	isSearchboxAdvancedVisible: false,
	slideSarchboxAdvanced: null
};

function initLayout(){
	var sbDates = jq('#SPickupdate, #SDropdate').datepicker({
        numberOfMonths: 2,
        /*showButtonPanel: true,*/
        buttonImage: globalVars.calendar_image_new /*"images/calendar.gif"*/,
        buttonImageOnly: true,
        showOn: 'both',
        dateFormat: 'dd/mm/yy',
        constrainInput: true,
        stepMonths: 2,
        changeMonth: true,
        minDate: "0D",
        maxDate: localVars.maxDate, /*"+1Y",*/
        defaultDate: "0D",
        onSelect: function (selectedDate)
        {
		
            var option = this.id == "SPickupdate" ? "minDate" : "maxDate",
					instance = jq(this).data("datepicker");
            date = jq.datepicker.parseDate(
						instance.settings.dateFormat ||
						jq.datepicker._defaults.dateFormat,
						selectedDate, instance.settings);
            sbDates.not(this).datepicker("option", option, date);

            if (this.id == "SPickupdate")
            {
                window.setTimeout(function ()
                {
                    jq("#SDropdate").datepicker('show');
                }, 300);
            }
        }
    });
	
	jq("#SDestinationValue").autocomplete({
        source: function (request, response)
        {
            jq.ajax({
                url: "http://www.cheaptickets.nl/autocomplete.cfm",
                dataType: "jsonp",
                jsonp: "jsonCallback",
                data: {
					p_lang: globalVars.lang,
                    product: "cars",
                    value: request.term
                },
                success: function (data)
                {
					response(jq.map(data.auto, function (item)
                    {
						if (item.c == "")
						{
							return {
								isHtml: true,
								noSelect: true,
								label: item.l,
								value: item.c       // l = full name, c = airport code
							}
						}
						else
						{
							return {
								label: item.l,
								value: item.c       // l = full name, c = airport code
							}
						}
                    }));
                }
            });
        },
        minLength: 3,
        select: function (event, ui)
        {
            // manually copy the value to the fields, since we'll cancel the event
            var field = jq('#' + this.id);
			if (ui.item.noSelect)
			{
				return false;
			}
            field.val(ui.item.label)

            // scroll to the start of the text
            field.setCursorPosition(0);

            // update the related airport code hidden field
			jq('#SDestinationID').val(ui.item.value);

            // cancel the event
            return false;			
        },
        /*open: function ()
        {
        },
        close: function ()
        {
        }*/
    });
};

function showAdvancedSearchboxCars(doShow)
{
    /// <summary>
    /// Toggles the box with the extended search options. Will automatically hide the most recent searches if they're visible.
    /// </summary>

    if (doShow)
    {
        jq('#advancedSearchboxLink').hide();
        jq('#searchboxCars_more').show('normal');
    }
    else
    {
        jq('#advancedSearchboxLink').show();
        jq('#searchboxCars_more').hide('normal');
    }
}


function ControleStap1(f) {
	var datum1  = back_date(f.SPickupdate.value);
	var datum2  = back_date(f.SDropdate.value);
	var vandaag = new Date();
	if(isNaN(datum1)) {
		alert("Please enter a valid pick-up date (dd/mm/yyyy).");
		f.Svertrekdate.focus();
		return false;
	}else if (isNaN(datum2) ) {
		alert("Please enter a valid drop-off date (dd/mm/yyyy).");
		f.Sretourdate.focus();
		return false;
	}else{
		if(datum1 <= vandaag) {
			alert('Your pick-up date has passed !');
			f.SPickupdate.focus();
			return false;
		}else if (datum2 <= vandaag) {
			alert('Your drop-off date has passed !');
			f.SDropdate.focus();
			return false;
		}else if (datum2 < datum1) {
			alert('Your drop-off date is before your pick-up date !');
			f.SPickupdate.focus();
			return false;
		}else{				  
			if (f.SDestinationValue.value == '') {
				alert('Please select a destination.');
				f.SDestinationValue.focus();
				return false;
			}else{
				if(document.getElementById('WaitPage')) document.getElementById('WaitPage').style.visibility='visible';
				return true;
			}
		}
	}
}
