function switchSearchFormAction(id_of_form) {
	
	// IF on REDEFINE SEARCH page
	if(id_of_form == "refine_search_form") {
		
		// View Results as Map
		if($('rs_new_search_form_map').checked==true) {
			$('refine_search_form').action="/listing_mapsearch.php";
		} else { 
			// View Results as List
			$('refine_search_form').action="/listing_results.php";
		}
		
	// IF on NEW SEARCH page
	} else {
		
		// View Results as Map
		if($('new_search_form_map').checked==true) {
			$('rural_property_search_form').action="/listing_mapsearch.php";
		} else { 
			// View Results as List
			$('rural_property_search_form').action="/listing_results.php";
		}
		
	}
}

// This function changes the class and LOOK of a checked option on the New Search Form
function changeOptionToSelectedClass(id_to_change) {
	if($(id_to_change)) {
		// Checked
		if($(id_to_change).checked==true) {
			$(id_to_change).getParent().className="include_option_selected";	
		} else {
			$(id_to_change).getParent().className="include_option";	

		}
	}
}

// This function changes the new search panel based on the property_type selected
function changeForm(selected) {

	if(selected == "H") {
		// Disable the buy button
		$('sale_type_S').disabled=true;		
	} else {
		// Enable the buy button
		$('sale_type_S').disabled=false;	
	}
	switch(selected) {
		
		// Rural Property
		case "RP":
		$('bedrooms_row').className="hidden";
		$('bathrooms_row').className="hidden";
		$('enterprise_types_rural').className="";
		$('enterprise_types_commercial').className="hidden";
		$('land_area').className="";
		$('property_station_name').className="";
		$('parking').className="hidden";
		$('business_types').className="hidden";
		$('building_types').className="hidden";
		$('building_area').className="hidden";
		// hide, then show fixes an issue where IE7 was not moving the label when property_type was changed.
		$('include_unset_land_area').className="hidden"; $('include_unset_land_area').className="";
		$('include_unset_building_area').className=""; $('include_unset_building_area').className="hidden";
		$('rural_property_search_form').surrounding_radius.options[2].selected=true;
		break;
		
		// Residential
		case "R":
		$('bedrooms_row').className="";
		$('bathrooms_row').className="";
		$('enterprise_types_rural').className="hidden";
		$('enterprise_types_commercial').className="hidden";
		$('land_area').className="hidden";
		$('property_station_name').className="hidden";
		$('parking').className="";
		$('business_types').className="hidden";
		$('building_types').className="";
		$('building_area').className="hidden";
		$('include_unset_land_area').className=""; $('include_unset_land_area').className="hidden";
		$('include_unset_building_area').className=""; $('include_unset_building_area').className="hidden";
		$('rural_property_search_form').surrounding_radius.options[0].selected=true;
		break;
		
		// Vacant Land
		case "V":
		$('bedrooms_row').className="hidden";
		$('bathrooms_row').className="hidden";
		$('parking').className="hidden";
		$('land_area').className="";
		$('property_station_name').className="hidden";
		$('enterprise_types_rural').className="hidden";
		$('enterprise_types_commercial').className="hidden";
		$('business_types').className="hidden";
		$('building_types').className="hidden";
		$('building_area').className="hidden";
		$('include_unset_land_area').className="hidden"; $('include_unset_land_area').className="";
		$('include_unset_building_area').className=""; $('include_unset_building_area').className="hidden";
		$('rural_property_search_form').surrounding_radius.options[0].selected=true;
		break;
		
		// Lifestyle
		case "L":
		$('bedrooms_row').className="hidden";
		$('bathrooms_row').className="hidden";
		$('parking').className="hidden";
		$('land_area').className="";
		$('property_station_name').className="hidden";
		$('enterprise_types_rural').className="";
		$('enterprise_types_commercial').className="hidden";
		$('business_types').className="hidden";
		$('building_types').className="hidden";
		$('building_area').className="hidden";
		$('include_unset_land_area').className="hidden"; $('include_unset_land_area').className="";
		$('include_unset_building_area').className=""; $('include_unset_building_area').className="hidden";
		$('rural_property_search_form').surrounding_radius.options[1].selected=true;
		break;
		
		// Commercial
		case "C":
		$('business_types').className="";
		$('bedrooms_row').className="hidden";
		$('bathrooms_row').className="hidden";
		$('parking').className="hidden";
		$('land_area').className="";
		$('property_station_name').className="hidden";
		$('enterprise_types_rural').className="hidden";
		$('enterprise_types_commercial').className="";
		$('building_types').className="hidden";
		$('building_area').className="";
		$('include_unset_land_area').className="hidden"; $('include_unset_land_area').className="";
		$('include_unset_building_area').className="hidden"; $('include_unset_building_area').className="";
		if(window.ie) $('search_button_holder').setStyle('padding-bottom', '5px');
		$('rural_property_search_form').surrounding_radius.options[1].selected=true;
		break;
		
		// For any OTHER property types - Needs to be exactly the same as Rural as Rural is the default
		default:
		$('bedrooms_row').className="hidden";
		$('bathrooms_row').className="hidden";
		$('enterprise_types_rural').className="";
		$('enterprise_types_commercial').className="hidden";
		$('land_area').className="";
		$('property_station_name').className="";
		$('parking').className="hidden";
		$('business_types').className="hidden";
		$('building_types').className="hidden";
		$('building_area').className="hidden";
		$('include_unset_land_area').className="hidden"; $('include_unset_land_area').className="";
		$('include_unset_building_area').className=""; $('include_unset_building_area').className="hidden";
		$('rural_property_search_form').surrounding_radius.options[2].selected=true;
		break;
		
	}
	//set rent text to lease for commercial
	if($('label_sale_type_R')){
		if(selected == "C") {
			$('label_sale_type_R').setHTML('Lease');
		}
		else{
			$('label_sale_type_R').setHTML('Rent');
		}
	}// End if rent label exists
	
	//update land area ranges and units
	if(new_search_land_from_slider){
		new_search_land_from_slider.updateContent();
	}
	if(new_search_land_to_slider){
		new_search_land_to_slider.updateContent();
	}
	
	//switchSearchFormAction();
	switchPriceRange();
}

