function PropertyType (	id, pid, name ) {
	this.id	= id;
	this.pid = pid;
	this.name = name;
}
var	propTypes =	new	Array();
var	ptCount = 0;
function getPropTypeName(id) {
	for (var i=0; i<propTypes.length; i++ )
		if (propTypes[i].id == id )
			return propTypes[i].name;
}
function PropertyLocation(id, name) {
	this.id = id;
	this.name = name;
}
var propLocs = new Array();
function getPropLocName(id) {
	for (var i=0; i<propLocs.length; i++ )
		if (propLocs[i].id == id )
			return propLocs[i].name;
}
function setContent(form) {
	if (form.propCatId.value != "" )
		propSearchLoadPropTypes(form,1);
	if (form.cityId.value != "" )
		propSearchLoadLocations(form);
	setFields(form)
}
function setFields(form) {
	document.getElementById('prop_rent_row').style.display = 'none';
	document.getElementById('prop_buy_row').style.display = 'none';
	document.getElementById('prop_bed_row').style.display = 'none';
	document.getElementById('prop_bath_row').style.display = 'none';
	document.getElementById('prop_built_row').style.display = 'none';
	document.getElementById('prop_plot_row').style.display = 'none';
	document.getElementById('prop_age_row').style.display = 'none';

	if ( form.bedFlag[0].checked == true )
		onBedFlagChange(form, form.bedFlag[0].value);
	else if ( form.bedFlag[1].checked == true )
		onBedFlagChange(form, form.bedFlag[1].value);
	if ( form.bathFlag[0].checked == true )
		onBathFlagChange(form, form.bathFlag[0].value);
	else if ( form.bathFlag[1].checked == true )
		onBathFlagChange(form, form.bathFlag[1].value);
	if ( form.builtAreaFlag[0].checked == true )
		onBuiltAreaFlagChange(form, form.builtAreaFlag[0].value);
	else if ( form.builtAreaFlag[1].checked == true )
		onBuiltAreaFlagChange(form, form.builtAreaFlag[1].value);
	if ( form.plotAreaFlag[0].checked == true )
		onPlotAreaFlagChange(form, form.plotAreaFlag[0].value);
	else if ( form.plotAreaFlag[1].checked == true )
		onPlotAreaFlagChange(form, form.plotAreaFlag[1].value);
	if ( form.buyPriceFlag[0].checked == true )
		onBuyPriceFlagChange(form, form.buyPriceFlag[0].value);
	else if ( form.buyPriceFlag[1].checked == true )
		onBuyPriceFlagChange(form, form.buyPriceFlag[1].value);
	if ( form.rentPriceFlag[0].checked == true )
		onRentPriceFlagChange(form, form.rentPriceFlag[0].value);
	else if ( form.rentPriceFlag[1].checked == true )
		onRentPriceFlagChange(form, form.rentPriceFlag[1].value);
	
	if ( form.transactionType.value != "" && form.transactionType.value == 1 )
		document.getElementById('prop_buy_row').style.display = '';
	else if ( form.transactionType.value != "" && ( form.transactionType.value == 2 || form.transactionType.value == 3) )
		document.getElementById('prop_rent_row').style.display = '';
	
	if ( form.propCatId.value != "" && form.propCatId.value == 1 ) {
		document.getElementById('prop_bed_row').style.display = '';
		document.getElementById('prop_bath_row').style.display = '';
		document.getElementById('prop_built_row').style.display = '';
		document.getElementById('prop_age_row').style.display = '';
	} else if ( form.propCatId.value != "" && form.propCatId.value == 2 ) {
		document.getElementById('prop_bed_row').style.display = '';
		document.getElementById('prop_bath_row').style.display = '';
		document.getElementById('prop_built_row').style.display = '';
		document.getElementById('prop_plot_row').style.display = '';
		document.getElementById('prop_age_row').style.display = '';
	} else if ( form.propCatId.value != "" && form.propCatId.value == 3 ) {
		document.getElementById('prop_built_row').style.display = '';
		document.getElementById('prop_age_row').style.display = '';
	} else if ( form.propCatId.value != "" && form.propCatId.value == 4 ) {
		document.getElementById('prop_built_row').style.display = '';
		document.getElementById('prop_age_row').style.display = '';
	} else if ( form.propCatId.value != "" && form.propCatId.value == 5 ) {
		document.getElementById('prop_plot_row').style.display = '';
	} else if ( form.propCatId.value != "" && form.propCatId.value == 6 ) {
		document.getElementById('prop_built_row').style.display = '';
		document.getElementById('prop_age_row').style.display = '';
	}
}
function propSearchLoadPropTypes(form,type) {
	document.getElementById('prop_type_row').style.display = 'none';
	form.propTypeIds.value = "";
	form.propTypeNames.value = "";
	var index = 0;
	var catId = form.propCatId.options[form.propCatId.selectedIndex].value;
	if ( catId != "" ) {
		var typeText = ""
		for (var i=0; i<propTypes.length; i++ ) {
			if ( catId == propTypes[i].pid ) {
				var checkbox = document.createElement("checkbox");
				checkbox.setAttribute("name", "propTypeId");
				checkbox.setAttribute("value", propTypes[i].id);
				form.appendChild(checkbox);
				typeText = typeText + '<input type="checkbox" name="propTypeId" value="' + propTypes[i].id + '" onclick="propSearchCheckPropTypes(this);">';
				typeText = typeText + '<font class="sf_form_label_text"> ' + propTypes[i].name + '</font><br>';
			}
		}
		document.getElementById('prop_type_row').style.display = '';
		document.getElementById('prop_type_div').innerHTML = typeText;
	} else {
		document.getElementById('prop_type_div').innerHTML = "";
	}
	if ( type == 2 ) {
		form.bedFlag[0].checked = true;	onBedFlagChange(form, 0);
		form.bathFlag[0].checked = true; onBathFlagChange(form, 0);
		form.builtAreaFlag[0].checked = true; onBuiltAreaFlagChange(form, 0);
		form.plotAreaFlag[0].checked = true; onPlotAreaFlagChange(form, 0);
		form.buyPriceFlag[0].checked = true; onBuyPriceFlagChange(form, 0);
		form.rentPriceFlag[0].checked = true; onRentPriceFlagChange(form, 0);
		setFields(form);
	}
}
function propSearchCheckPropTypes(field) {
	var propTypeIds = document.propertyAdvSearchForm.propTypeIds.value;
	var propTypeNames = document.propertyAdvSearchForm.propTypeNames.value;
	if ( field.checked == true ) {
		if ( propTypeIds != "" ) {
			document.propertyAdvSearchForm.propTypeIds.value = propTypeIds + "," + field.value;
			document.propertyAdvSearchForm.propTypeNames.value = propTypeNames + "|" + getPropTypeName(field.value);
		} else {
			document.propertyAdvSearchForm.propTypeIds.value = field.value;
			document.propertyAdvSearchForm.propTypeNames.value = getPropTypeName(field.value);
		}
	} else if ( field.checked == false ) {
		if ( propTypeIds != "" ) {
			var ids = propTypeIds.split(",");
			var text = "";
			for ( i=0; i<ids.length; i++ ) {
				if ( ids[i] != field.value )
				text = text + ids[i] + ",";
			}
			if ( text.length>0 )
				text = text.substring(0, text.length-1);
			document.propertyAdvSearchForm.propTypeIds.value = text;
		}
		if ( propTypeNames != "" ) {
			var ids = propTypeNames.split("|");
			var text = "";
			for ( i=0; i<ids.length; i++ ) {
				if ( ids[i] != getPropTypeName(field.value) )
				text = text + ids[i] + "|";
			}
			if ( text.length>0 )
				text = text.substring(0, text.length-1);
			document.propertyAdvSearchForm.propTypeNames.value = text;
		}
	}
}
function propSearchLoadLocations(form) {
	document.getElementById('prop_loc_row').style.display = 'none';
	form.locationIds.value = "";
	form.locationNames.value = "";
	var cityId = form.cityId.value;
	if ( cityId != "" && cityId != 0 && cityId != 1 ) {
		sendAjaxReq(propSearchUpdateLocations, "getSearchPropLocations", cityId, "places.do")
		document.getElementById('prop_loc_row').style.display='';
		document.getElementById('prop_loc_div').innerHTML = '<font class="sf_form_label_text">&nbsp;&nbsp;Loading...</font>';
		form.search.disabled = true;
	} else {
		document.getElementById('prop_loc_div').innerHTML = "";
	}
}
function propSearchUpdateLocations(locationXML) {
	var content = locationXML.getElementsByTagName("content")[0];
	var total = content.getAttribute("total");
	if ( total > 0 ) {
		var locText = ""
		locText = locText + '<table border="0" cellpadding="0" cellspacing="0">' ;
		locText = locText + '<tr>';
		var location = content.getElementsByTagName("location");
		for (var i = 0 ; i < location.length ; i++)  {
			var item = location[i];
			var id = item.getElementsByTagName("id")[0].firstChild.nodeValue;
		    var name = item.getElementsByTagName("name")[0].firstChild.nodeValue;
		    propLocs[i] = new PropertyLocation(id, name);
		    
			locText = locText + '<td><input type="checkbox" name="locationId" value="' + id + '" onclick="propSearchCheckLocations(this);">';
			locText = locText + '<font class="sf_form_label_text"> ' + name + '</font></td>';
			locText = locText + '<td width="5"></td>';
			if ( (i+1)%2 == 0 )
				locText = locText + '</tr><tr>';
		}
		locText = locText + '<td><input type="checkbox" name="locationId" value="1" onclick="propSearchCheckLocations(this);">';
		locText = locText + '<font class="sf_form_label_text"> Other</font></td>';
		
		document.getElementById('prop_loc_row').style.display='';
		document.getElementById('prop_loc_div').innerHTML = locText;
	} else {
		document.getElementById('prop_loc_row').style.display='';
		document.getElementById('prop_loc_div').innerHTML = '<font class="sf_form_label_text">&nbsp;&nbsp;None</font>';
	}
	document.propertyAdvSearchForm.search.disabled = false;
}
function propSearchCheckLocations(field) {
	var locationIds = document.propertyAdvSearchForm.locationIds.value;
	var locationNames = document.propertyAdvSearchForm.locationNames.value;
	if ( field.checked == true ) {
		if ( locationIds != "" ) {
			document.propertyAdvSearchForm.locationIds.value = locationIds + "," + field.value;
			if ( field.value == 1 )
				document.propertyAdvSearchForm.locationNames.value = locationNames + "|Other" ;
			else
				document.propertyAdvSearchForm.locationNames.value = locationNames + "|" + getPropLocName(field.value);
		} else {
			document.propertyAdvSearchForm.locationIds.value = field.value;
			if ( field.value == 1 )
				document.propertyAdvSearchForm.locationNames.value = "Other";
			else
				document.propertyAdvSearchForm.locationNames.value = getPropLocName(field.value);
		}
	} else if ( field.checked == false ) {
		if ( locationIds != "" ) {
			var ids = locationIds.split(",");
			var idText = "";
			var nameText = "";
			for ( i=0; i<ids.length; i++ ) {
				if ( ids[i] != field.value ) {
					idText = idText + ids[i] + ",";
					if ( ids[i] == 1 )
						nameText = nameText + "Other" + "|";
					else
						nameText = nameText + getPropLocName(ids[i]) + "|";
				}
			}
			if ( idText.length>0 )
				idText = idText.substring(0, idText.length-1);
			document.propertyAdvSearchForm.locationIds.value = idText;
			if ( nameText.length>0 )
				nameText = nameText.substring(0, nameText.length-1);
			document.propertyAdvSearchForm.locationNames.value = nameText;
		}
	}
}
function onBedFlagChange(form, val) {
	if ( val == "1" )
		{form.bedMin.disabled=false; form.bedMax.disabled=false;}
	else
		{form.bedMin.selectedIndex=0; form.bedMax.selectedIndex=0; form.bedMin.disabled=true; form.bedMax.disabled=true;}
}
function onBathFlagChange(form, val) {
	if ( val == "1" )
		{form.bathMin.disabled=false; form.bathMax.disabled=false;}
	else
		{form.bathMin.selectedIndex=0; form.bathMax.selectedIndex=0; form.bathMin.disabled=true; form.bathMax.disabled=true;}
}
function onBuiltAreaFlagChange(form, val){
	if ( val == "1" )
		{form.builtAreaMin.disabled=false; form.builtAreaMax.disabled=false; form.builtMeasure.disabled=false;}
	else
		{form.builtAreaMin.value=""; form.builtAreaMax.value=""; form.builtMeasure.selectedIndex=0; form.builtAreaMin.disabled=true; form.builtAreaMax.disabled=true; form.builtMeasure.disabled=true;}
}
function onPlotAreaFlagChange(form, val) {
	if ( val == "1" )
		{form.plotAreaMin.disabled=false; form.plotAreaMax.disabled=false; form.plotMeasure.disabled=false;}
	else
		{form.plotAreaMin.value=""; form.plotAreaMax.value=""; form.plotMeasure.selectedIndex=0; form.plotAreaMin.disabled=true; form.plotAreaMax.disabled=true; form.plotMeasure.disabled=true;}
}
function onBuyPriceFlagChange(form, val) {
	if ( val == "1" )
		{form.buyPriceMin.disabled=false; form.buyPriceMax.disabled=false;}
	else
		{form.buyPriceMin.value=""; form.buyPriceMax.value=""; form.buyPriceMin.disabled=true; form.buyPriceMax.disabled=true;}
}
function onRentPriceFlagChange(form, val) {
	if ( val == "1" )
		{form.rentPriceMin.disabled=false; form.rentPriceMax.disabled=false;}
	else
		{form.rentPriceMin.value=""; form.rentPriceMax.value=""; form.rentPriceMin.disabled=true; form.rentPriceMax.disabled=true;}
}
function validateUserType(form, val, checked) {
	if ( checked == true ) {
		if ( val == 0 ) {
			form.postedType[1].checked = false; form.postedType[2].checked = false; form.postedType[3].checked = false;
		} else if ( val == 1 || val == 2 || val == 4 ) {
			if ( form.postedType[1].checked == true && form.postedType[2].checked == true && form.postedType[3].checked == true ) {
				form.postedType[0].checked = true; form.postedType[1].checked = false; form.postedType[2].checked = false; form.postedType[3].checked = false;
			} else {
				form.postedType[0].checked = false;
			}
		}
	}
	else if ( checked == false ) { }
}

