$(document).ready(function() {
	init_loadfunctions();
	init_bookMod();
});
	
function init_loadfunctions() {

	init_widget_searchcruisedeals();
	$('form#search select').change(function(){ init_widget_searchcruisedeals(); });

	init_widget_cruisedeals();
	$('#cruise-small-search-panel select').change(function(){ init_widget_cruisedeals(); });
	
	init_frmBookIt();
	
	//hotel deals
	init_widget_hoteldeals_rooms();
	$('form[name="searchHotelForm"] #hotel-search-panel select#hotelRooms').change(function(){ init_widget_hoteldeals_rooms();  });	
	$('form[name="searchHotelForm"] #hotel-search-panel .roomchoices select').change(function(){ init_widget_hoteldeals_rooms();  });

	//city breaks
	init_widget_citybreaks();
	$('form[name="searchFlightAndHotelForm"] #all-search-panels select#flightHotelRooms').change(function(){ init_widget_citybreaks();  });	
	$('form[name="searchFlightAndHotelForm"] #all-search-panels .roomchoices select').change(function(){ init_widget_citybreaks();  });	
	
	//car hire
	init_widget_carhire();
	$('form[name="searchCarForm"] #car-search-panel #carSearchOccuracy label').click(function(){ init_widget_carhire();  });	

	init_widg_holidaydeals();

	//datepicker
	initDatePicker();
	//hints
	if(jQuery().hint) { $('input[title!=""]').hint(); }
}
	
function init_widget_cruisedeals() {

	if ( $('form[name="searchCruiseForm"] #cruise-small-search-panel').length > 0 ) {
	
		var url = '/searchFormDataFetch/virginListsSmall';

		var cruiseDepartDateSmall = $("#cruiseDepartDateSmall").val();
		var cruiseDurationSmall = $("#cruiseDurationSmall").val();
		var cruiseShipSmall = $("#cruiseShipSmall").val();
		var cruiseTypeSmall = $("#cruiseTypeSmall").val();
		var cruiseWhereSmall = $("#cruiseWhereSmall").val();
		var currentRequestPath = $("#currentRequestPath").val();
		var lastCruiseSearchSmall = $("#lastCruiseSearchSmall").val();
		var lastCruiseSearchSmallTiny = $("#lastCruiseSearchSmallTiny").val();
		var searchType = $("#searchType").val();
		var viewType = $("#viewType").val();
		
		$.post(
		url, 
		{ 
			cruiseDepartDateSmall: cruiseDepartDateSmall,
			cruiseDurationSmall: cruiseDurationSmall,
			cruiseShipSmall: cruiseShipSmall,
			cruiseTypeSmall: cruiseTypeSmall,
			cruiseWhereSmall: cruiseWhereSmall,
			currentRequestPath: currentRequestPath,
			lastCruiseSearchSmall: lastCruiseSearchSmall,
			lastCruiseSearchSmallTiny: lastCruiseSearchSmallTiny,
			searchType: searchType,
			viewType: viewType
		},
		function(data, textStatus) {
		
			if (textStatus == 'success')  {
	
				// this will give us an array of objects
				var dealsData = JSON.parse(data);
				
				var data_cruisetype = dealsData.cruisetype;
				var data_which = dealsData.which;
				var data_howlong = dealsData.howlong;
				var data_where = dealsData.where;
				var data_when = dealsData.when;
				
				//popukate departures
				var html = '<option value="xxx">Any departure month</option>';
		        for(var x=1; x < data_when.length; x++) {
					html += data_when[x];
				}
				$('#cruiseDepartDateSmall').html( html );
				$("#cruiseDepartDateSmall option[value='"+cruiseDepartDateSmall+"']").attr('selected', 'selected');
				
				//populate destinations
				html = '';
		        for(var x=0; x < data_where.length; x++) {
					html += data_where[x];
				}
				html = html.replace("<option value = 'xxx'>Anywhere</option>", '<option value="xxx">Any destination</option>');
				$('#cruiseWhereSmall').html( html );
				$("#cruiseWhereSmall option[value='"+cruiseWhereSmall+"']").attr('selected', 'selected');
				
				//populate cruises
				html = '';
		        for(var x=0; x < data_which.length; x++) {
					html += data_which[x];
				}
				html = html.replace("<option value = 'xxx'>I Don't Mind</option>", '<option value="xxx">All lines and ships</option>');
				$('#cruiseShipSmall').html( html );
				$("#cruiseShipSmall option[value='"+cruiseShipSmall+"']").attr('selected', 'selected');
				
				//enable form
				$('#searchSmall .searchWaitingMsg').removeClass("show");

			}
		},'text');
	
	}
	
}




