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)
		id 	(the ID of the slider element- other ids are calculated from this value)
		
*******************************************************************************************/

function buyPriceStepper(stepVal, id) {
	//buyPriceStepper(step, elt.id+'_val', elt.id+'_text',
	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((id=='rs_price_to' || id== 'ns_price_to') && amnt==10000000){
		$(id+'_val').value='';
	}
	else{
		$(id+'_val').value=amnt;
	}
	amnt = currencyFormatted(amnt);
	amnt = commaFormatted(amnt);
	$(id+'_text').setHTML(amnt);
}

/************************************************************************************************************
	@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;
}
function switchSearch(showThis) {
	if(showThis=='refine_search_switcher') {
		$('refine_search_switcher').className='on';
		$('new_search_switcher').className='off';
		$('refine_search_holder').className='';
		$('new_search_holder').className='hidden';
	}
	else{
		$('refine_search_switcher').className='off';
		$('new_search_switcher').className='on';
		$('refine_search_holder').className='hidden';
		$('new_search_holder').className='';
	}
}
// This function changes the advanced search form based on the property_type selected
function changeASForm(selected) {
	//loop through the search form rows and show or hide as neccesary
	$$('#advanced_search_form .row', '#advanced_search_form .row_shadow').each(function(row){
		if(row.hasClass(selected) || row.hasClass('all')){
			row.removeClass('hidden');
			row.addClass('visible');
		}
		else{
			row.addClass('hidden');
			row.removeClass('visible');
		}
	});
	
	//update land area units
	if(selected=='C'){
		$('as_land_to_units').setHTML('m<sup>2</sup>');
	}
	else{
		$('as_land_to_units').setHTML('ha');
	}
	
	//update suburb drop down
	var url='/branch/ajax_get_branch_suburbs.php?'+'prefix=as&'+$('advanced_search_form').toQueryString();
	$('as_suburb_val').options.length=0;
	$('as_suburb_val').options[0]=new Option('Loading...','');
	new Ajax(url, {evalResponse: true}).request();
}
// This function changes the new search panel based on the property_type selected
function changeForm(selected, slide) {

	if (selected == "H") {
		// Disable the buy button
		$('sale_type_S').disabled=true;
	} else {
		// Enable the buy button
		$('sale_type_S').disabled=false;
	}
	//loop through the search form rows and show or hide as neccesary
	$$('#new_search_form .row', '#new_search_form .row_shadow').each(function(row){
		if (row.hasClass(selected) || row.hasClass('all')){
			row.removeClass('hidden');
			row.addClass('visible');
		} else {
			row.addClass('hidden');
			row.removeClass('visible');
		}
	});
	
	//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 text
	$$('#new_search_form .land_area_unit').each(function(elt){
		if(selected == 'C'){
			elt.setHTML('m<sup>2</sup>');
		} else {
			elt.setHTML('ha');
		}
	});
	
	switchPriceRange();
	
	//resize form to fit
	if(slide && $('new_search_panel')){
		new_search_panel.slideIn();
	}
}

function switchPriceRange() {
	// Buy is selected
	if($('sale_type_S').checked==true) {
		// If "Holiday Rental" is in the list, remove it.
		$$('#ns_realist_type_val option').filterByAttribute('value','=','H').each(function(opt){
			opt.remove();
		});
		$('new_search_form_exclude_rented_properties_label').setStyle('display', 'none');
		$('new_search_form_exclude_sold_properties_label').setStyle('display', 'inline');
		$('ns_price_buy').removeClass('hidden');
		$('ns_price_rent').addClass('hidden');
		$('ns_price_from_buy_val').name = 'price_from';
		$('ns_price_to_buy_val').name = 'price_to';
		$('ns_price_from_rent_val').name = 'price_from_rent';
		$('ns_price_to_rent_val').name = 'price_to_rent';
	} else {
		// Rent is selected- Add "Holiday Rental" option.
		if($$('#ns_realist_type_val option').filterByAttribute('value','=','H').length==0){
			$('ns_realist_type_val').options[$('ns_realist_type_val').options.length] = new Option('Holiday Rental','H');
		}
		$('new_search_form_exclude_rented_properties_label').setStyle('display', 'inline');
		$('new_search_form_exclude_sold_properties_label').setStyle('display', 'none');
		$('ns_price_buy').addClass('hidden');
		$('ns_price_rent').removeClass('hidden');
		$('ns_price_from_buy_val').name = 'price_from_buy';
		$('ns_price_to_buy_val').name = 'price_to_buy';
		$('ns_price_from_rent_val').name = 'price_from';
		$('ns_price_to_rent_val').name = 'price_to';
	}
	
	//update suburb drop down
	var url='/branch/ajax_get_branch_suburbs.php?'+'prefix=ns&'+$('new_search_form').toQueryString();
	$('ns_suburb_val').options.length=0;
	$('ns_suburb_val').options[0]=new Option('Loading...','');
	new Ajax(url, {evalResponse: true}).request();
}

function switchSearchFormAction(form_type) {
	// View Results as Map
	if($(form_type+'_search_form_map').checked==true) {
		$(form_type+'_search_form').action="/branch/listing_mapsearch.php";
	} else { 
		// View Results as List
		$(form_type+'_search_form').action="/branch/listing_results.php";
	}
}

window.addEvent('domready', function() {
	
	if($('new_search_form')){
		changeForm('', false);
		//switchSearchFormAction('new');
	}
	if($('advanced_search_form')){
		changeASForm('');
		//switchSearchFormAction('advanced');
	}
});

