/* SCRIPT TO SWITCH BETWEEN BUYING AND RENTING PRICE OPTIONS
 * USAGE: <body onLoad="init()"> this initialises with buying prices loaded.
 * SELECT must have name of 'searchForm': <SELECT NAME="searchForm" ....>
 * CALL populate(buy) function with onClick/onChange with <radio> or <select> 
 * Use populate(true) on buy option and populate(false) on renting option. 
 * For this script to work correctly, add an extra blank option to end of each SELECT or
 * the last value will be deleted by the browser compatibility check.
 * If the check fails or javascript is not enabled, all values will show.
 */

/* init() checks browser compatibility
 * Stores form values and text in two arrays
 * initialises SELECTs with buying options
 */
 
function initPrices(){
	optionTest = true; //check whether browser can handle adding/removing options 
	if(document.forms['searchForm']){ //only initiate if the searchForm exists
		lgth = document.forms['searchForm'].min.options.length - 1;
	document.forms['searchForm'].min.options[lgth] = null;
	if (document.forms['searchForm'].min.options[lgth]) optionTest = false;
	if (!optionTest) return; 

	minops = document.forms['searchForm'].min;
	priceValues=new Array();
	priceText=new Array();
	
	for(var count=0; count<minops.length; count++){
		priceValues[count] = minops[count].value;
		priceText[count] = minops[count].text;
	}
	
	maxops = document.forms['searchForm'].max;
	populate(true); //initialise with buying options as this is the default checked on radio button
	}
}

function clearForm (){ //clears the select box contents
 minops.options.length=0;
 maxops.options.length=0;
}

function populate(buy) { //fills select with buying prices
	var count = 0;
	var opsCount = 0;
	clearForm();
	
	if(buy==true){
		var minThresh=20000;
		var maxThresh=10000000;
		minops.options[opsCount] = new Option(priceText[count],priceValues[count]); //add --any--
		maxops.options[opsCount] = new Option(priceText[count],priceValues[count]); //add --any--
		opsCount++;
	}

	else{
		var minThresh=0;
		var maxThresh=10000;
	}

	while(count<priceValues.length+1)  {
		currentVal = priceValues[count];
		currentText = priceText[count];
		if(currentVal >= minThresh && currentVal <= maxThresh){
  			minops.options[opsCount] = new Option(priceText[count],priceValues[count]);
			maxops.options[opsCount] = new Option(priceText[count],priceValues[count]);
			opsCount++;
		}
			count++;
 	}
}

