var dsMinimumPriceSales = new Array();
var dsMaximumPriceSales = new Array();
var dsMinimumPriceLettings = new Array();
var dsMaximumPriceLettings = new Array();

function PropertySearch_Currency_OnSelectionChange(){
		//var listingType = document.getElementsByName("listingtype");
		var listingType = HttpManager.Document.GetObject("ListingType");
		if(listingType){		
			//for(var i = 0; i < listingType.length; i++) {				
			//		if(listingType[i].selected.length > 0){
			//		alert('got here');
							PropertySearch_ListingType_OnSelectionChange(listingType)
					
			//		}
			//}
		}		
}


function PropertySearch_ListingType_OnSelectionChange(o, categoryID, includedGBSIDs, excludedGBSIDs, includedMasterGBSIDs, excludedMasterGBSIDs){
	
	//alert(categoryID);
	//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 ominprice = document.isc.minprice;
	var omaxprice = document.isc.maxprice;
	if ( ominprice ){ ominprice.options.length = 0; }
	if ( omaxprice ){ omaxprice.options.length = 0; }
	
	
	var currencySymbol = null;
	
	switch(document.isc.currency.value ){
	case "GBP" :
		currencySymbol = String.fromCharCode(parseInt(163))
		break;
	case "USD" :
		currencySymbol = "$"
		break;
	case "EUR" :
		currencySymbol = String.fromCharCode(parseInt(8364))
		break;
	default :
		currencySymbol = "R$"
		break
	}
	

	
	/*if (document.isc.currency.value == "GBP"){
		currencySymbol = String.fromCharCode(parseInt(163))
	} else {currencySymbol = String.fromCharCode(parseInt(8364))}*/
	
		switch ( o.value.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 ? currencySymbol : "" ) + 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 ? currencySymbol : "" ) + 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 ? currencySymbol : "" ) + 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 ? currencySymbol : "" ) + oitem[3], oitem[4] );
				}
			}
			break;
	}
	//alert(categoryID);
	//PropertySearch_GetAreas( o.value.toString(), categoryID, includedGBSIDs, excludedGBSIDs, includedMasterGBSIDs, excludedMasterGBSIDs );
	PropertySearch_GetPropertyTypes( o.value.toString(), '1', '', '', '', '' );
	//PropertySearch_GetOffices( o.value.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;
	try{
		if ( opropertytypes ){
		//alert('LT'+listingType);	
		//alert('CAT'+categoryID);	
		//alert('incGBS'+includedGBSIDs);	
		//alert('exGBS'+excludedGBSIDs);	
		//alert('incMASTGBS'+includedMasterGBSIDs);	
		//alert('exMASTGBS'+excludedMasterGBSIDs);	


		
			var response = Searching.GetPropertyTypes( listingType, categoryID, includedGBSIDs, excludedGBSIDs, includedMasterGBSIDs, excludedMasterGBSIDs );						
			if ( response.value != null ){	
			//alert('CAT'+categoryID);					
				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]);
				}
			}
		}
	}catch(e){}
}

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;
			}
		}
	}
	//if ( HttpManager.Document.GetObject("subgbsid").value.length > 0 ){
	//	HttpManager.Document.GetObject("igid").value = HttpManager.Document.GetObject("subgbsid").value;
	//}else{
	//	HttpManager.Document.GetObject("igid").value = HttpManager.Document.GetObject("parentgbsid").value;
	//}
	return true;
}