var adultOptions = {'0' : '0','1' : '1','2' : '2','3' : '3','4' : '4','5' : '5','6' : '6','7' : '7','8' : '8','9' : '9','10' : '10','11' : '11','12' : '12'};
var childOptions = {'0' : '0','1' : '1','2' : '2','3' : '3','4' : '4','5' : '5','6' : '6'};

$j(function() {	
	var $holidayPopulated = $j("#holidaySearch #holidaysDO #holidayAdults option").size();
	if ($holidayPopulated == 1) 
	{ 
		$j('option', '#holidaySearch #holidayAdults').remove();
		$j('option', '#holidaySearch #holidayChildren').remove();
		$j('option', '#holidaySearch #holidayInfants').remove();
		$j("#holidayCheckInDate").val('');
		$j("#ajaxResorts").val('');
			for (key in adultOptions) 
			{
    			if (typeof(adultOptions[key] == 'string')) 
				{
        			$j('#holidaySearch #holidayAdults').append('<option value="' + key + '">' + adultOptions[key] + '</option>');
    			}
			}
			for (key in childOptions) 
			{
    			if (typeof(childOptions[key] == 'string')) 
				{
        			$j('#holidaySearch #holidayChildren').append('<option value="' + key + '">' + childOptions[key] + '</option>');
        			$j('#holidaySearch #holidayInfants').append('<option value="' + key + '">' + childOptions[key] + '</option>');
    			}
			}
			var $i = 1;
			$j('option', '#holidaySearch #holidayDuration').remove();
			for ($i; $i<31; $i++) 
			{
    			$j('#holidaySearch #holidayDuration').append('<option value="' + $i + '">' + $i + '</option>');
    			$j('#hotelSearch #hotelDuration').append('<option value="' + $i + '">' + $i + '</option>');
			};
			$j("#holidaySearch #holidayAdults").prop("selectedIndex", 2);
			$j("#holidaySearch #holidayChildren").prop("selectedIndex", 0);
			$j("#holidaySearch #holidayInfants").prop("selectedIndex", 0);
			$j("#holidaySearch #holidayDuration").prop("selectedIndex", 6);
	} 
	$j( "#ajaxResorts" ).autocomplete({
	    source: function(request, response) {
	        $j.ajax({
	            url: '/proxy.php',
	            dataType: "json",
	            data: {
	                term : request.term,
	                url : "http://www.worldtravelbookers.net/ajax/index.php/ajax_jsonp/defaults/9/resorts/"
	            },
	            success: function(data) {
	                response(data);
	            }
	        });
	    },
		minLength: 2,
		select: function( event, ui ) {
			$j('#idResort').val(ui.item.idResort);
		}
	});
	$j( "#worldwideResorts" ).autocomplete({
	    source: function(request, response) {
	        $j.ajax({
	            url: '/proxy.php',
	            dataType: "json",
	            data: {
	                term : request.term,
	                url : "http://www.worldtravelbookers.net/ajax/index.php/ajax_jsonp/defaults/10/resorts/"
	            },
	            success: function(data) {
	                response(data);
	            }
	        });
	    },
		minLength: 2,
		select: function( event, ui ) {
			$j('#idResort').val(ui.item.idResort);
		}
	});
	$j( "#cityResorts" ).autocomplete({
	    source: function(request, response) {
	        $j.ajax({
	            url: '/proxy.php',
	            dataType: "json",
	            data: {
	                term : request.term,
	                url : "http://www.worldtravelbookers.net/ajax/index.php/ajax_jsonp/defaults/11/resorts/"
	            },
	            success: function(data) {
	                response(data);
	            }
	        });
	    },
		minLength: 2,
		select: function( event, ui ) {
			$j('#idResort').val(ui.item.idResort);
		}
	});
	$j( "#skiResorts" ).autocomplete({
	    source: function(request, response) {
	        $j.ajax({
	            url: '/proxy.php',
	            dataType: "json",
	            data: {
	                term : request.term,
	                url : "http://www.worldtravelbookers.net/ajax/index.php/ajax_jsonp/defaults/40/resorts/"
	            },
	            success: function(data) {
	                response(data);
	            }
	        });
	    },
		minLength: 2,
		select: function( event, ui ) {
			$j('#idResort').val(ui.item.idResort);
		}
	});
	$j( "#ajaxDepartures" ).autocomplete({
	    source: function(request, response) {
	        $j.ajax({
	            url: '/proxy.php',
	            dataType: "json",
	            data: {
	                term : request.term,
	                url : "http://www.worldtravelbookers.net/ajax/index.php/ajax_jsonp/defaults/9/departures/"
	            },
	            success: function(data) {
	                response(data);
	            }
	        });
	    },
		minLength: 0,
		select: function( event, ui ) {
			$j('#departureOutLocation').val(ui.item.idDeparture);
		}
	});
	$j( "#ajaxFlights" ).autocomplete({
	    source: function(request, response) {
	        $j.ajax({
	            url: '/proxy.php',
	            dataType: "json",
	            data: {
	                term : request.term,
	                url : "http://www.worldtravelbookers.net/ajax/index.php/ajax_jsonp/defaults/9/flights/"
	            },
	            success: function(data) {
	                response(data);
	            }
	        });
	    },
		minLength: 0,
		select: function( event, ui ) {
			$j('#destinationOutLocation').val(ui.item.searchValue);
			$j('#ajaxFlights').val(ui.item.searchValue);
		}
	});
	$j.datepicker.setDefaults($j.datepicker.regional['']);
    $j("#holidayCheckInDate").datepicker({
        dateFormat: 'MM/m/d/yy',
        changeMonth: true,
        changeYear: true,
        showOn: "both",
		numberOfMonths: 2,
		maxDate: '+1y',
		regional: '',
        buttonImage: "/common/img/calendar.gif",
        buttonImageOnly: true,
        buttonImageText: "Calendar",
        beforeShow: function(dateText, instance) { 
            $j("#holidayCheckInDate").datepicker('setDate', new Date());
        },
        onSelect: function(dateText, instance) {
            var pieces = dateText.split("/");
			var dateFormat = pieces[2] + ' ' + pieces[0] + ' ' + pieces[3] + ' ';
             $j("#holidayCheckInDateDay").val(pieces[2]);
             $j("#holidayCheckInDateMonth").val(pieces[1]);
             //$j("#checkInDateYear").val(pieces[3]);
             $j("#holidayCheckInDate").val(dateFormat);
         }              
    });

    $j("#flightCheckInDate").datepicker({
        dateFormat: 'MM/m/d/yy',
        changeMonth: true,
        changeYear: true,
        showOn: "both",
		numberOfMonths: 2,
		maxDate: '+1y',
		regional: '',
        buttonImage: "/common/img/calendar.gif",
        buttonImageOnly: true,
        buttonImageText: "Calendar",
        beforeShow: function(dateText, instance) { 
            $j("#flightCheckInDate").datepicker('setDate', new Date());
        },
        onSelect: function(dateText, instance) {
            var pieces = dateText.split("/");
			var dateFormat = pieces[2] + ' ' + pieces[0] + ' ' + pieces[3] + ' ';
             $j("#departureDateDay").val(pieces[2]);
             $j("#departureDateMonth").val(pieces[1]);
             //$j("#checkInDateYear").val(pieces[3]);
             $j("#flightCheckInDate").val(dateFormat);
         }              
    });

    $j("#flightReturnDateDay").datepicker({
        dateFormat: 'MM/m/d/yy',
        changeMonth: true,
        changeYear: true,
        showOn: "both",
		numberOfMonths: 2,
		maxDate: '+1y',
		regional: '',
        buttonImage: "/common/img/calendar.gif",
        buttonImageOnly: true,
        buttonImageText: "Calendar",
        beforeShow: function(dateText, instance) { 
            $j("#flightReturnDateDay").datepicker('setDate', new Date());
        },
        onSelect: function(dateText, instance) {
            var pieces = dateText.split("/");
			var dateFormat = pieces[2] + ' ' + pieces[0] + ' ' + pieces[3] + ' ';
             $j("#returnDateDay").val(pieces[2]);
             $j("#returnDateMonth").val(pieces[1]);
             //$j("#checkInDateYear").val(pieces[3]);
             $j("#flightReturnDateDay").val(dateFormat);
         }              
    });

	$j("#holidaySearch #holidaysDO #holidaySearchBtn").click(function() 
		{
			var returnValue = true ;
		}
	);
	
	var $hotelPopulated = $j("#hotelSearch #hotelsDO #hotelAdults option").size();
	if ($hotelPopulated == 1) 
	{ 
		$j('option', '#hotelSearch #hotelAdults').remove();
		$j('option', '#hotelSearch #hotelChildren').remove();
		$j('option', '#hotelSearch #hotelInfants').remove();
		$j("#hotelCheckInDate").val('');
		$j("#hotelResorts").val('');
			for (key in adultOptions) 
			{
    			if (typeof(adultOptions[key] == 'string')) 
				{
        			$j('#hotelSearch #hotelAdults').append('<option value="' + key + '">' + adultOptions[key] + '</option>');
    			}
			}
			for (key in childOptions) 
			{
    			if (typeof(childOptions[key] == 'string')) 
				{
        			$j('#hotelSearch #hotelChildren').append('<option value="' + key + '">' + childOptions[key] + '</option>');
        			$j('#hotelSearch #hotelInfants').append('<option value="' + key + '">' + childOptions[key] + '</option>');
    			}
			}
			var $i = 1;
			$j('option', '#hotelSearch #hotelDuration').remove();
			for ($i; $i<31; $i++) 
			{
    			$j('#hotelSearch #hotelDuration').append('<option value="' + $i + '">' + $i + '</option>');
			};
			$j("#hotelSearch #hotelAdults").prop("selectedIndex", 2);
			$j("#hotelSearch #hotelChildren").prop("selectedIndex", 0);
			$j("#hotelSearch #hotelInfants").prop("selectedIndex", 0);
			$j("#hotelSearch #hotelDuration").prop("selectedIndex", 6);
	} 
	$j( "#hotelResorts" ).autocomplete({
	    source: function(request, response) {
	        $j.ajax({
	            url: '/proxy.php',
	            dataType: "json",
	            data: {
	                term : request.term,
	                url : "http://www.worldtravelbookers.net/ajax/index.php/ajax_jsonp/defaults/9/hotels/"
	            },
	            success: function(data) {
	                response(data);
	            }
	        });
	    },
		minLength: 2,
		select: function( event, ui ) {
			$j('#idResort').val(ui.item.idResort);
		}
	});
	$j( "#ajaxDepartures" ).autocomplete({
	    source: function(request, response) {
	        $j.ajax({
	            url: '/proxy.php',
	            dataType: "json",
	            data: {
	                term : request.term,
	                url : "http://www.worldtravelbookers.net/ajax/index.php/ajax_jsonp/defaults/8/departures/"
	            },
	            success: function(data) {
	                response(data);
	            }
	        });
	    },
		minLength: 0,
		select: function( event, ui ) {
			$j('#departureOutLocation').val(ui.item.idDeparture);
		}
	});

	$j('#holidayResorts').change(
		function() 
		{
	        var x = $j(this).val();
	        // and update the hidden input's value
	        $j('#idResort').val(x);
		}
	);

	$j('#holidayDepartureOutLocations').change(
		function() 
		{
	        var x = $j(this).val();
	        // and update the hidden input's value
	        $j('#departureOutLocation').val(x);
		}
	);

	$j.datepicker.setDefaults($j.datepicker.regional['']);
    $j("#hotelCheckInDate").datepicker({
        dateFormat: 'MM/m/d/yy',
        changeMonth: true,
        changeYear: true,
        showOn: "both",
		numberOfMonths: 2,
		maxDate: '+1y',
		regional: '',
        buttonImage: "/common/img/calendar.gif",
        buttonImageOnly: true,
        buttonImageText: "Calendar",
        beforeShow: function(dateText, instance) { 
            $j("#hotelCheckInDate").datepicker('setDate', new Date());
        },
        onSelect: function(dateText, instance) {
            var pieces = dateText.split("/");
			var dateFormat = pieces[2] + ' ' + pieces[0] + ' ' + pieces[3] + ' ';
             $j("#hotelCheckInDateDay").val(pieces[2]);
             $j("#hotelCheckInDateMonth").val(pieces[1]);
             //$j("#checkInDateYear").val(pieces[3]);
             $j("#hotelCheckInDate").val(dateFormat);
         }              
    });

	$j("#hotelSearch #hotelsDO #hotelSearchBtn").click(function() 
		{
			var returnValue = true ;
		}
	);

});
/* Standard Javascript to modify flexible holiday form */

	function changeInputType(tagName)
	{
	    var resortSelectAjax = document.getElementById('ajaxResortsDiv');
	    var departureSelectAjax = document.getElementById('ajaxDeparturesDiv');
	    var resortSelectDrop = document.getElementById('dropResortsDiv');
	    var departureSelectDrop = document.getElementById('dropDeparturesDiv');
		var res = document.getElementById("holidayResorts");
		var strRes = res.options[res.selectedIndex].value;
		var dep = document.getElementById("holidayDepartureOutLocations");
		var strDep = dep.options[dep.selectedIndex].value;
		if (tagName=="resort")
		{
			if (resortSelectAjax.style.display=="block")
			{
				resortSelectAjax.style.display="none";
				resortSelectDrop.style.display="block";
				document.getElementById('idResort').value=strRes;
			} else
			{
				resortSelectAjax.style.display="block";
				resortSelectDrop.style.display="none";				
			}
		} else if (tagName=="departure")
		{
			if (departureSelectAjax.style.display=="block")
			{
				departureSelectAjax.style.display="none";
				departureSelectDrop.style.display="block";
				document.getElementById('departureOutLocation').value=strDep;
			} else
			{
				departureSelectAjax.style.display="block";
				departureSelectDrop.style.display="none";				
			}
			
		}
		return true;
	}
/*
	function setVarsAndPost()
	{
	    var resortSelectAjax = document.getElementById('ajaxResortsDiv');
	    var departureSelectAjax = document.getElementById('ajaxDeparturesDiv');
	    var resortSelectDrop = document.getElementById('dropResortsDiv');
	    var departureSelectDrop = document.getElementById('dropDeparturesDiv');
		var res = document.getElementById("holidayResorts");
		var strRes = res.options[res.selectedIndex].value;
		var dep = document.getElementById("holidayDepartureOutLocations");
		var strDep = dep.options[dep.selectedIndex].value;
		alert(" setPostVars " + " " + resortSelectAjax.style.display + " " + resortSelectDrop.style.display + " " + departureSelectAjax.style.display + " " + departureSelectDrop.style.display);

		if (resortSelectDrop.style.display=="block")
		{
			alert("Resort Drop " + strRes);
			document.getElementById('idResort').val()=strRes;
		} 
		if (departureSelectDrop.style.display=="block")
		{
			alert("Departure Drop " + strDep);
			document.getElementById('departureOutLocation').val()=strDep;
		} 
		return true;
	}
*/

