
// Update price dropdowns depending on what kind of
// real estate is selected

var rentalPrices = new Array(2);
rentalPrices['from'] = new Array(
	0,
	100,
	200,
	300,
	400,
	500,
	600,
	700,
	800,
	900,
	1000,
	1200,
	1400,
	1600,
	1800,
	2000,
	2200,
	2400,
	2600,
	2800,
	3000,
	3200,
	3400,
	3600,
	3800,
	4000,
	4500,
	5000,
	6000,
	7000,
	8000,
	9000
);
rentalPrices['to'] = new Array(
	0,
	100,
	200,
	300,
	400,
	500,
	600,
	700,
	800,
	900,
	1000,
	1200,
	1400,
	1600,
	1800,
	2000,
	2200,
	2400,
	2600,
	2800,
	3000,
	3200,
	3400,
	3600,
	3800,
	4000,
	4500,
	5000,
	6000,
	7000,
	8000,
	9000
);

var salePrices = new Array(2);
salePrices['from'] = new Array(
	0,
	10000,
	20000,
	30000,
	40000,
	50000,
	60000,
	70000,
	80000,
	90000,
	100000,
	120000,
	140000,
	160000,
	180000,
	200000,
	220000,
	240000,
	260000,
	280000,
	300000,
	320000,
	340000,
	360000,
	380000,
	400000,
	450000,
	500000,
	600000,
	700000,
	800000,
	900000
);
salePrices['to'] = new Array(
	0,
	10000,
	20000,
	30000,
	40000,
	50000,
	60000,
	70000,
	80000,
	90000,
	100000,
	120000,
	140000,
	160000,
	180000,
	200000,
	220000,
	240000,
	260000,
	280000,
	300000,
	320000,
	340000,
	360000,
	380000,
	400000,
	450000,
	500000,
	600000,
	700000,
	800000,
	900000
);

var carPrices = new Array(2);
carPrices['from'] = new Array(
	0,
	500,
	1500,
	2000,
	2500,
	3000,
	3500,
	4000,
	4500,
	5000,
	5500,
	6000,
	7000,
	8000,
	9000,
	10000,
	11000,
	12000,
	13000,
	14000,
	15000,
	16000,
	17000,
	18000,
	19000,
	20000,
	22000,
	24000,
	26000,
	28000,
	30000,
	40000
);
carPrices['to'] = new Array(
	0,
	500,
	1500,
	2000,
	2500,
	3000,
	3500,
	4000,
	4500,
	5000,
	5500,
	6000,
	7000,
	8000,
	9000,
	10000,
	11000,
	12000,
	13000,
	14000,
	15000,
	16000,
	17000,
	18000,
	19000,
	20000,
	22000,
	24000,
	26000,
	28000,
	30000,
	40000
);


function formatNumber(number) {
	numberStr = parseInt(number).toString();
	var len = numberStr.length;
	var result = "";
	if (number >= 1000000000) {
		result = numberStr.substring(0, len - 9)+" "+numberStr.substring(len-9, len-6)+" "+numberStr.substring(len-6, len-3)+" "+numberStr.substring(len-3, len);
	}
	else if (number >= 1000000) {
		result = numberStr.substring(0, len-6)+" "+numberStr.substring(len-6, len-3)+" "+numberStr.substring(len-3, len);
	}
	else if (number >= 1000) {
		result = numberStr.substring(0, len-3)+" "+numberStr.substring(len-3, len);
	}
	else {
		result = numberStr;
	}
	return result;
}

function createOption(text, value, selected) {
	var newOption = document.createElement('option');
	newOption.value = value;
	if(selected) {
		newOption.setAttribute("selected", "selected");
	}
	newOption.appendChild(document.createTextNode(text));
	return newOption;
}

function empty(select) {
	/* Empty the drop down */
	while (select.firstChild) {
		select.removeChild(select.firstChild);
	}
}

function fill_with_rental_prices(select, table, previous_value) {
	/* Fill in with rental prices */
	for ( i = 1; i < rentalPrices[table].length; i++ ) {
		select.appendChild(createOption(formatNumber(rentalPrices[table][i]), rentalPrices[table][i], (rentalPrices[table][i]==previous_value)));
	}
}

function fill_with_cars_prices(select, table, previous_value) {
	/* Fill in with rental prices */
	for ( i = 1; i < carPrices[table].length; i++ ) {
		select.appendChild(createOption(formatNumber(carPrices[table][i]), carPrices[table][i], (carPrices[table][i]==previous_value)));
	}
}

function fill_with_sale_prices(select, table, previous_value) {
	/* Fill up with sale prices */
	for ( i = 1; i < salePrices[table].length; i++ ) {
		select.appendChild(createOption(formatNumber(salePrices[table][i]), salePrices[table][i], (salePrices[table][i]==previous_value)));
	}
}

