var dsMinimumPriceSales = new Array();
var dsMaximumPriceSales = new Array();
var dsMinimumPriceLettings = new Array();
var dsMaximumPriceLettings = new Array();

function PropertySearch_ListingType_OnSelectionChange(o, categoryID, includedGBSIDs, excludedGBSIDs, includedMasterGBSIDs, excludedMasterGBSIDs){
	var oListingType = 5;
	if (typeof o.value == "undefined"){
		oListingType = 5;
		}
	else {		
		oListingType = o.value
		}
	//retrieves the price ranges
	//if we dont have any information for the current listingtype then query
	var invalidInformation = false;
	if ( oListingType.toString() == "5" && ( dsMinimumPriceSales.length == 0 && dsMaximumPriceSales.length == 0 ) ){
		invalidInformation = true;
	}
	if ( oListingType.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 ominprice = document.isc.minprice;
	var omaxprice = document.isc.maxprice;
	if ( ominprice ){ ominprice.options.length = 0; }
	if ( omaxprice ){ omaxprice.options.length = 0; }
	
	switch ( oListingType.toString() ){
		case "5":
			if ( ominprice ){
				for ( var i = 0; i < dsMinimumPriceSales.length; i ++ ){
					var oitem = dsMinimumPriceSales[i];
					ominprice.options[ominprice.options.length] = new Option( ( oitem[4].length > 0 ? String.fromCharCode(parseInt(oitem[2])) : "" ) + oitem[3], oitem[4] );
				}
			}
			if ( omaxprice ){
				for ( var i = 0; i < dsMaximumPriceSales.length; i ++ ){
					var oitem = dsMaximumPriceSales[i];
					omaxprice.options[omaxprice.options.length] = new Option( ( oitem[4].length > 0 ? String.fromCharCode(parseInt(oitem[2])) : "" ) + oitem[3], oitem[4] );
				}
			}
			break;
		case "6":
			if ( ominprice ){
				for ( var i = 0; i < dsMinimumPriceLettings.length; i ++ ){
					var oitem = dsMinimumPriceLettings[i];
					ominprice.options[ominprice.options.length] = new Option( ( oitem[4].length > 0 ? String.fromCharCode(parseInt(oitem[2])) : "" ) + oitem[3], oitem[4] );
				}
			}
			if ( omaxprice ){
				for ( var i = 0; i < dsMaximumPriceLettings.length; i ++ ){
					var oitem = dsMaximumPriceLettings[i];
					omaxprice.options[omaxprice.options.length] = new Option( ( oitem[4].length > 0 ? String.fromCharCode(parseInt(oitem[2])) : "" ) + oitem[3], oitem[4] );
				}
			}
			break;
	}
	PropertySearch_GetAreas( oListingType.toString(), categoryID, includedGBSIDs, excludedGBSIDs, includedMasterGBSIDs, excludedMasterGBSIDs );
	PropertySearch_GetPropertyTypes( oListingType.toString(), categoryID, includedGBSIDs, excludedGBSIDs, includedMasterGBSIDs, excludedMasterGBSIDs );
	PropertySearch_GetOffices( oListingType.toString() );
}

function PropertySearch_GetAreas(listingtype,categoryID, includedGBSIDs, excludedGBSIDs, includedMasterGBSIDs, excludedMasterGBSIDs){
	var oareas = document.isc.area;	
	if ( oareas ){
		var response = Searching.GetAreas( listingtype, categoryID, includedGBSIDs, excludedGBSIDs, includedMasterGBSIDs, excludedMasterGBSIDs );
		if ( response.value != null ){
			//remove options except the first
			oareas.options.length = 1;
			for ( var i = 0; i < response.value.length; i++ ){
				oareas.options[oareas.options.length] = new Option(response.value[i], response.value[i]);
			}
		}
	}
}

function PropertySearch_GetPropertyTypes(listingtype,categoryID, includedGBSIDs, excludedGBSIDs, includedMasterGBSIDs, excludedMasterGBSIDs){
	var opropertytypes = document.isc.pt;
	if ( opropertytypes ){
		var response = Searching.GetPropertyTypes( listingtype, categoryID, includedGBSIDs, excludedGBSIDs, includedMasterGBSIDs, excludedMasterGBSIDs );
		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]);
			}
		}
	}
}


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;
			}
		}
	}
	//Set properties for CIP
	var olistingtype = null;
	var ilistingtype = -1;
	if ( typeof o.listingtype != "undefined" ){
		olistingtype = o.listingtype;
	}else if ( typeof o.ListingType != "undefined" ){
		olistingtype = o.ListingType;
	}
	if ( typeof olistingtype.length == "number" ){
		//radio
		for ( var i = 0; i < olistingtype.length; i ++ ){
			if ( olistingtype[i].checked ){
				ilistingtype = olistingtype[i].value;
				break;
			}
		}
	}else{
		//select
		ilistingtype = olistingtype.value;
	}
	if ( typeof CIPInlineRegistration != "undefined" ){
		with ( CIPInlineRegistration ){
			setListingType( ilistingtype );
			setMinimumPrice( o.minprice.value );
			setMaximumPrice( o.maxprice.value );
			if ( typeof o.bedrooms != "undefined" ){
				setMinimumBedrooms( o.bedrooms.value );
			}
			if ( typeof o.area != "undefined" ){
				setAreaNames( o.area.value );
			}
		}			
	}
	//
	return false;
}
