//
// Routine to swap max and min priices for sales and lettings searches
// $Id: swap_menu.js,v 1.9 2008/02/19 15:16:01 norman Exp $
//

// Set serach type

var search_type;

// set select names

var select_names = new Array(
		'minprice'
	,	'maxprice'
);

// Set arrays of sales and lettings prices

var buy_prices = new Array(
		'0'
	,	'50000'
	,	'75000'
	,	'100000'
	,	'150000'
	,	'250000'
	,	'350000'
	,	'450000'
	,	'600000'
	,	'850000'
	,	'1000000'
	,	'10000000'
);

var rent_prices = new Array(
		'0'
	,	'300'
	,	'400'
	,	'500'
	,	'600'
	,	'800'
	,	'1000'
	,	'1500'
	,	'2000'
	,	'10000000'
);

//
// Swap select menu values and options if required
//

function swap_menu( onchange_event, search_form_name ) {

// set default search type and search type names

	var advanced, type_names;

	if ( search_form_name == 'dezrez_advanced_search' ) {
		advanced = true;
		search_type == 'buy';
		type_names = new Array ( 'buy', 'rent' ); 
	} else {
		advanced = false;
		search_type = 'none';
		type_names = new Array ( 'buy', 'buy', 'rent' ); 
	}

// set variables

	search_form  = document.getElementById(search_form_name);
	search_index  = search_form.search_type.options.selectedIndex;
	selected_type = type_names[search_index];

// check whether we actually need to change anything

	if ( selected_type == search_type ) {
		return;

// otherwise store new selection

	} else {
		search_type = selected_type;
		
	}

// select correct price array

	new_prices = new Array ();
	if (selected_type == "buy") {
		new_prices = buy_prices;
	} else {
		new_prices = rent_prices;
	}
	
// loop over select arrays

	for (n = 0; n < select_names.length; n++ ) {

// get array name and length

		select_name = select_names[n];
		select_length = search_form[select_name].options.length;

// clear all array elements
		
		search_form[select_name].options.length = 0;

// get new array length

		select_length = new_prices.length;
		
// set special case variables 

		first_element = 1;
		last_element = select_length - 1; 

// create new array elements

		for (m = 0; m < select_length; m++) {
			
			option_value = '';
			option_string = '';

// make instruction option for first element of max price array

			if ((select_name == 'maxprice') && first_element) {
				if (! advanced) {
					option_string = 'maximum price';
					option_value = new_prices[last_element];
					new_option = new Option(option_string,option_value);
					search_form[select_name].options[0] = new_option;
				}
// reset flag
				first_element--;
				continue;
				
// make instruction option for first element of min price array

			} else if ((select_name == 'minprice') && first_element) {
				if (! advanced) {
					option_string = 'minimum price';
					option_value = new_prices[0];
					new_option = new Option(option_string,option_value);
					search_form[select_name].options[0] = new_option;
				}
// reset flag
				first_element--;

// decrement loop counter so that we still make real first element

				m--;
				continue;

// skip last element if we are doing min price array

			} else if ((select_name == 'minprice') && ( m == last_element)) {
				continue;

// otherwise build option value

			} else {
				option_value = new_prices[m];
				if (selected_type == 'buy') {
					if (m == last_element) {
						option_string = '1m plus';
					} else if (m == (last_element - 1)) {
						option_string = '1m';
					} else {
						opt_len = option_value.length;
						if (opt_len > 3) {
							first_bit = option_value.substring(0, opt_len - 3);
							last_bit = option_value.substring(opt_len - 3, opt_len);
							option_value = first_bit + ',' + last_bit;
						}
						option_string = '' + option_value;;
					}
				} else {
					if (m == last_element) {
						option_string = '2,000 plus pcm';
					} else {
						opt_len = option_value.length;
						if (opt_len > 3) {
							first_bit = option_value.substring(0, opt_len - 3);
							last_bit = option_value.substring(opt_len - 3, opt_len);
							option_value = first_bit + ',' + last_bit;
						}
						option_string = option_value  + ' pcm';
					}
				}

// create new option
				option_string = '\u00a3' + option_string;
				new_option = new Option(option_string,option_value);

// add new option to select menu
				
				if (select_name == 'maxprice') {
					if (advanced) {
						search_form[select_name].options[m-1] = new_option;
					} else {
						search_form[select_name].options[m] = new_option;
					}
				} else {
					if (advanced) {
						search_form[select_name].options[m] = new_option;
					} else {
						search_form[select_name].options[m+1] = new_option;
					}
				}
			}
		}

// set default menu selection
			
		if ( onchange_event ) {
			if (advanced && (select_name == 'maxprice')) {
				search_form[select_name].options.selectedIndex = last_element - 1;
			} else {
				search_form[select_name].options.selectedIndex = 0;
			}
		}
	}

// set menu defaults for an onchange event 

	if ( onchange_event ) {

// reset bedrooms menu to keep Debbie happy

		search_form['bedrooms'].options.selectedIndex = 0;

// reset hidden rental period field
	
		if (selected_type == "buy") {
			search_form.rentalperiod.value = 0;
		} else {
			search_form.rentalperiod.value = 4;
		}
	}
}