function switchPriceRange() {
	
	// Get the value of realist_type
	var selected_type = document.forms['rural_property_search_form'].realist_type.options[document.forms['rural_property_search_form'].realist_type.selectedIndex].value;
	
	// If Residential, Lifestyle, Vacant or Commercial Property is selected
	if(selected_type == "R" || selected_type == "L" || selected_type == "V" || selected_type == "C") {
	
		// Buy is selected
		if($('sale_type_S').checked==true) {
				
			$('rent_row').className="hidden";
			$('buy_row').className="table_row";
			// If buy is selected and "Holiday Rental" is in the list, remove it.
			if(document.forms['rural_property_search_form'].realist_type.options[5] && document.forms['rural_property_search_form'].realist_type.options[5].value == "H") {
				document.forms['rural_property_search_form'].realist_type.options[5] = null;
			}
			$('price_to_new_search_id').value="10000000";
				
		} else {
		// Rent is selected
			$('rent_row').className="table_row";
			$('buy_row').className="hidden";
			// Add "Holiday Rental" option.
			document.forms['rural_property_search_form'].realist_type.options[5] = new Option('Holiday Rental','H');
			//rent for the year 52*1000
			$('price_to_new_search_id').value="52000";
		}
		// If Rural Property and Rural Property TO is set to MAX then set the value to be ""
		if ($('land_to_new_search_id') && $('land_to_new_search_id').value == "50") { $('land_to_new_search_id').value=""; }
	
	} // End IF Residential is selected
	
	// If Rural Property is selected
	if(selected_type == "RP") {
			
		// Buy is selected
		if($('sale_type_S').checked==true) {
			$('rent_row').className="hidden";
			$('buy_row').className="table_row";
			// If buy is selected and "Holiday Rental" is in the list, remove it.
			if(document.forms['rural_property_search_form'].realist_type.options[5] && document.forms['rural_property_search_form'].realist_type.options[5].value == "H") {
				document.forms['rural_property_search_form'].realist_type.options[5] = null;
			}
			$('price_to_new_search_id').value="10000000";
		} else { 
		// Rent is selected
			$('rent_row').className="table_row";
			$('buy_row').className="hidden";	
			// If "Holiday Rental" option does not already exist, add it.
			if(!document.forms['rural_property_search_form'].realist_type.options[5]) {
				document.forms['rural_property_search_form'].realist_type.options[5] = new Option('Holiday Rental','H');
			}
			//rent for the year 52*1000
			$('price_to_new_search_id').value="52000";
		}	
		// If Rural Property and Rural Property TO is set to MAX then set the value to be ""
		if ($('land_to_new_search_id') && $('land_to_new_search_id').value == "50") { $('land_to_new_search_id').value=""; }
		
	} // End IF Rural Property is selected
	
}

function currencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0; }
	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 CurrencyFormatted()

function commaFormatted(amount)
{
	var delimiter = ",";
	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;
	var amount_length = amount.length;
	amount_length = amount_length - 3;
	var formatted_amount = amount.substr(0,amount_length);
	return formatted_amount;
} // function CommaFormatted()


/*******************************************************************************************
	@function: buy_price_stepper
	
	@description: Performs the actions of changing the slider based on what step you are at
	@params: 
		stepVal 		(the value of the current step)
		hiddenValId 	(the ID of the hidden input that passes through the designated buy price)
		priceDisplayId  (the ID of the span which holds the visible selected $ amount)
		priceFromOrTo	(the "name" of the price that needs to be passed through in the search)
*******************************************************************************************/

function buyPriceStepper(stepVal,hiddenValId,priceDisplayId,priceFromOrTo) {
	
	var amnt = new String();
			
	if (stepVal <= 40) {
		amnt = (stepVal*20000);
	} 
	else if (stepVal <= 70) {
		amnt = (40*20000)+((stepVal-40)*50000);
	}
	else if (stepVal <= 99) {
		amnt = (40*20000)+(30*50000)+((stepVal-70)*250000);
	} else {
		amnt = 10000000;
	}
	//if the value is at the slider max, remove it
	if(priceFromOrTo=='price_to' && amnt==10000000){
		$(hiddenValId).value='';
	}
	else{
		$(hiddenValId).value=amnt;
	}
	amnt = currencyFormatted(amnt);
	amnt = commaFormatted(amnt);
	$(priceDisplayId).setHTML(amnt);
	$(hiddenValId).name=priceFromOrTo;
}

/************************************************************************************************************
	@function: getStep_FromBuyAmount
	
	@description: Returns the current "step" based on the amount passed to it in the parameter
	@params: 
		amount 	(the $_GET (Buy: Price From/Price To) amount passed to the query string by the search form)
************************************************************************************************************/

function getStep_FromBuyAmount(amount) {
	
	var step_value = ""
	
	if ( amount <= 800000) {
		step_value = (amount/20000);
		
	} else if (amount <= 2800000) {
		step_value = ((amount - 800000) / 50000) + 40;
	} 
	else if (amount <= 9550000) {
		step_value = ((amount - 2300000) / 250000) + 70;
	} 
	else {
		step_value = 100;
	}
	
	return step_value;
}

