	
	var input_arrival = null;
	var input_departure = null;

    // Switch Galery Images
	function showImage ( path,id ) {
	
		var newPath = '/cache/modules/hotels/models/gallery_image/image/' + id;
		$("#largeimage").attr("src",newPath);
        $("div.lrg_descrip").html ( $( 'div.lrg_descrip_rep_' + id ).html() )
	};	
    
    //Enable/show element
	function enable ( elem )
	{
		elem.removeAttr ( "disabled" );
		elem.show();
	}
	
    // Disable/Hide element
	function disable ( elem )
	{
		elem.attr ( "disabled", "disabled" );
		elem.hide();
	}	
	
    function book_rooms ()
    {
        ret = false;
        var arrival = $("#QuickBookerTable input#arrival").val();
        var departure = $("#QuickBookerTable input#departure").val();
        var rooms = [];
        
        var index = 0;
        $('select.no_rooms').each( function () 
            {
                if ( $(this).val() > 0 )
                {
                    rooms[index] = $(this).attr('id').substr(9) + ':' + $(this).val();
                    index++;
                }
            });
            
        if ( rooms.length < 1 )
        {
            alert( 'Please select a room in order to book!' );           
        }
        else
        {
            $('input#booked_rooms').val( rooms.join( "-" ));
            $('input#arrival_date').val( arrival );
            $('input#departure_date').val( departure );
            
            $('form#frm_booknow').submit();
            ret = true;
        }
        return ret;
    }
    
	function go_back_rooms ( action )
	{
		// Set known variables first
		var ret = false;
		var hotel_id = $("#QuickBooker input#quick_hotel_id").val();
		var arrival = $("#QuickBookerTable input#arrival").val();
		var departure = $("#QuickBookerTable input#departure").val();
		//var adults = $("#QuickBookerTable select#adults").val();
		//var children = $("#QuickBookerTable select#children").val();
		//var total_ppl = +adults + +children;
		var rooms = $("#QuickBookerTable select#rooms").val();
		var room_id_ppl = $("#QuickBookerTable select#rooms_type").val().split ("_");
		var rooms_type = room_id_ppl[0];
		var count = 0;
		
		// Fetch and set room grid variables
		
		var url = "?hotel_id=" + hotel_id + "&arrival=" + arrival + "&departure=" + departure + "&adults=" + adults + "&children=" + children + "&rooms=" + rooms + "&rooms_type=" + rooms_type;
		
		if ( action == 'book_rooms' )  // Daily Rates Booking Button 
		{			
			var rooms = new Array();
			var checked_rooms = new Array();
			var no_rooms = new Array();
			
			// Checks if ppl are booking for the correct number of ppl
			var ppl = true;
			var max_occupancy = 0;
			$('input:checkbox:checked').each( function () 
			{
				no_roomss= $('select#no_rooms_' + $(this).attr ('rel') ).val();
				max_occupancy += +$('td#occupancy_' + $(this).attr ('rel') ).html() * +no_roomss;
			});
			if ( total_ppl != max_occupancy )
			{
				alert ( 'The number of room types selected does not accommodate the amount of people intially chosen.' + "\n" + 'Please select the appropriate room types or change the amount of people by using the "Quick QuickBookerTable" below' );
				return ret;
			}
			else
			{
				$('input:checkbox:checked').each( function () 
				{ 			
					if ( $('select#no_rooms_' + $(this).attr ('rel') ).val() > 0 )
					{
						checked_rooms[count] = $(this).attr ('rel');
						no_rooms[count] = $('select#no_rooms_' + checked_rooms[count] ).val();
						count++;
					}
				});			
			
				var ch_rooms = checked_rooms.join ("_");
				var n_rooms = no_rooms.join ("_");

				if ( ch_rooms.length == 0 )
				{
					alert ( 'Please select a room/s to book!' );
					return false;
				}
				else
				{
					// make sure there are checked rooms
					if ( checked_rooms.length > 0 )
					{
						$('form#booknow input#booknow_checked_rooms').val( ch_rooms );
					}
					
					if ( no_rooms.length > 0 )
					{
						$('form#booknow input#booknow_no_rooms').val( n_rooms );
					}		
					
					ret = true;
				}
			}
			
			if ( ret = true ) 
			{
				$('form#booknow input#booknow_arrival').val( arrival );
				$('form#booknow input#booknow_departure').val( departure );
				//$('form#booknow input#booknow_adults').val( adults ) ;
				//$('form#booknow input#booknow_children').val( children );
				//$('form#booknow input#booknow_rooms').val( rooms ) ;
				//$('form#booknow input#booknow_rooms_type').val( rooms_type );			
				$('form#booknow').submit();
			}
			return ret;
		}
		else if ( action == 'quick_book_room' )
		{			
			if ( ( +room_id_ppl [1] * +rooms ) == total_ppl )
			{
				ret = true;
			}
			
			if ( ret == true )
			{
				window.location.replace ( "/hotels/book_now" + url );	
			}
			else
			{
				alert ( "The number of room types selected does not accommodate the amount of people initially chosen.\n Please select the appropriate room types or change the amount of people by using the \"Quick Booker\" below" ) ;
				
			}
		}
	}
	
	function validate_QuickBookerTable ( )
	{		
		var ret = true
        errors = '';
        
        if ( $("#QuickBookerTable input#arrival").val() == "" )
		{
			alert ( 'Please select an arrival date!' );
			$("#QuickBookerTable input#arrival").focus();
			ret = false;
            return ret;
		}

		if ( $("#QuickBookerTable input#departure").val() == "" )
		{
			alert ( 'Please select a departure date!' );
			$("#QuickBookerTable input#departure").focus();
			ret = false;
            return ret;
		}

		if ( $("#QuickBookerTable input#arrival").val() == $("#QuickBookerTable input#departure").val() )
		{
			alert ( 'Please select a later departure date!' );
			$("#QuickBookerTable input#departure").focus();
			ret = false;
            return ret;
		}

		if ( $("#QuickBookerTable input#arrival").val() > $("#QuickBookerTable input#departure").val() )
		{
			alert ( 'Please select a later departure date!' );
			$("#QuickBookerTable input#departure").focus();
			ret = false;
            return ret;
		}
        
		return ret;
	}
	
	$(function()
	{		
		var startDate = $('#QuickBookerTable input#arrival').val();
		var endDate = $('#QuickBookerTable input#departure').val();
		
		$('#QuickBookerTable input#arrival').bind(
			'dpClosed',
			function(e, selectedDates)
			{
				var d = selectedDates[0];
				if (d) {
					$('#QuickBookerTable input#departure').dpSetStartDate(d.addDays(1).asString());					
				}
				
				if($(this).val() != startDate){
					re_set_message ();
				}
			}			
		);
		$('#QuickBookerTable input#departure').bind(
			'dpClosed',
			function(e, selectedDates)
			{
				var d = selectedDates[0];
				if (d) {
					$('#QuickBookerTable input#arrival').dpSetEndDate(d.addDays(-1).asString());
				}
				
				if($(this).val() != endDate){
					re_set_message ();
				}
			}
		);
		
	});
	
	function CurrencyFormatted(amount)
	{
		var i = parseFloat(amount);
		if(isNaN(i)) { i = 0.00; }
		var minus = '';
		if(i < 0) { minus = '-'; }
		i = Math.abs(i);
		i = parseInt((i + .005) * 100);
		i = i / 100;
		s = new String(i);
		if(s.indexOf('.') < 0) { s += '.00'; }
		if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
		s = minus + s;
		return s;
	}

	function CommaFormatted(amount)
	{
		var delimiter = ","; // replace comma if desired
		var a = amount.split('.',2)
		var d = a[1];
		var i = parseInt(a[0]);
		if(isNaN(i)) { return ''; }
		var minus = '';
		if(i < 0) { minus = '-'; }
		i = Math.abs(i);
		var n = new String(i);
		var a = [];
		while(n.length > 3)
		{
			var nn = n.substr(n.length-3);
			a.unshift(nn);
			n = n.substr(0,n.length-3);
		}
		if(n.length > 0) { a.unshift(n); }
		n = a.join(delimiter);
		if(d.length < 1) { amount = n; }
		else { amount = n + '.' + d; }
		amount = minus + amount;
		return amount;
	}
	
	function update_total_price ()
	{
		var total_checked = 0;
		var total_price = 0;
		var checked_rooms = new Array();
		var count = 0;
		var base = $('input.currency').val();
		$('select.no_rooms').each( function () 
			{ 
				var id = $(this).attr ('id').slice( 9 );
				total_checked++;
				checked_rooms[count] = id;
				total_price += (+$( 'input#room_name_' + id ).val() * +$('select#no_rooms_'+id).val());
				count++;
			});
		$('span#yccConvert').attr ( 'amount', +total_price + ' ' + base );
		$('span#yccConvert').html( +total_price + base);
		$('span#yccConvert').trigger ('click');
	}
	
	$(document).ready ( function ()
		{
			// Store the locations of the fields we are going to use
			//input_book = $("input:button.booking");
			input_arrival = $("#QuickBookerTable input#arrival");
			//input_availability = $("input#availability");
			input_departure = $("#QuickBookerTable input#departure");
			
			// Link the date popup to the date input fields
			input_arrival.datePicker({clickInput:true,createButton:false,displayClose:true,showYearNavigation:false});			
			input_departure.datePicker({clickInput:true,createButton:false,displayClose:true,showYearNavigation:false});			
			
			// Update total pricing on rooms grid
			$('select.no_rooms').bind ( 'change', function ()
				{
					var id = $(this).attr('id');
					id = id.replace('no_rooms_','');
					var selected_room = $(this).val();
					var rate = $( 'input#rate_' + id ).val();
					var price = $( 'input#price_' + id ).val();
					
                    if (selected_room == 0)
                    {
                        var cost = price;
                    }
                    else
                    {
                        var cost = price * selected_room;
                    }
					
                    var base = $('input#currency').val();
					
					$('td#price_td_' + id + ' span').html ( CommaFormatted ( CurrencyFormatted( cost ) ) + ' ' + base );
					update_total_price ();
				});
				
			// Bind functions for all booking button clicks to all booking buttons
			$('input#rooms_book_now').bind ('click', function ()
				{
					//go_back_rooms ( 'book_rooms' );
                    book_rooms();
                    return false;
				});	
				
			// Bind functions onto the boooking links
			$('.booknow input#search').bind ('click', function ()
				{
                    // Set known variables first
                    
                    if (validate_QuickBookerTable() == true)  
                    {
                        var hotel_id = $("input#quick_hotel_id").val();
                        var arrival = $("#QuickBookerTable input#arrival").val();
                        var departure = $("#QuickBookerTable input#departure").val();
                        var url = "?id=" + hotel_id + "&arrival=" + arrival + "&departure=" + departure + '&action=check';
                        window.location.replace ( "/hotels/hotel_detail" + url );	                    
                        return true;
                    }
                    else
                    {
                        return false;
                    }
				});
        });	