function init_widget_searchcruisedeals() {

	if ( $('form[name="searchCruiseForm"] #cruise-search-panel').length > 0 ) {

		var url = '/searchFormDataFetch/virginLists';
		//var url = '/virginLists.php'
		var cruiseDepartDate = $("#cruiseDepartDate").val();
		var cruiseDuration = $("#cruiseDuration").val();
		var cruiseShip = $("#cruiseShip").val();
		var cruiseType = $("#cruiseType").val();
		var cruiseWhere = $("#cruiseWhere").val();
		var currentRequestPath = $("#currentRequestPath").val();
		var lastCruiseSearch = $("#lastCruiseSearch").val();
		var lastCruiseSearchSmall = $("#lastCruiseSearchSmall").val();
		var searchType = $("#searchType").val();
		var viewType = $("#viewType").val();
		
		$.post(
		url, 
		{ 
			cruiseDepartDate: cruiseDepartDate,
			cruiseDuration: cruiseDuration,
			cruiseShip: cruiseShip,
			cruiseType: cruiseType,
			cruiseWhere: cruiseWhere,
			currentRequestPath: currentRequestPath,
			lastCruiseSearch: lastCruiseSearch,
			lastCruiseSearchSmall: lastCruiseSearchSmall,
			searchType: searchType,
			viewType: viewType
		},
		function(data, textStatus) {
		
			if (textStatus == 'success')  {
	
				var html = '';
				// this will give us an array of objects
				var dealsData = JSON.parse(data);
				
				var data_cruisetype = dealsData.cruisetype;
				var data_which = dealsData.which;
				var data_howlong = dealsData.howlong;
				var data_where = dealsData.where;
				var data_when = dealsData.when;
				
				//populate where
		        for(var x=0; x < data_where.length; x++) {
					html += data_where[x];
				}
				$('#cruiseWhere').html( html );
				$("#cruiseWhere option[value='"+cruiseWhere+"']").attr('selected', 'selected');
				
				//populate durations
				html = '';
		        for(var x=0; x < data_howlong.length; x++) {
					html += data_howlong[x];
				}
				$('#cruiseDuration').html( html );
				$("#cruiseDuration option[value='"+cruiseDuration+"']").attr('selected', 'selected');
				
				//populate when
				html = '';
		        for(var x=0; x < data_when.length; x++) {
					html += data_when[x];
				}
				$('#cruiseDepartDate').html( html );
				$("#cruiseDepartDate option[value='"+cruiseDepartDate+"']").attr('selected', 'selected');
				
				//populate which
				html = '';
		        for(var x=0; x < data_when.length; x++) {
					html += data_which[x];
				}
				$('#cruiseShip').html( html );
				$("#cruiseShip option[value='"+cruiseShip+"']").attr('selected', 'selected');
				
				//populate type
				html = '';
		        for(var x=0; x < data_cruisetype.length; x++) {
					html += data_cruisetype[x];
				}

				
				$('#cruiseType').html( html );
				$("#cruiseType option[value='"+cruiseType+"']").attr('selected', 'selected');
				
				//enable form
				$('#search .searchWaitingMsg').removeClass("show");

			}
		},'text');
	}
}