//used by commercial and rural
function getAreaValueForStep(step) {
	if (step <= 10) {
		var amnt = (step);
	}
	else if (step <= 20) {
		var amnt = (10)+((step-10)*2);
	}
	else if (step <= 30) {
		var amnt = (10)+(10*2)+((step-20)*5);
	}
	else if (step <= 40) {
		var amnt = (10)+(10*2)+(10*5)+((step-30)*10);
	}
	else if (step <= 50) {
		var amnt = (10)+(10*2)+(10*5)+(10*10)+((step-40)*20);
	}
	else if (step <= 60) {
		var amnt = (10)+(10*2)+(10*5)+(10*10)+(10*20)+((step-50)*50);
	}
	else if (step <= 99) {
		var amnt = (10)+(10*2)+(10*5)+(10*10)+(10*20)+(10*50)+((step-60)*100);
	}
	else {
		var amnt = 5000;
	}
	return amnt;
}

function getStepForAreaValue(value) {
	if (value <= 10) {
		var step = value;
	} 
	else if (value <= 30) {
		var step = 10+((value-10)/2);
	}
	else if (value <= 80) {
		var step = 20+((value-30)/5);
	}
	else if (value <= 180) {
		var step = 30+((value-80)/10);
	}
	else if (value <= 380) {
		var step = 40+((value-180)/20);
	}
	else if (value <= 880) {
		var step = 50+((value-380)/50);
	}
	else if (value <= 4780) {
		var step = 60+((value-880)/100);
	}
	else {
		var step = 100;
	}
	return step;
}
//var to hold tooltips- initialise as global then overwrite when domready
var tips=false;
var wide_tips=false;
var icon_tips=false;
var icon_tips_clickable=false;

//var to hold land from and to sliders- initialise as global then overwrite in search panel scripts
var new_search_land_from_slider=false;
var new_search_land_to_slider=false;

