$(document).ready(function()
  {
    
     $('select#mnb').change(function()
	{
	   if(parseInt($(this).val()) > parseInt($('select#mxb').val()))
	      $('select#mxb').val($(this).val());
	});

     $('select#mxb').change(function()
	{
	   if(parseInt($(this).val()) < parseInt($('select#mnb').val()))
	      $('select#mnb').val($(this).val());
	}); 
	
     $('select#mnp').change(function()
	{
	   if(parseInt($(this).val()) > parseInt($('select#mxp').val()))
	      $('select#mxp').val($(this).val());
	});

     $('select#mxp').change(function()
	{
	   if(parseInt($(this).val()) < parseInt($('select#mnp').val()))
	      $('select#mnp').val($(this).val());
	});
     
	$('#sf_submit').hover(
		function() {$(this).attr('src', '/i/button_search_on.png'); },
		function() {$(this).attr('src', '/i/button_search_off.png'); }
	);

	$('#refine_button').hover(
		function() {$(this).attr('src', '/i/button_refine_search_on.png'); },
		function() {$(this).attr('src', '/i/button_refine_search_off.png'); }
	);

	$('#mnb').change(function(){
		if(parseInt($(this).val()) > parseInt($('#mxb').val()))
			$('#mxb').val($(this).val());
	});
	$('#mxb').change(function(){
		if(parseInt($(this).val()) < parseInt($('#mnb').val()))
			$('#mnb').val($(this).val());
	});

	$('#mnp').change(function(){
		if(parseInt($(this).val()) > parseInt($('#mxp').val()))
			$('#mxp').val($(this).val());
	});
	$('#mxp').change(function(){
		if(parseInt($(this).val()) < parseInt($('#mnp').val()))
			$('#mnp').val($(this).val());
	});

	$('#sqmn').change(function(){
		if(parseInt($(this).val()) > parseInt($('#sqmx').val()))
			$('#sqmx').val($(this).val());
	});
	$('#sqmx').change(function(){
		if(parseInt($(this).val()) < parseInt($('#sqmn').val()))
			$('#sqmn').val($(this).val());
	});

	$('#mna').change(function(){
		if(parseInt($(this).val()) > parseInt($('#mxa').val()))
			$('#mxa').val($(this).val());
	});
	$('#mxa').change(function(){
		if(parseInt($(this).val()) < parseInt($('#mna').val()))
			$('#mna').val($(this).val());
	});

	$('#min_size').change(function(){
		if(parseInt($(this).val()) > parseInt($('#max_size').val()))
			$('#max_size').val($(this).val());
	});
	$('#max_size').change(function(){
		if(parseInt($(this).val()) < parseInt($('#min_size').val()))
			$('#min_size').val($(this).val());
	});

	$('#single_beds, #double_beds, #twin_beds').change(function(){
		var sb = parseInt($('#single_beds').val());
		if (isNaN(sb)) sb = 0;
		var db = parseInt($('#double_beds').val());
		if(isNaN(db)) db = 0;
		var tb = parseInt($('#twin_beds').val());
		if (isNaN(tb)) tb = 0;
		var total_ind = sb+db+tb;

		if($('#total_beds'))
		{
			var bedrooms = parseInt($('#total_beds').val());
			if(total_ind>bedrooms)
			{
				if(total_ind>6)
				{
					total_ind = 6;
				}
				$('#total_beds').val(total_ind);
			}
		}

	});
	$('#total_beds').change(function(){
		var sb = parseInt($('#single_beds').val());
		if (isNaN(sb)) sb = 0;
		var db = parseInt($('#double_beds').val());
		if(isNaN(db)) db = 0;
		var tb = parseInt($('#twin_beds').val());
		if (isNaN(tb)) tb = 0;
		var total_ind = sb+db+tb;
		var bedrooms = parseInt($('#total_beds').val());
		if (isNaN(bedrooms)) bedrooms = 0;

		if(total_ind>bedrooms)
		{
			if(total_ind>6)
			{
				total_ind = 6;
			}
			$('#single_beds, #double_beds, #twin_beds').val(0);
		}
	});

	$('#area_type_1').change(function() {
		if ($(this).val().length>0) $('#area_type1').click();
	});
	$('#area_type_2').change(function() {
		if ($(this).val().length>0) $('#area_type2').click();
	});

	$('#pt_id').change(
		function() {
			if ( $(this).attr('value') != '5' && search_type == 'tabs_advanced' )
			{
				$('#advanced_sqm').show();
			}
			else
			{
				$('#advanced_sqm').hide();
			}
			if ( $(this).attr('value') == '1' )
			{
				$('#house_type_div').show();
			}
			else
			{
				$('#house_type_div').hide();
			}
			if ( $(this).attr('value') == '5' )
			{
				$('#advanced_acreage').show();
			}
			else
			{
				$('#advanced_acreage').hide();
			}
		}
	);


	// Only fire this event if the province has a selected value
	if($('select#province').val())
	{
		if($('select#province').val().length > 0)
		{
			$('select#province').change();
		}
	}
  });


function toggleTransport()
{
	if ( $('#s_transport').attr('value') == '1' )
	{
		$('#s_transport').attr('value', '0');
	}
	else
	{
		$('#s_transport').attr('value', '1');
	}
	$('#advanced').attr('value', '1');
	//$('#sf_search').submit();
}

function togglePriceRange()
{
	if ( $('#price_per_room').attr('value') == '1' )
	{
		$('#price_per_room').attr('value', '0');
	}
	else
	{
		$('#price_per_room').attr('value', '1');
	}
	$('#advanced').attr('value', '1');
	//$('#sf_search').submit();
}