function init_frmBookIt() {

	if ( $('form[name="searchHolidayForm"] #holiday-search-panel').length > 0 ) {

		$('form[name="searchHolidayForm"]').submit(function() {
		
			//bugfix: if not visible do not continue...
			if ($('form[name="searchHolidayForm"]').length < 1) { return; }

			//exciting validation checks
			var txtError = '';
			var holidayFrom = $('#holidayFrom').val();
			var holidayTo = $('#holidayTo').val();
			var holidayDepart = $('#holidayDepart').val();
			var holidayRoom1Adults = $('#holidayRoom1Adults').val();

			var todayDate=new Date();

			$('form[name="searchHolidayForm"] .error').html(txtError);

			if ( holidayFrom == '' ) { 
				txtError = 'Please select a departure airport.'; 
			} else if ( holidayTo == '' || holidayTo == 'Destination' ) { 
				txtError = 'Please select a destination.'; 
			} else if (holidayDepart=='' || !isValidDate(holidayDepart)) {
				txtError = 'Please enter a valid date.';
			} else if (todayDate > (dateConvert(holidayDepart)) ) { 
				txtError = 'Departure date must be after todays date.';
			}  else if ( holidayRoom1Adults < 1 ) { 
				txtError = 'You must select number of adults.'; 
			}			
			
			bookingErrorDisplay(txtError);
			if (txtError != '') { return false; }

			return true;

		});

		//setup departure select box
		$('select#holidayFrom').change(function(){ 
			var url = '/searchFormDataFetch/destinationTo';
			//var url = '/destinationTo.php'	
			var destNameParam = $(this).val();
			var html = '';
			
			$.post(
			url, 
			{  destNameParam: destNameParam },
			function(data, textStatus) {
				if (textStatus == 'success')  {

					var dealsData = JSON.parse(data);
					
					//populate departures
			        for(var x=0; x < dealsData.length; x++) {
						html += '<option value="'+dealsData[x]+'">'+dealsData[x]+'</option>';
					}
					var currentValue = $('#holidayTo').val() ;
					$('#holidayTo').html( html );
					$("#cruiseDepartDateSmall option[value='"+currentValue+"']").attr('selected', 'selected');
					$('.toSubLabel[for="cosmosRegionTo"]').css('display', 'none');
					$('.toSubLabel[for="cosmosResortTo"]').css('display', 'none');
				}
			},'text');
		});
		
		//setup destionation select box
		$('select#holidayTo').change(function(){ 
			var destNameParam = $(this).val();
			var url = '/searchFormDataFetch/cosmosRegionTo?destNameParam='+destNameParam;
			//var url = '/cosmosRegionTo.php'	
			var html = '';
			$.post(
			url, 
			{  destNameParam: destNameParam },
			function(data, textStatus) {
				if (textStatus == 'success')  {
					var dealsData = JSON.parse(data);					
					//populate departures
			        for(var x=0; x < dealsData.length; x++) {
						html += '<option value="'+dealsData[x]+'">'+dealsData[x]+'</option>';
					}
					$('#cosmosRegionTo').html( html );
					$('.toSubLabel[for="cosmosRegionTo"]').css('display', 'block');
					$('.toSubLabel[for="cosmosResortTo"]').css('display', 'none');
				}
			},'text');
		});
		
		//setup region select box
		$('select#cosmosRegionTo').change(function(){ 
			var destNameParam = $(this).val();
			var url = '/searchFormDataFetch/cosmosResortTo?destNameParam='+destNameParam;
			//var url = '/cosmosResortTo.php'	
			var html = '';
			$.post(
			url, 
			{  destNameParam: destNameParam },
			function(data, textStatus) {
				if (textStatus == 'success')  {
					var dealsData = JSON.parse(data);					
					//populate departures
			        for(var x=0; x < dealsData.length; x++) {
						html += '<option value="'+dealsData[x]+'">'+dealsData[x]+'</option>';
					}
					$('#cosmosResortTo').html( html );
					$('.toSubLabel[for="cosmosResortTo"]').css('display', 'block');
				}
			},'text');
		
		});
	}
}
	
