var dsMinimumPriceSales = new Array();
var dsMaximumPriceSales = new Array();
var dsMinimumPriceLettings = new Array();
var dsMaximumPriceLettings = new Array();

function PropertySearch_ListingType_OnSelectionChange(o){

	//retrieves the price ranges
	//if we dont have any information for the current listingtype then query
	var invalidInformation = false;
	if ( o.value.toString() == "5" && ( dsMinimumPriceSales.length == 0 && dsMaximumPriceSales.length == 0 ) ){

		invalidInformation = true;
	}
	if ( o.value.toString() == "6" && ( dsMinimumPriceLettings.length == 0 && dsMaximumPriceLettings.length == 0 ) ){
		
		invalidInformation = true;
	}
	//invalid information?
	if ( invalidInformation ){
		var opriceranges = Searching.GetPriceRanges("homepage");
		if ( opriceranges.value != null ){
			for ( var i = 0; i < opriceranges.value.length; i ++ ){
				var oitem = opriceranges.value[i];
				switch ( oitem[0] ){
					case "5":
						//min or max price range?
						switch ( oitem[1] ){
							case "minimum":
								dsMinimumPriceSales[dsMinimumPriceSales.length] = oitem;
								break;
							case "maximum":
								dsMaximumPriceSales[dsMaximumPriceSales.length] = oitem;
								break;
						}
						//
						break;
					case "6":
						//min or max price?
						switch ( oitem[1] ){
							case "minimum":
								dsMinimumPriceLettings[dsMinimumPriceLettings.length] = oitem;
								break;
							case "maximum":
								dsMaximumPriceLettings[dsMaximumPriceLettings.length] = oitem;
								break;
						}
						//
						break;
				}
			}
		}
	}

	//start binding data
	
	var aminprice = [];
	var amaxprice = [];
	
	aminprice.push( new Array("No Minimum", "") );
	amaxprice.push( new Array("No Maximum", "") );
			
	switch ( o.value.toString() ){
		case "5":
				//HttpManager.Document.GetObject("LocationSalesHome").style.height=144;
				for ( var i = 0; i < dsMinimumPriceSales.length; i ++ ){
					var oitem = dsMinimumPriceSales[i];
					aminprice.push(new Array( ( oitem[4].length > 0 ? String.fromCharCode(parseInt(oitem[2])) : "" ) + oitem[3], oitem[4] ) );
				}
				for ( var i = 0; i < dsMaximumPriceSales.length; i ++ ){
					var oitem = dsMaximumPriceSales[i];
					amaxprice.push( new Array( ( oitem[4].length > 0 ? String.fromCharCode(parseInt(oitem[2])) : "" ) + oitem[3], oitem[4] ) );
				}
			break;
		case "6":
				//HttpManager.Document.GetObject("LocationSalesHome").style.height=96;
				for ( var i = 0; i < dsMinimumPriceLettings.length; i ++ ){
					var oitem = dsMinimumPriceLettings[i];
					aminprice.push(new Array( ( oitem[4].length > 0 ? String.fromCharCode(parseInt(oitem[2])) : "" ) + oitem[3], oitem[4] ) );
				}
				for ( var i = 0; i < dsMaximumPriceLettings.length; i ++ ){
					var oitem = dsMaximumPriceLettings[i];
					amaxprice.push( new Array( ( oitem[4].length > 0 ? String.fromCharCode(parseInt(oitem[2])) : "" ) + oitem[3], oitem[4] ) );
				}
			break;
	}
	ominimumprice.DataSource = aminprice;
	ominimumprice.DataBind();
	__EstateWeb_Objects_DropDownListManager_SelectItem_ByValue( ominimumprice, aminprice[0][1] );
	omaximumprice.DataSource = amaxprice;
	omaximumprice.DataBind();
	__EstateWeb_Objects_DropDownListManager_SelectItem_ByValue( omaximumprice, amaxprice[0][1] );
	//PropertySearch_GetAreas( o.value.toString() );
	//PropertySearch_GetPropertyTypes( o.value.toString() );
	PropertySearch_GetOffices( o.value.toString() );
}

function PropertySearch_GetAreas(listingtype){
	//var oareas = document.isc.area;	
	if ( typeof nDropdownManager != "undefined" ){
		var response = Searching.GetAreas( listingtype );
		if ( response.value != null ){
			var aResults = [];
			aResults.push( new Array("Every Branch", "") );
			for ( var i = 0; i < response.value.length; i ++ ){
				aResults.push( new Array(response.value[i], response.value[i]) );
			}
			nDropdownManager.DataSource = aResults;
			nDropdownManager.DataBind();
		}
	}
}

function PropertySearch_GetPropertyTypes(listingtype){
	var opropertytypes = document.isc.pt;
	if ( opropertytypes ){
		var response = Searching.GetPropertyTypes( listingtype );
		if ( response.value != null ){
			opropertytypes.options.length = 1;
			for ( var i = 0; i < response.value.length; i ++ ){
				opropertytypes.options[opropertytypes.options.length] = new Option(response.value[i][1], response.value[i][0]);
			}
		}
	}
}

function PropertySearch_GetOffices(listingtype,offices){
	//if (!offices) { offices = "" }
	//var ooffices = document.isc.location;	
	//if ( ooffices ){
	//	var response = Searching.GetOffices( listingtype, offices );
	//	if ( response.value != null ){
	//		//remove options except the first
	//		ooffices.options.length = 1;
	//		for ( var i = 0; i < response.value.length; i++ ){
	//			var aitem = response.value[i];
	//			ooffices.options[ooffices.options.length] = new Option(aitem[1], aitem[0]);
	//		}
	//	}
	//}
	if ( typeof nDropdownManager != "undefined" ){
		var response = Searching.GetOffices( listingtype, offices );
		if ( response.value != null ){
			var aResults = [];
			aResults.push( new Array("Every Branch", "") );
			for ( var i = 0; i < response.value.length; i ++ ){
				var aitem = response.value[i];
				aResults.push( new Array(aitem[1], aitem[0]) );
			}
			nDropdownManager.OptionsContainerHeight = ( listingtype == "5" ? 144 : 96 );
			nDropdownManager.DataSource = aResults;
			nDropdownManager.DataBind();
		}
	}
}


function PropertySearch_OnSubmit(o){
	if (o.minprice && o.maxprice){
		if (!isNaN(o.minprice.value) && !isNaN(o.maxprice.value)){
			if (parseInt(o.minprice.value) >= parseInt(o.maxprice.value)){
				alert('Please select a maximum price higher than the minimum price selected');
				return false;
			}
		}
	}
	return true;
}