var json = '';

$(document).ready(function(){
	$("#selectCities").ajaxStart(function(){
		startLoading('#selectCities');
	});
});

function sendValues(channel,searchtype,hotelID) {
	
	var checkin_date = $('#CheckInDate').val(),
	checkout_date = $('#CheckOutDate').val(),
	vehicle_date = $('#VehiclePickUp').val(),
	citysel = $('#departure_city_id'),
	serverfile = $('#serverpath');
	
	selectStatus = !citysel ? 0 : citysel.val();
	
	if (hotelID) selectStatus = hotelID;
	
	if ( searchtype == 'PFS' || searchtype == 'PCS' ) {
		checkin_date = checkout_date;
		checkout_date = vehicle_date;
	}
	
	var paramsString = 'channel='+channel+'&searchtype='+searchtype+'&checkin='+checkin_date+'&checkout='+checkout_date+'&selected='+selectStatus;

	$.ajax({
		type: "GET",
		url: path + "/reservation/searchbox.ajax.php",
		data: paramsString,
		dataType: 'json',
		success: function(data){
			
			json = data;
			window.setTimeout(function(){
					
				var cityselect = $('#selectCities');
				var cityselect2 = $('#selectCities2');
				if (!cityselect){
					return;
				}
				
				cityselect.empty();
				cityselect2.empty();
				json.status = (json.status=='disabled')?true:false;
				
				if ( json.empty || json.data == null )
				{
					var select = $(document.createElement('select')).attr({id:'departure_city_id', name: 'departure_city_id', disabled : json.status}).prependTo(cityselect);
					$(document.createElement('option')).attr('value','0').text('Change Dates').appendTo(select);
					return false;
				}
				
				if (channel == 2 && json.selected != '')
				{
					referredUser(cityselect,json);
					return;
				}
				
				if (channel == 2)
				{
					var select = $(document.createElement('select')).attr({id:'departure_country_id', name: 'departure_country_id', disabled : json.status , 'onchange' : 'javascript:getCity(this);' }).appendTo(cityselect);
					$(document.createElement('option')).attr('value','0').text('Select a Country').appendTo(select);
					
					for (i = 0; i < json.data.length; i++){
						$(document.createElement('option')).attr('value',json.data[i]['country']).text(json.data[i]['country']).appendTo(select);
					}
					
					var select2 = $(document.createElement('select')).attr({id:'departure_city_id', name: 'departure_city_id', disabled : 'disabled' }).appendTo(cityselect2);
					$(document.createElement('option')).attr('value','0').text('Select a City').appendTo(select2);
					
					$('#departure_country_id').change( function(){
						getCity(this);
					});
				}
				else
				{
					var select = $(document.createElement('select')).attr({id:'departure_city_id', name: 'departure_city_id',disabled : json.status}).prependTo(cityselect);
					$(document.createElement('option')).attr('value','0').text('Select a City').appendTo(select);
					
					for (i = 0; i < json.data.length; i++){
						var optgroup = $(document.createElement('optgroup')).attr('label',json.data[i]['country']);
	
						for (y = 0; y < json.data[i]['cities'].length; y++){
							var element = $(document.createElement('option')).text(json.data[i]['cities'][y]['name']).attr('value', json.data[i]['cities'][y]['id']);

							if (json.selected == json.data[i]['cities'][y]['id'])
								element.attr('selected', true);
							
							element.appendTo(optgroup);
						}
						optgroup.appendTo(select);
					}
				}
			},500);
		}
	});
	
}

function getCity(obj)
{
	var select2 = $('#selectCities2');
	startLoading('#selectCities2');
	
	window.setTimeout(function() {
		select2.empty();
		var select = $(document.createElement('select')).attr({'id':'departure_city_id','name':'departure_city_id'}).appendTo(select2);
		$(document.createElement('option')).attr('value','0').text('Select a City').appendTo(select);
					
		for (i = 0; i < json.data.length; i++){
			if (obj.value == json.data[i]['country'])
			{
				for (y = 0; y < json.data[i]['cities'].length; y++){
					$(document.createElement('option')).attr('value',json.data[i]['cities'][y]['id']).text(json.data[i]['cities'][y]['name']).appendTo(select);			
				}
				return;
			}
		}
	},500);
}

function startLoading(id){
	var image = $(document.createElement('img')).attr('src', '/reservation/images/misc/loader.gif');
	$(id).html(image);
}

function referredUser(cityselect,list)
{
	var cityselect = $('#selectCities'),
	cityselect2 = $('#selectCities2'),
	countrySelected = false,
	citySelected	= false;
	
	var select = $(document.createElement('select')).attr({id:'departure_country_id', name: 'departure_country_id', disabled : json.status }).appendTo(cityselect);
	$(document.createElement('option')).attr('value','0').text('Select a Country').appendTo(select);
	
	var select2 = $(document.createElement('select')).attr({id:'departure_city_id', name: 'departure_city_id' }).appendTo(cityselect2);
	$(document.createElement('option')).attr('value','0').text('Select a City').appendTo(select2);
	
	for (i = 0; i < json.data.length; i++){
		for (y = 0; y < json.data[i]['cities'].length; y++){
			
			if (json.data[i]['cities'][y]['id'] == json.selected){
				for (z = 0; z < json.data[i]['cities'].length; z++){
					if ( json.data[i]['cities'][z]['id'] == json.selected ){
						citySelected = true;
					}
					$(document.createElement('option')).attr({'value':json.data[i]['cities'][z]['id'],'selected':citySelected }).text(json.data[i]['cities'][z]['name']).appendTo(select2);
					citySelected = false;
				}
				countrySelected = true;
			}
		}
		
		$(document.createElement('option')).attr({'value':json.data[i]['country'],'selected':countrySelected }).text(json.data[i]['country']).appendTo(select);
		if (countrySelected)
		{
			$('#departure_country_id').attr('disabled',json.status);
			$('#departure_city_id').attr('disabled',json.status);
		}
		countrySelected = false;
	}
	
	$('#departure_country_id').change( function(){
		getCity(this);
	});
}