//book it hotel deals
function init_widget_hoteldeals_rooms() {

	var formid = 'form[name="searchHotelForm"] #hotel-search-panel ';
	if ( $(formid).length > 0 ) {
	
	
		$('form[name="searchHotelForm"]').submit(function() {

			//bugfix: if not visible do not continue...
			if ($('form[name="searchHotelForm"]').length < 1) { return; }
		
			//exciting validation checks		
			var txtError = '';
			var hotelLocation = $('#hotelLocation').val();
			var hotelLocationTitle = $('#hotelLocation').attr('title');
			var hotelDepart = $('#hotelDepart').val();
			var hotelReturn = $('#hotelReturn').val();
			var todayDate=new Date();
			//erase time stamp on todays date
			todayDate=new Date(todayDate.getUTCFullYear(), todayDate.getUTCMonth(), todayDate.getUTCDate() );	
			
			if ( hotelLocation == '' || hotelLocation == hotelLocationTitle) { 
				txtError = 'Please enter a destination'; 
			} else if ( hotelDepart=='' || !isValidDate(hotelDepart) ) {
				txtError = 'Please enter a valid check-in date.';
			} else if ( hotelReturn=='' || !isValidDate(hotelReturn) ) {
				txtError = 'Please enter a valid check-out date.';
			} else if (todayDate > (dateConvert(hotelDepart)) ) { 
				txtError = 'Check-in date can not be a past date.';
			}  else if ( (dateConvert(hotelReturn)) < (dateConvert(hotelDepart)) ) { 
				txtError = 'Check-out date must be not be before check-in date.'; 
			} 
			
			bookingErrorDisplay(txtError);
			if (txtError != '') { return false; }
			
			return true;
		});
	
	

		var hotelRooms = $(formid+"#hotelRooms").val();
		$(formid+'div.roomchoices:gt(0)').hide();
		$(formid+'div.roomchoices:lt('+hotelRooms+')').show();
		$(formid+'#hotelChildRoomsWrapper').hide();
		$(formid+'#hotelChildRooms div select').hide();
		$(formid+'#hotelChildRooms ul.labels li:gt(0)').hide();

		$(formid+".roomchoices").each(function (i) {
		
			var roomChildrens = $(formid+'select#hotelRoom'+(i+1)+'Children').val();
		
			if( $(this).is(":visible") && ( roomChildrens > 0) ){
			
				for (i2=1;i2<=roomChildrens;i2++)
				{
					$(formid+'select#hotelRoom'+(i+1)+'Child'+i2 ).show();
					$( formid+'#hotelChildRooms ul.labels li:eq('+(i2-1)+')'  ).show();
				}
				$(formid+'#hotelChildRoomsWrapper #hotelChildRoom'+(i+1) ).show();
				$(formid+'#hotelChildRoomsWrapper').show();
   			} else {
				$(formid+'#hotelChildRoomsWrapper #hotelChildRoom'+(i+1) ).hide();
			}
		});
	}
}




