function UserLocation(id, name) {
	this.id = id;
	this.name = name;
}
var userLocs = new Array();
function getUserLocName(id) {
	for (var i=0; i<userLocs.length; i++ )
		if (userLocs[i].id == id )
			return userLocs[i].name;
}
function setContent(form) {
	
	if ( form.userTypeId[0].checked == true ) {
		onUserTypeClick(form, form.userTypeId[0].value);
	} else if ( form.userTypeId[1].checked == true ) {
		onUserTypeClick(form, form.userTypeId[1].value);
	}
}
function userSearchUpdateCities(cityXML) {
	document.userAdvSearchForm.cityId.disabled = false;
	document.userAdvSearchForm.search.disabled = false;
	var cityIdsField = document.userAdvSearchForm.cityId;
	removePlacesOptionData(cityIdsField);
	var content = cityXML.getElementsByTagName("content")[0];
	var total = content.getAttribute("total");
	cityIdsField.appendChild(createPlaceOption(userSearchCityNone,""));
	if ( total > 0 ) {
		var city = content.getElementsByTagName("city");
		for (var i = 0 ; i < city.length ; i++) {
			var item = city[i];
			var id = item.getElementsByTagName("id")[0].firstChild.nodeValue;
		    var name = item.getElementsByTagName("name")[0].firstChild.nodeValue;
			if ( cityIdsField )
				cityIdsField.appendChild(createPlaceOption(name,id));
		}
	}
}
function removePlacesOptionData(fieldName) {
	if ( fieldName) {
		if ( fieldName.options)
			if ( fieldName.options.length > 0 )
				fieldName.options.length = 0 ;
	}
}
function createPlaceOption(optionText, optionValue) {
	var option = document.createElement("option");
	option.setAttribute("value",optionValue);
	var text=document.createTextNode(optionText);
	option.appendChild(text);
	return option;
}
function onUserTypeClick(form, val){
	if ( val == 1 ) {
		sendAjaxReq(userSearchUpdateCities, "getSearchBuilderCities", form.userTypeId[0].value, "places.do");
		form.userTypeName.value = "Builders";
	} else if ( val == 2 ) {
		sendAjaxReq(userSearchUpdateCities, "getSearchAgentCities", form.userTypeId[1].value, "places.do");	
		form.userTypeName.value = "Agents";
	}
	form.cityId.disabled = true;
	form.search.disabled = true;
	document.getElementById('user_loc_div').innerHTML = '';
	document.getElementById('user_loc_row').style.display='none';
	form.locationIds.value = "";
	form.locationNames.value = "";
}
function userSearchLoadLocations(form) {
	document.getElementById('user_loc_row').style.display = 'none';
	form.locationIds.value = "";
	form.locationNames.value = "";
	var cityId = form.cityId.value;
	if ( cityId != "" && cityId != 0 && cityId != 1 ) {
		if ( form.userTypeId[0].checked == true )
			sendAjaxReq(userSearchUpdateLocations, "getSearchBuilderLocations", cityId, "places.do")
		else if ( form.userTypeId[1].checked == true )
			sendAjaxReq(userSearchUpdateLocations, "getSearchAgentLocations", cityId, "places.do")
		
		document.getElementById('user_loc_row').style.display='';
		document.getElementById('user_loc_div').innerHTML = '<font class="sf_form_label_text">&nbsp;&nbsp;Loading...</font>';
		form.search.disabled = true;
	} else {
		document.getElementById('user_loc_div').innerHTML = "";
	}
}
function userSearchUpdateLocations(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;
		    userLocs[i] = new UserLocation(id, name);
		    
			locText = locText + '<td><input type="checkbox" name="locationId" value="' + id + '" onclick="userSearchCheckLocations(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="userSearchCheckLocations(this);">';
		locText = locText + '<font class="sf_form_label_text"> Other</font></td>';
		document.getElementById('user_loc_row').style.display='';
		document.getElementById('user_loc_div').innerHTML = locText;
	} else {
		document.getElementById('user_loc_row').style.display='';
		document.getElementById('user_loc_div').innerHTML = '<font class="sf_form_label_text">&nbsp;&nbsp;None</font>';
	}
	document.userAdvSearchForm.search.disabled = false;
}
function userSearchCheckLocations(field) {
	var locationIds = document.userAdvSearchForm.locationIds.value;
	var locationNames = document.userAdvSearchForm.locationNames.value;
	if ( field.checked == true ) {
		if ( locationIds != "" ) {
			document.userAdvSearchForm.locationIds.value = locationIds + "," + field.value;
			document.userAdvSearchForm.locationNames.value = locationNames + "|" + getUserLocName(field.value);
			if ( field.value == 1 )
				document.userAdvSearchForm.locationNames.value = locationNames + "|Other";
			else
				document.userAdvSearchForm.locationNames.value = locationNames + "|" + getUserLocName(field.value);
		} else {
			document.userAdvSearchForm.locationIds.value = field.value;
			
			if ( field.value == 1 )
				document.userAdvSearchForm.locationNames.value = "Other";
			else
				document.userAdvSearchForm.locationNames.value = getUserLocName(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 + getUserLocName(ids[i]) + "|";
				}
			}
			if ( idText.length>0 )
				idText = idText.substring(0, idText.length-1);
			document.userAdvSearchForm.locationIds.value = idText;
			if ( nameText.length>0 )
				nameText = nameText.substring(0, nameText.length-1);
			document.userAdvSearchForm.locationNames.value = nameText;
		}
	}
}
function searchUsers(form) {
	if ( validateUserAdvSearchForm(form) ) {
		//disableAction(form);
		form.submit();
	}
	return false;
}