function searchProperties(form) {
	if ( validatePropertyAdvSearchForm(form) ) {
		var status = true;
		var message = new Array();
		var messageCount = 0;
		var field = null;
		
		if ( form.propCatId.value == 1 || form.propCatId.value == 2 ) {
			if ( form.bedFlag[1].checked == true) {
				if ( trim(form.bedMin.value) == "" ) {
					message[messageCount++] = propSearchBedMinReq;
					if ( field == null ) field = form.bedMin;		
				}
				if ( trim(form.bedMax.value) == "" ) {
					message[messageCount++] = propSearchBedMaxReq;
					if ( field == null ) field = form.bedMax;		
				}
				if ( trim(form.bedMin.value) != "" &&  trim(form.bedMax.value) != "" && form.bedMin.value >= form.bedMax.value ) {   
					message[messageCount++] = propSearchBedRanInv;
					if ( field == null ) field = form.bedMin;		
				}
			}
			if ( form.bathFlag[1].checked == true) {
				if ( trim(form.bathMin.value) == "" ) {
					message[messageCount++] = propSearchBathMinReq;
					if ( field == null ) field = form.bathMin;		
				}
				if ( trim(form.bathMax.value) == "" ) {
					message[messageCount++] = propSearchBathMaxReq;
					if ( field == null ) field = form.bathMax;		
				}
				if ( trim(form.bathMin.value) != "" &&  trim(form.bathMax.value) != "" && form.bathMin.value >= form.bathMax.value ) {   
					message[messageCount++] = propSearchBathRanInv;
					if ( field == null ) field = form.bathMin;		
				}
			}
		}
		if ( form.propCatId.value == 1 || form.propCatId.value == 2 || form.propCatId.value == 3 || form.propCatId.value == 4 || form.propCatId.value == 6 ) {
			if ( form.builtAreaFlag[1].checked == true) {
				if ( trim(form.builtAreaMin.value) == "" ) {
					message[messageCount++] = propSearchBuiltMinReq;
					if ( field == null ) field = form.builtAreaMin;		
				}
				if ( trim(form.builtAreaMax.value) == "" ) {
					message[messageCount++] = propSearchBuiltMaxReq;
					if ( field == null ) field = form.builtAreaMax;		
				}
				if ( trim(form.builtAreaMin.value) != "" &&  trim(form.builtAreaMax.value) != "" && parseFloat(form.builtAreaMin.value) >= parseFloat(form.builtAreaMax.value) ) {   
					message[messageCount++] = propSearchBuiltRanInv;
					if ( field == null ) field = form.builtAreaMin;		
				}
			}
		}
		if ( form.propCatId.value == 2 || form.propCatId.value == 5 ) {
			if ( form.plotAreaFlag[1].checked == true) {
				if ( trim(form.plotAreaMin.value) == "" ) {
					message[messageCount++] = propSearchPlotMinReq;
					if ( field == null ) field = form.plotAreaMin;		
				}
				if ( trim(form.plotAreaMax.value) == "" ) {
					message[messageCount++] = propSearchPlotMaxReq;
					if ( field == null ) field = form.plotAreaMax;		
				}
				if ( trim(form.plotAreaMin.value) != "" &&  trim(form.plotAreaMax.value) != "" && parseFloat(form.plotAreaMin.value) >= parseFloat(form.plotAreaMax.value) ) {   
					message[messageCount++] = propSearchPlotRanInv;
					if ( field == null ) field = form.plotAreaMin;		
				}
			}
		}
		if ( form.transactionType.value == 1 ) {
			if ( form.buyPriceFlag[1].checked == true) {
				if ( trim(form.buyPriceMin.value) == "" ) {
					message[messageCount++] = propSearchBudgMinReq;
					if ( field == null ) field = form.buyPriceMin;		
				}
				if ( trim(form.buyPriceMax.value) == "" ) {
					message[messageCount++] = propSearchBudgMaxReq;
					if ( field == null ) field = form.buyPriceMax;		
				}
				if ( trim(form.buyPriceMin.value) != "" &&  trim(form.buyPriceMax.value) != "" && parseFloat(form.buyPriceMin.value) >= parseFloat(form.buyPriceMax.value) ) {   
					message[messageCount++] = propSearchBudgRanInv;
					if ( field == null ) field = form.buyPriceMin;		
				}
			}
		}
		if ( form.transactionType.value == 2 || form.transactionType.value == 3 ) {
			if ( form.rentPriceFlag[1].checked == true) {
				if ( trim(form.rentPriceMin.value) == "" ) {
					message[messageCount++] = propSearchRentMinReq;
					if ( field == null ) field = form.rentPriceMin;		
				}
				if ( trim(form.rentPriceMax.value) == "" ) {
					message[messageCount++] = propSearchRentMaxReq;
					if ( field == null ) field = form.rentPriceMax;		
				}
				if ( trim(form.rentPriceMin.value) != "" &&  trim(form.rentPriceMax.value) != "" && parseFloat(form.rentPriceMin.value) >= parseFloat(form.rentPriceMax.value) ) {   
					message[messageCount++] = propSearchRentRanInv;
					if ( field == null ) field = form.rentPriceMin;		
				}
			}
		}
		if ( messageCount > 0 ) {
			showAlert(message); 
			field.focus(); status = false; return false;
		}
		if ( status == true ) {
			//disableAction(form);
			form.submit();
		}
	}
}