function init_widget_citybreaks() {
	var formid = 'form[name="searchFlightAndHotelForm"] ';
	
	if ( $(formid).length > 0 ) {
	
		$(formid).submit(function() {
		
			//bugfix: if not visible do not continue...
			if ($('form[name="searchFlightAndHotelForm"]').length < 1) { return; }

			//exciting validation checks
			var txtError = '';
			var flightHotelFrom = $('#flightHotelFrom').val();
			var flightHotelTo = $('#flightHotelTo').val();
			var flightHotelDepart = $('#flightHotelDepart').val();
			var flightHotelReturn = $('#flightHotelReturn').val();
			var flightHotelRoom1Adults = $('#flightHotelRoom1Adults').val();
			var todayDate=new Date();
			//erase time stamp on todays date
			todayDate=new Date(todayDate.getUTCFullYear(), todayDate.getUTCMonth(), todayDate.getUTCDate() );	

			//error checks
			if ( flightHotelFrom == '' ) {
				txtError = 'Please select a departure airport.';
			} else if ( flightHotelTo == '' || flightHotelTo == 'Destination' ) { 
				txtError = 'Please select a destination.';
			} else if ( flightHotelDepart=='' || !isValidDate(flightHotelDepart) ) {
				txtError = 'Please enter a valid depart date.';
			} else if ( flightHotelReturn=='' || !isValidDate(flightHotelReturn) ) {
				txtError = 'Please enter a valid return date.';
			} else if (todayDate > (dateConvert(flightHotelDepart)) ) { 
				txtError = 'Departure date must not be a past date.';
			}  else if ( (dateConvert(flightHotelReturn)) < (dateConvert(flightHotelDepart)) ) { 
				txtError = 'Return date must be not be before departing date.';
			} else if ( flightHotelRoom1Adults < 1 ) {
				txtError = 'Please select number of adults for the trip.'; 
			}

			bookingErrorDisplay(txtError);
			if (txtError != '') { return false; }

			return true;
		});
	
		//initialise default form settings
		var flightHotelRooms = $("#flightHotelRooms").val();
		$(formid+'#flightHotel-search-panel div.roomchoices:gt(0)').hide();
		$(formid+'#flightHotel-search-panel div.roomchoices:lt('+flightHotelRooms+')').show();
		$(formid+'#flightHotelChildRoomsWrapper').hide();
		$(formid+'.childrooms div select').hide();
		$(formid+'#flightHotelChildRooms ul.labels li:gt(0)').hide();

		//read 'room requirements'
		$(formid+"#flightHotel-search-panel .roomchoices").each(function (i) {
		
			var roomChildrens = $('select#flightHotelRoom'+(i+1)+'Children', this).val();
			if( $(this).is(":visible") && ( roomChildrens > 0) ){

				//set child room settings depending on 'room requirements'
				for (i2=1;i2<=roomChildrens;i2++)
				{
					$(formid+'select#flightHotelRoom'+(i+1)+'Child'+i2 ).show();
					$( formid+'#flightHotelChildRooms ul.labels li:eq('+(i2-1)+')'  ).show();
				}
				$(formid+'#flightHotelChildRoomsWrapper #flightHotelChildRoom'+(i+1) ).show();
				$(formid+'#flightHotelChildRoomsWrapper').show();
				
   			} else {
				$(formid+'#flightHotelChildRoomsWrapper #flightHotelChildRoom'+(i+1) ).hide();
			}
		});
	}
}


function initDatePicker() {

	$(".datefield").each(function (i) {
		var todayDate=new Date();
		var strClass = $(this).attr("class");		//list of classes
		if ( strClass.indexOf('add-days-') > 0 ) {
			var dayIncrement = ( strClass.substr( (strClass.lastIndexOf('add-days-') + 9), 2 )  ) ;
			todayDate = new Date(todayDate.getTime()+(dayIncrement * 86400000));
		}
		
		//add leading zero if needed
		var month = todayDate.getUTCMonth()+1;
		var tmpDate = todayDate.getUTCDate();
		if (month < 10) { month = '0'+month; }
		if (tmpDate < 10) { tmpDate = '0'+tmpDate; }
		
		//populate date
		$(this).val( tmpDate + '/' + month + '/' + todayDate.getUTCFullYear() );
	});

	if(jQuery().datepicker) {
		$(".datefield").datepicker({ 		//date picker
			showOn: 'button',
			buttonImage: '/system/modules/com.sky.travel/assets/images/icons/cal.gif',
			buttonImageOnly: true,
			dateFormat: 'dd/mm/yy',
		   	beforeShow: function(input) {  
				//apply ie6 fix for calender appearing behind '<select>' tags
				if (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6) { 
					$('form[name="searchFlightAndHotelForm"] #flightHotelChildRoomsWrapper select').css('visibility','hidden');
					$('form[name="searchHotelForm"] #hotel-search-panel #hotelChildRooms select').css('visibility','hidden');
					$('form[name="searchHolidayForm"] select#holidayRoom1Infants').css('visibility','hidden');
				}
			},
			onClose: function(dateText, inst) { 
				//deactivate ie6 fix for calender appearing behind '<select>' tags
				if (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6) { 
					$('form[name="searchFlightAndHotelForm"] #flightHotelChildRoomsWrapper select').css('visibility','visible');
					$('form[name="searchHotelForm"] #hotel-search-panel #hotelChildRooms select').css('visibility','visible');
					$('form[name="searchHolidayForm"] select#holidayRoom1Infants').css('visibility','visible');
				}
			}
		});
	}
	
	$(".ui-datepicker-trigger").each(function (i) { $(this).attr('alt', 'calendar'); $(this).attr('title', 'calendar'); });
}