// Set town code groupings

var click_codes = new Array ( 'EB', 'SF', 'NH', 'PH', 'SD', 'BAH', 'UK', 'HF', 'CR' ); 
var group_codes = new Array (
			[ 'SOV', 'LHP', 'RDM', 'POL', 'SC', 'PW', 'ED' ]
		,	[ 'BS', 'ALF' ]
		,	[ 'DSH', 'PID', 'ROD' ]
		,	[ 'TC', 'TV' ]
		,	[ 'WD', 'OD', 'RD' ]
		,	[ 'KT', 'BM', 'ROE', 'HV' ]
		,	[ 'MF', 'BX', 'HD', 'BB', 'FF', 'HLD', 'EH', 'PC', 'ISF', 'FCH', 'NW', 'FW', 'NT', 'HH', 'LD' ]
		,	[ 'CIH', 'WAL', 'HOR', 'BOA', 'BUR', 'PTN', 'FAS', 'MAY', 'RGN', 'DAL' , 'COW', 'HEL', 'HAI' ]
		,	[ 'TW', 'GR', 'RB', 'WC', 'FR', 'MC', 'HAR', 'ERI' ] );

function group_click(selected_code) {

// set click code value

	selected_value = '(' + selected_code + ')';

// Ger codes for this town group

	town_group_codes = new Array;
	for (n = 0; n < click_codes.length; n++ ) {
		if (click_codes[n] == selected_code) {
			town_group_codes = group_codes[n];
			break;
		}
	}

// get array of town checkboxes elements

	towns  = document.dezrez_advanced_search.town;

// set flags for selected town checkbox and town group indexes

	selected_town_ticked = 0;
	selected_town_index = null;
	town_group_indexes = new Array;
	
// loop over all checkbox objects

	for (n = 0; n < towns.length; n++ ) {
		town = towns[n];
		if ( town.value == selected_value) {
			selected_town_index = n;
			if (town.checked) {
				selected_town_ticked++;
			}
		} else {
			for (m = 0; m < town_group_codes.length; m++ ) {
				town_group_value = '(' + town_group_codes[m] + ')';
				if ( town.value == town_group_value ) {
					town_group_indexes.push(n);
					break;
				}
			}
		}
	}
	
// uncheck selected town if already checked

	if (selected_town_ticked) {
		towns[selected_town_index].checked = true;
		for (n=0; n < town_group_indexes.length; n++) {
			towns[town_group_indexes[n]].checked = true;
		}
	
// otherwise check selected town and other group members

	} else {
		towns[selected_town_index].checked = false;
	}
}