window.addEvent('domready', function() {
	//create an iframe shim for select boxes in ie6 and firefox on mac
	if(window.ie6 || (window.gecko && navigator.userAgent.test('mac', 'i'))){
		if(!$('DivShim')){
			var divShimProperties = {
				'id': "DivShim", 
				'src': "javascript:void(0);",
				'scrolling': "no",
				'frameborder': 0
			}; 
			var divShimStyles = {
				'position': "absolute",
				'display': "none",
				'padding': "0px",
				'border':'none',
				'filter': 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)'
			}; 
			var divShim = new Element('iframe').setProperties(divShimProperties).setStyles(divShimStyles);
			divShim.injectInside(document.body);
		}
	}
			
	//set up tooltips
	tips = new Tips($$('.tip'), {
		initialize:function(){
			this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 200, wait: false}).set(0);
		},
		onShow: function(toolTip) {
			this.fx.start(0.9);
		},
		onHide: function(toolTip) {
			this.fx.start(0);
		},
		offsets:{ x:20, y:10 }
	});
	// Normal un-clickable property features icons
	icon_tips = new Tips($$('.icon-tip'), {
		className: 'icon-tool',
		initialize:function(){
			this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 200, wait: false}).set(0);
		},
		onShow: function(toolTip) {
			if($('DivShim')){
				if(this.toolTip.getStyle('z-index').toInt()<1 || isNaN(this.toolTip.getStyle('z-index').toInt())){
					this.toolTip.setStyle('z-index',301);
				}
				$('DivShim').setStyle('z-index', this.toolTip.getStyle('z-index')-1);
				$('DivShim').setStyle('display', 'block');
				$('DivShim').setStyle('width', this.toolTip.getStyle('width').toInt()+this.toolTip.getStyle('padding-right').toInt()+this.toolTip.getStyle('padding-left').toInt()+'px');
				$('DivShim').setStyle('height', this.toolTip.getStyle('height').toInt()+this.toolTip.getStyle('padding-top').toInt()+this.toolTip.getStyle('padding-bottom').toInt()+'px');
			}
			this.fx.start(0.9);
		},
		onHide: function(toolTip) {
			this.fx.start(0);
			if($('DivShim')){
				$('DivShim').setStyle('display', 'none');
			}
		},
		offsets:{ x:0, y:12 }
	});
	// Clickable property features icons
	icon_tips_clickable = new Tips($$('.icon-tip-clickable'), {
		className: 'icon-tool-clickable',
		initialize:function(){
			this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 150, wait: false}).set(0);
		},
		onShow: function(toolTip) {
			if($('DivShim')){
				if(this.toolTip.getStyle('z-index').toInt()<1 || isNaN(this.toolTip.getStyle('z-index').toInt())){
					this.toolTip.setStyle('z-index',302);
				}
				$('DivShim').setStyle('z-index', this.toolTip.getStyle('z-index')-1);
				$('DivShim').setStyle('display', 'block');
				$('DivShim').setStyle('width', this.toolTip.getStyle('width').toInt()+this.toolTip.getStyle('padding-right').toInt()+this.toolTip.getStyle('padding-left').toInt()+'px');
				$('DivShim').setStyle('height', this.toolTip.getStyle('height').toInt()+this.toolTip.getStyle('padding-top').toInt()+this.toolTip.getStyle('padding-bottom').toInt()+'px');
			}
			this.fx.start(0.9);
		},
		onHide: function(toolTip) {
			this.fx.start(0);
			
		},
		offsets:{ x:0, y:12 }
	});
	wide_tips = new Tips($$('.tip-wide'), {
		className: 'wide-tool',
		initialize:function(){
			this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 200, wait: false}).set(0);
		},
		onShow: function(toolTip) {
			if($('DivShim')){
				if(this.toolTip.getStyle('z-Index').toInt()<1 || isNaN(this.toolTip.getStyle('z-Index').toInt())){
					this.toolTip.setStyle('z-Index',5);
				}
				$('DivShim').setStyle('z-Index', this.toolTip.getStyle('z-Index')-1);
				$('DivShim').setStyle('display', 'block');
				$('DivShim').setStyle('width', this.toolTip.getStyle('width').toInt()+this.toolTip.getStyle('padding-right').toInt()+this.toolTip.getStyle('padding-left').toInt()+'px');
				$('DivShim').setStyle('height', this.toolTip.getStyle('height').toInt()+this.toolTip.getStyle('padding-top').toInt()+this.toolTip.getStyle('padding-bottom').toInt()+'px');
			}
			this.fx.start(0.9);
		},
		onHide: function(toolTip) {
			this.fx.start(0);
			if($('DivShim')){
				$('DivShim').setStyle('display', 'none');
			}
		},
		offsets:{ x:20, y:10 }
	});
	Tips.implement({
		//function to update the tooltip text. Will remove the title if set
		updateContent: function(el, text){
			el.$tmp.myText=text;
			this.wrapper.empty();
			this.text = new Element('span').inject(
					new Element('div', {'class': this.options.className + '-text'}
			).inject(this.wrapper)).setHTML(text);
		},
		//function to move the iframe with the tip
		moveShim: function(el){
			if($('DivShim')){
				$('DivShim').setStyle('left', this.toolTip.getStyle('left'));
				$('DivShim').setStyle('top', this.toolTip.getStyle('top'));
			}
		}
	});
	//make the shim move with the tip
	if($('DivShim')){
		$$('.tip-wide').each(function(el){
			el.addEvent('mousemove', wide_tips.moveShim.bindWithEvent(wide_tips));
		});
		$$('.tip').each(function(el){
			el.addEvent('mousemove', tips.moveShim.bindWithEvent(tips));
		});
		$$('.icon-tip').each(function(el){
			el.addEvent('mousemove', icon_tips.moveShim.bindWithEvent(icon_tips));
		});
		$$('.icon-tip-clickable').each(function(el){
			el.addEvent('mousemove', icon_tips_clickable.moveShim.bindWithEvent(icon_tips_clickable));
		});
	}
	//add a function to the slider to let us refresh the value- used by land area when changing types
	Slider.implement({
		updateContent: function(){
			this.fireEvent('onChange', this.step);
		}
	});

});