function init_widget_carhire() {
	var formid = 'form[name="searchCarForm"] #car-search-panel ';
	
	if ( $(formid).length > 0 ) {

		$('form[name="searchCarForm"]').submit(function() {
		
			//bugfix: if not visible do not continue...
			if ($('form[name="searchCarForm"]').length < 1) { return; }
		
			//exciting validation checks		
			var txtError = '';
			var carLocation = $('#carLocation').val();
			var carLocationTitle = $('#carLocation').attr('title');
			var carDepart = $('#carDepart').val();
			var carReturn = $('#carReturn').val();
			var todayDate=new Date();
			//erase time stamp on todays date
			todayDate=new Date(todayDate.getUTCFullYear(), todayDate.getUTCMonth(), todayDate.getUTCDate() );	
			
			if ( carLocation == '' || carLocation == carLocationTitle) { 
				txtError = 'Please enter a pick up location'; 
			} else if ( carDepart=='' || !isValidDate(carDepart) ) {
				txtError = 'Please enter a valid depart date.';
			} else if ( carReturn=='' || !isValidDate(carReturn) ) {
				txtError = 'Please enter a valid depart date.';
			} else if (todayDate >= (dateConvert(carDepart)) ) { 
				txtError = 'Pick-up date must be after todays date.';
			}  else if ( (dateConvert(carReturn)) < (dateConvert(carDepart)) ) { 
				txtError = 'Return date must be not be before pick-up date.'; 
			} 
			
			bookingErrorDisplay(txtError);
			if (txtError != '') { return false; }

			return true;
		});
	
		$(formid+'.renderHidden').hide();
		if ( $(formid+' input[name="carLocationSelect"]:checked').val() == 2 ) {
			$(formid+'.renderHidden').show();
		}
	}
}

//global booking form
function init_bookMod() {
	if ( $('#box_bookall').length > 0 ) {

		//settings
		var defaultChecked = 0;
		var modeSelectorBox = $('input[name="modeSelectorBox"]:checked').val();
		
		//radio actions
		$('input[name="modeSelectorBox"]').click(function(){ 
				
			$('#box_bookall #bookit_preloader').show();
			bookingErrorDisplay('');
				
			var dirHtml = '/system/modules/com.sky.travel/assets/html/';
			//var dirHtml = '/assets/html/';
			
			modeSelectorBox = $(this).val();
			$('#box_bookall form').attr('action', '/search/'+modeSelectorBox+'/');
			$('input#searchType').attr('value', modeSelectorBox);
			$('#all-search-panels div').removeClass('active');
			
			switch(modeSelectorBox) {
				case 'holiday':

					var innerDiv = 'holiday-search-panel';
					var bookFormName = 'searchHolidayForm';
					break;

				case 'hotel':

					var innerDiv = 'hotel-search-panel';
					var bookFormName = 'searchHotelForm';
					break;

				case 'cruise':

					var innerDiv = 'cruise-search-panel';
					var bookFormName = 'searchCruiseForm';
					break;

				case 'flightAndHotel':

					var innerDiv = 'flightHotel-search-panel';
					var bookFormName = 'searchFlightAndHotelForm';
					break;

				case 'carHire':
					var innerDiv = 'car-search-panel';
					var bookFormName = 'searchCarForm';
					break;
				default:
					break;
			}
			
			//initialise new variables
			var content = $('#'+innerDiv).html();
			
			$('#box_bookall form#search').attr('name', bookFormName);
						
			if ( content == '' ) {
				$.get(dirHtml+innerDiv+".html", function(data){
					var dataHtml = $(data).html();
					$('#'+innerDiv).html( dataHtml );
					init_loadfunctions();
					$('#box_bookall #bookit_preloader').hide();
				});
			} else {
				$('#box_bookall #bookit_preloader').hide();
			}
			
			$('#'+innerDiv).addClass('active');
			
		});	

		//inititalise

		if (modeSelectorBox == '' || modeSelectorBox == undefined) { 
			$('input[name="modeSelectorBox"]:eq('+defaultChecked+')').click();
		} else {
			$('input[value="'+modeSelectorBox+'"]').click();
		}
	}
}