function show_all_prices(previous_value_l, previous_value_h) {
	/* From price... */
	var pricesFromDropDown = document.getElementById('priceFrom');
	empty(pricesFromDropDown);
	pricesFromDropDown.appendChild(createOption('Precio desde', 'none', ('none'==previous_value_l)));
	pricesFromDropDown.appendChild(createOption('0', '0', (0==previous_value_l)));
	fill_with_rental_prices(pricesFromDropDown, 'from', previous_value_l);
	fill_with_sale_prices(pricesFromDropDown, 'from', previous_value_l);
	/* ...to price */
	var pricesToDropDown = document.getElementById('priceTo');
	empty(pricesToDropDown);
	pricesToDropDown.appendChild(createOption('Precio hasta', 'none', ('none'==previous_value_h)));
	fill_with_rental_prices(pricesToDropDown, 'to', previous_value_h);
	fill_with_sale_prices(pricesToDropDown, 'to', previous_value_h);
	pricesToDropDown.appendChild(createOption('Más de ' + formatNumber(salePrices['to'][salePrices['to'].length-1]), salePrices['to'][salePrices['to'].length-1]+1, (salePrices['to'][salePrices['to'].length-1]+1==previous_value_h)));
}

function show_sale_prices(previous_value_l, previous_value_h) {
	/* From price... */
	var pricesFromDropDown = document.getElementById('priceFrom');
	empty(pricesFromDropDown);
	pricesFromDropDown.appendChild(createOption('Precio desde', 'none', ('none'==previous_value_l)));
	pricesFromDropDown.appendChild(createOption('0', '0', (0==previous_value_l)));
	fill_with_sale_prices(pricesFromDropDown, 'from', previous_value_l);
	/* ...to price */
	var pricesToDropDown = document.getElementById('priceTo');
	empty(pricesToDropDown);
	pricesToDropDown.appendChild(createOption('Precio hasta', 'none', ('none'==previous_value_h)));
	fill_with_sale_prices(pricesToDropDown, 'to', previous_value_h);
	pricesToDropDown.appendChild(createOption('Más de ' + formatNumber(salePrices['to'][salePrices['to'].length-1]), salePrices['to'][salePrices['to'].length-1]+1, (salePrices['to'][salePrices['to'].length-1]+1==previous_value_h)));
}

function show_rentals_prices(previous_value_l, previous_value_h) {
	/* From price... */
	var pricesFromDropDown = document.getElementById('priceFrom');
	empty(pricesFromDropDown);
	pricesFromDropDown.appendChild(createOption('Precio desde', 'none', ('none'==previous_value_l)));
	pricesFromDropDown.appendChild(createOption('0', '0', (0==previous_value_l)));
	fill_with_rental_prices(pricesFromDropDown, 'from', previous_value_l);
	/* ...to price */
	var pricesToDropDown = document.getElementById('priceTo');
	empty(pricesToDropDown);
	pricesToDropDown.appendChild(createOption('Precio hasta', 'none', ('none'==previous_value_h)));
	fill_with_rental_prices(pricesToDropDown, 'to', previous_value_h);
	pricesToDropDown.appendChild(createOption('Más de ' + formatNumber(rentalPrices['to'][rentalPrices['to'].length-1]), rentalPrices['to'][rentalPrices['to'].length-1]+1, (rentalPrices['to'][rentalPrices['to'].length-1]+1 == previous_value_h)));
}

function show_car_prices(previous_value_l, previous_value_h) {
	/* From price... */
	var pricesFromDropDown = document.getElementById('priceFrom');
	empty(pricesFromDropDown);
	pricesFromDropDown.appendChild(createOption('Precio desde', 'none', ('none'==previous_value_l)));
	pricesFromDropDown.appendChild(createOption('0', '0', (0==previous_value_l)));
	fill_with_cars_prices(pricesFromDropDown, 'from', previous_value_l);
	/* ...to price */
	var pricesToDropDown = document.getElementById('priceTo');
	empty(pricesToDropDown);
	pricesToDropDown.appendChild(createOption('Precio hasta', 'none', ('none'==previous_value_h)));
	fill_with_cars_prices(pricesToDropDown, 'to', previous_value_h);
	pricesToDropDown.appendChild(createOption('Más de ' + formatNumber(carPrices['to'][carPrices['to'].length-1]), carPrices['to'][carPrices['to'].length-1]+1, (carPrices['to'][carPrices['to'].length-1]+1 == previous_value_h)));
}

function update_prices(last_clicked, previous_value_l, previous_value_h) {
	var category = document.getElementById('category').options[document.getElementById('category').selectedIndex].value;
	if(IS_HOLIDAYS_RENT(category)) {
		show_rentals_prices(previous_value_l, previous_value_h);
	}
	else if(IS_REAL_ESTATE(category)) {
		if(document.getElementById('subt_all_buys') && document.getElementById('subt_all_buys').checked) {
			show_all_prices(previous_value_l, previous_value_h);
		}
		else if(document.getElementById('subt_buy_buys') && document.getElementById('subt_buy_buys').checked) {
			show_sale_prices(previous_value_l, previous_value_h);
		}
		else if(document.getElementById('subt_rent_buys') && document.getElementById('subt_rent_buys').checked) {
			show_rentals_prices(previous_value_l, previous_value_h);
		}
		else if(document.getElementById('subt_all_sales') && document.getElementById('subt_all_sales').checked) {
			show_all_prices(previous_value_l, previous_value_h);
		}
		else if(document.getElementById('subt_sell_sales') && document.getElementById('subt_sell_sales').checked) {
			show_sale_prices(previous_value_l, previous_value_h);
		}
		else if(document.getElementById('subt_rent_sales') && document.getElementById('subt_rent_sales').checked) {
			show_rentals_prices(previous_value_l, previous_value_h);
		}
	}
	else if(IS_VEHICLES(category)) {
		show_car_prices(previous_value_l, previous_value_h);
	}
}