//holiday deals widget
function init_widg_holidaydeals() {

	nodeId = '#widg_holidaydeals ';


	if ( $(nodeId).length > 0 ) {
	
		//setup html
	    $(nodeId+"dd li.expandable").before('<li class="control"><a class="nb" href="#">Show all destinations</a></li>'); 
		$(nodeId+"dd li.expandable").hide();
		$('#widg_holidaydeals .cc').html( $('#widg_holidaydeals .cc').html() + '<p id="cls4" class="close_controller"><a class="nb" href="#">Close</a></p>' );

		//split up lists in 2 columns
	     $('.split').each(function() {
	          if($(this).is("ol")) { var ordered = true; }
	          var colsize = Math.round($(this).find("li").size() / 2);
	          $(this).find("li").each(function(i) {
	               if (i>=colsize) {
	                    $(this).addClass('right_col');
	               }
	          });
	          if(ordered) {
	               $(this).find('.right_col').insertAfter(this).wrapAll("<ol class='split' start='" + (colsize+1) + "'></ol>").removeClass("right_col");
	          } else {
	               $(this).find('.right_col').insertAfter(this).wrapAll("<ul class='split even'></ul>").removeClass("right_col");
	          }
	     });

	
		//click functionality
		$('#widg_holidaydeals dd li.control').click(function() {
			var prevnode = $(this).prev().hide();
			var nextnode = $(this).next().show();
			$(this).hide();
			return false;
		});
	
		$('#widg_holidaydeals dt').click(function() {
			$(this).toggleClass('active');
	  		$(this).next().toggle();
			return false;
		});
		
		$('#widg_holidaydeals .cc .close_controller').click(function() {
	
	  		$(this).parent().toggle();
			return false;
		});	
	}
}

function bookingErrorDisplay(message) {
	$('#search .error').removeClass('normal');
	$('#search .error').html(message);
	if (message != '') { 	$('#search .error').addClass('normal'); }
}

function dateConvert( strDate ) {
	//converting dd/mm/yy to date
	var mySplitResult = strDate.split("/");
	var mynewString = mySplitResult[1] + "/" + mySplitResult[0] + "/" + mySplitResult[2]; 
	return new Date(mynewString);
}

function isValidDate(sText) {
	var reDate = /(?:0[1-9]|[12][0-9]|3[01])\/(?:0[1-9]|1[0-2])\/(?:19|20\d{2})/;
	return reDate.test(sText);
}

function days_between(date1, date2) {
    // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24
    // Convert both dates to milliseconds
    var date1_ms = date1.getTime()
    var date2_ms = date2.getTime()
    // Calculate the difference in milliseconds
    var difference_ms = Math.abs(date1_ms - date2_ms)
    // Convert back to days and return
    return Math.round(difference_ms/ONE_DAY)
}

